core changes for UploadWizard (merged from r73549 to HEAD in branches/uploadwizard...
[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' => 'SpecialProtectedpages',
96 'Protectedtitles' => 'SpecialProtectedtitles',
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' => 'SpecialCategories',
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' => 'IPBlockForm',
124 'Ipblocklist' => 'IPUnblockForm',
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 'Listadmins' => array( 'SpecialRedirectToSpecial', 'Listadmins', 'Listusers', 'sysop' ),
133 'Listbots' => array( 'SpecialRedirectToSpecial', 'Listbots', 'Listusers', 'bot' ),
134 'Activeusers' => 'SpecialActiveUsers',
135 'Userrights' => 'UserrightsPage',
136
137 # Recent changes and logs
138 'Newimages' => array( 'IncludableSpecialPage', 'Newimages' ),
139 'Log' => 'SpecialLog',
140 'Watchlist' => array( 'SpecialPage', 'Watchlist' ),
141 'Newpages' => 'SpecialNewpages',
142 'Recentchanges' => 'SpecialRecentchanges',
143 'Recentchangeslinked' => 'SpecialRecentchangeslinked',
144 'Tags' => 'SpecialTags',
145
146 # Media reports and uploads
147 'Listfiles' => array( 'SpecialPage', 'Listfiles' ),
148 'Filepath' => 'SpecialFilepath',
149 'MIMEsearch' => array( 'SpecialPage', 'MIMEsearch' ),
150 'FileDuplicateSearch' => array( 'SpecialPage', 'FileDuplicateSearch' ),
151 'Upload' => 'SpecialUpload',
152 'UploadStash' => 'SpecialUploadStash',
153
154 # Wiki data and tools
155 'Statistics' => 'SpecialStatistics',
156 'Allmessages' => 'SpecialAllmessages',
157 'Version' => 'SpecialVersion',
158 'Lockdb' => 'SpecialLockdb',
159 'Unlockdb' => 'SpecialUnlockdb',
160
161 # Redirecting special pages
162 'LinkSearch' => array( 'SpecialPage', 'LinkSearch' ),
163 'Randompage' => 'Randompage',
164 'Randomredirect' => 'SpecialRandomredirect',
165
166 # High use pages
167 'Mostlinkedcategories' => array( 'SpecialPage', 'Mostlinkedcategories' ),
168 'Mostimages' => array( 'SpecialPage', 'Mostimages' ),
169 'Mostlinked' => array( 'SpecialPage', 'Mostlinked' ),
170 'Mostlinkedtemplates' => array( 'SpecialPage', 'Mostlinkedtemplates' ),
171 'Mostcategories' => array( 'SpecialPage', 'Mostcategories' ),
172 'Mostrevisions' => array( 'SpecialPage', 'Mostrevisions' ),
173
174 # Page tools
175 'ComparePages' => 'SpecialComparePages',
176 'Export' => 'SpecialExport',
177 'Import' => 'SpecialImport',
178 'Undelete' => 'UndeleteForm',
179 'Whatlinkshere' => 'SpecialWhatlinkshere',
180 'MergeHistory' => 'SpecialMergeHistory',
181
182 # Other
183 'Booksources' => 'SpecialBookSources',
184
185 # Unlisted / redirects
186 'Blankpage' => 'SpecialBlankpage',
187 'Blockme' => 'SpecialBlockme',
188 'Emailuser' => 'SpecialEmailUser',
189 'Movepage' => 'MovePageForm',
190 'Mycontributions' => 'SpecialMycontributions',
191 'Mypage' => 'SpecialMypage',
192 'Mytalk' => 'SpecialMytalk',
193 'Revisiondelete' => 'SpecialRevisionDelete',
194 'RevisionMove' => 'SpecialRevisionMove',
195 'Specialpages' => 'SpecialSpecialpages',
196 'Userlogout' => 'SpecialUserlogout',
197 );
198
199 static public $mAliases;
200 static public $mListInitialised = false;
201
202 /**#@-*/
203
204 /**
205 * Initialise the special page list
206 * This must be called before accessing SpecialPage::$mList
207 */
208 static function initList() {
209 global $wgSpecialPages;
210 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
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 # Add extension special pages
234 self::$mList = array_merge( self::$mList, $wgSpecialPages );
235
236 # Run hooks
237 # This hook can be used to remove undesired built-in special pages
238 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
239 wfProfileOut( __METHOD__ );
240 }
241
242 static function initAliasList() {
243 if ( !is_null( self::$mAliases ) ) {
244 return;
245 }
246
247 global $wgContLang;
248 $aliases = $wgContLang->getSpecialPageAliases();
249 $missingPages = self::$mList;
250 self::$mAliases = array();
251 foreach ( $aliases as $realName => $aliasList ) {
252 foreach ( $aliasList as $alias ) {
253 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
254 }
255 unset( $missingPages[$realName] );
256 }
257 foreach ( $missingPages as $name => $stuff ) {
258 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
259 }
260 }
261
262 /**
263 * Given a special page alias, return the special page name.
264 * Returns false if there is no such alias.
265 *
266 * @param $alias String
267 * @return String or false
268 */
269 static function resolveAlias( $alias ) {
270 global $wgContLang;
271
272 if ( !self::$mListInitialised ) self::initList();
273 if ( is_null( self::$mAliases ) ) self::initAliasList();
274 $caseFoldedAlias = $wgContLang->caseFold( $alias );
275 if ( isset( self::$mAliases[$caseFoldedAlias] ) ) {
276 return self::$mAliases[$caseFoldedAlias];
277 } else {
278 return false;
279 }
280 }
281
282 /**
283 * Given a special page name with a possible subpage, return an array
284 * where the first element is the special page name and the second is the
285 * subpage.
286 *
287 * @param $alias String
288 * @return Array
289 */
290 static function resolveAliasWithSubpage( $alias ) {
291 $bits = explode( '/', $alias, 2 );
292 $name = self::resolveAlias( $bits[0] );
293 if( !isset( $bits[1] ) ) { // bug 2087
294 $par = null;
295 } else {
296 $par = $bits[1];
297 }
298 return array( $name, $par );
299 }
300
301 /**
302 * Add a page to the list of valid special pages. This used to be the preferred
303 * method for adding special pages in extensions. It's now suggested that you add
304 * an associative record to $wgSpecialPages. This avoids autoloading SpecialPage.
305 *
306 * @param $page SpecialPage
307 * Deprecated in 1.7, warnings in 1.17, might be removed in 1.20
308 */
309 static function addPage( &$page ) {
310 wfDeprecated( __METHOD__ );
311 if ( !self::$mListInitialised ) {
312 self::initList();
313 }
314 self::$mList[$page->mName] = $page;
315 }
316
317 /**
318 * Add a page to a certain display group for Special:SpecialPages
319 *
320 * @param $page Mixed: SpecialPage or string
321 * @param $group String
322 */
323 static function setGroup( $page, $group ) {
324 global $wgSpecialPageGroups;
325 $name = is_object($page) ? $page->mName : $page;
326 $wgSpecialPageGroups[$name] = $group;
327 }
328
329 /**
330 * Add a page to a certain display group for Special:SpecialPages
331 *
332 * @param $page SpecialPage
333 */
334 static function getGroup( &$page ) {
335 global $wgSpecialPageGroups;
336 static $specialPageGroupsCache = array();
337 if( isset($specialPageGroupsCache[$page->mName]) ) {
338 return $specialPageGroupsCache[$page->mName];
339 }
340 $group = wfMsg('specialpages-specialpagegroup-'.strtolower($page->mName));
341 if( $group == ''
342 || wfEmptyMsg('specialpages-specialpagegroup-'.strtolower($page->mName), $group ) ) {
343 $group = isset($wgSpecialPageGroups[$page->mName])
344 ? $wgSpecialPageGroups[$page->mName]
345 : '-';
346 }
347 if( $group == '-' ) $group = 'other';
348 $specialPageGroupsCache[$page->mName] = $group;
349 return $group;
350 }
351
352 /**
353 * Remove a special page from the list
354 * Formerly used to disable expensive or dangerous special pages. The
355 * preferred method is now to add a SpecialPage_initList hook.
356 */
357 static function removePage( $name ) {
358 if ( !self::$mListInitialised ) {
359 self::initList();
360 }
361 unset( self::$mList[$name] );
362 }
363
364 /**
365 * Check if a given name exist as a special page or as a special page alias
366 *
367 * @param $name String: name of a special page
368 * @return Boolean: true if a special page exists with this name
369 */
370 static function exists( $name ) {
371 global $wgContLang;
372 if ( !self::$mListInitialised ) {
373 self::initList();
374 }
375 if( !self::$mAliases ) {
376 self::initAliasList();
377 }
378
379 # Remove special pages inline parameters:
380 $bits = explode( '/', $name );
381 $name = $wgContLang->caseFold($bits[0]);
382
383 return
384 array_key_exists( $name, self::$mList )
385 or array_key_exists( $name, self::$mAliases )
386 ;
387 }
388
389 /**
390 * Find the object with a given name and return it (or NULL)
391 *
392 * @param $name String
393 * @return SpecialPage object or null if the page doesn't exist
394 */
395 static function getPage( $name ) {
396 if ( !self::$mListInitialised ) {
397 self::initList();
398 }
399 if ( array_key_exists( $name, self::$mList ) ) {
400 $rec = self::$mList[$name];
401 if ( is_string( $rec ) ) {
402 $className = $rec;
403 self::$mList[$name] = new $className;
404 } elseif ( is_array( $rec ) ) {
405 $className = array_shift( $rec );
406 self::$mList[$name] = wfCreateObject( $className, $rec );
407 }
408 return self::$mList[$name];
409 } else {
410 return null;
411 }
412 }
413
414 /**
415 * Get a special page with a given localised name, or NULL if there
416 * is no such special page.
417 *
418 * @return SpecialPage object or null if the page doesn't exist
419 */
420 static function getPageByAlias( $alias ) {
421 $realName = self::resolveAlias( $alias );
422 if ( $realName ) {
423 return self::getPage( $realName );
424 } else {
425 return null;
426 }
427 }
428
429 /**
430 * Return categorised listable special pages which are available
431 * for the current user, and everyone.
432 *
433 * @return Associative array mapping page's name to its SpecialPage object
434 */
435 static function getUsablePages() {
436 global $wgUser;
437 if ( !self::$mListInitialised ) {
438 self::initList();
439 }
440 $pages = array();
441
442 foreach ( self::$mList as $name => $rec ) {
443 $page = self::getPage( $name );
444 if ( $page->isListed()
445 && (
446 !$page->isRestricted()
447 || $page->userCanExecute( $wgUser )
448 )
449 ) {
450 $pages[$name] = $page;
451 }
452 }
453 return $pages;
454 }
455
456 /**
457 * Return categorised listable special pages for all users
458 *
459 * @return Associative array mapping page's name to its SpecialPage object
460 */
461 static function getRegularPages() {
462 if ( !self::$mListInitialised ) {
463 self::initList();
464 }
465 $pages = array();
466
467 foreach ( self::$mList as $name => $rec ) {
468 $page = self::getPage( $name );
469 if ( $page->isListed() && !$page->isRestricted() ) {
470 $pages[$name] = $page;
471 }
472 }
473 return $pages;
474 }
475
476 /**
477 * Return categorised listable special pages which are available
478 * for the current user, but not for everyone
479 *
480 * @return Associative array mapping page's name to its SpecialPage object
481 */
482 static function getRestrictedPages() {
483 global $wgUser;
484 if( !self::$mListInitialised ) {
485 self::initList();
486 }
487 $pages = array();
488
489 foreach( self::$mList as $name => $rec ) {
490 $page = self::getPage( $name );
491 if(
492 $page->isListed()
493 && $page->isRestricted()
494 && $page->userCanExecute( $wgUser )
495 ) {
496 $pages[$name] = $page;
497 }
498 }
499 return $pages;
500 }
501
502 /**
503 * Execute a special page path.
504 * The path may contain parameters, e.g. Special:Name/Params
505 * Extracts the special page name and call the execute method, passing the parameters
506 *
507 * Returns a title object if the page is redirected, false if there was no such special
508 * page, and true if it was successful.
509 *
510 * @param $title a title object
511 * @param $including output is being captured for use in {{special:whatever}}
512 */
513 static function executePath( &$title, $including = false ) {
514 global $wgOut, $wgTitle, $wgRequest;
515 wfProfileIn( __METHOD__ );
516
517 # FIXME: redirects broken due to this call
518 $bits = explode( '/', $title->getDBkey(), 2 );
519 $name = $bits[0];
520 if( !isset( $bits[1] ) ) { // bug 2087
521 $par = null;
522 } else {
523 $par = $bits[1];
524 }
525 $page = SpecialPage::getPageByAlias( $name );
526 # Nonexistent?
527 if ( !$page ) {
528 if ( !$including ) {
529 $wgOut->setArticleRelated( false );
530 $wgOut->setRobotPolicy( 'noindex,nofollow' );
531 $wgOut->setStatusCode( 404 );
532 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
533 }
534 wfProfileOut( __METHOD__ );
535 return false;
536 }
537
538 # Check for redirect
539 if ( !$including ) {
540 $redirect = $page->getRedirect( $par );
541 if ( $redirect ) {
542 $query = $page->getRedirectQuery();
543 $url = $redirect->getFullUrl( $query );
544 $wgOut->redirect( $url );
545 wfProfileOut( __METHOD__ );
546 return $redirect;
547 }
548 }
549
550 # Redirect to canonical alias for GET commands
551 # Not for POST, we'd lose the post data, so it's best to just distribute
552 # the request. Such POST requests are possible for old extensions that
553 # generate self-links without being aware that their default name has
554 # changed.
555 if ( !$including && $name != $page->getLocalName() && !$wgRequest->wasPosted() ) {
556 $query = $_GET;
557 unset( $query['title'] );
558 $query = wfArrayToCGI( $query );
559 $title = $page->getTitle( $par );
560 $url = $title->getFullUrl( $query );
561 $wgOut->redirect( $url );
562 wfProfileOut( __METHOD__ );
563 return $redirect;
564 }
565
566 if ( $including && !$page->includable() ) {
567 wfProfileOut( __METHOD__ );
568 return false;
569 } elseif ( !$including ) {
570 $wgTitle = $page->getTitle();
571 }
572 $page->including( $including );
573
574 // Execute special page
575 $profName = 'Special:' . $page->getName();
576 wfProfileIn( $profName );
577 $page->execute( $par );
578 wfProfileOut( $profName );
579 wfProfileOut( __METHOD__ );
580 return true;
581 }
582
583 /**
584 * Just like executePath() except it returns the HTML instead of outputting it
585 * Returns false if there was no such special page, or a title object if it was
586 * a redirect.
587 *
588 * @return String: HTML fragment
589 */
590 static function capturePath( &$title ) {
591 global $wgOut, $wgTitle;
592
593 $oldTitle = $wgTitle;
594 $oldOut = $wgOut;
595 $wgOut = new OutputPage;
596 $wgOut->setTitle( $title );
597
598 $ret = SpecialPage::executePath( $title, true );
599 if ( $ret === true ) {
600 $ret = $wgOut->getHTML();
601 }
602 $wgTitle = $oldTitle;
603 $wgOut = $oldOut;
604 return $ret;
605 }
606
607 /**
608 * Get the local name for a specified canonical name
609 *
610 * @param $name String
611 * @param $subpage Mixed: boolean false, or string
612 *
613 * @return String
614 */
615 static function getLocalNameFor( $name, $subpage = false ) {
616 global $wgContLang;
617 $aliases = $wgContLang->getSpecialPageAliases();
618 if ( isset( $aliases[$name][0] ) ) {
619 $name = $aliases[$name][0];
620 } else {
621 // Try harder in case someone misspelled the correct casing
622 $found = false;
623 foreach ( $aliases as $n => $values ) {
624 if ( strcasecmp( $name, $n ) === 0 ) {
625 wfWarn( "Found alias defined for $n when searching for" .
626 "special page aliases for $name. Case mismatch?" );
627 $name = $values[0];
628 $found = true;
629 break;
630 }
631 }
632 if ( !$found ) {
633 wfWarn( "Did not find alias for special page '$name'. " .
634 "Perhaps no aliases are defined for it?" );
635 }
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 __construct( $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 __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
914 parent::__construct( $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 __construct( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
925 parent::__construct( $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 }