Merge "Move around User::getEditCount() code."
[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 $context = RequestContext::newExtraneousContext( Title::newMainPage() );
375 $context->setUser( $user );
376 foreach ( self::getList() as $name => $rec ) {
377 $page = self::getPage( $name );
378 if ( $page ) { // not null
379 $page->setContext( $context );
380 if ( $page->isListed()
381 && ( !$page->isRestricted() || $page->userCanExecute( $user ) )
382 ) {
383 $pages[$name] = $page;
384 }
385 }
386 }
387 return $pages;
388 }
389
390 /**
391 * Return categorised listable special pages for all users
392 *
393 * @return Array( String => Specialpage )
394 */
395 public static function getRegularPages() {
396 $pages = array();
397 foreach ( self::getList() as $name => $rec ) {
398 $page = self::getPage( $name );
399 if ( $page->isListed() && !$page->isRestricted() ) {
400 $pages[$name] = $page;
401 }
402 }
403 return $pages;
404 }
405
406 /**
407 * Return categorised listable special pages which are available
408 * for the current user, but not for everyone
409 *
410 * @return Array( String => Specialpage )
411 */
412 public static function getRestrictedPages() {
413 global $wgUser;
414 $pages = array();
415 foreach ( self::getList() as $name => $rec ) {
416 $page = self::getPage( $name );
417 if (
418 $page->isListed()
419 && $page->isRestricted()
420 && $page->userCanExecute( $wgUser )
421 ) {
422 $pages[$name] = $page;
423 }
424 }
425 return $pages;
426 }
427
428 /**
429 * Execute a special page path.
430 * The path may contain parameters, e.g. Special:Name/Params
431 * Extracts the special page name and call the execute method, passing the parameters
432 *
433 * Returns a title object if the page is redirected, false if there was no such special
434 * page, and true if it was successful.
435 *
436 * @param $title Title object
437 * @param $context IContextSource
438 * @param $including Bool output is being captured for use in {{special:whatever}}
439 *
440 * @return bool
441 */
442 public static function executePath( Title &$title, IContextSource &$context, $including = false ) {
443 wfProfileIn( __METHOD__ );
444
445 // @todo FIXME: Redirects broken due to this call
446 $bits = explode( '/', $title->getDBkey(), 2 );
447 $name = $bits[0];
448 if ( !isset( $bits[1] ) ) { // bug 2087
449 $par = null;
450 } else {
451 $par = $bits[1];
452 }
453 $page = self::getPage( $name );
454 // Nonexistent?
455 if ( !$page ) {
456 $context->getOutput()->setArticleRelated( false );
457 $context->getOutput()->setRobotPolicy( 'noindex,nofollow' );
458
459 global $wgSend404Code;
460 if ( $wgSend404Code ) {
461 $context->getOutput()->setStatusCode( 404 );
462 }
463
464 $context->getOutput()->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
465 wfProfileOut( __METHOD__ );
466 return false;
467 }
468
469 // Page exists, set the context
470 $page->setContext( $context );
471
472 if ( !$including ) {
473 // Redirect to canonical alias for GET commands
474 // Not for POST, we'd lose the post data, so it's best to just distribute
475 // the request. Such POST requests are possible for old extensions that
476 // generate self-links without being aware that their default name has
477 // changed.
478 if ( $name != $page->getLocalName() && !$context->getRequest()->wasPosted() ) {
479 $query = $context->getRequest()->getQueryValues();
480 unset( $query['title'] );
481 $query = wfArrayToCGI( $query );
482 $title = $page->getTitle( $par );
483 $url = $title->getFullUrl( $query );
484 $context->getOutput()->redirect( $url );
485 wfProfileOut( __METHOD__ );
486 return $title;
487 } else {
488 $context->setTitle( $page->getTitle( $par ) );
489 }
490
491 } elseif ( !$page->isIncludable() ) {
492 wfProfileOut( __METHOD__ );
493 return false;
494 }
495
496 $page->including( $including );
497
498 // Execute special page
499 $profName = 'Special:' . $page->getName();
500 wfProfileIn( $profName );
501 $page->run( $par );
502 wfProfileOut( $profName );
503 wfProfileOut( __METHOD__ );
504 return true;
505 }
506
507 /**
508 * Just like executePath() but will override global variables and execute
509 * the page in "inclusion" mode. Returns true if the execution was
510 * successful or false if there was no such special page, or a title object
511 * if it was a redirect.
512 *
513 * Also saves the current $wgTitle, $wgOut, $wgRequest, $wgUser and $wgLang
514 * variables so that the special page will get the context it'd expect on a
515 * normal request, and then restores them to their previous values after.
516 *
517 * @param $title Title
518 * @param $context IContextSource
519 *
520 * @return String: HTML fragment
521 */
522 static function capturePath( Title $title, IContextSource $context ) {
523 global $wgOut, $wgTitle, $wgRequest, $wgUser, $wgLang;
524
525 // Save current globals
526 $oldTitle = $wgTitle;
527 $oldOut = $wgOut;
528 $oldRequest = $wgRequest;
529 $oldUser = $wgUser;
530 $oldLang = $wgLang;
531
532 // Set the globals to the current context
533 $wgTitle = $title;
534 $wgOut = $context->getOutput();
535 $wgRequest = $context->getRequest();
536 $wgUser = $context->getUser();
537 $wgLang = $context->getLanguage();
538
539 // The useful part
540 $ret = self::executePath( $title, $context, true );
541
542 // And restore the old globals
543 $wgTitle = $oldTitle;
544 $wgOut = $oldOut;
545 $wgRequest = $oldRequest;
546 $wgUser = $oldUser;
547 $wgLang = $oldLang;
548
549 return $ret;
550 }
551
552 /**
553 * Get the local name for a specified canonical name
554 *
555 * @param $name String
556 * @param $subpage String|Bool
557 *
558 * @return String
559 */
560 static function getLocalNameFor( $name, $subpage = false ) {
561 global $wgContLang;
562 $aliases = $wgContLang->getSpecialPageAliases();
563
564 if ( isset( $aliases[$name][0] ) ) {
565 $name = $aliases[$name][0];
566 } else {
567 // Try harder in case someone misspelled the correct casing
568 $found = false;
569 foreach ( $aliases as $n => $values ) {
570 if ( strcasecmp( $name, $n ) === 0 ) {
571 wfWarn( "Found alias defined for $n when searching for " .
572 "special page aliases for $name. Case mismatch?" );
573 $name = $values[0];
574 $found = true;
575 break;
576 }
577 }
578 if ( !$found ) {
579 wfWarn( "Did not find alias for special page '$name'. " .
580 "Perhaps no aliases are defined for it?" );
581 }
582 }
583 if ( $subpage !== false && !is_null( $subpage ) ) {
584 $name = "$name/$subpage";
585 }
586 return $wgContLang->ucfirst( $name );
587 }
588
589 /**
590 * Get a title for a given alias
591 *
592 * @param $alias String
593 *
594 * @return Title or null if there is no such alias
595 */
596 static function getTitleForAlias( $alias ) {
597 $name = self::resolveAlias( $alias );
598 if ( $name ) {
599 return SpecialPage::getTitleFor( $name );
600 } else {
601 return null;
602 }
603 }
604 }