Merge "[JobQueue] Added support for approximate FIFO job queues."
[lhc/web/wiklou.git] / includes / SpecialPageFactory.php
1 <?php
2 /**
3 * Factory for handling the special page list and generating SpecialPage objects.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
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 *
28 * To add a special page in an extension, add to $wgSpecialPages either
29 * an object instance or an array containing the name and constructor
30 * parameters. The latter is preferred for performance reasons.
31 *
32 * The object instantiated must be either an instance of SpecialPage or a
33 * sub-class thereof. It must have an execute() method, which sends the HTML
34 * for the special page to $wgOut. The parent class has an execute() method
35 * which distributes the call to the historical global functions. Additionally,
36 * execute() also checks if the user has the necessary access privileges
37 * and bails out if not.
38 *
39 * To add a core special page, use the similar static list in
40 * SpecialPage::$mList. To remove a core static special page at runtime, use
41 * a SpecialPage_initList hook.
42 *
43 * @ingroup SpecialPage
44 * @since 1.17
45 */
46 class SpecialPageFactory {
47
48 /**
49 * List of special page names to the subclass of SpecialPage which handles them.
50 */
51 private static $mList = array(
52 // Maintenance Reports
53 'BrokenRedirects' => 'BrokenRedirectsPage',
54 'Deadendpages' => 'DeadendpagesPage',
55 'DoubleRedirects' => 'DoubleRedirectsPage',
56 'Longpages' => 'LongpagesPage',
57 'Ancientpages' => 'AncientpagesPage',
58 'Lonelypages' => 'LonelypagesPage',
59 'Fewestrevisions' => 'FewestrevisionsPage',
60 'Withoutinterwiki' => 'WithoutinterwikiPage',
61 'Protectedpages' => 'SpecialProtectedpages',
62 'Protectedtitles' => 'SpecialProtectedtitles',
63 'Shortpages' => 'ShortpagesPage',
64 'Uncategorizedcategories' => 'UncategorizedcategoriesPage',
65 'Uncategorizedimages' => 'UncategorizedimagesPage',
66 'Uncategorizedpages' => 'UncategorizedpagesPage',
67 'Uncategorizedtemplates' => 'UncategorizedtemplatesPage',
68 'Unusedcategories' => 'UnusedcategoriesPage',
69 'Unusedimages' => 'UnusedimagesPage',
70 'Unusedtemplates' => 'UnusedtemplatesPage',
71 'Unwatchedpages' => 'UnwatchedpagesPage',
72 'Wantedcategories' => 'WantedcategoriesPage',
73 'Wantedfiles' => 'WantedfilesPage',
74 'Wantedpages' => 'WantedpagesPage',
75 'Wantedtemplates' => 'WantedtemplatesPage',
76
77 // List of pages
78 'Allpages' => 'SpecialAllpages',
79 'Prefixindex' => 'SpecialPrefixindex',
80 'Categories' => 'SpecialCategories',
81 'Disambiguations' => 'DisambiguationsPage',
82 'Listredirects' => 'ListredirectsPage',
83
84 // Login/create account
85 'Userlogin' => 'LoginForm',
86 'CreateAccount' => 'SpecialCreateAccount',
87
88 // Users and rights
89 'Block' => 'SpecialBlock',
90 'Unblock' => 'SpecialUnblock',
91 'BlockList' => 'SpecialBlockList',
92 'ChangePassword' => 'SpecialChangePassword',
93 'PasswordReset' => 'SpecialPasswordReset',
94 'DeletedContributions' => 'DeletedContributionsPage',
95 'Preferences' => 'SpecialPreferences',
96 'Contributions' => 'SpecialContributions',
97 'Listgrouprights' => 'SpecialListGroupRights',
98 'Listusers' => 'SpecialListUsers' ,
99 'Listadmins' => 'SpecialListAdmins',
100 'Listbots' => 'SpecialListBots',
101 'Activeusers' => 'SpecialActiveUsers',
102 'Userrights' => 'UserrightsPage',
103 'EditWatchlist' => 'SpecialEditWatchlist',
104
105 // Recent changes and logs
106 'Newimages' => 'SpecialNewFiles',
107 'Log' => 'SpecialLog',
108 'Watchlist' => 'SpecialWatchlist',
109 'Newpages' => 'SpecialNewpages',
110 'Recentchanges' => 'SpecialRecentchanges',
111 'Recentchangeslinked' => 'SpecialRecentchangeslinked',
112 'Tags' => 'SpecialTags',
113
114 // Media reports and uploads
115 'Listfiles' => 'SpecialListFiles',
116 'Filepath' => 'SpecialFilepath',
117 'MIMEsearch' => 'MIMEsearchPage',
118 'FileDuplicateSearch' => 'FileDuplicateSearchPage',
119 'Upload' => 'SpecialUpload',
120 'UploadStash' => 'SpecialUploadStash',
121
122 // Wiki data and tools
123 'Statistics' => 'SpecialStatistics',
124 'Allmessages' => 'SpecialAllmessages',
125 'Version' => 'SpecialVersion',
126 'Lockdb' => 'SpecialLockdb',
127 'Unlockdb' => 'SpecialUnlockdb',
128
129 // Redirecting special pages
130 'LinkSearch' => 'LinkSearchPage',
131 'Randompage' => 'Randompage',
132 'Randomredirect' => 'SpecialRandomredirect',
133
134 // High use pages
135 'Mostlinkedcategories' => 'MostlinkedCategoriesPage',
136 'Mostimages' => 'MostimagesPage',
137 'Mostinterwikis' => 'MostinterwikisPage',
138 'Mostlinked' => 'MostlinkedPage',
139 'Mostlinkedtemplates' => 'MostlinkedTemplatesPage',
140 'Mostcategories' => 'MostcategoriesPage',
141 'Mostrevisions' => 'MostrevisionsPage',
142
143 // Page tools
144 'ComparePages' => 'SpecialComparePages',
145 'Export' => 'SpecialExport',
146 'Import' => 'SpecialImport',
147 'Undelete' => 'SpecialUndelete',
148 'Whatlinkshere' => 'SpecialWhatlinkshere',
149 'MergeHistory' => 'SpecialMergeHistory',
150
151 // Other
152 'Booksources' => 'SpecialBookSources',
153
154 // Unlisted / redirects
155 'Blankpage' => 'SpecialBlankpage',
156 'Blockme' => 'SpecialBlockme',
157 'Emailuser' => 'SpecialEmailUser',
158 'Movepage' => 'MovePageForm',
159 'Mycontributions' => 'SpecialMycontributions',
160 'Mypage' => 'SpecialMypage',
161 'Mytalk' => 'SpecialMytalk',
162 'Myuploads' => 'SpecialMyuploads',
163 'PermanentLink' => 'SpecialPermanentLink',
164 'Revisiondelete' => 'SpecialRevisionDelete',
165 'Specialpages' => 'SpecialSpecialpages',
166 'Userlogout' => 'SpecialUserlogout',
167 );
168
169 private static $mAliases;
170
171 /**
172 * Initialise the special page list
173 * This must be called before accessing SpecialPage::$mList
174 *
175 * @return array
176 */
177 static function getList() {
178 global $wgSpecialPages;
179 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
180 global $wgEnableEmail, $wgEnableJavaScriptTest;
181
182 if ( !is_object( self::$mList ) ) {
183 wfProfileIn( __METHOD__ );
184
185 if ( !$wgDisableCounters ) {
186 self::$mList['Popularpages'] = 'PopularpagesPage';
187 }
188
189 if ( !$wgDisableInternalSearch ) {
190 self::$mList['Search'] = 'SpecialSearch';
191 }
192
193 if ( $wgEmailAuthentication ) {
194 self::$mList['Confirmemail'] = 'EmailConfirmation';
195 self::$mList['Invalidateemail'] = 'EmailInvalidation';
196 }
197
198 if ( $wgEnableEmail ) {
199 self::$mList['ChangeEmail'] = 'SpecialChangeEmail';
200 }
201
202 if( $wgEnableJavaScriptTest ) {
203 self::$mList['JavaScriptTest'] = 'SpecialJavaScriptTest';
204 }
205
206 // Add extension special pages
207 self::$mList = array_merge( self::$mList, $wgSpecialPages );
208
209 // Run hooks
210 // This hook can be used to remove undesired built-in special pages
211 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
212
213 // Cast to object: func()[$key] doesn't work, but func()->$key does
214 settype( self::$mList, 'object' );
215
216 wfProfileOut( __METHOD__ );
217 }
218 return self::$mList;
219 }
220
221 /**
222 * Initialise and return the list of special page aliases. Returns an object with
223 * properties which can be accessed $obj->pagename - each property is an array of
224 * aliases; the first in the array is the cannonical alias. All registered special
225 * pages are guaranteed to have a property entry, and for that property array to
226 * contain at least one entry (English fallbacks will be added if necessary).
227 * @return Object
228 */
229 static function getAliasList() {
230 if ( !is_object( self::$mAliases ) ) {
231 global $wgContLang;
232 $aliases = $wgContLang->getSpecialPageAliases();
233
234 // Objects are passed by reference by default, need to create a copy
235 $missingPages = clone self::getList();
236
237 self::$mAliases = array();
238 foreach ( $aliases as $realName => $aliasList ) {
239 foreach ( $aliasList as $alias ) {
240 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
241 }
242 unset( $missingPages->$realName );
243 }
244 foreach ( $missingPages as $name => $stuff ) {
245 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
246 }
247
248 // Cast to object: func()[$key] doesn't work, but func()->$key does
249 self::$mAliases = (object)self::$mAliases;
250 }
251 return self::$mAliases;
252 }
253
254 /**
255 * Given a special page name with a possible subpage, return an array
256 * where the first element is the special page name and the second is the
257 * subpage.
258 *
259 * @param $alias String
260 * @return Array( String, String|null ), or array( null, null ) if the page is invalid
261 */
262 public static function resolveAlias( $alias ) {
263 global $wgContLang;
264 $bits = explode( '/', $alias, 2 );
265
266 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
267 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
268 if ( isset( self::getAliasList()->$caseFoldedAlias ) ) {
269 $name = self::getAliasList()->$caseFoldedAlias;
270 } else {
271 return array( null, null );
272 }
273
274 if ( !isset( $bits[1] ) ) { // bug 2087
275 $par = null;
276 } else {
277 $par = $bits[1];
278 }
279
280 return array( $name, $par );
281 }
282
283 /**
284 * Add a page to a certain display group for Special:SpecialPages
285 *
286 * @param $page Mixed: SpecialPage or string
287 * @param $group String
288 */
289 public static function setGroup( $page, $group ) {
290 global $wgSpecialPageGroups;
291 $name = is_object( $page ) ? $page->getName() : $page;
292 $wgSpecialPageGroups[$name] = $group;
293 }
294
295 /**
296 * Get the group that the special page belongs in on Special:SpecialPage
297 *
298 * @param $page SpecialPage
299 * @return String
300 */
301 public static function getGroup( &$page ) {
302 $name = $page->getName();
303
304 global $wgSpecialPageGroups;
305 static $specialPageGroupsCache = array();
306 if ( isset( $specialPageGroupsCache[$name] ) ) {
307 return $specialPageGroupsCache[$name];
308 }
309 $msg = wfMessage( 'specialpages-specialpagegroup-' . strtolower( $name ) );
310 if ( !$msg->isBlank() ) {
311 $group = $msg->text();
312 } else {
313 $group = isset( $wgSpecialPageGroups[$name] )
314 ? $wgSpecialPageGroups[$name]
315 : '-';
316 }
317 if ( $group == '-' ) {
318 $group = 'other';
319 }
320 $specialPageGroupsCache[$name] = $group;
321 return $group;
322 }
323
324 /**
325 * Check if a given name exist as a special page or as a special page alias
326 *
327 * @param $name String: name of a special page
328 * @return Boolean: true if a special page exists with this name
329 */
330 public static function exists( $name ) {
331 list( $title, /*...*/ ) = self::resolveAlias( $name );
332 return property_exists( self::getList(), $title );
333 }
334
335 /**
336 * Find the object with a given name and return it (or NULL)
337 *
338 * @param $name String Special page name, may be localised and/or an alias
339 * @return SpecialPage object or null if the page doesn't exist
340 */
341 public static function getPage( $name ) {
342 list( $realName, /*...*/ ) = self::resolveAlias( $name );
343 if ( property_exists( self::getList(), $realName ) ) {
344 $rec = self::getList()->$realName;
345 if ( is_string( $rec ) ) {
346 $className = $rec;
347 return new $className;
348 } elseif ( is_array( $rec ) ) {
349 // @deprecated, officially since 1.18, unofficially since forever
350 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
351 $className = array_shift( $rec );
352 self::getList()->$realName = MWFunction::newObj( $className, $rec );
353 }
354 return self::getList()->$realName;
355 } else {
356 return null;
357 }
358 }
359
360 /**
361 * Return categorised listable special pages which are available
362 * for the current user, and everyone.
363 *
364 * @param $user User object to check permissions, $wgUser will be used
365 * if not provided
366 * @return Array( String => Specialpage )
367 */
368 public static function getUsablePages( User $user = null ) {
369 $pages = array();
370 if ( $user === null ) {
371 global $wgUser;
372 $user = $wgUser;
373 }
374 foreach ( self::getList() as $name => $rec ) {
375 $page = self::getPage( $name );
376 if ( $page // not null
377 && $page->isListed()
378 && ( !$page->isRestricted() || $page->userCanExecute( $user ) )
379 ) {
380 $pages[$name] = $page;
381 }
382 }
383 return $pages;
384 }
385
386 /**
387 * Return categorised listable special pages for all users
388 *
389 * @return Array( String => Specialpage )
390 */
391 public static function getRegularPages() {
392 $pages = array();
393 foreach ( self::getList() as $name => $rec ) {
394 $page = self::getPage( $name );
395 if ( $page->isListed() && !$page->isRestricted() ) {
396 $pages[$name] = $page;
397 }
398 }
399 return $pages;
400 }
401
402 /**
403 * Return categorised listable special pages which are available
404 * for the current user, but not for everyone
405 *
406 * @return Array( String => Specialpage )
407 */
408 public static function getRestrictedPages() {
409 global $wgUser;
410 $pages = array();
411 foreach ( self::getList() as $name => $rec ) {
412 $page = self::getPage( $name );
413 if (
414 $page->isListed()
415 && $page->isRestricted()
416 && $page->userCanExecute( $wgUser )
417 ) {
418 $pages[$name] = $page;
419 }
420 }
421 return $pages;
422 }
423
424 /**
425 * Execute a special page path.
426 * The path may contain parameters, e.g. Special:Name/Params
427 * Extracts the special page name and call the execute method, passing the parameters
428 *
429 * Returns a title object if the page is redirected, false if there was no such special
430 * page, and true if it was successful.
431 *
432 * @param $title Title object
433 * @param $context IContextSource
434 * @param $including Bool output is being captured for use in {{special:whatever}}
435 *
436 * @return bool
437 */
438 public static function executePath( Title &$title, IContextSource &$context, $including = false ) {
439 wfProfileIn( __METHOD__ );
440
441 // @todo FIXME: Redirects broken due to this call
442 $bits = explode( '/', $title->getDBkey(), 2 );
443 $name = $bits[0];
444 if ( !isset( $bits[1] ) ) { // bug 2087
445 $par = null;
446 } else {
447 $par = $bits[1];
448 }
449 $page = self::getPage( $name );
450 // Nonexistent?
451 if ( !$page ) {
452 $context->getOutput()->setArticleRelated( false );
453 $context->getOutput()->setRobotPolicy( 'noindex,nofollow' );
454
455 global $wgSend404Code;
456 if ( $wgSend404Code ) {
457 $context->getOutput()->setStatusCode( 404 );
458 }
459
460 $context->getOutput()->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
461 wfProfileOut( __METHOD__ );
462 return false;
463 }
464
465 // Page exists, set the context
466 $page->setContext( $context );
467
468 if ( !$including ) {
469 // Redirect to canonical alias for GET commands
470 // Not for POST, we'd lose the post data, so it's best to just distribute
471 // the request. Such POST requests are possible for old extensions that
472 // generate self-links without being aware that their default name has
473 // changed.
474 if ( $name != $page->getLocalName() && !$context->getRequest()->wasPosted() ) {
475 $query = $context->getRequest()->getQueryValues();
476 unset( $query['title'] );
477 $query = wfArrayToCGI( $query );
478 $title = $page->getTitle( $par );
479 $url = $title->getFullUrl( $query );
480 $context->getOutput()->redirect( $url );
481 wfProfileOut( __METHOD__ );
482 return $title;
483 } else {
484 $context->setTitle( $page->getTitle( $par ) );
485 }
486
487 } elseif ( !$page->isIncludable() ) {
488 wfProfileOut( __METHOD__ );
489 return false;
490 }
491
492 $page->including( $including );
493
494 // Execute special page
495 $profName = 'Special:' . $page->getName();
496 wfProfileIn( $profName );
497 $page->run( $par );
498 wfProfileOut( $profName );
499 wfProfileOut( __METHOD__ );
500 return true;
501 }
502
503 /**
504 * Just like executePath() but will override global variables and execute
505 * the page in "inclusion" mode. Returns true if the execution was
506 * successful or false if there was no such special page, or a title object
507 * if it was a redirect.
508 *
509 * Also saves the current $wgTitle, $wgOut, $wgRequest, $wgUser and $wgLang
510 * variables so that the special page will get the context it'd expect on a
511 * normal request, and then restores them to their previous values after.
512 *
513 * @param $title Title
514 * @param $context IContextSource
515 *
516 * @return String: HTML fragment
517 */
518 static function capturePath( Title $title, IContextSource $context ) {
519 global $wgOut, $wgTitle, $wgRequest, $wgUser, $wgLang;
520
521 // Save current globals
522 $oldTitle = $wgTitle;
523 $oldOut = $wgOut;
524 $oldRequest = $wgRequest;
525 $oldUser = $wgUser;
526 $oldLang = $wgLang;
527
528 // Set the globals to the current context
529 $wgTitle = $title;
530 $wgOut = $context->getOutput();
531 $wgRequest = $context->getRequest();
532 $wgUser = $context->getUser();
533 $wgLang = $context->getLanguage();
534
535 // The useful part
536 $ret = self::executePath( $title, $context, true );
537
538 // And restore the old globals
539 $wgTitle = $oldTitle;
540 $wgOut = $oldOut;
541 $wgRequest = $oldRequest;
542 $wgUser = $oldUser;
543 $wgLang = $oldLang;
544
545 return $ret;
546 }
547
548 /**
549 * Get the local name for a specified canonical name
550 *
551 * @param $name String
552 * @param $subpage String|Bool
553 *
554 * @return String
555 */
556 static function getLocalNameFor( $name, $subpage = false ) {
557 global $wgContLang;
558 $aliases = $wgContLang->getSpecialPageAliases();
559
560 if ( isset( $aliases[$name][0] ) ) {
561 $name = $aliases[$name][0];
562 } else {
563 // Try harder in case someone misspelled the correct casing
564 $found = false;
565 foreach ( $aliases as $n => $values ) {
566 if ( strcasecmp( $name, $n ) === 0 ) {
567 wfWarn( "Found alias defined for $n when searching for " .
568 "special page aliases for $name. Case mismatch?" );
569 $name = $values[0];
570 $found = true;
571 break;
572 }
573 }
574 if ( !$found ) {
575 wfWarn( "Did not find alias for special page '$name'. " .
576 "Perhaps no aliases are defined for it?" );
577 }
578 }
579 if ( $subpage !== false && !is_null( $subpage ) ) {
580 $name = "$name/$subpage";
581 }
582 return $wgContLang->ucfirst( $name );
583 }
584
585 /**
586 * Get a title for a given alias
587 *
588 * @param $alias String
589 *
590 * @return Title or null if there is no such alias
591 */
592 static function getTitleForAlias( $alias ) {
593 $name = self::resolveAlias( $alias );
594 if ( $name ) {
595 return SpecialPage::getTitleFor( $name );
596 } else {
597 return null;
598 }
599 }
600 }