Merge DisableAccount extension into core, disabled by default because the rights...
[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' => array( 'SpecialPage', 'Userlogin' ),
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 'Revisiondelete' => 'SpecialRevisionDelete',
195 'RevisionMove' => 'SpecialRevisionMove',
196 'Specialpages' => 'SpecialSpecialpages',
197 'Userlogout' => 'SpecialUserlogout',
198 );
199
200 static public $mAliases;
201 static public $mListInitialised = false;
202
203 /**#@-*/
204
205 /**
206 * Initialise the special page list
207 * This must be called before accessing SpecialPage::$mList
208 */
209 static function initList() {
210 global $wgSpecialPages;
211 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
212
213 if ( self::$mListInitialised ) {
214 return;
215 }
216 wfProfileIn( __METHOD__ );
217
218 # Better to set this now, to avoid infinite recursion in carelessly written hooks
219 self::$mListInitialised = true;
220
221 if( !$wgDisableCounters ) {
222 self::$mList['Popularpages'] = array( 'SpecialPage', 'Popularpages' );
223 }
224
225 if( !$wgDisableInternalSearch ) {
226 self::$mList['Search'] = array( 'SpecialPage', 'Search' );
227 }
228
229 if( $wgEmailAuthentication ) {
230 self::$mList['Confirmemail'] = 'EmailConfirmation';
231 self::$mList['Invalidateemail'] = 'EmailInvalidation';
232 }
233
234 # Add extension special pages
235 self::$mList = array_merge( self::$mList, $wgSpecialPages );
236
237 # Run hooks
238 # This hook can be used to remove undesired built-in special pages
239 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
240 wfProfileOut( __METHOD__ );
241 }
242
243 static function initAliasList() {
244 if ( !is_null( self::$mAliases ) ) {
245 return;
246 }
247
248 global $wgContLang;
249 $aliases = $wgContLang->getSpecialPageAliases();
250 $missingPages = self::$mList;
251 self::$mAliases = array();
252 foreach ( $aliases as $realName => $aliasList ) {
253 foreach ( $aliasList as $alias ) {
254 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
255 }
256 unset( $missingPages[$realName] );
257 }
258 foreach ( $missingPages as $name => $stuff ) {
259 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
260 }
261 }
262
263 /**
264 * Given a special page alias, return the special page name.
265 * Returns false if there is no such alias.
266 *
267 * @param $alias String
268 * @return String or false
269 */
270 static function resolveAlias( $alias ) {
271 global $wgContLang;
272
273 if ( !self::$mListInitialised ) self::initList();
274 if ( is_null( self::$mAliases ) ) self::initAliasList();
275 $caseFoldedAlias = $wgContLang->caseFold( $alias );
276 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
277 if ( isset( self::$mAliases[$caseFoldedAlias] ) ) {
278 return self::$mAliases[$caseFoldedAlias];
279 } else {
280 return false;
281 }
282 }
283
284 /**
285 * Given a special page name with a possible subpage, return an array
286 * where the first element is the special page name and the second is the
287 * subpage.
288 *
289 * @param $alias String
290 * @return Array
291 */
292 static function resolveAliasWithSubpage( $alias ) {
293 $bits = explode( '/', $alias, 2 );
294 $name = self::resolveAlias( $bits[0] );
295 if( !isset( $bits[1] ) ) { // bug 2087
296 $par = null;
297 } else {
298 $par = $bits[1];
299 }
300 return array( $name, $par );
301 }
302
303 /**
304 * Add a page to the list of valid special pages. This used to be the preferred
305 * method for adding special pages in extensions. It's now suggested that you add
306 * an associative record to $wgSpecialPages. This avoids autoloading SpecialPage.
307 *
308 * @param $page SpecialPage
309 * Deprecated in 1.7, warnings in 1.17, might be removed in 1.20
310 */
311 static function addPage( &$page ) {
312 wfDeprecated( __METHOD__ );
313 if ( !self::$mListInitialised ) {
314 self::initList();
315 }
316 self::$mList[$page->mName] = $page;
317 }
318
319 /**
320 * Add a page to a certain display group for Special:SpecialPages
321 *
322 * @param $page Mixed: SpecialPage or string
323 * @param $group String
324 */
325 static function setGroup( $page, $group ) {
326 global $wgSpecialPageGroups;
327 $name = is_object($page) ? $page->mName : $page;
328 $wgSpecialPageGroups[$name] = $group;
329 }
330
331 /**
332 * Add a page to a certain display group for Special:SpecialPages
333 *
334 * @param $page SpecialPage
335 */
336 static function getGroup( &$page ) {
337 global $wgSpecialPageGroups;
338 static $specialPageGroupsCache = array();
339 if( isset($specialPageGroupsCache[$page->mName]) ) {
340 return $specialPageGroupsCache[$page->mName];
341 }
342 $group = wfMsg('specialpages-specialpagegroup-'.strtolower($page->mName));
343 if( $group == ''
344 || wfEmptyMsg('specialpages-specialpagegroup-'.strtolower($page->mName), $group ) ) {
345 $group = isset($wgSpecialPageGroups[$page->mName])
346 ? $wgSpecialPageGroups[$page->mName]
347 : '-';
348 }
349 if( $group == '-' ) $group = 'other';
350 $specialPageGroupsCache[$page->mName] = $group;
351 return $group;
352 }
353
354 /**
355 * Remove a special page from the list
356 * Formerly used to disable expensive or dangerous special pages. The
357 * preferred method is now to add a SpecialPage_initList hook.
358 */
359 static function removePage( $name ) {
360 if ( !self::$mListInitialised ) {
361 self::initList();
362 }
363 unset( self::$mList[$name] );
364 }
365
366 /**
367 * Check if a given name exist as a special page or as a special page alias
368 *
369 * @param $name String: name of a special page
370 * @return Boolean: true if a special page exists with this name
371 */
372 static function exists( $name ) {
373 global $wgContLang;
374 if ( !self::$mListInitialised ) {
375 self::initList();
376 }
377 if( !self::$mAliases ) {
378 self::initAliasList();
379 }
380
381 # Remove special pages inline parameters:
382 $bits = explode( '/', $name );
383 $name = $wgContLang->caseFold($bits[0]);
384
385 return
386 array_key_exists( $name, self::$mList )
387 or array_key_exists( $name, self::$mAliases )
388 ;
389 }
390
391 /**
392 * Find the object with a given name and return it (or NULL)
393 *
394 * @param $name String
395 * @return SpecialPage object or null if the page doesn't exist
396 */
397 static function getPage( $name ) {
398 if ( !self::$mListInitialised ) {
399 self::initList();
400 }
401 if ( array_key_exists( $name, self::$mList ) ) {
402 $rec = self::$mList[$name];
403 if ( is_string( $rec ) ) {
404 $className = $rec;
405 self::$mList[$name] = new $className;
406 } elseif ( is_array( $rec ) ) {
407 $className = array_shift( $rec );
408 self::$mList[$name] = wfCreateObject( $className, $rec );
409 }
410 return self::$mList[$name];
411 } else {
412 return null;
413 }
414 }
415
416 /**
417 * Get a special page with a given localised name, or NULL if there
418 * is no such special page.
419 *
420 * @return SpecialPage object or null if the page doesn't exist
421 */
422 static function getPageByAlias( $alias ) {
423 $realName = self::resolveAlias( $alias );
424 if ( $realName ) {
425 return self::getPage( $realName );
426 } else {
427 return null;
428 }
429 }
430
431 /**
432 * Return categorised listable special pages which are available
433 * for the current user, and everyone.
434 *
435 * @return Associative array mapping page's name to its SpecialPage object
436 */
437 static function getUsablePages() {
438 global $wgUser;
439 if ( !self::$mListInitialised ) {
440 self::initList();
441 }
442 $pages = array();
443
444 foreach ( self::$mList as $name => $rec ) {
445 $page = self::getPage( $name );
446 if ( $page->isListed()
447 && (
448 !$page->isRestricted()
449 || $page->userCanExecute( $wgUser )
450 )
451 ) {
452 $pages[$name] = $page;
453 }
454 }
455 return $pages;
456 }
457
458 /**
459 * Return categorised listable special pages for all users
460 *
461 * @return Associative array mapping page's name to its SpecialPage object
462 */
463 static function getRegularPages() {
464 if ( !self::$mListInitialised ) {
465 self::initList();
466 }
467 $pages = array();
468
469 foreach ( self::$mList as $name => $rec ) {
470 $page = self::getPage( $name );
471 if ( $page->isListed() && !$page->isRestricted() ) {
472 $pages[$name] = $page;
473 }
474 }
475 return $pages;
476 }
477
478 /**
479 * Return categorised listable special pages which are available
480 * for the current user, but not for everyone
481 *
482 * @return Associative array mapping page's name to its SpecialPage object
483 */
484 static function getRestrictedPages() {
485 global $wgUser;
486 if( !self::$mListInitialised ) {
487 self::initList();
488 }
489 $pages = array();
490
491 foreach( self::$mList as $name => $rec ) {
492 $page = self::getPage( $name );
493 if(
494 $page->isListed()
495 && $page->isRestricted()
496 && $page->userCanExecute( $wgUser )
497 ) {
498 $pages[$name] = $page;
499 }
500 }
501 return $pages;
502 }
503
504 /**
505 * Execute a special page path.
506 * The path may contain parameters, e.g. Special:Name/Params
507 * Extracts the special page name and call the execute method, passing the parameters
508 *
509 * Returns a title object if the page is redirected, false if there was no such special
510 * page, and true if it was successful.
511 *
512 * @param $title a title object
513 * @param $including output is being captured for use in {{special:whatever}}
514 */
515 static function executePath( &$title, $including = false ) {
516 global $wgOut, $wgTitle, $wgRequest;
517 wfProfileIn( __METHOD__ );
518
519 # FIXME: redirects broken due to this call
520 $bits = explode( '/', $title->getDBkey(), 2 );
521 $name = $bits[0];
522 if( !isset( $bits[1] ) ) { // bug 2087
523 $par = null;
524 } else {
525 $par = $bits[1];
526 }
527 $page = SpecialPage::getPageByAlias( $name );
528 # Nonexistent?
529 if ( !$page ) {
530 if ( !$including ) {
531 $wgOut->setArticleRelated( false );
532 $wgOut->setRobotPolicy( 'noindex,nofollow' );
533 $wgOut->setStatusCode( 404 );
534 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
535 }
536 wfProfileOut( __METHOD__ );
537 return false;
538 }
539
540 # Check for redirect
541 if ( !$including ) {
542 $redirect = $page->getRedirect( $par );
543 if ( $redirect ) {
544 $query = $page->getRedirectQuery();
545 $url = $redirect->getFullUrl( $query );
546 $wgOut->redirect( $url );
547 wfProfileOut( __METHOD__ );
548 return $redirect;
549 }
550 }
551
552 # Redirect to canonical alias for GET commands
553 # Not for POST, we'd lose the post data, so it's best to just distribute
554 # the request. Such POST requests are possible for old extensions that
555 # generate self-links without being aware that their default name has
556 # changed.
557 if ( !$including && $name != $page->getLocalName() && !$wgRequest->wasPosted() ) {
558 $query = $_GET;
559 unset( $query['title'] );
560 $query = wfArrayToCGI( $query );
561 $title = $page->getTitle( $par );
562 $url = $title->getFullUrl( $query );
563 $wgOut->redirect( $url );
564 wfProfileOut( __METHOD__ );
565 return $redirect;
566 }
567
568 if ( $including && !$page->includable() ) {
569 wfProfileOut( __METHOD__ );
570 return false;
571 } elseif ( !$including ) {
572 $wgTitle = $page->getTitle();
573 }
574 $page->including( $including );
575
576 // Execute special page
577 $profName = 'Special:' . $page->name();
578 wfProfileIn( $profName );
579 $page->execute( $par );
580 wfProfileOut( $profName );
581 wfProfileOut( __METHOD__ );
582 return true;
583 }
584
585 /**
586 * Just like executePath() except it returns the HTML instead of outputting it
587 * Returns false if there was no such special page, or a title object if it was
588 * a redirect.
589 *
590 * @return String: HTML fragment
591 */
592 static function capturePath( &$title ) {
593 global $wgOut, $wgTitle;
594
595 $oldTitle = $wgTitle;
596 $oldOut = $wgOut;
597 $wgOut = new OutputPage;
598 $wgOut->setTitle( $title );
599
600 $ret = SpecialPage::executePath( $title, true );
601 if ( $ret === true ) {
602 $ret = $wgOut->getHTML();
603 }
604 $wgTitle = $oldTitle;
605 $wgOut = $oldOut;
606 return $ret;
607 }
608
609 /**
610 * Get the local name for a specified canonical name
611 *
612 * @param $name String
613 * @param $subpage Mixed: boolean false, or string
614 *
615 * @return String
616 */
617 static function getLocalNameFor( $name, $subpage = false ) {
618 global $wgContLang;
619 $aliases = $wgContLang->getSpecialPageAliases();
620 if ( isset( $aliases[$name][0] ) ) {
621 $name = $aliases[$name][0];
622 } else {
623 // Try harder in case someone misspelled the correct casing
624 $found = false;
625 foreach ( $aliases as $n => $values ) {
626 if ( strcasecmp( $name, $n ) === 0 ) {
627 wfWarn( "Found alias defined for $n when searching for" .
628 "special page aliases for $name. Case mismatch?" );
629 $name = $values[0];
630 $found = true;
631 break;
632 }
633 }
634 if ( !$found ) {
635 wfWarn( "Did not find alias for special page '$name'. " .
636 "Perhaps no aliases are defined for it?" );
637 }
638 }
639 if ( $subpage !== false && !is_null( $subpage ) ) {
640 $name = "$name/$subpage";
641 }
642 return $wgContLang->ucfirst( $name );
643 }
644
645 /**
646 * Get a localised Title object for a specified special page name
647 *
648 * @return Title object
649 */
650 static function getTitleFor( $name, $subpage = false ) {
651 $name = self::getLocalNameFor( $name, $subpage );
652 if ( $name ) {
653 return Title::makeTitle( NS_SPECIAL, $name );
654 } else {
655 throw new MWException( "Invalid special page name \"$name\"" );
656 }
657 }
658
659 /**
660 * Get a localised Title object for a page name with a possibly unvalidated subpage
661 *
662 * @return Title object or null if the page doesn't exist
663 */
664 static function getSafeTitleFor( $name, $subpage = false ) {
665 $name = self::getLocalNameFor( $name, $subpage );
666 if ( $name ) {
667 return Title::makeTitleSafe( NS_SPECIAL, $name );
668 } else {
669 return null;
670 }
671 }
672
673 /**
674 * Get a title for a given alias
675 *
676 * @return Title or null if there is no such alias
677 */
678 static function getTitleForAlias( $alias ) {
679 $name = self::resolveAlias( $alias );
680 if ( $name ) {
681 return self::getTitleFor( $name );
682 } else {
683 return null;
684 }
685 }
686
687 /**
688 * Default constructor for special pages
689 * Derivative classes should call this from their constructor
690 * Note that if the user does not have the required level, an error message will
691 * be displayed by the default execute() method, without the global function ever
692 * being called.
693 *
694 * If you override execute(), you can recover the default behaviour with userCanExecute()
695 * and displayRestrictionError()
696 *
697 * @param $name String: name of the special page, as seen in links and URLs
698 * @param $restriction String: user right required, e.g. "block" or "delete"
699 * @param $listed Boolean: whether the page is listed in Special:Specialpages
700 * @param $function Callback: function called by execute(). By default it is constructed from $name
701 * @param $file String: file which is included by execute(). It is also constructed from $name by default
702 * @param $includable Boolean: whether the page can be included in normal pages
703 */
704 function __construct( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
705 $this->mName = $name;
706 $this->mRestriction = $restriction;
707 $this->mListed = $listed;
708 $this->mIncludable = $includable;
709 if ( !$function ) {
710 $this->mFunction = 'wfSpecial'.$name;
711 } else {
712 $this->mFunction = $function;
713 }
714 if ( $file === 'default' ) {
715 $this->mFile = dirname(__FILE__) . "/specials/Special$name.php";
716 } else {
717 $this->mFile = $file;
718 }
719 }
720
721 /**#@+
722 * Accessor
723 *
724 * @deprecated
725 */
726 function getName() { return $this->mName; }
727 function getRestriction() { return $this->mRestriction; }
728 function getFile() { return $this->mFile; }
729 function isListed() { return $this->mListed; }
730 /**#@-*/
731
732 /**#@+
733 * Accessor and mutator
734 */
735 function name( $x = null ) { return wfSetVar( $this->mName, $x ); }
736 function restrictions( $x = null) {
737 # Use the one below this
738 wfDeprecated( __METHOD__ );
739 return wfSetVar( $this->mRestriction, $x );
740 }
741 function restriction( $x = null) { return wfSetVar( $this->mRestriction, $x ); }
742 function listed( $x = null) { return wfSetVar( $this->mListed, $x ); }
743 function func( $x = null) { return wfSetVar( $this->mFunction, $x ); }
744 function file( $x = null) { return wfSetVar( $this->mFile, $x ); }
745 function includable( $x = null ) { return wfSetVar( $this->mIncludable, $x ); }
746 function including( $x = null ) { return wfSetVar( $this->mIncluding, $x ); }
747 /**#@-*/
748
749 /**
750 * Get the localised name of the special page
751 */
752 function getLocalName() {
753 if ( !isset( $this->mLocalName ) ) {
754 $this->mLocalName = self::getLocalNameFor( $this->mName );
755 }
756 return $this->mLocalName;
757 }
758
759 /**
760 * Can be overridden by subclasses with more complicated permissions
761 * schemes.
762 *
763 * @return Boolean: should the page be displayed with the restricted-access
764 * pages?
765 */
766 public function isRestricted() {
767 global $wgGroupPermissions;
768 // DWIM: If all anons can do something, then it is not restricted
769 return $this->mRestriction != '' && empty($wgGroupPermissions['*'][$this->mRestriction]);
770 }
771
772 /**
773 * Checks if the given user (identified by an object) can execute this
774 * special page (as defined by $mRestriction). Can be overridden by sub-
775 * classes with more complicated permissions schemes.
776 *
777 * @param $user User: the user to check
778 * @return Boolean: does the user have permission to view the page?
779 */
780 public function userCanExecute( $user ) {
781 return $user->isAllowed( $this->mRestriction );
782 }
783
784 /**
785 * Output an error message telling the user what access level they have to have
786 */
787 function displayRestrictionError() {
788 global $wgOut;
789 $wgOut->permissionRequired( $this->mRestriction );
790 }
791
792 /**
793 * Sets headers - this should be called from the execute() method of all derived classes!
794 */
795 function setHeaders() {
796 global $wgOut;
797 $wgOut->setArticleRelated( false );
798 $wgOut->setRobotPolicy( "noindex,nofollow" );
799 $wgOut->setPageTitle( $this->getDescription() );
800 }
801
802 /**
803 * Default execute method
804 * Checks user permissions, calls the function given in mFunction
805 *
806 * This may be overridden by subclasses.
807 */
808 function execute( $par ) {
809 global $wgUser;
810
811 $this->setHeaders();
812
813 if ( $this->userCanExecute( $wgUser ) ) {
814 $func = $this->mFunction;
815 // only load file if the function does not exist
816 if(!is_callable($func) and $this->mFile) {
817 require_once( $this->mFile );
818 }
819 $this->outputHeader();
820 call_user_func( $func, $par, $this );
821 } else {
822 $this->displayRestrictionError();
823 }
824 }
825
826 /**
827 * Outputs a summary message on top of special pages
828 * Per default the message key is the canonical name of the special page
829 * May be overriden, i.e. by extensions to stick with the naming conventions
830 * for message keys: 'extensionname-xxx'
831 *
832 * @param $summaryMessageKey String: message key of the summary
833 */
834 function outputHeader( $summaryMessageKey = '' ) {
835 global $wgOut, $wgContLang;
836
837 if( $summaryMessageKey == '' ) {
838 $msg = $wgContLang->lc( $this->name() ) . '-summary';
839 } else {
840 $msg = $summaryMessageKey;
841 }
842 $out = wfMsgNoTrans( $msg );
843 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() ) {
844 $wgOut->wrapWikiMsg( "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
845 }
846
847 }
848
849 /**
850 * Returns the name that goes in the \<h1\> in the special page itself, and
851 * also the name that will be listed in Special:Specialpages
852 *
853 * Derived classes can override this, but usually it is easier to keep the
854 * default behaviour. Messages can be added at run-time, see
855 * MessageCache.php.
856 *
857 * @return String
858 */
859 function getDescription() {
860 return wfMsg( strtolower( $this->mName ) );
861 }
862
863 /**
864 * Get a self-referential title object
865 *
866 * @return Title object
867 */
868 function getTitle( $subpage = false ) {
869 return self::getTitleFor( $this->mName, $subpage );
870 }
871
872 /**
873 * Set whether this page is listed in Special:Specialpages, at run-time
874 */
875 function setListed( $listed ) {
876 return wfSetVar( $this->mListed, $listed );
877 }
878
879 /**
880 * If the special page is a redirect, then get the Title object it redirects to.
881 * False otherwise.
882 */
883 function getRedirect( $subpage ) {
884 return false;
885 }
886
887 /**
888 * Return part of the request string for a special redirect page
889 * This allows passing, e.g. action=history to Special:Mypage, etc.
890 *
891 * @return String
892 */
893 function getRedirectQuery() {
894 global $wgRequest;
895 $params = array();
896 foreach( $this->mAllowedRedirectParams as $arg ) {
897 if( ( $val = $wgRequest->getVal( $arg, null ) ) !== null )
898 $params[] = $arg . '=' . $val;
899 }
900
901 foreach( $this->mAddedRedirectParams as $arg => $val ) {
902 $params[] = $arg . '=' . $val;
903 }
904
905 return count( $params ) ? implode( '&', $params ) : false;
906 }
907 }
908
909 /**
910 * Shortcut to construct a special page which is unlisted by default
911 * @ingroup SpecialPage
912 */
913 class UnlistedSpecialPage extends SpecialPage
914 {
915 function __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
916 parent::__construct( $name, $restriction, false, $function, $file );
917 }
918 }
919
920 /**
921 * Shortcut to construct an includable special page
922 * @ingroup SpecialPage
923 */
924 class IncludableSpecialPage extends SpecialPage
925 {
926 function __construct( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
927 parent::__construct( $name, $restriction, $listed, $function, $file, true );
928 }
929 }
930
931 /**
932 * Shortcut to construct a special page alias.
933 * @ingroup SpecialPage
934 */
935 class SpecialRedirectToSpecial extends UnlistedSpecialPage {
936 var $redirName, $redirSubpage;
937
938 function __construct( $name, $redirName, $redirSubpage = false, $allowedRedirectParams = array(), $addedRedirectParams = array() ) {
939 parent::__construct( $name );
940 $this->redirName = $redirName;
941 $this->redirSubpage = $redirSubpage;
942 $this->mAllowedRedirectParams = $allowedRedirectParams;
943 $this->mAddedRedirectParams = $addedRedirectParams;
944 }
945
946 function getRedirect( $subpage ) {
947 if ( $this->redirSubpage === false ) {
948 return SpecialPage::getTitleFor( $this->redirName, $subpage );
949 } else {
950 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
951 }
952 }
953 }
954
955 /** SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
956 * are used to get user independant links pointing to the user page, talk
957 * page and list of contributions.
958 * This can let us cache a single copy of any generated content for all
959 * users.
960 */
961
962 /**
963 * Shortcut to construct a special page pointing to current user user's page.
964 * @ingroup SpecialPage
965 */
966 class SpecialMypage extends UnlistedSpecialPage {
967 function __construct() {
968 parent::__construct( 'Mypage' );
969 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro',
970 'section', 'oldid', 'diff', 'dir' );
971 }
972
973 function getRedirect( $subpage ) {
974 global $wgUser;
975 if ( strval( $subpage ) !== '' ) {
976 return Title::makeTitle( NS_USER, $wgUser->getName() . '/' . $subpage );
977 } else {
978 return Title::makeTitle( NS_USER, $wgUser->getName() );
979 }
980 }
981 }
982
983 /**
984 * Shortcut to construct a special page pointing to current user talk page.
985 * @ingroup SpecialPage
986 */
987 class SpecialMytalk extends UnlistedSpecialPage {
988 function __construct() {
989 parent::__construct( 'Mytalk' );
990 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro',
991 'section', 'oldid', 'diff', 'dir' );
992 }
993
994 function getRedirect( $subpage ) {
995 global $wgUser;
996 if ( strval( $subpage ) !== '' ) {
997 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() . '/' . $subpage );
998 } else {
999 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
1000 }
1001 }
1002 }
1003
1004 /**
1005 * Shortcut to construct a special page pointing to current user contributions.
1006 * @ingroup SpecialPage
1007 */
1008 class SpecialMycontributions extends UnlistedSpecialPage {
1009 function __construct() {
1010 parent::__construct( 'Mycontributions' );
1011 $this->mAllowedRedirectParams = array( 'limit', 'namespace', 'tagfilter',
1012 'offset', 'dir', 'year', 'month', 'feed' );
1013 }
1014
1015 function getRedirect( $subpage ) {
1016 global $wgUser;
1017 return SpecialPage::getTitleFor( 'Contributions', $wgUser->getName() );
1018 }
1019 }