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