Rename Special:Resetpass to Special:ChangePassword. "pass" is vague and unintuitive...
[lhc/web/wiklou.git] / includes / SpecialPageFactory.php
1 <?php
2 /**
3 * SpecialPage: handling special pages and lists thereof.
4 *
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.
8 *
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.
15 *
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.
19 *
20 * @file
21 * @ingroup SpecialPage
22 * @defgroup SpecialPage SpecialPage
23 */
24
25 /**
26 * Factory for handling the special page list and generating SpecialPage objects
27 * @ingroup SpecialPage
28 */
29 class SpecialPageFactory {
30
31 /**
32 * List of special page names to the subclass of SpecialPage which handles them.
33 */
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',
59
60 // List of pages
61 'Allpages' => 'SpecialAllpages',
62 'Prefixindex' => 'SpecialPrefixindex',
63 'Categories' => 'SpecialCategories',
64 'Disambiguations' => 'DisambiguationsPage',
65 'Listredirects' => 'ListredirectsPage',
66
67 // Login/create account
68 'Userlogin' => 'LoginForm',
69 'CreateAccount' => 'SpecialCreateAccount',
70
71 // Users and rights
72 'Block' => 'SpecialBlock',
73 'Unblock' => 'SpecialUnblock',
74 'BlockList' => 'SpecialBlockList',
75 'ChangePassword' => 'SpecialChangePassword',
76 'DeletedContributions' => 'DeletedContributionsPage',
77 'Preferences' => 'SpecialPreferences',
78 'Contributions' => 'SpecialContributions',
79 'Listgrouprights' => 'SpecialListGroupRights',
80 'Listusers' => 'SpecialListUsers' ,
81 'Listadmins' => 'SpecialListAdmins',
82 'Listbots' => 'SpecialListBots',
83 'Activeusers' => 'SpecialActiveUsers',
84 'Userrights' => 'UserrightsPage',
85 'EditWatchlist' => 'SpecialEditWatchlist',
86
87 // Recent changes and logs
88 'Newimages' => 'SpecialNewFiles',
89 'Log' => 'SpecialLog',
90 'Watchlist' => 'SpecialWatchlist',
91 'Newpages' => 'SpecialNewpages',
92 'Recentchanges' => 'SpecialRecentchanges',
93 'Recentchangeslinked' => 'SpecialRecentchangeslinked',
94 'Tags' => 'SpecialTags',
95
96 // Media reports and uploads
97 'Listfiles' => 'SpecialListFiles',
98 'Filepath' => 'SpecialFilepath',
99 'MIMEsearch' => 'MIMEsearchPage',
100 'FileDuplicateSearch' => 'FileDuplicateSearchPage',
101 'Upload' => 'SpecialUpload',
102 'UploadStash' => 'SpecialUploadStash',
103
104 // Wiki data and tools
105 'Statistics' => 'SpecialStatistics',
106 'Allmessages' => 'SpecialAllmessages',
107 'Version' => 'SpecialVersion',
108 'Lockdb' => 'SpecialLockdb',
109 'Unlockdb' => 'SpecialUnlockdb',
110
111 // Redirecting special pages
112 'LinkSearch' => 'LinkSearchPage',
113 'Randompage' => 'Randompage',
114 'Randomredirect' => 'SpecialRandomredirect',
115
116 // High use pages
117 'Mostlinkedcategories' => 'MostlinkedCategoriesPage',
118 'Mostimages' => 'MostimagesPage',
119 'Mostlinked' => 'MostlinkedPage',
120 'Mostlinkedtemplates' => 'MostlinkedTemplatesPage',
121 'Mostcategories' => 'MostcategoriesPage',
122 'Mostrevisions' => 'MostrevisionsPage',
123
124 // Page tools
125 'ComparePages' => 'SpecialComparePages',
126 'Export' => 'SpecialExport',
127 'Import' => 'SpecialImport',
128 'Undelete' => 'SpecialUndelete',
129 'Whatlinkshere' => 'SpecialWhatlinkshere',
130 'MergeHistory' => 'SpecialMergeHistory',
131
132 // Other
133 'Booksources' => 'SpecialBookSources',
134
135 // Unlisted / redirects
136 'Blankpage' => 'SpecialBlankpage',
137 'Blockme' => 'SpecialBlockme',
138 'Emailuser' => 'SpecialEmailUser',
139 'Movepage' => 'MovePageForm',
140 'Mycontributions' => 'SpecialMycontributions',
141 'Mypage' => 'SpecialMypage',
142 'Mytalk' => 'SpecialMytalk',
143 'Myuploads' => 'SpecialMyuploads',
144 'PermanentLink' => 'SpecialPermanentLink',
145 'Revisiondelete' => 'SpecialRevisionDelete',
146 'Specialpages' => 'SpecialSpecialpages',
147 'Userlogout' => 'SpecialUserlogout',
148 );
149
150 private static $mAliases;
151
152 /**
153 * Initialise the special page list
154 * This must be called before accessing SpecialPage::$mList
155 */
156 static function getList() {
157 global $wgSpecialPages;
158 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
159
160 if ( !is_object( self::$mList ) ) {
161 wfProfileIn( __METHOD__ );
162
163 if ( !$wgDisableCounters ) {
164 self::$mList['Popularpages'] = 'PopularpagesPage';
165 }
166
167 if ( !$wgDisableInternalSearch ) {
168 self::$mList['Search'] = 'SpecialSearch';
169 }
170
171 if ( $wgEmailAuthentication ) {
172 self::$mList['Confirmemail'] = 'EmailConfirmation';
173 self::$mList['Invalidateemail'] = 'EmailInvalidation';
174 }
175
176 // Add extension special pages
177 self::$mList = array_merge( self::$mList, $wgSpecialPages );
178
179 // Run hooks
180 // This hook can be used to remove undesired built-in special pages
181 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
182
183 // Cast to object: func()[$key] doesn't work, but func()->$key does
184 settype( self::$mList, 'object' );
185
186 wfProfileOut( __METHOD__ );
187 }
188 return self::$mList;
189 }
190
191 static function getAliasList() {
192 if ( !is_object( self::$mAliases ) ) {
193 global $wgContLang;
194 $aliases = $wgContLang->getSpecialPageAliases();
195
196 // Objects are passed by reference by default, need to create a copy
197 $missingPages = clone self::getList();
198
199 self::$mAliases = array();
200 foreach ( $aliases as $realName => $aliasList ) {
201 foreach ( $aliasList as $alias ) {
202 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
203 }
204 unset( $missingPages->$realName );
205 }
206 foreach ( $missingPages as $name => $stuff ) {
207 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
208 }
209
210 // Cast to object: func()[$key] doesn't work, but func()->$key does
211 self::$mAliases = (object)self::$mAliases;
212 }
213 return self::$mAliases;
214 }
215
216 /**
217 * Given a special page name with a possible subpage, return an array
218 * where the first element is the special page name and the second is the
219 * subpage.
220 *
221 * @param $alias String
222 * @return Array( String, String|null ), or array( null, null ) if the page is invalid
223 */
224 public static function resolveAlias( $alias ) {
225 global $wgContLang;
226 $bits = explode( '/', $alias, 2 );
227
228 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
229 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
230 if ( isset( self::getAliasList()->$caseFoldedAlias ) ) {
231 $name = self::getAliasList()->$caseFoldedAlias;
232 } else {
233 return array( null, null );
234 }
235
236 if ( !isset( $bits[1] ) ) { // bug 2087
237 $par = null;
238 } else {
239 $par = $bits[1];
240 }
241
242 return array( $name, $par );
243 }
244
245 /**
246 * Add a page to a certain display group for Special:SpecialPages
247 *
248 * @param $page Mixed: SpecialPage or string
249 * @param $group String
250 */
251 public static function setGroup( $page, $group ) {
252 global $wgSpecialPageGroups;
253 $name = is_object( $page ) ? $page->mName : $page;
254 $wgSpecialPageGroups[$name] = $group;
255 }
256
257 /**
258 * Add a page to a certain display group for Special:SpecialPages
259 *
260 * @param $page SpecialPage
261 */
262 public static function getGroup( &$page ) {
263 global $wgSpecialPageGroups;
264 static $specialPageGroupsCache = array();
265 if ( isset( $specialPageGroupsCache[$page->mName] ) ) {
266 return $specialPageGroupsCache[$page->mName];
267 }
268 $msg = wfMessage( 'specialpages-specialpagegroup-' . strtolower( $page->mName ) );
269 if ( !$msg->isBlank() ) {
270 $group = $msg->text();
271 } else {
272 $group = isset( $wgSpecialPageGroups[$page->mName] )
273 ? $wgSpecialPageGroups[$page->mName]
274 : '-';
275 }
276 if ( $group == '-' ) {
277 $group = 'other';
278 }
279 $specialPageGroupsCache[$page->mName] = $group;
280 return $group;
281 }
282
283 /**
284 * Check if a given name exist as a special page or as a special page alias
285 *
286 * @param $name String: name of a special page
287 * @return Boolean: true if a special page exists with this name
288 */
289 public static function exists( $name ) {
290 list( $title, /*...*/ ) = self::resolveAlias( $name );
291 return property_exists( self::getList(), $title );
292 }
293
294 /**
295 * Find the object with a given name and return it (or NULL)
296 *
297 * @param $name String Special page name, may be localised and/or an alias
298 * @return SpecialPage object or null if the page doesn't exist
299 */
300 public static function getPage( $name ) {
301 list( $realName, /*...*/ ) = self::resolveAlias( $name );
302 if ( property_exists( self::getList(), $realName ) ) {
303 $rec = self::getList()->$realName;
304 if ( is_string( $rec ) ) {
305 $className = $rec;
306 self::getList()->$realName = new $className;
307 } elseif ( is_array( $rec ) ) {
308 // @deprecated, officially since 1.18, unofficially since forever
309 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
310 $className = array_shift( $rec );
311 self::getList()->$realName = MWFunction::newObj( $className, $rec );
312 }
313 return self::getList()->$realName;
314 } else {
315 return null;
316 }
317 }
318
319 /**
320 * Return categorised listable special pages which are available
321 * for the current user, and everyone.
322 *
323 * @return Array( String => Specialpage )
324 */
325 public static function getUsablePages() {
326 global $wgUser;
327 $pages = array();
328 foreach ( self::getList() as $name => $rec ) {
329 $page = self::getPage( $name );
330 if ( $page->isListed()
331 && (
332 !$page->isRestricted()
333 || $page->userCanExecute( $wgUser )
334 )
335 ) {
336 $pages[$name] = $page;
337 }
338 }
339 return $pages;
340 }
341
342 /**
343 * Return categorised listable special pages for all users
344 *
345 * @return Array( String => Specialpage )
346 */
347 public static function getRegularPages() {
348 $pages = array();
349 foreach ( self::getList() as $name => $rec ) {
350 $page = self::getPage( $name );
351 if ( $page->isListed() && !$page->isRestricted() ) {
352 $pages[$name] = $page;
353 }
354 }
355 return $pages;
356 }
357
358 /**
359 * Return categorised listable special pages which are available
360 * for the current user, but not for everyone
361 *
362 * @return Array( String => Specialpage )
363 */
364 public static function getRestrictedPages() {
365 global $wgUser;
366 $pages = array();
367 foreach ( self::getList() as $name => $rec ) {
368 $page = self::getPage( $name );
369 if (
370 $page->isListed()
371 && $page->isRestricted()
372 && $page->userCanExecute( $wgUser )
373 ) {
374 $pages[$name] = $page;
375 }
376 }
377 return $pages;
378 }
379
380 /**
381 * Execute a special page path.
382 * The path may contain parameters, e.g. Special:Name/Params
383 * Extracts the special page name and call the execute method, passing the parameters
384 *
385 * Returns a title object if the page is redirected, false if there was no such special
386 * page, and true if it was successful.
387 *
388 * @param $title Title object
389 * @param $context RequestContext
390 * @param $including Bool output is being captured for use in {{special:whatever}}
391 */
392 public static function executePath( Title &$title, RequestContext &$context, $including = false ) {
393 wfProfileIn( __METHOD__ );
394
395 // FIXME: redirects broken due to this call
396 $bits = explode( '/', $title->getDBkey(), 2 );
397 $name = $bits[0];
398 if ( !isset( $bits[1] ) ) { // bug 2087
399 $par = null;
400 } else {
401 $par = $bits[1];
402 }
403 $page = self::getPage( $name );
404 // Nonexistent?
405 if ( !$page ) {
406 $context->output->setArticleRelated( false );
407 $context->output->setRobotPolicy( 'noindex,nofollow' );
408 $context->output->setStatusCode( 404 );
409 $context->output->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
410 wfProfileOut( __METHOD__ );
411 return false;
412 }
413
414 // Page exists, set the context
415 $page->setContext( $context );
416
417 // Check for redirect
418 if ( !$including ) {
419 $redirect = $page->getRedirect( $par );
420 $query = $page->getRedirectQuery();
421 if ( $redirect instanceof Title ) {
422 $url = $redirect->getFullUrl( $query );
423 $context->output->redirect( $url );
424 wfProfileOut( __METHOD__ );
425 return $redirect;
426 } elseif ( $redirect === true ) {
427 global $wgScript;
428 $url = $wgScript . '?' . wfArrayToCGI( $query );
429 $context->output->redirect( $url );
430 wfProfileOut( __METHOD__ );
431 return $redirect;
432 }
433
434 // Redirect to canonical alias for GET commands
435 // Not for POST, we'd lose the post data, so it's best to just distribute
436 // the request. Such POST requests are possible for old extensions that
437 // generate self-links without being aware that their default name has
438 // changed.
439 if ( $name != $page->getLocalName() && !$context->request->wasPosted() ) {
440 $query = $_GET;
441 unset( $query['title'] );
442 $query = wfArrayToCGI( $query );
443 $title = $page->getTitle( $par );
444 $url = $title->getFullUrl( $query );
445 $context->output->redirect( $url );
446 wfProfileOut( __METHOD__ );
447 return $redirect;
448 } else {
449 $context->title = $page->getTitle();
450 }
451
452 } elseif ( !$page->includable() ) {
453 wfProfileOut( __METHOD__ );
454 return false;
455 }
456
457 $page->including( $including );
458
459 // Execute special page
460 $profName = 'Special:' . $page->name();
461 wfProfileIn( $profName );
462 $page->execute( $par );
463 wfProfileOut( $profName );
464 wfProfileOut( __METHOD__ );
465 return true;
466 }
467
468 /**
469 * Just like executePath() except it returns the HTML instead of outputting it
470 * Returns false if there was no such special page, or a title object if it was
471 * a redirect.
472 *
473 * @return String: HTML fragment
474 */
475 static function capturePath( &$title ) {
476 global $wgOut, $wgTitle;
477
478 $oldTitle = $wgTitle;
479 $oldOut = $wgOut;
480
481 $context = new RequestContext;
482 $context->setTitle( $title );
483 $wgOut = $context->getOutput();
484
485 $ret = self::executePath( $title, $context, true );
486 if ( $ret === true ) {
487 $ret = $wgOut->getHTML();
488 }
489 $wgTitle = $oldTitle;
490 $wgOut = $oldOut;
491 return $ret;
492 }
493
494 /**
495 * Get the local name for a specified canonical name
496 *
497 * @param $name String
498 * @param $subpage String|Bool
499 *
500 * @return String
501 */
502 static function getLocalNameFor( $name, $subpage = false ) {
503 global $wgContLang;
504 $aliases = $wgContLang->getSpecialPageAliases();
505 if ( isset( $aliases[$name][0] ) ) {
506 $name = $aliases[$name][0];
507 } else {
508 // Try harder in case someone misspelled the correct casing
509 $found = false;
510 foreach ( $aliases as $n => $values ) {
511 if ( strcasecmp( $name, $n ) === 0 ) {
512 wfWarn( "Found alias defined for $n when searching for " .
513 "special page aliases for $name. Case mismatch?" );
514 $name = $values[0];
515 $found = true;
516 break;
517 }
518 }
519 if ( !$found ) {
520 wfWarn( "Did not find alias for special page '$name'. " .
521 "Perhaps no aliases are defined for it?" );
522 }
523 }
524 if ( $subpage !== false && !is_null( $subpage ) ) {
525 $name = "$name/$subpage";
526 }
527 return $wgContLang->ucfirst( $name );
528 }
529
530 /**
531 * Get a title for a given alias
532 *
533 * @return Title or null if there is no such alias
534 */
535 static function getTitleForAlias( $alias ) {
536 $name = self::resolveAlias( $alias );
537 if ( $name ) {
538 return self::getTitleFor( $name );
539 } else {
540 return null;
541 }
542 }
543 }