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