Fix #1064 by implementing the new Special:Unusedcategories (thanks avar for the name)
[lhc/web/wiklou.git] / includes / SpecialPage.php
index 7c2cb87..5f67310 100644 (file)
@@ -19,7 +19,7 @@
 /**
  *
  */
-global $wgSpecialPages;
+global $wgSpecialPages, $wgUser;
 
 /**
  * @access private
@@ -34,29 +34,25 @@ $wgSpecialPages = array(
        'Preferences'       => new SpecialPage( 'Preferences' ),
        'Watchlist'         => new SpecialPage( 'Watchlist' ),
        
-       'Mytalk'                        => new UnlistedSpecialPage( 'Mytalk'),
-       'Mycontributions'       => new UnlistedSpecialPage( 'Mycontributions'),
-       'Mypage'                        => new UnlistedSpecialPage( 'Mypage'),
-       
-       'Recentchanges'     => new SpecialPage( 'Recentchanges' ),
+       'Recentchanges'     => new IncludableSpecialPage( 'Recentchanges' ),
        'Upload'            => new SpecialPage( 'Upload' ),
        'Imagelist'         => new SpecialPage( 'Imagelist' ),
        'Newimages'         => new SpecialPage( 'Newimages' ),
        'Listusers'         => new SpecialPage( 'Listusers' ),
-       'Listadmins'        => new UnlistedSpecialPage( 'Listadmins' ),
        'Statistics'        => new SpecialPage( 'Statistics' ),
-       'Randompage'        => new SpecialPage( 'Randompage' ),
+       'Random'        => new SpecialPage( 'Randompage' ),
        'Lonelypages'       => new SpecialPage( 'Lonelypages' ),
        'Uncategorizedpages'=> new SpecialPage( 'Uncategorizedpages' ),
        'Uncategorizedcategories'=> new SpecialPage( 'Uncategorizedcategories' ),
+       'Unusedcategories'      => new SpecialPage( 'Unusedcategories' ),
        'Unusedimages'      => new SpecialPage( 'Unusedimages' ),
        'Wantedpages'   => new SpecialPage( 'Wantedpages' ),
        'Shortpages'    => new SpecialPage( 'Shortpages' ),
        'Longpages'             => new SpecialPage( 'Longpages' ),
-       'Newpages'              => new SpecialPage( 'Newpages' ),
+       'Newpages'              => new IncludableSpecialPage( 'Newpages' ),
        'Ancientpages'  => new SpecialPage( 'Ancientpages' ),
        'Deadendpages'  => new SpecialPage( 'Deadendpages' ),
-       'Allpages'              => new SpecialPage( 'Allpages' ),
+       'Allpages'              => new IncludableSpecialPage( 'Allpages' ),
        'Ipblocklist'   => new SpecialPage( 'Ipblocklist' ),
        'Specialpages'  => new UnlistedSpecialPage( 'Specialpages' ),
        'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
@@ -76,9 +72,7 @@ $wgSpecialPages = array(
        "Import"                => new SpecialPage( "Import", 'import' ),
        'Lockdb'                => new SpecialPage( 'Lockdb', 'siteadmin' ),
        'Unlockdb'              => new SpecialPage( 'Unlockdb', 'siteadmin' ),
-#      'Sitesettings'  => new SpecialPage( 'Sitesettings', 'siteadmin' ),
        'Userrights'    => new SpecialPage( 'Userrights', 'userrights' ),
-       'Groups'                => new SpecialPage( 'Groups' ),
 );
 
 global $wgUseValidation ;
@@ -132,8 +126,19 @@ class SpecialPage
         * File which needs to be included before the function above can be called
         */
        var $mFile;
+       /**
+        * Whether or not this special page is being included from an article
+        */
+       var $mIncluding;
+       /**
+        * Whether the special page can be included in an article
+        */
+       var $mIncludable;
+       
+
        /**#@-*/
 
+       
        /**
         * Add a page to the list of valid special pages
         * $obj->execute() must send HTML to $wgOut then return
@@ -168,6 +173,29 @@ class SpecialPage
                        return NULL;
                }
        }
+       
+       /**
+        * @static
+        * @param string $name
+        * @return mixed Title object if the redirect exists, otherwise NULL
+        */
+       function &getRedirect( $name ) {
+               global $wgUser;
+               switch ( $name ) {
+                       case 'Mypage':
+                               return Title::makeTitle( NS_USER, $wgUser->getName() );
+                       case 'Mytalk':
+                               return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
+                       case 'Mycontributions':
+                               return Title::makeTitle( NS_SPECIAL, 'Contributions/' . $wgUser->getName() );
+                       case 'Listadmins':
+                               return Title::makeTitle( NS_SPECIAL, 'Listusers' );
+                       case 'Randompage':
+                               return Title::makeTitle( NS_SPECIAL, 'Random' );
+                       default:
+                               return NULL;
+               }
+       }
 
        /**
         * Return categorised listable special pages
@@ -195,14 +223,18 @@ class SpecialPage
         * The path     may contain parameters, e.g. Special:Name/Params
         * Extracts the special page name and call the execute method, passing the parameters
         *
-        * @param $title should be a title object
+        * Returns a title object if the page is redirected, false if there was no such special 
+        * page, and true if it was successful.
+        *
+        * @param $title          a title object
+        * @param $including      output is being captured for use in {{special:whatever}}
         */
