Aaaaah, typo
[lhc/web/wiklou.git] / includes / SpecialPage.php
index bdc1d92..fc7efaf 100644 (file)
  */
 
 
-/**
- *
- */
-global $wgSpecialPages, $wgSpecialPageRedirects, $wgUser;
-
 /**
  * @access private
  */
@@ -33,25 +28,33 @@ $wgSpecialPages = array(
        'Userlogout'        => new UnlistedSpecialPage( 'Userlogout' ),
        'Preferences'       => new SpecialPage( 'Preferences' ),
        'Watchlist'         => new SpecialPage( 'Watchlist' ),
-       
-       'Recentchanges'     => new SpecialPage( 'Recentchanges' ),
+
+       'Recentchanges'     => new IncludableSpecialPage( 'Recentchanges' ),
        'Upload'            => new SpecialPage( 'Upload' ),
        'Imagelist'         => new SpecialPage( 'Imagelist' ),
-       'Newimages'         => new SpecialPage( 'Newimages' ),
+       'Newimages'         => new IncludableSpecialPage( 'Newimages' ),
        'Listusers'         => new SpecialPage( 'Listusers' ),
        'Statistics'        => new SpecialPage( 'Statistics' ),
        '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' ),
+       'Wantedpages'   => new IncludableSpecialPage( 'Wantedpages' ),
+       'Wantedcategories' => new SpecialPage( 'Wantedcategories' ),
+       'Mostlinked'    => new SpecialPage( 'Mostlinked' ),
+       'Mostlinkedcategories' => new SpecialPage( 'Mostlinkedcategories' ),
+       'Mostcategories' => new SpecialPage( 'Mostcategories' ),
+       'Mostimages' => new SpecialPage( 'Mostimages' ),
+       'Mostrevisions' => new SpecialPage( 'Mostrevisions' ),
        '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' ),
+       'Prefixindex'   => new IncludableSpecialPage( 'Prefixindex' ) ,
        'Ipblocklist'   => new SpecialPage( 'Ipblocklist' ),
        'Specialpages'  => new UnlistedSpecialPage( 'Specialpages' ),
        'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
@@ -67,47 +70,27 @@ $wgSpecialPages = array(
        'Allmessages'   => new SpecialPage( 'Allmessages' ),
        'Log'           => new SpecialPage( 'Log' ),
        'Blockip'               => new SpecialPage( 'Blockip', 'block' ),
-       'Undelete'              => new SpecialPage( 'Undelete', 'delete' ),
+       'Undelete'              => new SpecialPage( 'Undelete', 'deletedhistory' ),
        "Import"                => new SpecialPage( "Import", 'import' ),
        'Lockdb'                => new SpecialPage( 'Lockdb', 'siteadmin' ),
        'Unlockdb'              => new SpecialPage( 'Unlockdb', 'siteadmin' ),
        'Userrights'    => new SpecialPage( 'Userrights', 'userrights' ),
-       'Groups'                => new SpecialPage( 'Groups' ),
+       'MIMEsearch'    => new SpecialPage( 'MIMEsearch' ),
+       'Unwatchedpages' => new SpecialPage( 'Unwatchedpages', 'unwatchedpages' ),
+       'Listredirects' => new SpecialPage( 'Listredirects' ),
+       'Revisiondelete' => new SpecialPage( 'Revisiondelete', 'deleterevision' ),
+       'Unusedtemplates' => new SpecialPage( 'Unusedtemplates' ),
+       'Randomredirect' => new SpecialPage( 'Randomredirect' ),
 );
 
-/**
- * Redirect Special:$key to somewhere else.
- *
- * @access private
- */
-$wgSpecialPageRedirects = array(
-       # My*
-       'Mypage' => Title::makeTitle( NS_USER, $wgUser->getName() ),
-       'Mytalk' => Title::makeTitle( NS_USER_TALK, $wgUser->getName() ),
-       'Mycontributions' => Title::makeTitle( NS_SPECIAL, 'Contributions/' . $wgUser->getName() ),
-
-       # Deprecated specialpages
-       'Listadmins' => Title::makeTitle( NS_SPECIAL, 'Listusers' ),
-
-       # Redirects
-       'Randompage' => Title::makeTitle( NS_SPECIAL, 'Random' ),
-);
-
-global $wgUseValidation ;
-if ( $wgUseValidation )
-       $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
-
-global $wgDisableCounters;
 if( !$wgDisableCounters ) {
        $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
 }
 
-global $wgDisableInternalSearch;
 if( !$wgDisableInternalSearch ) {
-       $wgSpecialPages['Search'] = new UnlistedSpecialPage( 'Search' );
+       $wgSpecialPages['Search'] = new SpecialPage( 'Search' );
 }
 
-global $wgEmailAuthentication;
 if( $wgEmailAuthentication ) {
        $wgSpecialPages['Confirmemail'] = new UnlistedSpecialPage( 'Confirmemail' );
 }
@@ -144,15 +127,26 @@ 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
         * Use this for a special page extension
         * @static
         */
