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