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