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 * SpecialPage::$mList. To remove a core static special page at runtime, use
41 * a SpecialPage_initList hook.
43 * @ingroup SpecialPage
46 class SpecialPageFactory
{
49 * List of special page names to the subclass of SpecialPage which handles them.
51 private static $mList = array(
52 // Maintenance Reports
53 'BrokenRedirects' => 'BrokenRedirectsPage',
54 'Deadendpages' => 'DeadendpagesPage',
55 'DoubleRedirects' => 'DoubleRedirectsPage',
56 'Longpages' => 'LongpagesPage',
57 'Ancientpages' => 'AncientpagesPage',
58 'Lonelypages' => 'LonelypagesPage',
59 'Fewestrevisions' => 'FewestrevisionsPage',
60 'Withoutinterwiki' => 'WithoutinterwikiPage',
61 'Protectedpages' => 'SpecialProtectedpages',
62 'Protectedtitles' => 'SpecialProtectedtitles',
63 'Shortpages' => 'ShortpagesPage',
64 'Uncategorizedcategories' => 'UncategorizedcategoriesPage',
65 'Uncategorizedimages' => 'UncategorizedimagesPage',
66 'Uncategorizedpages' => 'UncategorizedpagesPage',
67 'Uncategorizedtemplates' => 'UncategorizedtemplatesPage',
68 'Unusedcategories' => 'UnusedcategoriesPage',
69 'Unusedimages' => 'UnusedimagesPage',
70 'Unusedtemplates' => 'UnusedtemplatesPage',
71 'Unwatchedpages' => 'UnwatchedpagesPage',
72 'Wantedcategories' => 'WantedcategoriesPage',
73 'Wantedfiles' => 'WantedfilesPage',
74 'Wantedpages' => 'WantedpagesPage',
75 'Wantedtemplates' => 'WantedtemplatesPage',
78 'Allpages' => 'SpecialAllpages',
79 'Prefixindex' => 'SpecialPrefixindex',
80 'Categories' => 'SpecialCategories',
81 'Disambiguations' => 'DisambiguationsPage',
82 'Listredirects' => 'ListredirectsPage',
83 'PagesWithProp' => 'SpecialPagesWithProp',
85 // Login/create account
86 'Userlogin' => 'LoginForm',
87 'CreateAccount' => 'SpecialCreateAccount',
90 'Block' => 'SpecialBlock',
91 'Unblock' => 'SpecialUnblock',
92 'BlockList' => 'SpecialBlockList',
93 'ChangePassword' => 'SpecialChangePassword',
94 'PasswordReset' => 'SpecialPasswordReset',
95 'DeletedContributions' => 'DeletedContributionsPage',
96 'Preferences' => 'SpecialPreferences',
97 'Contributions' => 'SpecialContributions',
98 'Listgrouprights' => 'SpecialListGroupRights',
99 'Listusers' => 'SpecialListUsers',
100 'Listadmins' => 'SpecialListAdmins',
101 'Listbots' => 'SpecialListBots',
102 'Userrights' => 'UserrightsPage',
103 'EditWatchlist' => 'SpecialEditWatchlist',
105 // Recent changes and logs
106 'Newimages' => 'SpecialNewFiles',
107 'Log' => 'SpecialLog',
108 'Watchlist' => 'SpecialWatchlist',
109 'Newpages' => 'SpecialNewpages',
110 'Recentchanges' => 'SpecialRecentchanges',
111 'Recentchangeslinked' => 'SpecialRecentchangeslinked',
112 'Tags' => 'SpecialTags',
114 // Media reports and uploads
115 'Listfiles' => 'SpecialListFiles',
116 'Filepath' => 'SpecialFilepath',
117 'MIMEsearch' => 'MIMEsearchPage',
118 'FileDuplicateSearch' => 'FileDuplicateSearchPage',
119 'Upload' => 'SpecialUpload',
120 'UploadStash' => 'SpecialUploadStash',
123 'Statistics' => 'SpecialStatistics',
124 'Allmessages' => 'SpecialAllmessages',
125 'Version' => 'SpecialVersion',
126 'Lockdb' => 'SpecialLockdb',
127 'Unlockdb' => 'SpecialUnlockdb',
129 // Redirecting special pages
130 'LinkSearch' => 'LinkSearchPage',
131 'Randompage' => 'Randompage',
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',
152 'Booksources' => 'SpecialBookSources',
154 // Unlisted / redirects
155 'Blankpage' => 'SpecialBlankpage',
156 'Blockme' => 'SpecialBlockme',
157 'Emailuser' => 'SpecialEmailUser',
158 'Movepage' => 'MovePageForm',
159 'Mycontributions' => 'SpecialMycontributions',
160 'Mypage' => 'SpecialMypage',
161 'Mytalk' => 'SpecialMytalk',
162 'Myuploads' => 'SpecialMyuploads',
163 'PermanentLink' => 'SpecialPermanentLink',
164 'Redirect' => 'SpecialRedirect',
165 'Revisiondelete' => 'SpecialRevisionDelete',
166 'Specialpages' => 'SpecialSpecialpages',
167 'Userlogout' => 'SpecialUserlogout',
170 private static $mAliases;
173 * Initialise the special page list
174 * This must be called before accessing SpecialPage::$mList
178 static function getList() {
179 global $wgSpecialPages;
180 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
181 global $wgEnableEmail, $wgEnableJavaScriptTest;
183 if ( !is_object( self
::$mList ) ) {
184 wfProfileIn( __METHOD__
);
186 if ( !$wgDisableCounters ) {
187 self
::$mList['Popularpages'] = 'PopularpagesPage';
190 if ( !$wgDisableInternalSearch ) {
191 self
::$mList['Search'] = 'SpecialSearch';
194 if ( $wgEmailAuthentication ) {
195 self
::$mList['Confirmemail'] = 'EmailConfirmation';
196 self
::$mList['Invalidateemail'] = 'EmailInvalidation';
199 if ( $wgEnableEmail ) {
200 self
::$mList['ChangeEmail'] = 'SpecialChangeEmail';
203 if ( $wgEnableJavaScriptTest ) {
204 self
::$mList['JavaScriptTest'] = 'SpecialJavaScriptTest';
207 // Add extension special pages
208 self
::$mList = array_merge( self
::$mList, $wgSpecialPages );
211 // This hook can be used to remove undesired built-in special pages
212 wfRunHooks( 'SpecialPage_initList', array( &self
::$mList ) );
214 // Cast to object: func()[$key] doesn't work, but func()->$key does
215 settype( self
::$mList, 'object' );
217 wfProfileOut( __METHOD__
);
223 * Initialise and return the list of special page aliases. Returns an object with
224 * properties which can be accessed $obj->pagename - each property is an array of
225 * aliases; the first in the array is the canonical alias. All registered special
226 * pages are guaranteed to have a property entry, and for that property array to
227 * contain at least one entry (English fallbacks will be added if necessary).
230 static function getAliasList() {
231 if ( !is_object( self
::$mAliases ) ) {
233 $aliases = $wgContLang->getSpecialPageAliases();
235 // Objects are passed by reference by default, need to create a copy
236 $missingPages = clone self
::getList();
238 self
::$mAliases = array();
239 foreach ( $aliases as $realName => $aliasList ) {
240 foreach ( $aliasList as $alias ) {
241 self
::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
243 unset( $missingPages->$realName );
245 foreach ( $missingPages as $name => $stuff ) {
246 self
::$mAliases[$wgContLang->caseFold( $name )] = $name;
249 // Cast to object: func()[$key] doesn't work, but func()->$key does
250 self
::$mAliases = (object)self
::$mAliases;
252 return self
::$mAliases;
256 * Given a special page name with a possible subpage, return an array
257 * where the first element is the special page name and the second is the
260 * @param $alias String
261 * @return Array( String, String|null ), or array( null, null ) if the page is invalid
263 public static function resolveAlias( $alias ) {
265 $bits = explode( '/', $alias, 2 );
267 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
268 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
269 if ( isset( self
::getAliasList()->$caseFoldedAlias ) ) {
270 $name = self
::getAliasList()->$caseFoldedAlias;
272 return array( null, null );
275 if ( !isset( $bits[1] ) ) { // bug 2087
281 return array( $name, $par );
285 * Add a page to a certain display group for Special:SpecialPages
287 * @param $page Mixed: SpecialPage or string
288 * @param $group String
289 * @deprecated 1.21 Override SpecialPage::getGroupName
291 public static function setGroup( $page, $group ) {
292 wfDeprecated( __METHOD__
, '1.21' );
294 global $wgSpecialPageGroups;
295 $name = is_object( $page ) ?
$page->getName() : $page;
296 $wgSpecialPageGroups[$name] = $group;
300 * Get the group that the special page belongs in on Special:SpecialPage
302 * @param $page SpecialPage
304 * @deprecated 1.21 Use SpecialPage::getFinalGroupName
306 public static function getGroup( &$page ) {
307 wfDeprecated( __METHOD__
, '1.21' );
309 return $page->getFinalGroupName();
313 * Check if a given name exist as a special page or as a special page alias
315 * @param string $name name of a special page
316 * @return Boolean: true if a special page exists with this name
318 public static function exists( $name ) {
319 list( $title, /*...*/ ) = self
::resolveAlias( $name );
320 return property_exists( self
::getList(), $title );
324 * Find the object with a given name and return it (or NULL)
326 * @param string $name Special page name, may be localised and/or an alias
327 * @return SpecialPage|null SpecialPage object or null if the page doesn't exist
329 public static function getPage( $name ) {
330 list( $realName, /*...*/ ) = self
::resolveAlias( $name );
331 if ( property_exists( self
::getList(), $realName ) ) {
332 $rec = self
::getList()->$realName;
333 if ( is_string( $rec ) ) {
335 return new $className;
336 } elseif ( is_array( $rec ) ) {
337 // @deprecated, officially since 1.18, unofficially since forever
338 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
339 $className = array_shift( $rec );
340 self
::getList()->$realName = MWFunction
::newObj( $className, $rec );
342 return self
::getList()->$realName;
349 * Return categorised listable special pages which are available
350 * for the current user, and everyone.
352 * @param $user User object to check permissions, $wgUser will be used
354 * @return Array( String => Specialpage )
356 public static function getUsablePages( User
$user = null ) {
358 if ( $user === null ) {
362 foreach ( self
::getList() as $name => $rec ) {
363 $page = self
::getPage( $name );
364 if ( $page ) { // not null
365 $page->setContext( RequestContext
::getMain() );
366 if ( $page->isListed()
367 && ( !$page->isRestricted() ||
$page->userCanExecute( $user ) )
369 $pages[$name] = $page;
377 * Return categorised listable special pages for all users
379 * @return Array( String => Specialpage )
381 public static function getRegularPages() {
383 foreach ( self
::getList() as $name => $rec ) {
384 $page = self
::getPage( $name );
385 if ( $page->isListed() && !$page->isRestricted() ) {
386 $pages[$name] = $page;
393 * Return categorised listable special pages which are available
394 * for the current user, but not for everyone
396 * @return Array( String => Specialpage )
398 public static function getRestrictedPages() {
401 foreach ( self
::getList() as $name => $rec ) {
402 $page = self
::getPage( $name );
405 && $page->isRestricted()
406 && $page->userCanExecute( $wgUser )
408 $pages[$name] = $page;
415 * Execute a special page path.
416 * The path may contain parameters, e.g. Special:Name/Params
417 * Extracts the special page name and call the execute method, passing the parameters
419 * Returns a title object if the page is redirected, false if there was no such special
420 * page, and true if it was successful.
422 * @param $title Title object
423 * @param $context IContextSource
424 * @param $including Bool output is being captured for use in {{special:whatever}}
428 public static function executePath( Title
&$title, IContextSource
&$context, $including = false ) {
429 wfProfileIn( __METHOD__
);
431 // @todo FIXME: Redirects broken due to this call
432 $bits = explode( '/', $title->getDBkey(), 2 );
434 if ( !isset( $bits[1] ) ) { // bug 2087
439 $page = self
::getPage( $name );
442 $context->getOutput()->setArticleRelated( false );
443 $context->getOutput()->setRobotPolicy( 'noindex,nofollow' );
445 global $wgSend404Code;
446 if ( $wgSend404Code ) {
447 $context->getOutput()->setStatusCode( 404 );
450 $context->getOutput()->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
451 wfProfileOut( __METHOD__
);
455 // Page exists, set the context
456 $page->setContext( $context );
459 // Redirect to canonical alias for GET commands
460 // Not for POST, we'd lose the post data, so it's best to just distribute
461 // the request. Such POST requests are possible for old extensions that
462 // generate self-links without being aware that their default name has
464 if ( $name != $page->getLocalName() && !$context->getRequest()->wasPosted() ) {
465 $query = $context->getRequest()->getQueryValues();
466 unset( $query['title'] );
467 $title = $page->getTitle( $par );
468 $url = $title->getFullURL( $query );
469 $context->getOutput()->redirect( $url );
470 wfProfileOut( __METHOD__
);
473 $context->setTitle( $page->getTitle( $par ) );
476 } elseif ( !$page->isIncludable() ) {
477 wfProfileOut( __METHOD__
);
481 $page->including( $including );
483 // Execute special page
484 $profName = 'Special:' . $page->getName();
485 wfProfileIn( $profName );
487 wfProfileOut( $profName );
488 wfProfileOut( __METHOD__
);
493 * Just like executePath() but will override global variables and execute
494 * the page in "inclusion" mode. Returns true if the execution was
495 * successful or false if there was no such special page, or a title object
496 * if it was a redirect.
498 * Also saves the current $wgTitle, $wgOut, $wgRequest, $wgUser and $wgLang
499 * variables so that the special page will get the context it'd expect on a
500 * normal request, and then restores them to their previous values after.
502 * @param $title Title
503 * @param $context IContextSource
505 * @return String: HTML fragment
507 static function capturePath( Title
$title, IContextSource
$context ) {
508 global $wgOut, $wgTitle, $wgRequest, $wgUser, $wgLang;
510 // Save current globals
511 $oldTitle = $wgTitle;
513 $oldRequest = $wgRequest;
517 // Set the globals to the current context
519 $wgOut = $context->getOutput();
520 $wgRequest = $context->getRequest();
521 $wgUser = $context->getUser();
522 $wgLang = $context->getLanguage();
525 $ret = self
::executePath( $title, $context, true );
527 // And restore the old globals
528 $wgTitle = $oldTitle;
530 $wgRequest = $oldRequest;
538 * Get the local name for a specified canonical name
540 * @param $name String
541 * @param $subpage String|Bool
545 static function getLocalNameFor( $name, $subpage = false ) {
547 $aliases = $wgContLang->getSpecialPageAliases();
549 if ( isset( $aliases[$name][0] ) ) {
550 $name = $aliases[$name][0];
552 // Try harder in case someone misspelled the correct casing
554 foreach ( $aliases as $n => $values ) {
555 if ( strcasecmp( $name, $n ) === 0 ) {
556 wfWarn( "Found alias defined for $n when searching for " .
557 "special page aliases for $name. Case mismatch?" );
564 wfWarn( "Did not find alias for special page '$name'. " .
565 "Perhaps no aliases are defined for it?" );
568 if ( $subpage !== false && !is_null( $subpage ) ) {
569 $name = "$name/$subpage";
571 return $wgContLang->ucfirst( $name );
575 * Get a title for a given alias
577 * @param $alias String
579 * @return Title or null if there is no such alias
581 static function getTitleForAlias( $alias ) {
582 $name = self
::resolveAlias( $alias );
584 return SpecialPage
::getTitleFor( $name );