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