Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / SpecialPageFactory.php
bloba53b9012569e955312015e489594f072652dc591
1 <?php
2 /**
3 * Factory for handling the special page list and generating SpecialPage objects.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup SpecialPage
22 * @defgroup SpecialPage SpecialPage
25 /**
26 * Factory for handling the special page list and generating SpecialPage objects.
28 * To add a special page in an extension, add to $wgSpecialPages either
29 * an object instance or an array containing the name and constructor
30 * parameters. The latter is preferred for performance reasons.
32 * The object instantiated must be either an instance of SpecialPage or a
33 * sub-class thereof. It must have an execute() method, which sends the HTML
34 * for the special page to $wgOut. The parent class has an execute() method
35 * which distributes the call to the historical global functions. Additionally,
36 * execute() also checks if the user has the necessary access privileges
37 * and bails out if not.
39 * To add a core special page, use the similar static list in
40 * SpecialPage::$mList. To remove a core static special page at runtime, use
41 * a SpecialPage_initList hook.
43 * @ingroup SpecialPage
44 * @since 1.17
46 class SpecialPageFactory {
48 /**
49 * List of special page names to the subclass of SpecialPage which handles them.
51 private static $mList = array(
52 // Maintenance Reports
53 'BrokenRedirects' => 'BrokenRedirectsPage',
54 'Deadendpages' => 'DeadendpagesPage',
55 'DoubleRedirects' => 'DoubleRedirectsPage',
56 'Longpages' => 'LongpagesPage',
57 'Ancientpages' => 'AncientpagesPage',
58 'Lonelypages' => 'LonelypagesPage',
59 'Fewestrevisions' => 'FewestrevisionsPage',
60 'Withoutinterwiki' => 'WithoutinterwikiPage',
61 'Protectedpages' => 'SpecialProtectedpages',
62 'Protectedtitles' => 'SpecialProtectedtitles',
63 'Shortpages' => 'ShortpagesPage',
64 'Uncategorizedcategories' => 'UncategorizedcategoriesPage',
65 'Uncategorizedimages' => 'UncategorizedimagesPage',
66 'Uncategorizedpages' => 'UncategorizedpagesPage',
67 'Uncategorizedtemplates' => 'UncategorizedtemplatesPage',
68 'Unusedcategories' => 'UnusedcategoriesPage',
69 'Unusedimages' => 'UnusedimagesPage',
70 'Unusedtemplates' => 'UnusedtemplatesPage',
71 'Unwatchedpages' => 'UnwatchedpagesPage',
72 'Wantedcategories' => 'WantedcategoriesPage',
73 'Wantedfiles' => 'WantedfilesPage',
74 'Wantedpages' => 'WantedpagesPage',
75 'Wantedtemplates' => 'WantedtemplatesPage',
77 // List of pages
78 'Allpages' => 'SpecialAllpages',
79 'Prefixindex' => 'SpecialPrefixindex',
80 'Categories' => 'SpecialCategories',
81 'Disambiguations' => 'DisambiguationsPage',
82 'Listredirects' => 'ListredirectsPage',
83 'PagesWithProp' => 'SpecialPagesWithProp',
85 // Login/create account
86 'Userlogin' => 'LoginForm',
87 'CreateAccount' => 'SpecialCreateAccount',
89 // Users and rights
90 'Block' => 'SpecialBlock',
91 'Unblock' => 'SpecialUnblock',
92 'BlockList' => 'SpecialBlockList',
93 'ChangePassword' => 'SpecialChangePassword',
94 'PasswordReset' => 'SpecialPasswordReset',
95 'DeletedContributions' => 'DeletedContributionsPage',
96 'Preferences' => 'SpecialPreferences',
97 'Contributions' => 'SpecialContributions',
98 'Listgrouprights' => 'SpecialListGroupRights',
99 'Listusers' => 'SpecialListUsers',
100 'Listadmins' => 'SpecialListAdmins',
101 'Listbots' => 'SpecialListBots',
102 'Userrights' => 'UserrightsPage',
103 'EditWatchlist' => 'SpecialEditWatchlist',
105 // Recent changes and logs
106 'Newimages' => 'SpecialNewFiles',
107 'Log' => 'SpecialLog',
108 'Watchlist' => 'SpecialWatchlist',
109 'Newpages' => 'SpecialNewpages',
110 'Recentchanges' => 'SpecialRecentchanges',
111 'Recentchangeslinked' => 'SpecialRecentchangeslinked',
112 'Tags' => 'SpecialTags',
114 // Media reports and uploads
115 'Listfiles' => 'SpecialListFiles',
116 'Filepath' => 'SpecialFilepath',
117 'MIMEsearch' => 'MIMEsearchPage',
118 'FileDuplicateSearch' => 'FileDuplicateSearchPage',
119 'Upload' => 'SpecialUpload',
120 'UploadStash' => 'SpecialUploadStash',
122 // Data and tools
123 'Statistics' => 'SpecialStatistics',
124 'Allmessages' => 'SpecialAllmessages',
125 'Version' => 'SpecialVersion',
126 'Lockdb' => 'SpecialLockdb',
127 'Unlockdb' => 'SpecialUnlockdb',
129 // Redirecting special pages
130 'LinkSearch' => 'LinkSearchPage',
131 'Randompage' => 'Randompage',
132 'Randomredirect' => 'SpecialRandomredirect',
134 // High use pages
135 'Mostlinkedcategories' => 'MostlinkedCategoriesPage',
136 'Mostimages' => 'MostimagesPage',
137 'Mostinterwikis' => 'MostinterwikisPage',
138 'Mostlinked' => 'MostlinkedPage',
139 'Mostlinkedtemplates' => 'MostlinkedTemplatesPage',
140 'Mostcategories' => 'MostcategoriesPage',
141 'Mostrevisions' => 'MostrevisionsPage',
143 // Page tools
144 'ComparePages' => 'SpecialComparePages',
145 'Export' => 'SpecialExport',
146 'Import' => 'SpecialImport',
147 'Undelete' => 'SpecialUndelete',
148 'Whatlinkshere' => 'SpecialWhatlinkshere',
149 'MergeHistory' => 'SpecialMergeHistory',
151 // Other
152 'Booksources' => 'SpecialBookSources',
154 // Unlisted / redirects
155 'Blankpage' => 'SpecialBlankpage',
156 'Blockme' => 'SpecialBlockme',
157 'Emailuser' => 'SpecialEmailUser',
158 'Movepage' => 'MovePageForm',
159 'Mycontributions' => 'SpecialMycontributions',
160 'Mypage' => 'SpecialMypage',
161 'Mytalk' => 'SpecialMytalk',
162 'Myuploads' => 'SpecialMyuploads',
163 'PermanentLink' => 'SpecialPermanentLink',
164 'Revisiondelete' => 'SpecialRevisionDelete',
165 'Specialpages' => 'SpecialSpecialpages',
166 'Userlogout' => 'SpecialUserlogout',
169 private static $mAliases;
172 * Initialise the special page list
173 * This must be called before accessing SpecialPage::$mList
175 * @return array
177 static function getList() {
178 global $wgSpecialPages;
179 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
180 global $wgEnableEmail, $wgEnableJavaScriptTest;
182 if ( !is_object( self::$mList ) ) {
183 wfProfileIn( __METHOD__ );
185 if ( !$wgDisableCounters ) {
186 self::$mList['Popularpages'] = 'PopularpagesPage';
189 if ( !$wgDisableInternalSearch ) {
190 self::$mList['Search'] = 'SpecialSearch';
193 if ( $wgEmailAuthentication ) {
194 self::$mList['Confirmemail'] = 'EmailConfirmation';
195 self::$mList['Invalidateemail'] = 'EmailInvalidation';
198 if ( $wgEnableEmail ) {
199 self::$mList['ChangeEmail'] = 'SpecialChangeEmail';
202 if( $wgEnableJavaScriptTest ) {
203 self::$mList['JavaScriptTest'] = 'SpecialJavaScriptTest';
206 // Add extension special pages
207 self::$mList = array_merge( self::$mList, $wgSpecialPages );
209 // Run hooks
210 // This hook can be used to remove undesired built-in special pages
211 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
213 // Cast to object: func()[$key] doesn't work, but func()->$key does
214 settype( self::$mList, 'object' );
216 wfProfileOut( __METHOD__ );
218 return self::$mList;
222 * Initialise and return the list of special page aliases. Returns an object with
223 * properties which can be accessed $obj->pagename - each property is an array of
224 * aliases; the first in the array is the canonical alias. All registered special
225 * pages are guaranteed to have a property entry, and for that property array to
226 * contain at least one entry (English fallbacks will be added if necessary).
227 * @return Object
229 static function getAliasList() {
230 if ( !is_object( self::$mAliases ) ) {
231 global $wgContLang;
232 $aliases = $wgContLang->getSpecialPageAliases();
234 // Objects are passed by reference by default, need to create a copy
235 $missingPages = clone self::getList();
237 self::$mAliases = array();
238 foreach ( $aliases as $realName => $aliasList ) {
239 foreach ( $aliasList as $alias ) {
240 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
242 unset( $missingPages->$realName );
244 foreach ( $missingPages as $name => $stuff ) {
245 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
248 // Cast to object: func()[$key] doesn't work, but func()->$key does
249 self::$mAliases = (object)self::$mAliases;
251 return self::$mAliases;
255 * Given a special page name with a possible subpage, return an array
256 * where the first element is the special page name and the second is the
257 * subpage.
259 * @param $alias String
260 * @return Array( String, String|null ), or array( null, null ) if the page is invalid
262 public static function resolveAlias( $alias ) {
263 global $wgContLang;
264 $bits = explode( '/', $alias, 2 );
266 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
267 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
268 if ( isset( self::getAliasList()->$caseFoldedAlias ) ) {
269 $name = self::getAliasList()->$caseFoldedAlias;
270 } else {
271 return array( null, null );
274 if ( !isset( $bits[1] ) ) { // bug 2087
275 $par = null;
276 } else {
277 $par = $bits[1];
280 return array( $name, $par );
284 * Add a page to a certain display group for Special:SpecialPages
286 * @param $page Mixed: SpecialPage or string
287 * @param $group String
288 * @deprecated 1.21 Override SpecialPage::getGroupName
290 public static function setGroup( $page, $group ) {
291 wfDeprecated( __METHOD__, '1.21' );
293 global $wgSpecialPageGroups;
294 $name = is_object( $page ) ? $page->getName() : $page;
295 $wgSpecialPageGroups[$name] = $group;
299 * Get the group that the special page belongs in on Special:SpecialPage
301 * @param $page SpecialPage
302 * @return String
303 * @deprecated 1.21 Use SpecialPage::getFinalGroupName
305 public static function getGroup( &$page ) {
306 wfDeprecated( __METHOD__, '1.21' );
308 return $page->getFinalGroupName();
312 * Check if a given name exist as a special page or as a special page alias
314 * @param string $name name of a special page
315 * @return Boolean: true if a special page exists with this name
317 public static function exists( $name ) {
318 list( $title, /*...*/ ) = self::resolveAlias( $name );
319 return property_exists( self::getList(), $title );
323 * Find the object with a given name and return it (or NULL)
325 * @param string $name Special page name, may be localised and/or an alias
326 * @return SpecialPage|null SpecialPage object or null if the page doesn't exist
328 public static function getPage( $name ) {
329 list( $realName, /*...*/ ) = self::resolveAlias( $name );
330 if ( property_exists( self::getList(), $realName ) ) {
331 $rec = self::getList()->$realName;
332 if ( is_string( $rec ) ) {
333 $className = $rec;
334 return new $className;
335 } elseif ( is_array( $rec ) ) {
336 // @deprecated, officially since 1.18, unofficially since forever
337 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
338 $className = array_shift( $rec );
339 self::getList()->$realName = MWFunction::newObj( $className, $rec );
341 return self::getList()->$realName;
342 } else {
343 return null;
348 * Return categorised listable special pages which are available
349 * for the current user, and everyone.
351 * @param $user User object to check permissions, $wgUser will be used
352 * if not provided
353 * @return Array( String => Specialpage )
355 public static function getUsablePages( User $user = null ) {
356 $pages = array();
357 if ( $user === null ) {
358 global $wgUser;
359 $user = $wgUser;
361 foreach ( self::getList() as $name => $rec ) {
362 $page = self::getPage( $name );
363 if ( $page ) { // not null
364 $page->setContext( RequestContext::getMain() );
365 if ( $page->isListed()
366 && ( !$page->isRestricted() || $page->userCanExecute( $user ) )
368 $pages[$name] = $page;
372 return $pages;
376 * Return categorised listable special pages for all users
378 * @return Array( String => Specialpage )
380 public static function getRegularPages() {
381 $pages = array();
382 foreach ( self::getList() as $name => $rec ) {
383 $page = self::getPage( $name );
384 if ( $page->isListed() && !$page->isRestricted() ) {
385 $pages[$name] = $page;
388 return $pages;
392 * Return categorised listable special pages which are available
393 * for the current user, but not for everyone
395 * @return Array( String => Specialpage )
397 public static function getRestrictedPages() {
398 global $wgUser;
399 $pages = array();
400 foreach ( self::getList() as $name => $rec ) {
401 $page = self::getPage( $name );
402 if (
403 $page->isListed()
404 && $page->isRestricted()
405 && $page->userCanExecute( $wgUser )
407 $pages[$name] = $page;
410 return $pages;
414 * Execute a special page path.
415 * The path may contain parameters, e.g. Special:Name/Params
416 * Extracts the special page name and call the execute method, passing the parameters
418 * Returns a title object if the page is redirected, false if there was no such special
419 * page, and true if it was successful.
421 * @param $title Title object
422 * @param $context IContextSource
423 * @param $including Bool output is being captured for use in {{special:whatever}}
425 * @return bool
427 public static function executePath( Title &$title, IContextSource &$context, $including = false ) {
428 wfProfileIn( __METHOD__ );
430 // @todo FIXME: Redirects broken due to this call
431 $bits = explode( '/', $title->getDBkey(), 2 );
432 $name = $bits[0];
433 if ( !isset( $bits[1] ) ) { // bug 2087
434 $par = null;
435 } else {
436 $par = $bits[1];
438 $page = self::getPage( $name );
439 // Nonexistent?
440 if ( !$page ) {
441 $context->getOutput()->setArticleRelated( false );
442 $context->getOutput()->setRobotPolicy( 'noindex,nofollow' );
444 global $wgSend404Code;
445 if ( $wgSend404Code ) {
446 $context->getOutput()->setStatusCode( 404 );
449 $context->getOutput()->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
450 wfProfileOut( __METHOD__ );
451 return false;
454 // Page exists, set the context
455 $page->setContext( $context );
457 if ( !$including ) {
458 // Redirect to canonical alias for GET commands
459 // Not for POST, we'd lose the post data, so it's best to just distribute
460 // the request. Such POST requests are possible for old extensions that
461 // generate self-links without being aware that their default name has
462 // changed.
463 if ( $name != $page->getLocalName() && !$context->getRequest()->wasPosted() ) {
464 $query = $context->getRequest()->getQueryValues();
465 unset( $query['title'] );
466 $query = wfArrayToCgi( $query );
467 $title = $page->getTitle( $par );
468 $url = $title->getFullURL( $query );
469 $context->getOutput()->redirect( $url );
470 wfProfileOut( __METHOD__ );
471 return $title;
472 } else {
473 $context->setTitle( $page->getTitle( $par ) );
476 } elseif ( !$page->isIncludable() ) {
477 wfProfileOut( __METHOD__ );
478 return false;
481 $page->including( $including );
483 // Execute special page
484 $profName = 'Special:' . $page->getName();
485 wfProfileIn( $profName );
486 $page->run( $par );
487 wfProfileOut( $profName );
488 wfProfileOut( __METHOD__ );
489 return true;
493 * Just like executePath() but will override global variables and execute
494 * the page in "inclusion" mode. Returns true if the execution was
495 * successful or false if there was no such special page, or a title object
496 * if it was a redirect.
498 * Also saves the current $wgTitle, $wgOut, $wgRequest, $wgUser and $wgLang
499 * variables so that the special page will get the context it'd expect on a
500 * normal request, and then restores them to their previous values after.
502 * @param $title Title
503 * @param $context IContextSource
505 * @return String: HTML fragment
507 static function capturePath( Title $title, IContextSource $context ) {
508 global $wgOut, $wgTitle, $wgRequest, $wgUser, $wgLang;
510 // Save current globals
511 $oldTitle = $wgTitle;
512 $oldOut = $wgOut;
513 $oldRequest = $wgRequest;
514 $oldUser = $wgUser;
515 $oldLang = $wgLang;
517 // Set the globals to the current context
518 $wgTitle = $title;
519 $wgOut = $context->getOutput();
520 $wgRequest = $context->getRequest();
521 $wgUser = $context->getUser();
522 $wgLang = $context->getLanguage();
524 // The useful part
525 $ret = self::executePath( $title, $context, true );
527 // And restore the old globals
528 $wgTitle = $oldTitle;
529 $wgOut = $oldOut;
530 $wgRequest = $oldRequest;
531 $wgUser = $oldUser;
532 $wgLang = $oldLang;
534 return $ret;
538 * Get the local name for a specified canonical name
540 * @param $name String
541 * @param $subpage String|Bool
543 * @return String
545 static function getLocalNameFor( $name, $subpage = false ) {
546 global $wgContLang;
547 $aliases = $wgContLang->getSpecialPageAliases();
549 if ( isset( $aliases[$name][0] ) ) {
550 $name = $aliases[$name][0];
551 } else {
552 // Try harder in case someone misspelled the correct casing
553 $found = false;
554 foreach ( $aliases as $n => $values ) {
555 if ( strcasecmp( $name, $n ) === 0 ) {
556 wfWarn( "Found alias defined for $n when searching for " .
557 "special page aliases for $name. Case mismatch?" );
558 $name = $values[0];
559 $found = true;
560 break;
563 if ( !$found ) {
564 wfWarn( "Did not find alias for special page '$name'. " .
565 "Perhaps no aliases are defined for it?" );
568 if ( $subpage !== false && !is_null( $subpage ) ) {
569 $name = "$name/$subpage";
571 return $wgContLang->ucfirst( $name );
575 * Get a title for a given alias
577 * @param $alias String
579 * @return Title or null if there is no such alias
581 static function getTitleForAlias( $alias ) {
582 $name = self::resolveAlias( $alias );
583 if ( $name ) {
584 return SpecialPage::getTitleFor( $name );
585 } else {
586 return null;