Add GitViewers hook for extensions to add git viewers to SpecialVersion
authorSimon Walker <github@stwalkerster.co.uk>
Mon, 16 Apr 2012 16:57:58 +0000 (16:57 +0000)
committerSimon Walker <github@stwalkerster.co.uk>
Mon, 16 Apr 2012 16:57:58 +0000 (16:57 +0000)
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
docs/hooks.txt
includes/GitInfo.php

index 07145ae..3d88702 100644 (file)
@@ -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.
index b02c305..d87bd3d 100644 (file)
@@ -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.
index 0dfd48d..74c07cc 100644 (file)
@@ -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;
+       }
 }