https://www.mediawiki.org/wiki/Special:Code/MediaWiki/112180
Revision: 112180
Author: awjrichards
Date: 2012-02-23 02:03:20 +0000 (Thu, 23 Feb 2012)
Log Message:
-----------
Added the ability to dynamically generate mobile URLs using a 'mobile url
template'. The functionality is now present for handling mobile URL host names,
as well as a stub for handling paths. This has not yet been made use of
anywhere in the MobileFrontend code. Also added corresponding tests.
Modified Paths:
--------------
trunk/extensions/MobileFrontend/MobileFrontend.body.php
trunk/extensions/MobileFrontend/MobileFrontend.php
trunk/extensions/MobileFrontend/tests/MobileFrontendTest.php
Modified: trunk/extensions/MobileFrontend/MobileFrontend.body.php
===================================================================
--- trunk/extensions/MobileFrontend/MobileFrontend.body.php 2012-02-23
01:25:14 UTC (rev 112179)
+++ trunk/extensions/MobileFrontend/MobileFrontend.body.php 2012-02-23
02:03:20 UTC (rev 112180)
@@ -1327,6 +1327,101 @@
return true;
}
+ /**
+ * Take a URL and return a copy that conforms to the mobile URL template
+ * @param $url string
+ * @return string
+ */
+ public function getMobileUrl( $url ) {
+ global $wgMobileUrlTemplate;
+ $parsedUrl = wfParseUrl( $url );
+ $this->updateMobileUrlHost( $parsedUrl );
+ $this->updateMobileUrlPath( $parsedUrl );
+ return wfAssembleUrl( $parsedUrl );
+ }
+
+ /**
+ * Update host of given URL to conform to mobile URL template.
+ * @param $parsedUrl array
+ * Result of parseUrl() or wfParseUrl()
+ */
+ protected function updateMobileUrlHost( &$parsedUrl ) {
+ $mobileUrlHostTemplate = $this->parseMobileUrlTemplate( 'host'
);
+ if ( !strlen( $mobileUrlHostTemplate )) {
+ return;
+ }
+
+ $parsedHostParts = explode( ".", $parsedUrl[ 'host' ] );
+ $templateHostParts = explode( ".", $mobileUrlHostTemplate );
+ $targetHostParts = array();
+
+ foreach ( $templateHostParts as $key => $templateHostPart ) {
+ if ( strstr( $templateHostPart, '%h' ) ) {
+ $parsedHostPartKey = substr( $templateHostPart,
2 );
+ $targetHostParts[ $key ] = $parsedHostParts[
$parsedHostPartKey ];
+ } elseif ( isset( $parsedHostParts[ $key ] )
+ && $templateHostPart ==
$parsedHostParts[ $key ] ) {
+ $targetHostParts = $parsedHostParts;
+ break;
+ } else {
+ $targetHostParts[ $key ] = $templateHostPart;
+ }
+ }
+
+ $parsedUrl[ 'host' ] = implode( ".", $targetHostParts );
+ }
+
+ /**
+ * Update path of given URL to conform to mobile URL template.
+ *
+ * This is just a stub at the moment; does nothing. Once this does
+ * something, be sure to update documentation for $wgMobileUrlTemplate.
+ * @param $parsedUrl array
+ * Result of parseUrl() or wfParseUrl()
+ */
+ protected function updateMobileUrlPath( &$parsedUrl ) {
+ $mobileUrlHostTemplate = $this->parseMobileUrlTemplate( 'path'
);
+ if ( !strlen( $mobileUrlHostTemplate )) {
+ return;
+ }
+ return;
+ }
+
+ /**
+ * Parse mobile URL template into its host and path components.
+ *
+ * Optionally specify which portion of the template you want returned.
+ * @param $part string
+ * @return Mixed
+ */
+ public function parseMobileUrlTemplate( $part = null ) {
+ global $wgMobileUrlTemplate;
+
+ $pathStartPos = strpos( $wgMobileUrlTemplate, '/' );
+
+ /**
+ * This if/else block exists because of an annoying aspect of
substr()
+ * Even if you pass 'null' or 'false' into the 'length' param,
it
+ * will return an empty string.
+ * http://www.stopgeek.com/wp-content/uploads/2007/07/sense.jpg
+ */
+ if ( $pathStartPos === false ) {
+ $host = substr( $wgMobileUrlTemplate, 0 );
+ } else {
+ $host = substr( $wgMobileUrlTemplate, 0, $pathStartPos
);
+ }
+
+ $path = substr( $wgMobileUrlTemplate, $pathStartPos );
+
+ if ( $part == 'host' ) {
+ return $host;
+ } elseif( $part == 'path' ) {
+ return $path;
+ } else {
+ return array( 'host' => $host, 'path' => $path );
+ }
+ }
+
public function getVersion() {
return __CLASS__ . ': $Id$';
}
Modified: trunk/extensions/MobileFrontend/MobileFrontend.php
===================================================================
--- trunk/extensions/MobileFrontend/MobileFrontend.php 2012-02-23 01:25:14 UTC
(rev 112179)
+++ trunk/extensions/MobileFrontend/MobileFrontend.php 2012-02-23 02:03:20 UTC
(rev 112180)
@@ -78,6 +78,28 @@
$wgMobileDomain = '.m.';
/**
+ * Template for mobile URLs.
+ *
+ * This will be used to transcode regular URLs into mobile URLs for the
+ * mobile view.
+ *
+ * You can either statically or dynamically create the host-portion of your
+ * mobile URL. To statically create it, just set $wgMobileUrlTemplate to
+ * the static hostname. For example:
+ * $wgMobileUrlTemplate = "mobile.mydomain.com";
+ *
+ * Alternatively, the host definition can include placeholders for different
+ * parts of the 'host' section of a URL. The placeholders are denoted by '%h'
+ * and followed with a digit that maps to the position of a host-part of the
+ * original, non-mobile URL. Take the host 'en.wikipedia.org' for example.
+ * '%h0' maps to 'en', '%h1' maps to 'wikipedia', and '%h2' maps to 'org'.
+ * So, if you wanted a mobile URL scheme that turned "en.wikipedia.org" into
+ * "en.m.wikipedia.org", your URL template would look like:
+ * %h0.m.%h1.%h2
+ */
+$wgMobileUrlTemplate = '%h0.m.%h1.%h2';
+
+/**
* URL for script used to disable mobile site
* (protocol, host, optional port; path portion)
*
Modified: trunk/extensions/MobileFrontend/tests/MobileFrontendTest.php
===================================================================
--- trunk/extensions/MobileFrontend/tests/MobileFrontendTest.php
2012-02-23 01:25:14 UTC (rev 112179)
+++ trunk/extensions/MobileFrontend/tests/MobileFrontendTest.php
2012-02-23 02:03:20 UTC (rev 112180)
@@ -61,4 +61,18 @@
$sendXDeviceVaryHeader->invokeArgs( $wgExtMobileFrontend,
array() );
$this->assertEquals( $_SERVER['HTTP_X_DEVICE'],
$wgRequest->response()->getheader( 'X-Device' ) );
}
+
+ public function testGetMobileUrl() {
+ global $wgMobileUrlTemplate, $wgExtMobileFrontend;
+ $wgMobileUrlTemplate = "%h0.m.%h1.%h2";
+ $this->assertEquals( 'http://en.m.wikipedia.org/wiki/Article',
$wgExtMobileFrontend->getMobileUrl( 'http://en.wikipedia.org/wiki/Article' ) );
+ }
+
+ public function testParseMobileUrlTemplate() {
+ global $wgMobileUrlTemplate, $wgExtMobileFrontend;
+ $wgMobileUrlTemplate = "%h0.m.%h1.%h2/path/morepath";
+ $this->assertEquals( '%h0.m.%h1.%h2',
$wgExtMobileFrontend->parseMobileUrlTemplate( 'host' ) );
+ $this->assertEquals( '/path/morepath',
$wgExtMobileFrontend->parseMobileUrlTemplate( 'path' ) );
+ $this->assertEquals( array( 'host' => '%h0.m.%h1.%h2', 'path'
=> '/path/morepath' ), $wgExtMobileFrontend->parseMobileUrlTemplate());
+ }
}
\ No newline at end of file
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs