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