On second thought, we should be using a lowercase form of the special page name.
[lhc/web/wiklou.git] / includes / SpecialPage.php
1 <?php
2 /**
3 * SpecialPage: handling special pages and lists thereof.
4 *
5 * To add a special page in an extension, add to $wgSpecialPages either
6 * an object instance or an array containing the name and constructor
7 * parameters. The latter is preferred for performance reasons.
8 *
9 * The object instantiated must be either an instance of SpecialPage or a
10 * sub-class thereof. It must have an execute() method, which sends the HTML
11 * for the special page to $wgOut. The parent class has an execute() method
12 * which distributes the call to the historical global functions. Additionally,
13 * execute() also checks if the user has the necessary access privileges
14 * and bails out if not.
15 *
16 * To add a core special page, use the similar static list in
17 * SpecialPage::$mList. To remove a core static special page at runtime, use
18 * a SpecialPage_initList hook.
19 *
20 * @file
21 * @ingroup SpecialPage
22 * @defgroup SpecialPage SpecialPage
23 */
24
25 /**
26 * Parent special page class, also static functions for handling the special
27 * page list.
28 * @ingroup SpecialPage
29 */
30 class SpecialPage
31 {
32 /**#@+
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 static $specialPageGroupsCache = array();
298 if( isset($specialPageGroupsCache[$page->mName]) ) {
299 return $specialPageGroupsCache[$page->mName];
300 }
301 $group = wfMsg('specialpages-specialpagegroup-'.strtolower($page->mName));
302 if( $group == ''
303 || wfEmptyMsg('specialpages-specialpagegroup-'.strtolower($page->mName), $group ) ) {
304 $group = isset($wgSpecialPageGroups[$page->mName])
305 ? $wgSpecialPageGroups[$page->mName]
306 : '-';
307 }
308 if( $group == '-' ) $group = 'other';
309 $specialPageGroupsCache[$page->mName] = $group;
310 return $group;
311 }
312
313 /**
314 * Remove a special page from the list
315 * Formerly used to disable expensive or dangerous special pages. The
316 * preferred method is now to add a SpecialPage_initList hook.
317 *
318 * @static
319 */
320 static function removePage( $name ) {
321 if ( !self::$mListInitialised ) {
322 self::initList();
323 }
324 unset( self::$mList[$name] );
325 }
326
327 /**
328 * Check if a given name exist as a special page or as a special page alias
329 * @param $name string: name of a special page
330 * @return boolean: true if a special page exists with this name
331 */
332 static function exists( $name ) {
333 global $wgContLang;
334 if ( !self::$mListInitialised ) {
335 self::initList();
336 }
337 if( !self::$mAliases ) {
338 self::initAliasList();
339 }
340
341 # Remove special pages inline parameters:
342 $bits = explode( '/', $name );
343 $name = $wgContLang->caseFold($bits[0]);
344
345 return
346 array_key_exists( $name, self::$mList )
347 or array_key_exists( $name, self::$mAliases )
348 ;
349 }
350
351 /**
352 * Find the object with a given name and return it (or NULL)
353 * @static
354 * @param string $name
355 */
356 static function getPage( $name ) {
357 if ( !self::$mListInitialised ) {
358 self::initList();
359 }
360 if ( array_key_exists( $name, self::$mList ) ) {
361 $rec = self::$mList[$name];
362 if ( is_string( $rec ) ) {
363 $className = $rec;
364 self::$mList[$name] = new $className;
365 } elseif ( is_array( $rec ) ) {
366 $className = array_shift( $rec );
367 self::$mList[$name] = wfCreateObject( $className, $rec );
368 }
369 return self::$mList[$name];
370 } else {
371 return NULL;
372 }
373 }
374
375 /**
376 * Get a special page with a given localised name, or NULL if there
377 * is no such special page.
378 */
379 static function getPageByAlias( $alias ) {
380 $realName = self::resolveAlias( $alias );
381 if ( $realName ) {
382 return self::getPage( $realName );
383 } else {
384 return NULL;
385 }
386 }
387
388 /**
389 * Return categorised listable special pages which are available
390 * for the current user, and everyone.
391 * @static
392 */
393 static function getUsablePages() {
394 global $wgUser;
395 if ( !self::$mListInitialised ) {
396 self::initList();
397 }
398 $pages = array();
399
400 foreach ( self::$mList as $name => $rec ) {
401 $page = self::getPage( $name );
402 if ( $page->isListed()
403 && (
404 !$page->isRestricted()
405 || $page->userCanExecute( $wgUser )
406 )
407 ) {
408 $pages[$name] = $page;
409 }
410 }
411 return $pages;
412 }
413
414 /**
415 * Return categorised listable special pages for all users
416 * @static
417 */
418 static function getRegularPages() {
419 if ( !self::$mListInitialised ) {
420 self::initList();
421 }
422 $pages = array();
423
424 foreach ( self::$mList as $name => $rec ) {
425 $page = self::getPage( $name );
426 if ( $page->isListed() && !$page->isRestricted() ) {
427 $pages[$name] = $page;
428 }
429 }
430 return $pages;
431 }
432
433 /**
434 * Return categorised listable special pages which are available
435 * for the current user, but not for everyone
436 * @static
437 */
438 static function getRestrictedPages() {
439 global $wgUser;
440 if ( !self::$mListInitialised ) {
441 self::initList();
442 }
443 $pages = array();
444
445 foreach ( self::$mList as $name => $rec ) {
446 $page = self::getPage( $name );
447 if (
448 $page->isListed()
449 && $page->isRestricted()
450 && $page->userCanExecute( $wgUser )
451 ) {
452 $pages[$name] = $page;
453 }
454 }
455 return $pages;
456 }
457
458 /**
459 * Execute a special page path.
460 * The path may contain parameters, e.g. Special:Name/Params
461 * Extracts the special page name and call the execute method, passing the parameters
462 *
463 * Returns a title object if the page is redirected, false if there was no such special
464 * page, and true if it was successful.
465 *
466 * @param $title a title object
467 * @param $including output is being captured for use in {{special:whatever}}
468 */
469 static function executePath( &$title, $including = false ) {
470 global $wgOut, $wgTitle, $wgRequest;
471 wfProfileIn( __METHOD__ );
472
473 # FIXME: redirects broken due to this call
474 $bits = explode( '/', $title->getDBkey(), 2 );
475 $name = $bits[0];
476 if( !isset( $bits[1] ) ) { // bug 2087
477 $par = NULL;
478 } else {
479 $par = $bits[1];
480 }
481 $page = SpecialPage::getPageByAlias( $name );
482 # Nonexistent?
483 if ( !$page ) {
484 if ( !$including ) {
485 $wgOut->setArticleRelated( false );
486 $wgOut->setRobotpolicy( 'noindex,nofollow' );
487 $wgOut->setStatusCode( 404 );
488 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
489 }
490 wfProfileOut( __METHOD__ );
491 return false;
492 }
493
494 # Check for redirect
495 if ( !$including ) {
496 $redirect = $page->getRedirect( $par );
497 if ( $redirect ) {
498 $query = $page->getRedirectQuery();
499 $url = $redirect->getFullUrl( $query );
500 $wgOut->redirect( $url );
501 wfProfileOut( __METHOD__ );
502 return $redirect;
503 }
504 }
505
506 # Redirect to canonical alias for GET commands
507 # Not for POST, we'd lose the post data, so it's best to just distribute
508 # the request. Such POST requests are possible for old extensions that
509 # generate self-links without being aware that their default name has
510 # changed.
511 if ( !$including && $name != $page->getLocalName() && !$wgRequest->wasPosted() ) {
512 $query = $_GET;
513 unset( $query['title'] );
514 $query = wfArrayToCGI( $query );
515 $title = $page->getTitle( $par );
516 $url = $title->getFullUrl( $query );
517 $wgOut->redirect( $url );
518 wfProfileOut( __METHOD__ );
519 return $redirect;
520 }
521
522 if ( $including && !$page->includable() ) {
523 wfProfileOut( __METHOD__ );
524 return false;
525 } elseif ( !$including ) {
526 $wgTitle = $page->getTitle();
527 }
528 $page->including( $including );
529
530 // Execute special page
531 $profName = 'Special:' . $page->getName();
532 wfProfileIn( $profName );
533 $page->execute( $par );
534 wfProfileOut( $profName );
535 wfProfileOut( __METHOD__ );
536 return true;
537 }
538
539 /**
540 * Just like executePath() except it returns the HTML instead of outputting it
541 * Returns false if there was no such special page, or a title object if it was
542 * a redirect.
543 * @static
544 */
545 static function capturePath( &$title ) {
546 global $wgOut, $wgTitle;
547
548 $oldTitle = $wgTitle;
549 $oldOut = $wgOut;
550 $wgOut = new OutputPage;
551
552 $ret = SpecialPage::executePath( $title, true );
553 if ( $ret === true ) {
554 $ret = $wgOut->getHTML();
555 }
556 $wgTitle = $oldTitle;
557 $wgOut = $oldOut;
558 return $ret;
559 }
560
561 /**
562 * Get the local name for a specified canonical name
563 *
564 * @param $name
565 * @param mixed $subpage Boolean false, or string
566 *
567 * @return string
568 */
569 static function getLocalNameFor( $name, $subpage = false ) {
570 global $wgContLang;
571 $aliases = $wgContLang->getSpecialPageAliases();
572 if ( isset( $aliases[$name][0] ) ) {
573 $name = $aliases[$name][0];
574 }
575 if ( $subpage !== false && !is_null( $subpage ) ) {
576 $name = "$name/$subpage";
577 }
578 return $name;
579 }
580
581 /**
582 * Get a localised Title object for a specified special page name
583 */
584 static function getTitleFor( $name, $subpage = false ) {
585 $name = self::getLocalNameFor( $name, $subpage );
586 if ( $name ) {
587 return Title::makeTitle( NS_SPECIAL, $name );
588 } else {
589 throw new MWException( "Invalid special page name \"$name\"" );
590 }
591 }
592
593 /**
594 * Get a localised Title object for a page name with a possibly unvalidated subpage
595 */
596 static function getSafeTitleFor( $name, $subpage = false ) {
597 $name = self::getLocalNameFor( $name, $subpage );
598 if ( $name ) {
599 return Title::makeTitleSafe( NS_SPECIAL, $name );
600 } else {
601 return null;
602 }
603 }
604
605 /**
606 * Get a title for a given alias
607 * @return Title or null if there is no such alias
608 */
609 static function getTitleForAlias( $alias ) {
610 $name = self::resolveAlias( $alias );
611 if ( $name ) {
612 return self::getTitleFor( $name );
613 } else {
614 return null;
615 }
616 }
617
618 /**
619 * Default constructor for special pages
620 * Derivative classes should call this from their constructor
621 * Note that if the user does not have the required level, an error message will
622 * be displayed by the default execute() method, without the global function ever
623 * being called.
624 *
625 * If you override execute(), you can recover the default behaviour with userCanExecute()
626 * and displayRestrictionError()
627 *
628 * @param string $name Name of the special page, as seen in links and URLs
629 * @param string $restriction User right required, e.g. "block" or "delete"
630 * @param boolean $listed Whether the page is listed in Special:Specialpages
631 * @param string $function Function called by execute(). By default it is constructed from $name
632 * @param string $file File which is included by execute(). It is also constructed from $name by default
633 */
634 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
635 $this->mName = $name;
636 $this->mRestriction = $restriction;
637 $this->mListed = $listed;
638 $this->mIncludable = $includable;
639 if ( $function == false ) {
640 $this->mFunction = 'wfSpecial'.$name;
641 } else {
642 $this->mFunction = $function;
643 }
644 if ( $file === 'default' ) {
645 $this->mFile = dirname(__FILE__) . "/Special{$name}.php";
646 } else {
647 $this->mFile = $file;
648 }
649 }
650
651 /**#@+
652 * Accessor
653 *
654 * @deprecated
655 */
656 function getName() { return $this->mName; }
657 function getRestriction() { return $this->mRestriction; }
658 function getFile() { return $this->mFile; }
659 function isListed() { return $this->mListed; }
660 /**#@-*/
661
662 /**#@+
663 * Accessor and mutator
664 */
665 function name( $x = NULL ) { return wfSetVar( $this->mName, $x ); }
666 function restrictions( $x = NULL) { return wfSetVar( $this->mRestrictions, $x ); }
667 function listed( $x = NULL) { return wfSetVar( $this->mListed, $x ); }
668 function func( $x = NULL) { return wfSetVar( $this->mFunction, $x ); }
669 function file( $x = NULL) { return wfSetVar( $this->mFile, $x ); }
670 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable, $x ); }
671 function including( $x = NULL ) { return wfSetVar( $this->mIncluding, $x ); }
672 /**#@-*/
673
674 /**
675 * Get the localised name of the special page
676 */
677 function getLocalName() {
678 if ( !isset( $this->mLocalName ) ) {
679 $this->mLocalName = self::getLocalNameFor( $this->mName );
680 }
681 return $this->mLocalName;
682 }
683
684 /**
685 * Can be overridden by subclasses with more complicated permissions
686 * schemes.
687 *
688 * @return bool Should the page be displayed with the restricted-access
689 * pages?
690 */
691 public function isRestricted() {
692 return $this->mRestriction != '';
693 }
694
695 /**
696 * Checks if the given user (identified by an object) can execute this
697 * special page (as defined by $mRestriction). Can be overridden by sub-
698 * classes with more complicated permissions schemes.
699 *
700 * @param User $user The user to check
701 * @return bool Does the user have permission to view the page?
702 */
703 public function userCanExecute( $user ) {
704 return $user->isAllowed( $this->mRestriction );
705 }
706
707 /**
708 * Output an error message telling the user what access level they have to have
709 */
710 function displayRestrictionError() {
711 global $wgOut;
712 $wgOut->permissionRequired( $this->mRestriction );
713 }
714
715 /**
716 * Sets headers - this should be called from the execute() method of all derived classes!
717 */
718 function setHeaders() {
719 global $wgOut;
720 $wgOut->setArticleRelated( false );
721 $wgOut->setRobotPolicy( "noindex,nofollow" );
722 $wgOut->setPageTitle( $this->getDescription() );
723 }
724
725 /**
726 * Default execute method
727 * Checks user permissions, calls the function given in mFunction
728 *
729 * This may be overridden by subclasses.
730 */
731 function execute( $par ) {
732 global $wgUser;
733
734 $this->setHeaders();
735
736 if ( $this->userCanExecute( $wgUser ) ) {
737 $func = $this->mFunction;
738 // only load file if the function does not exist
739 if(!is_callable($func) and $this->mFile) {
740 require_once( $this->mFile );
741 }
742 # FIXME: these hooks are broken for extensions and anything else that subclasses SpecialPage.
743 if ( wfRunHooks( 'SpecialPageExecuteBeforeHeader', array( &$this, &$par, &$func ) ) )
744 $this->outputHeader();
745 if ( ! wfRunHooks( 'SpecialPageExecuteBeforePage', array( &$this, &$par, &$func ) ) )
746 return;
747 call_user_func( $func, $par, $this );
748 if ( ! wfRunHooks( 'SpecialPageExecuteAfterPage', array( &$this, &$par, &$func ) ) )
749 return;
750 } else {
751 $this->displayRestrictionError();
752 }
753 }
754
755 function outputHeader() {
756 global $wgOut, $wgContLang;
757
758 $msg = $wgContLang->lc( $this->name() ) . '-summary';
759 $out = wfMsgNoTrans( $msg );
760 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() ) {
761 $wgOut->addWikiMsg( $msg );
762 }
763
764 }
765
766 # Returns the name that goes in the <h1> in the special page itself, and also the name that
767 # will be listed in Special:Specialpages
768 #
769 # Derived classes can override this, but usually it is easier to keep the default behaviour.
770 # Messages can be added at run-time, see MessageCache.php
771 function getDescription() {
772 return wfMsg( strtolower( $this->mName ) );
773 }
774
775 /**
776 * Get a self-referential title object
777 */
778 function getTitle( $subpage = false) {
779 return self::getTitleFor( $this->mName, $subpage );
780 }
781
782 /**
783 * Set whether this page is listed in Special:Specialpages, at run-time
784 */
785 function setListed( $listed ) {
786 return wfSetVar( $this->mListed, $listed );
787 }
788
789 /**
790 * If the special page is a redirect, then get the Title object it redirects to.
791 * False otherwise.
792 */
793 function getRedirect( $subpage ) {
794 return false;
795 }
796
797 /**
798 * Return part of the request string for a special redirect page
799 * This allows passing, e.g. action=history to Special:Mypage, etc.
800 *
801 * @return string
802 */
803 function getRedirectQuery() {
804 global $wgRequest;
805 $params = array();
806 foreach( $this->mAllowedRedirectParams as $arg ) {
807 if( $val = $wgRequest->getVal( $arg, false ) )
808 $params[] = $arg . '=' . $val;
809 }
810
811 return count( $params ) ? implode( '&', $params ) : false;
812 }
813 }
814
815 /**
816 * Shortcut to construct a special page which is unlisted by default
817 * @ingroup SpecialPage
818 */
819 class UnlistedSpecialPage extends SpecialPage
820 {
821 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
822 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
823 }
824 }
825
826 /**
827 * Shortcut to construct an includable special page
828 * @ingroup SpecialPage
829 */
830 class IncludableSpecialPage extends SpecialPage
831 {
832 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
833 SpecialPage::SpecialPage( $name, $restriction, $listed, $function, $file, true );
834 }
835 }
836
837 /**
838 * Shortcut to construct a special page alias.
839 * @ingroup SpecialPage
840 */
841 class SpecialRedirectToSpecial extends UnlistedSpecialPage {
842 var $redirName, $redirSubpage;
843
844 function __construct( $name, $redirName, $redirSubpage = false, $redirectParams = array() ) {
845 parent::__construct( $name );
846 $this->redirName = $redirName;
847 $this->redirSubpage = $redirSubpage;
848 $this->mAllowedRedirectParams = $redirectParams;
849 }
850
851 function getRedirect( $subpage ) {
852 if ( $this->redirSubpage === false ) {
853 return SpecialPage::getTitleFor( $this->redirName, $subpage );
854 } else {
855 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
856 }
857 }
858 }
859
860 /** SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
861 * are used to get user independant links pointing to the user page, talk
862 * page and list of contributions.
863 * This can let us cache a single copy of any generated content for all
864 * users.
865 */
866
867 /**
868 * Shortcut to construct a special page pointing to current user user's page.
869 * @ingroup SpecialPage
870 */
871 class SpecialMypage extends UnlistedSpecialPage {
872 function __construct() {
873 parent::__construct( 'Mypage' );
874 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro', 'section' );
875 }
876
877 function getRedirect( $subpage ) {
878 global $wgUser;
879 if ( strval( $subpage ) !== '' ) {
880 return Title::makeTitle( NS_USER, $wgUser->getName() . '/' . $subpage );
881 } else {
882 return Title::makeTitle( NS_USER, $wgUser->getName() );
883 }
884 }
885 }
886
887 /**
888 * Shortcut to construct a special page pointing to current user talk page.
889 * @ingroup SpecialPage
890 */
891 class SpecialMytalk extends UnlistedSpecialPage {
892 function __construct() {
893 parent::__construct( 'Mytalk' );
894 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro', 'section' );
895 }
896
897 function getRedirect( $subpage ) {
898 global $wgUser;
899 if ( strval( $subpage ) !== '' ) {
900 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() . '/' . $subpage );
901 } else {
902 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
903 }
904 }
905 }
906
907 /**
908 * Shortcut to construct a special page pointing to current user contributions.
909 * @ingroup SpecialPage
910 */
911 class SpecialMycontributions extends UnlistedSpecialPage {
912 function __construct() {
913 parent::__construct( 'Mycontributions' );
914 }
915
916 function getRedirect( $subpage ) {
917 global $wgUser;
918 return SpecialPage::getTitleFor( 'Contributions', $wgUser->getName() );
919 }
920 }