wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / specialpage / SpecialPageFactory.php
blob47b4fc89f1ecc416c4412279a0ddb3d636e336c8
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 * SpecialPageFactory::$list. 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 {
47 /**
48 * List of special page names to the subclass of SpecialPage which handles them.
50 private static $coreList = array(
51 // Maintenance Reports
52 'BrokenRedirects' => 'BrokenRedirectsPage',
53 'Deadendpages' => 'DeadendPagesPage',
54 'DoubleRedirects' => 'DoubleRedirectsPage',
55 'Longpages' => 'LongPagesPage',
56 'Ancientpages' => 'AncientPagesPage',
57 'Lonelypages' => 'LonelyPagesPage',
58 'Fewestrevisions' => 'FewestrevisionsPage',
59 'Withoutinterwiki' => 'WithoutInterwikiPage',
60 'Protectedpages' => 'SpecialProtectedpages',
61 'Protectedtitles' => 'SpecialProtectedtitles',
62 'Shortpages' => 'ShortPagesPage',
63 'Uncategorizedcategories' => 'UncategorizedCategoriesPage',
64 'Uncategorizedimages' => 'UncategorizedImagesPage',
65 'Uncategorizedpages' => 'UncategorizedPagesPage',
66 'Uncategorizedtemplates' => 'UncategorizedTemplatesPage',
67 'Unusedcategories' => 'UnusedCategoriesPage',
68 'Unusedimages' => 'UnusedimagesPage',
69 'Unusedtemplates' => 'UnusedtemplatesPage',
70 'Unwatchedpages' => 'UnwatchedpagesPage',
71 'Wantedcategories' => 'WantedCategoriesPage',
72 'Wantedfiles' => 'WantedFilesPage',
73 'Wantedpages' => 'WantedPagesPage',
74 'Wantedtemplates' => 'WantedTemplatesPage',
76 // List of pages
77 'Allpages' => 'SpecialAllPages',
78 'Prefixindex' => 'SpecialPrefixindex',
79 'Categories' => 'SpecialCategories',
80 'Listredirects' => 'ListredirectsPage',
81 'PagesWithProp' => 'SpecialPagesWithProp',
82 'TrackingCategories' => 'SpecialTrackingCategories',
84 // Login/create account
85 'Userlogin' => 'LoginForm',
86 'CreateAccount' => 'SpecialCreateAccount',
88 // Users and rights
89 'Block' => 'SpecialBlock',
90 'Unblock' => 'SpecialUnblock',
91 'BlockList' => 'SpecialBlockList',
92 'ChangePassword' => 'SpecialChangePassword',
93 'PasswordReset' => 'SpecialPasswordReset',
94 'DeletedContributions' => 'DeletedContributionsPage',
95 'Preferences' => 'SpecialPreferences',
96 'ResetTokens' => 'SpecialResetTokens',
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 'MediaStatistics' => 'MediaStatisticsPage',
118 'MIMEsearch' => 'MIMEsearchPage',
119 'FileDuplicateSearch' => 'FileDuplicateSearchPage',
120 'Upload' => 'SpecialUpload',
121 'UploadStash' => 'SpecialUploadStash',
122 'ListDuplicatedFiles' => 'ListDuplicatedFilesPage',
124 // Data and tools
125 'Statistics' => 'SpecialStatistics',
126 'Allmessages' => 'SpecialAllMessages',
127 'Version' => 'SpecialVersion',
128 'Lockdb' => 'SpecialLockdb',
129 'Unlockdb' => 'SpecialUnlockdb',
131 // Redirecting special pages
132 'LinkSearch' => 'LinkSearchPage',
133 'Randompage' => 'RandomPage',
134 'RandomInCategory' => 'SpecialRandomInCategory',
135 'Randomredirect' => 'SpecialRandomredirect',
137 // High use pages
138 'Mostlinkedcategories' => 'MostlinkedCategoriesPage',
139 'Mostimages' => 'MostimagesPage',
140 'Mostinterwikis' => 'MostinterwikisPage',
141 'Mostlinked' => 'MostlinkedPage',
142 'Mostlinkedtemplates' => 'MostlinkedTemplatesPage',
143 'Mostcategories' => 'MostcategoriesPage',
144 'Mostrevisions' => 'MostrevisionsPage',
146 // Page tools
147 'ComparePages' => 'SpecialComparePages',
148 'Export' => 'SpecialExport',
149 'Import' => 'SpecialImport',
150 'Undelete' => 'SpecialUndelete',
151 'Whatlinkshere' => 'SpecialWhatLinksHere',
152 'MergeHistory' => 'SpecialMergeHistory',
153 'ExpandTemplates' => 'SpecialExpandTemplates',
155 // Other
156 'Booksources' => 'SpecialBookSources',
158 // Unlisted / redirects
159 'ApiHelp' => 'SpecialApiHelp',
160 'Blankpage' => 'SpecialBlankpage',
161 'Diff' => 'SpecialDiff',
162 'EditTags' => 'SpecialEditTags',
163 'Emailuser' => 'SpecialEmailUser',
164 'Movepage' => 'MovePageForm',
165 'Mycontributions' => 'SpecialMycontributions',
166 'MyLanguage' => 'SpecialMyLanguage',
167 'Mypage' => 'SpecialMypage',
168 'Mytalk' => 'SpecialMytalk',
169 'Myuploads' => 'SpecialMyuploads',
170 'AllMyUploads' => 'SpecialAllMyUploads',
171 'PermanentLink' => 'SpecialPermanentLink',
172 'Redirect' => 'SpecialRedirect',
173 'Revisiondelete' => 'SpecialRevisionDelete',
174 'RunJobs' => 'SpecialRunJobs',
175 'Specialpages' => 'SpecialSpecialpages',
176 'Userlogout' => 'SpecialUserlogout',
179 private static $list;
180 private static $aliases;
183 * Reset the internal list of special pages. Useful when changing $wgSpecialPages after
184 * the internal list has already been initialized, e.g. during testing.
186 public static function resetList() {
187 self::$list = null;
188 self::$aliases = null;
192 * Returns a list of canonical special page names.
193 * May be used to iterate over all registered special pages.
195 * @return string[]
197 public static function getNames() {
198 return array_keys( self::getPageList() );
202 * Get the special page list as an array
204 * @deprecated since 1.24, use getNames() instead.
205 * @return array
207 public static function getList() {
208 wfDeprecated( __FUNCTION__, '1.24' );
209 return self::getPageList();
213 * Get the special page list as an array
215 * @return array
217 private static function getPageList() {
218 global $wgSpecialPages;
219 global $wgDisableInternalSearch, $wgEmailAuthentication;
220 global $wgEnableEmail, $wgEnableJavaScriptTest;
221 global $wgPageLanguageUseDB, $wgContentHandlerUseDB;
223 if ( !is_array( self::$list ) ) {
225 self::$list = self::$coreList;
227 if ( !$wgDisableInternalSearch ) {
228 self::$list['Search'] = 'SpecialSearch';
231 if ( $wgEmailAuthentication ) {
232 self::$list['Confirmemail'] = 'EmailConfirmation';
233 self::$list['Invalidateemail'] = 'EmailInvalidation';
236 if ( $wgEnableEmail ) {
237 self::$list['ChangeEmail'] = 'SpecialChangeEmail';
240 if ( $wgEnableJavaScriptTest ) {
241 self::$list['JavaScriptTest'] = 'SpecialJavaScriptTest';
244 if ( $wgPageLanguageUseDB ) {
245 self::$list['PageLanguage'] = 'SpecialPageLanguage';
247 if ( $wgContentHandlerUseDB ) {
248 self::$list['ChangeContentModel'] = 'SpecialChangeContentModel';
251 self::$list['Activeusers'] = 'SpecialActiveUsers';
253 // Add extension special pages
254 self::$list = array_merge( self::$list, $wgSpecialPages );
256 // This hook can be used to disable unwanted core special pages
257 // or conditionally register special pages.
258 Hooks::run( 'SpecialPage_initList', array( &self::$list ) );
262 return self::$list;
266 * Initialise and return the list of special page aliases. Returns an array where
267 * the key is an alias, and the value is the canonical name of the special page.
268 * All registered special pages are guaranteed to map to themselves.
269 * @return array
271 private static function getAliasList() {
272 if ( is_null( self::$aliases ) ) {
273 global $wgContLang;
274 $aliases = $wgContLang->getSpecialPageAliases();
275 $pageList = self::getPageList();
277 self::$aliases = array();
278 $keepAlias = array();
280 // Force every canonical name to be an alias for itself.
281 foreach ( $pageList as $name => $stuff ) {
282 $caseFoldedAlias = $wgContLang->caseFold( $name );
283 self::$aliases[$caseFoldedAlias] = $name;
284 $keepAlias[$caseFoldedAlias] = 'canonical';
287 // Check for $aliases being an array since Language::getSpecialPageAliases can return null
288 if ( is_array( $aliases ) ) {
289 foreach ( $aliases as $realName => $aliasList ) {
290 $aliasList = array_values( $aliasList );
291 foreach ( $aliasList as $i => $alias ) {
292 $caseFoldedAlias = $wgContLang->caseFold( $alias );
294 if ( isset( self::$aliases[$caseFoldedAlias] ) &&
295 $realName === self::$aliases[$caseFoldedAlias]
297 // Ignore same-realName conflicts
298 continue;
301 if ( !isset( $keepAlias[$caseFoldedAlias] ) ) {
302 self::$aliases[$caseFoldedAlias] = $realName;
303 if ( !$i ) {
304 $keepAlias[$caseFoldedAlias] = 'first';
306 } elseif ( !$i ) {
307 wfWarn( "First alias '$alias' for $realName conflicts with " .
308 "{$keepAlias[$caseFoldedAlias]} alias for " .
309 self::$aliases[$caseFoldedAlias]
317 return self::$aliases;
321 * Given a special page name with a possible subpage, return an array
322 * where the first element is the special page name and the second is the
323 * subpage.
325 * @param string $alias
326 * @return array Array( String, String|null ), or array( null, null ) if the page is invalid
328 public static function resolveAlias( $alias ) {
329 global $wgContLang;
330 $bits = explode( '/', $alias, 2 );
332 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
333 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
334 $aliases = self::getAliasList();
335 if ( isset( $aliases[$caseFoldedAlias] ) ) {
336 $name = $aliases[$caseFoldedAlias];
337 } else {
338 return array( null, null );
341 if ( !isset( $bits[1] ) ) { // bug 2087
342 $par = null;
343 } else {
344 $par = $bits[1];
347 return array( $name, $par );
351 * Check if a given name exist as a special page or as a special page alias
353 * @param string $name Name of a special page
354 * @return bool True if a special page exists with this name
356 public static function exists( $name ) {
357 list( $title, /*...*/ ) = self::resolveAlias( $name );
359 $specialPageList = self::getPageList();
360 return isset( $specialPageList[$title] );
364 * Find the object with a given name and return it (or NULL)
366 * @param string $name Special page name, may be localised and/or an alias
367 * @return SpecialPage|null SpecialPage object or null if the page doesn't exist
369 public static function getPage( $name ) {
370 list( $realName, /*...*/ ) = self::resolveAlias( $name );
372 $specialPageList = self::getPageList();
374 if ( isset( $specialPageList[$realName] ) ) {
375 $rec = $specialPageList[$realName];
377 if ( is_callable( $rec ) ) {
378 // Use callback to instantiate the special page
379 $page = call_user_func( $rec );
380 } elseif ( is_string( $rec ) ) {
381 $className = $rec;
382 $page = new $className;
383 } elseif ( is_array( $rec ) ) {
384 $className = array_shift( $rec );
385 // @deprecated, officially since 1.18, unofficially since forever
386 wfDeprecated( "Array syntax for \$wgSpecialPages is deprecated ($className), " .
387 "define a subclass of SpecialPage instead.", '1.18' );
388 $page = ObjectFactory::getObjectFromSpec( array(
389 'class' => $className,
390 'args' => $rec,
391 'closure_expansion' => false,
392 ) );
393 } elseif ( $rec instanceof SpecialPage ) {
394 $page = $rec; // XXX: we should deep clone here
395 } else {
396 $page = null;
399 if ( $page instanceof SpecialPage ) {
400 return $page;
401 } else {
402 // It's not a classname, nor a callback, nor a legacy constructor array,
403 // nor a special page object. Give up.
404 wfLogWarning( "Cannot instantiate special page $realName: bad spec!" );
405 return null;
408 } else {
409 return null;
414 * Return categorised listable special pages which are available
415 * for the current user, and everyone.
417 * @param User $user User object to check permissions, $wgUser will be used
418 * if not provided
419 * @return array ( string => Specialpage )
421 public static function getUsablePages( User $user = null ) {
422 $pages = array();
423 if ( $user === null ) {
424 global $wgUser;
425 $user = $wgUser;
427 foreach ( self::getPageList() as $name => $rec ) {
428 $page = self::getPage( $name );
429 if ( $page ) { // not null
430 $page->setContext( RequestContext::getMain() );
431 if ( $page->isListed()
432 && ( !$page->isRestricted() || $page->userCanExecute( $user ) )
434 $pages[$name] = $page;
439 return $pages;
443 * Return categorised listable special pages for all users
445 * @return array ( string => Specialpage )
447 public static function getRegularPages() {
448 $pages = array();
449 foreach ( self::getPageList() as $name => $rec ) {
450 $page = self::getPage( $name );
451 if ( $page->isListed() && !$page->isRestricted() ) {
452 $pages[$name] = $page;
456 return $pages;
460 * Return categorised listable special pages which are available
461 * for the current user, but not for everyone
463 * @param User|null $user User object to use or null for $wgUser
464 * @return array ( string => Specialpage )
466 public static function getRestrictedPages( User $user = null ) {
467 $pages = array();
468 if ( $user === null ) {
469 global $wgUser;
470 $user = $wgUser;
472 foreach ( self::getPageList() as $name => $rec ) {
473 $page = self::getPage( $name );
474 if (
475 $page->isListed()
476 && $page->isRestricted()
477 && $page->userCanExecute( $user )
479 $pages[$name] = $page;
483 return $pages;
487 * Execute a special page path.
488 * The path may contain parameters, e.g. Special:Name/Params
489 * Extracts the special page name and call the execute method, passing the parameters
491 * Returns a title object if the page is redirected, false if there was no such special
492 * page, and true if it was successful.
494 * @param Title $title
495 * @param IContextSource $context
496 * @param bool $including Bool output is being captured for use in {{special:whatever}}
498 * @return bool
500 public static function executePath( Title &$title, IContextSource &$context, $including = false ) {
502 // @todo FIXME: Redirects broken due to this call
503 $bits = explode( '/', $title->getDBkey(), 2 );
504 $name = $bits[0];
505 if ( !isset( $bits[1] ) ) { // bug 2087
506 $par = null;
507 } else {
508 $par = $bits[1];
510 $page = self::getPage( $name );
511 // Nonexistent?
512 if ( !$page ) {
513 $context->getOutput()->setArticleRelated( false );
514 $context->getOutput()->setRobotPolicy( 'noindex,nofollow' );
516 global $wgSend404Code;
517 if ( $wgSend404Code ) {
518 $context->getOutput()->setStatusCode( 404 );
521 $context->getOutput()->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
523 return false;
526 // Page exists, set the context
527 $page->setContext( $context );
529 if ( !$including ) {
530 // Redirect to canonical alias for GET commands
531 // Not for POST, we'd lose the post data, so it's best to just distribute
532 // the request. Such POST requests are possible for old extensions that
533 // generate self-links without being aware that their default name has
534 // changed.
535 if ( $name != $page->getLocalName() && !$context->getRequest()->wasPosted() ) {
536 $query = $context->getRequest()->getQueryValues();
537 unset( $query['title'] );
538 $title = $page->getPageTitle( $par );
539 $url = $title->getFullURL( $query );
540 $context->getOutput()->redirect( $url );
542 return $title;
543 } else {
544 $context->setTitle( $page->getPageTitle( $par ) );
546 } elseif ( !$page->isIncludable() ) {
547 return false;
550 $page->including( $including );
552 // Execute special page
553 $page->run( $par );
555 return true;
559 * Just like executePath() but will override global variables and execute
560 * the page in "inclusion" mode. Returns true if the execution was
561 * successful or false if there was no such special page, or a title object
562 * if it was a redirect.
564 * Also saves the current $wgTitle, $wgOut, $wgRequest, $wgUser and $wgLang
565 * variables so that the special page will get the context it'd expect on a
566 * normal request, and then restores them to their previous values after.
568 * @param Title $title
569 * @param IContextSource $context
570 * @return string HTML fragment
572 public static function capturePath( Title $title, IContextSource $context ) {
573 global $wgOut, $wgTitle, $wgRequest, $wgUser, $wgLang;
575 // Save current globals
576 $oldTitle = $wgTitle;
577 $oldOut = $wgOut;
578 $oldRequest = $wgRequest;
579 $oldUser = $wgUser;
580 $oldLang = $wgLang;
582 // Set the globals to the current context
583 $wgTitle = $title;
584 $wgOut = $context->getOutput();
585 $wgRequest = $context->getRequest();
586 $wgUser = $context->getUser();
587 $wgLang = $context->getLanguage();
589 // The useful part
590 $ret = self::executePath( $title, $context, true );
592 // And restore the old globals
593 $wgTitle = $oldTitle;
594 $wgOut = $oldOut;
595 $wgRequest = $oldRequest;
596 $wgUser = $oldUser;
597 $wgLang = $oldLang;
599 return $ret;
603 * Get the local name for a specified canonical name
605 * @param string $name
606 * @param string|bool $subpage
607 * @return string
609 public static function getLocalNameFor( $name, $subpage = false ) {
610 global $wgContLang;
611 $aliases = $wgContLang->getSpecialPageAliases();
612 $aliasList = self::getAliasList();
614 // Find the first alias that maps back to $name
615 if ( isset( $aliases[$name] ) ) {
616 $found = false;
617 foreach ( $aliases[$name] as $alias ) {
618 $caseFoldedAlias = $wgContLang->caseFold( $alias );
619 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
620 if ( isset( $aliasList[$caseFoldedAlias] ) &&
621 $aliasList[$caseFoldedAlias] === $name
623 $name = $alias;
624 $found = true;
625 break;
628 if ( !$found ) {
629 wfWarn( "Did not find a usable alias for special page '$name'. " .
630 "It seems all defined aliases conflict?" );
632 } else {
633 // Check if someone misspelled the correct casing
634 if ( is_array( $aliases ) ) {
635 foreach ( $aliases as $n => $values ) {
636 if ( strcasecmp( $name, $n ) === 0 ) {
637 wfWarn( "Found alias defined for $n when searching for " .
638 "special page aliases for $name. Case mismatch?" );
639 return self::getLocalNameFor( $n, $subpage );
644 wfWarn( "Did not find alias for special page '$name'. " .
645 "Perhaps no aliases are defined for it?" );
648 if ( $subpage !== false && !is_null( $subpage ) ) {
649 $name = "$name/$subpage";
652 return $wgContLang->ucfirst( $name );
656 * Get a title for a given alias
658 * @param string $alias
659 * @return Title|null Title or null if there is no such alias
661 public static function getTitleForAlias( $alias ) {
662 list( $name, $subpage ) = self::resolveAlias( $alias );
663 if ( $name != null ) {
664 return SpecialPage::getTitleFor( $name, $subpage );
665 } else {
666 return null;