addHeadItem() method
[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;
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 $this->addWikiText( wfMsg( 'blockedtext', $link, $reason, $ip, $name, $blockid ) );
753
754 # Don't auto-return to special pages
755 if( $return ) {
756 $return = $wgTitle->getNamespace() > -1 ? $wgTitle->getPrefixedText() : NULL;
757 $this->returnToMain( false, $return );
758 }
759 }
760
761 /**
762 * Outputs a pretty page to explain why the request exploded.
763 *
764 * @param string $title Message key for page title.
765 * @param string $msg Message key for page text.
766 * @return nothing
767 */
768 public function showErrorPage( $title, $msg ) {
769 global $wgTitle;
770
771 $this->mDebugtext .= 'Original title: ' .
772 $wgTitle->getPrefixedText() . "\n";
773 $this->setPageTitle( wfMsg( $title ) );
774 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
775 $this->setRobotpolicy( 'noindex,nofollow' );
776 $this->setArticleRelated( false );
777 $this->enableClientCache( false );
778 $this->mRedirect = '';
779
780 $this->mBodytext = '';
781 $this->addWikiText( wfMsg( $msg ) );
782 $this->returnToMain( false );
783 }
784
785 /** @deprecated */
786 public function errorpage( $title, $msg ) {
787 throw new ErrorPageError( $title, $msg );
788 }
789
790 /**
791 * Display an error page indicating that a given version of MediaWiki is
792 * required to use it
793 *
794 * @param mixed $version The version of MediaWiki needed to use the page
795 */
796 public function versionRequired( $version ) {
797 $this->setPageTitle( wfMsg( 'versionrequired', $version ) );
798 $this->setHTMLTitle( wfMsg( 'versionrequired', $version ) );
799 $this->setRobotpolicy( 'noindex,nofollow' );
800 $this->setArticleRelated( false );
801 $this->mBodytext = '';
802
803 $this->addWikiText( wfMsg( 'versionrequiredtext', $version ) );
804 $this->returnToMain();
805 }
806
807 /**
808 * Display an error page noting that a given permission bit is required.
809 *
810 * @param string $permission key required
811 */
812 public function permissionRequired( $permission ) {
813 global $wgGroupPermissions, $wgUser;
814
815 $this->setPageTitle( wfMsg( 'badaccess' ) );
816 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
817 $this->setRobotpolicy( 'noindex,nofollow' );
818 $this->setArticleRelated( false );
819 $this->mBodytext = '';
820
821 $groups = array();
822 foreach( $wgGroupPermissions as $key => $value ) {
823 if( isset( $value[$permission] ) && $value[$permission] == true ) {
824 $groupName = User::getGroupName( $key );
825 $groupPage = User::getGroupPage( $key );
826 if( $groupPage ) {
827 $skin = $wgUser->getSkin();
828 $groups[] = $skin->makeLinkObj( $groupPage, $groupName );
829 } else {
830 $groups[] = $groupName;
831 }
832 }
833 }
834 $n = count( $groups );
835 $groups = implode( ', ', $groups );
836 switch( $n ) {
837 case 0:
838 case 1:
839 case 2:
840 $message = wfMsgHtml( "badaccess-group$n", $groups );
841 break;
842 default:
843 $message = wfMsgHtml( 'badaccess-groups', $groups );
844 }
845 $this->addHtml( $message );
846 $this->returnToMain( false );
847 }
848
849 /**
850 * Use permissionRequired.
851 * @deprecated
852 */
853 public function sysopRequired() {
854 throw new MWException( "Call to deprecated OutputPage::sysopRequired() method\n" );
855 }
856
857 /**
858 * Use permissionRequired.
859 * @deprecated
860 */
861 public function developerRequired() {
862 throw new MWException( "Call to deprecated OutputPage::developerRequired() method\n" );
863 }
864
865 /**
866 * Produce the stock "please login to use the wiki" page
867 */
868 public function loginToUse() {
869 global $wgUser, $wgTitle, $wgContLang;
870
871 if( $wgUser->isLoggedIn() ) {
872 $this->permissionRequired( 'read' );
873 return;
874 }
875
876 $skin = $wgUser->getSkin();
877
878 $this->setPageTitle( wfMsg( 'loginreqtitle' ) );
879 $this->setHtmlTitle( wfMsg( 'errorpagetitle' ) );
880 $this->setRobotPolicy( 'noindex,nofollow' );
881 $this->setArticleFlag( false );
882
883 $loginTitle = SpecialPage::getTitleFor( 'Userlogin' );
884 $loginLink = $skin->makeKnownLinkObj( $loginTitle, wfMsgHtml( 'loginreqlink' ), 'returnto=' . $wgTitle->getPrefixedUrl() );
885 $this->addHtml( wfMsgWikiHtml( 'loginreqpagetext', $loginLink ) );
886 $this->addHtml( "\n<!--" . $wgTitle->getPrefixedUrl() . "-->" );
887
888 # Don't return to the main page if the user can't read it
889 # otherwise we'll end up in a pointless loop
890 $mainPage = Title::newMainPage();
891 if( $mainPage->userCanRead() )
892 $this->returnToMain( true, $mainPage );
893 }
894
895 /** @deprecated */
896 public function databaseError( $fname, $sql, $error, $errno ) {
897 throw new MWException( "OutputPage::databaseError is obsolete\n" );
898 }
899
900 /**
901 * @todo document
902 * @param bool $protected Is the reason the page can't be reached because it's protected?
903 * @param mixed $source
904 */
905 public function readOnlyPage( $source = null, $protected = false ) {
906 global $wgUser, $wgReadOnlyFile, $wgReadOnly, $wgTitle;
907 $skin = $wgUser->getSkin();
908
909 $this->setRobotpolicy( 'noindex,nofollow' );
910 $this->setArticleRelated( false );
911
912 if( $protected ) {
913 $this->setPageTitle( wfMsg( 'viewsource' ) );
914 $this->setSubtitle( wfMsg( 'viewsourcefor', $skin->makeKnownLinkObj( $wgTitle ) ) );
915
916 list( $cascadeSources, $restrictions ) = $wgTitle->getCascadeProtectionSources();
917
918 # Determine if protection is due to the page being a system message
919 # and show an appropriate explanation
920 if( $wgTitle->getNamespace() == NS_MEDIAWIKI ) {
921 $this->addWikiText( wfMsg( 'protectedinterface' ) );
922 } if ( $cascadeSources && count($cascadeSources) > 0 ) {
923 $titles = '';
924
925 foreach ( $cascadeSources as $title ) {
926 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
927 }
928
929 $notice = wfMsgExt( 'cascadeprotected', array('parsemag'), count($cascadeSources) ) . "\n$titles";
930
931 $this->addWikiText( $notice );
932 } else {
933 $this->addWikiText( wfMsg( 'protectedpagetext' ) );
934 }
935 } else {
936 $this->setPageTitle( wfMsg( 'readonly' ) );
937 if ( $wgReadOnly ) {
938 $reason = $wgReadOnly;
939 } else {
940 $reason = file_get_contents( $wgReadOnlyFile );
941 }
942 $this->addWikiText( wfMsg( 'readonlytext', $reason ) );
943 }
944
945 if( is_string( $source ) ) {
946 $this->addWikiText( wfMsg( 'viewsourcetext' ) );
947 $rows = $wgUser->getIntOption( 'rows' );
948 $cols = $wgUser->getIntOption( 'cols' );
949 $text = "\n<textarea name='wpTextbox1' id='wpTextbox1' cols='$cols' rows='$rows' readonly='readonly'>" .
950 htmlspecialchars( $source ) . "\n</textarea>";
951 $this->addHTML( $text );
952 }
953 $article = new Article($wgTitle);
954 $this->addHTML( $skin->formatTemplates($article->getUsedTemplates()) );
955
956 $this->returnToMain( false );
957 }
958
959 /** @deprecated */
960 public function fatalError( $message ) {
961 throw new FatalError( $message );
962 }
963
964 /** @deprecated */
965 public function unexpectedValueError( $name, $val ) {
966 throw new FatalError( wfMsg( 'unexpected', $name, $val ) );
967 }
968
969 /** @deprecated */
970 public function fileCopyError( $old, $new ) {
971 throw new FatalError( wfMsg( 'filecopyerror', $old, $new ) );
972 }
973
974 /** @deprecated */
975 public function fileRenameError( $old, $new ) {
976 throw new FatalError( wfMsg( 'filerenameerror', $old, $new ) );
977 }
978
979 /** @deprecated */
980 public function fileDeleteError( $name ) {
981 throw new FatalError( wfMsg( 'filedeleteerror', $name ) );
982 }
983
984 /** @deprecated */
985 public function fileNotFoundError( $name ) {
986 throw new FatalError( wfMsg( 'filenotfound', $name ) );
987 }
988
989 public function showFatalError( $message ) {
990 $this->setPageTitle( wfMsg( "internalerror" ) );
991 $this->setRobotpolicy( "noindex,nofollow" );
992 $this->setArticleRelated( false );
993 $this->enableClientCache( false );
994 $this->mRedirect = '';
995 $this->mBodytext = $message;
996 }
997
998 public function showUnexpectedValueError( $name, $val ) {
999 $this->showFatalError( wfMsg( 'unexpected', $name, $val ) );
1000 }
1001
1002 public function showFileCopyError( $old, $new ) {
1003 $this->showFatalError( wfMsg( 'filecopyerror', $old, $new ) );
1004 }
1005
1006 public function showFileRenameError( $old, $new ) {
1007 $this->showFatalError( wfMsg( 'filerenameerror', $old, $new ) );
1008 }
1009
1010 public function showFileDeleteError( $name ) {
1011 $this->showFatalError( wfMsg( 'filedeleteerror', $name ) );
1012 }
1013
1014 public function showFileNotFoundError( $name ) {
1015 $this->showFatalError( wfMsg( 'filenotfound', $name ) );
1016 }
1017
1018 /**
1019 * return from error messages or notes
1020 * @param $auto automatically redirect the user after 10 seconds
1021 * @param $returnto page title to return to. Default is Main Page.
1022 */
1023 public function returnToMain( $auto = true, $returnto = NULL ) {
1024 global $wgUser, $wgOut, $wgRequest;
1025
1026 if ( $returnto == NULL ) {
1027 $returnto = $wgRequest->getText( 'returnto' );
1028 }
1029
1030 if ( '' === $returnto ) {
1031 $returnto = Title::newMainPage();
1032 }
1033
1034 if ( is_object( $returnto ) ) {
1035 $titleObj = $returnto;
1036 } else {
1037 $titleObj = Title::newFromText( $returnto );
1038 }
1039 if ( !is_object( $titleObj ) ) {
1040 $titleObj = Title::newMainPage();
1041 }
1042
1043 $sk = $wgUser->getSkin();
1044 $link = $sk->makeLinkObj( $titleObj, '' );
1045
1046 $r = wfMsg( 'returnto', $link );
1047 if ( $auto ) {
1048 $wgOut->addMeta( 'http:Refresh', '10;url=' . $titleObj->escapeFullURL() );
1049 }
1050 $wgOut->addHTML( "\n<p>$r</p>\n" );
1051 }
1052
1053 /**
1054 * This function takes the title (first item of mGoodLinks), categories, existing and broken links for the page
1055 * and uses the first 10 of them for META keywords
1056 *
1057 * @param ParserOutput &$parserOutput
1058 */
1059 private function addKeywords( &$parserOutput ) {
1060 global $wgTitle;
1061 $this->addKeyword( $wgTitle->getPrefixedText() );
1062 $count = 1;
1063 $links2d =& $parserOutput->getLinks();
1064 if ( !is_array( $links2d ) ) {
1065 return;
1066 }
1067 foreach ( $links2d as $dbkeys ) {
1068 foreach( $dbkeys as $dbkey => $unused ) {
1069 $this->addKeyword( $dbkey );
1070 if ( ++$count > 10 ) {
1071 break 2;
1072 }
1073 }
1074 }
1075 }
1076
1077 /**
1078 * @return string The doctype, opening <html>, and head element.
1079 */
1080 public function headElement() {
1081 global $wgDocType, $wgDTD, $wgContLanguageCode, $wgOutputEncoding, $wgMimeType;
1082 global $wgXhtmlDefaultNamespace, $wgXhtmlNamespaces;
1083 global $wgUser, $wgContLang, $wgUseTrackbacks, $wgTitle, $wgStyleVersion;
1084
1085 if( $wgMimeType == 'text/xml' || $wgMimeType == 'application/xhtml+xml' || $wgMimeType == 'application/xml' ) {
1086 $ret = "<?xml version=\"1.0\" encoding=\"$wgOutputEncoding\" ?>\n";
1087 } else {
1088 $ret = '';
1089 }
1090
1091 $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
1092
1093 if ( '' == $this->getHTMLTitle() ) {
1094 $this->setHTMLTitle( wfMsg( 'pagetitle', $this->getPageTitle() ));
1095 }
1096
1097 $rtl = $wgContLang->isRTL() ? " dir='RTL'" : '';
1098 $ret .= "<html xmlns=\"{$wgXhtmlDefaultNamespace}\" ";
1099 foreach($wgXhtmlNamespaces as $tag => $ns) {
1100 $ret .= "xmlns:{$tag}=\"{$ns}\" ";
1101 }
1102 $ret .= "xml:lang=\"$wgContLanguageCode\" lang=\"$wgContLanguageCode\" $rtl>\n";
1103 $ret .= "<head>\n<title>" . htmlspecialchars( $this->getHTMLTitle() ) . "</title>\n";
1104 array_push( $this->mMetatags, array( "http:Content-type", "$wgMimeType; charset={$wgOutputEncoding}" ) );
1105
1106 $ret .= $this->getHeadLinks();
1107 global $wgStylePath;
1108 if( $this->isPrintable() ) {
1109 $media = '';
1110 } else {
1111 $media = "media='print'";
1112 }
1113 $printsheet = htmlspecialchars( "$wgStylePath/common/wikiprintable.css?$wgStyleVersion" );
1114 $ret .= "<link rel='stylesheet' type='text/css' $media href='$printsheet' />\n";
1115
1116 $sk = $wgUser->getSkin();
1117 $ret .= $sk->getHeadScripts();
1118 $ret .= $this->mScripts;
1119 $ret .= $sk->getUserStyles();
1120 $ret .= $this->getHeadItems();
1121
1122 if ($wgUseTrackbacks && $this->isArticleRelated())
1123 $ret .= $wgTitle->trackbackRDF();
1124
1125 $ret .= "</head>\n";
1126 return $ret;
1127 }
1128
1129 /**
1130 * @return string HTML tag links to be put in the header.
1131 */
1132 public function getHeadLinks() {
1133 global $wgRequest;
1134 $ret = '';
1135 foreach ( $this->mMetatags as $tag ) {
1136 if ( 0 == strcasecmp( 'http:', substr( $tag[0], 0, 5 ) ) ) {
1137 $a = 'http-equiv';
1138 $tag[0] = substr( $tag[0], 5 );
1139 } else {
1140 $a = 'name';
1141 }
1142 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\" />\n";
1143 }
1144
1145 $p = $this->mRobotpolicy;
1146 if( $p !== '' && $p != 'index,follow' ) {
1147 // http://www.robotstxt.org/wc/meta-user.html
1148 // Only show if it's different from the default robots policy
1149 $ret .= "<meta name=\"robots\" content=\"$p\" />\n";
1150 }
1151
1152 if ( count( $this->mKeywords ) > 0 ) {
1153 $strip = array(
1154 "/<.*?>/" => '',
1155 "/_/" => ' '
1156 );
1157 $ret .= "\t\t<meta name=\"keywords\" content=\"" .
1158 htmlspecialchars(preg_replace(array_keys($strip), array_values($strip),implode( ",", $this->mKeywords ))) . "\" />\n";
1159 }
1160 foreach ( $this->mLinktags as $tag ) {
1161 $ret .= "\t\t<link";
1162 foreach( $tag as $attr => $val ) {
1163 $ret .= " $attr=\"" . htmlspecialchars( $val ) . "\"";
1164 }
1165 $ret .= " />\n";
1166 }
1167 if( $this->isSyndicated() ) {
1168 # FIXME: centralize the mime-type and name information in Feed.php
1169 $link = $wgRequest->escapeAppendQuery( 'feed=rss' );
1170 $ret .= "<link rel='alternate' type='application/rss+xml' title='RSS 2.0' href='$link' />\n";
1171 $link = $wgRequest->escapeAppendQuery( 'feed=atom' );
1172 $ret .= "<link rel='alternate' type='application/atom+xml' title='Atom 1.0' href='$link' />\n";
1173 }
1174
1175 return $ret;
1176 }
1177
1178 /**
1179 * Turn off regular page output and return an error reponse
1180 * for when rate limiting has triggered.
1181 * @todo i18n
1182 */
1183 public function rateLimited() {
1184 global $wgOut;
1185 $wgOut->disable();
1186 wfHttpError( 500, 'Internal Server Error',
1187 'Sorry, the server has encountered an internal error. ' .
1188 'Please wait a moment and hit "refresh" to submit the request again.' );
1189 }
1190
1191 /**
1192 * Show an "add new section" link?
1193 *
1194 * @return bool True if the parser output instructs us to add one
1195 */
1196 public function showNewSectionLink() {
1197 return $this->mNewSectionLink;
1198 }
1199 }
1200 ?>