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