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