(bug 35565) Special:Log/patrol doesn't indicate whether patrolling was automatic
[mediawiki.git] / includes / SpecialPageFactory.php
blob66104514b850f1a3bb682ac0c66a0eaf2a35a99d
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 'JavaScriptTest' => 'SpecialJavaScriptTest',
142 'Movepage' => 'MovePageForm',
143 'Mycontributions' => 'SpecialMycontributions',
144 'Mypage' => 'SpecialMypage',
145 'Mytalk' => 'SpecialMytalk',
146 'Myuploads' => 'SpecialMyuploads',
147 'PermanentLink' => 'SpecialPermanentLink',
148 'Revisiondelete' => 'SpecialRevisionDelete',
149 'Specialpages' => 'SpecialSpecialpages',
150 'Userlogout' => 'SpecialUserlogout',
153 private static $mAliases;
156 * Initialise the special page list
157 * This must be called before accessing SpecialPage::$mList
159 * @return array
161 static function getList() {
162 global $wgSpecialPages;
163 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
164 global $wgEnableEmail;
166 if ( !is_object( self::$mList ) ) {
167 wfProfileIn( __METHOD__ );
169 if ( !$wgDisableCounters ) {
170 self::$mList['Popularpages'] = 'PopularpagesPage';
173 if ( !$wgDisableInternalSearch ) {
174 self::$mList['Search'] = 'SpecialSearch';
177 if ( $wgEmailAuthentication ) {
178 self::$mList['Confirmemail'] = 'EmailConfirmation';
179 self::$mList['Invalidateemail'] = 'EmailInvalidation';
182 if ( $wgEnableEmail ) {
183 self::$mList['ChangeEmail'] = 'SpecialChangeEmail';
186 // Add extension special pages
187 self::$mList = array_merge( self::$mList, $wgSpecialPages );
189 // Run hooks
190 // This hook can be used to remove undesired built-in special pages
191 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
193 // Cast to object: func()[$key] doesn't work, but func()->$key does
194 settype( self::$mList, 'object' );
196 wfProfileOut( __METHOD__ );
198 return self::$mList;
202 * Initialise and return the list of special page aliases. Returns an object with
203 * properties which can be accessed $obj->pagename - each property is an array of
204 * aliases; the first in the array is the cannonical alias. All registered special
205 * pages are guaranteed to have a property entry, and for that property array to
206 * contain at least one entry (English fallbacks will be added if necessary).
207 * @return Object
209 static function getAliasList() {
210 if ( !is_object( self::$mAliases ) ) {
211 global $wgContLang;
212 $aliases = $wgContLang->getSpecialPageAliases();
214 // Objects are passed by reference by default, need to create a copy
215 $missingPages = clone self::getList();
217 self::$mAliases = array();
218 foreach ( $aliases as $realName => $aliasList ) {
219 foreach ( $aliasList as $alias ) {
220 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
222 unset( $missingPages->$realName );
224 foreach ( $missingPages as $name => $stuff ) {
225 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
228 // Cast to object: func()[$key] doesn't work, but func()->$key does
229 self::$mAliases = (object)self::$mAliases;
231 return self::$mAliases;
235 * Given a special page name with a possible subpage, return an array
236 * where the first element is the special page name and the second is the
237 * subpage.
239 * @param $alias String
240 * @return Array( String, String|null ), or array( null, null ) if the page is invalid
242 public static function resolveAlias( $alias ) {
243 global $wgContLang;
244 $bits = explode( '/', $alias, 2 );
246 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
247 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
248 if ( isset( self::getAliasList()->$caseFoldedAlias ) ) {
249 $name = self::getAliasList()->$caseFoldedAlias;
250 } else {
251 return array( null, null );
254 if ( !isset( $bits[1] ) ) { // bug 2087
255 $par = null;
256 } else {
257 $par = $bits[1];
260 return array( $name, $par );
264 * Add a page to a certain display group for Special:SpecialPages
266 * @param $page Mixed: SpecialPage or string
267 * @param $group String
269 public static function setGroup( $page, $group ) {
270 global $wgSpecialPageGroups;
271 $name = is_object( $page ) ? $page->getName() : $page;
272 $wgSpecialPageGroups[$name] = $group;
276 * Get the group that the special page belongs in on Special:SpecialPage
278 * @param $page SpecialPage
279 * @return String
281 public static function getGroup( &$page ) {
282 $name = $page->getName();
284 global $wgSpecialPageGroups;
285 static $specialPageGroupsCache = array();
286 if ( isset( $specialPageGroupsCache[$name] ) ) {
287 return $specialPageGroupsCache[$name];
289 $msg = wfMessage( 'specialpages-specialpagegroup-' . strtolower( $name ) );
290 if ( !$msg->isBlank() ) {
291 $group = $msg->text();
292 } else {
293 $group = isset( $wgSpecialPageGroups[$name] )
294 ? $wgSpecialPageGroups[$name]
295 : '-';
297 if ( $group == '-' ) {
298 $group = 'other';
300 $specialPageGroupsCache[$name] = $group;
301 return $group;
305 * Check if a given name exist as a special page or as a special page alias
307 * @param $name String: name of a special page
308 * @return Boolean: true if a special page exists with this name
310 public static function exists( $name ) {
311 list( $title, /*...*/ ) = self::resolveAlias( $name );
312 return property_exists( self::getList(), $title );
316 * Find the object with a given name and return it (or NULL)
318 * @param $name String Special page name, may be localised and/or an alias
319 * @return SpecialPage object or null if the page doesn't exist
321 public static function getPage( $name ) {
322 list( $realName, /*...*/ ) = self::resolveAlias( $name );
323 if ( property_exists( self::getList(), $realName ) ) {
324 $rec = self::getList()->$realName;
325 if ( is_string( $rec ) ) {
326 $className = $rec;
327 return new $className;
328 } elseif ( is_array( $rec ) ) {
329 // @deprecated, officially since 1.18, unofficially since forever
330 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
331 $className = array_shift( $rec );
332 self::getList()->$realName = MWFunction::newObj( $className, $rec );
334 return self::getList()->$realName;
335 } else {
336 return null;
341 * Return categorised listable special pages which are available
342 * for the current user, and everyone.
344 * @param $user User object to check permissions, $wgUser will be used
345 * if not provided
346 * @return Array( String => Specialpage )
348 public static function getUsablePages( User $user = null ) {
349 $pages = array();
350 if ( $user === null ) {
351 global $wgUser;
352 $user = $wgUser;
354 foreach ( self::getList() as $name => $rec ) {
355 $page = self::getPage( $name );
356 if ( $page // not null
357 && $page->isListed()
358 && ( !$page->isRestricted() || $page->userCanExecute( $user ) )
360 $pages[$name] = $page;
363 return $pages;
367 * Return categorised listable special pages for all users
369 * @return Array( String => Specialpage )
371 public static function getRegularPages() {
372 $pages = array();
373 foreach ( self::getList() as $name => $rec ) {
374 $page = self::getPage( $name );
375 if ( $page->isListed() && !$page->isRestricted() ) {
376 $pages[$name] = $page;
379 return $pages;
383 * Return categorised listable special pages which are available
384 * for the current user, but not for everyone
386 * @return Array( String => Specialpage )
388 public static function getRestrictedPages() {
389 global $wgUser;
390 $pages = array();
391 foreach ( self::getList() as $name => $rec ) {
392 $page = self::getPage( $name );
393 if (
394 $page->isListed()
395 && $page->isRestricted()
396 && $page->userCanExecute( $wgUser )
398 $pages[$name] = $page;
401 return $pages;
405 * Execute a special page path.
406 * The path may contain parameters, e.g. Special:Name/Params
407 * Extracts the special page name and call the execute method, passing the parameters
409 * Returns a title object if the page is redirected, false if there was no such special
410 * page, and true if it was successful.
412 * @param $title Title object
413 * @param $context IContextSource
414 * @param $including Bool output is being captured for use in {{special:whatever}}
416 * @return bool
418 public static function executePath( Title &$title, IContextSource &$context, $including = false ) {
419 wfProfileIn( __METHOD__ );
421 // @todo FIXME: Redirects broken due to this call
422 $bits = explode( '/', $title->getDBkey(), 2 );
423 $name = $bits[0];
424 if ( !isset( $bits[1] ) ) { // bug 2087
425 $par = null;
426 } else {
427 $par = $bits[1];
429 $page = self::getPage( $name );
430 // Nonexistent?
431 if ( !$page ) {
432 $context->getOutput()->setArticleRelated( false );
433 $context->getOutput()->setRobotPolicy( 'noindex,nofollow' );
435 global $wgSend404Code;
436 if ( $wgSend404Code ) {
437 $context->getOutput()->setStatusCode( 404 );
440 $context->getOutput()->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
441 wfProfileOut( __METHOD__ );
442 return false;
445 // Page exists, set the context
446 $page->setContext( $context );
448 if ( !$including ) {
449 // Redirect to canonical alias for GET commands
450 // Not for POST, we'd lose the post data, so it's best to just distribute
451 // the request. Such POST requests are possible for old extensions that
452 // generate self-links without being aware that their default name has
453 // changed.
454 if ( $name != $page->getLocalName() && !$context->getRequest()->wasPosted() ) {
455 $query = $context->getRequest()->getQueryValues();
456 unset( $query['title'] );
457 $query = wfArrayToCGI( $query );
458 $title = $page->getTitle( $par );
459 $url = $title->getFullUrl( $query );
460 $context->getOutput()->redirect( $url );
461 wfProfileOut( __METHOD__ );
462 return $title;
463 } else {
464 $context->setTitle( $page->getTitle( $par ) );
467 } elseif ( !$page->isIncludable() ) {
468 wfProfileOut( __METHOD__ );
469 return false;
472 $page->including( $including );
474 // Execute special page
475 $profName = 'Special:' . $page->getName();
476 wfProfileIn( $profName );
477 $page->execute( $par );
478 wfProfileOut( $profName );
479 wfProfileOut( __METHOD__ );
480 return true;
484 * Just like executePath() but will override global variables and execute
485 * the page in "inclusion" mode. Returns true if the execution was
486 * successful or false if there was no such special page, or a title object
487 * if it was a redirect.
489 * Also saves the current $wgTitle, $wgOut, $wgRequest, $wgUser and $wgLang
490 * variables so that the special page will get the context it'd expect on a
491 * normal request, and then restores them to their previous values after.
493 * @param $title Title
494 * @param $context IContextSource
496 * @return String: HTML fragment
498 static function capturePath( Title $title, IContextSource $context ) {
499 global $wgOut, $wgTitle, $wgRequest, $wgUser, $wgLang;
501 // Save current globals
502 $oldTitle = $wgTitle;
503 $oldOut = $wgOut;
504 $oldRequest = $wgRequest;
505 $oldUser = $wgUser;
506 $oldLang = $wgLang;
508 // Set the globals to the current context
509 $wgTitle = $title;
510 $wgOut = $context->getOutput();
511 $wgRequest = $context->getRequest();
512 $wgUser = $context->getUser();
513 $wgLang = $context->getLanguage();
515 // The useful part
516 $ret = self::executePath( $title, $context, true );
518 // And restore the old globals
519 $wgTitle = $oldTitle;
520 $wgOut = $oldOut;
521 $wgRequest = $oldRequest;
522 $wgUser = $oldUser;
523 $wgLang = $oldLang;
525 return $ret;
529 * Get the local name for a specified canonical name
531 * @param $name String
532 * @param $subpage String|Bool
534 * @return String
536 static function getLocalNameFor( $name, $subpage = false ) {
537 global $wgContLang;
538 $aliases = $wgContLang->getSpecialPageAliases();
540 if ( isset( $aliases[$name][0] ) ) {
541 $name = $aliases[$name][0];
542 } else {
543 // Try harder in case someone misspelled the correct casing
544 $found = false;
545 foreach ( $aliases as $n => $values ) {
546 if ( strcasecmp( $name, $n ) === 0 ) {
547 wfWarn( "Found alias defined for $n when searching for " .
548 "special page aliases for $name. Case mismatch?" );
549 $name = $values[0];
550 $found = true;
551 break;
554 if ( !$found ) {
555 wfWarn( "Did not find alias for special page '$name'. " .
556 "Perhaps no aliases are defined for it?" );
559 if ( $subpage !== false && !is_null( $subpage ) ) {
560 $name = "$name/$subpage";
562 return $wgContLang->ucfirst( $name );
566 * Get a title for a given alias
568 * @param $alias String
570 * @return Title or null if there is no such alias
572 static function getTitleForAlias( $alias ) {
573 $name = self::resolveAlias( $alias );
574 if ( $name ) {
575 return SpecialPage::getTitleFor( $name );
576 } else {
577 return null;