Changing the UI for [[Special:Specialpages]].
[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 /**#@+
33 * @access private
34 */
35 /**
36 * The canonical name of this special page
37 * Also used for the default <h1> heading, @see getDescription()
38 */
39 var $mName;
40 /**
41 * The local name of this special page
42 */
43 var $mLocalName;
44 /**
45 * Minimum user level required to access this page, or "" for anyone.
46 * Also used to categorise the pages in Special:Specialpages
47 */
48 var $mRestriction;
49 /**
50 * Listed in Special:Specialpages?
51 */
52 var $mListed;
53 /**
54 * Function name called by the default execute()
55 */
56 var $mFunction;
57 /**
58 * File which needs to be included before the function above can be called
59 */
60 var $mFile;
61 /**
62 * Whether or not this special page is being included from an article
63 */
64 var $mIncluding;
65 /**
66 * Whether the special page can be included in an article
67 */
68 var $mIncludable;
69 /**
70 * Query parameters that can be passed through redirects
71 */
72 var $mAllowedRedirectParams = array();
73 /**
74 * List of special pages, followed by parameters.
75 * If the only parameter is a string, that is the page name.
76 * Otherwise, it is an array. The format is one of:
77 ** array( 'SpecialPage', name, right )
78 ** array( 'IncludableSpecialPage', name, right, listed? )
79 ** array( 'UnlistedSpecialPage', name, right )
80 ** array( 'SpecialRedirectToSpecial', name, page to redirect to, special page param, ... )
81 */
82 static public $mList = array(
83 'DoubleRedirects' => array( 'SpecialPage', 'DoubleRedirects' ),
84 'BrokenRedirects' => array( 'SpecialPage', 'BrokenRedirects' ),
85 'Disambiguations' => array( 'SpecialPage', 'Disambiguations' ),
86
87 'Userlogin' => array( 'SpecialPage', 'Userlogin' ),
88 'Userlogout' => array( 'UnlistedSpecialPage', 'Userlogout' ),
89 'CreateAccount' => array( 'SpecialRedirectToSpecial', 'CreateAccount', 'Userlogin', 'signup', array( 'uselang' ) ),
90 'Preferences' => array( 'SpecialPage', 'Preferences' ),
91 'Watchlist' => array( 'SpecialPage', 'Watchlist' ),
92
93 'Recentchanges' => array( 'IncludableSpecialPage', 'Recentchanges' ),
94 'Upload' => array( 'SpecialPage', 'Upload' ),
95 'Imagelist' => array( 'SpecialPage', 'Imagelist' ),
96 'Newimages' => array( 'IncludableSpecialPage', 'Newimages' ),
97 'Listusers' => array( 'SpecialPage', 'Listusers' ),
98 'Listgrouprights' => 'SpecialListGroupRights',
99 'Statistics' => array( 'SpecialPage', 'Statistics' ),
100 'Randompage' => 'Randompage',
101 'Lonelypages' => array( 'SpecialPage', 'Lonelypages' ),
102 'Uncategorizedpages' => array( 'SpecialPage', 'Uncategorizedpages' ),
103 'Uncategorizedcategories' => array( 'SpecialPage', 'Uncategorizedcategories' ),
104 'Uncategorizedimages' => array( 'SpecialPage', 'Uncategorizedimages' ),
105 'Uncategorizedtemplates' => array( 'SpecialPage', 'Uncategorizedtemplates' ),
106 'Unusedcategories' => array( 'SpecialPage', 'Unusedcategories' ),
107 'Unusedimages' => array( 'SpecialPage', 'Unusedimages' ),
108 'Wantedpages' => array( 'IncludableSpecialPage', 'Wantedpages' ),
109 'Wantedcategories' => array( 'SpecialPage', 'Wantedcategories' ),
110 'Mostlinked' => array( 'SpecialPage', 'Mostlinked' ),
111 'Mostlinkedcategories' => array( 'SpecialPage', 'Mostlinkedcategories' ),
112 'Mostlinkedtemplates' => array( 'SpecialPage', 'Mostlinkedtemplates' ),
113 'Mostcategories' => array( 'SpecialPage', 'Mostcategories' ),
114 'Mostimages' => array( 'SpecialPage', 'Mostimages' ),
115 'Mostrevisions' => array( 'SpecialPage', 'Mostrevisions' ),
116 'Fewestrevisions' => array( 'SpecialPage', 'Fewestrevisions' ),
117 'Shortpages' => array( 'SpecialPage', 'Shortpages' ),
118 'Longpages' => array( 'SpecialPage', 'Longpages' ),
119 'Newpages' => array( 'IncludableSpecialPage', 'Newpages' ),
120 'Ancientpages' => array( 'SpecialPage', 'Ancientpages' ),
121 'Deadendpages' => array( 'SpecialPage', 'Deadendpages' ),
122 'Protectedpages' => array( 'SpecialPage', 'Protectedpages' ),
123 'Protectedtitles' => array( 'SpecialPage', 'Protectedtitles' ),
124 'Allpages' => array( 'IncludableSpecialPage', 'Allpages' ),
125 'Prefixindex' => array( 'IncludableSpecialPage', 'Prefixindex' ) ,
126 'Ipblocklist' => array( 'SpecialPage', 'Ipblocklist' ),
127 'Specialpages' => array( 'UnlistedSpecialPage', 'Specialpages' ),
128 'Contributions' => array( 'SpecialPage', 'Contributions' ),
129 'Emailuser' => array( 'UnlistedSpecialPage', 'Emailuser' ),
130 'Whatlinkshere' => array( 'SpecialPage', 'Whatlinkshere' ),
131 'Recentchangeslinked' => array( 'SpecialPage', 'Recentchangeslinked' ),
132 'Movepage' => array( 'UnlistedSpecialPage', 'Movepage' ),
133 'Blockme' => array( 'UnlistedSpecialPage', 'Blockme' ),
134 'Resetpass' => array( 'UnlistedSpecialPage', 'Resetpass' ),
135 'Booksources' => 'SpecialBookSources',
136 'Categories' => array( 'SpecialPage', 'Categories' ),
137 'Export' => array( 'SpecialPage', 'Export' ),
138 'Version' => array( 'SpecialPage', 'Version' ),
139 'Allmessages' => array( 'SpecialPage', 'Allmessages' ),
140 'Log' => array( 'SpecialPage', 'Log' ),
141 'Blockip' => array( 'SpecialPage', 'Blockip', 'block' ),
142 'Undelete' => array( 'SpecialPage', 'Undelete', 'deletedhistory' ),
143 'Import' => array( 'SpecialPage', 'Import', 'import' ),
144 'Lockdb' => array( 'SpecialPage', 'Lockdb', 'siteadmin' ),
145 'Unlockdb' => array( 'SpecialPage', 'Unlockdb', 'siteadmin' ),
146 'Userrights' => 'UserrightsPage',
147 'MIMEsearch' => array( 'SpecialPage', 'MIMEsearch' ),
148 'FileDuplicateSearch' => array( 'SpecialPage', 'FileDuplicateSearch' ),
149 'Unwatchedpages' => array( 'SpecialPage', 'Unwatchedpages', 'unwatchedpages' ),
150 'Listredirects' => array( 'SpecialPage', 'Listredirects' ),
151 'Revisiondelete' => array( 'UnlistedSpecialPage', 'Revisiondelete', 'deleterevision' ),
152 'Unusedtemplates' => array( 'SpecialPage', 'Unusedtemplates' ),
153 'Randomredirect' => 'SpecialRandomredirect',
154 'Withoutinterwiki' => array( 'SpecialPage', 'Withoutinterwiki' ),
155 'Filepath' => array( 'SpecialPage', 'Filepath' ),
156
157 'Mypage' => array( 'SpecialMypage' ),
158 'Mytalk' => array( 'SpecialMytalk' ),
159 'Mycontributions' => array( 'SpecialMycontributions' ),
160 'Listadmins' => array( 'SpecialRedirectToSpecial', 'Listadmins', 'Listusers', 'sysop' ),
161 'MergeHistory' => array( 'SpecialPage', 'MergeHistory', 'mergehistory' ),
162 'Listbots' => array( 'SpecialRedirectToSpecial', 'Listbots', 'Listusers', 'bot' ),
163 );
164
165 static public $mAliases;
166 static public $mListInitialised = false;
167
168 /**#@-*/
169
170 /**
171 * Initialise the special page list
172 * This must be called before accessing SpecialPage::$mList
173 */
174 static function initList() {
175 global $wgSpecialPages;
176 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
177
178 if ( self::$mListInitialised ) {
179 return;
180 }
181 wfProfileIn( __METHOD__ );
182
183 # Better to set this now, to avoid infinite recursion in carelessly written hooks
184 self::$mListInitialised = true;
185
186 if( !$wgDisableCounters ) {
187 self::$mList['Popularpages'] = array( 'SpecialPage', 'Popularpages' );
188 }
189
190 if( !$wgDisableInternalSearch ) {
191 self::$mList['Search'] = array( 'SpecialPage', 'Search' );
192 }
193
194 if( $wgEmailAuthentication ) {
195 self::$mList['Confirmemail'] = 'EmailConfirmation';
196 self::$mList['Invalidateemail'] = 'EmailInvalidation';
197 }
198
199 # Add extension special pages
200 self::$mList = array_merge( self::$mList, $wgSpecialPages );
201
202 # Run hooks
203 # This hook can be used to remove undesired built-in special pages
204 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
205 wfProfileOut( __METHOD__ );
206 }
207
208 static function initAliasList() {
209 if ( !is_null( self::$mAliases ) ) {
210 return;
211 }
212
213 global $wgContLang;
214 $aliases = $wgContLang->getSpecialPageAliases();
215 $missingPages = self::$mList;
216 self::$mAliases = array();
217 foreach ( $aliases as $realName => $aliasList ) {
218 foreach ( $aliasList as $alias ) {
219 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
220 }
221 unset( $missingPages[$realName] );
222 }
223 foreach ( $missingPages as $name => $stuff ) {
224 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
225 }
226 }
227
228 /**
229 * Given a special page alias, return the special page name.
230 * Returns false if there is no such alias.
231 */
232 static function resolveAlias( $alias ) {
233 global $wgContLang;
234
235 if ( !self::$mListInitialised ) self::initList();
236 if ( is_null( self::$mAliases ) ) self::initAliasList();
237 $caseFoldedAlias = $wgContLang->caseFold( $alias );
238 if ( isset( self::$mAliases[$caseFoldedAlias] ) ) {
239 return self::$mAliases[$caseFoldedAlias];
240 } else {
241 return false;
242 }
243 }
244
245 /**
246 * Given a special page name with a possible subpage, return an array
247 * where the first element is the special page name and the second is the
248 * subpage.
249 */
250 static function resolveAliasWithSubpage( $alias ) {
251 $bits = explode( '/', $alias, 2 );
252 $name = self::resolveAlias( $bits[0] );
253 if( !isset( $bits[1] ) ) { // bug 2087
254 $par = NULL;
255 } else {
256 $par = $bits[1];
257 }
258 return array( $name, $par );
259 }
260
261 /**
262 * Add a page to the list of valid special pages. This used to be the preferred
263 * method for adding special pages in extensions. It's now suggested that you add
264 * an associative record to $wgSpecialPages. This avoids autoloading SpecialPage.
265 *
266 * @param SpecialPage $page
267 * @static
268 */
269 static function addPage( &$page ) {
270 if ( !self::$mListInitialised ) {
271 self::initList();
272 }
273 self::$mList[$page->mName] = $page;
274 }
275
276 /**
277 * Add a page to a certain display group for Special:SpecialPages
278 *
279 * @param mixed $page (SpecialPage or string)
280 * @param string $group
281 * @static
282 */
283 static function setGroup( $page, $group ) {
284 global $wgSpecialPageGroups;
285 $name = is_object($page) ? $page->mName : $page;
286 $wgSpecialPageGroups[$name] = $group;
287 }
288
289 /**
290 * Add a page to a certain display group for Special:SpecialPages
291 *
292 * @param SpecialPage $page
293 * @static
294 */
295 static function getGroup( &$page ) {
296 global $wgSpecialPageGroups;
297 $group = isset($wgSpecialPageGroups[$page->mName]) ?
298 $wgSpecialPageGroups[$page->mName] : 'other';
299 return $group;
300 }
301
302 /**
303 * Remove a special page from the list
304 * Formerly used to disable expensive or dangerous special pages. The
305 * preferred method is now to add a SpecialPage_initList hook.
306 *
307 * @static
308 */
309 static function removePage( $name ) {
310 if ( !self::$mListInitialised ) {
311 self::initList();
312 }
313 unset( self::$mList[$name] );
314 }
315
316 /**
317 * Check if a given name exist as a special page or as a special page alias
318 * @param $name string: name of a special page
319 * @return boolean: true if a special page exists with this name
320 */
321 static function exists( $name ) {
322 global $wgContLang;
323 if ( !self::$mListInitialised ) {
324 self::initList();
325 }
326 if( !self::$mAliases ) {
327 self::initAliasList();
328 }
329
330 # Remove special pages inline parameters:
331 $bits = explode( '/', $name );
332 $name = $wgContLang->caseFold($bits[0]);
333
334 return
335 array_key_exists( $name, self::$mList )
336 or array_key_exists( $name, self::$mAliases )
337 ;
338 }
339
340 /**
341 * Find the object with a given name and return it (or NULL)
342 * @static
343 * @param string $name
344 */
345 static function getPage( $name ) {
346 if ( !self::$mListInitialised ) {
347 self::initList();
348 }
349 if ( array_key_exists( $name, self::$mList ) ) {
350 $rec = self::$mList[$name];
351 if ( is_string( $rec ) ) {
352 $className = $rec;
353 self::$mList[$name] = new $className;
354 } elseif ( is_array( $rec ) ) {
355 $className = array_shift( $rec );
356 self::$mList[$name] = wfCreateObject( $className, $rec );
357 }
358 return self::$mList[$name];
359 } else {
360 return NULL;
361 }
362 }
363
364 /**
365 * Get a special page with a given localised name, or NULL if there
366 * is no such special page.
367 */
368 static function getPageByAlias( $alias ) {
369 $realName = self::resolveAlias( $alias );
370 if ( $realName ) {
371 return self::getPage( $realName );
372 } else {
373 return NULL;
374 }
375 }
376
377 /**
378 * Return categorised listable special pages which are available
379 * for the current user, and everyone.
380 * @static
381 */
382 static function getUsablePages() {
383 global $wgUser;
384 if ( !self::$mListInitialised ) {
385 self::initList();
386 }
387 $pages = array();
388
389 foreach ( self::$mList as $name => $rec ) {
390 $page = self::getPage( $name );
391 if ( $page->isListed()
392 && (
393 !$page->isRestricted()
394 || $page->userCanExecute( $wgUser )
395 )
396 ) {
397 $pages[$name] = $page;
398 }
399 }
400 return $pages;
401 }
402
403 /**
404 * Return categorised listable special pages for all users
405 * @static
406 */
407 static function getRegularPages() {
408 if ( !self::$mListInitialised ) {
409 self::initList();
410 }
411 $pages = array();
412
413 foreach ( self::$mList as $name => $rec ) {
414 $page = self::getPage( $name );
415 if ( $page->isListed() && !$page->isRestricted() ) {
416 $pages[$name] = $page;
417 }
418 }
419 return $pages;
420 }
421
422 /**
423 * Return categorised listable special pages which are available
424 * for the current user, but not for everyone
425 * @static
426 */
427 static function getRestrictedPages() {
428 global $wgUser;
429 if ( !self::$mListInitialised ) {
430 self::initList();
431 }
432 $pages = array();
433
434 foreach ( self::$mList as $name => $rec ) {
435 $page = self::getPage( $name );
436 if (
437 $page->isListed()
438 && $page->isRestricted()
439 && $page->userCanExecute( $wgUser )
440 ) {
441 $pages[$name] = $page;
442 }
443 }
444 return $pages;
445 }
446
447 /**
448 * Execute a special page path.
449 * The path may contain parameters, e.g. Special:Name/Params
450 * Extracts the special page name and call the execute method, passing the parameters
451 *
452 * Returns a title object if the page is redirected, false if there was no such special
453 * page, and true if it was successful.
454 *
455 * @param $title a title object
456 * @param $including output is being captured for use in {{special:whatever}}
457 */
458 static function executePath( &$title, $including = false ) {
459 global $wgOut, $wgTitle, $wgRequest;
460 wfProfileIn( __METHOD__ );
461
462 # FIXME: redirects broken due to this call
463 $bits = explode( '/', $title->getDBkey(), 2 );
464 $name = $bits[0];
465 if( !isset( $bits[1] ) ) { // bug 2087
466 $par = NULL;
467 } else {
468 $par = $bits[1];
469 }
470 $page = SpecialPage::getPageByAlias( $name );
471 # Nonexistent?
472 if ( !$page ) {
473 if ( !$including ) {
474 $wgOut->setArticleRelated( false );
475 $wgOut->setRobotpolicy( 'noindex,nofollow' );
476 $wgOut->setStatusCode( 404 );
477 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
478 }
479 wfProfileOut( __METHOD__ );
480 return false;
481 }
482
483 # Check for redirect
484 if ( !$including ) {
485 $redirect = $page->getRedirect( $par );
486 if ( $redirect ) {
487 $query = $page->getRedirectQuery();
488 $url = $redirect->getFullUrl( $query );
489 $wgOut->redirect( $url );
490 wfProfileOut( __METHOD__ );
491 return $redirect;
492 }
493 }
494
495 # Redirect to canonical alias for GET commands
496 # Not for POST, we'd lose the post data, so it's best to just distribute
497 # the request. Such POST requests are possible for old extensions that
498 # generate self-links without being aware that their default name has
499 # changed.
500 if ( !$including && $name != $page->getLocalName() && !$wgRequest->wasPosted() ) {
501 $query = $_GET;
502 unset( $query['title'] );
503 $query = wfArrayToCGI( $query );
504 $title = $page->getTitle( $par );
505 $url = $title->getFullUrl( $query );
506 $wgOut->redirect( $url );
507 wfProfileOut( __METHOD__ );
508 return $redirect;
509 }
510
511 if ( $including && !$page->includable() ) {
512 wfProfileOut( __METHOD__ );
513 return false;
514 } elseif ( !$including ) {
515 $wgTitle = $page->getTitle();
516 }
517 $page->including( $including );
518
519 // Execute special page
520 $profName = 'Special:' . $page->getName();
521 wfProfileIn( $profName );
522 $page->execute( $par );
523 wfProfileOut( $profName );
524 wfProfileOut( __METHOD__ );
525 return true;
526 }
527
528 /**
529 * Just like executePath() except it returns the HTML instead of outputting it
530 * Returns false if there was no such special page, or a title object if it was
531 * a redirect.
532 * @static
533 */
534 static function capturePath( &$title ) {
535 global $wgOut, $wgTitle;
536
537 $oldTitle = $wgTitle;
538 $oldOut = $wgOut;
539 $wgOut = new OutputPage;
540
541 $ret = SpecialPage::executePath( $title, true );
542 if ( $ret === true ) {
543 $ret = $wgOut->getHTML();
544 }
545 $wgTitle = $oldTitle;
546 $wgOut = $oldOut;
547 return $ret;
548 }
549
550 /**
551 * Get the local name for a specified canonical name
552 *
553 * @param $name
554 * @param mixed $subpage Boolean false, or string
555 *
556 * @return string
557 */
558 static function getLocalNameFor( $name, $subpage = false ) {
559 global $wgContLang;
560 $aliases = $wgContLang->getSpecialPageAliases();
561 if ( isset( $aliases[$name][0] ) ) {
562 $name = $aliases[$name][0];
563 }
564 if ( $subpage !== false && !is_null( $subpage ) ) {
565 $name = "$name/$subpage";
566 }
567 return $name;
568 }
569
570 /**
571 * Get a localised Title object for a specified special page name
572 */
573 static function getTitleFor( $name, $subpage = false ) {
574 $name = self::getLocalNameFor( $name, $subpage );
575 if ( $name ) {
576 return Title::makeTitle( NS_SPECIAL, $name );
577 } else {
578 throw new MWException( "Invalid special page name \"$name\"" );
579 }
580 }
581
582 /**
583 * Get a localised Title object for a page name with a possibly unvalidated subpage
584 */
585 static function getSafeTitleFor( $name, $subpage = false ) {
586 $name = self::getLocalNameFor( $name, $subpage );
587 if ( $name ) {
588 return Title::makeTitleSafe( NS_SPECIAL, $name );
589 } else {
590 return null;
591 }
592 }
593
594 /**
595 * Get a title for a given alias
596 * @return Title or null if there is no such alias
597 */
598 static function getTitleForAlias( $alias ) {
599 $name = self::resolveAlias( $alias );
600 if ( $name ) {
601 return self::getTitleFor( $name );
602 } else {
603 return null;
604 }
605 }
606
607 /**
608 * Default constructor for special pages
609 * Derivative classes should call this from their constructor
610 * Note that if the user does not have the required level, an error message will
611 * be displayed by the default execute() method, without the global function ever
612 * being called.
613 *
614 * If you override execute(), you can recover the default behaviour with userCanExecute()
615 * and displayRestrictionError()
616 *
617 * @param string $name Name of the special page, as seen in links and URLs
618 * @param string $restriction User right required, e.g. "block" or "delete"
619 * @param boolean $listed Whether the page is listed in Special:Specialpages
620 * @param string $function Function called by execute(). By default it is constructed from $name
621 * @param string $file File which is included by execute(). It is also constructed from $name by default
622 */
623 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
624 $this->mName = $name;
625 $this->mRestriction = $restriction;
626 $this->mListed = $listed;
627 $this->mIncludable = $includable;
628 if ( $function == false ) {
629 $this->mFunction = 'wfSpecial'.$name;
630 } else {
631 $this->mFunction = $function;
632 }
633 if ( $file === 'default' ) {
634 $this->mFile = dirname(__FILE__) . "/Special{$name}.php";
635 } else {
636 $this->mFile = $file;
637 }
638 }
639
640 /**#@+
641 * Accessor
642 *
643 * @deprecated
644 */
645 function getName() { return $this->mName; }
646 function getRestriction() { return $this->mRestriction; }
647 function getFile() { return $this->mFile; }
648 function isListed() { return $this->mListed; }
649 /**#@-*/
650
651 /**#@+
652 * Accessor and mutator
653 */
654 function name( $x = NULL ) { return wfSetVar( $this->mName, $x ); }
655 function restrictions( $x = NULL) { return wfSetVar( $this->mRestrictions, $x ); }
656 function listed( $x = NULL) { return wfSetVar( $this->mListed, $x ); }
657 function func( $x = NULL) { return wfSetVar( $this->mFunction, $x ); }
658 function file( $x = NULL) { return wfSetVar( $this->mFile, $x ); }
659 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable, $x ); }
660 function including( $x = NULL ) { return wfSetVar( $this->mIncluding, $x ); }
661 /**#@-*/
662
663 /**
664 * Get the localised name of the special page
665 */
666 function getLocalName() {
667 if ( !isset( $this->mLocalName ) ) {
668 $this->mLocalName = self::getLocalNameFor( $this->mName );
669 }
670 return $this->mLocalName;
671 }
672
673 /**
674 * Can be overridden by subclasses with more complicated permissions
675 * schemes.
676 *
677 * @return bool Should the page be displayed with the restricted-access
678 * pages?
679 */
680 public function isRestricted() {
681 return $this->mRestriction != '';
682 }
683
684 /**
685 * Checks if the given user (identified by an object) can execute this
686 * special page (as defined by $mRestriction). Can be overridden by sub-
687 * classes with more complicated permissions schemes.
688 *
689 * @param User $user The user to check
690 * @return bool Does the user have permission to view the page?
691 */
692 public function userCanExecute( $user ) {
693 return $user->isAllowed( $this->mRestriction );
694 }
695
696 /**
697 * Output an error message telling the user what access level they have to have
698 */
699 function displayRestrictionError() {
700 global $wgOut;
701 $wgOut->permissionRequired( $this->mRestriction );
702 }
703
704 /**
705 * Sets headers - this should be called from the execute() method of all derived classes!
706 */
707 function setHeaders() {
708 global $wgOut;
709 $wgOut->setArticleRelated( false );
710 $wgOut->setRobotPolicy( "noindex,nofollow" );
711 $wgOut->setPageTitle( $this->getDescription() );
712 }
713
714 /**
715 * Default execute method
716 * Checks user permissions, calls the function given in mFunction
717 *
718 * This may be overridden by subclasses.
719 */
720 function execute( $par ) {
721 global $wgUser;
722
723 $this->setHeaders();
724
725 if ( $this->userCanExecute( $wgUser ) ) {
726 $func = $this->mFunction;
727 // only load file if the function does not exist
728 if(!is_callable($func) and $this->mFile) {
729 require_once( $this->mFile );
730 }
731 # FIXME: these hooks are broken for extensions and anything else that subclasses SpecialPage.
732 if ( wfRunHooks( 'SpecialPageExecuteBeforeHeader', array( &$this, &$par, &$func ) ) )
733 $this->outputHeader();
734 if ( ! wfRunHooks( 'SpecialPageExecuteBeforePage', array( &$this, &$par, &$func ) ) )
735 return;
736 call_user_func( $func, $par, $this );
737 if ( ! wfRunHooks( 'SpecialPageExecuteAfterPage', array( &$this, &$par, &$func ) ) )
738 return;
739 } else {
740 $this->displayRestrictionError();
741 }
742 }
743
744 function outputHeader() {
745 global $wgOut, $wgContLang;
746
747 $msg = $wgContLang->lc( $this->name() ) . '-summary';
748 $out = wfMsgNoTrans( $msg );
749 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() ) {
750 $wgOut->addWikiMsg( $msg );
751 }
752
753 }
754
755 # Returns the name that goes in the <h1> in the special page itself, and also the name that
756 # will be listed in Special:Specialpages
757 #
758 # Derived classes can override this, but usually it is easier to keep the default behaviour.
759 # Messages can be added at run-time, see MessageCache.php
760 function getDescription() {
761 return wfMsg( strtolower( $this->mName ) );
762 }
763
764 /**
765 * Get a self-referential title object
766 */
767 function getTitle( $subpage = false) {
768 return self::getTitleFor( $this->mName, $subpage );
769 }
770
771 /**
772 * Set whether this page is listed in Special:Specialpages, at run-time
773 */
774 function setListed( $listed ) {
775 return wfSetVar( $this->mListed, $listed );
776 }
777
778 /**
779 * If the special page is a redirect, then get the Title object it redirects to.
780 * False otherwise.
781 */
782 function getRedirect( $subpage ) {
783 return false;
784 }
785
786 /**
787 * Return part of the request string for a special redirect page
788 * This allows passing, e.g. action=history to Special:Mypage, etc.
789 *
790 * @return string
791 */
792 function getRedirectQuery() {
793 global $wgRequest;
794 $params = array();
795 foreach( $this->mAllowedRedirectParams as $arg ) {
796 if( $val = $wgRequest->getVal( $arg, false ) )
797 $params[] = $arg . '=' . $val;
798 }
799
800 return count( $params ) ? implode( '&', $params ) : false;
801 }
802 }
803
804 /**
805 * Shortcut to construct a special page which is unlisted by default
806 * @ingroup SpecialPage
807 */
808 class UnlistedSpecialPage extends SpecialPage
809 {
810 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
811 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
812 }
813 }
814
815 /**
816 * Shortcut to construct an includable special page
817 * @ingroup SpecialPage
818 */
819 class IncludableSpecialPage extends SpecialPage
820 {
821 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
822 SpecialPage::SpecialPage( $name, $restriction, $listed, $function, $file, true );
823 }
824 }
825
826 /**
827 * Shortcut to construct a special page alias.
828 * @ingroup SpecialPage
829 */
830 class SpecialRedirectToSpecial extends UnlistedSpecialPage {
831 var $redirName, $redirSubpage;
832
833 function __construct( $name, $redirName, $redirSubpage = false, $redirectParams = array() ) {
834 parent::__construct( $name );
835 $this->redirName = $redirName;
836 $this->redirSubpage = $redirSubpage;
837 $this->mAllowedRedirectParams = $redirectParams;
838 }
839
840 function getRedirect( $subpage ) {
841 if ( $this->redirSubpage === false ) {
842 return SpecialPage::getTitleFor( $this->redirName, $subpage );
843 } else {
844 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
845 }
846 }
847 }
848
849 /** SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
850 * are used to get user independant links pointing to the user page, talk
851 * page and list of contributions.
852 * This can let us cache a single copy of any generated content for all
853 * users.
854 */
855
856 /**
857 * Shortcut to construct a special page pointing to current user user's page.
858 * @ingroup SpecialPage
859 */
860 class SpecialMypage extends UnlistedSpecialPage {
861 function __construct() {
862 parent::__construct( 'Mypage' );
863 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro', 'section' );
864 }
865
866 function getRedirect( $subpage ) {
867 global $wgUser;
868 if ( strval( $subpage ) !== '' ) {
869 return Title::makeTitle( NS_USER, $wgUser->getName() . '/' . $subpage );
870 } else {
871 return Title::makeTitle( NS_USER, $wgUser->getName() );
872 }
873 }
874 }
875
876 /**
877 * Shortcut to construct a special page pointing to current user talk page.
878 * @ingroup SpecialPage
879 */
880 class SpecialMytalk extends UnlistedSpecialPage {
881 function __construct() {
882 parent::__construct( 'Mytalk' );
883 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro', 'section' );
884 }
885
886 function getRedirect( $subpage ) {
887 global $wgUser;
888 if ( strval( $subpage ) !== '' ) {
889 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() . '/' . $subpage );
890 } else {
891 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
892 }
893 }
894 }
895
896 /**
897 * Shortcut to construct a special page pointing to current user contributions.
898 * @ingroup SpecialPage
899 */
900 class SpecialMycontributions extends UnlistedSpecialPage {
901 function __construct() {
902 parent::__construct( 'Mycontributions' );
903 }
904
905 function getRedirect( $subpage ) {
906 global $wgUser;
907 return SpecialPage::getTitleFor( 'Contributions', $wgUser->getName() );
908 }
909 }