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