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