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