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