r98547 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r98546‎ | r98547 | r98548 >
Date:18:49, 30 September 2011
Author:preilly
Status:ok (Comments)
Tags:todo 
Comment:
fix for bug 30772 - mobile wikipedia commonly uses non-canonical urls that do not get squid/varnish/whatever cache purged
Modified paths:
  • /trunk/extensions/MobileFrontend/MobileFrontend.php (modified) (history)

Diff [purge]

Index: trunk/extensions/MobileFrontend/MobileFrontend.php
@@ -84,7 +84,7 @@
8585 }
8686
8787 class ExtMobileFrontend {
88 - const VERSION = '0.5.75';
 88+ const VERSION = '0.5.76';
8989
9090 /**
9191 * @var DOMDocument
@@ -200,10 +200,52 @@
201201 );
202202
203203 public function testCanonicalRedirect( $request, $title, $output ) {
 204+ global $wgUsePathInfo, $wgMobileDomain;
204205 $xDevice = isset( $_SERVER['HTTP_X_DEVICE'] ) ? $_SERVER['HTTP_X_DEVICE'] : '';
205206 if ( empty( $xDevice ) ) {
206207 return true; // Let the redirect happen
207208 } else {
 209+ if ( $title->getNamespace() == NS_SPECIAL ) {
 210+ list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
 211+ if ( $name ) {
 212+ $title = SpecialPage::getTitleFor( $name, $subpage );
 213+ }
 214+ }
 215+ $targetUrl = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
 216+ // Redirect to canonical url, make it a 301 to allow caching
 217+ if ( $targetUrl == $request->getFullRequestURL() ) {
 218+ $message = "Redirect loop detected!\n\n" .
 219+ "This means the wiki got confused about what page was " .
 220+ "requested; this sometimes happens when moving a wiki " .
 221+ "to a new server or changing the server configuration.\n\n";
 222+
 223+ if ( $wgUsePathInfo ) {
 224+ $message .= "The wiki is trying to interpret the page " .
 225+ "title from the URL path portion (PATH_INFO), which " .
 226+ "sometimes fails depending on the web server. Try " .
 227+ "setting \"\$wgUsePathInfo = false;\" in your " .
 228+ "LocalSettings.php, or check that \$wgArticlePath " .
 229+ "is correct.";
 230+ } else {
 231+ $message .= "Your web server was detected as possibly not " .
 232+ "supporting URL path components (PATH_INFO) correctly; " .
 233+ "check your LocalSettings.php for a customized " .
 234+ "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
 235+ "to true.";
 236+ }
 237+ throw new HttpError( 500, $message );
 238+ } else {
 239+ $parsedUrl = wfParseUrl( $targetUrl );
 240+ if ( stristr( $parsedUrl['host'], $wgMobileDomain ) === FALSE ) {
 241+ $hostParts = explode( '.', $parsedUrl['host'] );
 242+ $parsedUrl['host'] = $hostParts[0] . $wgMobileDomain . $hostParts[1] . '.' . $hostParts[2];
 243+ }
 244+ $fragmentDelimiter = ( !empty( $parsedUrl['fragment'] ) ) ? '#' : '';
 245+ $queryDelimiter = ( !empty( $parsedUrl['query'] ) ) ? '?' : '';
 246+ $targetUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . $parsedUrl['path'] . $queryDelimiter . $parsedUrl['query'] . $fragmentDelimiter . $parsedUrl['fragment'];
 247+ $output->setSquidMaxage( 1200 );
 248+ $output->redirect( $targetUrl, '301' );
 249+ }
208250 return false; // Prevent the redirect from occuring
209251 }
210252 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r98549mft r98462 r98547preilly18:55, 30 September 2011
r98550mft r98462 r98547preilly18:55, 30 September 2011

Comments

#Comment by Brion VIBBER (talk | contribs)   18:54, 30 September 2011

Adding a todo note to myself to see if we can make this hook interface prettier (eg checking the redirect loop should be doable in the parent code).

#Comment by Preilly (talk | contribs)   18:56, 30 September 2011

Yeah, that makes total sense to me.

Status & tagging log