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.
21 * @ingroup SpecialPage
22 * @defgroup SpecialPage SpecialPage
26 * Factory for handling the special page list and generating SpecialPage objects
27 * @ingroup SpecialPage
30 class SpecialPageFactory
{
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',
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',
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',
119 'Mostlinkedcategories' => 'MostlinkedCategoriesPage',
120 'Mostimages' => 'MostimagesPage',
121 'Mostlinked' => 'MostlinkedPage',
122 'Mostlinkedtemplates' => 'MostlinkedTemplatesPage',
123 'Mostcategories' => 'MostcategoriesPage',
124 'Mostrevisions' => 'MostrevisionsPage',
127 'ComparePages' => 'SpecialComparePages',
128 'Export' => 'SpecialExport',
129 'Import' => 'SpecialImport',
130 'Undelete' => 'SpecialUndelete',
131 'Whatlinkshere' => 'SpecialWhatlinkshere',
132 'MergeHistory' => 'SpecialMergeHistory',
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
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 );
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__
);
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).
209 static function getAliasList() {
210 if ( !is_object( self
::$mAliases ) ) {
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
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 ) {
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;
251 return array( null, null );
254 if ( !isset( $bits[1] ) ) { // bug 2087
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
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();
293 $group = isset( $wgSpecialPageGroups[$name] )
294 ?
$wgSpecialPageGroups[$name]
297 if ( $group == '-' ) {
300 $specialPageGroupsCache[$name] = $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 ) ) {
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;
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
346 * @return Array( String => Specialpage )
348 public static function getUsablePages( User
$user = null ) {
350 if ( $user === null ) {
354 foreach ( self
::getList() as $name => $rec ) {
355 $page = self
::getPage( $name );
356 if ( $page // not null
358 && ( !$page->isRestricted() ||
$page->userCanExecute( $user ) )
360 $pages[$name] = $page;
367 * Return categorised listable special pages for all users
369 * @return Array( String => Specialpage )
371 public static function getRegularPages() {
373 foreach ( self
::getList() as $name => $rec ) {
374 $page = self
::getPage( $name );
375 if ( $page->isListed() && !$page->isRestricted() ) {
376 $pages[$name] = $page;
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() {
391 foreach ( self
::getList() as $name => $rec ) {
392 $page = self
::getPage( $name );
395 && $page->isRestricted()
396 && $page->userCanExecute( $wgUser )
398 $pages[$name] = $page;
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}}
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 );
424 if ( !isset( $bits[1] ) ) { // bug 2087
429 $page = self
::getPage( $name );
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__
);
445 // Page exists, set the context
446 $page->setContext( $context );
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
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__
);
464 $context->setTitle( $page->getTitle( $par ) );
467 } elseif ( !$page->isIncludable() ) {
468 wfProfileOut( __METHOD__
);
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__
);
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;
504 $oldRequest = $wgRequest;
508 // Set the globals to the current context
510 $wgOut = $context->getOutput();
511 $wgRequest = $context->getRequest();
512 $wgUser = $context->getUser();
513 $wgLang = $context->getLanguage();
516 $ret = self
::executePath( $title, $context, true );
518 // And restore the old globals
519 $wgTitle = $oldTitle;
521 $wgRequest = $oldRequest;
529 * Get the local name for a specified canonical name
531 * @param $name String
532 * @param $subpage String|Bool
536 static function getLocalNameFor( $name, $subpage = false ) {
538 $aliases = $wgContLang->getSpecialPageAliases();
540 if ( isset( $aliases[$name][0] ) ) {
541 $name = $aliases[$name][0];
543 // Try harder in case someone misspelled the correct casing
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?" );
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 );
575 return SpecialPage
::getTitleFor( $name );