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