Add $robotPolicy parameter to SpecialPage::setHeaders()
[lhc/web/wiklou.git] / includes / SpecialPage.php
1 <?php
2 /**
3 * Parent class for all special pages.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Parent special page class, also static functions for handling the special
26 * page list.
27 * @ingroup SpecialPage
28 */
29 class SpecialPage {
30 // The canonical name of this special page
31 // Also used for the default <h1> heading, @see getDescription()
32 protected $mName;
33
34 // The local name of this special page
35 private $mLocalName;
36
37 // Minimum user level required to access this page, or "" for anyone.
38 // Also used to categorise the pages in Special:Specialpages
39 private $mRestriction;
40
41 // Listed in Special:Specialpages?
42 private $mListed;
43
44 // Function name called by the default execute()
45 private $mFunction;
46
47 // File which needs to be included before the function above can be called
48 private $mFile;
49
50 // Whether or not this special page is being included from an article
51 protected $mIncluding;
52
53 // Whether the special page can be included in an article
54 protected $mIncludable;
55
56 /**
57 * Current request context
58 * @var IContextSource
59 */
60 protected $mContext;
61
62 /**
63 * Initialise the special page list
64 * This must be called before accessing SpecialPage::$mList
65 * @deprecated since 1.18
66 */
67 static function initList() {
68 wfDeprecated( __METHOD__, '1.18' );
69 // Noop
70 }
71
72 /**
73 * @deprecated since 1.18
74 */
75 static function initAliasList() {
76 wfDeprecated( __METHOD__, '1.18' );
77 // Noop
78 }
79
80 /**
81 * Given a special page alias, return the special page name.
82 * Returns false if there is no such alias.
83 *
84 * @param $alias String
85 * @return String or false
86 * @deprecated since 1.18 call SpecialPageFactory method directly
87 */
88 static function resolveAlias( $alias ) {
89 wfDeprecated( __METHOD__, '1.18' );
90 list( $name, /*...*/ ) = SpecialPageFactory::resolveAlias( $alias );
91 return $name;
92 }
93
94 /**
95 * Given a special page name with a possible subpage, return an array
96 * where the first element is the special page name and the second is the
97 * subpage.
98 *
99 * @param $alias String
100 * @return Array
101 * @deprecated since 1.18 call SpecialPageFactory method directly
102 */
103 static function resolveAliasWithSubpage( $alias ) {
104 return SpecialPageFactory::resolveAlias( $alias );
105 }
106
107 /**
108 * Add a page to a certain display group for Special:SpecialPages
109 *
110 * @param $page Mixed: SpecialPage or string
111 * @param $group String
112 * @deprecated since 1.18 call SpecialPageFactory method directly
113 */
114 static function setGroup( $page, $group ) {
115 wfDeprecated( __METHOD__, '1.18' );
116 SpecialPageFactory::setGroup( $page, $group );
117 }
118
119 /**
120 * Get the group that the special page belongs in on Special:SpecialPage
121 *
122 * @param $page SpecialPage
123 * @return string
124 * @deprecated since 1.18 call SpecialPageFactory method directly
125 */
126 static function getGroup( &$page ) {
127 wfDeprecated( __METHOD__, '1.18' );
128 return SpecialPageFactory::getGroup( $page );
129 }
130
131 /**
132 * Remove a special page from the list
133 * Formerly used to disable expensive or dangerous special pages. The
134 * preferred method is now to add a SpecialPage_initList hook.
135 * @deprecated since 1.18
136 *
137 * @param string $name the page to remove
138 */
139 static function removePage( $name ) {
140 wfDeprecated( __METHOD__, '1.18' );
141 unset( SpecialPageFactory::getList()->$name );
142 }
143
144 /**
145 * Check if a given name exist as a special page or as a special page alias
146 *
147 * @param string $name name of a special page
148 * @return Boolean: true if a special page exists with this name
149 * @deprecated since 1.18 call SpecialPageFactory method directly
150 */
151 static function exists( $name ) {
152 wfDeprecated( __METHOD__, '1.18' );
153 return SpecialPageFactory::exists( $name );
154 }
155
156 /**
157 * Find the object with a given name and return it (or NULL)
158 *
159 * @param $name String
160 * @return SpecialPage object or null if the page doesn't exist
161 * @deprecated since 1.18 call SpecialPageFactory method directly
162 */
163 static function getPage( $name ) {
164 wfDeprecated( __METHOD__, '1.18' );
165 return SpecialPageFactory::getPage( $name );
166 }
167
168 /**
169 * Get a special page with a given localised name, or NULL if there
170 * is no such special page.
171 *
172 * @param $alias String
173 * @return SpecialPage object or null if the page doesn't exist
174 * @deprecated since 1.18 call SpecialPageFactory method directly
175 */
176 static function getPageByAlias( $alias ) {
177 wfDeprecated( __METHOD__, '1.18' );
178 return SpecialPageFactory::getPage( $alias );
179 }
180
181 /**
182 * Return categorised listable special pages which are available
183 * for the current user, and everyone.
184 *
185 * @param $user User object to check permissions, $wgUser will be used
186 * if not provided
187 * @return array Associative array mapping page's name to its SpecialPage object
188 * @deprecated since 1.18 call SpecialPageFactory method directly
189 */
190 static function getUsablePages( User $user = null ) {
191 wfDeprecated( __METHOD__, '1.18' );
192 return SpecialPageFactory::getUsablePages( $user );
193 }
194
195 /**
196 * Return categorised listable special pages for all users
197 *
198 * @return array Associative array mapping page's name to its SpecialPage object
199 * @deprecated since 1.18 call SpecialPageFactory method directly
200 */
201 static function getRegularPages() {
202 wfDeprecated( __METHOD__, '1.18' );
203 return SpecialPageFactory::getRegularPages();
204 }
205
206 /**
207 * Return categorised listable special pages which are available
208 * for the current user, but not for everyone
209 *
210 * @return array Associative array mapping page's name to its SpecialPage object
211 * @deprecated since 1.18 call SpecialPageFactory method directly
212 */
213 static function getRestrictedPages() {
214 wfDeprecated( __METHOD__, '1.18' );
215 return SpecialPageFactory::getRestrictedPages();
216 }
217
218 /**
219 * Execute a special page path.
220 * The path may contain parameters, e.g. Special:Name/Params
221 * Extracts the special page name and call the execute method, passing the parameters
222 *
223 * Returns a title object if the page is redirected, false if there was no such special
224 * page, and true if it was successful.
225 *
226 * @param $title Title object
227 * @param $context IContextSource
228 * @param $including Bool output is being captured for use in {{special:whatever}}
229 * @return Bool
230 * @deprecated since 1.18 call SpecialPageFactory method directly
231 */
232 public static function executePath( &$title, IContextSource &$context, $including = false ) {
233 wfDeprecated( __METHOD__, '1.18' );
234 return SpecialPageFactory::executePath( $title, $context, $including );
235 }
236
237 /**
238 * Get the local name for a specified canonical name
239 *
240 * @param $name String
241 * @param $subpage Mixed: boolean false, or string
242 *
243 * @return String
244 * @deprecated since 1.18 call SpecialPageFactory method directly
245 */
246 static function getLocalNameFor( $name, $subpage = false ) {
247 wfDeprecated( __METHOD__, '1.18' );
248 return SpecialPageFactory::getLocalNameFor( $name, $subpage );
249 }
250
251 /**
252 * Get a localised Title object for a specified special page name
253 *
254 * @param $name String
255 * @param string|Bool $subpage subpage string, or false to not use a subpage
256 * @param string $fragment the link fragment (after the "#")
257 * @throws MWException
258 * @return Title object
259 */
260 public static function getTitleFor( $name, $subpage = false, $fragment = '' ) {
261 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
262 return Title::makeTitle( NS_SPECIAL, $name, $fragment );
263 }
264
265 /**
266 * Get a localised Title object for a page name with a possibly unvalidated subpage
267 *
268 * @param $name String
269 * @param string|Bool $subpage subpage string, or false to not use a subpage
270 * @return Title object or null if the page doesn't exist
271 */
272 public static function getSafeTitleFor( $name, $subpage = false ) {
273 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
274 if ( $name ) {
275 return Title::makeTitleSafe( NS_SPECIAL, $name );
276 } else {
277 return null;
278 }
279 }
280
281 /**
282 * Get a title for a given alias
283 *
284 * @param $alias String
285 * @return Title or null if there is no such alias
286 * @deprecated since 1.18 call SpecialPageFactory method directly
287 */
288 static function getTitleForAlias( $alias ) {
289 wfDeprecated( __METHOD__, '1.18' );
290 return SpecialPageFactory::getTitleForAlias( $alias );
291 }
292
293 /**
294 * Default constructor for special pages
295 * Derivative classes should call this from their constructor
296 * Note that if the user does not have the required level, an error message will
297 * be displayed by the default execute() method, without the global function ever
298 * being called.
299 *
300 * If you override execute(), you can recover the default behavior with userCanExecute()
301 * and displayRestrictionError()
302 *
303 * @param string $name Name of the special page, as seen in links and URLs
304 * @param string $restriction User right required, e.g. "block" or "delete"
305 * @param bool $listed Whether the page is listed in Special:Specialpages
306 * @param Callback|Bool $function Function called by execute(). By default
307 * it is constructed from $name
308 * @param string $file File which is included by execute(). It is also
309 * constructed from $name by default
310 * @param bool $includable Whether the page can be included in normal pages
311 */
312 public function __construct(
313 $name = '', $restriction = '', $listed = true,
314 $function = false, $file = 'default', $includable = false
315 ) {
316 $this->init( $name, $restriction, $listed, $function, $file, $includable );
317 }
318
319 /**
320 * Do the real work for the constructor, mainly so __call() can intercept
321 * calls to SpecialPage()
322 * @param string $name Name of the special page, as seen in links and URLs
323 * @param string $restriction User right required, e.g. "block" or "delete"
324 * @param bool $listed Whether the page is listed in Special:Specialpages
325 * @param Callback|Bool $function Function called by execute(). By default
326 * it is constructed from $name
327 * @param string $file File which is included by execute(). It is also
328 * constructed from $name by default
329 * @param bool $includable Whether the page can be included in normal pages
330 */
331 private function init( $name, $restriction, $listed, $function, $file, $includable ) {
332 $this->mName = $name;
333 $this->mRestriction = $restriction;
334 $this->mListed = $listed;
335 $this->mIncludable = $includable;
336 if ( !$function ) {
337 $this->mFunction = 'wfSpecial' . $name;
338 } else {
339 $this->mFunction = $function;
340 }
341 if ( $file === 'default' ) {
342 $this->mFile = __DIR__ . "/specials/Special$name.php";
343 } else {
344 $this->mFile = $file;
345 }
346 }
347
348 /**
349 * Use PHP's magic __call handler to get calls to the old PHP4 constructor
350 * because PHP E_STRICT yells at you for having __construct() and SpecialPage()
351 *
352 * @param string $fName Name of called method
353 * @param array $a Arguments to the method
354 * @throws MWException
355 * @deprecated since 1.17, call parent::__construct()
356 */
357 public function __call( $fName, $a ) {
358 // Deprecated messages now, remove in 1.19 or 1.20?
359 wfDeprecated( __METHOD__, '1.17' );
360
361 // Sometimes $fName is SpecialPage, sometimes it's specialpage. <3 PHP
362 if ( strtolower( $fName ) == 'specialpage' ) {
363 $name = isset( $a[0] ) ? $a[0] : '';
364 $restriction = isset( $a[1] ) ? $a[1] : '';
365 $listed = isset( $a[2] ) ? $a[2] : true;
366 $function = isset( $a[3] ) ? $a[3] : false;
367 $file = isset( $a[4] ) ? $a[4] : 'default';
368 $includable = isset( $a[5] ) ? $a[5] : false;
369 $this->init( $name, $restriction, $listed, $function, $file, $includable );
370 } else {
371 $className = get_class( $this );
372 throw new MWException( "Call to undefined method $className::$fName" );
373 }
374 }
375
376 /**
377 * Get the name of this Special Page.
378 * @return String
379 */
380 function getName() {
381 return $this->mName;
382 }
383
384 /**
385 * Get the permission that a user must have to execute this page
386 * @return String
387 */
388 function getRestriction() {
389 return $this->mRestriction;
390 }
391
392 /**
393 * Get the file which will be included by SpecialPage::execute() if your extension is
394 * still stuck in the past and hasn't overridden the execute() method. No modern code
395 * should want or need to know this.
396 * @return String
397 * @deprecated since 1.18
398 */
399 function getFile() {
400 wfDeprecated( __METHOD__, '1.18' );
401 return $this->mFile;
402 }
403
404 // @todo FIXME: Decide which syntax to use for this, and stick to it
405 /**
406 * Whether this special page is listed in Special:SpecialPages
407 * @since r3583 (v1.3)
408 * @return Bool
409 */
410 function isListed() {
411 return $this->mListed;
412 }
413 /**
414 * Set whether this page is listed in Special:Specialpages, at run-time
415 * @since r3583 (v1.3)
416 * @param $listed Bool
417 * @return Bool
418 */
419 function setListed( $listed ) {
420 return wfSetVar( $this->mListed, $listed );
421 }
422 /**
423 * Get or set whether this special page is listed in Special:SpecialPages
424 * @since r11308 (v1.6)
425 * @param $x Bool
426 * @return Bool
427 */
428 function listed( $x = null ) {
429 return wfSetVar( $this->mListed, $x );
430 }
431
432 /**
433 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
434 * @return Bool
435 */
436 public function isIncludable() {
437 return $this->mIncludable;
438 }
439
440 /**
441 * These mutators are very evil, as the relevant variables should not mutate. So
442 * don't use them.
443 * @param $x Mixed
444 * @return Mixed
445 * @deprecated since 1.18
446 */
447 function name( $x = null ) {
448 wfDeprecated( __METHOD__, '1.18' );
449 return wfSetVar( $this->mName, $x );
450 }
451
452 /**
453 * These mutators are very evil, as the relevant variables should not mutate. So
454 * don't use them.
455 * @param $x Mixed
456 * @return Mixed
457 * @deprecated since 1.18
458 */
459 function restriction( $x = null ) {
460 wfDeprecated( __METHOD__, '1.18' );
461 return wfSetVar( $this->mRestriction, $x );
462 }
463
464 /**
465 * These mutators are very evil, as the relevant variables should not mutate. So
466 * don't use them.
467 * @param $x Mixed
468 * @return Mixed
469 * @deprecated since 1.18
470 */
471 function func( $x = null ) {
472 wfDeprecated( __METHOD__, '1.18' );
473 return wfSetVar( $this->mFunction, $x );
474 }
475
476 /**
477 * These mutators are very evil, as the relevant variables should not mutate. So
478 * don't use them.
479 * @param $x Mixed
480 * @return Mixed
481 * @deprecated since 1.18
482 */
483 function file( $x = null ) {
484 wfDeprecated( __METHOD__, '1.18' );
485 return wfSetVar( $this->mFile, $x );
486 }
487
488 /**
489 * These mutators are very evil, as the relevant variables should not mutate. So
490 * don't use them.
491 * @param $x Mixed
492 * @return Mixed
493 * @deprecated since 1.18
494 */
495 function includable( $x = null ) {
496 wfDeprecated( __METHOD__, '1.18' );
497 return wfSetVar( $this->mIncludable, $x );
498 }
499
500 /**
501 * Whether the special page is being evaluated via transclusion
502 * @param $x Bool
503 * @return Bool
504 */
505 function including( $x = null ) {
506 return wfSetVar( $this->mIncluding, $x );
507 }
508
509 /**
510 * Get the localised name of the special page
511 */
512 function getLocalName() {
513 if ( !isset( $this->mLocalName ) ) {
514 $this->mLocalName = SpecialPageFactory::getLocalNameFor( $this->mName );
515 }
516 return $this->mLocalName;
517 }
518
519 /**
520 * Is this page expensive (for some definition of expensive)?
521 * Expensive pages are disabled or cached in miser mode. Originally used
522 * (and still overridden) by QueryPage and subclasses, moved here so that
523 * Special:SpecialPages can safely call it for all special pages.
524 *
525 * @return Boolean
526 */
527 public function isExpensive() {
528 return false;
529 }
530
531 /**
532 * Is this page cached?
533 * Expensive pages are cached or disabled in miser mode.
534 * Used by QueryPage and subclasses, moved here so that
535 * Special:SpecialPages can safely call it for all special pages.
536 *
537 * @return Boolean
538 * @since 1.21
539 */
540 public function isCached() {
541 return false;
542 }
543
544 /**
545 * Can be overridden by subclasses with more complicated permissions
546 * schemes.
547 *
548 * @return Boolean: should the page be displayed with the restricted-access
549 * pages?
550 */
551 public function isRestricted() {
552 // DWIM: If anons can do something, then it is not restricted
553 return $this->mRestriction != '' && !User::groupHasPermission( '*', $this->mRestriction );
554 }
555
556 /**
557 * Checks if the given user (identified by an object) can execute this
558 * special page (as defined by $mRestriction). Can be overridden by sub-
559 * classes with more complicated permissions schemes.
560 *
561 * @param $user User: the user to check
562 * @return Boolean: does the user have permission to view the page?
563 */
564 public function userCanExecute( User $user ) {
565 return $user->isAllowed( $this->mRestriction );
566 }
567
568 /**
569 * Output an error message telling the user what access level they have to have
570 */
571 function displayRestrictionError() {
572 throw new PermissionsError( $this->mRestriction );
573 }
574
575 /**
576 * Checks if userCanExecute, and if not throws a PermissionsError
577 *
578 * @since 1.19
579 */
580 public function checkPermissions() {
581 if ( !$this->userCanExecute( $this->getUser() ) ) {
582 $this->displayRestrictionError();
583 }
584 }
585
586 /**
587 * If the wiki is currently in readonly mode, throws a ReadOnlyError
588 *
589 * @since 1.19
590 * @throws ReadOnlyError
591 */
592 public function checkReadOnly() {
593 if ( wfReadOnly() ) {
594 throw new ReadOnlyError;
595 }
596 }
597
598 /**
599 * If the user is not logged in, throws UserNotLoggedIn error.
600 *
601 * Default error message includes a link to Special:Userlogin with properly set 'returnto' query
602 * parameter.
603 *
604 * @since 1.23
605 * @param string|Message $reasonMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
606 * will be used as message keys. If a string is given, the message will also receive a
607 * formatted login link (generated using the 'loginreqlink' message) as first parameter. If a
608 * Message is given, it will be passed on verbatim.
609 * @param string|Message $titleMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
610 * will be used as message keys.
611 * @throws UserNotLoggedIn
612 */
613 public function requireLogin( $reasonMsg = null, $titleMsg = null ) {
614 if ( $this->getUser()->isAnon() ) {
615 // Use default messages if not given or explicit null passed
616 if ( !$reasonMsg ) {
617 $reasonMsg = 'exception-nologin-text-manual';
618 }
619 if ( !$titleMsg ) {
620 $titleMsg = 'exception-nologin';
621 }
622
623 // Convert to Messages with current context
624 if ( is_string( $reasonMsg ) ) {
625 $loginreqlink = Linker::linkKnown(
626 SpecialPage::getTitleFor( 'Userlogin' ),
627 $this->msg( 'loginreqlink' )->escaped(),
628 array(),
629 array( 'returnto' => $this->getTitle()->getPrefixedText() )
630 );
631 $reasonMsg = $this->msg( $reasonMsg )->rawParams( $loginreqlink );
632 }
633 if ( is_string( $titleMsg ) ) {
634 $titleMsg = $this->msg( $titleMsg );
635 }
636
637 throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
638 }
639 }
640
641 /**
642 * Sets headers - this should be called from the execute() method of all derived classes!
643 * @param string|array $robotPolicy string such as "noindex,nofollow" or an array in this
644 * format:
645 * array(
646 * 'index' => 'noindex' // or 'index'
647 * 'follow' => 'nofollow' // or 'follow'
648 * )
649 */
650 function setHeaders( $robotPolicy = "noindex,nofollow" ) {
651 $out = $this->getOutput();
652 $out->setArticleRelated( false );
653 $out->setRobotPolicy( $robotPolicy );
654 $out->setPageTitle( $this->getDescription() );
655 }
656
657 /**
658 * Entry point.
659 *
660 * @since 1.20
661 *
662 * @param $subPage string|null
663 */
664 final public function run( $subPage ) {
665 /**
666 * Gets called before @see SpecialPage::execute.
667 *
668 * @since 1.20
669 *
670 * @param $special SpecialPage
671 * @param $subPage string|null
672 */
673 wfRunHooks( 'SpecialPageBeforeExecute', array( $this, $subPage ) );
674
675 $this->beforeExecute( $subPage );
676 $this->execute( $subPage );
677 $this->afterExecute( $subPage );
678
679 /**
680 * Gets called after @see SpecialPage::execute.
681 *
682 * @since 1.20
683 *
684 * @param $special SpecialPage
685 * @param $subPage string|null
686 */
687 wfRunHooks( 'SpecialPageAfterExecute', array( $this, $subPage ) );
688 }
689
690 /**
691 * Gets called before @see SpecialPage::execute.
692 *
693 * @since 1.20
694 *
695 * @param $subPage string|null
696 */
697 protected function beforeExecute( $subPage ) {
698 // No-op
699 }
700
701 /**
702 * Gets called after @see SpecialPage::execute.
703 *
704 * @since 1.20
705 *
706 * @param $subPage string|null
707 */
708 protected function afterExecute( $subPage ) {
709 // No-op
710 }
711
712 /**
713 * Default execute method
714 * Checks user permissions, calls the function given in mFunction
715 *
716 * This must be overridden by subclasses; it will be made abstract in a future version
717 *
718 * @param $subPage string|null
719 */
720 public function execute( $subPage ) {
721 $this->setHeaders();
722 $this->checkPermissions();
723
724 $func = $this->mFunction;
725 // only load file if the function does not exist
726 if ( !is_callable( $func ) && $this->mFile ) {
727 require_once $this->mFile;
728 }
729 $this->outputHeader();
730 call_user_func( $func, $subPage, $this );
731 }
732
733 /**
734 * Outputs a summary message on top of special pages
735 * Per default the message key is the canonical name of the special page
736 * May be overridden, i.e. by extensions to stick with the naming conventions
737 * for message keys: 'extensionname-xxx'
738 *
739 * @param string $summaryMessageKey message key of the summary
740 */
741 function outputHeader( $summaryMessageKey = '' ) {
742 global $wgContLang;
743
744 if ( $summaryMessageKey == '' ) {
745 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
746 } else {
747 $msg = $summaryMessageKey;
748 }
749 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
750 $this->getOutput()->wrapWikiMsg(
751 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
752 }
753
754 }
755
756 /**
757 * Returns the name that goes in the \<h1\> in the special page itself, and
758 * also the name that will be listed in Special:Specialpages
759 *
760 * Derived classes can override this, but usually it is easier to keep the
761 * default behavior. Messages can be added at run-time, see
762 * MessageCache.php.
763 *
764 * @return String
765 */
766 function getDescription() {
767 return $this->msg( strtolower( $this->mName ) )->text();
768 }
769
770 /**
771 * Get a self-referential title object
772 *
773 * @param $subpage String|Bool
774 * @return Title object
775 */
776 function getTitle( $subpage = false ) {
777 return self::getTitleFor( $this->mName, $subpage );
778 }
779
780 /**
781 * Sets the context this SpecialPage is executed in
782 *
783 * @param $context IContextSource
784 * @since 1.18
785 */
786 public function setContext( $context ) {
787 $this->mContext = $context;
788 }
789
790 /**
791 * Gets the context this SpecialPage is executed in
792 *
793 * @return IContextSource|RequestContext
794 * @since 1.18
795 */
796 public function getContext() {
797 if ( $this->mContext instanceof IContextSource ) {
798 return $this->mContext;
799 } else {
800 wfDebug( __METHOD__ . " called and \$mContext is null. " .
801 "Return RequestContext::getMain(); for sanity\n" );
802 return RequestContext::getMain();
803 }
804 }
805
806 /**
807 * Get the WebRequest being used for this instance
808 *
809 * @return WebRequest
810 * @since 1.18
811 */
812 public function getRequest() {
813 return $this->getContext()->getRequest();
814 }
815
816 /**
817 * Get the OutputPage being used for this instance
818 *
819 * @return OutputPage
820 * @since 1.18
821 */
822 public function getOutput() {
823 return $this->getContext()->getOutput();
824 }
825
826 /**
827 * Shortcut to get the User executing this instance
828 *
829 * @return User
830 * @since 1.18
831 */
832 public function getUser() {
833 return $this->getContext()->getUser();
834 }
835
836 /**
837 * Shortcut to get the skin being used for this instance
838 *
839 * @return Skin
840 * @since 1.18
841 */
842 public function getSkin() {
843 return $this->getContext()->getSkin();
844 }
845
846 /**
847 * Shortcut to get user's language
848 *
849 * @deprecated since 1.19 Use getLanguage instead
850 * @return Language
851 * @since 1.18
852 */
853 public function getLang() {
854 wfDeprecated( __METHOD__, '1.19' );
855 return $this->getLanguage();
856 }
857
858 /**
859 * Shortcut to get user's language
860 *
861 * @return Language
862 * @since 1.19
863 */
864 public function getLanguage() {
865 return $this->getContext()->getLanguage();
866 }
867
868 /**
869 * Return the full title, including $par
870 *
871 * @return Title
872 * @since 1.18
873 */
874 public function getFullTitle() {
875 return $this->getContext()->getTitle();
876 }
877
878 /**
879 * Wrapper around wfMessage that sets the current context.
880 *
881 * @return Message
882 * @see wfMessage
883 */
884 public function msg( /* $args */ ) {
885 $message = call_user_func_array(
886 array( $this->getContext(), 'msg' ),
887 func_get_args()
888 );
889 // RequestContext passes context to wfMessage, and the language is set from
890 // the context, but setting the language for Message class removes the
891 // interface message status, which breaks for example usernameless gender
892 // invocations. Restore the flag when not including special page in content.
893 if ( $this->including() ) {
894 $message->setInterfaceMessageFlag( false );
895 }
896 return $message;
897 }
898
899 /**
900 * Adds RSS/atom links
901 *
902 * @param $params array
903 */
904 protected function addFeedLinks( $params ) {
905 global $wgFeedClasses;
906
907 $feedTemplate = wfScript( 'api' );
908
909 foreach ( $wgFeedClasses as $format => $class ) {
910 $theseParams = $params + array( 'feedformat' => $format );
911 $url = wfAppendQuery( $feedTemplate, $theseParams );
912 $this->getOutput()->addFeedLink( $format, $url );
913 }
914 }
915
916 /**
917 * Get the group that the special page belongs in on Special:SpecialPage
918 * Use this method, instead of getGroupName to allow customization
919 * of the group name from the wiki side
920 *
921 * @return string Group of this special page
922 * @since 1.21
923 */
924 public function getFinalGroupName() {
925 global $wgSpecialPageGroups;
926 $name = $this->getName();
927
928 // Allow overbidding the group from the wiki side
929 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
930 if ( !$msg->isBlank() ) {
931 $group = $msg->text();
932 } else {
933 // Than use the group from this object
934 $group = $this->getGroupName();
935
936 // Group '-' is used as default to have the chance to determine,
937 // if the special pages overrides this method,
938 // if not overridden, $wgSpecialPageGroups is checked for b/c
939 if ( $group === '-' && isset( $wgSpecialPageGroups[$name] ) ) {
940 $group = $wgSpecialPageGroups[$name];
941 }
942 }
943
944 // never give '-' back, change to 'other'
945 if ( $group === '-' ) {
946 $group = 'other';
947 }
948
949 return $group;
950 }
951
952 /**
953 * Under which header this special page is listed in Special:SpecialPages
954 * See messages 'specialpages-group-*' for valid names
955 * This method defaults to group 'other'
956 *
957 * @return string
958 * @since 1.21
959 */
960 protected function getGroupName() {
961 // '-' used here to determine, if this group is overridden or has a hardcoded 'other'
962 // Needed for b/c in getFinalGroupName
963 return '-';
964 }
965 }
966
967 /**
968 * Special page which uses an HTMLForm to handle processing. This is mostly a
969 * clone of FormAction. More special pages should be built this way; maybe this could be
970 * a new structure for SpecialPages
971 */
972 abstract class FormSpecialPage extends SpecialPage {
973 /**
974 * The sub-page of the special page.
975 * @var string
976 */
977 protected $par = null;
978
979 /**
980 * Get an HTMLForm descriptor array
981 * @return Array
982 */
983 abstract protected function getFormFields();
984
985 /**
986 * Add pre-text to the form
987 * @return String HTML which will be sent to $form->addPreText()
988 */
989 protected function preText() {
990 return '';
991 }
992
993 /**
994 * Add post-text to the form
995 * @return String HTML which will be sent to $form->addPostText()
996 */
997 protected function postText() {
998 return '';
999 }
1000
1001 /**
1002 * Play with the HTMLForm if you need to more substantially
1003 * @param $form HTMLForm
1004 */
1005 protected function alterForm( HTMLForm $form ) {
1006 }
1007
1008 /**
1009 * Get message prefix for HTMLForm
1010 *
1011 * @since 1.21
1012 * @return string
1013 */
1014 protected function getMessagePrefix() {
1015 return strtolower( $this->getName() );
1016 }
1017
1018 /**
1019 * Get the HTMLForm to control behavior
1020 * @return HTMLForm|null
1021 */
1022 protected function getForm() {
1023 $this->fields = $this->getFormFields();
1024
1025 $form = new HTMLForm( $this->fields, $this->getContext(), $this->getMessagePrefix() );
1026 $form->setSubmitCallback( array( $this, 'onSubmit' ) );
1027 // If the form is a compact vertical form, then don't output this ugly
1028 // fieldset surrounding it.
1029 // XXX Special pages can setDisplayFormat to 'vform' in alterForm(), but that
1030 // is called after this.
1031 if ( !$form->isVForm() ) {
1032 $form->setWrapperLegendMsg( $this->getMessagePrefix() . '-legend' );
1033 }
1034
1035 $headerMsg = $this->msg( $this->getMessagePrefix() . '-text' );
1036 if ( !$headerMsg->isDisabled() ) {
1037 $form->addHeaderText( $headerMsg->parseAsBlock() );
1038 }
1039
1040 // Retain query parameters (uselang etc)
1041 $params = array_diff_key(
1042 $this->getRequest()->getQueryValues(), array( 'title' => null ) );
1043 $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
1044
1045 $form->addPreText( $this->preText() );
1046 $form->addPostText( $this->postText() );
1047 $this->alterForm( $form );
1048
1049 // Give hooks a chance to alter the form, adding extra fields or text etc
1050 wfRunHooks( "Special{$this->getName()}BeforeFormDisplay", array( &$form ) );
1051
1052 return $form;
1053 }
1054
1055 /**
1056 * Process the form on POST submission.
1057 * @param $data Array
1058 * @return Bool|Array true for success, false for didn't-try, array of errors on failure
1059 */
1060 abstract public function onSubmit( array $data );
1061
1062 /**
1063 * Do something exciting on successful processing of the form, most likely to show a
1064 * confirmation message
1065 * @since 1.22 Default is to do nothing
1066 */
1067 public function onSuccess() {
1068 }
1069
1070 /**
1071 * Basic SpecialPage workflow: get a form, send it to the user; get some data back,
1072 *
1073 * @param string $par Subpage string if one was specified
1074 */
1075 public function execute( $par ) {
1076 $this->setParameter( $par );
1077 $this->setHeaders();
1078
1079 // This will throw exceptions if there's a problem
1080 $this->checkExecutePermissions( $this->getUser() );
1081
1082 $form = $this->getForm();
1083 if ( $form->show() ) {
1084 $this->onSuccess();
1085 }
1086 }
1087
1088 /**
1089 * Maybe do something interesting with the subpage parameter
1090 * @param string $par
1091 */
1092 protected function setParameter( $par ) {
1093 $this->par = $par;
1094 }
1095
1096 /**
1097 * Called from execute() to check if the given user can perform this action.
1098 * Failures here must throw subclasses of ErrorPageError.
1099 * @param $user User
1100 * @throws UserBlockedError
1101 * @return Bool true
1102 */
1103 protected function checkExecutePermissions( User $user ) {
1104 $this->checkPermissions();
1105
1106 if ( $this->requiresUnblock() && $user->isBlocked() ) {
1107 $block = $user->getBlock();
1108 throw new UserBlockedError( $block );
1109 }
1110
1111 if ( $this->requiresWrite() ) {
1112 $this->checkReadOnly();
1113 }
1114
1115 return true;
1116 }
1117
1118 /**
1119 * Whether this action requires the wiki not to be locked
1120 * @return Bool
1121 */
1122 public function requiresWrite() {
1123 return true;
1124 }
1125
1126 /**
1127 * Whether this action cannot be executed by a blocked user
1128 * @return Bool
1129 */
1130 public function requiresUnblock() {
1131 return true;
1132 }
1133 }
1134
1135 /**
1136 * Shortcut to construct a special page which is unlisted by default
1137 * @ingroup SpecialPage
1138 */
1139 class UnlistedSpecialPage extends SpecialPage {
1140 function __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
1141 parent::__construct( $name, $restriction, false, $function, $file );
1142 }
1143
1144 public function isListed() {
1145 return false;
1146 }
1147 }
1148
1149 /**
1150 * Shortcut to construct an includable special page
1151 * @ingroup SpecialPage
1152 */
1153 class IncludableSpecialPage extends SpecialPage {
1154 function __construct(
1155 $name, $restriction = '', $listed = true, $function = false, $file = 'default'
1156 ) {
1157 parent::__construct( $name, $restriction, $listed, $function, $file, true );
1158 }
1159
1160 public function isIncludable() {
1161 return true;
1162 }
1163 }
1164
1165 /**
1166 * Shortcut to construct a special page alias.
1167 * @ingroup SpecialPage
1168 */
1169 abstract class RedirectSpecialPage extends UnlistedSpecialPage {
1170
1171 // Query parameters that can be passed through redirects
1172 protected $mAllowedRedirectParams = array();
1173
1174 // Query parameters added by redirects
1175 protected $mAddedRedirectParams = array();
1176
1177 public function execute( $par ) {
1178 $redirect = $this->getRedirect( $par );
1179 $query = $this->getRedirectQuery();
1180 // Redirect to a page title with possible query parameters
1181 if ( $redirect instanceof Title ) {
1182 $url = $redirect->getFullURL( $query );
1183 $this->getOutput()->redirect( $url );
1184 return $redirect;
1185 } elseif ( $redirect === true ) {
1186 // Redirect to index.php with query parameters
1187 $url = wfAppendQuery( wfScript( 'index' ), $query );
1188 $this->getOutput()->redirect( $url );
1189 return $redirect;
1190 } else {
1191 $class = get_class( $this );
1192 throw new MWException( "RedirectSpecialPage $class doesn't redirect!" );
1193 }
1194 }
1195
1196 /**
1197 * If the special page is a redirect, then get the Title object it redirects to.
1198 * False otherwise.
1199 *
1200 * @param string $par Subpage string
1201 * @return Title|bool
1202 */
1203 abstract public function getRedirect( $par );
1204
1205 /**
1206 * Return part of the request string for a special redirect page
1207 * This allows passing, e.g. action=history to Special:Mypage, etc.
1208 *
1209 * @return String
1210 */
1211 public function getRedirectQuery() {
1212 $params = array();
1213
1214 foreach ( $this->mAllowedRedirectParams as $arg ) {
1215 if ( $this->getRequest()->getVal( $arg, null ) !== null ) {
1216 $params[$arg] = $this->getRequest()->getVal( $arg );
1217 }
1218 }
1219
1220 foreach ( $this->mAddedRedirectParams as $arg => $val ) {
1221 $params[$arg] = $val;
1222 }
1223
1224 return count( $params )
1225 ? $params
1226 : false;
1227 }
1228 }
1229
1230 abstract class SpecialRedirectToSpecial extends RedirectSpecialPage {
1231 // @todo FIXME: Visibility must be declared
1232 var $redirName, $redirSubpage;
1233
1234 function __construct(
1235 $name, $redirName, $redirSubpage = false,
1236 $allowedRedirectParams = array(), $addedRedirectParams = array()
1237 ) {
1238 parent::__construct( $name );
1239 $this->redirName = $redirName;
1240 $this->redirSubpage = $redirSubpage;
1241 $this->mAllowedRedirectParams = $allowedRedirectParams;
1242 $this->mAddedRedirectParams = $addedRedirectParams;
1243 }
1244
1245 public function getRedirect( $subpage ) {
1246 if ( $this->redirSubpage === false ) {
1247 return SpecialPage::getTitleFor( $this->redirName, $subpage );
1248 } else {
1249 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
1250 }
1251 }
1252 }
1253
1254 /**
1255 * ListAdmins --> ListUsers/sysop
1256 */
1257 class SpecialListAdmins extends SpecialRedirectToSpecial {
1258 function __construct() {
1259 parent::__construct( 'Listadmins', 'Listusers', 'sysop' );
1260 }
1261 }
1262
1263 /**
1264 * ListBots --> ListUsers/bot
1265 */
1266 class SpecialListBots extends SpecialRedirectToSpecial {
1267 function __construct() {
1268 parent::__construct( 'Listbots', 'Listusers', 'bot' );
1269 }
1270 }
1271
1272 /**
1273 * CreateAccount --> UserLogin/signup
1274 * @todo FIXME: This (and the rest of the login frontend) needs to die a horrible painful death
1275 */
1276 class SpecialCreateAccount extends SpecialRedirectToSpecial {
1277 function __construct() {
1278 parent::__construct( 'CreateAccount', 'Userlogin', 'signup', array( 'returnto', 'returntoquery', 'uselang' ) );
1279 }
1280
1281 // No reason to hide this link on Special:Specialpages
1282 public function isListed() {
1283 return true;
1284 }
1285
1286 protected function getGroupName() {
1287 return 'login';
1288 }
1289 }
1290 /**
1291 * SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
1292 * are used to get user independent links pointing to the user page, talk
1293 * page and list of contributions.
1294 * This can let us cache a single copy of any generated content for all
1295 * users.
1296 */
1297
1298 /**
1299 * Superclass for any RedirectSpecialPage which redirects the user
1300 * to a particular article (as opposed to user contributions, logs, etc.).
1301 *
1302 * For security reasons these special pages are restricted to pass on
1303 * the following subset of GET parameters to the target page while
1304 * removing all others:
1305 *
1306 * - useskin, uselang, printable: to alter the appearance of the resulting page
1307 *
1308 * - redirect: allows viewing one's user page or talk page even if it is a
1309 * redirect.
1310 *
1311 * - rdfrom: allows redirecting to one's user page or talk page from an
1312 * external wiki with the "Redirect from..." notice.
1313 *
1314 * - limit, offset: Useful for linking to history of one's own user page or
1315 * user talk page. For example, this would be a link to "the last edit to your
1316 * user talk page in the year 2010":
1317 * http://en.wikipedia.org/wiki/Special:MyPage?offset=20110000000000&limit=1&action=history
1318 *
1319 * - feed: would allow linking to the current user's RSS feed for their user
1320 * talk page:
1321 * http://en.wikipedia.org/w/index.php?title=Special:MyTalk&action=history&feed=rss
1322 *
1323 * - preloadtitle: Can be used to provide a default section title for a
1324 * preloaded new comment on one's own talk page.
1325 *
1326 * - summary : Can be used to provide a default edit summary for a preloaded
1327 * edit to one's own user page or talk page.
1328 *
1329 * - preview: Allows showing/hiding preview on first edit regardless of user
1330 * preference, useful for preloaded edits where you know preview wouldn't be
1331 * useful.
1332 *
1333 * - internaledit, externaledit, mode: Allows forcing the use of the
1334 * internal/external editor, e.g. to force the internal editor for
1335 * short/simple preloaded edits.
1336 *
1337 * - redlink: Affects the message the user sees if their talk page/user talk
1338 * page does not currently exist. Avoids confusion for newbies with no user
1339 * pages over why they got a "permission error" following this link:
1340 * http://en.wikipedia.org/w/index.php?title=Special:MyPage&redlink=1
1341 *
1342 * - debug: determines whether the debug parameter is passed to load.php,
1343 * which disables reformatting and allows scripts to be debugged. Useful
1344 * when debugging scripts that manipulate one's own user page or talk page.
1345 *
1346 * @par Hook extension:
1347 * Extensions can add to the redirect parameters list by using the hook
1348 * RedirectSpecialArticleRedirectParams
1349 *
1350 * This hook allows extensions which add GET parameters like FlaggedRevs to
1351 * retain those parameters when redirecting using special pages.
1352 *
1353 * @par Hook extension example:
1354 * @code
1355 * $wgHooks['RedirectSpecialArticleRedirectParams'][] =
1356 * 'MyExtensionHooks::onRedirectSpecialArticleRedirectParams';
1357 * public static function onRedirectSpecialArticleRedirectParams( &$redirectParams ) {
1358 * $redirectParams[] = 'stable';
1359 * return true;
1360 * }
1361 * @endcode
1362 * @ingroup SpecialPage
1363 */
1364 abstract class RedirectSpecialArticle extends RedirectSpecialPage {
1365 function __construct( $name ) {
1366 parent::__construct( $name );
1367 $redirectParams = array(
1368 'action',
1369 'redirect', 'rdfrom',
1370 # Options for preloaded edits
1371 'preload', 'editintro', 'preloadtitle', 'summary', 'nosummary',
1372 # Options for overriding user settings
1373 'preview', 'internaledit', 'externaledit', 'mode', 'minor', 'watchthis',
1374 # Options for history/diffs
1375 'section', 'oldid', 'diff', 'dir',
1376 'limit', 'offset', 'feed',
1377 # Misc options
1378 'redlink', 'debug',
1379 # Options for action=raw; missing ctype can break JS or CSS in some browsers
1380 'ctype', 'maxage', 'smaxage',
1381 );
1382
1383 wfRunHooks( "RedirectSpecialArticleRedirectParams", array( &$redirectParams ) );
1384 $this->mAllowedRedirectParams = $redirectParams;
1385 }
1386 }
1387
1388 /**
1389 * Shortcut to construct a special page pointing to current user user's page.
1390 * @ingroup SpecialPage
1391 */
1392 class SpecialMypage extends RedirectSpecialArticle {
1393 function __construct() {
1394 parent::__construct( 'Mypage' );
1395 }
1396
1397 function getRedirect( $subpage ) {
1398 if ( strval( $subpage ) !== '' ) {
1399 return Title::makeTitle( NS_USER, $this->getUser()->getName() . '/' . $subpage );
1400 } else {
1401 return Title::makeTitle( NS_USER, $this->getUser()->getName() );
1402 }
1403 }
1404 }
1405
1406 /**
1407 * Shortcut to construct a special page pointing to current user talk page.
1408 * @ingroup SpecialPage
1409 */
1410 class SpecialMytalk extends RedirectSpecialArticle {
1411 function __construct() {
1412 parent::__construct( 'Mytalk' );
1413 }
1414
1415 function getRedirect( $subpage ) {
1416 if ( strval( $subpage ) !== '' ) {
1417 return Title::makeTitle( NS_USER_TALK, $this->getUser()->getName() . '/' . $subpage );
1418 } else {
1419 return Title::makeTitle( NS_USER_TALK, $this->getUser()->getName() );
1420 }
1421 }
1422 }
1423
1424 /**
1425 * Shortcut to construct a special page pointing to current user contributions.
1426 * @ingroup SpecialPage
1427 */
1428 class SpecialMycontributions extends RedirectSpecialPage {
1429 function __construct() {
1430 parent::__construct( 'Mycontributions' );
1431 $this->mAllowedRedirectParams = array( 'limit', 'namespace', 'tagfilter',
1432 'offset', 'dir', 'year', 'month', 'feed' );
1433 }
1434
1435 function getRedirect( $subpage ) {
1436 return SpecialPage::getTitleFor( 'Contributions', $this->getUser()->getName() );
1437 }
1438 }
1439
1440 /**
1441 * Redirect to Special:Listfiles?user=$wgUser
1442 */
1443 class SpecialMyuploads extends RedirectSpecialPage {
1444 function __construct() {
1445 parent::__construct( 'Myuploads' );
1446 $this->mAllowedRedirectParams = array( 'limit', 'ilshowall', 'ilsearch' );
1447 }
1448
1449 function getRedirect( $subpage ) {
1450 return SpecialPage::getTitleFor( 'Listfiles', $this->getUser()->getName() );
1451 }
1452 }
1453
1454 /**
1455 * Redirect Special:Listfiles?user=$wgUser&ilshowall=true
1456 */
1457 class SpecialAllMyUploads extends RedirectSpecialPage {
1458 function __construct() {
1459 parent::__construct( 'AllMyUploads' );
1460 $this->mAllowedRedirectParams = array( 'limit', 'ilsearch' );
1461 }
1462
1463 function getRedirect( $subpage ) {
1464 $this->mAddedRedirectParams['ilshowall'] = 1;
1465 return SpecialPage::getTitleFor( 'Listfiles', $this->getUser()->getName() );
1466 }
1467 }
1468
1469 /**
1470 * Redirect from Special:PermanentLink/### to index.php?oldid=###
1471 */
1472 class SpecialPermanentLink extends RedirectSpecialPage {
1473 function __construct() {
1474 parent::__construct( 'PermanentLink' );
1475 $this->mAllowedRedirectParams = array();
1476 }
1477
1478 function getRedirect( $subpage ) {
1479 $subpage = intval( $subpage );
1480 if ( $subpage === 0 ) {
1481 # throw an error page when no subpage was given
1482 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
1483 }
1484 $this->mAddedRedirectParams['oldid'] = $subpage;
1485 return true;
1486 }
1487 }