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