Merge "Replace in_array( array_keys( ... ) ) with array_key_exists( ... )"
[lhc/web/wiklou.git] / includes / specialpage / 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 class for all special pages.
26 *
27 * Includes some static functions for handling the special page list deprecated
28 * in favor of SpecialPageFactory.
29 *
30 * @todo Turn this into a real ContextSource
31 * @ingroup SpecialPage
32 */
33 class SpecialPage {
34 // The canonical name of this special page
35 // Also used for the default <h1> heading, @see getDescription()
36 protected $mName;
37
38 // The local name of this special page
39 private $mLocalName;
40
41 // Minimum user level required to access this page, or "" for anyone.
42 // Also used to categorise the pages in Special:Specialpages
43 private $mRestriction;
44
45 // Listed in Special:Specialpages?
46 private $mListed;
47
48 // Function name called by the default execute()
49 private $mFunction;
50
51 // File which needs to be included before the function above can be called
52 private $mFile;
53
54 // Whether or not this special page is being included from an article
55 protected $mIncluding;
56
57 // Whether the special page can be included in an article
58 protected $mIncludable;
59
60 /**
61 * Current request context
62 * @var IContextSource
63 */
64 protected $mContext;
65
66 /**
67 * Get a localised Title object for a specified special page name
68 *
69 * @param string $name
70 * @param string|bool $subpage Subpage string, or false to not use a subpage
71 * @param string $fragment The link fragment (after the "#")
72 * @return Title
73 * @throws MWException
74 */
75 public static function getTitleFor( $name, $subpage = false, $fragment = '' ) {
76 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
77
78 return Title::makeTitle( NS_SPECIAL, $name, $fragment );
79 }
80
81 /**
82 * Get a localised Title object for a page name with a possibly unvalidated subpage
83 *
84 * @param string $name
85 * @param string|bool $subpage Subpage string, or false to not use a subpage
86 * @return Title|null Title object or null if the page doesn't exist
87 */
88 public static function getSafeTitleFor( $name, $subpage = false ) {
89 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
90 if ( $name ) {
91 return Title::makeTitleSafe( NS_SPECIAL, $name );
92 } else {
93 return null;
94 }
95 }
96
97 /**
98 * Default constructor for special pages
99 * Derivative classes should call this from their constructor
100 * Note that if the user does not have the required level, an error message will
101 * be displayed by the default execute() method, without the global function ever
102 * being called.
103 *
104 * If you override execute(), you can recover the default behavior with userCanExecute()
105 * and displayRestrictionError()
106 *
107 * @param string $name Name of the special page, as seen in links and URLs
108 * @param string $restriction User right required, e.g. "block" or "delete"
109 * @param bool $listed Whether the page is listed in Special:Specialpages
110 * @param callable|bool $function Function called by execute(). By default
111 * it is constructed from $name
112 * @param string $file File which is included by execute(). It is also
113 * constructed from $name by default
114 * @param bool $includable Whether the page can be included in normal pages
115 */
116 public function __construct(
117 $name = '', $restriction = '', $listed = true,
118 $function = false, $file = 'default', $includable = false
119 ) {
120 $this->mName = $name;
121 $this->mRestriction = $restriction;
122 $this->mListed = $listed;
123 $this->mIncludable = $includable;
124 if ( !$function ) {
125 $this->mFunction = 'wfSpecial' . $name;
126 } else {
127 $this->mFunction = $function;
128 }
129 if ( $file === 'default' ) {
130 $this->mFile = __DIR__ . "/specials/Special$name.php";
131 } else {
132 $this->mFile = $file;
133 }
134 }
135
136 /**
137 * Get the name of this Special Page.
138 * @return string
139 */
140 function getName() {
141 return $this->mName;
142 }
143
144 /**
145 * Get the permission that a user must have to execute this page
146 * @return string
147 */
148 function getRestriction() {
149 return $this->mRestriction;
150 }
151
152 /**
153 * Get the file which will be included by SpecialPage::execute() if your extension is
154 * still stuck in the past and hasn't overridden the execute() method. No modern code
155 * should want or need to know this.
156 * @return string
157 * @deprecated since 1.18
158 */
159 function getFile() {
160 wfDeprecated( __METHOD__, '1.18' );
161
162 return $this->mFile;
163 }
164
165 // @todo FIXME: Decide which syntax to use for this, and stick to it
166 /**
167 * Whether this special page is listed in Special:SpecialPages
168 * @since r3583 (v1.3)
169 * @return bool
170 */
171 function isListed() {
172 return $this->mListed;
173 }
174
175 /**
176 * Set whether this page is listed in Special:Specialpages, at run-time
177 * @since 1.3
178 * @param bool $listed
179 * @return bool
180 */
181 function setListed( $listed ) {
182 return wfSetVar( $this->mListed, $listed );
183 }
184
185 /**
186 * Get or set whether this special page is listed in Special:SpecialPages
187 * @since 1.6
188 * @param bool $x
189 * @return bool
190 */
191 function listed( $x = null ) {
192 return wfSetVar( $this->mListed, $x );
193 }
194
195 /**
196 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
197 * @return bool
198 */
199 public function isIncludable() {
200 return $this->mIncludable;
201 }
202
203 /**
204 * Whether the special page is being evaluated via transclusion
205 * @param bool $x
206 * @return bool
207 */
208 function including( $x = null ) {
209 return wfSetVar( $this->mIncluding, $x );
210 }
211
212 /**
213 * Get the localised name of the special page
214 * @return string
215 */
216 function getLocalName() {
217 if ( !isset( $this->mLocalName ) ) {
218 $this->mLocalName = SpecialPageFactory::getLocalNameFor( $this->mName );
219 }
220
221 return $this->mLocalName;
222 }
223
224 /**
225 * Is this page expensive (for some definition of expensive)?
226 * Expensive pages are disabled or cached in miser mode. Originally used
227 * (and still overridden) by QueryPage and subclasses, moved here so that
228 * Special:SpecialPages can safely call it for all special pages.
229 *
230 * @return bool
231 */
232 public function isExpensive() {
233 return false;
234 }
235
236 /**
237 * Is this page cached?
238 * Expensive pages are cached or disabled in miser mode.
239 * Used by QueryPage and subclasses, moved here so that
240 * Special:SpecialPages can safely call it for all special pages.
241 *
242 * @return bool
243 * @since 1.21
244 */
245 public function isCached() {
246 return false;
247 }
248
249 /**
250 * Can be overridden by subclasses with more complicated permissions
251 * schemes.
252 *
253 * @return bool Should the page be displayed with the restricted-access
254 * pages?
255 */
256 public function isRestricted() {
257 // DWIM: If anons can do something, then it is not restricted
258 return $this->mRestriction != '' && !User::groupHasPermission( '*', $this->mRestriction );
259 }
260
261 /**
262 * Checks if the given user (identified by an object) can execute this
263 * special page (as defined by $mRestriction). Can be overridden by sub-
264 * classes with more complicated permissions schemes.
265 *
266 * @param User $user The user to check
267 * @return bool Does the user have permission to view the page?
268 */
269 public function userCanExecute( User $user ) {
270 return $user->isAllowed( $this->mRestriction );
271 }
272
273 /**
274 * Output an error message telling the user what access level they have to have
275 * @throws PermissionsError
276 */
277 function displayRestrictionError() {
278 throw new PermissionsError( $this->mRestriction );
279 }
280
281 /**
282 * Checks if userCanExecute, and if not throws a PermissionsError
283 *
284 * @since 1.19
285 * @return void
286 * @throws PermissionsError
287 */
288 public function checkPermissions() {
289 if ( !$this->userCanExecute( $this->getUser() ) ) {
290 $this->displayRestrictionError();
291 }
292 }
293
294 /**
295 * If the wiki is currently in readonly mode, throws a ReadOnlyError
296 *
297 * @since 1.19
298 * @return void
299 * @throws ReadOnlyError
300 */
301 public function checkReadOnly() {
302 if ( wfReadOnly() ) {
303 throw new ReadOnlyError;
304 }
305 }
306
307 /**
308 * If the user is not logged in, throws UserNotLoggedIn error.
309 *
310 * Default error message includes a link to Special:Userlogin with properly set 'returnto' query
311 * parameter.
312 *
313 * @since 1.23
314 * @param string|Message $reasonMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
315 * will be used as message keys. If a string is given, the message will also receive a
316 * formatted login link (generated using the 'loginreqlink' message) as first parameter. If a
317 * Message is given, it will be passed on verbatim.
318 * @param string|Message $titleMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
319 * will be used as message keys.
320 * @throws UserNotLoggedIn
321 */
322 public function requireLogin( $reasonMsg = null, $titleMsg = null ) {
323 if ( $this->getUser()->isAnon() ) {
324 // Use default messages if not given or explicit null passed
325 if ( !$reasonMsg ) {
326 $reasonMsg = 'exception-nologin-text-manual';
327 }
328 if ( !$titleMsg ) {
329 $titleMsg = 'exception-nologin';
330 }
331
332 // Convert to Messages with current context
333 if ( is_string( $reasonMsg ) ) {
334 $loginreqlink = Linker::linkKnown(
335 SpecialPage::getTitleFor( 'Userlogin' ),
336 $this->msg( 'loginreqlink' )->escaped(),
337 array(),
338 array( 'returnto' => $this->getPageTitle()->getPrefixedText() )
339 );
340 $reasonMsg = $this->msg( $reasonMsg )->rawParams( $loginreqlink );
341 }
342 if ( is_string( $titleMsg ) ) {
343 $titleMsg = $this->msg( $titleMsg );
344 }
345
346 throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
347 }
348 }
349
350 /**
351 * Sets headers - this should be called from the execute() method of all derived classes!
352 */
353 function setHeaders() {
354 $out = $this->getOutput();
355 $out->setArticleRelated( false );
356 $out->setRobotPolicy( $this->getRobotPolicy() );
357 $out->setPageTitle( $this->getDescription() );
358 }
359
360 /**
361 * Entry point.
362 *
363 * @since 1.20
364 *
365 * @param string|null $subPage
366 */
367 final public function run( $subPage ) {
368 /**
369 * Gets called before @see SpecialPage::execute.
370 *
371 * @since 1.20
372 *
373 * @param SpecialPage $this
374 * @param string|null $subPage
375 */
376 wfRunHooks( 'SpecialPageBeforeExecute', array( $this, $subPage ) );
377
378 $this->beforeExecute( $subPage );
379 $this->execute( $subPage );
380 $this->afterExecute( $subPage );
381
382 /**
383 * Gets called after @see SpecialPage::execute.
384 *
385 * @since 1.20
386 *
387 * @param SpecialPage $this
388 * @param string|null $subPage
389 */
390 wfRunHooks( 'SpecialPageAfterExecute', array( $this, $subPage ) );
391 }
392
393 /**
394 * Gets called before @see SpecialPage::execute.
395 *
396 * @since 1.20
397 *
398 * @param string|null $subPage
399 */
400 protected function beforeExecute( $subPage ) {
401 // No-op
402 }
403
404 /**
405 * Gets called after @see SpecialPage::execute.
406 *
407 * @since 1.20
408 *
409 * @param string|null $subPage
410 */
411 protected function afterExecute( $subPage ) {
412 // No-op
413 }
414
415 /**
416 * Default execute method
417 * Checks user permissions, calls the function given in mFunction
418 *
419 * This must be overridden by subclasses; it will be made abstract in a future version
420 *
421 * @param string|null $subPage
422 */
423 public function execute( $subPage ) {
424 $this->setHeaders();
425 $this->checkPermissions();
426
427 $func = $this->mFunction;
428 // only load file if the function does not exist
429 if ( !is_callable( $func ) && $this->mFile ) {
430 require_once $this->mFile;
431 }
432 $this->outputHeader();
433 call_user_func( $func, $subPage, $this );
434 }
435
436 /**
437 * Outputs a summary message on top of special pages
438 * Per default the message key is the canonical name of the special page
439 * May be overridden, i.e. by extensions to stick with the naming conventions
440 * for message keys: 'extensionname-xxx'
441 *
442 * @param string $summaryMessageKey Message key of the summary
443 */
444 function outputHeader( $summaryMessageKey = '' ) {
445 global $wgContLang;
446
447 if ( $summaryMessageKey == '' ) {
448 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
449 } else {
450 $msg = $summaryMessageKey;
451 }
452 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
453 $this->getOutput()->wrapWikiMsg(
454 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
455 }
456 }
457
458 /**
459 * Returns the name that goes in the \<h1\> in the special page itself, and
460 * also the name that will be listed in Special:Specialpages
461 *
462 * Derived classes can override this, but usually it is easier to keep the
463 * default behavior.
464 *
465 * @return string
466 */
467 function getDescription() {
468 return $this->msg( strtolower( $this->mName ) )->text();
469 }
470
471 /**
472 * Get a self-referential title object
473 *
474 * @param string|bool $subpage
475 * @return Title
476 * @deprecated in 1.23, use SpecialPage::getPageTitle
477 */
478 function getTitle( $subpage = false ) {
479 return $this->getPageTitle( $subpage );
480 }
481
482 /**
483 * Get a self-referential title object
484 *
485 * @param string|bool $subpage
486 * @return Title
487 * @since 1.23
488 */
489 function getPageTitle( $subpage = false ) {
490 return self::getTitleFor( $this->mName, $subpage );
491 }
492
493 /**
494 * Sets the context this SpecialPage is executed in
495 *
496 * @param IContextSource $context
497 * @since 1.18
498 */
499 public function setContext( $context ) {
500 $this->mContext = $context;
501 }
502
503 /**
504 * Gets the context this SpecialPage is executed in
505 *
506 * @return IContextSource|RequestContext
507 * @since 1.18
508 */
509 public function getContext() {
510 if ( $this->mContext instanceof IContextSource ) {
511 return $this->mContext;
512 } else {
513 wfDebug( __METHOD__ . " called and \$mContext is null. " .
514 "Return RequestContext::getMain(); for sanity\n" );
515
516 return RequestContext::getMain();
517 }
518 }
519
520 /**
521 * Get the WebRequest being used for this instance
522 *
523 * @return WebRequest
524 * @since 1.18
525 */
526 public function getRequest() {
527 return $this->getContext()->getRequest();
528 }
529
530 /**
531 * Get the OutputPage being used for this instance
532 *
533 * @return OutputPage
534 * @since 1.18
535 */
536 public function getOutput() {
537 return $this->getContext()->getOutput();
538 }
539
540 /**
541 * Shortcut to get the User executing this instance
542 *
543 * @return User
544 * @since 1.18
545 */
546 public function getUser() {
547 return $this->getContext()->getUser();
548 }
549
550 /**
551 * Shortcut to get the skin being used for this instance
552 *
553 * @return Skin
554 * @since 1.18
555 */
556 public function getSkin() {
557 return $this->getContext()->getSkin();
558 }
559
560 /**
561 * Shortcut to get user's language
562 *
563 * @deprecated since 1.19 Use getLanguage instead
564 * @return Language
565 * @since 1.18
566 */
567 public function getLang() {
568 wfDeprecated( __METHOD__, '1.19' );
569
570 return $this->getLanguage();
571 }
572
573 /**
574 * Shortcut to get user's language
575 *
576 * @return Language
577 * @since 1.19
578 */
579 public function getLanguage() {
580 return $this->getContext()->getLanguage();
581 }
582
583 /**
584 * Return the full title, including $par
585 *
586 * @return Title
587 * @since 1.18
588 */
589 public function getFullTitle() {
590 return $this->getContext()->getTitle();
591 }
592
593 /**
594 * Return the robot policy. Derived classes that override this can change
595 * the robot policy set by setHeaders() from the default 'noindex,nofollow'.
596 *
597 * @return string
598 * @since 1.23
599 */
600 protected function getRobotPolicy() {
601 return 'noindex,nofollow';
602 }
603
604 /**
605 * Wrapper around wfMessage that sets the current context.
606 *
607 * @return Message
608 * @see wfMessage
609 */
610 public function msg( /* $args */ ) {
611 $message = call_user_func_array(
612 array( $this->getContext(), 'msg' ),
613 func_get_args()
614 );
615 // RequestContext passes context to wfMessage, and the language is set from
616 // the context, but setting the language for Message class removes the
617 // interface message status, which breaks for example usernameless gender
618 // invocations. Restore the flag when not including special page in content.
619 if ( $this->including() ) {
620 $message->setInterfaceMessageFlag( false );
621 }
622
623 return $message;
624 }
625
626 /**
627 * Adds RSS/atom links
628 *
629 * @param array $params
630 */
631 protected function addFeedLinks( $params ) {
632 global $wgFeedClasses;
633
634 $feedTemplate = wfScript( 'api' );
635
636 foreach ( $wgFeedClasses as $format => $class ) {
637 $theseParams = $params + array( 'feedformat' => $format );
638 $url = wfAppendQuery( $feedTemplate, $theseParams );
639 $this->getOutput()->addFeedLink( $format, $url );
640 }
641 }
642
643 /**
644 * Get the group that the special page belongs in on Special:SpecialPage
645 * Use this method, instead of getGroupName to allow customization
646 * of the group name from the wiki side
647 *
648 * @return string Group of this special page
649 * @since 1.21
650 */
651 public function getFinalGroupName() {
652 global $wgSpecialPageGroups;
653 $name = $this->getName();
654
655 // Allow overbidding the group from the wiki side
656 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
657 if ( !$msg->isBlank() ) {
658 $group = $msg->text();
659 } else {
660 // Than use the group from this object
661 $group = $this->getGroupName();
662
663 // Group '-' is used as default to have the chance to determine,
664 // if the special pages overrides this method,
665 // if not overridden, $wgSpecialPageGroups is checked for b/c
666 if ( $group === '-' && isset( $wgSpecialPageGroups[$name] ) ) {
667 $group = $wgSpecialPageGroups[$name];
668 }
669 }
670
671 // never give '-' back, change to 'other'
672 if ( $group === '-' ) {
673 $group = 'other';
674 }
675
676 return $group;
677 }
678
679 /**
680 * Under which header this special page is listed in Special:SpecialPages
681 * See messages 'specialpages-group-*' for valid names
682 * This method defaults to group 'other'
683 *
684 * @return string
685 * @since 1.21
686 */
687 protected function getGroupName() {
688 // '-' used here to determine, if this group is overridden or has a hardcoded 'other'
689 // Needed for b/c in getFinalGroupName
690 return '-';
691 }
692 }