* Bidi-aid on list pages
[mediawiki.git] / includes / OutputPage.php
blob1f531a6e759a1fd9a5a1f4b3c80d66b80d7f4fcf
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( -1 );
4 /**
5 * @package MediaWiki
6 */
8 if ( $wgUseTeX )
9 require_once 'Math.php';
11 /**
12 * @todo document
13 * @package MediaWiki
15 class OutputPage {
16 var $mHeaders, $mMetatags, $mKeywords;
17 var $mLinktags, $mPagetitle, $mBodytext, $mDebugtext;
18 var $mHTMLtitle, $mRobotpolicy, $mIsarticle, $mPrintable;
19 var $mSubtitle, $mRedirect, $mStatusCode;
20 var $mLastModified, $mETag, $mCategoryLinks;
21 var $mScripts, $mLinkColours, $mPageLinkTitle;
23 var $mSuppressQuickbar;
24 var $mOnloadHandler;
25 var $mDoNothing;
26 var $mContainsOldMagic, $mContainsNewMagic;
27 var $mIsArticleRelated;
28 var $mParserOptions;
29 var $mShowFeedLinks = false;
30 var $mEnableClientCache = true;
31 var $mArticleBodyOnly = false;
33 var $mNewSectionLink = false;
35 /**
36 * Constructor
37 * Initialise private variables
39 function OutputPage() {
40 $this->mHeaders = $this->mMetatags =
41 $this->mKeywords = $this->mLinktags = array();
42 $this->mHTMLtitle = $this->mPagetitle = $this->mBodytext =
43 $this->mRedirect = $this->mLastModified =
44 $this->mSubtitle = $this->mDebugtext = $this->mRobotpolicy =
45 $this->mOnloadHandler = $this->mPageLinkTitle = '';
46 $this->mIsArticleRelated = $this->mIsarticle = $this->mPrintable = true;
47 $this->mSuppressQuickbar = $this->mPrintable = false;
48 $this->mLanguageLinks = array();
49 $this->mCategoryLinks = array();
50 $this->mDoNothing = false;
51 $this->mContainsOldMagic = $this->mContainsNewMagic = 0;
52 $this->mParserOptions = ParserOptions::newFromUser( $temp = NULL );
53 $this->mSquidMaxage = 0;
54 $this->mScripts = '';
55 $this->mETag = false;
56 $this->mRevisionId = null;
57 $this->mNewSectionLink = false;
60 function addHeader( $name, $val ) { array_push( $this->mHeaders, $name.': '.$val ); }
61 function redirect( $url, $responsecode = '302' ) { $this->mRedirect = $url; $this->mRedirectCode = $responsecode; }
62 function setStatusCode( $statusCode ) { $this->mStatusCode = $statusCode; }
64 # To add an http-equiv meta tag, precede the name with "http:"
65 function addMeta( $name, $val ) { array_push( $this->mMetatags, array( $name, $val ) ); }
66 function addKeyword( $text ) { array_push( $this->mKeywords, $text ); }
67 function addScript( $script ) { $this->mScripts .= $script; }
68 function getScript() { return $this->mScripts; }
70 function setETag($tag) { $this->mETag = $tag; }
71 function setArticleBodyOnly($only) { $this->mArticleBodyOnly = $only; }
72 function getArticleBodyOnly($only) { return $this->mArticleBodyOnly; }
74 function addLink( $linkarr ) {
75 # $linkarr should be an associative array of attributes. We'll escape on output.
76 array_push( $this->mLinktags, $linkarr );
79 function addMetadataLink( $linkarr ) {
80 # note: buggy CC software only reads first "meta" link
81 static $haveMeta = false;
82 $linkarr['rel'] = ($haveMeta) ? 'alternate meta' : 'meta';
83 $this->addLink( $linkarr );
84 $haveMeta = true;
87 /**
88 * checkLastModified tells the client to use the client-cached page if
89 * possible. If sucessful, the OutputPage is disabled so that
90 * any future call to OutputPage->output() have no effect. The method
91 * returns true iff cache-ok headers was sent.
93 function checkLastModified ( $timestamp ) {
94 global $wgCachePages, $wgCacheEpoch, $wgUser;
95 if ( !$timestamp || $timestamp == '19700101000000' ) {
96 wfDebug( "CACHE DISABLED, NO TIMESTAMP\n" );
97 return;
99 if( !$wgCachePages ) {
100 wfDebug( "CACHE DISABLED\n", false );
101 return;
103 if( $wgUser->getOption( 'nocache' ) ) {
104 wfDebug( "USER DISABLED CACHE\n", false );
105 return;
108 $timestamp=wfTimestamp(TS_MW,$timestamp);
109 $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched, $wgCacheEpoch ) );
111 if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
112 # IE sends sizes after the date like this:
113 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
114 # this breaks strtotime().
115 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
116 $modsinceTime = strtotime( $modsince );
117 $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
118 wfDebug( "-- client send If-Modified-Since: " . $modsince . "\n", false );
119 wfDebug( "-- we might send Last-Modified : $lastmod\n", false );
120 if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
121 # Make sure you're in a place you can leave when you call us!
122 header( "HTTP/1.0 304 Not Modified" );
123 $this->mLastModified = $lastmod;
124 $this->sendCacheControl();
125 wfDebug( "CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
126 $this->disable();
127 @ob_end_clean(); // Don't output compressed blob
128 return true;
129 } else {
130 wfDebug( "READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
131 $this->mLastModified = $lastmod;
133 } else {
134 wfDebug( "client did not send If-Modified-Since header\n", false );
135 $this->mLastModified = $lastmod;
139 function getPageTitleActionText () {
140 global $action;
141 switch($action) {
142 case 'edit':
143 case 'delete':
144 case 'protect':
145 case 'unprotect':
146 case 'watch':
147 case 'unwatch':
148 // Display title is already customized
149 return '';
150 case 'history':
151 return wfMsg('history_short');
152 case 'submit':
153 // FIXME: bug 2735; not correct for special pages etc
154 return wfMsg('preview');
155 case 'info':
156 return wfMsg('info_short');
157 default:
158 return '';
162 function setRobotpolicy( $str ) { $this->mRobotpolicy = $str; }
163 function setHTMLTitle( $name ) {$this->mHTMLtitle = $name; }
164 function setPageTitle( $name ) {
165 global $action, $wgContLang;
166 $name = $wgContLang->convert($name, true);
167 $this->mPagetitle = $name;
168 if(!empty($action)) {
169 $taction = $this->getPageTitleActionText();
170 if( !empty( $taction ) ) {
171 $name .= ' - '.$taction;
175 $this->setHTMLTitle( wfMsg( 'pagetitle', $name ) );
177 function getHTMLTitle() { return $this->mHTMLtitle; }
178 function getPageTitle() { return $this->mPagetitle; }
179 function setSubtitle( $str ) { $this->mSubtitle = /*$this->parse(*/$str/*)*/; } // @bug 2514
180 function getSubtitle() { return $this->mSubtitle; }
181 function isArticle() { return $this->mIsarticle; }
182 function setPrintable() { $this->mPrintable = true; }
183 function isPrintable() { return $this->mPrintable; }
184 function setSyndicated( $show = true ) { $this->mShowFeedLinks = $show; }
185 function isSyndicated() { return $this->mShowFeedLinks; }
186 function setOnloadHandler( $js ) { $this->mOnloadHandler = $js; }
187 function getOnloadHandler() { return $this->mOnloadHandler; }
188 function disable() { $this->mDoNothing = true; }
190 function setArticleRelated( $v ) {
191 $this->mIsArticleRelated = $v;
192 if ( !$v ) {
193 $this->mIsarticle = false;
196 function setArticleFlag( $v ) {
197 $this->mIsarticle = $v;
198 if ( $v ) {
199 $this->mIsArticleRelated = $v;
203 function isArticleRelated() { return $this->mIsArticleRelated; }
205 function getLanguageLinks() { return $this->mLanguageLinks; }
206 function addLanguageLinks($newLinkArray) {
207 $this->mLanguageLinks += $newLinkArray;
209 function setLanguageLinks($newLinkArray) {
210 $this->mLanguageLinks = $newLinkArray;
213 function getCategoryLinks() {
214 return $this->mCategoryLinks;
218 * Add an array of categories, with names in the keys
220 function addCategoryLinks($categories) {
221 global $wgUser, $wgContLang;
223 if ( !is_array( $categories ) ) {
224 return;
226 # Add the links to the link cache in a batch
227 $arr = array( NS_CATEGORY => $categories );
228 $lb = new LinkBatch;
229 $lb->setArray( $arr );
230 $lb->execute();
232 $sk =& $wgUser->getSkin();
233 foreach ( $categories as $category => $arbitrary ) {
234 $title = Title::makeTitleSafe( NS_CATEGORY, $category );
235 $text = $wgContLang->convertHtml( $title->getText() );
236 $this->mCategoryLinks[] = $sk->makeLinkObj( $title, $text );
240 function setCategoryLinks($categories) {
241 $this->mCategoryLinks = array();
242 $this->addCategoryLinks($categories);
245 function suppressQuickbar() { $this->mSuppressQuickbar = true; }
246 function isQuickbarSuppressed() { return $this->mSuppressQuickbar; }
248 function addHTML( $text ) { $this->mBodytext .= $text; }
249 function clearHTML() { $this->mBodytext = ''; }
250 function getHTML() { return $this->mBodytext; }
251 function debug( $text ) { $this->mDebugtext .= $text; }
253 /* @deprecated */
254 function setParserOptions( $options ) {
255 return $this->ParserOptions( $options );
258 function ParserOptions( $options = null ) {
259 return wfSetVar( $this->mParserOptions, $options );
263 * Set the revision ID which will be seen by the wiki text parser
264 * for things such as embedded {{REVISIONID}} variable use.
265 * @param mixed $revid an integer, or NULL
266 * @return mixed previous value
268 function setRevisionId( $revid ) {
269 $val = is_null( $revid ) ? null : intval( $revid );
270 return wfSetVar( $this->mRevisionId, $val );
274 * Convert wikitext to HTML and add it to the buffer
275 * Default assumes that the current page title will
276 * be used.
278 function addWikiText( $text, $linestart = true ) {
279 global $wgTitle;
280 $this->addWikiTextTitle($text, $wgTitle, $linestart);
283 function addWikiTextWithTitle($text, &$title, $linestart = true) {
284 $this->addWikiTextTitle($text, $title, $linestart);
287 function addWikiTextTitle($text, &$title, $linestart) {
288 global $wgParser;
289 $parserOutput = $wgParser->parse( $text, $title, $this->mParserOptions,
290 $linestart, true, $this->mRevisionId );
291 $this->addParserOutput( $parserOutput );
294 function addParserOutputNoText( &$parserOutput ) {
295 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
296 $this->addCategoryLinks( $parserOutput->getCategories() );
297 $this->mNewSectionLink = $parserOutput->getNewSection();
298 $this->addKeywords( $parserOutput );
299 if ( $parserOutput->getCacheTime() == -1 ) {
300 $this->enableClientCache( false );
302 if ( $parserOutput->mHTMLtitle != "" ) {
303 $this->mPagetitle = $parserOutput->mHTMLtitle ;
304 $this->mSubtitle .= $parserOutput->mSubtitle ;
308 function addParserOutput( &$parserOutput ) {
309 $this->addParserOutputNoText( $parserOutput );
310 $this->addHTML( $parserOutput->getText() );
314 * Add wikitext to the buffer, assuming that this is the primary text for a page view
315 * Saves the text into the parser cache if possible
317 function addPrimaryWikiText( $text, $article, $cache = true ) {
318 global $wgParser, $wgUser;
320 $this->mParserOptions->setTidy(true);
321 $parserOutput = $wgParser->parse( $text, $article->mTitle,
322 $this->mParserOptions, true, true, $this->mRevisionId );
323 $this->mParserOptions->setTidy(false);
324 if ( $cache && $article && $parserOutput->getCacheTime() != -1 ) {
325 $parserCache =& ParserCache::singleton();
326 $parserCache->save( $parserOutput, $article, $wgUser );
329 $this->addParserOutputNoText( $parserOutput );
330 $text = $parserOutput->getText();
331 wfRunHooks( 'OutputPageBeforeHTML',array( &$this, &$text ) );
332 $parserOutput->setText( $text );
333 $this->addHTML( $parserOutput->getText() );
337 * For anything that isn't primary text or interface message
339 function addSecondaryWikiText( $text, $linestart = true ) {
340 global $wgTitle;
341 $this->mParserOptions->setTidy(true);
342 $this->addWikiTextTitle($text, $wgTitle, $linestart);
343 $this->mParserOptions->setTidy(false);
348 * Add the output of a QuickTemplate to the output buffer
349 * @param QuickTemplate $template
351 function addTemplate( &$template ) {
352 ob_start();
353 $template->execute();
354 $this->addHTML( ob_get_contents() );
355 ob_end_clean();
359 * Parse wikitext and return the HTML.
361 function parse( $text, $linestart = true, $interface = false ) {
362 global $wgParser, $wgTitle;
363 if ( $interface) { $this->mParserOptions->setInterfaceMessage(true); }
364 $parserOutput = $wgParser->parse( $text, $wgTitle, $this->mParserOptions,
365 $linestart, true, $this->mRevisionId );
366 if ( $interface) { $this->mParserOptions->setInterfaceMessage(false); }
367 return $parserOutput->getText();
371 * @param $article
372 * @param $user
374 * @return bool
376 function tryParserCache( &$article, $user ) {
377 $parserCache =& ParserCache::singleton();
378 $parserOutput = $parserCache->get( $article, $user );
379 if ( $parserOutput !== false ) {
380 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
381 $this->addCategoryLinks( $parserOutput->getCategories() );
382 $this->addKeywords( $parserOutput );
383 $this->mNewSectionLink = $parserOutput->getNewSection();
384 $text = $parserOutput->getText();
385 wfRunHooks( 'OutputPageBeforeHTML', array( &$this, &$text ) );
386 $this->addHTML( $text );
387 $t = $parserOutput->getTitleText();
388 if( !empty( $t ) ) {
389 $this->setPageTitle( $t );
391 return true;
392 } else {
393 return false;
398 * Set the maximum cache time on the Squid in seconds
399 * @param $maxage
401 function setSquidMaxage( $maxage ) {
402 $this->mSquidMaxage = $maxage;
406 * Use enableClientCache(false) to force it to send nocache headers
407 * @param $state
409 function enableClientCache( $state ) {
410 return wfSetVar( $this->mEnableClientCache, $state );
413 function uncacheableBecauseRequestvars() {
414 global $wgRequest;
415 return $wgRequest->getText('useskin', false) === false
416 && $wgRequest->getText('uselang', false) === false;
419 function sendCacheControl() {
420 global $wgUseSquid, $wgUseESI, $wgSquidMaxage;
422 if ($this->mETag)
423 header("ETag: $this->mETag");
425 # don't serve compressed data to clients who can't handle it
426 # maintain different caches for logged-in users and non-logged in ones
427 header( 'Vary: Accept-Encoding, Cookie' );
428 if( !$this->uncacheableBecauseRequestvars() && $this->mEnableClientCache ) {
429 if( $wgUseSquid && ! isset( $_COOKIE[ini_get( 'session.name') ] ) &&
430 ! $this->isPrintable() && $this->mSquidMaxage != 0 )
432 if ( $wgUseESI ) {
433 # We'll purge the proxy cache explicitly, but require end user agents
434 # to revalidate against the proxy on each visit.
435 # Surrogate-Control controls our Squid, Cache-Control downstream caches
436 wfDebug( "** proxy caching with ESI; {$this->mLastModified} **\n", false );
437 # start with a shorter timeout for initial testing
438 # header( 'Surrogate-Control: max-age=2678400+2678400, content="ESI/1.0"');
439 header( 'Surrogate-Control: max-age='.$wgSquidMaxage.'+'.$this->mSquidMaxage.', content="ESI/1.0"');
440 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
441 } else {
442 # We'll purge the proxy cache for anons explicitly, but require end user agents
443 # to revalidate against the proxy on each visit.
444 # IMPORTANT! The Squid needs to replace the Cache-Control header with
445 # Cache-Control: s-maxage=0, must-revalidate, max-age=0
446 wfDebug( "** local proxy caching; {$this->mLastModified} **\n", false );
447 # start with a shorter timeout for initial testing
448 # header( "Cache-Control: s-maxage=2678400, must-revalidate, max-age=0" );
449 header( 'Cache-Control: s-maxage='.$this->mSquidMaxage.', must-revalidate, max-age=0' );
451 } else {
452 # We do want clients to cache if they can, but they *must* check for updates
453 # on revisiting the page.
454 wfDebug( "** private caching; {$this->mLastModified} **\n", false );
455 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
456 header( "Cache-Control: private, must-revalidate, max-age=0" );
458 if($this->mLastModified) header( "Last-modified: {$this->mLastModified}" );
459 } else {
460 wfDebug( "** no caching **\n", false );
462 # In general, the absence of a last modified header should be enough to prevent
463 # the client from using its cache. We send a few other things just to make sure.
464 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
465 header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
466 header( 'Pragma: no-cache' );
471 * Finally, all the text has been munged and accumulated into
472 * the object, let's actually output it:
474 function output() {
475 global $wgUser, $wgOutputEncoding;
476 global $wgContLanguageCode, $wgDebugRedirects, $wgMimeType;
477 global $wgJsMimeType, $wgStylePath, $wgUseAjax, $wgScriptPath, $wgServer;
479 if( $this->mDoNothing ){
480 return;
482 $fname = 'OutputPage::output';
483 wfProfileIn( $fname );
484 $sk = $wgUser->getSkin();
486 if ( $wgUseAjax ) {
487 $this->addScript( "<script type=\"{$wgJsMimeType}\">
488 var wgScriptPath=\"{$wgScriptPath}\";
489 var wgServer=\"{$wgServer}\";
490 </script>" );
491 $this->addScript( "<script type=\"{$wgJsMimeType}\" src=\"{$wgStylePath}/common/ajax.js\"></script>\n" );
494 if ( '' != $this->mRedirect ) {
495 if( substr( $this->mRedirect, 0, 4 ) != 'http' ) {
496 # Standards require redirect URLs to be absolute
497 global $wgServer;
498 $this->mRedirect = $wgServer . $this->mRedirect;
500 if( $this->mRedirectCode == '301') {
501 if( !$wgDebugRedirects ) {
502 header("HTTP/1.1 {$this->mRedirectCode} Moved Permanently");
504 $this->mLastModified = wfTimestamp( TS_RFC2822 );
507 $this->sendCacheControl();
509 if( $wgDebugRedirects ) {
510 $url = htmlspecialchars( $this->mRedirect );
511 print "<html>\n<head>\n<title>Redirect</title>\n</head>\n<body>\n";
512 print "<p>Location: <a href=\"$url\">$url</a></p>\n";
513 print "</body>\n</html>\n";
514 } else {
515 header( 'Location: '.$this->mRedirect );
517 wfProfileOut( $fname );
518 return;
520 elseif ( $this->mStatusCode )
522 $statusMessage = array(
523 100 => 'Continue',
524 101 => 'Switching Protocols',
525 102 => 'Processing',
526 200 => 'OK',
527 201 => 'Created',
528 202 => 'Accepted',
529 203 => 'Non-Authoritative Information',
530 204 => 'No Content',
531 205 => 'Reset Content',
532 206 => 'Partial Content',
533 207 => 'Multi-Status',
534 300 => 'Multiple Choices',
535 301 => 'Moved Permanently',
536 302 => 'Found',
537 303 => 'See Other',
538 304 => 'Not Modified',
539 305 => 'Use Proxy',
540 307 => 'Temporary Redirect',
541 400 => 'Bad Request',
542 401 => 'Unauthorized',
543 402 => 'Payment Required',
544 403 => 'Forbidden',
545 404 => 'Not Found',
546 405 => 'Method Not Allowed',
547 406 => 'Not Acceptable',
548 407 => 'Proxy Authentication Required',
549 408 => 'Request Timeout',
550 409 => 'Conflict',
551 410 => 'Gone',
552 411 => 'Length Required',
553 412 => 'Precondition Failed',
554 413 => 'Request Entity Too Large',
555 414 => 'Request-URI Too Large',
556 415 => 'Unsupported Media Type',
557 416 => 'Request Range Not Satisfiable',
558 417 => 'Expectation Failed',
559 422 => 'Unprocessable Entity',
560 423 => 'Locked',
561 424 => 'Failed Dependency',
562 500 => 'Internal Server Error',
563 501 => 'Not Implemented',
564 502 => 'Bad Gateway',
565 503 => 'Service Unavailable',
566 504 => 'Gateway Timeout',
567 505 => 'HTTP Version Not Supported',
568 507 => 'Insufficient Storage'
571 if ( $statusMessage[$this->mStatusCode] )
572 header( 'HTTP/1.1 ' . $this->mStatusCode . ' ' . $statusMessage[$this->mStatusCode] );
575 # Buffer output; final headers may depend on later processing
576 ob_start();
578 # Disable temporary placeholders, so that the skin produces HTML
579 $sk->postParseLinkColour( false );
581 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
582 header( 'Content-language: '.$wgContLanguageCode );
584 if ($this->mArticleBodyOnly) {
585 $this->out($this->mBodytext);
586 } else {
587 wfProfileIn( 'Output-skin' );
588 $sk->outputPage( $this );
589 wfProfileOut( 'Output-skin' );
592 $this->sendCacheControl();
593 ob_end_flush();
594 wfProfileOut( $fname );
597 function out( $ins ) {
598 global $wgInputEncoding, $wgOutputEncoding, $wgContLang;
599 if ( 0 == strcmp( $wgInputEncoding, $wgOutputEncoding ) ) {
600 $outs = $ins;
601 } else {
602 $outs = $wgContLang->iconv( $wgInputEncoding, $wgOutputEncoding, $ins );
603 if ( false === $outs ) { $outs = $ins; }
605 print $outs;
608 function setEncodings() {
609 global $wgInputEncoding, $wgOutputEncoding;
610 global $wgUser, $wgContLang;
612 $wgInputEncoding = strtolower( $wgInputEncoding );
614 if( $wgUser->getOption( 'altencoding' ) ) {
615 $wgContLang->setAltEncoding();
616 return;
619 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
620 $wgOutputEncoding = strtolower( $wgOutputEncoding );
621 return;
623 $wgOutputEncoding = $wgInputEncoding;
627 * Returns a HTML comment with the elapsed time since request.
628 * This method has no side effects.
629 * Use wfReportTime() instead.
630 * @return string
631 * @deprecated
633 function reportTime() {
634 $time = wfReportTime();
635 return $time;
639 * Produce a "user is blocked" page
641 function blockedPage() {
642 global $wgUser, $wgContLang, $wgTitle;
644 $this->setPageTitle( wfMsg( 'blockedtitle' ) );
645 $this->setRobotpolicy( 'noindex,nofollow' );
646 $this->setArticleRelated( false );
648 $id = $wgUser->blockedBy();
649 $reason = $wgUser->blockedFor();
650 $ip = wfGetIP();
652 if ( is_numeric( $id ) ) {
653 $name = User::whoIs( $id );
654 } else {
655 $name = $id;
657 $link = '[[' . $wgContLang->getNsText( NS_USER ) . ":{$name}|{$name}]]";
659 $this->addWikiText( wfMsg( 'blockedtext', $link, $reason, $ip, $name ) );
661 # Don't auto-return to special pages
662 $return = $wgTitle->getNamespace() > -1 ? $wgTitle->getPrefixedText() : NULL;
663 $this->returnToMain( false, $return );
667 * Note: these arguments are keys into wfMsg(), not text!
669 function errorpage( $title, $msg ) {
670 global $wgTitle;
672 $this->mDebugtext .= 'Original title: ' .
673 $wgTitle->getPrefixedText() . "\n";
674 $this->setPageTitle( wfMsg( $title ) );
675 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
676 $this->setRobotpolicy( 'noindex,nofollow' );
677 $this->setArticleRelated( false );
678 $this->enableClientCache( false );
679 $this->mRedirect = '';
681 $this->mBodytext = '';
682 $this->addWikiText( wfMsg( $msg ) );
683 $this->returnToMain( false );
685 $this->output();
686 wfErrorExit();
690 * Display an error page indicating that a given version of MediaWiki is
691 * required to use it
693 * @param mixed $version The version of MediaWiki needed to use the page
695 function versionRequired( $version ) {
696 $this->setPageTitle( wfMsg( 'versionrequired', $version ) );
697 $this->setHTMLTitle( wfMsg( 'versionrequired', $version ) );
698 $this->setRobotpolicy( 'noindex,nofollow' );
699 $this->setArticleRelated( false );
700 $this->mBodytext = '';
702 $this->addWikiText( wfMsg( 'versionrequiredtext', $version ) );
703 $this->returnToMain();
707 * Display an error page noting that a given permission bit is required.
708 * This should generally replace the sysopRequired, developerRequired etc.
709 * @param string $permission key required
711 function permissionRequired( $permission ) {
712 global $wgUser;
714 $this->setPageTitle( wfMsg( 'badaccess' ) );
715 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
716 $this->setRobotpolicy( 'noindex,nofollow' );
717 $this->setArticleRelated( false );
718 $this->mBodytext = '';
720 $sk = $wgUser->getSkin();
721 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ) );
722 $this->addHTML( wfMsgHtml( 'badaccesstext', $ap, $permission ) );
723 $this->returnToMain();
727 * @deprecated
729 function sysopRequired() {
730 global $wgUser;
732 $this->setPageTitle( wfMsg( 'sysoptitle' ) );
733 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
734 $this->setRobotpolicy( 'noindex,nofollow' );
735 $this->setArticleRelated( false );
736 $this->mBodytext = '';
738 $sk = $wgUser->getSkin();
739 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ), '' );
740 $this->addHTML( wfMsgHtml( 'sysoptext', $ap ) );
741 $this->returnToMain();
745 * @deprecated
747 function developerRequired() {
748 global $wgUser;
750 $this->setPageTitle( wfMsg( 'developertitle' ) );
751 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
752 $this->setRobotpolicy( 'noindex,nofollow' );
753 $this->setArticleRelated( false );
754 $this->mBodytext = '';
756 $sk = $wgUser->getSkin();
757 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ), '' );
758 $this->addHTML( wfMsgHtml( 'developertext', $ap ) );
759 $this->returnToMain();
762 function loginToUse() {
763 global $wgUser, $wgTitle, $wgContLang;
765 $this->setPageTitle( wfMsg( 'loginreqtitle' ) );
766 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
767 $this->setRobotpolicy( 'noindex,nofollow' );
768 $this->setArticleFlag( false );
769 $this->mBodytext = '';
770 $loginpage = Title::makeTitle(NS_SPECIAL, 'Userlogin');
771 $sk = $wgUser->getSkin();
772 $loginlink = $sk->makeKnownLinkObj($loginpage, wfMsg('loginreqlink'),
773 'returnto=' . htmlspecialchars($wgTitle->getPrefixedDBkey()));
774 $this->addHTML( wfMsgHtml( 'loginreqpagetext', $loginlink ) );
776 # We put a comment in the .html file so a Sysop can diagnose the page the
777 # user can't see.
778 $this->addHTML( "\n<!--" .
779 $wgContLang->getNsText( $wgTitle->getNamespace() ) .
780 ':' .
781 $wgTitle->getDBkey() . '-->' );
782 $this->returnToMain(); # Flip back to the main page after 10 seconds.
785 function databaseError( $fname, $sql, $error, $errno ) {
786 global $wgUser, $wgCommandLineMode, $wgShowSQLErrors;
788 $this->setPageTitle( wfMsgNoDB( 'databaseerror' ) );
789 $this->setRobotpolicy( 'noindex,nofollow' );
790 $this->setArticleRelated( false );
791 $this->enableClientCache( false );
792 $this->mRedirect = '';
794 if( !$wgShowSQLErrors ) {
795 $sql = wfMsg( 'sqlhidden' );
798 if ( $wgCommandLineMode ) {
799 $msg = wfMsgNoDB( 'dberrortextcl', htmlspecialchars( $sql ),
800 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
801 } else {
802 $msg = wfMsgNoDB( 'dberrortext', htmlspecialchars( $sql ),
803 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
806 if ( $wgCommandLineMode || !is_object( $wgUser )) {
807 print $msg."\n";
808 wfErrorExit();
810 $this->mBodytext = $msg;
811 $this->output();
812 wfErrorExit();
815 function readOnlyPage( $source = null, $protected = false ) {
816 global $wgUser, $wgReadOnlyFile, $wgReadOnly, $wgTitle;
818 $this->setRobotpolicy( 'noindex,nofollow' );
819 $this->setArticleRelated( false );
821 if( $protected ) {
822 $skin = $wgUser->getSkin();
823 $this->setPageTitle( wfMsg( 'viewsource' ) );
824 $this->setSubtitle( wfMsg( 'viewsourcefor', $skin->makeKnownLinkObj( $wgTitle ) ) );
826 # Determine if protection is due to the page being a system message
827 # and show an appropriate explanation
828 if( $wgTitle->getNamespace() == NS_MEDIAWIKI && !$wgUser->isAllowed( 'editinterface' ) ) {
829 $this->addWikiText( wfMsg( 'protectedinterface' ) );
830 } else {
831 $this->addWikiText( wfMsg( 'protectedtext' ) );
833 } else {
834 $this->setPageTitle( wfMsg( 'readonly' ) );
835 if ( $wgReadOnly ) {
836 $reason = $wgReadOnly;
837 } else {
838 $reason = file_get_contents( $wgReadOnlyFile );
840 $this->addWikiText( wfMsg( 'readonlytext', $reason ) );
843 if( is_string( $source ) ) {
844 if( strcmp( $source, '' ) == 0 ) {
845 global $wgTitle;
846 if ( $wgTitle->getNamespace() == NS_MEDIAWIKI ) {
847 $source = wfMsgWeirdKey ( $wgTitle->getText() );
848 } else {
849 $source = wfMsg( $wgUser->isLoggedIn() ? 'noarticletext' : 'noarticletextanon' );
852 $rows = $wgUser->getOption( 'rows' );
853 $cols = $wgUser->getOption( 'cols' );
855 $text = "\n<textarea name='wpTextbox1' id='wpTextbox1' cols='$cols' rows='$rows' readonly='readonly'>" .
856 htmlspecialchars( $source ) . "\n</textarea>";
857 $this->addHTML( $text );
860 $this->returnToMain( false );
863 function fatalError( $message ) {
864 $this->setPageTitle( wfMsg( "internalerror" ) );
865 $this->setRobotpolicy( "noindex,nofollow" );
866 $this->setArticleRelated( false );
867 $this->enableClientCache( false );
868 $this->mRedirect = '';
870 $this->mBodytext = $message;
871 $this->output();
872 wfErrorExit();
875 function unexpectedValueError( $name, $val ) {
876 $this->fatalError( wfMsg( 'unexpected', $name, $val ) );
879 function fileCopyError( $old, $new ) {
880 $this->fatalError( wfMsg( 'filecopyerror', $old, $new ) );
883 function fileRenameError( $old, $new ) {
884 $this->fatalError( wfMsg( 'filerenameerror', $old, $new ) );
887 function fileDeleteError( $name ) {
888 $this->fatalError( wfMsg( 'filedeleteerror', $name ) );
891 function fileNotFoundError( $name ) {
892 $this->fatalError( wfMsg( 'filenotfound', $name ) );
896 * return from error messages or notes
897 * @param $auto automatically redirect the user after 10 seconds
898 * @param $returnto page title to return to. Default is Main Page.
900 function returnToMain( $auto = true, $returnto = NULL ) {
901 global $wgUser, $wgOut, $wgRequest;
903 if ( $returnto == NULL ) {
904 $returnto = $wgRequest->getText( 'returnto' );
906 $returnto = htmlspecialchars( $returnto );
908 $sk = $wgUser->getSkin();
909 if ( '' == $returnto ) {
910 $returnto = wfMsgForContent( 'mainpage' );
912 $link = $sk->makeLinkObj( Title::newFromText( $returnto ), '' );
914 $r = wfMsg( 'returnto', $link );
915 if ( $auto ) {
916 $titleObj = Title::newFromText( $returnto );
917 $wgOut->addMeta( 'http:Refresh', '10;url=' . $titleObj->escapeFullURL() );
919 $wgOut->addHTML( "\n<p>$r</p>\n" );
923 * This function takes the title (first item of mGoodLinks), categories, existing and broken links for the page
924 * and uses the first 10 of them for META keywords
926 function addKeywords( &$parserOutput ) {
927 global $wgTitle;
928 $this->addKeyword( $wgTitle->getPrefixedText() );
929 $count = 1;
930 $links2d =& $parserOutput->getLinks();
931 if ( !is_array( $links2d ) ) {
932 return;
934 foreach ( $links2d as $ns => $dbkeys ) {
935 foreach( $dbkeys as $dbkey => $id ) {
936 $this->addKeyword( $dbkey );
937 if ( ++$count > 10 ) {
938 break 2;
945 * @access private
946 * @return string
948 function headElement() {
949 global $wgDocType, $wgDTD, $wgContLanguageCode, $wgOutputEncoding, $wgMimeType;
950 global $wgUser, $wgContLang, $wgUseTrackbacks, $wgTitle;
952 if( $wgMimeType == 'text/xml' || $wgMimeType == 'application/xhtml+xml' || $wgMimeType == 'application/xml' ) {
953 $ret = "<?xml version=\"1.0\" encoding=\"$wgOutputEncoding\" ?>\n";
954 } else {
955 $ret = '';
958 $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
960 if ( '' == $this->getHTMLTitle() ) {
961 $this->setHTMLTitle( wfMsg( 'pagetitle', $this->getPageTitle() ));
964 $rtl = $wgContLang->isRTL() ? " dir='RTL'" : '';
965 $ret .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"$wgContLanguageCode\" lang=\"$wgContLanguageCode\" $rtl>\n";
966 $ret .= "<head>\n<title>" . htmlspecialchars( $this->getHTMLTitle() ) . "</title>\n";
967 array_push( $this->mMetatags, array( "http:Content-type", "$wgMimeType; charset={$wgOutputEncoding}" ) );
969 $ret .= $this->getHeadLinks();
970 global $wgStylePath;
971 if( $this->isPrintable() ) {
972 $media = '';
973 } else {
974 $media = "media='print'";
976 $printsheet = htmlspecialchars( "$wgStylePath/common/wikiprintable.css" );
977 $ret .= "<link rel='stylesheet' type='text/css' $media href='$printsheet' />\n";
979 $sk = $wgUser->getSkin();
980 $ret .= $sk->getHeadScripts();
981 $ret .= $this->mScripts;
982 $ret .= $sk->getUserStyles();
984 if ($wgUseTrackbacks && $this->isArticleRelated())
985 $ret .= $wgTitle->trackbackRDF();
987 $ret .= "</head>\n";
988 return $ret;
991 function getHeadLinks() {
992 global $wgRequest;
993 $ret = '';
994 foreach ( $this->mMetatags as $tag ) {
995 if ( 0 == strcasecmp( 'http:', substr( $tag[0], 0, 5 ) ) ) {
996 $a = 'http-equiv';
997 $tag[0] = substr( $tag[0], 5 );
998 } else {
999 $a = 'name';
1001 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\" />\n";
1004 $p = $this->mRobotpolicy;
1005 if( $p !== '' && $p != 'index,follow' ) {
1006 // http://www.robotstxt.org/wc/meta-user.html
1007 // Only show if it's different from the default robots policy
1008 $ret .= "<meta name=\"robots\" content=\"$p\" />\n";
1011 if ( count( $this->mKeywords ) > 0 ) {
1012 $strip = array(
1013 "/<.*?>/" => '',
1014 "/_/" => ' '
1016 $ret .= "<meta name=\"keywords\" content=\"" .
1017 htmlspecialchars(preg_replace(array_keys($strip), array_values($strip),implode( ",", $this->mKeywords ))) . "\" />\n";
1019 foreach ( $this->mLinktags as $tag ) {
1020 $ret .= '<link';
1021 foreach( $tag as $attr => $val ) {
1022 $ret .= " $attr=\"" . htmlspecialchars( $val ) . "\"";
1024 $ret .= " />\n";
1026 if( $this->isSyndicated() ) {
1027 # FIXME: centralize the mime-type and name information in Feed.php
1028 $link = $wgRequest->escapeAppendQuery( 'feed=rss' );
1029 $ret .= "<link rel='alternate' type='application/rss+xml' title='RSS 2.0' href='$link' />\n";
1030 $link = $wgRequest->escapeAppendQuery( 'feed=atom' );
1031 $ret .= "<link rel='alternate' type='application/atom+xml' title='Atom 0.3' href='$link' />\n";
1034 return $ret;
1038 * Turn off regular page output and return an error reponse
1039 * for when rate limiting has triggered.
1040 * @todo i18n
1041 * @access public
1043 function rateLimited() {
1044 global $wgOut;
1045 $wgOut->disable();
1046 wfHttpError( 500, 'Internal Server Error',
1047 'Sorry, the server has encountered an internal error. ' .
1048 'Please wait a moment and hit "refresh" to submit the request again.' );
1052 * Show an "add new section" link?
1054 * @return bool True if the parser output instructs us to add one
1056 function showNewSectionLink() {
1057 return $this->mNewSectionLink;