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