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