Merge "Remove check for PHP version in install.php"
[lhc/web/wiklou.git] / includes / specialpage / 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 * SpecialPageFactory::$list. 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 * List of special page names to the subclass of SpecialPage which handles them.
49 */
50 private static $list = array(
51 // Maintenance Reports
52 'BrokenRedirects' => 'BrokenRedirectsPage',
53 'Deadendpages' => 'DeadendPagesPage',
54 'DoubleRedirects' => 'DoubleRedirectsPage',
55 'Longpages' => 'LongPagesPage',
56 'Ancientpages' => 'AncientPagesPage',
57 'Lonelypages' => 'LonelyPagesPage',
58 'Fewestrevisions' => 'FewestrevisionsPage',
59 'Withoutinterwiki' => 'WithoutInterwikiPage',
60 'Protectedpages' => 'SpecialProtectedpages',
61 'Protectedtitles' => 'SpecialProtectedtitles',
62 'Shortpages' => 'ShortpagesPage',
63 'Uncategorizedcategories' => 'UncategorizedCategoriesPage',
64 'Uncategorizedimages' => 'UncategorizedImagesPage',
65 'Uncategorizedpages' => 'UncategorizedPagesPage',
66 'Uncategorizedtemplates' => 'UncategorizedTemplatesPage',
67 'Unusedcategories' => 'UnusedCategoriesPage',
68 'Unusedimages' => 'UnusedimagesPage',
69 'Unusedtemplates' => 'UnusedtemplatesPage',
70 'Unwatchedpages' => 'UnwatchedpagesPage',
71 'Wantedcategories' => 'WantedCategoriesPage',
72 'Wantedfiles' => 'WantedFilesPage',
73 'Wantedpages' => 'WantedPagesPage',
74 'Wantedtemplates' => 'WantedTemplatesPage',
75
76 // List of pages
77 'Allpages' => 'SpecialAllpages',
78 'Prefixindex' => 'SpecialPrefixindex',
79 'Categories' => 'SpecialCategories',
80 'Listredirects' => 'ListredirectsPage',
81 'PagesWithProp' => 'SpecialPagesWithProp',
82 'TrackingCategories' => 'SpecialTrackingCategories',
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 'ResetTokens' => 'SpecialResetTokens',
97 'Contributions' => 'SpecialContributions',
98 'Listgrouprights' => 'SpecialListGroupRights',
99 'Listusers' => 'SpecialListUsers',
100 'Listadmins' => 'SpecialListAdmins',
101 'Listbots' => 'SpecialListBots',
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 'ListDuplicatedFiles' => 'ListDuplicatedFilesPage',
122
123 // Data and tools
124 'Statistics' => 'SpecialStatistics',
125 'Allmessages' => 'SpecialAllmessages',
126 'Version' => 'SpecialVersion',
127 'Lockdb' => 'SpecialLockdb',
128 'Unlockdb' => 'SpecialUnlockdb',
129
130 // Redirecting special pages
131 'LinkSearch' => 'LinkSearchPage',
132 'Randompage' => 'RandomPage',
133 'RandomInCategory' => 'SpecialRandomInCategory',
134 'Randomredirect' => 'SpecialRandomredirect',
135
136 // High use pages
137 'Mostlinkedcategories' => 'MostlinkedCategoriesPage',
138 'Mostimages' => 'MostimagesPage',
139 'Mostinterwikis' => 'MostinterwikisPage',
140 'Mostlinked' => 'MostlinkedPage',
141 'Mostlinkedtemplates' => 'MostlinkedTemplatesPage',
142 'Mostcategories' => 'MostcategoriesPage',
143 'Mostrevisions' => 'MostrevisionsPage',
144
145 // Page tools
146 'ComparePages' => 'SpecialComparePages',
147 'Export' => 'SpecialExport',
148 'Import' => 'SpecialImport',
149 'Undelete' => 'SpecialUndelete',
150 'Whatlinkshere' => 'SpecialWhatLinksHere',
151 'MergeHistory' => 'SpecialMergeHistory',
152 'ExpandTemplates' => 'SpecialExpandTemplates',
153
154 // Other
155 'Booksources' => 'SpecialBookSources',
156
157 // Unlisted / redirects
158 'Blankpage' => 'SpecialBlankpage',
159 'Diff' => 'SpecialDiff',
160 'Emailuser' => 'SpecialEmailUser',
161 'Movepage' => 'MovePageForm',
162 'Mycontributions' => 'SpecialMycontributions',
163 'MyLanguage' => 'SpecialMyLanguage',
164 'Mypage' => 'SpecialMypage',
165 'Mytalk' => 'SpecialMytalk',
166 'Myuploads' => 'SpecialMyuploads',
167 'AllMyUploads' => 'SpecialAllMyUploads',
168 'PermanentLink' => 'SpecialPermanentLink',
169 'Redirect' => 'SpecialRedirect',
170 'Revisiondelete' => 'SpecialRevisionDelete',
171 'RunJobs' => 'SpecialRunJobs',
172 'Specialpages' => 'SpecialSpecialpages',
173 'Userlogout' => 'SpecialUserlogout',
174 );
175
176 private static $aliases;
177
178 /**
179 * Get the special page list
180 *
181 * @return array
182 */
183 static function getList() {
184 global $wgSpecialPages;
185 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
186 global $wgEnableEmail, $wgEnableJavaScriptTest;
187 global $wgPageLanguageUseDB;
188
189 if ( !is_object( self::$list ) ) {
190 wfProfileIn( __METHOD__ );
191
192 if ( !$wgDisableCounters ) {
193 self::$list['Popularpages'] = 'PopularPagesPage';
194 }
195
196 if ( !$wgDisableInternalSearch ) {
197 self::$list['Search'] = 'SpecialSearch';
198 }
199
200 if ( $wgEmailAuthentication ) {
201 self::$list['Confirmemail'] = 'EmailConfirmation';
202 self::$list['Invalidateemail'] = 'EmailInvalidation';
203 }
204
205 if ( $wgEnableEmail ) {
206 self::$list['ChangeEmail'] = 'SpecialChangeEmail';
207 }
208
209 if ( $wgEnableJavaScriptTest ) {
210 self::$list['JavaScriptTest'] = 'SpecialJavaScriptTest';
211 }
212
213 if ( $wgPageLanguageUseDB ) {
214 self::$list['PageLanguage'] = 'SpecialPageLanguage';
215 }
216
217 self::$list['Activeusers'] = 'SpecialActiveUsers';
218
219 // Add extension special pages
220 self::$list = array_merge( self::$list, $wgSpecialPages );
221
222 // Run hooks
223 // This hook can be used to remove undesired built-in special pages
224 wfRunHooks( 'SpecialPage_initList', array( &self::$list ) );
225
226 // Cast to object: func()[$key] doesn't work, but func()->$key does
227 settype( self::$list, 'object' );
228
229 wfProfileOut( __METHOD__ );
230 }
231
232 return self::$list;
233 }
234
235 /**
236 * Initialise and return the list of special page aliases. Returns an object with
237 * properties which can be accessed $obj->pagename - each property is an array of
238 * aliases; the first in the array is the canonical alias. All registered special
239 * pages are guaranteed to have a property entry, and for that property array to
240 * contain at least one entry (English fallbacks will be added if necessary).
241 * @return object
242 */
243 static function getAliasList() {
244 if ( !is_object( self::$aliases ) ) {
245 global $wgContLang;
246 $aliases = $wgContLang->getSpecialPageAliases();
247
248 // Objects are passed by reference by default, need to create a copy
249 $missingPages = clone self::getList();
250
251 self::$aliases = array();
252 // Check for $aliases being an array since Language::getSpecialPageAliases can return null
253 if ( is_array( $aliases ) ) {
254 foreach ( $aliases as $realName => $aliasList ) {
255 foreach ( $aliasList as $alias ) {
256 self::$aliases[$wgContLang->caseFold( $alias )] = $realName;
257 }
258 unset( $missingPages->$realName );
259 }
260 }
261 foreach ( $missingPages as $name => $stuff ) {
262 self::$aliases[$wgContLang->caseFold( $name )] = $name;
263 }
264
265 // Cast to object: func()[$key] doesn't work, but func()->$key does
266 self::$aliases = (object)self::$aliases;
267 }
268
269 return self::$aliases;
270 }
271
272 /**
273 * Given a special page name with a possible subpage, return an array
274 * where the first element is the special page name and the second is the
275 * subpage.
276 *
277 * @param string $alias
278 * @return array Array( String, String|null ), or array( null, null ) if the page is invalid
279 */
280 public static function resolveAlias( $alias ) {
281 global $wgContLang;
282 $bits = explode( '/', $alias, 2 );
283
284 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
285 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
286 if ( isset( self::getAliasList()->$caseFoldedAlias ) ) {
287 $name = self::getAliasList()->$caseFoldedAlias;
288 } else {
289 return array( null, null );
290 }
291
292 if ( !isset( $bits[1] ) ) { // bug 2087
293 $par = null;
294 } else {
295 $par = $bits[1];
296 }
297
298 return array( $name, $par );
299 }
300
301 /**
302 * Add a page to a certain display group for Special:SpecialPages
303 *
304 * @param SpecialPage|string $page
305 * @param string $group
306 * @deprecated since 1.21 Override SpecialPage::getGroupName
307 */
308 public static function setGroup( $page, $group ) {
309 wfDeprecated( __METHOD__, '1.21' );
310
311 global $wgSpecialPageGroups;
312 $name = is_object( $page ) ? $page->getName() : $page;
313 $wgSpecialPageGroups[$name] = $group;
314 }
315
316 /**
317 * Get the group that the special page belongs in on Special:SpecialPage
318 *
319 * @param SpecialPage $page
320 * @return string
321 * @deprecated since 1.21 Use SpecialPage::getFinalGroupName
322 */
323 public static function getGroup( &$page ) {
324 wfDeprecated( __METHOD__, '1.21' );
325
326 return $page->getFinalGroupName();
327 }
328
329 /**
330 * Check if a given name exist as a special page or as a special page alias
331 *
332 * @param string $name Name of a special page
333 * @return bool True if a special page exists with this name
334 */
335 public static function exists( $name ) {
336 list( $title, /*...*/ ) = self::resolveAlias( $name );
337
338 return property_exists( self::getList(), $title );
339 }
340
341 /**
342 * Find the object with a given name and return it (or NULL)
343 *
344 * @param string $name Special page name, may be localised and/or an alias
345 * @return SpecialPage|null SpecialPage object or null if the page doesn't exist
346 */
347 public static function getPage( $name ) {
348 list( $realName, /*...*/ ) = self::resolveAlias( $name );
349 if ( property_exists( self::getList(), $realName ) ) {
350 $rec = self::getList()->$realName;
351 if ( is_string( $rec ) ) {
352 $className = $rec;
353
354 return new $className;
355 } elseif ( is_array( $rec ) ) {
356 $className = array_shift( $rec );
357 // @deprecated, officially since 1.18, unofficially since forever
358 wfDeprecated( "Array syntax for \$wgSpecialPages is deprecated ($className), " .
359 "define a subclass of SpecialPage instead.", '1.18' );
360 self::getList()->$realName = MWFunction::newObj( $className, $rec );
361 }
362
363 return self::getList()->$realName;
364 } else {
365 return null;
366 }
367 }
368
369 /**
370 * Return categorised listable special pages which are available
371 * for the current user, and everyone.
372 *
373 * @param User $user User object to check permissions, $wgUser will be used
374 * if not provided
375 * @return array ( string => Specialpage )
376 */
377 public static function getUsablePages( User $user = null ) {
378 $pages = array();
379 if ( $user === null ) {
380 global $wgUser;
381 $user = $wgUser;
382 }
383 foreach ( self::getList() as $name => $rec ) {
384 $page = self::getPage( $name );
385 if ( $page ) { // not null
386 $page->setContext( RequestContext::getMain() );
387 if ( $page->isListed()
388 && ( !$page->isRestricted() || $page->userCanExecute( $user ) )
389 ) {
390 $pages[$name] = $page;
391 }
392 }
393 }
394
395 return $pages;
396 }
397
398 /**
399 * Return categorised listable special pages for all users
400 *
401 * @return array ( string => Specialpage )
402 */
403 public static function getRegularPages() {
404 $pages = array();
405 foreach ( self::getList() as $name => $rec ) {
406 $page = self::getPage( $name );
407 if ( $page->isListed() && !$page->isRestricted() ) {
408 $pages[$name] = $page;
409 }
410 }
411
412 return $pages;
413 }
414
415 /**
416 * Return categorised listable special pages which are available
417 * for the current user, but not for everyone
418 *
419 * @param User|null $user User object to use or null for $wgUser
420 * @return array ( string => Specialpage )
421 */
422 public static function getRestrictedPages( User $user = null ) {
423 $pages = array();
424 if ( $user === null ) {
425 global $wgUser;
426 $user = $wgUser;
427 }
428 foreach ( self::getList() as $name => $rec ) {
429 $page = self::getPage( $name );
430 if (
431 $page->isListed()
432 && $page->isRestricted()
433 && $page->userCanExecute( $user )
434 ) {
435 $pages[$name] = $page;
436 }
437 }
438
439 return $pages;
440 }
441
442 /**
443 * Execute a special page path.
444 * The path may contain parameters, e.g. Special:Name/Params
445 * Extracts the special page name and call the execute method, passing the parameters
446 *
447 * Returns a title object if the page is redirected, false if there was no such special
448 * page, and true if it was successful.
449 *
450 * @param Title $title
451 * @param IContextSource $context
452 * @param bool $including Bool output is being captured for use in {{special:whatever}}
453 *
454 * @return bool
455 */
456 public static function executePath( Title &$title, IContextSource &$context, $including = false ) {
457 wfProfileIn( __METHOD__ );
458
459 // @todo FIXME: Redirects broken due to this call
460 $bits = explode( '/', $title->getDBkey(), 2 );
461 $name = $bits[0];
462 if ( !isset( $bits[1] ) ) { // bug 2087
463 $par = null;
464 } else {
465 $par = $bits[1];
466 }
467 $page = self::getPage( $name );
468 // Nonexistent?
469 if ( !$page ) {
470 $context->getOutput()->setArticleRelated( false );
471 $context->getOutput()->setRobotPolicy( 'noindex,nofollow' );
472
473 global $wgSend404Code;
474 if ( $wgSend404Code ) {
475 $context->getOutput()->setStatusCode( 404 );
476 }
477
478 $context->getOutput()->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
479 wfProfileOut( __METHOD__ );
480
481 return false;
482 }
483
484 // Page exists, set the context
485 $page->setContext( $context );
486
487 if ( !$including ) {
488 // Redirect to canonical alias for GET commands
489 // Not for POST, we'd lose the post data, so it's best to just distribute
490 // the request. Such POST requests are possible for old extensions that
491 // generate self-links without being aware that their default name has
492 // changed.
493 if ( $name != $page->getLocalName() && !$context->getRequest()->wasPosted() ) {
494 $query = $context->getRequest()->getQueryValues();
495 unset( $query['title'] );
496 $title = $page->getPageTitle( $par );
497 $url = $title->getFullURL( $query );
498 $context->getOutput()->redirect( $url );
499 wfProfileOut( __METHOD__ );
500
501 return $title;
502 } else {
503 $context->setTitle( $page->getPageTitle( $par ) );
504 }
505 } elseif ( !$page->isIncludable() ) {
506 wfProfileOut( __METHOD__ );
507
508 return false;
509 }
510
511 $page->including( $including );
512
513 // Execute special page
514 $profName = 'Special:' . $page->getName();
515 wfProfileIn( $profName );
516 $page->run( $par );
517 wfProfileOut( $profName );
518 wfProfileOut( __METHOD__ );
519
520 return true;
521 }
522
523 /**
524 * Just like executePath() but will override global variables and execute
525 * the page in "inclusion" mode. Returns true if the execution was
526 * successful or false if there was no such special page, or a title object
527 * if it was a redirect.
528 *
529 * Also saves the current $wgTitle, $wgOut, $wgRequest, $wgUser and $wgLang
530 * variables so that the special page will get the context it'd expect on a
531 * normal request, and then restores them to their previous values after.
532 *
533 * @param Title $title
534 * @param IContextSource $context
535 * @return string HTML fragment
536 */
537 static function capturePath( Title $title, IContextSource $context ) {
538 global $wgOut, $wgTitle, $wgRequest, $wgUser, $wgLang;
539
540 // Save current globals
541 $oldTitle = $wgTitle;
542 $oldOut = $wgOut;
543 $oldRequest = $wgRequest;
544 $oldUser = $wgUser;
545 $oldLang = $wgLang;
546
547 // Set the globals to the current context
548 $wgTitle = $title;
549 $wgOut = $context->getOutput();
550 $wgRequest = $context->getRequest();
551 $wgUser = $context->getUser();
552 $wgLang = $context->getLanguage();
553
554 // The useful part
555 $ret = self::executePath( $title, $context, true );
556
557 // And restore the old globals
558 $wgTitle = $oldTitle;
559 $wgOut = $oldOut;
560 $wgRequest = $oldRequest;
561 $wgUser = $oldUser;
562 $wgLang = $oldLang;
563
564 return $ret;
565 }
566
567 /**
568 * Get the local name for a specified canonical name
569 *
570 * @param string $name
571 * @param string|bool $subpage
572 * @return string
573 */
574 static function getLocalNameFor( $name, $subpage = false ) {
575 global $wgContLang;
576 $aliases = $wgContLang->getSpecialPageAliases();
577
578 if ( isset( $aliases[$name][0] ) ) {
579 $name = $aliases[$name][0];
580 } else {
581 // Try harder in case someone misspelled the correct casing
582 $found = false;
583 // Check for $aliases being an array since Language::getSpecialPageAliases can return null
584 if ( is_array( $aliases ) ) {
585 foreach ( $aliases as $n => $values ) {
586 if ( strcasecmp( $name, $n ) === 0 ) {
587 wfWarn( "Found alias defined for $n when searching for " .
588 "special page aliases for $name. Case mismatch?" );
589 $name = $values[0];
590 $found = true;
591 break;
592 }
593 }
594 }
595 if ( !$found ) {
596 wfWarn( "Did not find alias for special page '$name'. " .
597 "Perhaps no aliases are defined for it?" );
598 }
599 }
600 if ( $subpage !== false && !is_null( $subpage ) ) {
601 $name = "$name/$subpage";
602 }
603
604 return $wgContLang->ucfirst( $name );
605 }
606
607 /**
608 * Get a title for a given alias
609 *
610 * @param string $alias
611 * @return Title|null Title or null if there is no such alias
612 */
613 static function getTitleForAlias( $alias ) {
614 $name = self::resolveAlias( $alias );
615 if ( $name ) {
616 return SpecialPage::getTitleFor( $name );
617 } else {
618 return null;
619 }
620 }
621 }