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