2a74186e7f6e334679f6fd28759508944b125a36
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
157 static function getList() {
158 global $wgSpecialPages;
159 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
161 if ( !is_object( self
::$mList ) ) {
162 wfProfileIn( __METHOD__
);
164 if ( !$wgDisableCounters ) {
165 self
::$mList['Popularpages'] = 'PopularpagesPage';
168 if ( !$wgDisableInternalSearch ) {
169 self
::$mList['Search'] = 'SpecialSearch';
172 if ( $wgEmailAuthentication ) {
173 self
::$mList['Confirmemail'] = 'EmailConfirmation';
174 self
::$mList['Invalidateemail'] = 'EmailInvalidation';
177 // Add extension special pages
178 self
::$mList = array_merge( self
::$mList, $wgSpecialPages );
181 // This hook can be used to remove undesired built-in special pages
182 wfRunHooks( 'SpecialPage_initList', array( &self
::$mList ) );
184 // Cast to object: func()[$key] doesn't work, but func()->$key does
185 settype( self
::$mList, 'object' );
187 wfProfileOut( __METHOD__
);
192 static function getAliasList() {
193 if ( !is_object( self
::$mAliases ) ) {
195 $aliases = $wgContLang->getSpecialPageAliases();
197 // Objects are passed by reference by default, need to create a copy
198 $missingPages = clone self
::getList();
200 self
::$mAliases = array();
201 foreach ( $aliases as $realName => $aliasList ) {
202 foreach ( $aliasList as $alias ) {
203 self
::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
205 unset( $missingPages->$realName );
207 foreach ( $missingPages as $name => $stuff ) {
208 self
::$mAliases[$wgContLang->caseFold( $name )] = $name;
211 // Cast to object: func()[$key] doesn't work, but func()->$key does
212 self
::$mAliases = (object)self
::$mAliases;
214 return self
::$mAliases;
218 * Given a special page name with a possible subpage, return an array
219 * where the first element is the special page name and the second is the
222 * @param $alias String
223 * @return Array( String, String|null ), or array( null, null ) if the page is invalid
225 public static function resolveAlias( $alias ) {
227 $bits = explode( '/', $alias, 2 );
229 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
230 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
231 if ( isset( self
::getAliasList()->$caseFoldedAlias ) ) {
232 $name = self
::getAliasList()->$caseFoldedAlias;
234 return array( null, null );
237 if ( !isset( $bits[1] ) ) { // bug 2087
243 return array( $name, $par );
247 * Add a page to a certain display group for Special:SpecialPages
249 * @param $page Mixed: SpecialPage or string
250 * @param $group String
252 public static function setGroup( $page, $group ) {
253 global $wgSpecialPageGroups;
254 $name = is_object( $page ) ?
$page->mName
: $page;
255 $wgSpecialPageGroups[$name] = $group;
259 * Add a page to a certain display group for Special:SpecialPages
261 * @param $page SpecialPage
263 public static function getGroup( &$page ) {
264 global $wgSpecialPageGroups;
265 static $specialPageGroupsCache = array();
266 if ( isset( $specialPageGroupsCache[$page->mName
] ) ) {
267 return $specialPageGroupsCache[$page->mName
];
269 $msg = wfMessage( 'specialpages-specialpagegroup-' . strtolower( $page->mName
) );
270 if ( !$msg->isBlank() ) {
271 $group = $msg->text();
273 $group = isset( $wgSpecialPageGroups[$page->mName
] )
274 ?
$wgSpecialPageGroups[$page->mName
]
277 if ( $group == '-' ) {
280 $specialPageGroupsCache[$page->mName
] = $group;
285 * Check if a given name exist as a special page or as a special page alias
287 * @param $name String: name of a special page
288 * @return Boolean: true if a special page exists with this name
290 public static function exists( $name ) {
291 list( $title, /*...*/ ) = self
::resolveAlias( $name );
292 return property_exists( self
::getList(), $title );
296 * Find the object with a given name and return it (or NULL)
298 * @param $name String Special page name, may be localised and/or an alias
299 * @return SpecialPage object or null if the page doesn't exist
301 public static function getPage( $name ) {
302 list( $realName, /*...*/ ) = self
::resolveAlias( $name );
303 if ( property_exists( self
::getList(), $realName ) ) {
304 $rec = self
::getList()->$realName;
305 if ( is_string( $rec ) ) {
307 self
::getList()->$realName = new $className;
308 } elseif ( is_array( $rec ) ) {
309 // @deprecated, officially since 1.18, unofficially since forever
310 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
311 $className = array_shift( $rec );
312 self
::getList()->$realName = MWFunction
::newObj( $className, $rec );
314 return self
::getList()->$realName;
321 * Return categorised listable special pages which are available
322 * for the current user, and everyone.
324 * @return Array( String => Specialpage )
326 public static function getUsablePages() {
329 foreach ( self
::getList() as $name => $rec ) {
330 $page = self
::getPage( $name );
331 if ( $page->isListed()
333 !$page->isRestricted()
334 ||
$page->userCanExecute( $wgUser )
337 $pages[$name] = $page;
344 * Return categorised listable special pages for all users
346 * @return Array( String => Specialpage )
348 public static function getRegularPages() {
350 foreach ( self
::getList() as $name => $rec ) {
351 $page = self
::getPage( $name );
352 if ( $page->isListed() && !$page->isRestricted() ) {
353 $pages[$name] = $page;
360 * Return categorised listable special pages which are available
361 * for the current user, but not for everyone
363 * @return Array( String => Specialpage )
365 public static function getRestrictedPages() {
368 foreach ( self
::getList() as $name => $rec ) {
369 $page = self
::getPage( $name );
372 && $page->isRestricted()
373 && $page->userCanExecute( $wgUser )
375 $pages[$name] = $page;
382 * Execute a special page path.
383 * The path may contain parameters, e.g. Special:Name/Params
384 * Extracts the special page name and call the execute method, passing the parameters
386 * Returns a title object if the page is redirected, false if there was no such special
387 * page, and true if it was successful.
389 * @param $title Title object
390 * @param $context RequestContext
391 * @param $including Bool output is being captured for use in {{special:whatever}}
393 public static function executePath( Title
&$title, RequestContext
&$context, $including = false ) {
394 wfProfileIn( __METHOD__
);
396 // FIXME: redirects broken due to this call
397 $bits = explode( '/', $title->getDBkey(), 2 );
399 if ( !isset( $bits[1] ) ) { // bug 2087
404 $page = self
::getPage( $name );
407 $context->output
->setArticleRelated( false );
408 $context->output
->setRobotPolicy( 'noindex,nofollow' );
409 $context->output
->setStatusCode( 404 );
410 $context->output
->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
411 wfProfileOut( __METHOD__
);
415 // Page exists, set the context
416 $page->setContext( $context );
419 // Redirect to canonical alias for GET commands
420 // Not for POST, we'd lose the post data, so it's best to just distribute
421 // the request. Such POST requests are possible for old extensions that
422 // generate self-links without being aware that their default name has
424 if ( $name != $page->getLocalName() && !$context->request
->wasPosted() ) {
426 unset( $query['title'] );
427 $query = wfArrayToCGI( $query );
428 $title = $page->getTitle( $par );
429 $url = $title->getFullUrl( $query );
430 $context->output
->redirect( $url );
431 wfProfileOut( __METHOD__
);
434 $context->title
= $page->getTitle();
437 } elseif ( !$page->isIncludable() ) {
438 wfProfileOut( __METHOD__
);
442 $page->including( $including );
444 // Execute special page
445 $profName = 'Special:' . $page->getName();
446 wfProfileIn( $profName );
447 $page->execute( $par );
448 wfProfileOut( $profName );
449 wfProfileOut( __METHOD__
);
454 * Just like executePath() except it returns the HTML instead of outputting it
455 * Returns false if there was no such special page, or a title object if it was
458 * @return String: HTML fragment
460 static function capturePath( &$title ) {
461 global $wgOut, $wgTitle;
463 $oldTitle = $wgTitle;
466 $context = new RequestContext
;
467 $context->setTitle( $title );
468 $wgOut = $context->getOutput();
470 $ret = self
::executePath( $title, $context, true );
471 if ( $ret === true ) {
472 $ret = $wgOut->getHTML();
474 $wgTitle = $oldTitle;
480 * Get the local name for a specified canonical name
482 * @param $name String
483 * @param $subpage String|Bool
487 static function getLocalNameFor( $name, $subpage = false ) {
489 $aliases = $wgContLang->getSpecialPageAliases();
490 if ( isset( $aliases[$name][0] ) ) {
491 $name = $aliases[$name][0];
493 // Try harder in case someone misspelled the correct casing
495 foreach ( $aliases as $n => $values ) {
496 if ( strcasecmp( $name, $n ) === 0 ) {
497 wfWarn( "Found alias defined for $n when searching for " .
498 "special page aliases for $name. Case mismatch?" );
505 wfWarn( "Did not find alias for special page '$name'. " .
506 "Perhaps no aliases are defined for it?" );
509 if ( $subpage !== false && !is_null( $subpage ) ) {
510 $name = "$name/$subpage";
512 return $wgContLang->ucfirst( $name );
516 * Get a title for a given alias
518 * @return Title or null if there is no such alias
520 static function getTitleForAlias( $alias ) {
521 $name = self
::resolveAlias( $alias );
523 return self
::getTitleFor( $name );