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