Jeroen De Dauw has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/49777


Change subject: Allow registration of special pages using a callback that 
returns an instance of SpecialPage
......................................................................

Allow registration of special pages using a callback that returns an instance 
of SpecialPage

Basically implementing what Brion suggested on wikitech

This allows for injecting dependencies while still only loading the actual 
class when needed.

Simple example:

$wgSpecialPages['ListDatatypes'] = function() use ( $availableTypes ) {
        return new SpecialListDatatypes( $availableTypes );
};

Change-Id: I196af7b153faa6a1c2088b136e2af934ab184979
---
M includes/SpecialPageFactory.php
1 file changed, 33 insertions(+), 13 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/77/49777/1

diff --git a/includes/SpecialPageFactory.php b/includes/SpecialPageFactory.php
index add7efc..3ba51f0 100644
--- a/includes/SpecialPageFactory.php
+++ b/includes/SpecialPageFactory.php
@@ -337,24 +337,44 @@
         *
         * @param $name String Special page name, may be localised and/or an 
alias
         * @return SpecialPage|null SpecialPage object or null if the page 
doesn't exist
+        * @throws MWException
         */
        public static function getPage( $name ) {
                list( $realName, /*...*/ ) = self::resolveAlias( $name );
-               if ( property_exists( self::getList(), $realName ) ) {
-                       $rec = self::getList()->$realName;
-                       if ( is_string( $rec ) ) {
-                               $className = $rec;
-                               return new $className;
-                       } elseif ( is_array( $rec ) ) {
-                               // @deprecated, officially since 1.18, 
unofficially since forever
-                               wfDebug( "Array syntax for \$wgSpecialPages is 
deprecated, define a subclass of SpecialPage instead." );
-                               $className = array_shift( $rec );
-                               self::getList()->$realName = 
MWFunction::newObj( $className, $rec );
-                       }
-                       return self::getList()->$realName;
-               } else {
+
+               if ( !property_exists( self::getList(), $realName ) ) {
                        return null;
                }
+
+               $rec = self::getList()->$realName;
+
+               if ( is_string( $rec ) ) {
+                       $className = $rec;
+                       return new $className;
+               }
+
+               if ( is_array( $rec ) ) {
+                       // @deprecated, officially since 1.18, unofficially 
since forever
+                       wfDebug( "Array syntax for \$wgSpecialPages is 
deprecated, define a subclass of SpecialPage instead." );
+                       $className = array_shift( $rec );
+                       return MWFunction::newObj( $className, $rec );
+               }
+
+               if ( is_callable( $rec ) ) {
+                       $pageInstance = call_user_func( $rec );
+
+                       if ( !( $pageInstance instanceof SpecialPage ) ) {
+                               throw new MWException( 'Callbacks registered 
for special pages should return a SpecialPage instance' );
+                       }
+
+                       return $pageInstance;
+               }
+
+               if ( $rec instanceof SpecialPage ) {
+                       return $rec;
+               }
+
+               throw new MWException( "Invalid handler registered for special 
page '$name'" );
        }
 
        /**

-- 
To view, visit https://gerrit.wikimedia.org/r/49777
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I196af7b153faa6a1c2088b136e2af934ab184979
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to