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 'Movepage' => 'MovePageForm',
142 'Mycontributions' => 'SpecialMycontributions',
143 'Mypage' => 'SpecialMypage',
144 'Mytalk' => 'SpecialMytalk',
145 'Myuploads' => 'SpecialMyuploads',
146 'PermanentLink' => 'SpecialPermanentLink',
147 'Revisiondelete' => 'SpecialRevisionDelete',
148 'Specialpages' => 'SpecialSpecialpages',
149 'Userlogout' => 'SpecialUserlogout',
152 private static $mAliases;
155 * Initialise the special page list
156 * This must be called before accessing SpecialPage::$mList
160 static function getList() {
161 global $wgSpecialPages;
162 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
163 global $wgEnableEmail;
165 if ( !is_object( self
::$mList ) ) {
166 wfProfileIn( __METHOD__
);
168 if ( !$wgDisableCounters ) {
169 self
::$mList['Popularpages'] = 'PopularpagesPage';
172 if ( !$wgDisableInternalSearch ) {
173 self
::$mList['Search'] = 'SpecialSearch';
176 if ( $wgEmailAuthentication ) {
177 self
::$mList['Confirmemail'] = 'EmailConfirmation';
178 self
::$mList['Invalidateemail'] = 'EmailInvalidation';
181 if ( $wgEnableEmail ) {
182 self
::$mList['ChangeEmail'] = 'SpecialChangeEmail';
185 // Add extension special pages
186 self
::$mList = array_merge( self
::$mList, $wgSpecialPages );
189 // This hook can be used to remove undesired built-in special pages
190 wfRunHooks( 'SpecialPage_initList', array( &self
::$mList ) );
192 // Cast to object: func()[$key] doesn't work, but func()->$key does
193 settype( self
::$mList, 'object' );
195 wfProfileOut( __METHOD__
);
201 * Initialise and return the list of special page aliases. Returns an object with
202 * properties which can be accessed $obj->pagename - each property is an array of
203 * aliases; the first in the array is the cannonical alias. All registered special
204 * pages are guaranteed to have a property entry, and for that property array to
205 * contain at least one entry (English fallbacks will be added if necessary).
208 static function getAliasList() {
209 if ( !is_object( self
::$mAliases ) ) {
211 $aliases = $wgContLang->getSpecialPageAliases();
213 // Objects are passed by reference by default, need to create a copy
214 $missingPages = clone self
::getList();
216 self
::$mAliases = array();
217 foreach ( $aliases as $realName => $aliasList ) {
218 foreach ( $aliasList as $alias ) {
219 self
::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
221 unset( $missingPages->$realName );
223 foreach ( $missingPages as $name => $stuff ) {
224 self
::$mAliases[$wgContLang->caseFold( $name )] = $name;
227 // Cast to object: func()[$key] doesn't work, but func()->$key does
228 self
::$mAliases = (object)self
::$mAliases;
230 return self
::$mAliases;
234 * Given a special page name with a possible subpage, return an array
235 * where the first element is the special page name and the second is the
238 * @param $alias String
239 * @return Array( String, String|null ), or array( null, null ) if the page is invalid
241 public static function resolveAlias( $alias ) {
243 $bits = explode( '/', $alias, 2 );
245 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
246 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
247 if ( isset( self
::getAliasList()->$caseFoldedAlias ) ) {
248 $name = self
::getAliasList()->$caseFoldedAlias;
250 return array( null, null );
253 if ( !isset( $bits[1] ) ) { // bug 2087
259 return array( $name, $par );
263 * Add a page to a certain display group for Special:SpecialPages
265 * @param $page Mixed: SpecialPage or string
266 * @param $group String
268 public static function setGroup( $page, $group ) {
269 global $wgSpecialPageGroups;
270 $name = is_object( $page ) ?
$page->mName
: $page;
271 $wgSpecialPageGroups[$name] = $group;
275 * Get the group that the special page belongs in on Special:SpecialPage
277 * @param $page SpecialPage
279 public static function getGroup( &$page ) {
280 global $wgSpecialPageGroups;
281 static $specialPageGroupsCache = array();
282 if ( isset( $specialPageGroupsCache[$page->mName
] ) ) {
283 return $specialPageGroupsCache[$page->mName
];
285 $msg = wfMessage( 'specialpages-specialpagegroup-' . strtolower( $page->mName
) );
286 if ( !$msg->isBlank() ) {
287 $group = $msg->text();
289 $group = isset( $wgSpecialPageGroups[$page->mName
] )
290 ?
$wgSpecialPageGroups[$page->mName
]
293 if ( $group == '-' ) {
296 $specialPageGroupsCache[$page->mName
] = $group;
301 * Check if a given name exist as a special page or as a special page alias
303 * @param $name String: name of a special page
304 * @return Boolean: true if a special page exists with this name
306 public static function exists( $name ) {
307 list( $title, /*...*/ ) = self
::resolveAlias( $name );
308 return property_exists( self
::getList(), $title );
312 * Find the object with a given name and return it (or NULL)
314 * @param $name String Special page name, may be localised and/or an alias
315 * @return SpecialPage object or null if the page doesn't exist
317 public static function getPage( $name ) {
318 list( $realName, /*...*/ ) = self
::resolveAlias( $name );
319 if ( property_exists( self
::getList(), $realName ) ) {
320 $rec = self
::getList()->$realName;
321 if ( is_string( $rec ) ) {
323 return new $className;
324 } elseif ( is_array( $rec ) ) {
325 // @deprecated, officially since 1.18, unofficially since forever
326 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
327 $className = array_shift( $rec );
328 self
::getList()->$realName = MWFunction
::newObj( $className, $rec );
330 return self
::getList()->$realName;
337 * Return categorised listable special pages which are available
338 * for the current user, and everyone.
340 * @return Array( String => Specialpage )
342 public static function getUsablePages() {
345 foreach ( self
::getList() as $name => $rec ) {
346 $page = self
::getPage( $name );
347 if ( $page->isListed()
349 !$page->isRestricted()
350 ||
$page->userCanExecute( $wgUser )
353 $pages[$name] = $page;
360 * Return categorised listable special pages for all users
362 * @return Array( String => Specialpage )
364 public static function getRegularPages() {
366 foreach ( self
::getList() as $name => $rec ) {
367 $page = self
::getPage( $name );
368 if ( $page->isListed() && !$page->isRestricted() ) {
369 $pages[$name] = $page;
376 * Return categorised listable special pages which are available
377 * for the current user, but not for everyone
379 * @return Array( String => Specialpage )
381 public static function getRestrictedPages() {
384 foreach ( self
::getList() as $name => $rec ) {
385 $page = self
::getPage( $name );
388 && $page->isRestricted()
389 && $page->userCanExecute( $wgUser )
391 $pages[$name] = $page;
398 * Execute a special page path.
399 * The path may contain parameters, e.g. Special:Name/Params
400 * Extracts the special page name and call the execute method, passing the parameters
402 * Returns a title object if the page is redirected, false if there was no such special
403 * page, and true if it was successful.
405 * @param $title Title object
406 * @param $context RequestContext
407 * @param $including Bool output is being captured for use in {{special:whatever}}
411 public static function executePath( Title
&$title, RequestContext
&$context, $including = false ) {
412 wfProfileIn( __METHOD__
);
414 // @todo FIXME: Redirects broken due to this call
415 $bits = explode( '/', $title->getDBkey(), 2 );
417 if ( !isset( $bits[1] ) ) { // bug 2087
422 $page = self
::getPage( $name );
425 $context->getOutput()->setArticleRelated( false );
426 $context->getOutput()->setRobotPolicy( 'noindex,nofollow' );
427 $context->getOutput()->setStatusCode( 404 );
428 $context->getOutput()->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
429 wfProfileOut( __METHOD__
);
433 // Page exists, set the context
434 $page->setContext( $context );
437 // Redirect to canonical alias for GET commands
438 // Not for POST, we'd lose the post data, so it's best to just distribute
439 // the request. Such POST requests are possible for old extensions that
440 // generate self-links without being aware that their default name has
442 if ( $name != $page->getLocalName() && !$context->getRequest()->wasPosted() ) {
443 $query = $context->getRequest()->getQueryValues();
444 unset( $query['title'] );
445 $query = wfArrayToCGI( $query );
446 $title = $page->getTitle( $par );
447 $url = $title->getFullUrl( $query );
448 $context->getOutput()->redirect( $url );
449 wfProfileOut( __METHOD__
);
452 $context->setTitle( $page->getTitle() );
455 } elseif ( !$page->isIncludable() ) {
456 wfProfileOut( __METHOD__
);
460 $page->including( $including );
462 // Execute special page
463 $profName = 'Special:' . $page->getName();
464 wfProfileIn( $profName );
465 $page->execute( $par );
466 wfProfileOut( $profName );
467 wfProfileOut( __METHOD__
);
472 * Just like executePath() except it returns the HTML instead of outputting it
473 * Returns false if there was no such special page, or a title object if it was
476 * Also saves the current $wgTitle, $wgOut, and $wgRequest variables so that
477 * the special page will get the context it'd expect on a normal request,
478 * and then restores them to their previous values after.
480 * @param $title Title
482 * @return String: HTML fragment
484 static function capturePath( &$title ) {
485 global $wgOut, $wgTitle, $wgRequest;
487 $oldTitle = $wgTitle;
489 $oldRequest = $wgRequest;
491 // Don't want special pages interpreting ?feed=atom parameters.
492 $wgRequest = new FauxRequest( array() );
494 $context = new RequestContext
;
495 $context->setTitle( $title );
496 $context->setRequest( $wgRequest );
497 $wgOut = $context->getOutput();
499 $ret = self
::executePath( $title, $context, true );
500 if ( $ret === true ) {
501 $ret = $wgOut->getHTML();
503 $wgTitle = $oldTitle;
505 $wgRequest = $oldRequest;
510 * Get the local name for a specified canonical name
512 * @param $name String
513 * @param $subpage String|Bool
517 static function getLocalNameFor( $name, $subpage = false ) {
519 $aliases = $wgContLang->getSpecialPageAliases();
521 if ( isset( $aliases[$name][0] ) ) {
522 $name = $aliases[$name][0];
524 // Try harder in case someone misspelled the correct casing
526 foreach ( $aliases as $n => $values ) {
527 if ( strcasecmp( $name, $n ) === 0 ) {
528 wfWarn( "Found alias defined for $n when searching for " .
529 "special page aliases for $name. Case mismatch?" );
536 wfWarn( "Did not find alias for special page '$name'. " .
537 "Perhaps no aliases are defined for it?" );
540 if ( $subpage !== false && !is_null( $subpage ) ) {
541 $name = "$name/$subpage";
543 return $wgContLang->ucfirst( $name );
547 * Get a title for a given alias
549 * @param $alias String
551 * @return Title or null if there is no such alias
553 static function getTitleForAlias( $alias ) {
554 $name = self
::resolveAlias( $alias );
556 return SpecialPage
::getTitleFor( $name );