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