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