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