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