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