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