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