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