-       function executePath( &$title ) {
+       function executePath( &$title, $including = false ) {
                global $wgSpecialPages, $wgOut, $wgTitle;
 
                $bits = split( "/", $title->getDBkey(), 2 );
                $name = $bits[0];
-               if( empty( $bits[1] ) ) {
+               if( !isset( $bits[1] ) ) { // bug 2087
                        $par = NULL;
                } else {
                        $par = $bits[1];
@@ -210,18 +242,60 @@ class SpecialPage
 
                $page =& SpecialPage::getPage( $name );
                if ( is_null( $page ) ) {
-                       $wgOut->setArticleRelated( false );
-                       $wgOut->setRobotpolicy( "noindex,follow" );
-                       $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
+                       if ( $including ) {
+                               return false;
+                       } else {
+                               $redir =& SpecialPage::getRedirect( $name );
+                               if ( isset( $redir ) ) {
+                                       if ( isset( $par ) ) 
+                                               $wgOut->redirect( $redir->getFullURL() . '/' . $par );
+                                       else
+                                               $wgOut->redirect( $redir->getFullURL() );
+                                       $retVal = $redir;
+                               } else {
+                                       $wgOut->setArticleRelated( false );
+                                       $wgOut->setRobotpolicy( "noindex,follow" );
+                                       $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
+                                       $retVal = false;
+                               }
+                       }
                } else {
+                       if ( $including && !$page->includable() ) {
+                               return false;
+                       }
                        if($par !== NULL) {
                                $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
                        } else {
                                $wgTitle = $title;
                        }
+                       $page->including( $including );
 
                        $page->execute( $par );
+                       $retVal = true;
+               }
+               return $retVal;
+       }
+
+       /**
+        * Just like executePath() except it returns the HTML instead of outputting it
+        * Returns false if there was no such special page, or a title object if it was
+        * a redirect.
+        * @static
+        */
+       function capturePath( &$title ) {
+               global $wgOut, $wgTitle;
+
+               $oldTitle = $wgTitle;
+               $oldOut = $wgOut;
+               $wgOut = new OutputPage;
+               
+               $ret = SpecialPage::executePath( $title, true );
+               if ( $ret === true ) {
+                       $ret = $wgOut->getHTML();
                }
+               $wgTitle = $oldTitle;
+               $wgOut = $oldOut;
+               return $ret;
        }
 
        /**
@@ -240,10 +314,11 @@ class SpecialPage
         * @param string $function Function called by execute(). By default it is constructed from $name
         * @param string $file File which is included by execute(). It is also constructed from $name by default
         */
-       function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default' ) {
+       function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
                $this->mName = $name;
                $this->mRestriction = $restriction;
                $this->mListed = $listed;
+               $this->mIncludable = $includable;
                if ( $function == false ) {
                        $this->mFunction = 'wfSpecial'.$name;
                } else {
@@ -261,6 +336,8 @@ class SpecialPage
        function getRestriction() { return $this->mRestriction; }
        function isListed() { return $this->mListed; }
        function getFile() { return $this->mFile; }
+       function including( $x = NULL ) { return wfSetVar( $this->mIncluding, $x ); }
+       function includable( $x = NULL ) { return wfSetVar( $this->mIncludable, $x ); } 
 
        /**
         * Checks if the given user (identified by an object) can execute this
@@ -283,11 +360,7 @@ class SpecialPage
         */
        function displayRestrictionError() {
                global $wgOut;
-               if ( $this->mRestriction == "developer" ) {
-                       $wgOut->developerRequired();
-               } else {
-                       $wgOut->sysopRequired();
-               }
+               $wgOut->permissionRequired( $this->mRestriction );
        }
 
        /**
@@ -314,7 +387,7 @@ class SpecialPage
                                require_once( $this->mFile );
                        }
                        $func = $this->mFunction;
-                       $func( $par );
+                       $func( $par, $this );
                } else {
                        $this->displayRestrictionError();
                }
@@ -342,6 +415,7 @@ class SpecialPage
        function setListed( $listed ) {
                return wfSetVar( $this->mListed, $listed );
        }
+
 }
 
 /**
@@ -354,4 +428,15 @@ class UnlistedSpecialPage extends SpecialPage
                SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
        }
 }
+
+/**
+ * Shortcut to construct an includable special  page
+ * @package MediaWiki
+ */
+class IncludableSpecialPage extends SpecialPage
+{
+       function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
+               SpecialPage::SpecialPage( $name, $restriction, $listed, $function, $file, true );
+       }
+}
 ?>