]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/SpecialPage.php
MediaWiki 1.16.0
[autoinstallsdev/mediawiki.git] / includes / SpecialPage.php
1 <?php
2 /**
3  * SpecialPage: handling special pages and lists thereof.
4  *
5  * To add a special page in an extension, add to $wgSpecialPages either
6  * an object instance or an array containing the name and constructor
7  * parameters. The latter is preferred for performance reasons.
8  *
9  * The object instantiated must be either an instance of SpecialPage or a
10  * sub-class thereof. It must have an execute() method, which sends the HTML
11  * for the special page to $wgOut. The parent class has an execute() method
12  * which distributes the call to the historical global functions. Additionally,
13  * execute() also checks if the user has the necessary access privileges
14  * and bails out if not.
15  *
16  * To add a core special page, use the similar static list in
17  * SpecialPage::$mList. To remove a core static special page at runtime, use
18  * a SpecialPage_initList hook.
19  *
20  * @file
21  * @ingroup SpecialPage
22  * @defgroup SpecialPage SpecialPage
23  */
24
25 /**
26  * Parent special page class, also static functions for handling the special
27  * page list.
28  * @ingroup SpecialPage
29  */
30 class SpecialPage {
31         /**#@+
32          * @access private
33          */
34         /**
35          * The canonical name of this special page
36          * Also used for the default <h1> heading, @see getDescription()
37          */
38         var $mName;
39         /**
40          * The local name of this special page
41          */
42         var $mLocalName;
43         /**
44          * Minimum user level required to access this page, or "" for anyone.
45          * Also used to categorise the pages in Special:Specialpages
46          */
47         var $mRestriction;
48         /**
49          * Listed in Special:Specialpages?
50          */
51         var $mListed;
52         /**
53          * Function name called by the default execute()
54          */
55         var $mFunction;
56         /**
57          * File which needs to be included before the function above can be called
58          */
59         var $mFile;
60         /**
61          * Whether or not this special page is being included from an article
62          */
63         var $mIncluding;
64         /**
65          * Whether the special page can be included in an article
66          */
67         var $mIncludable;
68         /**
69          * Query parameters that can be passed through redirects
70          */
71         var $mAllowedRedirectParams = array();
72         /**
73          * List of special pages, followed by parameters.
74          * If the only parameter is a string, that is the page name.
75          * Otherwise, it is an array. The format is one of:
76          ** array( 'SpecialPage', name, right )
77          ** array( 'IncludableSpecialPage', name, right, listed? )
78          ** array( 'UnlistedSpecialPage', name, right )
79          ** array( 'SpecialRedirectToSpecial', name, page to redirect to, special page param, ... )
80          */
81         static public $mList = array(
82                 # Maintenance Reports
83                 'BrokenRedirects'           => array( 'SpecialPage', 'BrokenRedirects' ),
84                 'Deadendpages'              => array( 'SpecialPage', 'Deadendpages' ),
85                 'DoubleRedirects'           => array( 'SpecialPage', 'DoubleRedirects' ),
86                 'Longpages'                 => array( 'SpecialPage', 'Longpages' ),
87                 'Ancientpages'              => array( 'SpecialPage', 'Ancientpages' ),
88                 'Lonelypages'               => array( 'SpecialPage', 'Lonelypages' ),
89                 'Fewestrevisions'           => array( 'SpecialPage', 'Fewestrevisions' ),
90                 'Withoutinterwiki'          => array( 'SpecialPage', 'Withoutinterwiki' ),
91                 'Protectedpages'            => array( 'SpecialPage', 'Protectedpages' ),
92                 'Protectedtitles'           => array( 'SpecialPage', 'Protectedtitles' ),
93                 'Shortpages'                => array( 'SpecialPage', 'Shortpages' ),
94                 'Uncategorizedcategories'   => array( 'SpecialPage', 'Uncategorizedcategories' ),
95                 'Uncategorizedimages'       => array( 'SpecialPage', 'Uncategorizedimages' ),
96                 'Uncategorizedpages'        => array( 'SpecialPage', 'Uncategorizedpages' ),
97                 'Uncategorizedtemplates'    => array( 'SpecialPage', 'Uncategorizedtemplates' ),
98                 'Unusedcategories'          => array( 'SpecialPage', 'Unusedcategories' ),
99                 'Unusedimages'              => array( 'SpecialPage', 'Unusedimages' ),
100                 'Unusedtemplates'           => array( 'SpecialPage', 'Unusedtemplates' ),
101                 'Unwatchedpages'            => array( 'SpecialPage', 'Unwatchedpages', 'unwatchedpages' ),
102                 'Wantedcategories'          => array( 'SpecialPage', 'Wantedcategories' ),
103                 'Wantedfiles'               => array( 'SpecialPage', 'Wantedfiles' ),
104                 'Wantedpages'               => array( 'IncludableSpecialPage', 'Wantedpages' ),
105                 'Wantedtemplates'           => array( 'SpecialPage', 'Wantedtemplates' ),
106
107                 # List of pages
108                 'Allpages'                  => 'SpecialAllpages',
109                 'Prefixindex'               => 'SpecialPrefixindex',
110                 'Categories'                => array( 'SpecialPage', 'Categories' ),
111                 'Disambiguations'           => array( 'SpecialPage', 'Disambiguations' ),
112                 'Listredirects'             => array( 'SpecialPage', 'Listredirects' ),
113
114                 # Login/create account
115                 'Userlogin'                 => array( 'SpecialPage', 'Userlogin' ),
116                 'CreateAccount'             => array( 'SpecialRedirectToSpecial', 'CreateAccount', 'Userlogin', 'signup', array( 'uselang' ) ),
117
118                 # Users and rights
119                 'Blockip'                   => array( 'SpecialPage', 'Blockip', 'block' ),
120                 'Ipblocklist'               => array( 'SpecialPage', 'Ipblocklist' ),
121                 'Resetpass'                 => 'SpecialResetpass',
122                 'DeletedContributions'      => 'DeletedContributionsPage',
123                 'Preferences'               => 'SpecialPreferences',
124                 'Contributions'             => 'SpecialContributions',
125                 'Listgrouprights'           => 'SpecialListGroupRights',
126                 'Listusers'                 => array( 'SpecialPage', 'Listusers' ),
127                 'Activeusers'               => 'SpecialActiveUsers',
128                 'Userrights'                => 'UserrightsPage',
129
130                 # Recent changes and logs
131                 'Newimages'                 => array( 'IncludableSpecialPage', 'Newimages' ),
132                 'Log'                       => array( 'SpecialPage', 'Log' ),
133                 'Watchlist'                 => array( 'SpecialPage', 'Watchlist' ),
134                 'Newpages'                  => 'SpecialNewpages',
135                 'Recentchanges'             => 'SpecialRecentchanges',
136                 'Recentchangeslinked'       => 'SpecialRecentchangeslinked',
137                 'Tags'                      => 'SpecialTags',
138
139                 # Media reports and uploads
140                 'Listfiles'                 => array( 'SpecialPage', 'Listfiles' ),
141                 'Filepath'                  => array( 'SpecialPage', 'Filepath' ),
142                 'MIMEsearch'                => array( 'SpecialPage', 'MIMEsearch' ),
143                 'FileDuplicateSearch'       => array( 'SpecialPage', 'FileDuplicateSearch' ),
144                 'Upload'                    => 'SpecialUpload',
145
146                 # Wiki data and tools
147                 'Statistics'                => 'SpecialStatistics',
148                 'Allmessages'               => 'SpecialAllmessages',
149                 'Version'                   => 'SpecialVersion',
150                 'Lockdb'                    => array( 'SpecialPage', 'Lockdb', 'siteadmin' ),
151                 'Unlockdb'                  => array( 'SpecialPage', 'Unlockdb', 'siteadmin' ),
152
153                 # Redirecting special pages
154                 'LinkSearch'                => array( 'SpecialPage', 'LinkSearch' ),
155                 'Randompage'                => 'Randompage',
156                 'Randomredirect'            => 'SpecialRandomredirect',
157
158                 # High use pages
159                 'Mostlinkedcategories'      => array( 'SpecialPage', 'Mostlinkedcategories' ),
160                 'Mostimages'                => array( 'SpecialPage', 'Mostimages' ),
161                 'Mostlinked'                => array( 'SpecialPage', 'Mostlinked' ),
162                 'Mostlinkedtemplates'       => array( 'SpecialPage', 'Mostlinkedtemplates' ),
163                 'Mostcategories'            => array( 'SpecialPage', 'Mostcategories' ),
164                 'Mostrevisions'             => array( 'SpecialPage', 'Mostrevisions' ),
165
166                 # Page tools
167                 'Export'                    => 'SpecialExport',
168                 'Import'                    => 'SpecialImport',
169                 'Undelete'                  => array( 'SpecialPage', 'Undelete', 'deletedhistory' ),
170                 'Whatlinkshere'             => 'SpecialWhatlinkshere',
171                 'MergeHistory'              => array( 'SpecialPage', 'MergeHistory', 'mergehistory' ),
172
173                 # Other
174                 'Booksources'               => 'SpecialBookSources',
175
176                 # Unlisted / redirects
177                 'Blankpage'                 => 'SpecialBlankpage',
178                 'Blockme'                   => array( 'UnlistedSpecialPage', 'Blockme' ),
179                 'Emailuser'                 => array( 'UnlistedSpecialPage', 'Emailuser' ),
180                 'Listadmins'                => array( 'SpecialRedirectToSpecial', 'Listadmins', 'Listusers', 'sysop' ),
181                 'Listbots'                  => array( 'SpecialRedirectToSpecial', 'Listbots', 'Listusers', 'bot' ),
182                 'Movepage'                  => array( 'UnlistedSpecialPage', 'Movepage' ),
183                 'Mycontributions'           => array( 'SpecialMycontributions' ),
184                 'Mypage'                    => array( 'SpecialMypage' ),
185                 'Mytalk'                    => array( 'SpecialMytalk' ),
186                 'Revisiondelete'            => 'SpecialRevisionDelete',
187                 'Specialpages'              => array( 'UnlistedSpecialPage', 'Specialpages' ),
188                 'Userlogout'                => array( 'UnlistedSpecialPage', 'Userlogout' ),
189         );
190
191         static public $mAliases;
192         static public $mListInitialised = false;
193
194         /**#@-*/
195
196         /**
197          * Initialise the special page list
198          * This must be called before accessing SpecialPage::$mList
199          */
200         static function initList() {
201                 global $wgSpecialPages;
202                 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
203
204                 if ( self::$mListInitialised ) {
205                         return;
206                 }
207                 wfProfileIn( __METHOD__ );
208
209                 # Better to set this now, to avoid infinite recursion in carelessly written hooks
210                 self::$mListInitialised = true;
211
212                 if( !$wgDisableCounters ) {
213                         self::$mList['Popularpages'] = array( 'SpecialPage', 'Popularpages' );
214                 }
215
216                 if( !$wgDisableInternalSearch ) {
217                         self::$mList['Search'] = array( 'SpecialPage', 'Search' );
218                 }
219
220                 if( $wgEmailAuthentication ) {
221                         self::$mList['Confirmemail'] = 'EmailConfirmation';
222                         self::$mList['Invalidateemail'] = 'EmailInvalidation';
223                 }
224
225                 # Add extension special pages
226                 self::$mList = array_merge( self::$mList, $wgSpecialPages );
227
228                 # Run hooks
229                 # This hook can be used to remove undesired built-in special pages
230                 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
231                 wfProfileOut( __METHOD__ );
232         }
233
234         static function initAliasList() {
235                 if ( !is_null( self::$mAliases ) ) {
236                         return;
237                 }
238
239                 global $wgContLang;
240                 $aliases = $wgContLang->getSpecialPageAliases();
241                 $missingPages = self::$mList;
242                 self::$mAliases = array();
243                 foreach ( $aliases as $realName => $aliasList ) {
244                         foreach ( $aliasList as $alias ) {
245                                 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
246                         }
247                         unset( $missingPages[$realName] );
248                 }
249                 foreach ( $missingPages as $name => $stuff ) {
250                         self::$mAliases[$wgContLang->caseFold( $name )] = $name;
251                 }
252         }
253
254         /**
255          * Given a special page alias, return the special page name.
256          * Returns false if there is no such alias.
257          */
258         static function resolveAlias( $alias ) {
259                 global $wgContLang;
260
261                 if ( !self::$mListInitialised ) self::initList();
262                 if ( is_null( self::$mAliases ) ) self::initAliasList();
263                 $caseFoldedAlias = $wgContLang->caseFold( $alias );
264                 if ( isset( self::$mAliases[$caseFoldedAlias] ) ) {
265                         return self::$mAliases[$caseFoldedAlias];
266                 } else {
267                         return false;
268                 }
269         }
270
271         /**
272          * Given a special page name with a possible subpage, return an array
273          * where the first element is the special page name and the second is the
274          * subpage.
275          */
276         static function resolveAliasWithSubpage( $alias ) {
277                 $bits = explode( '/', $alias, 2 );
278                 $name = self::resolveAlias( $bits[0] );
279                 if( !isset( $bits[1] ) ) { // bug 2087
280                         $par = null;
281                 } else {
282                         $par = $bits[1];
283                 }
284                 return array( $name, $par );
285         }
286
287         /**
288          * Add a page to the list of valid special pages. This used to be the preferred
289          * method for adding special pages in extensions. It's now suggested that you add
290          * an associative record to $wgSpecialPages. This avoids autoloading SpecialPage.
291          *
292          * @param SpecialPage $page
293          * @static
294          */
295         static function addPage( &$page ) {
296                 if ( !self::$mListInitialised ) {
297                         self::initList();
298                 }
299                 self::$mList[$page->mName] = $page;
300         }
301
302         /**
303          * Add a page to a certain display group for Special:SpecialPages
304          *
305          * @param mixed $page (SpecialPage or string)
306          * @param string $group
307          * @static
308          */
309         static function setGroup( $page, $group ) {
310                 global $wgSpecialPageGroups;
311                 $name = is_object($page) ? $page->mName : $page;
312                 $wgSpecialPageGroups[$name] = $group;
313         }
314
315         /**
316          * Add a page to a certain display group for Special:SpecialPages
317          *
318          * @param SpecialPage $page
319          * @static
320          */
321         static function getGroup( &$page ) {
322                 global $wgSpecialPageGroups;
323                 static $specialPageGroupsCache = array();
324                 if( isset($specialPageGroupsCache[$page->mName]) ) {
325                         return $specialPageGroupsCache[$page->mName];
326                 }
327                 $group = wfMsg('specialpages-specialpagegroup-'.strtolower($page->mName));
328                 if( $group == ''
329                  || wfEmptyMsg('specialpages-specialpagegroup-'.strtolower($page->mName), $group ) ) {
330                         $group = isset($wgSpecialPageGroups[$page->mName])
331                                 ? $wgSpecialPageGroups[$page->mName]
332                                 : '-';
333                 }
334                 if( $group == '-' ) $group = 'other';
335                 $specialPageGroupsCache[$page->mName] = $group;
336                 return $group;
337         }
338
339         /**
340          * Remove a special page from the list
341          * Formerly used to disable expensive or dangerous special pages. The
342          * preferred method is now to add a SpecialPage_initList hook.
343          *
344          * @static
345          */
346         static function removePage( $name ) {
347                 if ( !self::$mListInitialised ) {
348                         self::initList();
349                 }
350                 unset( self::$mList[$name] );
351         }
352
353         /**
354          * Check if a given name exist as a special page or as a special page alias
355          * @param $name string: name of a special page
356          * @return boolean: true if a special page exists with this name
357          */
358         static function exists( $name ) {
359                 global $wgContLang;
360                 if ( !self::$mListInitialised ) {
361                         self::initList();
362                 }
363                 if( !self::$mAliases ) {
364                         self::initAliasList();
365                 }
366
367                 # Remove special pages inline parameters:
368                 $bits = explode( '/', $name );
369                 $name = $wgContLang->caseFold($bits[0]);
370
371                 return
372                         array_key_exists( $name, self::$mList )
373                         or array_key_exists( $name, self::$mAliases )
374                 ;
375         }
376
377         /**
378          * Find the object with a given name and return it (or NULL)
379          * @static
380          * @param string $name
381          */
382         static function getPage( $name ) {
383                 if ( !self::$mListInitialised ) {
384                         self::initList();
385                 }
386                 if ( array_key_exists( $name, self::$mList ) ) {
387                         $rec = self::$mList[$name];
388                         if ( is_string( $rec ) ) {
389                                 $className = $rec;
390                                 self::$mList[$name] = new $className;
391                         } elseif ( is_array( $rec ) ) {
392                                 $className = array_shift( $rec );
393                                 self::$mList[$name] = wfCreateObject( $className, $rec );
394                         }
395                         return self::$mList[$name];
396                 } else {
397                         return null;
398                 }
399         }
400
401         /**
402          * Get a special page with a given localised name, or NULL if there
403          * is no such special page.
404          */
405         static function getPageByAlias( $alias ) {
406                 $realName = self::resolveAlias( $alias );
407                 if ( $realName ) {
408                         return self::getPage( $realName );
409                 } else {
410                         return null;
411                 }
412         }
413
414         /**
415          * Return categorised listable special pages which are available
416          * for the current user, and everyone.
417          * @static
418          */
419         static function getUsablePages() {
420                 global $wgUser;
421                 if ( !self::$mListInitialised ) {
422                         self::initList();
423                 }
424                 $pages = array();
425
426                 foreach ( self::$mList as $name => $rec ) {
427                         $page = self::getPage( $name );
428                         if ( $page->isListed()
429                                 && (
430                                         !$page->isRestricted()
431                                         || $page->userCanExecute( $wgUser )
432                                 )
433                         ) {
434                                 $pages[$name] = $page;
435                         }
436                 }
437                 return $pages;
438         }
439
440         /**
441          * Return categorised listable special pages for all users
442          * @static
443          */
444         static function getRegularPages() {
445                 if ( !self::$mListInitialised ) {
446                         self::initList();
447                 }
448                 $pages = array();
449
450                 foreach ( self::$mList as $name => $rec ) {
451                         $page = self::getPage( $name );
452                         if ( $page->isListed() && !$page->isRestricted() ) {
453                                 $pages[$name] = $page;
454                         }
455                 }
456                 return $pages;
457         }
458
459         /**
460          * Return categorised listable special pages which are available
461          * for the current user, but not for everyone
462          * @static
463          */
464         static function getRestrictedPages() {
465                 global $wgUser;
466                 if( !self::$mListInitialised ) {
467                         self::initList();
468                 }
469                 $pages = array();
470
471                 foreach( self::$mList as $name => $rec ) {
472                         $page = self::getPage( $name );
473                         if(
474                                 $page->isListed()
475                                 && $page->isRestricted()
476                                 && $page->userCanExecute( $wgUser )
477                         ) {
478                                 $pages[$name] = $page;
479                         }
480                 }
481                 return $pages;
482         }
483
484         /**
485          * Execute a special page path.
486          * The path     may contain parameters, e.g. Special:Name/Params
487          * Extracts the special page name and call the execute method, passing the parameters
488          *
489          * Returns a title object if the page is redirected, false if there was no such special
490          * page, and true if it was successful.
491          *
492          * @param $title          a title object
493          * @param $including      output is being captured for use in {{special:whatever}}
494          */
495         static function executePath( &$title, $including = false ) {
496                 global $wgOut, $wgTitle, $wgRequest;
497                 wfProfileIn( __METHOD__ );
498
499                 # FIXME: redirects broken due to this call
500                 $bits = explode( '/', $title->getDBkey(), 2 );
501                 $name = $bits[0];
502                 if( !isset( $bits[1] ) ) { // bug 2087
503                         $par = null;
504                 } else {
505                         $par = $bits[1];
506                 }
507                 $page = SpecialPage::getPageByAlias( $name );
508                 # Nonexistent?
509                 if ( !$page ) {
510                         if ( !$including ) {
511                                 $wgOut->setArticleRelated( false );
512                                 $wgOut->setRobotPolicy( 'noindex,nofollow' );
513                                 $wgOut->setStatusCode( 404 );
514                                 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
515                         }
516                         wfProfileOut( __METHOD__ );
517                         return false;
518                 }
519
520                 # Check for redirect
521                 if ( !$including ) {
522                         $redirect = $page->getRedirect( $par );
523                         if ( $redirect ) {
524                                 $query = $page->getRedirectQuery();
525                                 $url = $redirect->getFullUrl( $query );
526                                 $wgOut->redirect( $url );
527                                 wfProfileOut( __METHOD__ );
528                                 return $redirect;
529                         }
530                 }
531
532                 # Redirect to canonical alias for GET commands
533                 # Not for POST, we'd lose the post data, so it's best to just distribute
534                 # the request. Such POST requests are possible for old extensions that
535                 # generate self-links without being aware that their default name has
536                 # changed.
537                 if ( !$including && $name != $page->getLocalName() && !$wgRequest->wasPosted() ) {
538                         $query = $_GET;
539                         unset( $query['title'] );
540                         $query = wfArrayToCGI( $query );
541                         $title = $page->getTitle( $par );
542                         $url = $title->getFullUrl( $query );
543                         $wgOut->redirect( $url );
544                         wfProfileOut( __METHOD__ );
545                         return $redirect;
546                 }
547
548                 if ( $including && !$page->includable() ) {
549                         wfProfileOut( __METHOD__ );
550                         return false;
551                 } elseif ( !$including ) {
552                         $wgTitle = $page->getTitle();
553                 }
554                 $page->including( $including );
555
556                 // Execute special page
557                 $profName = 'Special:' . $page->getName();
558                 wfProfileIn( $profName );
559                 $page->execute( $par );
560                 wfProfileOut( $profName );
561                 wfProfileOut( __METHOD__ );
562                 return true;
563         }
564
565         /**
566          * Just like executePath() except it returns the HTML instead of outputting it
567          * Returns false if there was no such special page, or a title object if it was
568          * a redirect.
569          * @static
570          */
571         static function capturePath( &$title ) {
572                 global $wgOut, $wgTitle;
573
574                 $oldTitle = $wgTitle;
575                 $oldOut = $wgOut;
576                 $wgOut = new OutputPage;
577                 $wgOut->setTitle( $title );
578
579                 $ret = SpecialPage::executePath( $title, true );
580                 if ( $ret === true ) {
581                         $ret = $wgOut->getHTML();
582                 }
583                 $wgTitle = $oldTitle;
584                 $wgOut = $oldOut;
585                 return $ret;
586         }
587
588         /**
589          * Get the local name for a specified canonical name
590          *
591          * @param $name
592          * @param mixed $subpage Boolean false, or string
593          *
594          * @return string
595          */
596         static function getLocalNameFor( $name, $subpage = false ) {
597                 global $wgContLang;
598                 $aliases = $wgContLang->getSpecialPageAliases();
599                 if ( isset( $aliases[$name][0] ) ) {
600                         $name = $aliases[$name][0];
601                 } else {
602                         // Try harder in case someone misspelled the correct casing
603                         $found = false;
604                         foreach ( $aliases as $n => $values ) {
605                                 if ( strcasecmp( $name, $n ) === 0 ) {
606                                         wfWarn( "Found alias defined for $n when searching for special page aliases
607 for $name. Case mismatch?" );
608                                         $name = $values[0];
609                                         $found = true;
610                                         break;
611                                 }
612                         }
613                         if ( !$found ) wfWarn( "Did not find alias for special page '$name'.
614 Perhaps no page aliases are defined for it?" );
615                 }
616                 if ( $subpage !== false && !is_null( $subpage ) ) {
617                         $name = "$name/$subpage";
618                 }
619                 return $wgContLang->ucfirst( $name );
620         }
621
622         /**
623          * Get a localised Title object for a specified special page name
624          */
625         static function getTitleFor( $name, $subpage = false ) {
626                 $name = self::getLocalNameFor( $name, $subpage );
627                 if ( $name ) {
628                         return Title::makeTitle( NS_SPECIAL, $name );
629                 } else {
630                         throw new MWException( "Invalid special page name \"$name\"" );
631                 }
632         }
633
634         /**
635          * Get a localised Title object for a page name with a possibly unvalidated subpage
636          */
637         static function getSafeTitleFor( $name, $subpage = false ) {
638                 $name = self::getLocalNameFor( $name, $subpage );
639                 if ( $name ) {
640                         return Title::makeTitleSafe( NS_SPECIAL, $name );
641                 } else {
642                         return null;
643                 }
644         }
645
646         /**
647          * Get a title for a given alias
648          * @return Title or null if there is no such alias
649          */
650         static function getTitleForAlias( $alias ) {
651                 $name = self::resolveAlias( $alias );
652                 if ( $name ) {
653                         return self::getTitleFor( $name );
654                 } else {
655                         return null;
656                 }
657         }
658
659         /**
660          * Default constructor for special pages
661          * Derivative classes should call this from their constructor
662          *     Note that if the user does not have the required level, an error message will
663          *     be displayed by the default execute() method, without the global function ever
664          *     being called.
665          *
666          *     If you override execute(), you can recover the default behaviour with userCanExecute()
667          *     and displayRestrictionError()
668          *
669          * @param string $name Name of the special page, as seen in links and URLs
670          * @param string $restriction User right required, e.g. "block" or "delete"
671          * @param boolean $listed Whether the page is listed in Special:Specialpages
672          * @param string $function Function called by execute(). By default it is constructed from $name
673          * @param string $file File which is included by execute(). It is also constructed from $name by default
674          */
675         function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
676                 $this->mName = $name;
677                 $this->mRestriction = $restriction;
678                 $this->mListed = $listed;
679                 $this->mIncludable = $includable;
680                 if ( $function == false ) {
681                         $this->mFunction = 'wfSpecial'.$name;
682                 } else {
683                         $this->mFunction = $function;
684                 }
685                 if ( $file === 'default' ) {
686                         $this->mFile = dirname(__FILE__) . "/specials/Special$name.php";
687                 } else {
688                         $this->mFile = $file;
689                 }
690         }
691
692         /**#@+
693           * Accessor
694           *
695           * @deprecated
696           */
697         function getName() { return $this->mName; }
698         function getRestriction() { return $this->mRestriction; }
699         function getFile() { return $this->mFile; }
700         function isListed() { return $this->mListed; }
701         /**#@-*/
702
703         /**#@+
704           * Accessor and mutator
705           */
706         function name( $x = null ) { return wfSetVar( $this->mName, $x ); }
707         function restrictions( $x = null) {
708                 # Use the one below this
709                 wfDeprecated( __METHOD__ );
710                 return wfSetVar( $this->mRestriction, $x );
711         }
712         function restriction( $x = null) { return wfSetVar( $this->mRestriction, $x ); }
713         function listed( $x = null) { return wfSetVar( $this->mListed, $x ); }
714         function func( $x = null) { return wfSetVar( $this->mFunction, $x ); }
715         function file( $x = null) { return wfSetVar( $this->mFile, $x ); }
716         function includable( $x = null ) { return wfSetVar( $this->mIncludable, $x ); }
717         function including( $x = null ) { return wfSetVar( $this->mIncluding, $x ); }
718         /**#@-*/
719
720         /**
721          * Get the localised name of the special page
722          */
723         function getLocalName() {
724                 if ( !isset( $this->mLocalName ) ) {
725                         $this->mLocalName = self::getLocalNameFor( $this->mName );
726                 }
727                 return $this->mLocalName;
728         }
729
730         /**
731          * Can be overridden by subclasses with more complicated permissions
732          * schemes.
733          *
734          * @return bool Should the page be displayed with the restricted-access
735          *   pages?
736          */
737         public function isRestricted() {
738                 global $wgGroupPermissions;
739                 // DWIM: If all anons can do something, then it is not restricted
740                 return $this->mRestriction != '' && empty($wgGroupPermissions['*'][$this->mRestriction]);
741         }
742
743         /**
744          * Checks if the given user (identified by an object) can execute this
745          * special page (as defined by $mRestriction).  Can be overridden by sub-
746          * classes with more complicated permissions schemes.
747          *
748          * @param User $user The user to check
749          * @return bool Does the user have permission to view the page?
750          */
751         public function userCanExecute( $user ) {
752                 return $user->isAllowed( $this->mRestriction );
753         }
754
755         /**
756          * Output an error message telling the user what access level they have to have
757          */
758         function displayRestrictionError() {
759                 global $wgOut;
760                 $wgOut->permissionRequired( $this->mRestriction );
761         }
762
763         /**
764          * Sets headers - this should be called from the execute() method of all derived classes!
765          */
766         function setHeaders() {
767                 global $wgOut;
768                 $wgOut->setArticleRelated( false );
769                 $wgOut->setRobotPolicy( "noindex,nofollow" );
770                 $wgOut->setPageTitle( $this->getDescription() );
771         }
772
773         /**
774          * Default execute method
775          * Checks user permissions, calls the function given in mFunction
776          *
777          * This may be overridden by subclasses.
778          */
779         function execute( $par ) {
780                 global $wgUser;
781
782                 $this->setHeaders();
783
784                 if ( $this->userCanExecute( $wgUser ) ) {
785                         $func = $this->mFunction;
786                         // only load file if the function does not exist
787                         if(!is_callable($func) and $this->mFile) {
788                                 require_once( $this->mFile );
789                         }
790                         $this->outputHeader();
791                         call_user_func( $func, $par, $this );
792                 } else {
793                         $this->displayRestrictionError();
794                 }
795         }
796
797         /**
798          * Outputs a summary message on top of special pages
799          * Per default the message key is the canonical name of the special page
800          * May be overriden, i.e. by extensions to stick with the naming conventions
801          * for message keys: 'extensionname-xxx'
802          *
803          * @param string message key of the summary
804          */
805         function outputHeader( $summaryMessageKey = '' ) {
806                 global $wgOut, $wgContLang;
807
808                 if( $summaryMessageKey == '' ) {
809                         $msg = $wgContLang->lc( $this->name() ) . '-summary';
810                 } else {
811                         $msg = $summaryMessageKey;
812                 }
813                 $out = wfMsgNoTrans( $msg );
814                 if ( ! wfEmptyMsg( $msg, $out ) and  $out !== '' and ! $this->including() ) {
815                         $wgOut->wrapWikiMsg( "<div class='mw-specialpage-summary'>\n$1</div>", $msg );
816                 }
817
818         }
819
820         # Returns the name that goes in the <h1> in the special page itself, and also the name that
821         # will be listed in Special:Specialpages
822         #
823         # Derived classes can override this, but usually it is easier to keep the default behaviour.
824         # Messages can be added at run-time, see MessageCache.php
825         function getDescription() {
826                 return wfMsg( strtolower( $this->mName ) );
827         }
828
829         /**
830          * Get a self-referential title object
831          */
832         function getTitle( $subpage = false ) {
833                 return self::getTitleFor( $this->mName, $subpage );
834         }
835
836         /**
837          * Set whether this page is listed in Special:Specialpages, at run-time
838          */
839         function setListed( $listed ) {
840                 return wfSetVar( $this->mListed, $listed );
841         }
842
843         /**
844          * If the special page is a redirect, then get the Title object it redirects to.
845          * False otherwise.
846          */
847         function getRedirect( $subpage ) {
848                 return false;
849         }
850
851         /**
852          * Return part of the request string for a special redirect page
853          * This allows passing, e.g. action=history to Special:Mypage, etc.
854          *
855          * @return string
856          */
857         function getRedirectQuery() {
858                 global $wgRequest;
859                 $params = array();
860                 foreach( $this->mAllowedRedirectParams as $arg ) {
861                         if( ( $val = $wgRequest->getVal( $arg, null ) ) !== null )
862                                 $params[] = $arg . '=' . $val;
863                 }
864
865                 return count( $params ) ? implode( '&', $params ) : false;
866         }
867 }
868
869 /**
870  * Shortcut to construct a special page which is unlisted by default
871  * @ingroup SpecialPage
872  */
873 class UnlistedSpecialPage extends SpecialPage
874 {
875         function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
876                 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
877         }
878 }
879
880 /**
881  * Shortcut to construct an includable special  page
882  * @ingroup SpecialPage
883  */
884 class IncludableSpecialPage extends SpecialPage
885 {
886         function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
887                 SpecialPage::SpecialPage( $name, $restriction, $listed, $function, $file, true );
888         }
889 }
890
891 /**
892  * Shortcut to construct a special page alias.
893  * @ingroup SpecialPage
894  */
895 class SpecialRedirectToSpecial extends UnlistedSpecialPage {
896         var $redirName, $redirSubpage;
897
898         function __construct( $name, $redirName, $redirSubpage = false, $redirectParams = array() ) {
899                 parent::__construct( $name );
900                 $this->redirName = $redirName;
901                 $this->redirSubpage = $redirSubpage;
902                 $this->mAllowedRedirectParams = $redirectParams;
903         }
904
905         function getRedirect( $subpage ) {
906                 if ( $this->redirSubpage === false ) {
907                         return SpecialPage::getTitleFor( $this->redirName, $subpage );
908                 } else {
909                         return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
910                 }
911         }
912 }
913
914 /** SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
915  * are used to get user independant links pointing to the user page, talk
916  * page and list of contributions.
917  * This can let us cache a single copy of any generated content for all
918  * users.
919  */
920
921 /**
922  * Shortcut to construct a special page pointing to current user user's page.
923  * @ingroup SpecialPage
924  */
925 class SpecialMypage extends UnlistedSpecialPage {
926         function __construct() {
927                 parent::__construct( 'Mypage' );
928                 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro', 'section' );
929         }
930
931         function getRedirect( $subpage ) {
932                 global $wgUser;
933                 if ( strval( $subpage ) !== '' ) {
934                         return Title::makeTitle( NS_USER, $wgUser->getName() . '/' . $subpage );
935                 } else {
936                         return Title::makeTitle( NS_USER, $wgUser->getName() );
937                 }
938         }
939 }
940
941 /**
942  * Shortcut to construct a special page pointing to current user talk page.
943  * @ingroup SpecialPage
944  */
945 class SpecialMytalk extends UnlistedSpecialPage {
946         function __construct() {
947                 parent::__construct( 'Mytalk' );
948                 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro', 'section' );
949         }
950
951         function getRedirect( $subpage ) {
952                 global $wgUser;
953                 if ( strval( $subpage ) !== '' ) {
954                         return Title::makeTitle( NS_USER_TALK, $wgUser->getName() . '/' . $subpage );
955                 } else {
956                         return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
957                 }
958         }
959 }
960
961 /**
962  * Shortcut to construct a special page pointing to current user contributions.
963  * @ingroup SpecialPage
964  */
965 class SpecialMycontributions extends UnlistedSpecialPage {
966         function __construct() {
967                 parent::__construct(  'Mycontributions' );
968                 $this->mAllowedRedirectParams = array( 'limit', 'namespace', 'tagfilter',
969                         'offset', 'dir', 'year', 'month', 'feed' );
970         }
971
972         function getRedirect( $subpage ) {
973                 global $wgUser;
974                 return SpecialPage::getTitleFor( 'Contributions', $wgUser->getName() );
975         }
976 }