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