Merge "Relicense jquery.placeholder.js to MIT"
[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 * Sets headers - this should be called from the execute() method of all derived classes!
600 */
601 function setHeaders() {
602 $out = $this->getOutput();
603 $out->setArticleRelated( false );
604 $out->setRobotPolicy( "noindex,nofollow" );
605 $out->setPageTitle( $this->getDescription() );
606 }
607
608 /**
609 * Entry point.
610 *
611 * @since 1.20
612 *
613 * @param $subPage string|null
614 */
615 final public function run( $subPage ) {
616 /**
617 * Gets called before @see SpecialPage::execute.
618 *
619 * @since 1.20
620 *
621 * @param $special SpecialPage
622 * @param $subPage string|null
623 */
624 wfRunHooks( 'SpecialPageBeforeExecute', array( $this, $subPage ) );
625
626 $this->beforeExecute( $subPage );
627 $this->execute( $subPage );
628 $this->afterExecute( $subPage );
629
630 /**
631 * Gets called after @see SpecialPage::execute.
632 *
633 * @since 1.20
634 *
635 * @param $special SpecialPage
636 * @param $subPage string|null
637 */
638 wfRunHooks( 'SpecialPageAfterExecute', array( $this, $subPage ) );
639 }
640
641 /**
642 * Gets called before @see SpecialPage::execute.
643 *
644 * @since 1.20
645 *
646 * @param $subPage string|null
647 */
648 protected function beforeExecute( $subPage ) {
649 // No-op
650 }
651
652 /**
653 * Gets called after @see SpecialPage::execute.
654 *
655 * @since 1.20
656 *
657 * @param $subPage string|null
658 */
659 protected function afterExecute( $subPage ) {
660 // No-op
661 }
662
663 /**
664 * Default execute method
665 * Checks user permissions, calls the function given in mFunction
666 *
667 * This must be overridden by subclasses; it will be made abstract in a future version
668 *
669 * @param $subPage string|null
670 */
671 public function execute( $subPage ) {
672 $this->setHeaders();
673 $this->checkPermissions();
674
675 $func = $this->mFunction;
676 // only load file if the function does not exist
677 if ( !is_callable( $func ) && $this->mFile ) {
678 require_once $this->mFile;
679 }
680 $this->outputHeader();
681 call_user_func( $func, $subPage, $this );
682 }
683
684 /**
685 * Outputs a summary message on top of special pages
686 * Per default the message key is the canonical name of the special page
687 * May be overridden, i.e. by extensions to stick with the naming conventions
688 * for message keys: 'extensionname-xxx'
689 *
690 * @param string $summaryMessageKey message key of the summary
691 */
692 function outputHeader( $summaryMessageKey = '' ) {
693 global $wgContLang;
694
695 if ( $summaryMessageKey == '' ) {
696 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
697 } else {
698 $msg = $summaryMessageKey;
699 }
700 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
701 $this->getOutput()->wrapWikiMsg(
702 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
703 }
704
705 }
706
707 /**
708 * Returns the name that goes in the \<h1\> in the special page itself, and
709 * also the name that will be listed in Special:Specialpages
710 *
711 * Derived classes can override this, but usually it is easier to keep the
712 * default behavior. Messages can be added at run-time, see
713 * MessageCache.php.
714 *
715 * @return String
716 */
717 function getDescription() {
718 return $this->msg( strtolower( $this->mName ) )->text();
719 }
720
721 /**
722 * Get a self-referential title object
723 *
724 * @param $subpage String|Bool
725 * @return Title object
726 */
727 function getTitle( $subpage = false ) {
728 return self::getTitleFor( $this->mName, $subpage );
729 }
730
731 /**
732 * Sets the context this SpecialPage is executed in
733 *
734 * @param $context IContextSource
735 * @since 1.18
736 */
737 public function setContext( $context ) {
738 $this->mContext = $context;
739 }
740
741 /**
742 * Gets the context this SpecialPage is executed in
743 *
744 * @return IContextSource|RequestContext
745 * @since 1.18
746 */
747 public function getContext() {
748 if ( $this->mContext instanceof IContextSource ) {
749 return $this->mContext;
750 } else {
751 wfDebug( __METHOD__ . " called and \$mContext is null. " .
752 "Return RequestContext::getMain(); for sanity\n" );
753 return RequestContext::getMain();
754 }
755 }
756
757 /**
758 * Get the WebRequest being used for this instance
759 *
760 * @return WebRequest
761 * @since 1.18
762 */
763 public function getRequest() {
764 return $this->getContext()->getRequest();
765 }
766
767 /**
768 * Get the OutputPage being used for this instance
769 *
770 * @return OutputPage
771 * @since 1.18
772 */
773 public function getOutput() {
774 return $this->getContext()->getOutput();
775 }
776
777 /**
778 * Shortcut to get the User executing this instance
779 *
780 * @return User
781 * @since 1.18
782 */
783 public function getUser() {
784 return $this->getContext()->getUser();
785 }
786
787 /**
788 * Shortcut to get the skin being used for this instance
789 *
790 * @return Skin
791 * @since 1.18
792 */
793 public function getSkin() {
794 return $this->getContext()->getSkin();
795 }
796
797 /**
798 * Shortcut to get user's language
799 *
800 * @deprecated since 1.19 Use getLanguage instead
801 * @return Language
802 * @since 1.18
803 */
804 public function getLang() {
805 wfDeprecated( __METHOD__, '1.19' );
806 return $this->getLanguage();
807 }
808
809 /**
810 * Shortcut to get user's language
811 *
812 * @return Language
813 * @since 1.19
814 */
815 public function getLanguage() {
816 return $this->getContext()->getLanguage();
817 }
818
819 /**
820 * Return the full title, including $par
821 *
822 * @return Title
823 * @since 1.18
824 */
825 public function getFullTitle() {
826 return $this->getContext()->getTitle();
827 }
828
829 /**
830 * Wrapper around wfMessage that sets the current context.
831 *
832 * @return Message
833 * @see wfMessage
834 */
835 public function msg( /* $args */ ) {
836 // Note: can't use func_get_args() directly as second or later item in
837 // a parameter list until PHP 5.3 or you get a fatal error.
838 // Works fine as the first parameter, which appears elsewhere in the
839 // code base. Sighhhh.
840 $args = func_get_args();
841 $message = call_user_func_array( array( $this->getContext(), 'msg' ), $args );
842 // RequestContext passes context to wfMessage, and the language is set from
843 // the context, but setting the language for Message class removes the
844 // interface message status, which breaks for example usernameless gender
845 // invocations. Restore the flag when not including special page in content.
846 if ( $this->including() ) {
847 $message->setInterfaceMessageFlag( false );
848 }
849 return $message;
850 }
851
852 /**
853 * Adds RSS/atom links
854 *
855 * @param $params array
856 */
857 protected function addFeedLinks( $params ) {
858 global $wgFeedClasses;
859
860 $feedTemplate = wfScript( 'api' );
861
862 foreach ( $wgFeedClasses as $format => $class ) {
863 $theseParams = $params + array( 'feedformat' => $format );
864 $url = wfAppendQuery( $feedTemplate, $theseParams );
865 $this->getOutput()->addFeedLink( $format, $url );
866 }
867 }
868
869 /**
870 * Get the group that the special page belongs in on Special:SpecialPage
871 * Use this method, instead of getGroupName to allow customization
872 * of the group name from the wiki side
873 *
874 * @return string Group of this special page
875 * @since 1.21
876 */
877 public function getFinalGroupName() {
878 global $wgSpecialPageGroups;
879 $name = $this->getName();
880 $group = '-';
881
882 // Allow overbidding the group from the wiki side
883 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
884 if ( !$msg->isBlank() ) {
885 $group = $msg->text();
886 } else {
887 // Than use the group from this object
888 $group = $this->getGroupName();
889
890 // Group '-' is used as default to have the chance to determine,
891 // if the special pages overrides this method,
892 // if not overridden, $wgSpecialPageGroups is checked for b/c
893 if ( $group === '-' && isset( $wgSpecialPageGroups[$name] ) ) {
894 $group = $wgSpecialPageGroups[$name];
895 }
896 }
897
898 // never give '-' back, change to 'other'
899 if ( $group === '-' ) {
900 $group = 'other';
901 }
902
903 return $group;
904 }
905
906 /**
907 * Under which header this special page is listed in Special:SpecialPages
908 * See messages 'specialpages-group-*' for valid names
909 * This method defaults to group 'other'
910 *
911 * @return string
912 * @since 1.21
913 */
914 protected function getGroupName() {
915 // '-' used here to determine, if this group is overridden or has a hardcoded 'other'
916 // Needed for b/c in getFinalGroupName
917 return '-';
918 }
919 }
920
921 /**
922 * Special page which uses an HTMLForm to handle processing. This is mostly a
923 * clone of FormAction. More special pages should be built this way; maybe this could be
924 * a new structure for SpecialPages
925 */
926 abstract class FormSpecialPage extends SpecialPage {
927 /**
928 * The sub-page of the special page.
929 * @var string
930 */
931 protected $par = null;
932
933 /**
934 * Get an HTMLForm descriptor array
935 * @return Array
936 */
937 abstract protected function getFormFields();
938
939 /**
940 * Add pre-text to the form
941 * @return String HTML which will be sent to $form->addPreText()
942 */
943 protected function preText() {
944 return '';
945 }
946
947 /**
948 * Add post-text to the form
949 * @return String HTML which will be sent to $form->addPostText()
950 */
951 protected function postText() {
952 return '';
953 }
954
955 /**
956 * Play with the HTMLForm if you need to more substantially
957 * @param $form HTMLForm
958 */
959 protected function alterForm( HTMLForm $form ) {
960 }
961
962 /**
963 * Get message prefix for HTMLForm
964 *
965 * @since 1.21
966 * @return string
967 */
968 protected function getMessagePrefix() {
969 return strtolower( $this->getName() );
970 }
971
972 /**
973 * Get the HTMLForm to control behavior
974 * @return HTMLForm|null
975 */
976 protected function getForm() {
977 $this->fields = $this->getFormFields();
978
979 $form = new HTMLForm( $this->fields, $this->getContext(), $this->getMessagePrefix() );
980 $form->setSubmitCallback( array( $this, 'onSubmit' ) );
981 // If the form is a compact vertical form, then don't output this ugly
982 // fieldset surrounding it.
983 // XXX Special pages can setDisplayFormat to 'vform' in alterForm(), but that
984 // is called after this.
985 if ( !$form->isVForm() ) {
986 $form->setWrapperLegendMsg( $this->getMessagePrefix() . '-legend' );
987 }
988
989 $headerMsg = $this->msg( $this->getMessagePrefix() . '-text' );
990 if ( !$headerMsg->isDisabled() ) {
991 $form->addHeaderText( $headerMsg->parseAsBlock() );
992 }
993
994 // Retain query parameters (uselang etc)
995 $params = array_diff_key(
996 $this->getRequest()->getQueryValues(), array( 'title' => null ) );
997 $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
998
999 $form->addPreText( $this->preText() );
1000 $form->addPostText( $this->postText() );
1001 $this->alterForm( $form );
1002
1003 // Give hooks a chance to alter the form, adding extra fields or text etc
1004 wfRunHooks( "Special{$this->getName()}BeforeFormDisplay", array( &$form ) );
1005
1006 return $form;
1007 }
1008
1009 /**
1010 * Process the form on POST submission.
1011 * @param $data Array
1012 * @return Bool|Array true for success, false for didn't-try, array of errors on failure
1013 */
1014 abstract public function onSubmit( array $data );
1015
1016 /**
1017 * Do something exciting on successful processing of the form, most likely to show a
1018 * confirmation message
1019 * @since 1.22 Default is to do nothing
1020 */
1021 public function onSuccess() {
1022 }
1023
1024 /**
1025 * Basic SpecialPage workflow: get a form, send it to the user; get some data back,
1026 *
1027 * @param string $par Subpage string if one was specified
1028 */
1029 public function execute( $par ) {
1030 $this->setParameter( $par );
1031 $this->setHeaders();
1032
1033 // This will throw exceptions if there's a problem
1034 $this->checkExecutePermissions( $this->getUser() );
1035
1036 $form = $this->getForm();
1037 if ( $form->show() ) {
1038 $this->onSuccess();
1039 }
1040 }
1041
1042 /**
1043 * Maybe do something interesting with the subpage parameter
1044 * @param string $par
1045 */
1046 protected function setParameter( $par ) {
1047 $this->par = $par;
1048 }
1049
1050 /**
1051 * Called from execute() to check if the given user can perform this action.
1052 * Failures here must throw subclasses of ErrorPageError.
1053 * @param $user User
1054 * @throws UserBlockedError
1055 * @return Bool true
1056 */
1057 protected function checkExecutePermissions( User $user ) {
1058 $this->checkPermissions();
1059
1060 if ( $this->requiresUnblock() && $user->isBlocked() ) {
1061 $block = $user->getBlock();
1062 throw new UserBlockedError( $block );
1063 }
1064
1065 if ( $this->requiresWrite() ) {
1066 $this->checkReadOnly();
1067 }
1068
1069 return true;
1070 }
1071
1072 /**
1073 * Whether this action requires the wiki not to be locked
1074 * @return Bool
1075 */
1076 public function requiresWrite() {
1077 return true;
1078 }
1079
1080 /**
1081 * Whether this action cannot be executed by a blocked user
1082 * @return Bool
1083 */
1084 public function requiresUnblock() {
1085 return true;
1086 }
1087 }
1088
1089 /**
1090 * Shortcut to construct a special page which is unlisted by default
1091 * @ingroup SpecialPage
1092 */
1093 class UnlistedSpecialPage extends SpecialPage {
1094 function __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
1095 parent::__construct( $name, $restriction, false, $function, $file );
1096 }
1097
1098 public function isListed() {
1099 return false;
1100 }
1101 }
1102
1103 /**
1104 * Shortcut to construct an includable special page
1105 * @ingroup SpecialPage
1106 */
1107 class IncludableSpecialPage extends SpecialPage {
1108 function __construct(
1109 $name, $restriction = '', $listed = true, $function = false, $file = 'default'
1110 ) {
1111 parent::__construct( $name, $restriction, $listed, $function, $file, true );
1112 }
1113
1114 public function isIncludable() {
1115 return true;
1116 }
1117 }
1118
1119 /**
1120 * Shortcut to construct a special page alias.
1121 * @ingroup SpecialPage
1122 */
1123 abstract class RedirectSpecialPage extends UnlistedSpecialPage {
1124
1125 // Query parameters that can be passed through redirects
1126 protected $mAllowedRedirectParams = array();
1127
1128 // Query parameters added by redirects
1129 protected $mAddedRedirectParams = array();
1130
1131 public function execute( $par ) {
1132 $redirect = $this->getRedirect( $par );
1133 $query = $this->getRedirectQuery();
1134 // Redirect to a page title with possible query parameters
1135 if ( $redirect instanceof Title ) {
1136 $url = $redirect->getFullURL( $query );
1137 $this->getOutput()->redirect( $url );
1138 return $redirect;
1139 } elseif ( $redirect === true ) {
1140 // Redirect to index.php with query parameters
1141 $url = wfAppendQuery( wfScript( 'index' ), $query );
1142 $this->getOutput()->redirect( $url );
1143 return $redirect;
1144 } else {
1145 $class = get_class( $this );
1146 throw new MWException( "RedirectSpecialPage $class doesn't redirect!" );
1147 }
1148 }
1149
1150 /**
1151 * If the special page is a redirect, then get the Title object it redirects to.
1152 * False otherwise.
1153 *
1154 * @param string $par Subpage string
1155 * @return Title|bool
1156 */
1157 abstract public function getRedirect( $par );
1158
1159 /**
1160 * Return part of the request string for a special redirect page
1161 * This allows passing, e.g. action=history to Special:Mypage, etc.
1162 *
1163 * @return String
1164 */
1165 public function getRedirectQuery() {
1166 $params = array();
1167
1168 foreach ( $this->mAllowedRedirectParams as $arg ) {
1169 if ( $this->getRequest()->getVal( $arg, null ) !== null ) {
1170 $params[$arg] = $this->getRequest()->getVal( $arg );
1171 }
1172 }
1173
1174 foreach ( $this->mAddedRedirectParams as $arg => $val ) {
1175 $params[$arg] = $val;
1176 }
1177
1178 return count( $params )
1179 ? $params
1180 : false;
1181 }
1182 }
1183
1184 abstract class SpecialRedirectToSpecial extends RedirectSpecialPage {
1185 // @todo FIXME: Visibility must be declared
1186 var $redirName, $redirSubpage;
1187
1188 function __construct(
1189 $name, $redirName, $redirSubpage = false,
1190 $allowedRedirectParams = array(), $addedRedirectParams = array()
1191 ) {
1192 parent::__construct( $name );
1193 $this->redirName = $redirName;
1194 $this->redirSubpage = $redirSubpage;
1195 $this->mAllowedRedirectParams = $allowedRedirectParams;
1196 $this->mAddedRedirectParams = $addedRedirectParams;
1197 }
1198
1199 public function getRedirect( $subpage ) {
1200 if ( $this->redirSubpage === false ) {
1201 return SpecialPage::getTitleFor( $this->redirName, $subpage );
1202 } else {
1203 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
1204 }
1205 }
1206 }
1207
1208 /**
1209 * ListAdmins --> ListUsers/sysop
1210 */
1211 class SpecialListAdmins extends SpecialRedirectToSpecial {
1212 function __construct() {
1213 parent::__construct( 'Listadmins', 'Listusers', 'sysop' );
1214 }
1215 }
1216
1217 /**
1218 * ListBots --> ListUsers/bot
1219 */
1220 class SpecialListBots extends SpecialRedirectToSpecial {
1221 function __construct() {
1222 parent::__construct( 'Listbots', 'Listusers', 'bot' );
1223 }
1224 }
1225
1226 /**
1227 * CreateAccount --> UserLogin/signup
1228 * @todo FIXME: This (and the rest of the login frontend) needs to die a horrible painful death
1229 */
1230 class SpecialCreateAccount extends SpecialRedirectToSpecial {
1231 function __construct() {
1232 parent::__construct( 'CreateAccount', 'Userlogin', 'signup', array( 'returnto', 'returntoquery', 'uselang' ) );
1233 }
1234
1235 // No reason to hide this link on Special:Specialpages
1236 public function isListed() {
1237 return true;
1238 }
1239
1240 protected function getGroupName() {
1241 return 'login';
1242 }
1243 }
1244 /**
1245 * SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
1246 * are used to get user independent links pointing to the user page, talk
1247 * page and list of contributions.
1248 * This can let us cache a single copy of any generated content for all
1249 * users.
1250 */
1251
1252 /**
1253 * Superclass for any RedirectSpecialPage which redirects the user
1254 * to a particular article (as opposed to user contributions, logs, etc.).
1255 *
1256 * For security reasons these special pages are restricted to pass on
1257 * the following subset of GET parameters to the target page while
1258 * removing all others:
1259 *
1260 * - useskin, uselang, printable: to alter the appearance of the resulting page
1261 *
1262 * - redirect: allows viewing one's user page or talk page even if it is a
1263 * redirect.
1264 *
1265 * - rdfrom: allows redirecting to one's user page or talk page from an
1266 * external wiki with the "Redirect from..." notice.
1267 *
1268 * - limit, offset: Useful for linking to history of one's own user page or
1269 * user talk page. For example, this would be a link to "the last edit to your
1270 * user talk page in the year 2010":
1271 * http://en.wikipedia.org/wiki/Special:MyPage?offset=20110000000000&limit=1&action=history
1272 *
1273 * - feed: would allow linking to the current user's RSS feed for their user
1274 * talk page:
1275 * http://en.wikipedia.org/w/index.php?title=Special:MyTalk&action=history&feed=rss
1276 *
1277 * - preloadtitle: Can be used to provide a default section title for a
1278 * preloaded new comment on one's own talk page.
1279 *
1280 * - summary : Can be used to provide a default edit summary for a preloaded
1281 * edit to one's own user page or talk page.
1282 *
1283 * - preview: Allows showing/hiding preview on first edit regardless of user
1284 * preference, useful for preloaded edits where you know preview wouldn't be
1285 * useful.
1286 *
1287 * - internaledit, externaledit, mode: Allows forcing the use of the
1288 * internal/external editor, e.g. to force the internal editor for
1289 * short/simple preloaded edits.
1290 *
1291 * - redlink: Affects the message the user sees if their talk page/user talk
1292 * page does not currently exist. Avoids confusion for newbies with no user
1293 * pages over why they got a "permission error" following this link:
1294 * http://en.wikipedia.org/w/index.php?title=Special:MyPage&redlink=1
1295 *
1296 * - debug: determines whether the debug parameter is passed to load.php,
1297 * which disables reformatting and allows scripts to be debugged. Useful
1298 * when debugging scripts that manipulate one's own user page or talk page.
1299 *
1300 * @par Hook extension:
1301 * Extensions can add to the redirect parameters list by using the hook
1302 * RedirectSpecialArticleRedirectParams
1303 *
1304 * This hook allows extensions which add GET parameters like FlaggedRevs to
1305 * retain those parameters when redirecting using special pages.
1306 *
1307 * @par Hook extension example:
1308 * @code
1309 * $wgHooks['RedirectSpecialArticleRedirectParams'][] =
1310 * 'MyExtensionHooks::onRedirectSpecialArticleRedirectParams';
1311 * public static function onRedirectSpecialArticleRedirectParams( &$redirectParams ) {
1312 * $redirectParams[] = 'stable';
1313 * return true;
1314 * }
1315 * @endcode
1316 * @ingroup SpecialPage
1317 */
1318 abstract class RedirectSpecialArticle extends RedirectSpecialPage {
1319 function __construct( $name ) {
1320 parent::__construct( $name );
1321 $redirectParams = array(
1322 'action',
1323 'redirect', 'rdfrom',
1324 # Options for preloaded edits
1325 'preload', 'editintro', 'preloadtitle', 'summary', 'nosummary',
1326 # Options for overriding user settings
1327 'preview', 'internaledit', 'externaledit', 'mode', 'minor', 'watchthis',
1328 # Options for history/diffs
1329 'section', 'oldid', 'diff', 'dir',
1330 'limit', 'offset', 'feed',
1331 # Misc options
1332 'redlink', 'debug',
1333 # Options for action=raw; missing ctype can break JS or CSS in some browsers
1334 'ctype', 'maxage', 'smaxage',
1335 );
1336
1337 wfRunHooks( "RedirectSpecialArticleRedirectParams", array( &$redirectParams ) );
1338 $this->mAllowedRedirectParams = $redirectParams;
1339 }
1340 }
1341
1342 /**
1343 * Shortcut to construct a special page pointing to current user user's page.
1344 * @ingroup SpecialPage
1345 */
1346 class SpecialMypage extends RedirectSpecialArticle {
1347 function __construct() {
1348 parent::__construct( 'Mypage' );
1349 }
1350
1351 function getRedirect( $subpage ) {
1352 if ( strval( $subpage ) !== '' ) {
1353 return Title::makeTitle( NS_USER, $this->getUser()->getName() . '/' . $subpage );
1354 } else {
1355 return Title::makeTitle( NS_USER, $this->getUser()->getName() );
1356 }
1357 }
1358 }
1359
1360 /**
1361 * Shortcut to construct a special page pointing to current user talk page.
1362 * @ingroup SpecialPage
1363 */
1364 class SpecialMytalk extends RedirectSpecialArticle {
1365 function __construct() {
1366 parent::__construct( 'Mytalk' );
1367 }
1368
1369 function getRedirect( $subpage ) {
1370 if ( strval( $subpage ) !== '' ) {
1371 return Title::makeTitle( NS_USER_TALK, $this->getUser()->getName() . '/' . $subpage );
1372 } else {
1373 return Title::makeTitle( NS_USER_TALK, $this->getUser()->getName() );
1374 }
1375 }
1376 }
1377
1378 /**
1379 * Shortcut to construct a special page pointing to current user contributions.
1380 * @ingroup SpecialPage
1381 */
1382 class SpecialMycontributions extends RedirectSpecialPage {
1383 function __construct() {
1384 parent::__construct( 'Mycontributions' );
1385 $this->mAllowedRedirectParams = array( 'limit', 'namespace', 'tagfilter',
1386 'offset', 'dir', 'year', 'month', 'feed' );
1387 }
1388
1389 function getRedirect( $subpage ) {
1390 return SpecialPage::getTitleFor( 'Contributions', $this->getUser()->getName() );
1391 }
1392 }
1393
1394 /**
1395 * Redirect to Special:Listfiles?user=$wgUser
1396 */
1397 class SpecialMyuploads extends RedirectSpecialPage {
1398 function __construct() {
1399 parent::__construct( 'Myuploads' );
1400 $this->mAllowedRedirectParams = array( 'limit', 'ilshowall', 'ilsearch' );
1401 }
1402
1403 function getRedirect( $subpage ) {
1404 return SpecialPage::getTitleFor( 'Listfiles', $this->getUser()->getName() );
1405 }
1406 }
1407
1408 /**
1409 * Redirect Special:Listfiles?user=$wgUser&ilshowall=true
1410 */
1411 class SpecialAllMyUploads extends RedirectSpecialPage {
1412 function __construct() {
1413 parent::__construct( 'AllMyUploads' );
1414 $this->mAllowedRedirectParams = array( 'limit', 'ilsearch' );
1415 }
1416
1417 function getRedirect( $subpage ) {
1418 $this->mAddedRedirectParams['ilshowall'] = 1;
1419 return SpecialPage::getTitleFor( 'Listfiles', $this->getUser()->getName() );
1420 }
1421 }
1422
1423
1424 /**
1425 * Redirect from Special:PermanentLink/### to index.php?oldid=###
1426 */
1427 class SpecialPermanentLink extends RedirectSpecialPage {
1428 function __construct() {
1429 parent::__construct( 'PermanentLink' );
1430 $this->mAllowedRedirectParams = array();
1431 }
1432
1433 function getRedirect( $subpage ) {
1434 $subpage = intval( $subpage );
1435 if ( $subpage === 0 ) {
1436 # throw an error page when no subpage was given
1437 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
1438 }
1439 $this->mAddedRedirectParams['oldid'] = $subpage;
1440 return true;
1441 }
1442 }