From 0139030f2bd03b8ebf08978ed4c1b1bbfd52b308 Mon Sep 17 00:00:00 2001 From: Daniel Friesen Date: Fri, 12 Aug 2011 19:23:43 +0000 Subject: [PATCH] Improve the ability for extensions to participate in how MediaWiki handles url paths: - 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 | 3 +++ docs/hooks.txt | 30 ++++++++++++++++++++++++++++-- includes/Title.php | 5 ++++- includes/WebRequest.php | 6 ++++-- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/RELEASE-NOTES-1.19 b/RELEASE-NOTES-1.19 index 674e7ecb54..04cce33c9a 100644 --- a/RELEASE-NOTES-1.19 +++ b/RELEASE-NOTES-1.19 @@ -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 diff --git a/docs/hooks.txt b/docs/hooks.txt index 6df106a259..2dfafb0d61 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -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. diff --git a/includes/Title.php b/includes/Title.php index a0af584d11..65dd8b6a96 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -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; } diff --git a/includes/WebRequest.php b/includes/WebRequest.php index 8acd0dfcac..cfb6ce146f 100644 --- a/includes/WebRequest.php +++ b/includes/WebRequest.php @@ -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 ); -- 2.20.1