Follow-up r77796: a couple of typos in the comment, and break more consistently at...
[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 }
752 }
753
754 /**#@+
755 * Accessor
756 *
757 * @deprecated
758 */
759 function getName() { return $this->mName; }
760 function getRestriction() { return $this->mRestriction; }
761 function getFile() { return $this->mFile; }
762 function isListed() { return $this->mListed; }
763 /**#@-*/
764
765 /**#@+
766 * Accessor and mutator
767 */
768 function name( $x = null ) { return wfSetVar( $this->mName, $x ); }
769 function restrictions( $x = null) {
770 # Use the one below this
771 wfDeprecated( __METHOD__ );
772 return wfSetVar( $this->mRestriction, $x );
773 }
774 function restriction( $x = null) { return wfSetVar( $this->mRestriction, $x ); }
775 function listed( $x = null) { return wfSetVar( $this->mListed, $x ); }
776 function func( $x = null) { return wfSetVar( $this->mFunction, $x ); }
777 function file( $x = null) { return wfSetVar( $this->mFile, $x ); }
778 function includable( $x = null ) { return wfSetVar( $this->mIncludable, $x ); }
779 function including( $x = null ) { return wfSetVar( $this->mIncluding, $x ); }
780 /**#@-*/
781
782 /**
783 * Get the localised name of the special page
784 */
785 function getLocalName() {
786 if ( !isset( $this->mLocalName ) ) {
787 $this->mLocalName = self::getLocalNameFor( $this->mName );
788 }
789 return $this->mLocalName;
790 }
791
792 /**
793 * Can be overridden by subclasses with more complicated permissions
794 * schemes.
795 *
796 * @return Boolean: should the page be displayed with the restricted-access
797 * pages?
798 */
799 public function isRestricted() {
800 global $wgGroupPermissions;
801 // DWIM: If all anons can do something, then it is not restricted
802 return $this->mRestriction != '' && empty($wgGroupPermissions['*'][$this->mRestriction]);
803 }
804
805 /**
806 * Checks if the given user (identified by an object) can execute this
807 * special page (as defined by $mRestriction). Can be overridden by sub-
808 * classes with more complicated permissions schemes.
809 *
810 * @param $user User: the user to check
811 * @return Boolean: does the user have permission to view the page?
812 */
813 public function userCanExecute( $user ) {
814 return $user->isAllowed( $this->mRestriction );
815 }
816
817 /**
818 * Output an error message telling the user what access level they have to have
819 */
820 function displayRestrictionError() {
821 global $wgOut;
822 $wgOut->permissionRequired( $this->mRestriction );
823 }
824
825 /**
826 * Sets headers - this should be called from the execute() method of all derived classes!
827 */
828 function setHeaders() {
829 global $wgOut;
830 $wgOut->setArticleRelated( false );
831 $wgOut->setRobotPolicy( "noindex,nofollow" );
832 $wgOut->setPageTitle( $this->getDescription() );
833 }
834
835 /**
836 * Default execute method
837 * Checks user permissions, calls the function given in mFunction
838 *
839 * This may be overridden by subclasses.
840 */
841 function execute( $par ) {
842 global $wgUser;
843
844 $this->setHeaders();
845
846 if ( $this->userCanExecute( $wgUser ) ) {
847 $func = $this->mFunction;
848 // only load file if the function does not exist
849 if(!is_callable($func) and $this->mFile) {
850 require_once( $this->mFile );
851 }
852 $this->outputHeader();
853 call_user_func( $func, $par, $this );
854 } else {
855 $this->displayRestrictionError();
856 }
857 }
858
859 /**
860 * Outputs a summary message on top of special pages
861 * Per default the message key is the canonical name of the special page
862 * May be overriden, i.e. by extensions to stick with the naming conventions
863 * for message keys: 'extensionname-xxx'
864 *
865 * @param $summaryMessageKey String: message key of the summary
866 */
867 function outputHeader( $summaryMessageKey = '' ) {
868 global $wgOut, $wgContLang;
869
870 if( $summaryMessageKey == '' ) {
871 $msg = $wgContLang->lc( $this->name() ) . '-summary';
872 } else {
873 $msg = $summaryMessageKey;
874 }
875 $out = wfMsgNoTrans( $msg );
876 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() ) {
877 $wgOut->wrapWikiMsg( "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
878 }
879
880 }
881
882 /**
883 * Returns the name that goes in the \<h1\> in the special page itself, and
884 * also the name that will be listed in Special:Specialpages
885 *
886 * Derived classes can override this, but usually it is easier to keep the
887 * default behaviour. Messages can be added at run-time, see
888 * MessageCache.php.
889 *
890 * @return String
891 */
892 function getDescription() {
893 return wfMsg( strtolower( $this->mName ) );
894 }
895
896 /**
897 * Get a self-referential title object
898 *
899 * @return Title object
900 */
901 function getTitle( $subpage = false ) {
902 return self::getTitleFor( $this->mName, $subpage );
903 }
904
905 /**
906 * Set whether this page is listed in Special:Specialpages, at run-time
907 */
908 function setListed( $listed ) {
909 return wfSetVar( $this->mListed, $listed );
910 }
911
912 /**
913 * If the special page is a redirect, then get the Title object it redirects to.
914 * False otherwise.
915 */
916 function getRedirect( $subpage ) {
917 return false;
918 }
919
920 /**
921 * Return part of the request string for a special redirect page
922 * This allows passing, e.g. action=history to Special:Mypage, etc.
923 *
924 * @return String
925 */
926 function getRedirectQuery() {
927 global $wgRequest;
928 $params = array();
929 foreach( $this->mAllowedRedirectParams as $arg ) {
930 if( ( $val = $wgRequest->getVal( $arg, null ) ) !== null )
931 $params[] = $arg . '=' . $val;
932 }
933
934 foreach( $this->mAddedRedirectParams as $arg => $val ) {
935 $params[] = $arg . '=' . $val;
936 }
937
938 return count( $params ) ? implode( '&', $params ) : false;
939 }
940 }
941
942 /**
943 * Shortcut to construct a special page which is unlisted by default
944 * @ingroup SpecialPage
945 */
946 class UnlistedSpecialPage extends SpecialPage
947 {
948 function __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
949 parent::__construct( $name, $restriction, false, $function, $file );
950 }
951 }
952
953 /**
954 * Shortcut to construct an includable special page
955 * @ingroup SpecialPage
956 */
957 class IncludableSpecialPage extends SpecialPage
958 {
959 function __construct( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
960 parent::__construct( $name, $restriction, $listed, $function, $file, true );
961 }
962 }
963
964 /**
965 * Shortcut to construct a special page alias.
966 * @ingroup SpecialPage
967 */
968 class SpecialRedirectToSpecial extends UnlistedSpecialPage {
969 var $redirName, $redirSubpage;
970
971 function __construct( $name, $redirName, $redirSubpage = false, $allowedRedirectParams = array(), $addedRedirectParams = array() ) {
972 parent::__construct( $name );
973 $this->redirName = $redirName;
974 $this->redirSubpage = $redirSubpage;
975 $this->mAllowedRedirectParams = $allowedRedirectParams;
976 $this->mAddedRedirectParams = $addedRedirectParams;
977 }
978
979 function getRedirect( $subpage ) {
980 if ( $this->redirSubpage === false ) {
981 return SpecialPage::getTitleFor( $this->redirName, $subpage );
982 } else {
983 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
984 }
985 }
986 }
987
988 /** SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
989 * are used to get user independant links pointing to the user page, talk
990 * page and list of contributions.
991 * This can let us cache a single copy of any generated content for all
992 * users.
993 */
994
995 /**
996 * Shortcut to construct a special page pointing to current user user's page.
997 * @ingroup SpecialPage
998 */
999 class SpecialMypage extends UnlistedSpecialPage {
1000 function __construct() {
1001 parent::__construct( 'Mypage' );
1002 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro',
1003 'section', 'oldid', 'diff', 'dir' );
1004 }
1005
1006 function getRedirect( $subpage ) {
1007 global $wgUser;
1008 if ( strval( $subpage ) !== '' ) {
1009 return Title::makeTitle( NS_USER, $wgUser->getName() . '/' . $subpage );
1010 } else {
1011 return Title::makeTitle( NS_USER, $wgUser->getName() );
1012 }
1013 }
1014 }
1015
1016 /**
1017 * Shortcut to construct a special page pointing to current user talk page.
1018 * @ingroup SpecialPage
1019 */
1020 class SpecialMytalk extends UnlistedSpecialPage {
1021 function __construct() {
1022 parent::__construct( 'Mytalk' );
1023 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro',
1024 'section', 'oldid', 'diff', 'dir' );
1025 }
1026
1027 function getRedirect( $subpage ) {
1028 global $wgUser;
1029 if ( strval( $subpage ) !== '' ) {
1030 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() . '/' . $subpage );
1031 } else {
1032 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
1033 }
1034 }
1035 }
1036
1037 /**
1038 * Shortcut to construct a special page pointing to current user contributions.
1039 * @ingroup SpecialPage
1040 */
1041 class SpecialMycontributions extends UnlistedSpecialPage {
1042 function __construct() {
1043 parent::__construct( 'Mycontributions' );
1044 $this->mAllowedRedirectParams = array( 'limit', 'namespace', 'tagfilter',
1045 'offset', 'dir', 'year', 'month', 'feed' );
1046 }
1047
1048 function getRedirect( $subpage ) {
1049 global $wgUser;
1050 return SpecialPage::getTitleFor( 'Contributions', $wgUser->getName() );
1051 }
1052 }
1053
1054 /**
1055 * Redirect to Special:Listfiles?user=$wgUser
1056 */
1057 class SpecialMyuploads extends UnlistedSpecialPage {
1058 function __construct() {
1059 parent::__construct( 'Myuploads' );
1060 $this->mAllowedRedirectParams = array( 'limit' );
1061 }
1062
1063 function getRedirect( $subpage ) {
1064 global $wgUser;
1065 return SpecialPage::getTitleFor( 'Listfiles', $wgUser->getName() );
1066 }
1067 }