* (bug 4104) 'OutputPageBeforeHTML' hook to postprocess article HTML on
[lhc/web/wiklou.git] / includes / OutputPage.php
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die();
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, $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 ) );
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 ) ) {
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\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\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 $parserOutput = $wgParser->parse( $text, $article->mTitle,
310 $this->mParserOptions, true, true, $this->mRevisionId );
311
312 if ( $cache && $article && $parserOutput->getCacheTime() != -1 ) {
313 $parserCache =& ParserCache::singleton();
314 $parserCache->save( $parserOutput, $article, $wgUser );
315 }
316
317 $this->addParserOutputNoText( $parserOutput );
318 $text = $parserOutput->getText();
319 wfRunHooks( 'OutputPageBeforeHTML',array( &$this, &$text ) );
320 $parserOutput->setText( $text );
321 $this->addHTML( $parserOutput->getText() );
322 }
323
324 /**
325 * Add the output of a QuickTemplate to the output buffer
326 * @param QuickTemplate $template
327 */
328 function addTemplate( &$template ) {
329 ob_start();
330 $template->execute();
331 $this->addHTML( ob_get_contents() );
332 ob_end_clean();
333 }
334
335 /**
336 * Parse wikitext and return the HTML. This is for special pages that add the text later
337 */
338 function parse( $text, $linestart = true ) {
339 global $wgParser, $wgTitle;
340 $parserOutput = $wgParser->parse( $text, $wgTitle, $this->mParserOptions,
341 $linestart, true, $this->mRevisionId );
342 return $parserOutput->getText();
343 }
344
345 /**
346 * @param $article
347 * @param $user
348 *
349 * @return bool
350 */
351 function tryParserCache( $article, $user ) {
352 $parserCache =& ParserCache::singleton();
353 $parserOutput = $parserCache->get( $article, $user );
354 if ( $parserOutput !== false ) {
355 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
356 $this->addCategoryLinks( $parserOutput->getCategories() );
357 $this->addKeywords( $parserOutput );
358 $text = $parserOutput->getText();
359 wfRunHooks( 'OutputPageBeforeHTML', array( &$this, &$text ) );
360 $this->addHTML( $text );
361 $t = $parserOutput->getTitleText();
362 if( !empty( $t ) ) {
363 $this->setPageTitle( $t );
364 }
365 return true;
366 } else {
367 return false;
368 }
369 }
370
371 /**
372 * Set the maximum cache time on the Squid in seconds
373 * @param $maxage
374 */
375 function setSquidMaxage( $maxage ) {
376 $this->mSquidMaxage = $maxage;
377 }
378
379 /**
380 * Use enableClientCache(false) to force it to send nocache headers
381 * @param $state
382 */
383 function enableClientCache( $state ) {
384 return wfSetVar( $this->mEnableClientCache, $state );
385 }
386
387 function uncacheableBecauseRequestvars() {
388 global $wgRequest;
389 return $wgRequest->getText('useskin', false) === false
390 && $wgRequest->getText('uselang', false) === false;
391 }
392
393 function sendCacheControl() {
394 global $wgUseSquid, $wgUseESI, $wgSquidMaxage;
395
396 if ($this->mETag)
397 header("ETag: $this->mETag");
398
399 # don't serve compressed data to clients who can't handle it
400 # maintain different caches for logged-in users and non-logged in ones
401 header( 'Vary: Accept-Encoding, Cookie' );
402 if( !$this->uncacheableBecauseRequestvars() && $this->mEnableClientCache ) {
403 if( $wgUseSquid && ! isset( $_COOKIE[ini_get( 'session.name') ] ) &&
404 ! $this->isPrintable() && $this->mSquidMaxage != 0 )
405 {
406 if ( $wgUseESI ) {
407 # We'll purge the proxy cache explicitly, but require end user agents
408 # to revalidate against the proxy on each visit.
409 # Surrogate-Control controls our Squid, Cache-Control downstream caches
410 wfDebug( "** proxy caching with ESI; {$this->mLastModified} **\n", false );
411 # start with a shorter timeout for initial testing
412 # header( 'Surrogate-Control: max-age=2678400+2678400, content="ESI/1.0"');
413 header( 'Surrogate-Control: max-age='.$wgSquidMaxage.'+'.$this->mSquidMaxage.', content="ESI/1.0"');
414 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
415 } else {
416 # We'll purge the proxy cache for anons explicitly, but require end user agents
417 # to revalidate against the proxy on each visit.
418 # IMPORTANT! The Squid needs to replace the Cache-Control header with
419 # Cache-Control: s-maxage=0, must-revalidate, max-age=0
420 wfDebug( "** local proxy caching; {$this->mLastModified} **\n", false );
421 # start with a shorter timeout for initial testing
422 # header( "Cache-Control: s-maxage=2678400, must-revalidate, max-age=0" );
423 header( 'Cache-Control: s-maxage='.$this->mSquidMaxage.', must-revalidate, max-age=0' );
424 }
425 } else {
426 # We do want clients to cache if they can, but they *must* check for updates
427 # on revisiting the page.
428 wfDebug( "** private caching; {$this->mLastModified} **\n", false );
429 header( "Expires: -1" );
430 header( "Cache-Control: private, must-revalidate, max-age=0" );
431 }
432 if($this->mLastModified) header( "Last-modified: {$this->mLastModified}" );
433 } else {
434 wfDebug( "** no caching **\n", false );
435
436 # In general, the absence of a last modified header should be enough to prevent
437 # the client from using its cache. We send a few other things just to make sure.
438 header( 'Expires: -1' );
439 header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
440 header( 'Pragma: no-cache' );
441 }
442 }
443
444 /**
445 * Finally, all the text has been munged and accumulated into
446 * the object, let's actually output it:
447 */
448 function output() {
449 global $wgUser, $wgOutputEncoding;
450 global $wgContLanguageCode, $wgDebugRedirects, $wgMimeType, $wgProfiler;
451
452 if( $this->mDoNothing ){
453 return;
454 }
455 $fname = 'OutputPage::output';
456 wfProfileIn( $fname );
457 $sk = $wgUser->getSkin();
458
459 if ( '' != $this->mRedirect ) {
460 if( substr( $this->mRedirect, 0, 4 ) != 'http' ) {
461 # Standards require redirect URLs to be absolute
462 global $wgServer;
463 $this->mRedirect = $wgServer . $this->mRedirect;
464 }
465 if( $this->mRedirectCode == '301') {
466 if( !$wgDebugRedirects ) {
467 header("HTTP/1.1 {$this->mRedirectCode} Moved Permanently");
468 }
469 $this->mLastModified = wfTimestamp( TS_RFC2822 );
470 }
471
472 $this->sendCacheControl();
473
474 if( $wgDebugRedirects ) {
475 $url = htmlspecialchars( $this->mRedirect );
476 print "<html>\n<head>\n<title>Redirect</title>\n</head>\n<body>\n";
477 print "<p>Location: <a href=\"$url\">$url</a></p>\n";
478 print "</body>\n</html>\n";
479 } else {
480 header( 'Location: '.$this->mRedirect );
481 }
482 if ( isset( $wgProfiler ) ) { wfDebug( $wgProfiler->getOutput() ); }
483 wfProfileOut( $fname );
484 return;
485 }
486 elseif ( $this->mStatusCode )
487 {
488 $statusMessage = array(
489 100 => 'Continue',
490 101 => 'Switching Protocols',
491 102 => 'Processing',
492 200 => 'OK',
493 201 => 'Created',
494 202 => 'Accepted',
495 203 => 'Non-Authoritative Information',
496 204 => 'No Content',
497 205 => 'Reset Content',
498 206 => 'Partial Content',
499 207 => 'Multi-Status',
500 300 => 'Multiple Choices',
501 301 => 'Moved Permanently',
502 302 => 'Found',
503 303 => 'See Other',
504 304 => 'Not Modified',
505 305 => 'Use Proxy',
506 307 => 'Temporary Redirect',
507 400 => 'Bad Request',
508 401 => 'Unauthorized',
509 402 => 'Payment Required',
510 403 => 'Forbidden',
511 404 => 'Not Found',
512 405 => 'Method Not Allowed',
513 406 => 'Not Acceptable',
514 407 => 'Proxy Authentication Required',
515 408 => 'Request Timeout',
516 409 => 'Conflict',
517 410 => 'Gone',
518 411 => 'Length Required',
519 412 => 'Precondition Failed',
520 413 => 'Request Entity Too Large',
521 414 => 'Request-URI Too Large',
522 415 => 'Unsupported Media Type',
523 416 => 'Request Range Not Satisfiable',
524 417 => 'Expectation Failed',
525 422 => 'Unprocessable Entity',
526 423 => 'Locked',
527 424 => 'Failed Dependency',
528 500 => 'Internal Server Error',
529 501 => 'Not Implemented',
530 502 => 'Bad Gateway',
531 503 => 'Service Unavailable',
532 504 => 'Gateway Timeout',
533 505 => 'HTTP Version Not Supported',
534 507 => 'Insufficient Storage'
535 );
536
537 if ( $statusMessage[$this->mStatusCode] )
538 header( 'HTTP/1.1 ' . $this->mStatusCode . ' ' . $statusMessage[$this->mStatusCode] );
539 }
540
541 # Buffer output; final headers may depend on later processing
542 ob_start();
543
544 # Disable temporary placeholders, so that the skin produces HTML
545 $sk->postParseLinkColour( false );
546
547 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
548 header( 'Content-language: '.$wgContLanguageCode );
549
550 if ($this->mArticleBodyOnly) {
551 $this->out($this->mBodytext);
552 } else {
553 wfProfileIn( 'Output-skin' );
554 $sk->outputPage( $this );
555 wfProfileOut( 'Output-skin' );
556 }
557
558 $this->sendCacheControl();
559 ob_end_flush();
560 wfProfileOut( $fname );
561 }
562
563 function out( $ins ) {
564 global $wgInputEncoding, $wgOutputEncoding, $wgContLang;
565 if ( 0 == strcmp( $wgInputEncoding, $wgOutputEncoding ) ) {
566 $outs = $ins;
567 } else {
568 $outs = $wgContLang->iconv( $wgInputEncoding, $wgOutputEncoding, $ins );
569 if ( false === $outs ) { $outs = $ins; }
570 }
571 print $outs;
572 }
573
574 function setEncodings() {
575 global $wgInputEncoding, $wgOutputEncoding;
576 global $wgUser, $wgContLang;
577
578 $wgInputEncoding = strtolower( $wgInputEncoding );
579
580 if( $wgUser->getOption( 'altencoding' ) ) {
581 $wgContLang->setAltEncoding();
582 return;
583 }
584
585 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
586 $wgOutputEncoding = strtolower( $wgOutputEncoding );
587 return;
588 }
589 $wgOutputEncoding = $wgInputEncoding;
590 }
591
592 /**
593 * Returns a HTML comment with the elapsed time since request.
594 * This method has no side effects.
595 * Use wfReportTime() instead.
596 * @return string
597 * @deprecated
598 */
599 function reportTime() {
600 $time = wfReportTime();
601 return $time;
602 }
603
604 /**
605 * Produce a "user is blocked" page
606 */
607 function blockedPage() {
608 global $wgUser, $wgContLang;
609
610 $this->setPageTitle( wfMsg( 'blockedtitle' ) );
611 $this->setRobotpolicy( 'noindex,nofollow' );
612 $this->setArticleRelated( false );
613
614 $id = $wgUser->blockedBy();
615 $reason = $wgUser->blockedFor();
616 $ip = wfGetIP();
617
618 if ( is_numeric( $id ) ) {
619 $name = User::whoIs( $id );
620 } else {
621 $name = $id;
622 }
623 $link = '[[' . $wgContLang->getNsText( NS_USER ) . ":{$name}|{$name}]]";
624
625 $this->addWikiText( wfMsg( 'blockedtext', $link, $reason, $ip, $name ) );
626 $this->returnToMain( false );
627 }
628
629 /**
630 * Note: these arguments are keys into wfMsg(), not text!
631 */
632 function errorpage( $title, $msg ) {
633 global $wgTitle;
634
635 $this->mDebugtext .= 'Original title: ' .
636 $wgTitle->getPrefixedText() . "\n";
637 $this->setPageTitle( wfMsg( $title ) );
638 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
639 $this->setRobotpolicy( 'noindex,nofollow' );
640 $this->setArticleRelated( false );
641 $this->enableClientCache( false );
642 $this->mRedirect = '';
643
644 $this->mBodytext = '';
645 $this->addWikiText( wfMsg( $msg ) );
646 $this->returnToMain( false );
647
648 $this->output();
649 wfErrorExit();
650 }
651
652 /**
653 * Display an error page indicating that a given version of MediaWiki is
654 * required to use it
655 *
656 * @param mixed $version The version of MediaWiki needed to use the page
657 */
658 function versionRequired( $version ) {
659 global $wgUser;
660
661 $this->setPageTitle( wfMsg( 'versionrequired', $version ) );
662 $this->setHTMLTitle( wfMsg( 'versionrequired', $version ) );
663 $this->setRobotpolicy( 'noindex,nofollow' );
664 $this->setArticleRelated( false );
665 $this->mBodytext = '';
666
667 $this->addWikiText( wfMsg( 'versionrequiredtext', $version ) );
668 $this->returnToMain();
669 }
670
671 /**
672 * Display an error page noting that a given permission bit is required.
673 * This should generally replace the sysopRequired, developerRequired etc.
674 * @param string $permission key required
675 */
676 function permissionRequired( $permission ) {
677 global $wgUser;
678
679 $this->setPageTitle( wfMsg( 'badaccess' ) );
680 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
681 $this->setRobotpolicy( 'noindex,nofollow' );
682 $this->setArticleRelated( false );
683 $this->mBodytext = '';
684
685 $sk = $wgUser->getSkin();
686 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ) );
687 $this->addHTML( wfMsgHtml( 'badaccesstext', $ap, $permission ) );
688 $this->returnToMain();
689 }
690
691 /**
692 * @deprecated
693 */
694 function sysopRequired() {
695 global $wgUser;
696
697 $this->setPageTitle( wfMsg( 'sysoptitle' ) );
698 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
699 $this->setRobotpolicy( 'noindex,nofollow' );
700 $this->setArticleRelated( false );
701 $this->mBodytext = '';
702
703 $sk = $wgUser->getSkin();
704 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ), '' );
705 $this->addHTML( wfMsgHtml( 'sysoptext', $ap ) );
706 $this->returnToMain();
707 }
708
709 /**
710 * @deprecated
711 */
712 function developerRequired() {
713 global $wgUser;
714
715 $this->setPageTitle( wfMsg( 'developertitle' ) );
716 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
717 $this->setRobotpolicy( 'noindex,nofollow' );
718 $this->setArticleRelated( false );
719 $this->mBodytext = '';
720
721 $sk = $wgUser->getSkin();
722 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ), '' );
723 $this->addHTML( wfMsgHtml( 'developertext', $ap ) );
724 $this->returnToMain();
725 }
726
727 function loginToUse() {
728 global $wgUser, $wgTitle, $wgContLang;
729
730 $this->setPageTitle( wfMsg( 'loginreqtitle' ) );
731 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
732 $this->setRobotpolicy( 'noindex,nofollow' );
733 $this->setArticleFlag( false );
734 $this->mBodytext = '';
735 $loginpage = Title::makeTitle(NS_SPECIAL, 'Userlogin');
736 $sk = $wgUser->getSkin();
737 $loginlink = $sk->makeKnownLinkObj($loginpage, wfMsg('loginreqlink'),
738 'returnto=' . htmlspecialchars($wgTitle->getPrefixedDBkey()));
739 $this->addHTML( wfMsgHtml( 'loginreqpagetext', $loginlink ) );
740
741 # We put a comment in the .html file so a Sysop can diagnose the page the
742 # user can't see.
743 $this->addHTML( "\n<!--" .
744 $wgContLang->getNsText( $wgTitle->getNamespace() ) .
745 ':' .
746 $wgTitle->getDBkey() . '-->' );
747 $this->returnToMain(); # Flip back to the main page after 10 seconds.
748 }
749
750 function databaseError( $fname, $sql, $error, $errno ) {
751 global $wgUser, $wgCommandLineMode, $wgShowSQLErrors;
752
753 $this->setPageTitle( wfMsgNoDB( 'databaseerror' ) );
754 $this->setRobotpolicy( 'noindex,nofollow' );
755 $this->setArticleRelated( false );
756 $this->enableClientCache( false );
757 $this->mRedirect = '';
758
759 if( !$wgShowSQLErrors ) {
760 $sql = wfMsg( 'sqlhidden' );
761 }
762
763 if ( $wgCommandLineMode ) {
764 $msg = wfMsgNoDB( 'dberrortextcl', htmlspecialchars( $sql ),
765 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
766 } else {
767 $msg = wfMsgNoDB( 'dberrortext', htmlspecialchars( $sql ),
768 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
769 }
770
771 if ( $wgCommandLineMode || !is_object( $wgUser )) {
772 print $msg."\n";
773 wfErrorExit();
774 }
775 $this->mBodytext = $msg;
776 $this->output();
777 wfErrorExit();
778 }
779
780 function readOnlyPage( $source = null, $protected = false ) {
781 global $wgUser, $wgReadOnlyFile, $wgReadOnly;
782
783 $this->setRobotpolicy( 'noindex,nofollow' );
784 $this->setArticleRelated( false );
785
786 if( $protected ) {
787 $this->setPageTitle( wfMsg( 'viewsource' ) );
788 $this->addWikiText( wfMsg( 'protectedtext' ) );
789 } else {
790 $this->setPageTitle( wfMsg( 'readonly' ) );
791 if ( $wgReadOnly ) {
792 $reason = $wgReadOnly;
793 } else {
794 $reason = file_get_contents( $wgReadOnlyFile );
795 }
796 $this->addWikiText( wfMsg( 'readonlytext', $reason ) );
797 }
798
799 if( is_string( $source ) ) {
800 if( strcmp( $source, '' ) == 0 ) {
801 global $wgTitle ;
802 if ( $wgTitle->getNamespace() == NS_MEDIAWIKI ) {
803 $source = wfMsgWeirdKey ( $wgTitle->getText() ) ;
804 } else {
805 $source = wfMsg( $wgUser->isLoggedIn() ? 'noarticletext' : 'noarticletextanon' );
806 }
807 }
808 $rows = $wgUser->getOption( 'rows' );
809 $cols = $wgUser->getOption( 'cols' );
810 $text = "\n<textarea cols='$cols' rows='$rows' readonly='readonly'>" .
811 htmlspecialchars( $source ) . "\n</textarea>";
812 $this->addHTML( $text );
813 }
814
815 $this->returnToMain( false );
816 }
817
818 function fatalError( $message ) {
819 $this->setPageTitle( wfMsg( "internalerror" ) );
820 $this->setRobotpolicy( "noindex,nofollow" );
821 $this->setArticleRelated( false );
822 $this->enableClientCache( false );
823 $this->mRedirect = '';
824
825 $this->mBodytext = $message;
826 $this->output();
827 wfErrorExit();
828 }
829
830 function unexpectedValueError( $name, $val ) {
831 $this->fatalError( wfMsg( 'unexpected', $name, $val ) );
832 }
833
834 function fileCopyError( $old, $new ) {
835 $this->fatalError( wfMsg( 'filecopyerror', $old, $new ) );
836 }
837
838 function fileRenameError( $old, $new ) {
839 $this->fatalError( wfMsg( 'filerenameerror', $old, $new ) );
840 }
841
842 function fileDeleteError( $name ) {
843 $this->fatalError( wfMsg( 'filedeleteerror', $name ) );
844 }
845
846 function fileNotFoundError( $name ) {
847 $this->fatalError( wfMsg( 'filenotfound', $name ) );
848 }
849
850 /**
851 * return from error messages or notes
852 * @param $auto automatically redirect the user after 10 seconds
853 * @param $returnto page title to return to. Default is Main Page.
854 */
855 function returnToMain( $auto = true, $returnto = NULL ) {
856 global $wgUser, $wgOut, $wgRequest;
857
858 if ( $returnto == NULL ) {
859 $returnto = $wgRequest->getText( 'returnto' );
860 }
861 $returnto = htmlspecialchars( $returnto );
862
863 $sk = $wgUser->getSkin();
864 if ( '' == $returnto ) {
865 $returnto = wfMsgForContent( 'mainpage' );
866 }
867 $link = $sk->makeKnownLink( $returnto, '' );
868
869 $r = wfMsg( 'returnto', $link );
870 if ( $auto ) {
871 $titleObj = Title::newFromText( $returnto );
872 $wgOut->addMeta( 'http:Refresh', '10;url=' . $titleObj->escapeFullURL() );
873 }
874 $wgOut->addHTML( "\n<p>$r</p>\n" );
875 }
876
877 /**
878 * This function takes the title (first item of mGoodLinks), categories, existing and broken links for the page
879 * and uses the first 10 of them for META keywords
880 */
881 function addKeywords( &$parserOutput ) {
882 global $wgTitle;
883 $this->addKeyword( $wgTitle->getPrefixedText() );
884 $count = 1;
885 $links2d =& $parserOutput->getLinks();
886 foreach ( $links2d as $ns => $dbkeys ) {
887 foreach( $dbkeys as $dbkey => $id ) {
888 $this->addKeyword( $dbkey );
889 if ( ++$count > 10 ) {
890 break 2;
891 }
892 }
893 }
894 }
895
896 /**
897 * @access private
898 * @return string
899 */
900 function headElement() {
901 global $wgDocType, $wgDTD, $wgContLanguageCode, $wgOutputEncoding, $wgMimeType;
902 global $wgUser, $wgContLang, $wgRequest, $wgUseTrackbacks, $wgTitle;
903
904 if( $wgMimeType == 'text/xml' || $wgMimeType == 'application/xhtml+xml' || $wgMimeType == 'application/xml' ) {
905 $ret = "<?xml version=\"1.0\" encoding=\"$wgOutputEncoding\" ?>\n";
906 } else {
907 $ret = '';
908 }
909
910 $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
911
912 if ( '' == $this->getHTMLTitle() ) {
913 $this->setHTMLTitle( wfMsg( 'pagetitle', $this->getPageTitle() ));
914 }
915
916 $rtl = $wgContLang->isRTL() ? " dir='RTL'" : '';
917 $ret .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"$wgContLanguageCode\" lang=\"$wgContLanguageCode\" $rtl>\n";
918 $ret .= "<head>\n<title>" . htmlspecialchars( $this->getHTMLTitle() ) . "</title>\n";
919 array_push( $this->mMetatags, array( "http:Content-type", "$wgMimeType; charset={$wgOutputEncoding}" ) );
920
921 $ret .= $this->getHeadLinks();
922 global $wgStylePath;
923 if( $this->isPrintable() ) {
924 $media = '';
925 } else {
926 $media = "media='print'";
927 }
928 $printsheet = htmlspecialchars( "$wgStylePath/common/wikiprintable.css" );
929 $ret .= "<link rel='stylesheet' type='text/css' $media href='$printsheet' />\n";
930
931 $sk = $wgUser->getSkin();
932 $ret .= $sk->getHeadScripts();
933 $ret .= $this->mScripts;
934 $ret .= $sk->getUserStyles();
935
936 if ($wgUseTrackbacks && $this->isArticleRelated())
937 $ret .= $wgTitle->trackbackRDF();
938
939 $ret .= "</head>\n";
940 return $ret;
941 }
942
943 function getHeadLinks() {
944 global $wgRequest, $wgStylePath;
945 $ret = '';
946 foreach ( $this->mMetatags as $tag ) {
947 if ( 0 == strcasecmp( 'http:', substr( $tag[0], 0, 5 ) ) ) {
948 $a = 'http-equiv';
949 $tag[0] = substr( $tag[0], 5 );
950 } else {
951 $a = 'name';
952 }
953 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\" />\n";
954 }
955
956 $p = $this->mRobotpolicy;
957 if( $p !== '' && $p != 'index,follow' ) {
958 // http://www.robotstxt.org/wc/meta-user.html
959 // Only show if it's different from the default robots policy
960 $ret .= "<meta name=\"robots\" content=\"$p\" />\n";
961 }
962
963 if ( count( $this->mKeywords ) > 0 ) {
964 $strip = array(
965 "/<.*?>/" => '',
966 "/_/" => ' '
967 );
968 $ret .= "<meta name=\"keywords\" content=\"" .
969 htmlspecialchars(preg_replace(array_keys($strip), array_values($strip),implode( ",", $this->mKeywords ))) . "\" />\n";
970 }
971 foreach ( $this->mLinktags as $tag ) {
972 $ret .= '<link';
973 foreach( $tag as $attr => $val ) {
974 $ret .= " $attr=\"" . htmlspecialchars( $val ) . "\"";
975 }
976 $ret .= " />\n";
977 }
978 if( $this->isSyndicated() ) {
979 # FIXME: centralize the mime-type and name information in Feed.php
980 $link = $wgRequest->escapeAppendQuery( 'feed=rss' );
981 $ret .= "<link rel='alternate' type='application/rss+xml' title='RSS 2.0' href='$link' />\n";
982 $link = $wgRequest->escapeAppendQuery( 'feed=atom' );
983 $ret .= "<link rel='alternate' type='application/atom+xml' title='Atom 0.3' href='$link' />\n";
984 }
985
986 return $ret;
987 }
988
989 /**
990 * Turn off regular page output and return an error reponse
991 * for when rate limiting has triggered.
992 * @todo i18n
993 * @access public
994 */
995 function rateLimited() {
996 global $wgOut;
997 $wgOut->disable();
998 wfHttpError( 500, 'Internal Server Error',
999 'Sorry, the server has encountered an internal error. ' .
1000 'Please wait a moment and hit "refresh" to submit the request again.' );
1001 }
1002
1003 }
1004 ?>