Follow-up r85226, r85227: add @since annotations
[lhc/web/wiklou.git] / includes / SpecialPage.php
1 <?php
2 /**
3 * SpecialPage: handling special pages and lists thereof.
4 *
5 * To add a special page in an extension, add to $wgSpecialPages either
6 * an object instance or an array containing the name and constructor
7 * parameters. The latter is preferred for performance reasons.
8 *
9 * The object instantiated must be either an instance of SpecialPage or a
10 * sub-class thereof. It must have an execute() method, which sends the HTML
11 * for the special page to $wgOut. The parent class has an execute() method
12 * which distributes the call to the historical global functions. Additionally,
13 * execute() also checks if the user has the necessary access privileges
14 * and bails out if not.
15 *
16 * To add a core special page, use the similar static list in
17 * SpecialPage::$mList. To remove a core static special page at runtime, use
18 * a SpecialPage_initList hook.
19 *
20 * @file
21 * @ingroup SpecialPage
22 * @defgroup SpecialPage SpecialPage
23 */
24
25 /**
26 * Parent special page class, also static functions for handling the special
27 * page list.
28 * @ingroup SpecialPage
29 */
30 class SpecialPage {
31 /**#@+
32 * @access private
33 */
34 /**
35 * The canonical name of this special page
36 * Also used for the default <h1> heading, @see getDescription()
37 */
38 var $mName;
39 /**
40 * The local name of this special page
41 */
42 var $mLocalName;
43 /**
44 * Minimum user level required to access this page, or "" for anyone.
45 * Also used to categorise the pages in Special:Specialpages
46 */
47 var $mRestriction;
48 /**
49 * Listed in Special:Specialpages?
50 */
51 var $mListed;
52 /**
53 * Function name called by the default execute()
54 */
55 var $mFunction;
56 /**
57 * File which needs to be included before the function above can be called
58 */
59 var $mFile;
60 /**
61 * Whether or not this special page is being included from an article
62 */
63 var $mIncluding;
64 /**
65 * Whether the special page can be included in an article
66 */
67 var $mIncludable;
68 /**
69 * Query parameters that can be passed through redirects
70 */
71 var $mAllowedRedirectParams = array();
72 /**
73 * Query parameteres added by redirects
74 */
75 var $mAddedRedirectParams = array();
76 /**
77 * Current request
78 * @var WebRequest
79 */
80 protected $mRequest;
81 /**
82 * Current output page
83 * @var OutputPage
84 */
85 protected $mOutput;
86 /**
87 * Full title including $par
88 * @var Title
89 */
90 protected $mFullTitle;
91
92 /**
93 * List of special pages, followed by parameters.
94 * If the only parameter is a string, that is the page name.
95 * Otherwise, it is an array. The format is one of:
96 ** array( 'SpecialPage', name, right )
97 ** array( 'IncludableSpecialPage', name, right, listed? )
98 ** array( 'UnlistedSpecialPage', name, right )
99 ** array( 'SpecialRedirectToSpecial', name, page to redirect to, special page param, ... )
100 */
101 static public $mList = array(
102 # Maintenance Reports
103 'BrokenRedirects' => 'BrokenRedirectsPage',
104 'Deadendpages' => 'DeadendpagesPage',
105 'DoubleRedirects' => 'DoubleRedirectsPage',
106 'Longpages' => 'LongpagesPage',
107 'Ancientpages' => 'AncientpagesPage',
108 'Lonelypages' => 'LonelypagesPage',
109 'Fewestrevisions' => 'FewestrevisionsPage',
110 'Withoutinterwiki' => 'WithoutinterwikiPage',
111 'Protectedpages' => 'SpecialProtectedpages',
112 'Protectedtitles' => 'SpecialProtectedtitles',
113 'Shortpages' => 'ShortpagesPage',
114 'Uncategorizedcategories' => 'UncategorizedcategoriesPage',
115 'Uncategorizedimages' => 'UncategorizedimagesPage',
116 'Uncategorizedpages' => 'UncategorizedpagesPage',
117 'Uncategorizedtemplates' => 'UncategorizedtemplatesPage',
118 'Unusedcategories' => 'UnusedcategoriesPage',
119 'Unusedimages' => 'UnusedimagesPage',
120 'Unusedtemplates' => 'UnusedtemplatesPage',
121 'Unwatchedpages' => 'UnwatchedpagesPage',
122 'Wantedcategories' => 'WantedcategoriesPage',
123 'Wantedfiles' => 'WantedfilesPage',
124 'Wantedpages' => 'WantedpagesPage',
125 'Wantedtemplates' => 'WantedtemplatesPage',
126
127 # List of pages
128 'Allpages' => 'SpecialAllpages',
129 'Prefixindex' => 'SpecialPrefixindex',
130 'Categories' => 'SpecialCategories',
131 'Disambiguations' => 'DisambiguationsPage',
132 'Listredirects' => 'ListredirectsPage',
133
134 # Login/create account
135 'Userlogin' => 'LoginForm',
136 'CreateAccount' => array( 'SpecialRedirectToSpecial', 'CreateAccount', 'Userlogin', 'signup', array( 'uselang' ) ),
137
138 # Users and rights
139 'Block' => 'SpecialBlock',
140 'Unblock' => 'SpecialUnblock',
141 'BlockList' => 'SpecialBlockList',
142 'Resetpass' => 'SpecialResetpass',
143 'DeletedContributions' => 'DeletedContributionsPage',
144 'Preferences' => 'SpecialPreferences',
145 'Contributions' => 'SpecialContributions',
146 'Listgrouprights' => 'SpecialListGroupRights',
147 'Listusers' => 'SpecialListusers',
148 'Listadmins' => array( 'SpecialRedirectToSpecial', 'Listadmins', 'Listusers', 'sysop' ),
149 'Listbots' => array( 'SpecialRedirectToSpecial', 'Listbots', 'Listusers', 'bot' ),
150 'Activeusers' => 'SpecialActiveUsers',
151 'Userrights' => 'UserrightsPage',
152 'DisableAccount' => 'SpecialDisableAccount',
153 'EditWatchlist' => 'SpecialEditWatchlist',
154
155 # Recent changes and logs
156 'Newimages' => 'SpecialNewFiles',
157 'Log' => 'SpecialLog',
158 'Watchlist' => 'SpecialWatchlist',
159 'Newpages' => 'SpecialNewpages',
160 'Recentchanges' => 'SpecialRecentchanges',
161 'Recentchangeslinked' => 'SpecialRecentchangeslinked',
162 'Tags' => 'SpecialTags',
163
164 # Media reports and uploads
165 'Listfiles' => 'SpecialListFiles',
166 'Filepath' => 'SpecialFilepath',
167 'MIMEsearch' => 'MIMEsearchPage',
168 'FileDuplicateSearch' => 'FileDuplicateSearchPage',
169 'Upload' => 'SpecialUpload',
170 'UploadStash' => 'SpecialUploadStash',
171
172 # Wiki data and tools
173 'Statistics' => 'SpecialStatistics',
174 'Allmessages' => 'SpecialAllmessages',
175 'Version' => 'SpecialVersion',
176 'Lockdb' => 'SpecialLockdb',
177 'Unlockdb' => 'SpecialUnlockdb',
178
179 # Redirecting special pages
180 'LinkSearch' => 'LinkSearchPage',
181 'Randompage' => 'Randompage',
182 'Randomredirect' => 'SpecialRandomredirect',
183
184 # High use pages
185 'Mostlinkedcategories' => 'MostlinkedCategoriesPage',
186 'Mostimages' => 'MostimagesPage',
187 'Mostlinked' => 'MostlinkedPage',
188 'Mostlinkedtemplates' => 'MostlinkedTemplatesPage',
189 'Mostcategories' => 'MostcategoriesPage',
190 'Mostrevisions' => 'MostrevisionsPage',
191
192 # Page tools
193 'ComparePages' => 'SpecialComparePages',
194 'Export' => 'SpecialExport',
195 'Import' => 'SpecialImport',
196 'Undelete' => 'SpecialUndelete',
197 'Whatlinkshere' => 'SpecialWhatlinkshere',
198 'MergeHistory' => 'SpecialMergeHistory',
199
200 # Other
201 'Booksources' => 'SpecialBookSources',
202
203 # Unlisted / redirects
204 'Blankpage' => 'SpecialBlankpage',
205 'Blockme' => 'SpecialBlockme',
206 'Emailuser' => 'SpecialEmailUser',
207 'Movepage' => 'MovePageForm',
208 'Mycontributions' => 'SpecialMycontributions',
209 'Mypage' => 'SpecialMypage',
210 'Mytalk' => 'SpecialMytalk',
211 'Myuploads' => 'SpecialMyuploads',
212 'PermanentLink' => 'SpecialPermanentLink',
213 'Revisiondelete' => 'SpecialRevisionDelete',
214 'RevisionMove' => 'SpecialRevisionMove',
215 'Specialpages' => 'SpecialSpecialpages',
216 'Userlogout' => 'SpecialUserlogout',
217 );
218
219 static public $mAliases;
220 static public $mListInitialised = false;
221
222 /**#@-*/
223
224 /**
225 * Initialise the special page list
226 * This must be called before accessing SpecialPage::$mList
227 */
228 static function initList() {
229 global $wgSpecialPages;
230 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
231
232 if ( self::$mListInitialised ) {
233 return;
234 }
235 wfProfileIn( __METHOD__ );
236
237 # Better to set this now, to avoid infinite recursion in carelessly written hooks
238 self::$mListInitialised = true;
239
240 if( !$wgDisableCounters ) {
241 self::$mList['Popularpages'] = 'PopularpagesPage';
242 }
243
244 if( !$wgDisableInternalSearch ) {
245 self::$mList['Search'] = 'SpecialSearch';
246 }
247
248 if( $wgEmailAuthentication ) {
249 self::$mList['Confirmemail'] = 'EmailConfirmation';
250 self::$mList['Invalidateemail'] = 'EmailInvalidation';
251 }
252
253 # Add extension special pages
254 self::$mList = array_merge( self::$mList, $wgSpecialPages );
255
256 # Run hooks
257 # This hook can be used to remove undesired built-in special pages
258 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
259 wfProfileOut( __METHOD__ );
260 }
261
262 static function initAliasList() {
263 if ( !is_null( self::$mAliases ) ) {
264 return;
265 }
266
267 global $wgContLang;
268 $aliases = $wgContLang->getSpecialPageAliases();
269 $missingPages = self::$mList;
270 self::$mAliases = array();
271 foreach ( $aliases as $realName => $aliasList ) {
272 foreach ( $aliasList as $alias ) {
273 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
274 }
275 unset( $missingPages[$realName] );
276 }
277 foreach ( $missingPages as $name => $stuff ) {
278 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
279 }
280 }
281
282 /**
283 * Given a special page alias, return the special page name.
284 * Returns false if there is no such alias.
285 *
286 * @param $alias String
287 * @return String or false
288 */
289 static function resolveAlias( $alias ) {
290 global $wgContLang;
291
292 if ( !self::$mListInitialised ) self::initList();
293 if ( is_null( self::$mAliases ) ) self::initAliasList();
294 $caseFoldedAlias = $wgContLang->caseFold( $alias );
295 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
296 if ( isset( self::$mAliases[$caseFoldedAlias] ) ) {
297 return self::$mAliases[$caseFoldedAlias];
298 } else {
299 return false;
300 }
301 }
302
303 /**
304 * Given a special page name with a possible subpage, return an array
305 * where the first element is the special page name and the second is the
306 * subpage.
307 *
308 * @param $alias String
309 * @return Array
310 */
311 static function resolveAliasWithSubpage( $alias ) {
312 $bits = explode( '/', $alias, 2 );
313 $name = self::resolveAlias( $bits[0] );
314 if( !isset( $bits[1] ) ) { // bug 2087
315 $par = null;
316 } else {
317 $par = $bits[1];
318 }
319 return array( $name, $par );
320 }
321
322 /**
323 * Add a page to the list of valid special pages. This used to be the preferred
324 * method for adding special pages in extensions. It's now suggested that you add
325 * an associative record to $wgSpecialPages. This avoids autoloading SpecialPage.
326 *
327 * @param $page SpecialPage
328 * Deprecated in 1.7, warnings in 1.17, might be removed in 1.20
329 */
330 static function addPage( &$page ) {
331 wfDeprecated( __METHOD__ );
332 if ( !self::$mListInitialised ) {
333 self::initList();
334 }
335 self::$mList[$page->mName] = $page;
336 }
337
338 /**
339 * Add a page to a certain display group for Special:SpecialPages
340 *
341 * @param $page Mixed: SpecialPage or string
342 * @param $group String
343 */
344 static function setGroup( $page, $group ) {
345 global $wgSpecialPageGroups;
346 $name = is_object($page) ? $page->mName : $page;
347 $wgSpecialPageGroups[$name] = $group;
348 }
349
350 /**
351 * Add a page to a certain display group for Special:SpecialPages
352 *
353 * @param $page SpecialPage
354 */
355 static function getGroup( &$page ) {
356 global $wgSpecialPageGroups;
357 static $specialPageGroupsCache = array();
358 if( isset($specialPageGroupsCache[$page->mName]) ) {
359 return $specialPageGroupsCache[$page->mName];
360 }
361 $msg = wfMessage('specialpages-specialpagegroup-'.strtolower($page->mName));
362 if ( !$msg->isBlank() ) {
363 $group = $msg->text();
364 } else {
365 $group = isset($wgSpecialPageGroups[$page->mName])
366 ? $wgSpecialPageGroups[$page->mName]
367 : '-';
368 }
369 if( $group == '-' ) $group = 'other';
370 $specialPageGroupsCache[$page->mName] = $group;
371 return $group;
372 }
373
374 /**
375 * Remove a special page from the list
376 * Formerly used to disable expensive or dangerous special pages. The
377 * preferred method is now to add a SpecialPage_initList hook.
378 */
379 static function removePage( $name ) {
380 if ( !self::$mListInitialised ) {
381 self::initList();
382 }
383 unset( self::$mList[$name] );
384 }
385
386 /**
387 * Check if a given name exist as a special page or as a special page alias
388 *
389 * @param $name String: name of a special page
390 * @return Boolean: true if a special page exists with this name
391 */
392 static function exists( $name ) {
393 global $wgContLang;
394 if ( !self::$mListInitialised ) {
395 self::initList();
396 }
397 if( !self::$mAliases ) {
398 self::initAliasList();
399 }
400
401 # Remove special pages inline parameters:
402 $bits = explode( '/', $name );
403 $name = $wgContLang->caseFold($bits[0]);
404
405 return
406 array_key_exists( $name, self::$mList )
407 or array_key_exists( $name, self::$mAliases )
408 ;
409 }
410
411 /**
412 * Find the object with a given name and return it (or NULL)
413 *
414 * @param $name String
415 * @return SpecialPage object or null if the page doesn't exist
416 */
417 static function getPage( $name ) {
418 if ( !self::$mListInitialised ) {
419 self::initList();
420 }
421 if ( array_key_exists( $name, self::$mList ) ) {
422 $rec = self::$mList[$name];
423 if ( is_string( $rec ) ) {
424 $className = $rec;
425 self::$mList[$name] = new $className;
426 } elseif ( is_array( $rec ) ) {
427 $className = array_shift( $rec );
428 self::$mList[$name] = MWFunction::newObj( $className, $rec );
429 }
430 return self::$mList[$name];
431 } else {
432 return null;
433 }
434 }
435
436 /**
437 * Get a special page with a given localised name, or NULL if there
438 * is no such special page.
439 *
440 * @return SpecialPage object or null if the page doesn't exist
441 */
442 static function getPageByAlias( $alias ) {
443 $realName = self::resolveAlias( $alias );
444 if ( $realName ) {
445 return self::getPage( $realName );
446 } else {
447 return null;
448 }
449 }
450
451 /**
452 * Return categorised listable special pages which are available
453 * for the current user, and everyone.
454 *
455 * @return Associative array mapping page's name to its SpecialPage object
456 */
457 static function getUsablePages() {
458 global $wgUser;
459 if ( !self::$mListInitialised ) {
460 self::initList();
461 }
462 $pages = array();
463
464 foreach ( self::$mList as $name => $rec ) {
465 $page = self::getPage( $name );
466 if ( $page->isListed()
467 && (
468 !$page->isRestricted()
469 || $page->userCanExecute( $wgUser )
470 )
471 ) {
472 $pages[$name] = $page;
473 }
474 }
475 return $pages;
476 }
477
478 /**
479 * Return categorised listable special pages for all users
480 *
481 * @return Associative array mapping page's name to its SpecialPage object
482 */
483 static function getRegularPages() {
484 if ( !self::$mListInitialised ) {
485 self::initList();
486 }
487 $pages = array();
488
489 foreach ( self::$mList as $name => $rec ) {
490 $page = self::getPage( $name );
491 if ( $page->isListed() && !$page->isRestricted() ) {
492 $pages[$name] = $page;
493 }
494 }
495 return $pages;
496 }
497
498 /**
499 * Return categorised listable special pages which are available
500 * for the current user, but not for everyone
501 *
502 * @return Associative array mapping page's name to its SpecialPage object
503 */
504 static function getRestrictedPages() {
505 global $wgUser;
506 if( !self::$mListInitialised ) {
507 self::initList();
508 }
509 $pages = array();
510
511 foreach( self::$mList as $name => $rec ) {
512 $page = self::getPage( $name );
513 if(
514 $page->isListed()
515 && $page->isRestricted()
516 && $page->userCanExecute( $wgUser )
517 ) {
518 $pages[$name] = $page;
519 }
520 }
521 return $pages;
522 }
523
524 /**
525 * Execute a special page path.
526 * The path may contain parameters, e.g. Special:Name/Params
527 * Extracts the special page name and call the execute method, passing the parameters
528 *
529 * Returns a title object if the page is redirected, false if there was no such special
530 * page, and true if it was successful.
531 *
532 * @param $title a title object
533 * @param $including output is being captured for use in {{special:whatever}}
534 */
535 static function executePath( &$title, $including = false ) {
536 global $wgOut, $wgTitle, $wgRequest;
537 wfProfileIn( __METHOD__ );
538
539 # FIXME: redirects broken due to this call
540 $bits = explode( '/', $title->getDBkey(), 2 );
541 $name = $bits[0];
542 if( !isset( $bits[1] ) ) { // bug 2087
543 $par = null;
544 } else {
545 $par = $bits[1];
546 }
547 $page = SpecialPage::getPageByAlias( $name );
548 # Nonexistent?
549 if ( !$page ) {
550 $wgOut->setArticleRelated( false );
551 $wgOut->setRobotPolicy( 'noindex,nofollow' );
552 $wgOut->setStatusCode( 404 );
553 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
554 wfProfileOut( __METHOD__ );
555 return false;
556 }
557
558 # Page exists, set the context
559 $page->setContext( $wgRequest, $wgOut );
560
561 # Check for redirect
562 if ( !$including ) {
563 $redirect = $page->getRedirect( $par );
564 $query = $page->getRedirectQuery();
565 if ( $redirect instanceof Title ) {
566 $url = $redirect->getFullUrl( $query );
567 $wgOut->redirect( $url );
568 wfProfileOut( __METHOD__ );
569 return $redirect;
570 } elseif( $redirect === true ) {
571 global $wgScript;
572 $url = $wgScript . '?' . wfArrayToCGI( $query );
573 $wgOut->redirect( $url );
574 wfProfileOut( __METHOD__ );
575 return $redirect;
576 }
577 }
578
579 # Redirect to canonical alias for GET commands
580 # Not for POST, we'd lose the post data, so it's best to just distribute
581 # the request. Such POST requests are possible for old extensions that
582 # generate self-links without being aware that their default name has
583 # changed.
584 if ( !$including && $name != $page->getLocalName() && !$wgRequest->wasPosted() ) {
585 $query = $_GET;
586 unset( $query['title'] );
587 $query = wfArrayToCGI( $query );
588 $title = $page->getTitle( $par );
589 $url = $title->getFullUrl( $query );
590 $wgOut->redirect( $url );
591 wfProfileOut( __METHOD__ );
592 return $redirect;
593 }
594
595 if ( $including && !$page->includable() ) {
596 wfProfileOut( __METHOD__ );
597 return false;
598 } elseif ( !$including ) {
599 $wgTitle = $page->getTitle();
600 }
601 $page->including( $including );
602
603 // Execute special page
604 $profName = 'Special:' . $page->name();
605 wfProfileIn( $profName );
606 $page->execute( $par );
607 wfProfileOut( $profName );
608 wfProfileOut( __METHOD__ );
609 return true;
610 }
611
612 /**
613 * Just like executePath() except it returns the HTML instead of outputting it
614 * Returns false if there was no such special page, or a title object if it was
615 * a redirect.
616 *
617 * @return String: HTML fragment
618 */
619 static function capturePath( &$title ) {
620 global $wgOut, $wgTitle, $wgUser;
621
622 // preload the skin - Sometimes the SpecialPage loads it at a bad point in time making a includable special page override the skin title
623 // This hack is ok for now. The plan is for
624 // - Skin to stop storing it's own title
625 // - includable special pages to stop using $wgTitle and $wgOut
626 // - and OutputPage to store it's own skin object instead of askin $wgUser
627 // Once just about any of those are implemented preloading will not be necessarily
628 $wgOut->getSkin();
629
630 $oldTitle = $wgTitle;
631 $oldOut = $wgOut;
632 $wgOut = new OutputPage;
633 $wgOut->setTitle( $title );
634 $wgOut->setUser( $wgUser ); # for now, there may be a better idea in the future
635
636 $ret = SpecialPage::executePath( $title, true );
637 if ( $ret === true ) {
638 $ret = $wgOut->getHTML();
639 }
640 $wgTitle = $oldTitle;
641 $wgOut = $oldOut;
642 return $ret;
643 }
644
645 /**
646 * Get the local name for a specified canonical name
647 *
648 * @param $name String
649 * @param $subpage Mixed: boolean false, or string
650 *
651 * @return String
652 */
653 static function getLocalNameFor( $name, $subpage = false ) {
654 global $wgContLang;
655 $aliases = $wgContLang->getSpecialPageAliases();
656 if ( isset( $aliases[$name][0] ) ) {
657 $name = $aliases[$name][0];
658 } else {
659 // Try harder in case someone misspelled the correct casing
660 $found = false;
661 foreach ( $aliases as $n => $values ) {
662 if ( strcasecmp( $name, $n ) === 0 ) {
663 wfWarn( "Found alias defined for $n when searching for " .
664 "special page aliases for $name. Case mismatch?" );
665 $name = $values[0];
666 $found = true;
667 break;
668 }
669 }
670 if ( !$found ) {
671 wfWarn( "Did not find alias for special page '$name'. " .
672 "Perhaps no aliases are defined for it?" );
673 }
674 }
675 if ( $subpage !== false && !is_null( $subpage ) ) {
676 $name = "$name/$subpage";
677 }
678 return $wgContLang->ucfirst( $name );
679 }
680
681 /**
682 * Get a localised Title object for a specified special page name
683 *
684 * @return Title object
685 */
686 static function getTitleFor( $name, $subpage = false ) {
687 $name = self::getLocalNameFor( $name, $subpage );
688 if ( $name ) {
689 return Title::makeTitle( NS_SPECIAL, $name );
690 } else {
691 throw new MWException( "Invalid special page name \"$name\"" );
692 }
693 }
694
695 /**
696 * Get a localised Title object for a page name with a possibly unvalidated subpage
697 *
698 * @return Title object or null if the page doesn't exist
699 */
700 static function getSafeTitleFor( $name, $subpage = false ) {
701 $name = self::getLocalNameFor( $name, $subpage );
702 if ( $name ) {
703 return Title::makeTitleSafe( NS_SPECIAL, $name );
704 } else {
705 return null;
706 }
707 }
708
709 /**
710 * Get a title for a given alias
711 *
712 * @return Title or null if there is no such alias
713 */
714 static function getTitleForAlias( $alias ) {
715 $name = self::resolveAlias( $alias );
716 if ( $name ) {
717 return self::getTitleFor( $name );
718 } else {
719 return null;
720 }
721 }
722
723 /**
724 * Default constructor for special pages
725 * Derivative classes should call this from their constructor
726 * Note that if the user does not have the required level, an error message will
727 * be displayed by the default execute() method, without the global function ever
728 * being called.
729 *
730 * If you override execute(), you can recover the default behaviour with userCanExecute()
731 * and displayRestrictionError()
732 *
733 * @param $name String: name of the special page, as seen in links and URLs
734 * @param $restriction String: user right required, e.g. "block" or "delete"
735 * @param $listed Boolean: whether the page is listed in Special:Specialpages
736 * @param $function Callback: function called by execute(). By default it is constructed from $name
737 * @param $file String: file which is included by execute(). It is also constructed from $name by default
738 * @param $includable Boolean: whether the page can be included in normal pages
739 */
740 public function __construct( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
741 $this->init( $name, $restriction, $listed, $function, $file, $includable );
742 }
743
744 /**
745 * Do the real work for the constructor, mainly so __call() can intercept
746 * calls to SpecialPage()
747 * @see __construct() for param docs
748 */
749 private function init( $name, $restriction, $listed, $function, $file, $includable ) {
750 $this->mName = $name;
751 $this->mRestriction = $restriction;
752 $this->mListed = $listed;
753 $this->mIncludable = $includable;
754 if ( !$function ) {
755 $this->mFunction = 'wfSpecial'.$name;
756 } else {
757 $this->mFunction = $function;
758 }
759 if ( $file === 'default' ) {
760 $this->mFile = dirname(__FILE__) . "/specials/Special$name.php";
761 } else {
762 $this->mFile = $file;
763 }
764 }
765
766 /**
767 * Use PHP's magic __call handler to get calls to the old PHP4 constructor
768 * because PHP E_STRICT yells at you for having __construct() and SpecialPage()
769 *
770 * @param $fName String Name of called method
771 * @param $a Array Arguments to the method
772 * @deprecated Call isn't deprecated, but SpecialPage::SpecialPage() is
773 */
774 public function __call( $fName, $a ) {
775 // Sometimes $fName is SpecialPage, sometimes it's specialpage. <3 PHP
776 if( strtolower( $fName ) == 'specialpage' ) {
777 // Debug messages now, warnings in 1.19 or 1.20?
778 wfDebug( "Deprecated SpecialPage::SpecialPage() called, use __construct();\n" );
779 $name = isset( $a[0] ) ? $a[0] : '';
780 $restriction = isset( $a[1] ) ? $a[1] : '';
781 $listed = isset( $a[2] ) ? $a[2] : true;
782 $function = isset( $a[3] ) ? $a[3] : false;
783 $file = isset( $a[4] ) ? $a[4] : 'default';
784 $includable = isset( $a[5] ) ? $a[5] : false;
785 $this->init( $name, $restriction, $listed, $function, $file, $includable );
786 } else {
787 $className = get_class( $this );
788 throw new MWException( "Call to undefined method $className::$fName" );
789 }
790 }
791
792 /**#@+
793 * Accessor
794 *
795 * @deprecated
796 */
797 function getName() { return $this->mName; }
798 function getRestriction() { return $this->mRestriction; }
799 function getFile() { return $this->mFile; }
800 function isListed() { return $this->mListed; }
801 /**#@-*/
802
803 /**#@+
804 * Accessor and mutator
805 */
806 function name( $x = null ) { return wfSetVar( $this->mName, $x ); }
807 function restrictions( $x = null) {
808 # Use the one below this
809 wfDeprecated( __METHOD__ );
810 return wfSetVar( $this->mRestriction, $x );
811 }
812 function restriction( $x = null) { return wfSetVar( $this->mRestriction, $x ); }
813 function listed( $x = null) { return wfSetVar( $this->mListed, $x ); }
814 function func( $x = null) { return wfSetVar( $this->mFunction, $x ); }
815 function file( $x = null) { return wfSetVar( $this->mFile, $x ); }
816 function includable( $x = null ) { return wfSetVar( $this->mIncludable, $x ); }
817 function including( $x = null ) { return wfSetVar( $this->mIncluding, $x ); }
818 /**#@-*/
819
820 /**
821 * Get the localised name of the special page
822 */
823 function getLocalName() {
824 if ( !isset( $this->mLocalName ) ) {
825 $this->mLocalName = self::getLocalNameFor( $this->mName );
826 }
827 return $this->mLocalName;
828 }
829
830 /**
831 * Is this page expensive (for some definition of expensive)?
832 * Expensive pages are disabled or cached in miser mode. Originally used
833 * (and still overridden) by QueryPage and subclasses, moved here so that
834 * Special:SpecialPages can safely call it for all special pages.
835 *
836 * @return Boolean
837 */
838 public function isExpensive() {
839 return false;
840 }
841
842 /**
843 * Can be overridden by subclasses with more complicated permissions
844 * schemes.
845 *
846 * @return Boolean: should the page be displayed with the restricted-access
847 * pages?
848 */
849 public function isRestricted() {
850 global $wgGroupPermissions;
851 // DWIM: If all anons can do something, then it is not restricted
852 return $this->mRestriction != '' && empty($wgGroupPermissions['*'][$this->mRestriction]);
853 }
854
855 /**
856 * Checks if the given user (identified by an object) can execute this
857 * special page (as defined by $mRestriction). Can be overridden by sub-
858 * classes with more complicated permissions schemes.
859 *
860 * @param $user User: the user to check
861 * @return Boolean: does the user have permission to view the page?
862 */
863 public function userCanExecute( $user ) {
864 return $user->isAllowed( $this->mRestriction );
865 }
866
867 /**
868 * Output an error message telling the user what access level they have to have
869 */
870 function displayRestrictionError() {
871 $this->getOutput()->permissionRequired( $this->mRestriction );
872 }
873
874 /**
875 * Sets headers - this should be called from the execute() method of all derived classes!
876 */
877 function setHeaders() {
878 $out = $this->getOutput();
879 $out->setArticleRelated( false );
880 $out->setRobotPolicy( "noindex,nofollow" );
881 $out->setPageTitle( $this->getDescription() );
882 }
883
884 /**
885 * Default execute method
886 * Checks user permissions, calls the function given in mFunction
887 *
888 * This may be overridden by subclasses.
889 */
890 function execute( $par ) {
891 global $wgUser;
892
893 $this->setHeaders();
894
895 if ( $this->userCanExecute( $wgUser ) ) {
896 $func = $this->mFunction;
897 // only load file if the function does not exist
898 if(!is_callable($func) and $this->mFile) {
899 require_once( $this->mFile );
900 }
901 $this->outputHeader();
902 call_user_func( $func, $par, $this );
903 } else {
904 $this->displayRestrictionError();
905 }
906 }
907
908 /**
909 * Outputs a summary message on top of special pages
910 * Per default the message key is the canonical name of the special page
911 * May be overriden, i.e. by extensions to stick with the naming conventions
912 * for message keys: 'extensionname-xxx'
913 *
914 * @param $summaryMessageKey String: message key of the summary
915 */
916 function outputHeader( $summaryMessageKey = '' ) {
917 global $wgContLang;
918
919 if( $summaryMessageKey == '' ) {
920 $msg = $wgContLang->lc( $this->name() ) . '-summary';
921 } else {
922 $msg = $summaryMessageKey;
923 }
924 if ( !wfMessage( $msg )->isBlank() and ! $this->including() ) {
925 $this->getOutput()->wrapWikiMsg( "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
926 }
927
928 }
929
930 /**
931 * Returns the name that goes in the \<h1\> in the special page itself, and
932 * also the name that will be listed in Special:Specialpages
933 *
934 * Derived classes can override this, but usually it is easier to keep the
935 * default behaviour. Messages can be added at run-time, see
936 * MessageCache.php.
937 *
938 * @return String
939 */
940 function getDescription() {
941 return wfMsg( strtolower( $this->mName ) );
942 }
943
944 /**
945 * Get a self-referential title object
946 *
947 * @return Title object
948 */
949 function getTitle( $subpage = false ) {
950 return self::getTitleFor( $this->mName, $subpage );
951 }
952
953 /**
954 * Set whether this page is listed in Special:Specialpages, at run-time
955 */
956 function setListed( $listed ) {
957 return wfSetVar( $this->mListed, $listed );
958 }
959
960 /**
961 * If the special page is a redirect, then get the Title object it redirects to.
962 * False otherwise.
963 */
964 function getRedirect( $subpage ) {
965 return false;
966 }
967
968 /**
969 * Return part of the request string for a special redirect page
970 * This allows passing, e.g. action=history to Special:Mypage, etc.
971 *
972 * @return String
973 */
974 function getRedirectQuery() {
975 global $wgRequest;
976 $params = array();
977
978 foreach( $this->mAllowedRedirectParams as $arg ) {
979 if( $wgRequest->getVal( $arg, null ) !== null ){
980 $params[$arg] = $wgRequest->getVal( $arg );
981 }
982 }
983
984 foreach( $this->mAddedRedirectParams as $arg => $val ) {
985 $params[$arg] = $val;
986 }
987
988 return count( $params )
989 ? $params
990 : false;
991 }
992
993 /**
994 * Sets the context this SpecialPage is executed in
995 *
996 * @param $request WebRequest
997 * @param $output OutputPage
998 */
999 protected function setContext( $request, $output ) {
1000 $this->mRequest = $request;
1001 $this->mOutput = $output;
1002 $this->mFullTitle = $output->getTitle();
1003 }
1004
1005 /**
1006 * Get the WebRequest being used for this instance
1007 *
1008 * @return WebRequest
1009 * @since 1.18
1010 */
1011 public function getRequest() {
1012 if ( !isset($this->mRequest) ) {
1013 wfDebug( __METHOD__ . " called and \$mRequest is null. Return \$wgRequest for sanity\n" );
1014 global $wgRequest;
1015 return $wgRequest;
1016 }
1017 return $this->mRequest;
1018 }
1019
1020 /**
1021 * Get the OutputPage being used for this instance
1022 *
1023 * @return OutputPage
1024 * @since 1.18
1025 */
1026 public function getOutput() {
1027 if ( !isset($this->mOutput) ) {
1028 wfDebug( __METHOD__ . " called and \$mOutput is null. Return \$wgOut for sanity\n" );
1029 global $wgOut;
1030 return $wgOut;
1031 }
1032 return $this->mOutput;
1033 }
1034
1035 /**
1036 * Shortcut to get the skin being used for this instance
1037 *
1038 * @return User
1039 * @since 1.18
1040 */
1041 public function getUser() {
1042 return $this->getOutput()->getUser();
1043 }
1044
1045 /**
1046 * Shortcut to get the skin being used for this instance
1047 *
1048 * @return Skin
1049 * @since 1.18
1050 */
1051 public function getSkin() {
1052 return $this->getOutput()->getSkin();
1053 }
1054
1055 /**
1056 * Wrapper around wfMessage that sets the current context. Currently this
1057 * is only the title.
1058 *
1059 * @see wfMessage
1060 */
1061 public function msg( /* $args */ ) {
1062 return call_user_func_array( 'wfMessage', func_get_args() )->title( $this->mFullTitle );
1063 }
1064 }
1065
1066 /**
1067 * Shortcut to construct a special page which is unlisted by default
1068 * @ingroup SpecialPage
1069 */
1070 class UnlistedSpecialPage extends SpecialPage
1071 {
1072 function __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
1073 parent::__construct( $name, $restriction, false, $function, $file );
1074 }
1075 }
1076
1077 /**
1078 * Shortcut to construct an includable special page
1079 * @ingroup SpecialPage
1080 */
1081 class IncludableSpecialPage extends SpecialPage
1082 {
1083 function __construct( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
1084 parent::__construct( $name, $restriction, $listed, $function, $file, true );
1085 }
1086 }
1087
1088 /**
1089 * Shortcut to construct a special page alias.
1090 * @ingroup SpecialPage
1091 */
1092 class SpecialRedirectToSpecial extends UnlistedSpecialPage {
1093 var $redirName, $redirSubpage;
1094
1095 function __construct( $name, $redirName, $redirSubpage = false, $allowedRedirectParams = array(), $addedRedirectParams = array() ) {
1096 parent::__construct( $name );
1097 $this->redirName = $redirName;
1098 $this->redirSubpage = $redirSubpage;
1099 $this->mAllowedRedirectParams = $allowedRedirectParams;
1100 $this->mAddedRedirectParams = $addedRedirectParams;
1101 }
1102
1103 function getRedirect( $subpage ) {
1104 if ( $this->redirSubpage === false ) {
1105 return SpecialPage::getTitleFor( $this->redirName, $subpage );
1106 } else {
1107 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
1108 }
1109 }
1110 }
1111
1112 /**
1113 * SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
1114 * are used to get user independant links pointing to the user page, talk
1115 * page and list of contributions.
1116 * This can let us cache a single copy of any generated content for all
1117 * users.
1118 */
1119
1120 /**
1121 * Shortcut to construct a special page pointing to current user user's page.
1122 * @ingroup SpecialPage
1123 */
1124 class SpecialMypage extends UnlistedSpecialPage {
1125 function __construct() {
1126 parent::__construct( 'Mypage' );
1127 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro',
1128 'section', 'oldid', 'diff', 'dir' );
1129 }
1130
1131 function getRedirect( $subpage ) {
1132 global $wgUser;
1133 if ( strval( $subpage ) !== '' ) {
1134 return Title::makeTitle( NS_USER, $wgUser->getName() . '/' . $subpage );
1135 } else {
1136 return Title::makeTitle( NS_USER, $wgUser->getName() );
1137 }
1138 }
1139 }
1140
1141 /**
1142 * Shortcut to construct a special page pointing to current user talk page.
1143 * @ingroup SpecialPage
1144 */
1145 class SpecialMytalk extends UnlistedSpecialPage {
1146 function __construct() {
1147 parent::__construct( 'Mytalk' );
1148 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro',
1149 'section', 'oldid', 'diff', 'dir' );
1150 }
1151
1152 function getRedirect( $subpage ) {
1153 global $wgUser;
1154 if ( strval( $subpage ) !== '' ) {
1155 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() . '/' . $subpage );
1156 } else {
1157 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
1158 }
1159 }
1160 }
1161
1162 /**
1163 * Shortcut to construct a special page pointing to current user contributions.
1164 * @ingroup SpecialPage
1165 */
1166 class SpecialMycontributions extends UnlistedSpecialPage {
1167 function __construct() {
1168 parent::__construct( 'Mycontributions' );
1169 $this->mAllowedRedirectParams = array( 'limit', 'namespace', 'tagfilter',
1170 'offset', 'dir', 'year', 'month', 'feed' );
1171 }
1172
1173 function getRedirect( $subpage ) {
1174 global $wgUser;
1175 return SpecialPage::getTitleFor( 'Contributions', $wgUser->getName() );
1176 }
1177 }
1178
1179 /**
1180 * Redirect to Special:Listfiles?user=$wgUser
1181 */
1182 class SpecialMyuploads extends UnlistedSpecialPage {
1183 function __construct() {
1184 parent::__construct( 'Myuploads' );
1185 $this->mAllowedRedirectParams = array( 'limit' );
1186 }
1187
1188 function getRedirect( $subpage ) {
1189 global $wgUser;
1190 return SpecialPage::getTitleFor( 'Listfiles', $wgUser->getName() );
1191 }
1192 }
1193
1194 /**
1195 * Redirect from Special:PermanentLink/### to index.php?oldid=###
1196 */
1197 class SpecialPermanentLink extends UnlistedSpecialPage {
1198 function __construct() {
1199 parent::__construct( 'PermanentLink' );
1200 $this->mAllowedRedirectParams = array();
1201 }
1202
1203 function getRedirect( $subpage ) {
1204 $subpage = intval( $subpage );
1205 $this->mAddedRedirectParams['oldid'] = $subpage;
1206 return true;
1207 }
1208 }