r22232 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r22231‎ | r22232 | r22233 >
Date:20:02, 17 May 2007
Author:brion
Status:old
Tags:
Comment:
Fix for regression in variant short-URLs...
Cleaned up the new URL parsing code a bit, now able to recognize both $wgActionPaths and $wgVariantArticlePath and interpolate those variables.
Moved that bit into WebRequest::interpolateTitle() function which is called after $wgContLang stub object is created, so it can be used to get the variant list.
I'm not totally happy with all this code, but it works. The variant bits generally creep me out.
Modified paths:
  • /trunk/phase3/includes/Setup.php (modified) (history)
  • /trunk/phase3/includes/WebRequest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Setup.php
@@ -167,6 +167,10 @@
168168 $wgLoadBalancer = new StubObject( 'wgLoadBalancer', 'LoadBalancer',
169169 array( $wgDBservers, false, $wgMasterWaitTimeout, true ) );
170170 $wgContLang = new StubContLang;
 171+
 172+// Now that variant lists may be available...
 173+$wgRequest->interpolateTitle();
 174+
171175 $wgUser = new StubUser;
172176 $wgLang = new StubUserLang;
173177 $wgOut = new StubObject( 'wgOut', 'OutputPage' );
Index: trunk/phase3/includes/WebRequest.php
@@ -44,44 +44,93 @@
4545 class WebRequest {
4646 function __construct() {
4747 $this->checkMagicQuotes();
 48+ }
 49+
 50+ /**
 51+ * Check for title, action, and/or variant data in the URL
 52+ * and interpolate it into the GET variables.
 53+ * This should only be run after $wgContLang is available,
 54+ * as we may need the list of language variants to determine
 55+ * available variant URLs.
 56+ */
 57+ function interpolateTitle() {
4858 global $wgUsePathInfo;
4959 if ( $wgUsePathInfo ) {
5060 // PATH_INFO is mangled due to http://bugs.php.net/bug.php?id=31892
5161 // And also by Apache 2.x, double slashes are converted to single slashes.
5262 // So we will use REQUEST_URI if possible.
53 - $title = '';
 63+ $matches = array();
5464 if ( !empty( $_SERVER['REQUEST_URI'] ) ) {
55 - global $wgArticlePath, $wgActionPaths;
56 - $paths["view"] = $wgArticlePath;
57 - $paths = array_merge( $paths, $wgActionPaths );
58 - $title = $this->extractActionPaths( $paths );
 65+ // Slurp out the path portion to examine...
 66+ $url = $_SERVER['REQUEST_URI'];
 67+ if ( !preg_match( '!^https?://!', $url ) ) {
 68+ $url = 'http://unused' . $url;
 69+ }
 70+ $a = parse_url( $url );
 71+ if( $a ) {
 72+ $path = $a['path'];
 73+
 74+ global $wgArticlePath;
 75+ $matches = $this->extractTitle( $path, $wgArticlePath );
 76+
 77+ global $wgActionPaths;
 78+ if( !$matches && $wgActionPaths) {
 79+ $matches = $this->extractTitle( $path, $wgActionPaths, 'action' );
 80+ }
 81+
 82+ global $wgVariantArticlePath, $wgContLang;
 83+ if( !$matches && $wgVariantArticlePath ) {
 84+ $variantPaths = array();
 85+ foreach( $wgContLang->getVariants() as $variant ) {
 86+ $variantPaths[$variant] =
 87+ str_replace( '$2', $variant, $wgVariantArticlePath );
 88+ }
 89+ $matches = $this->extractTitle( $path, $variantPaths, 'variant' );
 90+ }
 91+ }
5992 } elseif ( isset( $_SERVER['ORIG_PATH_INFO'] ) && $_SERVER['ORIG_PATH_INFO'] != '' ) {
60 - # Mangled PATH_INFO
61 - # http://bugs.php.net/bug.php?id=31892
62 - # Also reported when ini_get('cgi.fix_pathinfo')==false
63 - $title = substr( $_SERVER['ORIG_PATH_INFO'], 1 );
64 - } elseif ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != '') && $wgUsePathInfo ) {
65 - $title = substr( $_SERVER['PATH_INFO'], 1 );
 93+ // Mangled PATH_INFO
 94+ // http://bugs.php.net/bug.php?id=31892
 95+ // Also reported when ini_get('cgi.fix_pathinfo')==false
 96+ $matches['title'] = substr( $_SERVER['ORIG_PATH_INFO'], 1 );
 97+
 98+ } elseif ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != '') ) {
 99+ // Regular old PATH_INFO yay
 100+ $matches['title'] = substr( $_SERVER['PATH_INFO'], 1 );
66101 }
67 - if ( strval( $title ) != '' ) {
68 - $_GET['title'] = $_REQUEST['title'] = $title;
 102+ foreach( $matches as $key => $val) {
 103+ $_GET[$key] = $_REQUEST[$key] = $val;
69104 }
70105 }
71106 }
72107
73 - private function extractActionPaths( $paths ) {
74 - $url = $_SERVER['REQUEST_URI'];
75 - if ( !preg_match( '!^https?://!', $url ) ) {
76 - $url = 'http://unused' . $url;
77 - }
78 - $a = parse_url( $url );
79 - foreach( $paths as $action => $path ) {
 108+ /**
 109+ * Internal URL rewriting function; tries to extract page title and,
 110+ * optionally, one other fixed parameter value from a URL path.
 111+ *
 112+ * @param string $path the URL path given from the client
 113+ * @param array $bases one or more URLs, optionally with $1 at the end
 114+ * @param string $key if provided, the matching key in $bases will be
 115+ * passed on as the value of this URL parameter
 116+ * @return array of URL variables to interpolate; empty if no match
 117+ */
 118+ private function extractTitle( $path, $bases, $key=false ) {
 119+ foreach( (array)$bases as $keyValue => $base ) {
80120 // Find the part after $wgArticlePath
81 - $base = str_replace( '$1', '', $path );
82 - if ( $a && substr( $a['path'], 0, strlen( $base ) ) == $base ) {
83 - return urldecode( substr( $a['path'], strlen( $base ) ) );
 121+ $base = str_replace( '$1', '', $base );
 122+ $baseLen = strlen( $base );
 123+ if( substr( $path, 0, $baseLen ) == $base ) {
 124+ $raw = substr( $path, $baseLen );
 125+ if( $raw !== '' ) {
 126+ $matches = array( 'title' => urldecode( $raw ) );
 127+ if( $key ) {
 128+ $matches[$key] = $keyValue;
 129+ }
 130+ return $matches;
 131+ }
84132 }
85133 }
 134+ return array();
86135 }
87136
88137 private $_response;