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
29 class SpecialPageFactory
{
32 * List of special page names to the subclass of SpecialPage which handles them.
34 private static $mList = array(
35 // Maintenance Reports
36 'BrokenRedirects' => 'BrokenRedirectsPage',
37 'Deadendpages' => 'DeadendpagesPage',
38 'DoubleRedirects' => 'DoubleRedirectsPage',
39 'Longpages' => 'LongpagesPage',
40 'Ancientpages' => 'AncientpagesPage',
41 'Lonelypages' => 'LonelypagesPage',
42 'Fewestrevisions' => 'FewestrevisionsPage',
43 'Withoutinterwiki' => 'WithoutinterwikiPage',
44 'Protectedpages' => 'SpecialProtectedpages',
45 'Protectedtitles' => 'SpecialProtectedtitles',
46 'Shortpages' => 'ShortpagesPage',
47 'Uncategorizedcategories' => 'UncategorizedcategoriesPage',
48 'Uncategorizedimages' => 'UncategorizedimagesPage',
49 'Uncategorizedpages' => 'UncategorizedpagesPage',
50 'Uncategorizedtemplates' => 'UncategorizedtemplatesPage',
51 'Unusedcategories' => 'UnusedcategoriesPage',
52 'Unusedimages' => 'UnusedimagesPage',
53 'Unusedtemplates' => 'UnusedtemplatesPage',
54 'Unwatchedpages' => 'UnwatchedpagesPage',
55 'Wantedcategories' => 'WantedcategoriesPage',
56 'Wantedfiles' => 'WantedfilesPage',
57 'Wantedpages' => 'WantedpagesPage',
58 'Wantedtemplates' => 'WantedtemplatesPage',
61 'Allpages' => 'SpecialAllpages',
62 'Prefixindex' => 'SpecialPrefixindex',
63 'Categories' => 'SpecialCategories',
64 'Disambiguations' => 'DisambiguationsPage',
65 'Listredirects' => 'ListredirectsPage',
67 // Login/create account
68 'Userlogin' => 'LoginForm',
69 'CreateAccount' => 'SpecialCreateAccount',
72 'Block' => 'SpecialBlock',
73 'Unblock' => 'SpecialUnblock',
74 'BlockList' => 'SpecialBlockList',
75 'Resetpass' => 'SpecialResetpass',
76 'DeletedContributions' => 'DeletedContributionsPage',
77 'Preferences' => 'SpecialPreferences',
78 'Contributions' => 'SpecialContributions',
79 'Listgrouprights' => 'SpecialListGroupRights',
80 'Listusers' => 'SpecialListUsers' ,
81 'Listadmins' => 'SpecialListAdmins',
82 'Listbots' => 'SpecialListBots',
83 'Activeusers' => 'SpecialActiveUsers',
84 'Userrights' => 'UserrightsPage',
85 'EditWatchlist' => 'SpecialEditWatchlist',
87 // Recent changes and logs
88 'Newimages' => 'SpecialNewFiles',
89 'Log' => 'SpecialLog',
90 'Watchlist' => 'SpecialWatchlist',
91 'Newpages' => 'SpecialNewpages',
92 'Recentchanges' => 'SpecialRecentchanges',
93 'Recentchangeslinked' => 'SpecialRecentchangeslinked',
94 'Tags' => 'SpecialTags',
96 // Media reports and uploads
97 'Listfiles' => 'SpecialListFiles',
98 'Filepath' => 'SpecialFilepath',
99 'MIMEsearch' => 'MIMEsearchPage',
100 'FileDuplicateSearch' => 'FileDuplicateSearchPage',
101 'Upload' => 'SpecialUpload',
102 'UploadStash' => 'SpecialUploadStash',
104 // Wiki data and tools
105 'Statistics' => 'SpecialStatistics',
106 'Allmessages' => 'SpecialAllmessages',
107 'Version' => 'SpecialVersion',
108 'Lockdb' => 'SpecialLockdb',
109 'Unlockdb' => 'SpecialUnlockdb',
111 // Redirecting special pages
112 'LinkSearch' => 'LinkSearchPage',
113 'Randompage' => 'Randompage',
114 'Randomredirect' => 'SpecialRandomredirect',
117 'Mostlinkedcategories' => 'MostlinkedCategoriesPage',
118 'Mostimages' => 'MostimagesPage',
119 'Mostlinked' => 'MostlinkedPage',
120 'Mostlinkedtemplates' => 'MostlinkedTemplatesPage',
121 'Mostcategories' => 'MostcategoriesPage',
122 'Mostrevisions' => 'MostrevisionsPage',
125 'ComparePages' => 'SpecialComparePages',
126 'Export' => 'SpecialExport',
127 'Import' => 'SpecialImport',
128 'Undelete' => 'SpecialUndelete',
129 'Whatlinkshere' => 'SpecialWhatlinkshere',
130 'MergeHistory' => 'SpecialMergeHistory',
133 'Booksources' => 'SpecialBookSources',
135 // Unlisted / redirects
136 'Blankpage' => 'SpecialBlankpage',
137 'Blockme' => 'SpecialBlockme',
138 'Emailuser' => 'SpecialEmailUser',
139 'Movepage' => 'MovePageForm',
140 'Mycontributions' => 'SpecialMycontributions',
141 'Mypage' => 'SpecialMypage',
142 'Mytalk' => 'SpecialMytalk',
143 'Myuploads' => 'SpecialMyuploads',
144 'PermanentLink' => 'SpecialPermanentLink',
145 'Revisiondelete' => 'SpecialRevisionDelete',
146 'Specialpages' => 'SpecialSpecialpages',
147 'Userlogout' => 'SpecialUserlogout',
150 private static $mAliases;
153 * Initialise the special page list
154 * This must be called before accessing SpecialPage::$mList
156 static function getList() {
157 global $wgSpecialPages;
158 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
160 if ( !is_object( self
::$mList ) ) {
161 wfProfileIn( __METHOD__
);
163 if ( !$wgDisableCounters ) {
164 self
::$mList['Popularpages'] = 'PopularpagesPage';
167 if ( !$wgDisableInternalSearch ) {
168 self
::$mList['Search'] = 'SpecialSearch';
171 if ( $wgEmailAuthentication ) {
172 self
::$mList['Confirmemail'] = 'EmailConfirmation';
173 self
::$mList['Invalidateemail'] = 'EmailInvalidation';
176 // Add extension special pages
177 self
::$mList = array_merge( self
::$mList, $wgSpecialPages );
180 // This hook can be used to remove undesired built-in special pages
181 wfRunHooks( 'SpecialPage_initList', array( &self
::$mList ) );
183 // Cast to object: func()[$key] doesn't work, but func()->$key does
184 settype( self
::$mList, 'object' );
186 wfProfileOut( __METHOD__
);
191 static function getAliasList() {
192 if ( !is_object( self
::$mAliases ) ) {
194 $aliases = $wgContLang->getSpecialPageAliases();
196 // Objects are passed by reference by default, need to create a copy
197 $missingPages = clone self
::getList();
199 self
::$mAliases = array();
200 foreach ( $aliases as $realName => $aliasList ) {
201 foreach ( $aliasList as $alias ) {
202 self
::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
204 unset( $missingPages->$realName );
206 foreach ( $missingPages as $name => $stuff ) {
207 self
::$mAliases[$wgContLang->caseFold( $name )] = $name;
210 // Cast to object: func()[$key] doesn't work, but func()->$key does
211 self
::$mAliases = (object)self
::$mAliases;
213 return self
::$mAliases;
217 * Given a special page name with a possible subpage, return an array
218 * where the first element is the special page name and the second is the
221 * @param $alias String
222 * @return Array( String, String|null ), or array( null, null ) if the page is invalid
224 public static function resolveAlias( $alias ) {
226 $bits = explode( '/', $alias, 2 );
228 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
229 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
230 if ( isset( self
::getAliasList()->$caseFoldedAlias ) ) {
231 $name = self
::getAliasList()->$caseFoldedAlias;
233 return array( null, null );
236 if ( !isset( $bits[1] ) ) { // bug 2087
242 return array( $name, $par );
246 * Add a page to a certain display group for Special:SpecialPages
248 * @param $page Mixed: SpecialPage or string
249 * @param $group String
251 public static function setGroup( $page, $group ) {
252 global $wgSpecialPageGroups;
253 $name = is_object( $page ) ?
$page->mName
: $page;
254 $wgSpecialPageGroups[$name] = $group;
258 * Add a page to a certain display group for Special:SpecialPages
260 * @param $page SpecialPage
262 public static function getGroup( &$page ) {
263 global $wgSpecialPageGroups;
264 static $specialPageGroupsCache = array();
265 if ( isset( $specialPageGroupsCache[$page->mName
] ) ) {
266 return $specialPageGroupsCache[$page->mName
];
268 $msg = wfMessage( 'specialpages-specialpagegroup-' . strtolower( $page->mName
) );
269 if ( !$msg->isBlank() ) {
270 $group = $msg->text();
272 $group = isset( $wgSpecialPageGroups[$page->mName
] )
273 ?
$wgSpecialPageGroups[$page->mName
]
276 if ( $group == '-' ) {
279 $specialPageGroupsCache[$page->mName
] = $group;
284 * Check if a given name exist as a special page or as a special page alias
286 * @param $name String: name of a special page
287 * @return Boolean: true if a special page exists with this name
289 public static function exists( $name ) {
290 list( $title, /*...*/ ) = self
::resolveAlias( $name );
291 return property_exists( self
::getList(), $title );
295 * Find the object with a given name and return it (or NULL)
297 * @param $name String Special page name, may be localised and/or an alias
298 * @return SpecialPage object or null if the page doesn't exist
300 public static function getPage( $name ) {
301 list( $realName, /*...*/ ) = self
::resolveAlias( $name );
302 if ( property_exists( self
::getList(), $realName ) ) {
303 $rec = self
::getList()->$realName;
304 if ( is_string( $rec ) ) {
306 self
::getList()->$realName = new $className;
307 } elseif ( is_array( $rec ) ) {
308 // @deprecated, officially since 1.18, unofficially since forever
309 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
310 $className = array_shift( $rec );
311 self
::getList()->$realName = MWFunction
::newObj( $className, $rec );
313 return self
::getList()->$realName;
320 * Return categorised listable special pages which are available
321 * for the current user, and everyone.
323 * @return Array( String => Specialpage )
325 public static function getUsablePages() {
328 foreach ( self
::getList() as $name => $rec ) {
329 $page = self
::getPage( $name );
330 if ( $page->isListed()
332 !$page->isRestricted()
333 ||
$page->userCanExecute( $wgUser )
336 $pages[$name] = $page;
343 * Return categorised listable special pages for all users
345 * @return Array( String => Specialpage )
347 public static function getRegularPages() {
349 foreach ( self
::getList() as $name => $rec ) {
350 $page = self
::getPage( $name );
351 if ( $page->isListed() && !$page->isRestricted() ) {
352 $pages[$name] = $page;
359 * Return categorised listable special pages which are available
360 * for the current user, but not for everyone
362 * @return Array( String => Specialpage )
364 public static function getRestrictedPages() {
367 foreach ( self
::getList() as $name => $rec ) {
368 $page = self
::getPage( $name );
371 && $page->isRestricted()
372 && $page->userCanExecute( $wgUser )
374 $pages[$name] = $page;
381 * Execute a special page path.
382 * The path may contain parameters, e.g. Special:Name/Params
383 * Extracts the special page name and call the execute method, passing the parameters
385 * Returns a title object if the page is redirected, false if there was no such special
386 * page, and true if it was successful.
388 * @param $title Title object
389 * @param $context RequestContext
390 * @param $including Bool output is being captured for use in {{special:whatever}}
392 public static function executePath( Title
&$title, RequestContext
&$context, $including = false ) {
393 wfProfileIn( __METHOD__
);
395 // FIXME: redirects broken due to this call
396 $bits = explode( '/', $title->getDBkey(), 2 );
398 if ( !isset( $bits[1] ) ) { // bug 2087
403 $page = self
::getPage( $name );
406 $context->output
->setArticleRelated( false );
407 $context->output
->setRobotPolicy( 'noindex,nofollow' );
408 $context->output
->setStatusCode( 404 );
409 $context->output
->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
410 wfProfileOut( __METHOD__
);
414 // Page exists, set the context
415 $page->setContext( $context );
417 // Check for redirect
419 $redirect = $page->getRedirect( $par );
420 $query = $page->getRedirectQuery();
421 if ( $redirect instanceof Title
) {
422 $url = $redirect->getFullUrl( $query );
423 $context->output
->redirect( $url );
424 wfProfileOut( __METHOD__
);
426 } elseif ( $redirect === true ) {
428 $url = $wgScript . '?' . wfArrayToCGI( $query );
429 $context->output
->redirect( $url );
430 wfProfileOut( __METHOD__
);
434 // Redirect to canonical alias for GET commands
435 // Not for POST, we'd lose the post data, so it's best to just distribute
436 // the request. Such POST requests are possible for old extensions that
437 // generate self-links without being aware that their default name has
439 if ( $name != $page->getLocalName() && !$context->request
->wasPosted() ) {
441 unset( $query['title'] );
442 $query = wfArrayToCGI( $query );
443 $title = $page->getTitle( $par );
444 $url = $title->getFullUrl( $query );
445 $context->output
->redirect( $url );
446 wfProfileOut( __METHOD__
);
449 $context->title
= $page->getTitle();
452 } elseif ( !$page->includable() ) {
453 wfProfileOut( __METHOD__
);
457 $page->including( $including );
459 // Execute special page
460 $profName = 'Special:' . $page->name();
461 wfProfileIn( $profName );
462 $page->execute( $par );
463 wfProfileOut( $profName );
464 wfProfileOut( __METHOD__
);
469 * Just like executePath() except it returns the HTML instead of outputting it
470 * Returns false if there was no such special page, or a title object if it was
473 * @return String: HTML fragment
475 static function capturePath( &$title ) {
476 global $wgOut, $wgTitle;
478 $oldTitle = $wgTitle;
481 $context = new RequestContext
;
482 $context->setTitle( $title );
483 $wgOut = $context->getOutput();
485 $ret = self
::executePath( $title, $context, true );
486 if ( $ret === true ) {
487 $ret = $wgOut->getHTML();
489 $wgTitle = $oldTitle;
495 * Get the local name for a specified canonical name
497 * @param $name String
498 * @param $subpage String|Bool
502 static function getLocalNameFor( $name, $subpage = false ) {
504 $aliases = $wgContLang->getSpecialPageAliases();
505 if ( isset( $aliases[$name][0] ) ) {
506 $name = $aliases[$name][0];
508 // Try harder in case someone misspelled the correct casing
510 foreach ( $aliases as $n => $values ) {
511 if ( strcasecmp( $name, $n ) === 0 ) {
512 wfWarn( "Found alias defined for $n when searching for " .
513 "special page aliases for $name. Case mismatch?" );
520 wfWarn( "Did not find alias for special page '$name'. " .
521 "Perhaps no aliases are defined for it?" );
524 if ( $subpage !== false && !is_null( $subpage ) ) {
525 $name = "$name/$subpage";
527 return $wgContLang->ucfirst( $name );
531 * Get a title for a given alias
533 * @return Title or null if there is no such alias
535 static function getTitleForAlias( $alias ) {
536 $name = self
::resolveAlias( $alias );
538 return self
::getTitleFor( $name );