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