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