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