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