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