r112180 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112179‎ | r112180 | r112181 >
Date:02:03, 23 February 2012
Author:awjrichards
Status:ok
Tags:
Comment:
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 (modified) (history)
  • /trunk/extensions/MobileFrontend/MobileFrontend.php (modified) (history)
  • /trunk/extensions/MobileFrontend/tests/MobileFrontendTest.php (modified) (history)

Diff [purge]

Index: trunk/extensions/MobileFrontend/tests/MobileFrontendTest.php
@@ -61,4 +61,18 @@
6262 $sendXDeviceVaryHeader->invokeArgs( $wgExtMobileFrontend, array() );
6363 $this->assertEquals( $_SERVER['HTTP_X_DEVICE'], $wgRequest->response()->getheader( 'X-Device' ) );
6464 }
 65+
 66+ public function testGetMobileUrl() {
 67+ global $wgMobileUrlTemplate, $wgExtMobileFrontend;
 68+ $wgMobileUrlTemplate = "%h0.m.%h1.%h2";
 69+ $this->assertEquals( 'http://en.m.wikipedia.org/wiki/Article', $wgExtMobileFrontend->getMobileUrl( 'http://en.wikipedia.org/wiki/Article' ) );
 70+ }
 71+
 72+ public function testParseMobileUrlTemplate() {
 73+ global $wgMobileUrlTemplate, $wgExtMobileFrontend;
 74+ $wgMobileUrlTemplate = "%h0.m.%h1.%h2/path/morepath";
 75+ $this->assertEquals( '%h0.m.%h1.%h2', $wgExtMobileFrontend->parseMobileUrlTemplate( 'host' ) );
 76+ $this->assertEquals( '/path/morepath', $wgExtMobileFrontend->parseMobileUrlTemplate( 'path' ) );
 77+ $this->assertEquals( array( 'host' => '%h0.m.%h1.%h2', 'path' => '/path/morepath' ), $wgExtMobileFrontend->parseMobileUrlTemplate());
 78+ }
