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