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