Improve the ability for extensions to participate in how MediaWiki handles url paths:
authorDaniel Friesen <dantman@users.mediawiki.org>
Fri, 12 Aug 2011 19:23:43 +0000 (19:23 +0000)
committerDaniel Friesen <dantman@users.mediawiki.org>
Fri, 12 Aug 2011 19:23:43 +0000 (19:23 +0000)
- Allow extensions to hook into WebRequest::getPathInfo and add to or alter the way titles are extracted from paths
- Add a $variant argument to the GetLocalURL hook; It's always had $query, but never had $variant. As a result extensions using GetLocalURL never new if getLocalURL and have the possibility of trying to change the url in cases where they shouldn't and as a result breaking links on wiki with language variants.
- Add GetLocalURL::Internal hook for non-interwiki links. These kinds of links internally use a ugly hack for action=render and an extension using GetLocalURL can be buggy in render mode if they don't re-implement the same ugly hack that MW does. This ::Internal hook runs before the hack does so extension authors don't need to be exposed to our ugly hacky code.
- Add GetLocalURL::Article hook specifically for url tweaks to pretty urls (ie: Only when we would apply $wgArticlePath); This hook avoids the need for extensions that only want to tweak pretty url output. This hook avoids the need to make a bunch of tests for things like !$title->isExternal(), $query == '', and $variant === false which getLocalURL does and could potentially change in the future making wider GetLocalURL hooks change in function requiring extension updates.

RELEASE-NOTES-1.19
docs/hooks.txt
includes/Title.php
includes/WebRequest.php

index 674e7ec..04cce33 100644 (file)
@@ -28,6 +28,9 @@ production.
   as with templates.
 * Installer now issues a warning if mod_security is present.
 * (bug 29455) Add support for a filter callback function in jQuery byteLimit plugin.
+* Extensions can now participate in the extraction of titles from url paths
+* Added two new GetLocalURL hooks to better serve extensions working on a limited
+  type of titles.
 
 === Bug fixes in 1.19 ===
 * $wgUploadNavigationUrl should be used for file redlinks if
index 6df106a..2dfafb0 100644 (file)
@@ -903,10 +903,27 @@ $linkcolour_ids: array of prefixed DB keys of the pages linked to,
        indexed by page_id.
 &$colours: (output) array of CSS classes, indexed by prefixed DB keys
 
-'GetLocalURL': modify local URLs as output into page links
+'GetLocalURL': modify local URLs as output into page links. Note that if you
+       are working with internal urls (non-interwiki) then it may be preferable
+       to work with the GetLocalURL::Internal or GetLocalURL::Article hooks as
+       GetLocalURL can be buggy for internal urls on render if you do not
+       re-implement the horrible hack that Title::getLocalURL uses
+       in your own extension.
 $title: Title object of page
-$url: string value as output (out parameter, can modify)
+&$url: string value as output (out parameter, can modify)
+$query: query options passed to Title::getLocalURL()
+$variant: variant options passed to Title::getLocalURL()
+
+'GetLocalURL::Internal': modify local URLs to internal pages.
+$title: Title object of page
+&$url: string value as output (out parameter, can modify)
 $query: query options passed to Title::getLocalURL()
+$variant: variant options passed to Title::getLocalURL()
+
+'GetLocalURL::Article': modify local URLs specifically pointing to article paths
+       without any fancy queries or variants.
+$title: Title object of page
+&$url: string value as output (out parameter, can modify)
 
 'GetMetadataVersion': modify the image metadata version currently in use. This is
        used when requesting image metadata from a ForiegnApiRepo. Media handlers
@@ -2056,6 +2073,15 @@ $title: Title object
 $redirect: whether the page is a redirect
 $skin: Skin object
 
+'WebRequestGetPathInfoRequestURI': while extracting path info from REQUEST_URI.
+       Allows an extension to extend the extraction of titles from paths.
+       Implementing hooks should follow the pattern used in core:
+       * Use the `$matches = WebRequest::extractTitle` pattern
+       * Ensure that you test `if ( !$matches ) {` before you try extracting a title
+         from the path so that you don't override an already found match.
+$path: The request path to extract a title from.
+&$matches: The array to apply matches to.
+
 'WikiExporter::dumpStableQuery': Get the SELECT query for "stable" revisions
 dumps
 One, and only one hook should set this, and return false.
index a0af584..65dd8b6 100644 (file)
@@ -909,6 +909,7 @@ class Title {
                                        $url = str_replace( '$1', $dbkey, $url  );
                                } else {
                                        $url = str_replace( '$1', $dbkey, $wgArticlePath );
+                                       wfRunHooks( 'GetLocalURL::Article', array( &$this, &$url ) );
                                }
                        } else {
                                global $wgActionPaths;
@@ -937,6 +938,8 @@ class Title {
                                        $url = "{$wgScript}?title={$dbkey}&{$query}";
                                }
                        }
+                       
+                       wfRunHooks( 'GetLocalURL::Internal', array( &$this, &$url, $query, $variant ) );
 
                        // @todo FIXME: This causes breakage in various places when we
                        // actually expected a local URL and end up with dupe prefixes.
@@ -944,7 +947,7 @@ class Title {
                                $url = $wgServer . $url;
                        }
                }
-               wfRunHooks( 'GetLocalURL', array( &$this, &$url, $query ) );
+               wfRunHooks( 'GetLocalURL', array( &$this, &$url, $query, $variant ) );
                return $url;
        }
 
index 8acd0df..cfb6ce1 100644 (file)
@@ -109,6 +109,8 @@ class WebRequest {
                                        }
                                        $matches = self::extractTitle( $path, $variantPaths, 'variant' );
                                }
+
+                               wfRunHooks( 'WebRequestGetPathInfoRequestURI', array( $path, &$matches ) );
                        }
                } elseif ( isset( $_SERVER['ORIG_PATH_INFO'] ) && $_SERVER['ORIG_PATH_INFO'] != '' ) {
                        // Mangled PATH_INFO
@@ -192,7 +194,7 @@ class WebRequest {
        }
 
        /**
-        * Internal URL rewriting function; tries to extract page title and,
+        * URL rewriting function; tries to extract page title and,
         * optionally, one other fixed parameter value from a URL path.
         *
         * @param $path string: the URL path given from the client
@@ -201,7 +203,7 @@ class WebRequest {
         *             passed on as the value of this URL parameter
         * @return array of URL variables to interpolate; empty if no match
         */
-       private static function extractTitle( $path, $bases, $key = false ) {
+       static function extractTitle( $path, $bases, $key = false ) {
                foreach( (array)$bases as $keyValue => $base ) {
                        // Find the part after $wgArticlePath
                        $base = str_replace( '$1', '', $base );