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 'ChangePassword' => 'SpecialChangePassword',
76 'PasswordReset' => 'SpecialPasswordReset',
77 'DeletedContributions' => 'DeletedContributionsPage',
78 'Preferences' => 'SpecialPreferences',
79 'Contributions' => 'SpecialContributions',
80 'Listgrouprights' => 'SpecialListGroupRights',
81 'Listusers' => 'SpecialListUsers' ,
82 'Listadmins' => 'SpecialListAdmins',
83 'Listbots' => 'SpecialListBots',
84 'Activeusers' => 'SpecialActiveUsers',
85 'Userrights' => 'UserrightsPage',
86 'EditWatchlist' => 'SpecialEditWatchlist',
88 // Recent changes and logs
89 'Newimages' => 'SpecialNewFiles',
90 'Log' => 'SpecialLog',
91 'Watchlist' => 'SpecialWatchlist',
92 'Newpages' => 'SpecialNewpages',
93 'Recentchanges' => 'SpecialRecentchanges',
94 'Recentchangeslinked' => 'SpecialRecentchangeslinked',
95 'Tags' => 'SpecialTags',
97 // Media reports and uploads
98 'Listfiles' => 'SpecialListFiles',
99 'Filepath' => 'SpecialFilepath',
100 'MIMEsearch' => 'MIMEsearchPage',
101 'FileDuplicateSearch' => 'FileDuplicateSearchPage',
102 'Upload' => 'SpecialUpload',
103 'UploadStash' => 'SpecialUploadStash',
105 // Wiki data and tools
106 'Statistics' => 'SpecialStatistics',
107 'Allmessages' => 'SpecialAllmessages',
108 'Version' => 'SpecialVersion',
109 'Lockdb' => 'SpecialLockdb',
110 'Unlockdb' => 'SpecialUnlockdb',
112 // Redirecting special pages
113 'LinkSearch' => 'LinkSearchPage',
114 'Randompage' => 'Randompage',
115 'Randomredirect' => 'SpecialRandomredirect',
118 'Mostlinkedcategories' => 'MostlinkedCategoriesPage',
119 'Mostimages' => 'MostimagesPage',
120 'Mostlinked' => 'MostlinkedPage',
121 'Mostlinkedtemplates' => 'MostlinkedTemplatesPage',
122 'Mostcategories' => 'MostcategoriesPage',
123 'Mostrevisions' => 'MostrevisionsPage',
126 'ComparePages' => 'SpecialComparePages',
127 'Export' => 'SpecialExport',
128 'Import' => 'SpecialImport',
129 'Undelete' => 'SpecialUndelete',
130 'Whatlinkshere' => 'SpecialWhatlinkshere',
131 'MergeHistory' => 'SpecialMergeHistory',
134 'Booksources' => 'SpecialBookSources',
136 // Unlisted / redirects
137 'Blankpage' => 'SpecialBlankpage',
138 'Blockme' => 'SpecialBlockme',
139 'Emailuser' => 'SpecialEmailUser',
140 'Movepage' => 'MovePageForm',
141 'Mycontributions' => 'SpecialMycontributions',
142 'Mypage' => 'SpecialMypage',
143 'Mytalk' => 'SpecialMytalk',
144 'Myuploads' => 'SpecialMyuploads',
145 'PermanentLink' => 'SpecialPermanentLink',
146 'Revisiondelete' => 'SpecialRevisionDelete',
147 'Specialpages' => 'SpecialSpecialpages',
148 'Userlogout' => 'SpecialUserlogout',
151 private static $mAliases;
154 * Initialise the special page list
155 * This must be called before accessing SpecialPage::$mList
159 static function getList() {
160 global $wgSpecialPages;
161 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
163 if ( !is_object( self
::$mList ) ) {
164 wfProfileIn( __METHOD__
);
166 if ( !$wgDisableCounters ) {
167 self
::$mList['Popularpages'] = 'PopularpagesPage';
170 if ( !$wgDisableInternalSearch ) {
171 self
::$mList['Search'] = 'SpecialSearch';
174 if ( $wgEmailAuthentication ) {
175 self
::$mList['Confirmemail'] = 'EmailConfirmation';
176 self
::$mList['Invalidateemail'] = 'EmailInvalidation';
179 // Add extension special pages
180 self
::$mList = array_merge( self
::$mList, $wgSpecialPages );
183 // This hook can be used to remove undesired built-in special pages
184 wfRunHooks( 'SpecialPage_initList', array( &self
::$mList ) );
186 // Cast to object: func()[$key] doesn't work, but func()->$key does
187 settype( self
::$mList, 'object' );
189 wfProfileOut( __METHOD__
);
195 * Initialise and return the list of special page aliases. Returns an object with
196 * properties which can be accessed $obj->pagename - each property is an array of
197 * aliases; the first in the array is the cannonical alias. All registered special
198 * pages are guaranteed to have a property entry, and for that property array to
199 * contain at least one entry (English fallbacks will be added if necessary).
202 static function getAliasList() {
203 if ( !is_object( self
::$mAliases ) ) {
205 $aliases = $wgContLang->getSpecialPageAliases();
207 // Objects are passed by reference by default, need to create a copy
208 $missingPages = clone self
::getList();
210 self
::$mAliases = array();
211 foreach ( $aliases as $realName => $aliasList ) {
212 foreach ( $aliasList as $alias ) {
213 self
::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
215 unset( $missingPages->$realName );
217 foreach ( $missingPages as $name => $stuff ) {
218 self
::$mAliases[$wgContLang->caseFold( $name )] = $name;
221 // Cast to object: func()[$key] doesn't work, but func()->$key does
222 self
::$mAliases = (object)self
::$mAliases;
224 return self
::$mAliases;
228 * Given a special page name with a possible subpage, return an array
229 * where the first element is the special page name and the second is the
232 * @param $alias String
233 * @return Array( String, String|null ), or array( null, null ) if the page is invalid
235 public static function resolveAlias( $alias ) {
237 $bits = explode( '/', $alias, 2 );
239 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
240 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
241 if ( isset( self
::getAliasList()->$caseFoldedAlias ) ) {
242 $name = self
::getAliasList()->$caseFoldedAlias;
244 return array( null, null );
247 if ( !isset( $bits[1] ) ) { // bug 2087
253 return array( $name, $par );
257 * Add a page to a certain display group for Special:SpecialPages
259 * @param $page Mixed: SpecialPage or string
260 * @param $group String
262 public static function setGroup( $page, $group ) {
263 global $wgSpecialPageGroups;
264 $name = is_object( $page ) ?
$page->mName
: $page;
265 $wgSpecialPageGroups[$name] = $group;
269 * Add a page to a certain display group for Special:SpecialPages
271 * @param $page SpecialPage
273 public static function getGroup( &$page ) {
274 global $wgSpecialPageGroups;
275 static $specialPageGroupsCache = array();
276 if ( isset( $specialPageGroupsCache[$page->mName
] ) ) {
277 return $specialPageGroupsCache[$page->mName
];
279 $msg = wfMessage( 'specialpages-specialpagegroup-' . strtolower( $page->mName
) );
280 if ( !$msg->isBlank() ) {
281 $group = $msg->text();
283 $group = isset( $wgSpecialPageGroups[$page->mName
] )
284 ?
$wgSpecialPageGroups[$page->mName
]
287 if ( $group == '-' ) {
290 $specialPageGroupsCache[$page->mName
] = $group;
295 * Check if a given name exist as a special page or as a special page alias
297 * @param $name String: name of a special page
298 * @return Boolean: true if a special page exists with this name
300 public static function exists( $name ) {
301 list( $title, /*...*/ ) = self
::resolveAlias( $name );
302 return property_exists( self
::getList(), $title );
306 * Find the object with a given name and return it (or NULL)
308 * @param $name String Special page name, may be localised and/or an alias
309 * @return SpecialPage object or null if the page doesn't exist
311 public static function getPage( $name ) {
312 list( $realName, /*...*/ ) = self
::resolveAlias( $name );
313 if ( property_exists( self
::getList(), $realName ) ) {
314 $rec = self
::getList()->$realName;
315 if ( is_string( $rec ) ) {
317 return new $className;
318 } elseif ( is_array( $rec ) ) {
319 // @deprecated, officially since 1.18, unofficially since forever
320 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
321 $className = array_shift( $rec );
322 self
::getList()->$realName = MWFunction
::newObj( $className, $rec );
324 return self
::getList()->$realName;
331 * Return categorised listable special pages which are available
332 * for the current user, and everyone.
334 * @return Array( String => Specialpage )
336 public static function getUsablePages() {
339 foreach ( self
::getList() as $name => $rec ) {
340 $page = self
::getPage( $name );
341 if ( $page->isListed()
343 !$page->isRestricted()
344 ||
$page->userCanExecute( $wgUser )
347 $pages[$name] = $page;
354 * Return categorised listable special pages for all users
356 * @return Array( String => Specialpage )
358 public static function getRegularPages() {
360 foreach ( self
::getList() as $name => $rec ) {
361 $page = self
::getPage( $name );
362 if ( $page->isListed() && !$page->isRestricted() ) {
363 $pages[$name] = $page;
370 * Return categorised listable special pages which are available
371 * for the current user, but not for everyone
373 * @return Array( String => Specialpage )
375 public static function getRestrictedPages() {
378 foreach ( self
::getList() as $name => $rec ) {
379 $page = self
::getPage( $name );
382 && $page->isRestricted()
383 && $page->userCanExecute( $wgUser )
385 $pages[$name] = $page;
392 * Execute a special page path.
393 * The path may contain parameters, e.g. Special:Name/Params
394 * Extracts the special page name and call the execute method, passing the parameters
396 * Returns a title object if the page is redirected, false if there was no such special
397 * page, and true if it was successful.
399 * @param $title Title object
400 * @param $context RequestContext
401 * @param $including Bool output is being captured for use in {{special:whatever}}
405 public static function executePath( Title
&$title, RequestContext
&$context, $including = false ) {
406 wfProfileIn( __METHOD__
);
408 // FIXME: redirects broken due to this call
409 $bits = explode( '/', $title->getDBkey(), 2 );
411 if ( !isset( $bits[1] ) ) { // bug 2087
416 $page = self
::getPage( $name );
419 $context->output
->setArticleRelated( false );
420 $context->output
->setRobotPolicy( 'noindex,nofollow' );
421 $context->output
->setStatusCode( 404 );
422 $context->output
->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
423 wfProfileOut( __METHOD__
);
427 // Page exists, set the context
428 $page->setContext( $context );
431 // Redirect to canonical alias for GET commands
432 // Not for POST, we'd lose the post data, so it's best to just distribute
433 // the request. Such POST requests are possible for old extensions that
434 // generate self-links without being aware that their default name has
436 if ( $name != $page->getLocalName() && !$context->request
->wasPosted() ) {
438 unset( $query['title'] );
439 $query = wfArrayToCGI( $query );
440 $title = $page->getTitle( $par );
441 $url = $title->getFullUrl( $query );
442 $context->output
->redirect( $url );
443 wfProfileOut( __METHOD__
);
446 $context->title
= $page->getTitle();
449 } elseif ( !$page->isIncludable() ) {
450 wfProfileOut( __METHOD__
);
454 $page->including( $including );
456 // Execute special page
457 $profName = 'Special:' . $page->getName();
458 wfProfileIn( $profName );
459 $page->execute( $par );
460 wfProfileOut( $profName );
461 wfProfileOut( __METHOD__
);
466 * Just like executePath() except it returns the HTML instead of outputting it
467 * Returns false if there was no such special page, or a title object if it was
470 * @param $title Title
472 * @return String: HTML fragment
474 static function capturePath( &$title ) {
475 global $wgOut, $wgTitle, $wgRequest;
477 $oldTitle = $wgTitle;
479 $oldRequest = $wgRequest;
481 // Don't want special pages interpreting ?feed=atom parameters.
482 $wgRequest = new FauxRequest( array() );
484 $context = new RequestContext
;
485 $context->setTitle( $title );
486 $context->setRequest( $wgRequest );
487 $wgOut = $context->getOutput();
489 $ret = self
::executePath( $title, $context, true );
490 if ( $ret === true ) {
491 $ret = $wgOut->getHTML();
493 $wgTitle = $oldTitle;
495 $wgRequest = $oldRequest;
500 * Get the local name for a specified canonical name
502 * @param $name String
503 * @param $subpage String|Bool
507 static function getLocalNameFor( $name, $subpage = false ) {
509 $aliases = $wgContLang->getSpecialPageAliases();
510 if ( isset( $aliases[$name][0] ) ) {
511 $name = $aliases[$name][0];
513 // Try harder in case someone misspelled the correct casing
515 foreach ( $aliases as $n => $values ) {
516 if ( strcasecmp( $name, $n ) === 0 ) {
517 wfWarn( "Found alias defined for $n when searching for " .
518 "special page aliases for $name. Case mismatch?" );
525 wfWarn( "Did not find alias for special page '$name'. " .
526 "Perhaps no aliases are defined for it?" );
529 if ( $subpage !== false && !is_null( $subpage ) ) {
530 $name = "$name/$subpage";
532 return $wgContLang->ucfirst( $name );
536 * Get a title for a given alias
538 * @param $alias String
540 * @return Title or null if there is no such alias
542 static function getTitleForAlias( $alias ) {
543 $name = self
::resolveAlias( $alias );
545 return SpecialPage
::getTitleFor( $name );