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
21 * @ingroup SpecialPage
22 * @defgroup SpecialPage SpecialPage
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
46 class SpecialPageFactory
{
48 * List of special page names to the subclass of SpecialPage which handles them.
50 private static $list = 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',
77 'Allpages' => 'SpecialAllpages',
78 'Prefixindex' => 'SpecialPrefixindex',
79 'Categories' => 'SpecialCategories',
80 'Listredirects' => 'ListredirectsPage',
81 'PagesWithProp' => 'SpecialPagesWithProp',
83 // Login/create account
84 'Userlogin' => 'LoginForm',
85 'CreateAccount' => 'SpecialCreateAccount',
88 'Block' => 'SpecialBlock',
89 'Unblock' => 'SpecialUnblock',
90 'BlockList' => 'SpecialBlockList',
91 'ChangePassword' => 'SpecialChangePassword',
92 'PasswordReset' => 'SpecialPasswordReset',
93 'DeletedContributions' => 'DeletedContributionsPage',
94 'Preferences' => 'SpecialPreferences',
95 'ResetTokens' => 'SpecialResetTokens',
96 'Contributions' => 'SpecialContributions',
97 'Listgrouprights' => 'SpecialListGroupRights',
98 'Listusers' => 'SpecialListUsers',
99 'Listadmins' => 'SpecialListAdmins',
100 'Listbots' => 'SpecialListBots',
101 'Userrights' => 'UserrightsPage',
102 'EditWatchlist' => 'SpecialEditWatchlist',
104 // Recent changes and logs
105 'Newimages' => 'SpecialNewFiles',
106 'Log' => 'SpecialLog',
107 'Watchlist' => 'SpecialWatchlist',
108 'Newpages' => 'SpecialNewpages',
109 'Recentchanges' => 'SpecialRecentChanges',
110 'Recentchangeslinked' => 'SpecialRecentChangesLinked',
111 'Tags' => 'SpecialTags',
113 // Media reports and uploads
114 'Listfiles' => 'SpecialListFiles',
115 'Filepath' => 'SpecialFilepath',
116 'MIMEsearch' => 'MIMEsearchPage',
117 'FileDuplicateSearch' => 'FileDuplicateSearchPage',
118 'Upload' => 'SpecialUpload',
119 'UploadStash' => 'SpecialUploadStash',
122 'Statistics' => 'SpecialStatistics',
123 'Allmessages' => 'SpecialAllmessages',
124 'Version' => 'SpecialVersion',
125 'Lockdb' => 'SpecialLockdb',
126 'Unlockdb' => 'SpecialUnlockdb',
128 // Redirecting special pages
129 'LinkSearch' => 'LinkSearchPage',
130 'Randompage' => 'Randompage',
131 'RandomInCategory' => 'SpecialRandomInCategory',
132 'Randomredirect' => 'SpecialRandomredirect',
135 'Mostlinkedcategories' => 'MostlinkedCategoriesPage',
136 'Mostimages' => 'MostimagesPage',
137 'Mostinterwikis' => 'MostinterwikisPage',
138 'Mostlinked' => 'MostlinkedPage',
139 'Mostlinkedtemplates' => 'MostlinkedTemplatesPage',
140 'Mostcategories' => 'MostcategoriesPage',
141 'Mostrevisions' => 'MostrevisionsPage',
144 'ComparePages' => 'SpecialComparePages',
145 'Export' => 'SpecialExport',
146 'Import' => 'SpecialImport',
147 'Undelete' => 'SpecialUndelete',
148 'Whatlinkshere' => 'SpecialWhatlinkshere',
149 'MergeHistory' => 'SpecialMergeHistory',
150 'ExpandTemplates' => 'SpecialExpandTemplates',
153 'Booksources' => 'SpecialBookSources',
155 // Unlisted / redirects
156 'Blankpage' => 'SpecialBlankpage',
157 'Diff' => 'SpecialDiff',
158 'Emailuser' => 'SpecialEmailUser',
159 'Movepage' => 'MovePageForm',
160 'Mycontributions' => 'SpecialMycontributions',
161 'Mypage' => 'SpecialMypage',
162 'Mytalk' => 'SpecialMytalk',
163 'Myuploads' => 'SpecialMyuploads',
164 'AllMyUploads' => 'SpecialAllMyUploads',
165 'PermanentLink' => 'SpecialPermanentLink',
166 'Redirect' => 'SpecialRedirect',
167 'Revisiondelete' => 'SpecialRevisionDelete',
168 'RunJobs' => 'SpecialRunJobs',
169 'Specialpages' => 'SpecialSpecialpages',
170 'Userlogout' => 'SpecialUserlogout',
173 private static $aliases;
176 * Get the special page list
180 static function getList() {
181 global $wgSpecialPages;
182 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
183 global $wgEnableEmail, $wgEnableJavaScriptTest;
185 if ( !is_object( self
::$list ) ) {
186 wfProfileIn( __METHOD__
);
188 if ( !$wgDisableCounters ) {
189 self
::$list['Popularpages'] = 'PopularpagesPage';
192 if ( !$wgDisableInternalSearch ) {
193 self
::$list['Search'] = 'SpecialSearch';
196 if ( $wgEmailAuthentication ) {
197 self
::$list['Confirmemail'] = 'EmailConfirmation';
198 self
::$list['Invalidateemail'] = 'EmailInvalidation';
201 if ( $wgEnableEmail ) {
202 self
::$list['ChangeEmail'] = 'SpecialChangeEmail';
205 if ( $wgEnableJavaScriptTest ) {
206 self
::$list['JavaScriptTest'] = 'SpecialJavaScriptTest';
209 self
::$list['Activeusers'] = 'SpecialActiveUsers';
211 // Add extension special pages
212 self
::$list = array_merge( self
::$list, $wgSpecialPages );
215 // This hook can be used to remove undesired built-in special pages
216 wfRunHooks( 'SpecialPage_initList', array( &self
::$list ) );
218 // Cast to object: func()[$key] doesn't work, but func()->$key does
219 settype( self
::$list, 'object' );
221 wfProfileOut( __METHOD__
);
227 * Initialise and return the list of special page aliases. Returns an object with
228 * properties which can be accessed $obj->pagename - each property is an array of
229 * aliases; the first in the array is the canonical alias. All registered special
230 * pages are guaranteed to have a property entry, and for that property array to
231 * contain at least one entry (English fallbacks will be added if necessary).
234 static function getAliasList() {
235 if ( !is_object( self
::$aliases ) ) {
237 $aliases = $wgContLang->getSpecialPageAliases();
239 // Objects are passed by reference by default, need to create a copy
240 $missingPages = clone self
::getList();
242 self
::$aliases = array();
243 foreach ( $aliases as $realName => $aliasList ) {
244 foreach ( $aliasList as $alias ) {
245 self
::$aliases[$wgContLang->caseFold( $alias )] = $realName;
247 unset( $missingPages->$realName );
249 foreach ( $missingPages as $name => $stuff ) {
250 self
::$aliases[$wgContLang->caseFold( $name )] = $name;
253 // Cast to object: func()[$key] doesn't work, but func()->$key does
254 self
::$aliases = (object)self
::$aliases;
256 return self
::$aliases;
260 * Given a special page name with a possible subpage, return an array
261 * where the first element is the special page name and the second is the
264 * @param string $alias
265 * @return array Array( String, String|null ), or array( null, null ) if the page is invalid
267 public static function resolveAlias( $alias ) {
269 $bits = explode( '/', $alias, 2 );
271 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
272 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
273 if ( isset( self
::getAliasList()->$caseFoldedAlias ) ) {
274 $name = self
::getAliasList()->$caseFoldedAlias;
276 return array( null, null );
279 if ( !isset( $bits[1] ) ) { // bug 2087
285 return array( $name, $par );
289 * Add a page to a certain display group for Special:SpecialPages
291 * @param SpecialPage|string $page
292 * @param string $group
293 * @deprecated since 1.21 Override SpecialPage::getGroupName
295 public static function setGroup( $page, $group ) {
296 wfDeprecated( __METHOD__
, '1.21' );
298 global $wgSpecialPageGroups;
299 $name = is_object( $page ) ?
$page->getName() : $page;
300 $wgSpecialPageGroups[$name] = $group;
304 * Get the group that the special page belongs in on Special:SpecialPage
306 * @param SpecialPage $page
308 * @deprecated since 1.21 Use SpecialPage::getFinalGroupName
310 public static function getGroup( &$page ) {
311 wfDeprecated( __METHOD__
, '1.21' );
313 return $page->getFinalGroupName();
317 * Check if a given name exist as a special page or as a special page alias
319 * @param string $name Name of a special page
320 * @return bool True if a special page exists with this name
322 public static function exists( $name ) {
323 list( $title, /*...*/ ) = self
::resolveAlias( $name );
324 return property_exists( self
::getList(), $title );
328 * Find the object with a given name and return it (or NULL)
330 * @param string $name Special page name, may be localised and/or an alias
331 * @return SpecialPage|null SpecialPage object or null if the page doesn't exist
333 public static function getPage( $name ) {
334 list( $realName, /*...*/ ) = self
::resolveAlias( $name );
335 if ( property_exists( self
::getList(), $realName ) ) {
336 $rec = self
::getList()->$realName;
337 if ( is_string( $rec ) ) {
339 return new $className;
340 } elseif ( is_array( $rec ) ) {
341 // @deprecated, officially since 1.18, unofficially since forever
342 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
343 $className = array_shift( $rec );
344 self
::getList()->$realName = MWFunction
::newObj( $className, $rec );
346 return self
::getList()->$realName;
353 * Return categorised listable special pages which are available
354 * for the current user, and everyone.
356 * @param $user User object to check permissions, $wgUser will be used if
358 * @return array ( string => Specialpage )
360 public static function getUsablePages( User
$user = null ) {
362 if ( $user === null ) {
366 foreach ( self
::getList() as $name => $rec ) {
367 $page = self
::getPage( $name );
368 if ( $page ) { // not null
369 $page->setContext( RequestContext
::getMain() );
370 if ( $page->isListed()
371 && ( !$page->isRestricted() ||
$page->userCanExecute( $user ) )
373 $pages[$name] = $page;
381 * Return categorised listable special pages for all users
383 * @return array ( string => Specialpage )
385 public static function getRegularPages() {
387 foreach ( self
::getList() as $name => $rec ) {
388 $page = self
::getPage( $name );
389 if ( $page->isListed() && !$page->isRestricted() ) {
390 $pages[$name] = $page;
397 * Return categorised listable special pages which are available
398 * for the current user, but not for everyone
400 * @return array ( string => Specialpage )
402 public static function getRestrictedPages() {
405 foreach ( self
::getList() as $name => $rec ) {
406 $page = self
::getPage( $name );
409 && $page->isRestricted()
410 && $page->userCanExecute( $wgUser )
412 $pages[$name] = $page;
419 * Execute a special page path.
420 * The path may contain parameters, e.g. Special:Name/Params
421 * Extracts the special page name and call the execute method, passing the parameters
423 * Returns a title object if the page is redirected, false if there was no such special
424 * page, and true if it was successful.
426 * @param Title $title
427 * @param IContextSource $context
428 * @param bool $including Bool output is being captured for use in {{special:whatever}}
432 public static function executePath( Title
&$title, IContextSource
&$context, $including = false ) {
433 wfProfileIn( __METHOD__
);
435 // @todo FIXME: Redirects broken due to this call
436 $bits = explode( '/', $title->getDBkey(), 2 );
438 if ( !isset( $bits[1] ) ) { // bug 2087
443 $page = self
::getPage( $name );
446 $context->getOutput()->setArticleRelated( false );
447 $context->getOutput()->setRobotPolicy( 'noindex,nofollow' );
449 global $wgSend404Code;
450 if ( $wgSend404Code ) {
451 $context->getOutput()->setStatusCode( 404 );
454 $context->getOutput()->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
455 wfProfileOut( __METHOD__
);
459 // Page exists, set the context
460 $page->setContext( $context );
463 // Redirect to canonical alias for GET commands
464 // Not for POST, we'd lose the post data, so it's best to just distribute
465 // the request. Such POST requests are possible for old extensions that
466 // generate self-links without being aware that their default name has
468 if ( $name != $page->getLocalName() && !$context->getRequest()->wasPosted() ) {
469 $query = $context->getRequest()->getQueryValues();
470 unset( $query['title'] );
471 $title = $page->getPageTitle( $par );
472 $url = $title->getFullURL( $query );
473 $context->getOutput()->redirect( $url );
474 wfProfileOut( __METHOD__
);
477 $context->setTitle( $page->getPageTitle( $par ) );
480 } elseif ( !$page->isIncludable() ) {
481 wfProfileOut( __METHOD__
);
485 $page->including( $including );
487 // Execute special page
488 $profName = 'Special:' . $page->getName();
489 wfProfileIn( $profName );
491 wfProfileOut( $profName );
492 wfProfileOut( __METHOD__
);
497 * Just like executePath() but will override global variables and execute
498 * the page in "inclusion" mode. Returns true if the execution was
499 * successful or false if there was no such special page, or a title object
500 * if it was a redirect.
502 * Also saves the current $wgTitle, $wgOut, $wgRequest, $wgUser and $wgLang
503 * variables so that the special page will get the context it'd expect on a
504 * normal request, and then restores them to their previous values after.
506 * @param Title $title
507 * @param IContextSource $context
508 * @return string HTML fragment
510 static function capturePath( Title
$title, IContextSource
$context ) {
511 global $wgOut, $wgTitle, $wgRequest, $wgUser, $wgLang;
513 // Save current globals
514 $oldTitle = $wgTitle;
516 $oldRequest = $wgRequest;
520 // Set the globals to the current context
522 $wgOut = $context->getOutput();
523 $wgRequest = $context->getRequest();
524 $wgUser = $context->getUser();
525 $wgLang = $context->getLanguage();
528 $ret = self
::executePath( $title, $context, true );
530 // And restore the old globals
531 $wgTitle = $oldTitle;
533 $wgRequest = $oldRequest;
541 * Get the local name for a specified canonical name
543 * @param string $name
544 * @param string|bool $subpage
547 static function getLocalNameFor( $name, $subpage = false ) {
549 $aliases = $wgContLang->getSpecialPageAliases();
551 if ( isset( $aliases[$name][0] ) ) {
552 $name = $aliases[$name][0];
554 // Try harder in case someone misspelled the correct casing
556 foreach ( $aliases as $n => $values ) {
557 if ( strcasecmp( $name, $n ) === 0 ) {
558 wfWarn( "Found alias defined for $n when searching for " .
559 "special page aliases for $name. Case mismatch?" );
566 wfWarn( "Did not find alias for special page '$name'. " .
567 "Perhaps no aliases are defined for it?" );
570 if ( $subpage !== false && !is_null( $subpage ) ) {
571 $name = "$name/$subpage";
573 return $wgContLang->ucfirst( $name );
577 * Get a title for a given alias
579 * @param string $alias
580 * @return Title|null Title or null if there is no such alias
582 static function getTitleForAlias( $alias ) {
583 $name = self
::resolveAlias( $alias );
585 return SpecialPage
::getTitleFor( $name );