From 6c29ec29219ec97ed76dbd3a46adb9457b9a9ff0 Mon Sep 17 00:00:00 2001 From: Simon Walker Date: Mon, 16 Apr 2012 16:57:58 +0000 Subject: [PATCH] Add GitViewers hook for extensions to add git viewers to SpecialVersion This adds a GitViewers hook to GitInfo, and slightly refactors the $viewers variable to be called through a function so the hook can be called. This is so extensions can add their own git repository viewer to the Special:Version git sha field. Change-Id: Ifc8396984de6a51be85fb63cd9754d57333bde03 --- RELEASE-NOTES-1.20 | 2 ++ docs/hooks.txt | 5 +++++ includes/GitInfo.php | 39 ++++++++++++++++++++++++++++----------- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/RELEASE-NOTES-1.20 b/RELEASE-NOTES-1.20 index 07145ae4cc..3d8870257a 100644 --- a/RELEASE-NOTES-1.20 +++ b/RELEASE-NOTES-1.20 @@ -38,6 +38,8 @@ production. * (bug 23006) create #speciale parser function. * generateSitemap can now optionally skip redirect pages. * (bug 27757) new API command just for retrieving tokens (not page-based) +* Added GitViewers hook for extensions using external git repositories to have a web-based + repository viewer linked to from Special:Version. === Bug fixes in 1.20 === * (bug 30245) Use the correct way to construct a log page title. diff --git a/docs/hooks.txt b/docs/hooks.txt index b02c30514b..d87bd3d3f2 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -997,6 +997,11 @@ $result: User permissions error to add. If none, return true. 'getUserPermissionsErrorsExpensive': Absolutely the same, but is called only if expensive checks are enabled. +'GitViewers': called when generating the list of git viewers for Special:Version, use + this to change the list. +&$extTypes: associative array of repo URLS to viewer URLs. + + 'ImageBeforeProduceHTML': Called before producing the HTML created by a wiki image insertion. You can skip the default logic entirely by returning false, or just modify a few things using call-by-reference. diff --git a/includes/GitInfo.php b/includes/GitInfo.php index 0dfd48dff2..74c07ccafc 100644 --- a/includes/GitInfo.php +++ b/includes/GitInfo.php @@ -21,17 +21,9 @@ class GitInfo { protected $basedir; /** - * Map of repo URLs to viewer URLs. - * Key is a pattern passed to preg_match() and preg_replace(), - * without the delimiters (which are #) and must match the whole URL. - * The value is the replacement for the key (it can contain $1, etc.) - * %h will be replaced by the short SHA-1 (7 first chars) and %H by the - * full SHA-1 of the HEAD revision. + * Map of repo URLs to viewer URLs. Access via static method getViewers(). */ - protected $viewers = array( - 'https://gerrit.wikimedia.org/r/p/(.*)' => 'https://gerrit.wikimedia.org/r/gitweb?p=$1;h=%H', - 'ssh://(?:[a-z0-9_]+@)?gerrit.wikimedia.org:29418/(.*)' => 'https://gerrit.wikimedia.org/r/gitweb?p=$1;h=%H', - ); + private static $viewers = false; /** * @param $dir string The root directory of the repo where the .git dir can be found @@ -151,7 +143,7 @@ class GitInfo { if ( substr( $url, -4 ) !== '.git' ) { $url .= '.git'; } - foreach( $this->viewers as $repo => $viewer ) { + foreach( self::getViewers() as $repo => $viewer ) { $pattern = '#^' . $repo . '$#'; if ( preg_match( $pattern, $url ) ) { $viewerUrl = preg_replace( $pattern, $viewer, $url ); @@ -189,4 +181,29 @@ class GitInfo { public static function headViewUrl() { return self::repo()->getHeadViewUrl(); } + + /** + * Gets the list of repository viewers + * @return array + */ + protected static function getViewers() { + if( self::$viewers === false ) { + + // Map of repo URLs to viewer URLs. + // + // Key is a pattern passed to preg_match() and preg_replace(), + // without the delimiters (which are #) and must match the whole URL. + // The value is the replacement for the key (it can contain $1, etc.) + // %h will be replaced by the short SHA-1 (7 first chars) and %H by the + // full SHA-1 of the HEAD revision. + self::$viewers = array( + 'https://gerrit.wikimedia.org/r/p/(.*)' => 'https://gerrit.wikimedia.org/r/gitweb?p=$1;h=%H', + 'ssh://(?:[a-z0-9_]+@)?gerrit.wikimedia.org:29418/(.*)' => 'https://gerrit.wikimedia.org/r/gitweb?p=$1;h=%H', + ); + + wfRunHooks( 'GitViewers', array( &self::$viewers ) ); + } + + return self::$viewers; + } } -- 2.20.1