Add support for Number grouping(commafy) based on CLDR number grouping patterns like...
[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 SpecialPage|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 * @return string
282 */
283 public static function getGroup( &$page ) {
284 $name = $page->getName();
285
286 global $wgSpecialPageGroups;
287 static $specialPageGroupsCache = array();
288 if ( isset( $specialPageGroupsCache[$name] ) ) {
289 return $specialPageGroupsCache[$name];
290 }
291 $msg = wfMessage( 'specialpages-specialpagegroup-' . strtolower( $name ) );
292 if ( !$msg->isBlank() ) {
293 $group = $msg->text();
294 } else {
295 $group = isset( $wgSpecialPageGroups[$name] )
296 ? $wgSpecialPageGroups[$name]
297 : '-';
298 }
299 if ( $group == '-' ) {
300 $group = 'other';
301 }
302 $specialPageGroupsCache[$name] = $group;
303 return $group;
304 }
305
306 /**
307 * Check if a given name exist as a special page or as a special page alias
308 *
309 * @param $name String: name of a special page
310 * @return Boolean: true if a special page exists with this name
311 */
312 public static function exists( $name ) {
313 list( $title, /*...*/ ) = self::resolveAlias( $name );
314 return property_exists( self::getList(), $title );
315 }
316
317 /**
318 * Find the object with a given name and return it (or NULL)
319 *
320 * @param $name String Special page name, may be localised and/or an alias
321 * @return SpecialPage object or null if the page doesn't exist
322 */
323 public static function getPage( $name ) {
324 list( $realName, /*...*/ ) = self::resolveAlias( $name );
325 if ( property_exists( self::getList(), $realName ) ) {
326 $rec = self::getList()->$realName;
327 if ( is_string( $rec ) ) {
328 $className = $rec;
329 return new $className;
330 } elseif ( is_array( $rec ) ) {
331 // @deprecated, officially since 1.18, unofficially since forever
332 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
333 $className = array_shift( $rec );
334 self::getList()->$realName = MWFunction::newObj( $className, $rec );
335 }
336 return self::getList()->$realName;
337 } else {
338 return null;
339 }
340 }
341
342 /**
343 * Return categorised listable special pages which are available
344 * for the current user, and everyone.
345 *
346 * @return Array( String => Specialpage )
347 */
348 public static function getUsablePages() {
349 global $wgUser;
350 $pages = array();
351 foreach ( self::getList() as $name => $rec ) {
352 $page = self::getPage( $name );
353 if ( $page->isListed()
354 && (
355 !$page->isRestricted()
356 || $page->userCanExecute( $wgUser )
357 )
358 ) {
359 $pages[$name] = $page;
360 }
361 }
362 return $pages;
363 }
364
365 /**
366 * Return categorised listable special pages for all users
367 *
368 * @return Array( String => Specialpage )
369 */
370 public static function getRegularPages() {
371 $pages = array();
372 foreach ( self::getList() as $name => $rec ) {
373 $page = self::getPage( $name );
374 if ( $page->isListed() && !$page->isRestricted() ) {
375 $pages[$name] = $page;
376 }
377 }
378 return $pages;
379 }
380
381 /**
382 * Return categorised listable special pages which are available
383 * for the current user, but not for everyone
384 *
385 * @return Array( String => Specialpage )
386 */
387 public static function getRestrictedPages() {
388 global $wgUser;
389 $pages = array();
390 foreach ( self::getList() as $name => $rec ) {
391 $page = self::getPage( $name );
392 if (
393 $page->isListed()
394 && $page->isRestricted()
395 && $page->userCanExecute( $wgUser )
396 ) {
397 $pages[$name] = $page;
398 }
399 }
400 return $pages;
401 }
402
403 /**
404 * Execute a special page path.
405 * The path may contain parameters, e.g. Special:Name/Params
406 * Extracts the special page name and call the execute method, passing the parameters
407 *
408 * Returns a title object if the page is redirected, false if there was no such special
409 * page, and true if it was successful.
410 *
411 * @param $title Title object
412 * @param $context IContextSource
413 * @param $including Bool output is being captured for use in {{special:whatever}}
414 *
415 * @return bool
416 */
417 public static function executePath( Title &$title, IContextSource &$context, $including = false ) {
418 wfProfileIn( __METHOD__ );
419
420 // @todo FIXME: Redirects broken due to this call
421 $bits = explode( '/', $title->getDBkey(), 2 );
422 $name = $bits[0];
423 if ( !isset( $bits[1] ) ) { // bug 2087
424 $par = null;
425 } else {
426 $par = $bits[1];
427 }
428 $page = self::getPage( $name );
429 // Nonexistent?
430 if ( !$page ) {
431 $context->getOutput()->setArticleRelated( false );
432 $context->getOutput()->setRobotPolicy( 'noindex,nofollow' );
433 $context->getOutput()->setStatusCode( 404 );
434 $context->getOutput()->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
435 wfProfileOut( __METHOD__ );
436 return false;
437 }
438
439 // Page exists, set the context
440 $page->setContext( $context );
441
442 if ( !$including ) {
443 // Redirect to canonical alias for GET commands
444 // Not for POST, we'd lose the post data, so it's best to just distribute
445 // the request. Such POST requests are possible for old extensions that
446 // generate self-links without being aware that their default name has
447 // changed.
448 if ( $name != $page->getLocalName() && !$context->getRequest()->wasPosted() ) {
449 $query = $context->getRequest()->getQueryValues();
450 unset( $query['title'] );
451 $query = wfArrayToCGI( $query );
452 $title = $page->getTitle( $par );
453 $url = $title->getFullUrl( $query );
454 $context->getOutput()->redirect( $url );
455 wfProfileOut( __METHOD__ );
456 return $title;
457 } else {
458 $context->setTitle( $page->getTitle() );
459 }
460
461 } elseif ( !$page->isIncludable() ) {
462 wfProfileOut( __METHOD__ );
463 return false;
464 }
465
466 $page->including( $including );
467
468 // Execute special page
469 $profName = 'Special:' . $page->getName();
470 wfProfileIn( $profName );
471 $page->execute( $par );
472 wfProfileOut( $profName );
473 wfProfileOut( __METHOD__ );
474 return true;
475 }
476
477 /**
478 * Just like executePath() but will override global variables and execute
479 * the page in "inclusion" mode. Returns true if the excution was successful
480 * or false if there was no such special page, or a title object if it was
481 * a redirect.
482 *
483 * Also saves the current $wgTitle, $wgOut, $wgRequest, $wgUser and $wgLang
484 * variables so that the special page will get the context it'd expect on a
485 * normal request, and then restores them to their previous values after.
486 *
487 * @param $title Title
488 * @param $context IContextSource
489 *
490 * @return String: HTML fragment
491 */
492 static function capturePath( Title $title, IContextSource $context ) {
493 global $wgOut, $wgTitle, $wgRequest, $wgUser, $wgLang;
494
495 // Save current globals
496 $oldTitle = $wgTitle;
497 $oldOut = $wgOut;
498 $oldRequest = $wgRequest;
499 $oldUser = $wgUser;
500 $oldLang = $wgLang;
501
502 // Set the globals to the current context
503 $wgTitle = $title;
504 $wgOut = $context->getOutput();
505 $wgRequest = $context->getRequest();
506 $wgUser = $context->getUser();
507 $wgLang = $context->getLang();
508
509 // The useful part
510 $ret = self::executePath( $title, $context, true );
511
512 // And restore that globals
513 $wgTitle = $oldTitle;
514 $wgOut = $oldOut;
515 $wgRequest = $oldRequest;
516 $wgUser = $oldUser;
517 $wgLang = $oldLang;
518
519 return $ret;
520 }
521
522 /**
523 * Get the local name for a specified canonical name
524 *
525 * @param $name String
526 * @param $subpage String|Bool
527 *
528 * @return String
529 */
530 static function getLocalNameFor( $name, $subpage = false ) {
531 global $wgContLang;
532 $aliases = $wgContLang->getSpecialPageAliases();
533
534 if ( isset( $aliases[$name][0] ) ) {
535 $name = $aliases[$name][0];
536 } else {
537 // Try harder in case someone misspelled the correct casing
538 $found = false;
539 foreach ( $aliases as $n => $values ) {
540 if ( strcasecmp( $name, $n ) === 0 ) {
541 wfWarn( "Found alias defined for $n when searching for " .
542 "special page aliases for $name. Case mismatch?" );
543 $name = $values[0];
544 $found = true;
545 break;
546 }
547 }
548 if ( !$found ) {
549 wfWarn( "Did not find alias for special page '$name'. " .
550 "Perhaps no aliases are defined for it?" );
551 }
552 }
553 if ( $subpage !== false && !is_null( $subpage ) ) {
554 $name = "$name/$subpage";
555 }
556 return $wgContLang->ucfirst( $name );
557 }
558
559 /**
560 * Get a title for a given alias
561 *
562 * @param $alias String
563 *
564 * @return Title or null if there is no such alias
565 */
566 static function getTitleForAlias( $alias ) {
567 $name = self::resolveAlias( $alias );
568 if ( $name ) {
569 return SpecialPage::getTitleFor( $name );
570 } else {
571 return null;
572 }
573 }
574 }