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