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__
);
197 static function getAliasList() {
198 if ( !is_object( self
::$mAliases ) ) {
200 $aliases = $wgContLang->getSpecialPageAliases();
202 // Objects are passed by reference by default, need to create a copy
203 $missingPages = clone self
::getList();
205 self
::$mAliases = array();
206 foreach ( $aliases as $realName => $aliasList ) {
207 foreach ( $aliasList as $alias ) {
208 self
::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
210 unset( $missingPages->$realName );
212 foreach ( $missingPages as $name => $stuff ) {
213 self
::$mAliases[$wgContLang->caseFold( $name )] = $name;
216 // Cast to object: func()[$key] doesn't work, but func()->$key does
217 self
::$mAliases = (object)self
::$mAliases;
219 return self
::$mAliases;
223 * Given a special page name with a possible subpage, return an array
224 * where the first element is the special page name and the second is the
227 * @param $alias String
228 * @return Array( String, String|null ), or array( null, null ) if the page is invalid
230 public static function resolveAlias( $alias ) {
232 $bits = explode( '/', $alias, 2 );
234 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
235 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
236 if ( isset( self
::getAliasList()->$caseFoldedAlias ) ) {
237 $name = self
::getAliasList()->$caseFoldedAlias;
239 return array( null, null );
242 if ( !isset( $bits[1] ) ) { // bug 2087
248 return array( $name, $par );
252 * Add a page to a certain display group for Special:SpecialPages
254 * @param $page Mixed: SpecialPage or string
255 * @param $group String
257 public static function setGroup( $page, $group ) {
258 global $wgSpecialPageGroups;
259 $name = is_object( $page ) ?
$page->mName
: $page;
260 $wgSpecialPageGroups[$name] = $group;
264 * Add a page to a certain display group for Special:SpecialPages
266 * @param $page SpecialPage
268 public static function getGroup( &$page ) {
269 global $wgSpecialPageGroups;
270 static $specialPageGroupsCache = array();
271 if ( isset( $specialPageGroupsCache[$page->mName
] ) ) {
272 return $specialPageGroupsCache[$page->mName
];
274 $msg = wfMessage( 'specialpages-specialpagegroup-' . strtolower( $page->mName
) );
275 if ( !$msg->isBlank() ) {
276 $group = $msg->text();
278 $group = isset( $wgSpecialPageGroups[$page->mName
] )
279 ?
$wgSpecialPageGroups[$page->mName
]
282 if ( $group == '-' ) {
285 $specialPageGroupsCache[$page->mName
] = $group;
290 * Check if a given name exist as a special page or as a special page alias
292 * @param $name String: name of a special page
293 * @return Boolean: true if a special page exists with this name
295 public static function exists( $name ) {
296 list( $title, /*...*/ ) = self
::resolveAlias( $name );
297 return property_exists( self
::getList(), $title );
301 * Find the object with a given name and return it (or NULL)
303 * @param $name String Special page name, may be localised and/or an alias
304 * @return SpecialPage object or null if the page doesn't exist
306 public static function getPage( $name ) {
307 list( $realName, /*...*/ ) = self
::resolveAlias( $name );
308 if ( property_exists( self
::getList(), $realName ) ) {
309 $rec = self
::getList()->$realName;
310 if ( is_string( $rec ) ) {
312 self
::getList()->$realName = new $className;
313 } elseif ( is_array( $rec ) ) {
314 // @deprecated, officially since 1.18, unofficially since forever
315 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
316 $className = array_shift( $rec );
317 self
::getList()->$realName = MWFunction
::newObj( $className, $rec );
319 return self
::getList()->$realName;
326 * Return categorised listable special pages which are available
327 * for the current user, and everyone.
329 * @return Array( String => Specialpage )
331 public static function getUsablePages() {
334 foreach ( self
::getList() as $name => $rec ) {
335 $page = self
::getPage( $name );
336 if ( $page->isListed()
338 !$page->isRestricted()
339 ||
$page->userCanExecute( $wgUser )
342 $pages[$name] = $page;
349 * Return categorised listable special pages for all users
351 * @return Array( String => Specialpage )
353 public static function getRegularPages() {
355 foreach ( self
::getList() as $name => $rec ) {
356 $page = self
::getPage( $name );
357 if ( $page->isListed() && !$page->isRestricted() ) {
358 $pages[$name] = $page;
365 * Return categorised listable special pages which are available
366 * for the current user, but not for everyone
368 * @return Array( String => Specialpage )
370 public static function getRestrictedPages() {
373 foreach ( self
::getList() as $name => $rec ) {
374 $page = self
::getPage( $name );
377 && $page->isRestricted()
378 && $page->userCanExecute( $wgUser )
380 $pages[$name] = $page;
387 * Execute a special page path.
388 * The path may contain parameters, e.g. Special:Name/Params
389 * Extracts the special page name and call the execute method, passing the parameters
391 * Returns a title object if the page is redirected, false if there was no such special
392 * page, and true if it was successful.
394 * @param $title Title object
395 * @param $context RequestContext
396 * @param $including Bool output is being captured for use in {{special:whatever}}
400 public static function executePath( Title
&$title, RequestContext
&$context, $including = false ) {
401 wfProfileIn( __METHOD__
);
403 // FIXME: redirects broken due to this call
404 $bits = explode( '/', $title->getDBkey(), 2 );
406 if ( !isset( $bits[1] ) ) { // bug 2087
411 $page = self
::getPage( $name );
414 $context->output
->setArticleRelated( false );
415 $context->output
->setRobotPolicy( 'noindex,nofollow' );
416 $context->output
->setStatusCode( 404 );
417 $context->output
->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
418 wfProfileOut( __METHOD__
);
422 // Page exists, set the context
423 $page->setContext( $context );
426 // Redirect to canonical alias for GET commands
427 // Not for POST, we'd lose the post data, so it's best to just distribute
428 // the request. Such POST requests are possible for old extensions that
429 // generate self-links without being aware that their default name has
431 if ( $name != $page->getLocalName() && !$context->request
->wasPosted() ) {
433 unset( $query['title'] );
434 $query = wfArrayToCGI( $query );
435 $title = $page->getTitle( $par );
436 $url = $title->getFullUrl( $query );
437 $context->output
->redirect( $url );
438 wfProfileOut( __METHOD__
);
441 $context->title
= $page->getTitle();
444 } elseif ( !$page->isIncludable() ) {
445 wfProfileOut( __METHOD__
);
449 $page->including( $including );
451 // Execute special page
452 $profName = 'Special:' . $page->getName();
453 wfProfileIn( $profName );
454 $page->execute( $par );
455 wfProfileOut( $profName );
456 wfProfileOut( __METHOD__
);
461 * Just like executePath() except it returns the HTML instead of outputting it
462 * Returns false if there was no such special page, or a title object if it was
465 * @param $title Title
467 * @return String: HTML fragment
469 static function capturePath( &$title ) {
470 global $wgOut, $wgTitle;
472 $oldTitle = $wgTitle;
475 $context = new RequestContext
;
476 $context->setTitle( $title );
477 $wgOut = $context->getOutput();
479 $ret = self
::executePath( $title, $context, true );
480 if ( $ret === true ) {
481 $ret = $wgOut->getHTML();
483 $wgTitle = $oldTitle;
489 * Get the local name for a specified canonical name
491 * @param $name String
492 * @param $subpage String|Bool
496 static function getLocalNameFor( $name, $subpage = false ) {
498 $aliases = $wgContLang->getSpecialPageAliases();
499 if ( isset( $aliases[$name][0] ) ) {
500 $name = $aliases[$name][0];
502 // Try harder in case someone misspelled the correct casing
504 foreach ( $aliases as $n => $values ) {
505 if ( strcasecmp( $name, $n ) === 0 ) {
506 wfWarn( "Found alias defined for $n when searching for " .
507 "special page aliases for $name. Case mismatch?" );
514 wfWarn( "Did not find alias for special page '$name'. " .
515 "Perhaps no aliases are defined for it?" );
518 if ( $subpage !== false && !is_null( $subpage ) ) {
519 $name = "$name/$subpage";
521 return $wgContLang->ucfirst( $name );
525 * Get a title for a given alias
527 * @param $alias String
529 * @return Title or null if there is no such alias
531 static function getTitleForAlias( $alias ) {
532 $name = self
::resolveAlias( $alias );
534 return SpecialPage
::getTitleFor( $name );