Followup to r86053 - fix special page cases
[mediawiki.git] / includes / SpecialPageFactory.php
blob27cdc3eb35bdd01f25255867dbe19bdcfb23ced3
1 <?php
2 /**
3 * SpecialPage: handling special pages and lists thereof.
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.
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.
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.
20 * @file
21 * @ingroup SpecialPage
22 * @defgroup SpecialPage SpecialPage
25 /**
26 * Factory for handling the special page list and generating SpecialPage objects
27 * @ingroup SpecialPage
28 * @since 1.17
30 class SpecialPageFactory {
32 /**
33 * List of special page names to the subclass of SpecialPage which handles them.
35 private static $mList = array(
36 // Maintenance Reports
37 'BrokenRedirects' => 'BrokenRedirectsPage',
38 'Deadendpages' => 'DeadendpagesPage',
39 'DoubleRedirects' => 'DoubleRedirectsPage',
40 'Longpages' => 'LongpagesPage',
41 'Ancientpages' => 'AncientpagesPage',
42 'Lonelypages' => 'LonelypagesPage',
43 'Fewestrevisions' => 'FewestrevisionsPage',
44 'Withoutinterwiki' => 'WithoutinterwikiPage',
45 'Protectedpages' => 'SpecialProtectedpages',
46 'Protectedtitles' => 'SpecialProtectedtitles',
47 'Shortpages' => 'ShortpagesPage',
48 'Uncategorizedcategories' => 'UncategorizedcategoriesPage',
49 'Uncategorizedimages' => 'UncategorizedimagesPage',
50 'Uncategorizedpages' => 'UncategorizedpagesPage',
51 'Uncategorizedtemplates' => 'UncategorizedtemplatesPage',
52 'Unusedcategories' => 'UnusedcategoriesPage',
53 'Unusedimages' => 'UnusedimagesPage',
54 'Unusedtemplates' => 'UnusedtemplatesPage',
55 'Unwatchedpages' => 'UnwatchedpagesPage',
56 'Wantedcategories' => 'WantedcategoriesPage',
57 'Wantedfiles' => 'WantedfilesPage',
58 'Wantedpages' => 'WantedpagesPage',
59 'Wantedtemplates' => 'WantedtemplatesPage',
61 // List of pages
62 'Allpages' => 'SpecialAllpages',
63 'Prefixindex' => 'SpecialPrefixindex',
64 'Categories' => 'SpecialCategories',
65 'Disambiguations' => 'DisambiguationsPage',
66 'Listredirects' => 'ListredirectsPage',
68 // Login/create account
69 'Userlogin' => 'LoginForm',
70 'CreateAccount' => 'SpecialCreateAccount',
72 // Users and rights
73 'Block' => 'SpecialBlock',
74 'Unblock' => 'SpecialUnblock',
75 'BlockList' => 'SpecialBlockList',
76 'ChangePassword' => 'SpecialChangePassword',
77 'PasswordReset' => 'SpecialPasswordReset',
78 'DeletedContributions' => 'DeletedContributionsPage',
79 'Preferences' => 'SpecialPreferences',
80 'Contributions' => 'SpecialContributions',
81 'Listgrouprights' => 'SpecialListGroupRights',
82 'Listusers' => 'SpecialListUsers' ,
83 'Listadmins' => 'SpecialListAdmins',
84 'Listbots' => 'SpecialListBots',
85 'Activeusers' => 'SpecialActiveUsers',
86 'Userrights' => 'UserrightsPage',
87 'EditWatchlist' => 'SpecialEditWatchlist',
89 // Recent changes and logs
90 'Newimages' => 'SpecialNewFiles',
91 'Log' => 'SpecialLog',
92 'Watchlist' => 'SpecialWatchlist',
93 'Newpages' => 'SpecialNewpages',
94 'Recentchanges' => 'SpecialRecentchanges',
95 'Recentchangeslinked' => 'SpecialRecentchangeslinked',
96 'Tags' => 'SpecialTags',
98 // Media reports and uploads
99 'Listfiles' => 'SpecialListFiles',
100 'Filepath' => 'SpecialFilepath',
101 'MIMEsearch' => 'MIMEsearchPage',
102 'FileDuplicateSearch' => 'FileDuplicateSearchPage',
103 'Upload' => 'SpecialUpload',
104 'UploadStash' => 'SpecialUploadStash',
106 // Wiki data and tools
107 'Statistics' => 'SpecialStatistics',
108 'Allmessages' => 'SpecialAllmessages',
109 'Version' => 'SpecialVersion',
110 'Lockdb' => 'SpecialLockdb',
111 'Unlockdb' => 'SpecialUnlockdb',
113 // Redirecting special pages
114 'LinkSearch' => 'LinkSearchPage',
115 'Randompage' => 'Randompage',
116 'Randomredirect' => 'SpecialRandomredirect',
118 // High use pages
119 'Mostlinkedcategories' => 'MostlinkedCategoriesPage',
120 'Mostimages' => 'MostimagesPage',
121 'Mostlinked' => 'MostlinkedPage',
122 'Mostlinkedtemplates' => 'MostlinkedTemplatesPage',
123 'Mostcategories' => 'MostcategoriesPage',
124 'Mostrevisions' => 'MostrevisionsPage',
126 // Page tools
127 'ComparePages' => 'SpecialComparePages',
128 'Export' => 'SpecialExport',
129 'Import' => 'SpecialImport',
130 'Undelete' => 'SpecialUndelete',
131 'Whatlinkshere' => 'SpecialWhatlinkshere',
132 'MergeHistory' => 'SpecialMergeHistory',
134 // Other
135 'Booksources' => 'SpecialBookSources',
137 // Unlisted / redirects
138 'Blankpage' => 'SpecialBlankpage',
139 'Blockme' => 'SpecialBlockme',
140 'Emailuser' => 'SpecialEmailUser',
141 'Movepage' => 'MovePageForm',
142 'Mycontributions' => 'SpecialMycontributions',
143 'Mypage' => 'SpecialMypage',
144 'Mytalk' => 'SpecialMytalk',
145 'Myuploads' => 'SpecialMyuploads',
146 'PermanentLink' => 'SpecialPermanentLink',
147 'Revisiondelete' => 'SpecialRevisionDelete',
148 'Specialpages' => 'SpecialSpecialpages',
149 'Userlogout' => 'SpecialUserlogout',
152 private static $mAliases;
155 * Initialise the special page list
156 * This must be called before accessing SpecialPage::$mList
158 * @return array
160 static function getList() {
161 global $wgSpecialPages;
162 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
163 global $wgEnableEmail;
165 if ( !is_object( self::$mList ) ) {
166 wfProfileIn( __METHOD__ );
168 if ( !$wgDisableCounters ) {
169 self::$mList['Popularpages'] = 'PopularpagesPage';
172 if ( !$wgDisableInternalSearch ) {
173 self::$mList['Search'] = 'SpecialSearch';
176 if ( $wgEmailAuthentication ) {
177 self::$mList['Confirmemail'] = 'EmailConfirmation';
178 self::$mList['Invalidateemail'] = 'EmailInvalidation';
181 if ( $wgEnableEmail ) {
182 self::$mList['ChangeEmail'] = 'SpecialChangeEmail';
185 // Add extension special pages
186 self::$mList = array_merge( self::$mList, $wgSpecialPages );
188 // Run hooks
189 // This hook can be used to remove undesired built-in special pages
190 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
192 // Cast to object: func()[$key] doesn't work, but func()->$key does
193 settype( self::$mList, 'object' );
195 wfProfileOut( __METHOD__ );
197 return self::$mList;
201 * Initialise and return the list of special page aliases. Returns an object with
202 * properties which can be accessed $obj->pagename - each property is an array of
203 * aliases; the first in the array is the cannonical alias. All registered special
204 * pages are guaranteed to have a property entry, and for that property array to
205 * contain at least one entry (English fallbacks will be added if necessary).
206 * @return Object
208 static function getAliasList() {
209 if ( !is_object( self::$mAliases ) ) {
210 global $wgContLang;
211 $aliases = $wgContLang->getSpecialPageAliases();
213 // Objects are passed by reference by default, need to create a copy
214 $missingPages = clone self::getList();
216 self::$mAliases = array();
217 foreach ( $aliases as $realName => $aliasList ) {
218 foreach ( $aliasList as $alias ) {
219 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
221 unset( $missingPages->$realName );
223 foreach ( $missingPages as $name => $stuff ) {
224 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
227 // Cast to object: func()[$key] doesn't work, but func()->$key does
228 self::$mAliases = (object)self::$mAliases;
230 return self::$mAliases;
234 * Given a special page name with a possible subpage, return an array
235 * where the first element is the special page name and the second is the
236 * subpage.
238 * @param $alias String
239 * @return Array( String, String|null ), or array( null, null ) if the page is invalid
241 public static function resolveAlias( $alias ) {
242 global $wgContLang;
243 $bits = explode( '/', $alias, 2 );
245 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
246 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
247 if ( isset( self::getAliasList()->$caseFoldedAlias ) ) {
248 $name = self::getAliasList()->$caseFoldedAlias;
249 } else {
250 return array( null, null );
253 if ( !isset( $bits[1] ) ) { // bug 2087
254 $par = null;
255 } else {
256 $par = $bits[1];
259 return array( $name, $par );
263 * Add a page to a certain display group for Special:SpecialPages
265 * @param $page Mixed: SpecialPage or string
266 * @param $group String
268 public static function setGroup( $page, $group ) {
269 global $wgSpecialPageGroups;
270 $name = is_object( $page ) ? $page->getName() : $page;
271 $wgSpecialPageGroups[$name] = $group;
275 * Get the group that the special page belongs in on Special:SpecialPage
277 * @param $page SpecialPage
279 public static function getGroup( &$page ) {
280 $name = $page->getName();
282 global $wgSpecialPageGroups;
283 static $specialPageGroupsCache = array();
284 if ( isset( $specialPageGroupsCache[$name] ) ) {
285 return $specialPageGroupsCache[$name];
287 $msg = wfMessage( 'specialpages-specialpagegroup-' . strtolower( $name ) );
288 if ( !$msg->isBlank() ) {
289 $group = $msg->text();
290 } else {
291 $group = isset( $wgSpecialPageGroups[$name] )
292 ? $wgSpecialPageGroups[$name]
293 : '-';
295 if ( $group == '-' ) {
296 $group = 'other';
298 $specialPageGroupsCache[$name] = $group;
299 return $group;
303 * Check if a given name exist as a special page or as a special page alias
305 * @param $name String: name of a special page
306 * @return Boolean: true if a special page exists with this name
308 public static function exists( $name ) {
309 list( $title, /*...*/ ) = self::resolveAlias( $name );
310 return property_exists( self::getList(), $title );
314 * Find the object with a given name and return it (or NULL)
316 * @param $name String Special page name, may be localised and/or an alias
317 * @return SpecialPage object or null if the page doesn't exist
319 public static function getPage( $name ) {
320 list( $realName, /*...*/ ) = self::resolveAlias( $name );
321 if ( property_exists( self::getList(), $realName ) ) {
322 $rec = self::getList()->$realName;
323 if ( is_string( $rec ) ) {
324 $className = $rec;
325 return new $className;
326 } elseif ( is_array( $rec ) ) {
327 // @deprecated, officially since 1.18, unofficially since forever
328 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
329 $className = array_shift( $rec );
330 self::getList()->$realName = MWFunction::newObj( $className, $rec );
332 return self::getList()->$realName;
333 } else {
334 return null;
339 * Return categorised listable special pages which are available
340 * for the current user, and everyone.
342 * @param $user User object to check permissions, $wgUser will be used
343 * if not provided
344 * @return Array( String => Specialpage )
346 public static function getUsablePages( User $user = null ) {
347 $pages = array();
348 if ( $user === null ) {
349 global $wgUser;
350 $user = $wgUser;
352 foreach ( self::getList() as $name => $rec ) {
353 $page = self::getPage( $name );
354 if ( $page // not null
355 && $page->isListed()
356 && ( !$page->isRestricted() || $page->userCanExecute( $user ) )
358 $pages[$name] = $page;
361 return $pages;
365 * Return categorised listable special pages for all users
367 * @return Array( String => Specialpage )
369 public static function getRegularPages() {
370 $pages = array();
371 foreach ( self::getList() as $name => $rec ) {
372 $page = self::getPage( $name );
373 if ( $page->isListed() && !$page->isRestricted() ) {
374 $pages[$name] = $page;
377 return $pages;
381 * Return categorised listable special pages which are available
382 * for the current user, but not for everyone
384 * @return Array( String => Specialpage )
386 public static function getRestrictedPages() {
387 global $wgUser;
388 $pages = array();
389 foreach ( self::getList() as $name => $rec ) {
390 $page = self::getPage( $name );
391 if (
392 $page->isListed()
393 && $page->isRestricted()
394 && $page->userCanExecute( $wgUser )
396 $pages[$name] = $page;
399 return $pages;
403 * Execute a special page path.
404 * The path may contain parameters, e.g. Special:Name/Params
405 * Extracts the special page name and call the execute method, passing the parameters
407 * Returns a title object if the page is redirected, false if there was no such special
408 * page, and true if it was successful.
410 * @param $title Title object
411 * @param $context IContextSource
412 * @param $including Bool output is being captured for use in {{special:whatever}}
414 * @return bool
416 public static function executePath( Title &$title, IContextSource &$context, $including = false ) {
417 wfProfileIn( __METHOD__ );
419 // @todo FIXME: Redirects broken due to this call
420 $bits = explode( '/', $title->getDBkey(), 2 );
421 $name = $bits[0];
422 if ( !isset( $bits[1] ) ) { // bug 2087
423 $par = null;
424 } else {
425 $par = $bits[1];
427 $page = self::getPage( $name );
428 // Nonexistent?
429 if ( !$page ) {
430 $context->getOutput()->setArticleRelated( false );
431 $context->getOutput()->setRobotPolicy( 'noindex,nofollow' );
432 $context->getOutput()->setStatusCode( 404 );
433 $context->getOutput()->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
434 wfProfileOut( __METHOD__ );
435 return false;
438 // Page exists, set the context
439 $page->setContext( $context );
441 if ( !$including ) {
442 // Redirect to canonical alias for GET commands
443 // Not for POST, we'd lose the post data, so it's best to just distribute
444 // the request. Such POST requests are possible for old extensions that
445 // generate self-links without being aware that their default name has
446 // changed.
447 if ( $name != $page->getLocalName() && !$context->getRequest()->wasPosted() ) {
448 $query = $context->getRequest()->getQueryValues();
449 unset( $query['title'] );
450 $query = wfArrayToCGI( $query );
451 $title = $page->getTitle( $par );
452 $url = $title->getFullUrl( $query );
453 $context->getOutput()->redirect( $url );
454 wfProfileOut( __METHOD__ );
455 return $title;
456 } else {
457 $context->setTitle( $page->getTitle( $par ) );
460 } elseif ( !$page->isIncludable() ) {
461 wfProfileOut( __METHOD__ );
462 return false;
465 $page->including( $including );
467 // Execute special page
468 $profName = 'Special:' . $page->getName();
469 wfProfileIn( $profName );
470 $page->execute( $par );
471 wfProfileOut( $profName );
472 wfProfileOut( __METHOD__ );
473 return true;
477 * Just like executePath() but will override global variables and execute
478 * the page in "inclusion" mode. Returns true if the execution was
479 * successful or false if there was no such special page, or a title object
480 * if it was a redirect.
482 * Also saves the current $wgTitle, $wgOut, $wgRequest, $wgUser and $wgLang
483 * variables so that the special page will get the context it'd expect on a
484 * normal request, and then restores them to their previous values after.
486 * @param $title Title
487 * @param $context IContextSource
489 * @return String: HTML fragment
491 static function capturePath( Title $title, IContextSource $context ) {
492 global $wgOut, $wgTitle, $wgRequest, $wgUser, $wgLang;
494 // Save current globals
495 $oldTitle = $wgTitle;
496 $oldOut = $wgOut;
497 $oldRequest = $wgRequest;
498 $oldUser = $wgUser;
499 $oldLang = $wgLang;
501 // Set the globals to the current context
502 $wgTitle = $title;
503 $wgOut = $context->getOutput();
504 $wgRequest = $context->getRequest();
505 $wgUser = $context->getUser();
506 $wgLang = $context->getLang();
508 // The useful part
509 $ret = self::executePath( $title, $context, true );
511 // And restore the old globals
512 $wgTitle = $oldTitle;
513 $wgOut = $oldOut;
514 $wgRequest = $oldRequest;
515 $wgUser = $oldUser;
516 $wgLang = $oldLang;
518 return $ret;
522 * Get the local name for a specified canonical name
524 * @param $name String
525 * @param $subpage String|Bool
527 * @return String
529 static function getLocalNameFor( $name, $subpage = false ) {
530 global $wgContLang;
531 $aliases = $wgContLang->getSpecialPageAliases();
533 if ( isset( $aliases[$name][0] ) ) {
534 $name = $aliases[$name][0];
535 } else {
536 // Try harder in case someone misspelled the correct casing
537 $found = false;
538 foreach ( $aliases as $n => $values ) {
539 if ( strcasecmp( $name, $n ) === 0 ) {
540 wfWarn( "Found alias defined for $n when searching for " .
541 "special page aliases for $name. Case mismatch?" );
542 $name = $values[0];
543 $found = true;
544 break;
547 if ( !$found ) {
548 wfWarn( "Did not find alias for special page '$name'. " .
549 "Perhaps no aliases are defined for it?" );
552 if ( $subpage !== false && !is_null( $subpage ) ) {
553 $name = "$name/$subpage";
555 return $wgContLang->ucfirst( $name );
559 * Get a title for a given alias
561 * @param $alias String
563 * @return Title or null if there is no such alias
565 static function getTitleForAlias( $alias ) {
566 $name = self::resolveAlias( $alias );
567 if ( $name ) {
568 return SpecialPage::getTitleFor( $name );
569 } else {
570 return null;