-       function addPage( &$obj ) {
+       static function addPage( &$obj ) {
                global $wgSpecialPages;
                $wgSpecialPages[$obj->mName] = $obj;
        }
@@ -162,7 +156,7 @@ class SpecialPage
         * Occasionally used to disable expensive or dangerous special pages
         * @static
         */
-       function removePage( $name ) {
+       static function removePage( $name ) {
                global $wgSpecialPages;
                unset( $wgSpecialPages[$name] );
        }
@@ -172,7 +166,7 @@ class SpecialPage
         * @static
         * @param string $name
         */
-       function &getPage( $name ) {
+       static function getPage( $name ) {
                global $wgSpecialPages;
                if ( array_key_exists( $name, $wgSpecialPages ) ) {
                        return $wgSpecialPages[$name];
@@ -180,27 +174,62 @@ class SpecialPage
                        return NULL;
                }
        }
-       
+
        /**
         * @static
         * @param string $name
         * @return mixed Title object if the redirect exists, otherwise NULL
         */
-       function &getRedirect( $name ) {
-               global $wgSpecialPageRedirects;
-               if ( array_key_exists( $name, $wgSpecialPageRedirects ) ) {
-                       return $wgSpecialPageRedirects[$name];
-               } else {
-                       return NULL;
-               }
+       static function getRedirect( $name ) {
+               global $wgUser;
+
+               $redirects = array(
+                       'Mypage' => Title::makeTitle( NS_USER, $wgUser->getName() ),
+                       'Mytalk' => Title::makeTitle( NS_USER_TALK, $wgUser->getName() ),
+                       'Mycontributions' => Title::makeTitle( NS_SPECIAL, 'Contributions/' . $wgUser->getName() ),
+                       'Listadmins' => Title::makeTitle( NS_SPECIAL, 'Listusers/sysop' ), # @bug 2832
+                       'Logs' => Title::makeTitle( NS_SPECIAL, 'Log' ),
+                       'Randompage' => Title::makeTitle( NS_SPECIAL, 'Random' ),
+                       'Userlist' => Title::makeTitle( NS_SPECIAL, 'Listusers' )
+               );
+               wfRunHooks( 'SpecialPageGetRedirect', array( &$redirects ) );
+
+               return isset( $redirects[$name] ) ? $redirects[$name] : null;
        }
 
+       /**
+        * Return part of the request string for a special redirect page
+        * This allows passing, e.g. action=history to Special:Mypage, etc.
+        *
+        * @param $name Name of the redirect page
+        * @return string
+        */
+       function getRedirectParams( $name ) {
+               global $wgRequest;
+               
+               $args = array();
+               switch( $name ) {
+                       case 'Mypage':
+                       case 'Mytalk':
+                       case 'Randompage':
+                               $args = array( 'action' );
+               }
+               
+               $params = array();
+               foreach( $args as $arg ) {
+                       if( $val = $wgRequest->getVal( $arg, false ) )
+                               $params[] = $arg . '=' . $val;
+               }
+               
+               return count( $params ) ? implode( '&', $params ) : false;
+       }       
+
        /**
         * Return categorised listable special pages
         * Returns a 2d array where the first index is the restriction name
         * @static
         */
-       function getPages() {
+       static function getPages() {
                global $wgSpecialPages;
                $pages = array(
                  '' => array(),
@@ -221,10 +250,16 @@ 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 ) {
-               global $wgSpecialPages, $wgOut, $wgTitle;
+       function executePath( &$title, $including = false ) {
+               global $wgOut, $wgTitle;
+               $fname = 'SpecialPage::executePath';
+               wfProfileIn( $fname );
 
                $bits = split( "/", $title->getDBkey(), 2 );
                $name = $bits[0];
@@ -234,28 +269,77 @@ class SpecialPage
                        $par = $bits[1];
                }
 
-               $page =& SpecialPage::getPage( $name );
+               $page = SpecialPage::getPage( $name );
                if ( is_null( $page ) ) {
-                       $redir =& SpecialPage::getRedirect( $name );
-                       if ( isset( $redir ) ) {
-                               if ( isset( $par ) ) 
-                                       $wgOut->redirect( $redir->getFullURL() . '/' . $par );
-                               else
-                                       $wgOut->redirect( $redir->getFullURL() );
+                       if ( $including ) {
+                               wfProfileOut( $fname );
+                               return false;
                        } else {
-                               $wgOut->setArticleRelated( false );
-                               $wgOut->setRobotpolicy( "noindex,follow" );
-                               $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
+                               $redir = SpecialPage::getRedirect( $name );
+                               if ( isset( $redir ) ) {
+                                       if( $par )
+                                               $redir = Title::makeTitle( $redir->getNamespace(), $redir->getText() . '/' . $par );
+                                       $params = SpecialPage::getRedirectParams( $name );
+                                       if( $params ) {
+                                               $url = $redir->getFullUrl( $params );
+                                       } else {
+                                               $url = $redir->getFullUrl();
+                                       }
+                                       $wgOut->redirect( $url );
+                                       $retVal = $redir;
+                                       $wgOut->redirect( $url );
+                                       $retVal = $redir;
+                               } else {
+                                       $wgOut->setArticleRelated( false );
+                                       $wgOut->setRobotpolicy( 'noindex,nofollow' );
+                                       $wgOut->setStatusCode( 404 );
+                                       $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
+                                       $retVal = false;
+                               }
                        }
                } else {
-                       if($par !== NULL) {
-                               $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
-                       } else {
-                               $wgTitle = $title;
+                       if ( $including && !$page->includable() ) {
+                               wfProfileOut( $fname );
+                               return false;
+                       } elseif ( !$including ) {
+                               if($par !== NULL) {
+                                       $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
+                               } else {
+                                       $wgTitle = $title;
+                               }
                        }
+                       $page->including( $including );
 
+                       $profName = 'Special:' . $page->getName();
+                       wfProfileIn( $profName );
                        $page->execute( $par );
+                       wfProfileOut( $profName );
+                       $retVal = true;
                }
+               wfProfileOut( $fname );
+               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
+        */
+       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;
        }
 
        /**
@@ -274,10 +358,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 {
@@ -290,11 +375,28 @@ class SpecialPage
                }
        }
 
-       # Accessor functions, see the descriptions of the associated variables above
+       /**#@+
+         * Accessor
+         *
+         * @deprecated
+         */
        function getName() { return $this->mName; }
        function getRestriction() { return $this->mRestriction; }
-       function isListed() { return $this->mListed; }
        function getFile() { return $this->mFile; }
+       function isListed() { return $this->mListed; }
+       /**#@-*/
+
+       /**#@+
+         * Accessor and mutator
+         */
+       function name( $x = NULL ) { return wfSetVar( $this->mName, $x ); }
+       function restrictions( $x = NULL) { return wfSetVar( $this->mRestrictions, $x ); }
+       function listed( $x = NULL) { return wfSetVar( $this->mListed, $x ); }
+       function func( $x = NULL) { return wfSetVar( $this->mFunction, $x ); }
+       function file( $x = NULL) { return wfSetVar( $this->mFile, $x ); }
+       function includable( $x = NULL ) { return wfSetVar( $this->mIncludable, $x ); }
+       function including( $x = NULL ) { return wfSetVar( $this->mIncluding, $x ); }
+       /**#@-*/
 
        /**
         * Checks if the given user (identified by an object) can execute this
@@ -317,11 +419,7 @@ class SpecialPage
         */
        function displayRestrictionError() {
                global $wgOut;
-               if ( $this->mRestriction == "developer" ) {
-                       $wgOut->developerRequired();
-               } else {
-                       $wgOut->sysopRequired();
-               }
+               $wgOut->permissionRequired( $this->mRestriction );
        }
 
        /**
@@ -330,7 +428,7 @@ class SpecialPage
        function setHeaders() {
                global $wgOut;
                $wgOut->setArticleRelated( false );
-               $wgOut->setRobotPolicy( "noindex,follow" );
+               $wgOut->setRobotPolicy( "noindex,nofollow" );
                $wgOut->setPageTitle( $this->getDescription() );
        }
 
@@ -339,21 +437,38 @@ class SpecialPage
         * Checks user permissions, calls the function given in mFunction
         */
        function execute( $par ) {
-               global $wgUser, $wgOut, $wgTitle;
+               global $wgUser;
 
                $this->setHeaders();
 
                if ( $this->userCanExecute( $wgUser ) ) {
-                       if ( $this->mFile ) {
+                       $func = $this->mFunction;
+                       // only load file if the function does not exist
+                       if(!function_exists($func) and $this->mFile) {
                                require_once( $this->mFile );
                        }
-                       $func = $this->mFunction;
-                       $func( $par );
+                       if ( wfRunHooks( 'SpecialPageExecuteBeforeHeader', array( &$this, &$par, &$func ) ) )
+                               $this->outputHeader();
+                       if ( ! wfRunHooks( 'SpecialPageExecuteBeforePage', array( &$this, &$par, &$func ) ) )
+                               return;
+                       $func( $par, $this );
+                       if ( ! wfRunHooks( 'SpecialPageExecuteAfterPage', array( &$this, &$par, &$func ) ) )
+                               return;
                } else {
                        $this->displayRestrictionError();
                }
        }
 
+       function outputHeader() {
+               global $wgOut, $wgContLang;
+
+               $msg = $wgContLang->lc( $this->name() ) . '-summary';
+               $out = wfMsg( $msg );
+               if ( ! wfEmptyMsg( $msg, $out ) and  $out !== '' and ! $this->including() )
+                       $wgOut->addWikiText( $out );
+
+       }
+
        # Returns the name that goes in the <h1> in the special page itself, and also the name that
        # will be listed in Special:Specialpages
        #
@@ -376,6 +491,7 @@ class SpecialPage
        function setListed( $listed ) {
                return wfSetVar( $this->mListed, $listed );
        }
+
 }
 
 /**
@@ -388,4 +504,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 );
+       }
+}
 ?>