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