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