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