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