r112380 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112379‎ | r112380 | r112381 >
Date:01:42, 25 February 2012
Author:awjrichards
Status:ok (Comments)
Tags:
Comment:
Implement dynamic mobile url functionality (replacing the old way of building the mobile URL). added ability to force https when building the mobile URL. At the moment this has only been tested against unit tests.
Modified paths:
  • /trunk/extensions/MobileFrontend/MobileFrontend.body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/MobileFrontend/MobileFrontend.body.php
@@ -126,7 +126,7 @@
127127 * @throws HttpError
128128 */
129129 public function testCanonicalRedirect( $request, $title, $output ) {
130 - global $wgUsePathInfo, $wgMobileDomain;
 130+ global $wgUsePathInfo;
131131 $xDevice = isset( $_SERVER['HTTP_X_DEVICE'] ) ? $_SERVER['HTTP_X_DEVICE'] : '';
132132 if ( empty( $xDevice ) ) {
133133 return true; // Let the redirect happen
@@ -161,15 +161,7 @@
162162 }
163163 throw new HttpError( 500, $message );
164164 } else {
165 - $parsedUrl = wfParseUrl( $targetUrl );
166 - if ( stristr( $parsedUrl['host'], $wgMobileDomain ) === false ) {
167 - $hostParts = explode( '.', $parsedUrl['host'] );
168 - $parsedUrl['host'] = $hostParts[0] . $wgMobileDomain . $hostParts[1] . '.' . $hostParts[2];
169 - }
170 - $fragmentDelimiter = ( !empty( $parsedUrl['fragment'] ) ) ? '#' : '';
171 - $queryDelimiter = ( !empty( $parsedUrl['query'] ) ) ? '?' : '';
172 - $targetUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . $parsedUrl['path']
173 - . $queryDelimiter . $parsedUrl['query'] . $fragmentDelimiter . $parsedUrl['fragment'];
 165+ $targetUrl = $this->getMobileUrl( $targetUrl );
174166 $output->setSquidMaxage( 1200 );
175167 $output->redirect( $targetUrl, '301' );
176168 }
@@ -215,7 +207,7 @@
216208 }
217209
218210 public function getMsg() {
219 - global $wgUser, $wgContLang, $wgRequest, $wgServer, $wgMobileRedirectFormAction, $wgMobileDomain, $wgOut, $wgLanguageCode;
 211+ global $wgUser, $wgContLang, $wgRequest, $wgServer, $wgMobileRedirectFormAction, $wgOut, $wgLanguageCode;
220212 wfProfileIn( __METHOD__ );
221213
222214 self::$disableImagesURL = $wgRequest->escapeAppendQuery( 'disableImages=1' );
@@ -269,21 +261,7 @@
270262 unset( $tmp );
271263 $nt = Title::newFromText( $l );
272264 if ( $nt ) {
273 - $parsedUrl = wfParseUrl( $nt->getFullURL() );
274 - if ( stristr( $parsedUrl['host'], $wgMobileDomain ) === false ) {
275 - $hostParts = explode( '.', $parsedUrl['host'] );
276 - $parsedUrl['host'] = $hostParts[0] . $wgMobileDomain . $hostParts[1] . '.' . $hostParts[2];
277 - }
278 - $fragmentDelimiter = ( isset( $parsedUrl['fragment'] ) && $parsedUrl['fragment'] !== null ) ? '#' : '';
279 - $queryDelimiter = ( isset( $parsedUrl['query'] ) && $parsedUrl['query'] !== null ) ? '?' : '';
280 -
281 - $languageUrl = $parsedUrl['scheme'] . $parsedUrl['delimiter'] . $parsedUrl['host'] . $parsedUrl['path'];
282 - if ( isset( $parsedUrl['query'] ) ) {
283 - $languageUrl .= $queryDelimiter . $parsedUrl['query'];
284 - }
285 - if ( isset( $parsedUrl['fragment'] ) ) {
286 - $languageUrl .= $fragmentDelimiter . $parsedUrl['fragment'];
287 - }
 265+ $languageUrl = $this->getMobileUrl( $nt->getFullURL() );
288266 $languageUrls[] = array(
289267 'href' => $languageUrl,
290268 'text' => ( $wgContLang->getLanguageName( $nt->getInterwiki() ) != ''
@@ -298,10 +276,9 @@
299277
300278 self::$languageUrls = $languageUrls;
301279
302 - $nonMobileServerBaseURL = str_replace( $wgMobileDomain, '.', $wgServer );
303280 self::$mobileRedirectFormAction = ( $wgMobileRedirectFormAction !== false )
304281 ? $wgMobileRedirectFormAction
305 - : "{$nonMobileServerBaseURL}/w/mobileRedirect.php";
 282+ : "{$wgServer}/w/mobileRedirect.php";
306283
307284 self::$mainPageUrl = Title::newMainPage()->getLocalUrl();
308285 self::$randomPageUrl = $this->getRelativeURL( SpecialPage::getTitleFor( 'Randompage' )->getLocalUrl() );
@@ -310,58 +287,23 @@
311288 }
312289
313290 /**
314 - * @param $parsedUrl wfParseUrl Array
315 - * @return string
316 - */
317 - public function parsePageRedirect( $parsedUrl ) {
318 - global $wgMobileDomain;
319 - wfProfileIn( __METHOD__ );
320 - $redirect = '';
321 - $hostParts = explode( '.', $parsedUrl['host'] );
322 - $parsedUrl['host'] = $hostParts[0] . $wgMobileDomain . $hostParts[1] . '.' . $hostParts[2];
323 - $fragmentDelimiter = ( !empty( $parsedUrl['fragment'] ) ) ? '#' : '';
324 - $queryDelimiter = ( !empty( $parsedUrl['query'] ) ) ? '?' : '';
325 - $redirect = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . $parsedUrl['path'];
326 - if ( isset( $parsedUrl['query'] ) ) {
327 - $redirect .= $queryDelimiter . $parsedUrl['query'];
328 - }
329 - if ( isset( $parsedUrl['fragment'] ) ) {
330 - $redirect .= $fragmentDelimiter . $parsedUrl['fragment'];
331 - }
332 - wfProfileOut( __METHOD__ );
333 - return $redirect;
334 - }
335 -
336 - /**
337291 * @param $out OutputPage
338292 * @param $redirect
339293 * @param $code
340294 * @return bool
341295 */
342296 public function beforePageRedirect( $out, &$redirect, &$code ) {
343 - global $wgMobileDomain;
344297 wfProfileIn( __METHOD__ );
345298 if ( $out->getTitle()->isSpecial( 'Userlogin' ) ) {
346299 $xDevice = isset( $_SERVER['HTTP_X_DEVICE'] ) ? $_SERVER['HTTP_X_DEVICE'] : '';
347300 if ( $xDevice ) {
348 - $parsedUrl = wfParseUrl( $redirect );
349 - if ( stristr( $parsedUrl['host'], $wgMobileDomain ) === false ) {
350 - $hostParts = explode( '.', $parsedUrl['host'] );
351 - $parsedUrl['host'] = $hostParts[0] . $wgMobileDomain . $hostParts[1] . '.' . $hostParts[2];
352 - }
353 - if ( $parsedUrl['scheme'] == 'http' ) {
354 - $parsedUrl['scheme'] = 'https';
355 - }
356 -
357 - $redirect = $this->parsePageRedirect( $parsedUrl );
 301+ $forceHttps = true;
 302+ $redirect = $this->getMobileUrl( $redirect, $forceHttps );
358303 }
359304 } else if ( $out->getTitle()->isSpecial( 'Randompage' ) ) {
360305 $xDevice = isset( $_SERVER['HTTP_X_DEVICE'] ) ? $_SERVER['HTTP_X_DEVICE'] : '';
361306 if ( $xDevice ) {
362 - $parsedUrl = wfParseUrl( $redirect );
363 - if ( stristr( $parsedUrl['host'], $wgMobileDomain ) === false ) {
364 - $redirect = $this->parsePageRedirect( $parsedUrl );
365 - }
 307+ $redirect = $this->getMobileUrl( $redirect );
366308 }
367309 }
368310 wfProfileOut( __METHOD__ );
@@ -1326,11 +1268,13 @@
13271269 * @param $url string
13281270 * @return string
13291271 */
1330 - public function getMobileUrl( $url ) {
1331 - global $wgMobileUrlTemplate;
 1272+ public function getMobileUrl( $url, $forceHttps = false ) {
13321273 $parsedUrl = wfParseUrl( $url );
13331274 $this->updateMobileUrlHost( $parsedUrl );
13341275 $this->updateMobileUrlPath( $parsedUrl );
 1276+ if ( $forceHttps ) {
 1277+ $parsedUrl[ 'scheme' ] = 'https';
 1278+ }
13351279 return wfAssembleUrl( $parsedUrl );
13361280 }
13371281

Comments

#Comment by Preilly (talk | contribs)   19:31, 6 March 2012

Why is it currently setting, "$forceHttps = true;" in the beforePageRedirect method?

Status & tagging log