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