6579 }
\ No newline at end of file
Index: trunk/extensions/MobileFrontend/MobileFrontend.body.php
@@ -1327,6 +1327,101 @@
13281328 return true;
13291329 }
13301330
 1331+ /**
 1332+ * Take a URL and return a copy that conforms to the mobile URL template
 1333+ * @param $url string
 1334+ * @return string
 1335+ */
 1336+ public function getMobileUrl( $url ) {
 1337+ global $wgMobileUrlTemplate;
 1338+ $parsedUrl = wfParseUrl( $url );
 1339+ $this->updateMobileUrlHost( $parsedUrl );
 1340+ $this->updateMobileUrlPath( $parsedUrl );
 1341+ return wfAssembleUrl( $parsedUrl );
 1342+ }
 1343+
 1344+ /**
 1345+ * Update host of given URL to conform to mobile URL template.
 1346+ * @param $parsedUrl array
 1347+ * Result of parseUrl() or wfParseUrl()
 1348+ */
 1349+ protected function updateMobileUrlHost( &$parsedUrl ) {
 1350+ $mobileUrlHostTemplate = $this->parseMobileUrlTemplate( 'host' );
 1351+ if ( !strlen( $mobileUrlHostTemplate )) {
 1352+ return;
 1353+ }
 1354+
 1355+ $parsedHostParts = explode( ".", $parsedUrl[ 'host' ] );
 1356+ $templateHostParts = explode( ".", $mobileUrlHostTemplate );
 1357+ $targetHostParts = array();
 1358+
 1359+ foreach ( $templateHostParts as $key => $templateHostPart ) {
 1360+ if ( strstr( $templateHostPart, '%h' ) ) {
 1361+ $parsedHostPartKey = substr( $templateHostPart, 2 );
 1362+ $targetHostParts[ $key ] = $parsedHostParts[ $parsedHostPartKey ];
 1363+ } elseif ( isset( $parsedHostParts[ $key ] )
 1364+ && $templateHostPart == $parsedHostParts[ $key ] ) {
 1365+ $targetHostParts = $parsedHostParts;
 1366+ break;
 1367+ } else {
 1368+ $targetHostParts[ $key ] = $templateHostPart;
 1369+ }
 1370+ }
 1371+
 1372+ $parsedUrl[ 'host' ] = implode( ".", $targetHostParts );
 1373+ }
 1374+
 1375+ /**
 1376+ * Update path of given URL to conform to mobile URL template.
 1377+ *
 1378+ * This is just a stub at the moment; does nothing. Once this does
 1379+ * something, be sure to update documentation for $wgMobileUrlTemplate.
 1380+ * @param $parsedUrl array
 1381+ * Result of parseUrl() or wfParseUrl()
 1382+ */
 1383+ protected function updateMobileUrlPath( &$parsedUrl ) {
 1384+ $mobileUrlHostTemplate = $this->parseMobileUrlTemplate( 'path' );
 1385+ if ( !strlen( $mobileUrlHostTemplate )) {
 1386+ return;
 1387+ }
 1388+ return;
 1389+ }
 1390+
 1391+ /**
 1392+ * Parse mobile URL template into its host and path components.
 1393+ *
 1394+ * Optionally specify which portion of the template you want returned.
 1395+ * @param $part string
 1396+ * @return Mixed
 1397+ */
 1398+ public function parseMobileUrlTemplate( $part = null ) {
 1399+ global $wgMobileUrlTemplate;
 1400+
 1401+ $pathStartPos = strpos( $wgMobileUrlTemplate, '/' );
 1402+
 1403+ /**
 1404+ * This if/else block exists because of an annoying aspect of substr()
 1405+ * Even if you pass 'null' or 'false' into the 'length' param, it
 1406+ * will return an empty string.
 1407+ * http://www.stopgeek.com/wp-content/uploads/2007/07/sense.jpg
 1408+ */
 1409+ if ( $pathStartPos === false ) {
 1410+ $host = substr( $wgMobileUrlTemplate, 0 );
 1411+ } else {
 1412+ $host = substr( $wgMobileUrlTemplate, 0, $pathStartPos );
 1413+ }
 1414+
 1415+ $path = substr( $wgMobileUrlTemplate, $pathStartPos );
 1416+
 1417+ if ( $part == 'host' ) {
 1418+ return $host;
 1419+ } elseif( $part == 'path' ) {
 1420+ return $path;
 1421+ } else {
 1422+ return array( 'host' => $host, 'path' => $path );
 1423+ }
 1424+ }
 1425+
13311426 public function getVersion() {
13321427 return __CLASS__ . ': $Id$';
13331428 }
Index: trunk/extensions/MobileFrontend/MobileFrontend.php
@@ -78,6 +78,28 @@
7979 $wgMobileDomain = '.m.';
8080
8181 /**
 82+ * Template for mobile URLs.
 83+ *
 84+ * This will be used to transcode regular URLs into mobile URLs for the
 85+ * mobile view.
 86+ *
 87+ * You can either statically or dynamically create the host-portion of your
 88+ * mobile URL. To statically create it, just set $wgMobileUrlTemplate to
 89+ * the static hostname. For example:
 90+ * $wgMobileUrlTemplate = "mobile.mydomain.com";
 91+ *
 92+ * Alternatively, the host definition can include placeholders for different
 93+ * parts of the 'host' section of a URL. The placeholders are denoted by '%h'
 94+ * and followed with a digit that maps to the position of a host-part of the
 95+ * original, non-mobile URL. Take the host 'en.wikipedia.org' for example.
 96+ * '%h0' maps to 'en', '%h1' maps to 'wikipedia', and '%h2' maps to 'org'.
 97+ * So, if you wanted a mobile URL scheme that turned "en.wikipedia.org" into
 98+ * "en.m.wikipedia.org", your URL template would look like:
 99+ * %h0.m.%h1.%h2
 100+ */
 101+$wgMobileUrlTemplate = '%h0.m.%h1.%h2';
 102+
 103+/**
82104 * URL for script used to disable mobile site
83105 * (protocol, host, optional port; path portion)
84106 *

Follow-up revisions

RevisionCommit summaryAuthorDate
r112207Follow-up r106297: spacing fixesmaxsem15:23, 23 February 2012

Status & tagging log