Update formatting for includes/specialpage
[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 wfDeprecated( __METHOD__, '1.23' );
480
481 return $this->getPageTitle( $subpage );
482 }
483
484 /**
485 * Get a self-referential title object
486 *
487 * @param string|bool $subpage
488 * @return Title
489 * @since 1.23
490 */
491 function getPageTitle( $subpage = false ) {
492 return self::getTitleFor( $this->mName, $subpage );
493 }
494
495 /**
496 * Sets the context this SpecialPage is executed in
497 *
498 * @param IContextSource $context
499 * @since 1.18
500 */
501 public function setContext( $context ) {
502 $this->mContext = $context;
503 }
504
505 /**
506 * Gets the context this SpecialPage is executed in
507 *
508 * @return IContextSource|RequestContext
509 * @since 1.18
510 */
511 public function getContext() {
512 if ( $this->mContext instanceof IContextSource ) {
513 return $this->mContext;
514 } else {
515 wfDebug( __METHOD__ . " called and \$mContext is null. " .
516 "Return RequestContext::getMain(); for sanity\n" );
517
518 return RequestContext::getMain();
519 }
520 }
521
522 /**
523 * Get the WebRequest being used for this instance
524 *
525 * @return WebRequest
526 * @since 1.18
527 */
528 public function getRequest() {
529 return $this->getContext()->getRequest();
530 }
531
532 /**
533 * Get the OutputPage being used for this instance
534 *
535 * @return OutputPage
536 * @since 1.18
537 */
538 public function getOutput() {
539 return $this->getContext()->getOutput();
540 }
541
542 /**
543 * Shortcut to get the User executing this instance
544 *
545 * @return User
546 * @since 1.18
547 */
548 public function getUser() {
549 return $this->getContext()->getUser();
550 }
551
552 /**
553 * Shortcut to get the skin being used for this instance
554 *
555 * @return Skin
556 * @since 1.18
557 */
558 public function getSkin() {
559 return $this->getContext()->getSkin();
560 }
561
562 /**
563 * Shortcut to get user's language
564 *
565 * @deprecated since 1.19 Use getLanguage instead
566 * @return Language
567 * @since 1.18
568 */
569 public function getLang() {
570 wfDeprecated( __METHOD__, '1.19' );
571
572 return $this->getLanguage();
573 }
574
575 /**
576 * Shortcut to get user's language
577 *
578 * @return Language
579 * @since 1.19
580 */
581 public function getLanguage() {
582 return $this->getContext()->getLanguage();
583 }
584
585 /**
586 * Return the full title, including $par
587 *
588 * @return Title
589 * @since 1.18
590 */
591 public function getFullTitle() {
592 return $this->getContext()->getTitle();
593 }
594
595 /**
596 * Return the robot policy. Derived classes that override this can change
597 * the robot policy set by setHeaders() from the default 'noindex,nofollow'.
598 *
599 * @return string
600 * @since 1.23
601 */
602 protected function getRobotPolicy() {
603 return 'noindex,nofollow';
604 }
605
606 /**
607 * Wrapper around wfMessage that sets the current context.
608 *
609 * @return Message
610 * @see wfMessage
611 */
612 public function msg( /* $args */ ) {
613 $message = call_user_func_array(
614 array( $this->getContext(), 'msg' ),
615 func_get_args()
616 );
617 // RequestContext passes context to wfMessage, and the language is set from
618 // the context, but setting the language for Message class removes the
619 // interface message status, which breaks for example usernameless gender
620 // invocations. Restore the flag when not including special page in content.
621 if ( $this->including() ) {
622 $message->setInterfaceMessageFlag( false );
623 }
624
625 return $message;
626 }
627
628 /**
629 * Adds RSS/atom links
630 *
631 * @param array $params
632 */
633 protected function addFeedLinks( $params ) {
634 global $wgFeedClasses;
635
636 $feedTemplate = wfScript( 'api' );
637
638 foreach ( $wgFeedClasses as $format => $class ) {
639 $theseParams = $params + array( 'feedformat' => $format );
640 $url = wfAppendQuery( $feedTemplate, $theseParams );
641 $this->getOutput()->addFeedLink( $format, $url );
642 }
643 }
644
645 /**
646 * Get the group that the special page belongs in on Special:SpecialPage
647 * Use this method, instead of getGroupName to allow customization
648 * of the group name from the wiki side
649 *
650 * @return string Group of this special page
651 * @since 1.21
652 */
653 public function getFinalGroupName() {
654 global $wgSpecialPageGroups;
655 $name = $this->getName();
656
657 // Allow overbidding the group from the wiki side
658 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
659 if ( !$msg->isBlank() ) {
660 $group = $msg->text();
661 } else {
662 // Than use the group from this object
663 $group = $this->getGroupName();
664
665 // Group '-' is used as default to have the chance to determine,
666 // if the special pages overrides this method,
667 // if not overridden, $wgSpecialPageGroups is checked for b/c
668 if ( $group === '-' && isset( $wgSpecialPageGroups[$name] ) ) {
669 $group = $wgSpecialPageGroups[$name];
670 }
671 }
672
673 // never give '-' back, change to 'other'
674 if ( $group === '-' ) {
675 $group = 'other';
676 }
677
678 return $group;
679 }
680
681 /**
682 * Under which header this special page is listed in Special:SpecialPages
683 * See messages 'specialpages-group-*' for valid names
684 * This method defaults to group 'other'
685 *
686 * @return string
687 * @since 1.21
688 */
689 protected function getGroupName() {
690 // '-' used here to determine, if this group is overridden or has a hardcoded 'other'
691 // Needed for b/c in getFinalGroupName
692 return '-';
693 }
694 }