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