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