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