Fix #2393: incorrect MIME type for atom feeds
[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;
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 }
59
60 function addHeader( $name, $val ) { array_push( $this->mHeaders, $name.': '.$val ) ; }
61 function addCookie( $name, $val ) { array_push( $this->mCookies, array( $name, $val ) ); }
62 function redirect( $url, $responsecode = '302' ) { $this->mRedirect = $url; $this->mRedirectCode = $responsecode; }
63
64 # To add an http-equiv meta tag, precede the name with "http:"
65 function addMeta( $name, $val ) { array_push( $this->mMetatags, array( $name, $val ) ); }
66 function addKeyword( $text ) { array_push( $this->mKeywords, $text ); }
67 function addScript( $script ) { $this->mScripts .= $script; }
68 function getScript() { return $this->mScripts; }
69
70 function setETag($tag) { $this->mETag = $tag; }
71 function setArticleBodyOnly($only) { $this->mArticleBodyOnly = $only; }
72 function getArticleBodyOnly($only) { return $this->mArticleBodyOnly; }
73
74 function addLink( $linkarr ) {
75 # $linkarr should be an associative array of attributes. We'll escape on output.
76 array_push( $this->mLinktags, $linkarr );
77 }
78
79 function addMetadataLink( $linkarr ) {
80 # note: buggy CC software only reads first "meta" link
81 static $haveMeta = false;
82 $linkarr['rel'] = ($haveMeta) ? 'alternate meta' : 'meta';
83 $this->addLink( $linkarr );
84 $haveMeta = true;
85 }
86
87 /**
88 * checkLastModified tells the client to use the client-cached page if
89 * possible. If sucessful, the OutputPage is disabled so that
90 * any future call to OutputPage->output() have no effect. The method
91 * returns true iff cache-ok headers was sent.
92 */
93 function checkLastModified ( $timestamp ) {
94 global $wgLang, $wgCachePages, $wgUser;
95 if ( !$timestamp || $timestamp == '19700101000000' ) {
96 wfDebug( "CACHE DISABLED, NO TIMESTAMP\n" );
97 return;
98 }
99 if( !$wgCachePages ) {
100 wfDebug( "CACHE DISABLED\n", false );
101 return;
102 }
103 if( $wgUser->getOption( 'nocache' ) ) {
104 wfDebug( "USER DISABLED CACHE\n", false );
105 return;
106 }
107
108 $timestamp=wfTimestamp(TS_MW,$timestamp);
109 $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched ) );
110
111 if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
112 # IE sends sizes after the date like this:
113 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
114 # this breaks strtotime().
115 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
116 $modsinceTime = strtotime( $modsince );
117 $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
118 wfDebug( "-- client send If-Modified-Since: " . $modsince . "\n", false );
119 wfDebug( "-- we might send Last-Modified : $lastmod\n", false );
120 if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) ) {
121 # Make sure you're in a place you can leave when you call us!
122 header( "HTTP/1.0 304 Not Modified" );
123 $this->mLastModified = $lastmod;
124 $this->sendCacheControl();
125 wfDebug( "CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
126 $this->disable();
127 @ob_end_clean(); // Don't output compressed blob
128 return true;
129 } else {
130 wfDebug( "READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
131 $this->mLastModified = $lastmod;
132 }
133 } else {
134 wfDebug( "client did not send If-Modified-Since header\n", false );
135 $this->mLastModified = $lastmod;
136 }
137 }
138
139 function getPageTitleActionText () {
140 global $action;
141 switch($action) {
142 case 'edit':
143 return wfMsg('edit');
144 case 'history':
145 return wfMsg('history_short');
146 case 'protect':
147 return wfMsg('protect');
148 case 'unprotect':
149 return wfMsg('unprotect');
150 case 'delete':
151 return wfMsg('delete');
152 case 'watch':
153 return wfMsg('watch');
154 case 'unwatch':
155 return wfMsg('unwatch');
156 case 'submit':
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 $this->setHTMLTitle( $name . ' - ' . wfMsg( 'wikititlesuffix' ) );
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 function setParserOptions( $options ) {
234 return wfSetVar( $this->mParserOptions, $options );
235 }
236
237 /**
238 * Convert wikitext to HTML and add it to the buffer
239 * Default assumes that the current page title will
240 * be used.
241 */
242 function addWikiText( $text, $linestart = true ) {
243 global $wgTitle;
244 $this->addWikiTextTitle($text, $wgTitle, $linestart);
245 }
246
247 function addWikiTextWithTitle($text, &$title, $linestart = true) {
248 $this->addWikiTextTitle($text, $title, $linestart);
249 }
250
251 function addWikiTextTitle($text, &$title, $linestart) {
252 global $wgParser, $wgUseTidy;
253 $parserOutput = $wgParser->parse( $text, $title, $this->mParserOptions, $linestart );
254 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
255 $this->mCategoryLinks += $parserOutput->getCategoryLinks();
256 if ( $parserOutput->getCacheTime() == -1 ) {
257 $this->enableClientCache( false );
258 }
259 $this->addHTML( $parserOutput->getText() );
260 }
261
262 /**
263 * Add wikitext to the buffer, assuming that this is the primary text for a page view
264 * Saves the text into the parser cache if possible
265 */
266 function addPrimaryWikiText( $text, $cacheArticle ) {
267 global $wgParser, $wgParserCache, $wgUser, $wgTitle, $wgUseTidy;
268
269 $parserOutput = $wgParser->parse( $text, $wgTitle, $this->mParserOptions, true );
270
271 $text = $parserOutput->getText();
272
273 if ( $cacheArticle && $parserOutput->getCacheTime() != -1 ) {
274 $wgParserCache->save( $parserOutput, $cacheArticle, $wgUser );
275 }
276
277 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
278 $this->mCategoryLinks += $parserOutput->getCategoryLinks();
279 if ( $parserOutput->getCacheTime() == -1 ) {
280 $this->enableClientCache( false );
281 }
282 $this->addHTML( $text );
283 }
284
285 /**
286 * Add the output of a QuickTemplate to the output buffer
287 * @param QuickTemplate $template
288 */
289 function addTemplate( &$template ) {
290 ob_start();
291 $template->execute();
292 $this->addHtml( ob_get_contents() );
293 ob_end_clean();
294 }
295
296 /**
297 * Parse wikitext and return the HTML. This is for special pages that add the text later
298 */
299 function parse( $text, $linestart = true ) {
300 global $wgParser, $wgTitle;
301 $parserOutput = $wgParser->parse( $text, $wgTitle, $this->mParserOptions, $linestart );
302 return $parserOutput->getText();
303 }
304
305 /**
306 * @param $article
307 * @param $user
308 *
309 * @return bool
310 */
311 function tryParserCache( $article, $user ) {
312 global $wgParserCache;
313 $parserOutput = $wgParserCache->get( $article, $user );
314 if ( $parserOutput !== false ) {
315 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
316 $this->mCategoryLinks += $parserOutput->getCategoryLinks();
317 $this->addHTML( $parserOutput->getText() );
318 $t = $parserOutput->getTitleText();
319 if( !empty( $t ) ) {
320 $this->setPageTitle( $t );
321 }
322 return true;
323 } else {
324 return false;
325 }
326 }
327
328 /**
329 * Set the maximum cache time on the Squid in seconds
330 * @param $maxage
331 */
332 function setSquidMaxage( $maxage ) {
333 $this->mSquidMaxage = $maxage;
334 }
335
336 /**
337 * Use enableClientCache(false) to force it to send nocache headers
338 * @param $state
339 */
340 function enableClientCache( $state ) {
341 return wfSetVar( $this->mEnableClientCache, $state );
342 }
343
344 function uncacheableBecauseRequestvars() {
345 global $wgRequest;
346 return $wgRequest->getText('useskin', false) === false
347 && $wgRequest->getText('uselang', false) === false;
348 }
349
350 function sendCacheControl() {
351 global $wgUseSquid, $wgUseESI;
352
353 if ($this->mETag)
354 header("ETag: $this->mETag");
355
356 # don't serve compressed data to clients who can't handle it
357 # maintain different caches for logged-in users and non-logged in ones
358 header( 'Vary: Accept-Encoding, Cookie' );
359 if( !$this->uncacheableBecauseRequestvars() && $this->mEnableClientCache ) {
360 if( $wgUseSquid && ! isset( $_COOKIE[ini_get( 'session.name') ] ) &&
361 ! $this->isPrintable() && $this->mSquidMaxage != 0 )
362 {
363 if ( $wgUseESI ) {
364 # We'll purge the proxy cache explicitly, but require end user agents
365 # to revalidate against the proxy on each visit.
366 # Surrogate-Control controls our Squid, Cache-Control downstream caches
367 wfDebug( "** proxy caching with ESI; {$this->mLastModified} **\n", false );
368 # start with a shorter timeout for initial testing
369 # header( 'Surrogate-Control: max-age=2678400+2678400, content="ESI/1.0"');
370 header( 'Surrogate-Control: max-age='.$wgSquidMaxage.'+'.$this->mSquidMaxage.', content="ESI/1.0"');
371 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
372 } else {
373 # We'll purge the proxy cache for anons explicitly, but require end user agents
374 # to revalidate against the proxy on each visit.
375 # IMPORTANT! The Squid needs to replace the Cache-Control header with
376 # Cache-Control: s-maxage=0, must-revalidate, max-age=0
377 wfDebug( "** local proxy caching; {$this->mLastModified} **\n", false );
378 # start with a shorter timeout for initial testing
379 # header( "Cache-Control: s-maxage=2678400, must-revalidate, max-age=0" );
380 header( 'Cache-Control: s-maxage='.$this->mSquidMaxage.', must-revalidate, max-age=0' );
381 }
382 } else {
383 # We do want clients to cache if they can, but they *must* check for updates
384 # on revisiting the page.
385 wfDebug( "** private caching; {$this->mLastModified} **\n", false );
386 header( "Expires: -1" );
387 header( "Cache-Control: private, must-revalidate, max-age=0" );
388 }
389 if($this->mLastModified) header( "Last-modified: {$this->mLastModified}" );
390 } else {
391 wfDebug( "** no caching **\n", false );
392
393 # In general, the absence of a last modified header should be enough to prevent
394 # the client from using its cache. We send a few other things just to make sure.
395 header( 'Expires: -1' );
396 header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
397 header( 'Pragma: no-cache' );
398 }
399 }
400
401 /**
402 * Finally, all the text has been munged and accumulated into
403 * the object, let's actually output it:
404 */
405 function output() {
406 global $wgUser, $wgLang, $wgDebugComments, $wgCookieExpiration;
407 global $wgInputEncoding, $wgOutputEncoding, $wgContLanguageCode;
408 global $wgDebugRedirects, $wgMimeType, $wgProfiler;
409
410 if( $this->mDoNothing ){
411 return;
412 }
413 $fname = 'OutputPage::output';
414 wfProfileIn( $fname );
415 $sk = $wgUser->getSkin();
416
417 if ( '' != $this->mRedirect ) {
418 if( substr( $this->mRedirect, 0, 4 ) != 'http' ) {
419 # Standards require redirect URLs to be absolute
420 global $wgServer;
421 $this->mRedirect = $wgServer . $this->mRedirect;
422 }
423 if( $this->mRedirectCode == '301') {
424 if( !$wgDebugRedirects ) {
425 header("HTTP/1.1 {$this->mRedirectCode} Moved Permanently");
426 }
427 $this->mLastModified = wfTimestamp( TS_RFC2822 );
428 }
429
430 $this->sendCacheControl();
431
432 if( $wgDebugRedirects ) {
433 $url = htmlspecialchars( $this->mRedirect );
434 print "<html>\n<head>\n<title>Redirect</title>\n</head>\n<body>\n";
435 print "<p>Location: <a href=\"$url\">$url</a></p>\n";
436 print "</body>\n</html>\n";
437 } else {
438 header( 'Location: '.$this->mRedirect );
439 }
440 if ( isset( $wgProfiler ) ) { wfDebug( $wgProfiler->getOutput() ); }
441 wfProfileOut( $fname );
442 return;
443 }
444
445
446 # Buffer output; final headers may depend on later processing
447 ob_start();
448
449 $this->transformBuffer();
450
451 # Disable temporary placeholders, so that the skin produces HTML
452 $sk->postParseLinkColour( false );
453
454 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
455 header( 'Content-language: '.$wgContLanguageCode );
456
457 $exp = time() + $wgCookieExpiration;
458 foreach( $this->mCookies as $name => $val ) {
459 setcookie( $name, $val, $exp, '/' );
460 }
461
462 if ($this->mArticleBodyOnly) {
463 $this->out($this->mBodytext);
464 } else {
465 wfProfileIn( 'Output-skin' );
466 $sk->outputPage( $this );
467 wfProfileOut( 'Output-skin' );
468 }
469
470 $this->sendCacheControl();
471 ob_end_flush();
472 wfProfileOut( $fname );
473 }
474
475 function out( $ins ) {
476 global $wgInputEncoding, $wgOutputEncoding, $wgContLang;
477 if ( 0 == strcmp( $wgInputEncoding, $wgOutputEncoding ) ) {
478 $outs = $ins;
479 } else {
480 $outs = $wgContLang->iconv( $wgInputEncoding, $wgOutputEncoding, $ins );
481 if ( false === $outs ) { $outs = $ins; }
482 }
483 print $outs;
484 }
485
486 function setEncodings() {
487 global $wgInputEncoding, $wgOutputEncoding;
488 global $wgUser, $wgContLang;
489
490 $wgInputEncoding = strtolower( $wgInputEncoding );
491
492 if( $wgUser->getOption( 'altencoding' ) ) {
493 $wgContLang->setAltEncoding();
494 return;
495 }
496
497 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
498 $wgOutputEncoding = strtolower( $wgOutputEncoding );
499 return;
500 }
501 $wgOutputEncoding = $wgInputEncoding;
502 }
503
504 /**
505 * Returns a HTML comment with the elapsed time since request.
506 * This method has no side effects.
507 * @return string
508 */
509 function reportTime() {
510 global $wgRequestTime;
511
512 $now = wfTime();
513 list( $usec, $sec ) = explode( ' ', $wgRequestTime );
514 $start = (float)$sec + (float)$usec;
515 $elapsed = $now - $start;
516
517 # Use real server name if available, so we know which machine
518 # in a server farm generated the current page.
519 if ( function_exists( 'posix_uname' ) ) {
520 $uname = @posix_uname();
521 } else {
522 $uname = false;
523 }
524 if( is_array( $uname ) && isset( $uname['nodename'] ) ) {
525 $hostname = $uname['nodename'];
526 } else {
527 # This may be a virtual server.
528 $hostname = $_SERVER['SERVER_NAME'];
529 }
530 $com = sprintf( "<!-- Served by %s in %01.2f secs. -->",
531 $hostname, $elapsed );
532 return $com;
533 }
534
535 /**
536 * Note: these arguments are keys into wfMsg(), not text!
537 */
538 function errorpage( $title, $msg ) {
539 global $wgTitle;
540
541 $this->mDebugtext .= 'Original title: ' .
542 $wgTitle->getPrefixedText() . "\n";
543 $this->setPageTitle( wfMsg( $title ) );
544 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
545 $this->setRobotpolicy( 'noindex,nofollow' );
546 $this->setArticleRelated( false );
547 $this->enableClientCache( false );
548 $this->mRedirect = '';
549
550 $this->mBodytext = '';
551 $this->addWikiText( wfMsg( $msg ) );
552 $this->returnToMain( false );
553
554 $this->output();
555 wfErrorExit();
556 }
557
558 /**
559 * Display an error page indicating that a given version of MediaWiki is
560 * required to use it
561 *
562 * @param mixed $version The version of MediaWiki needed to use the page
563 */
564 function versionRequired( $version ) {
565 global $wgUser;
566
567 $this->setPageTitle( wfMsg( 'versionrequired', $version ) );
568 $this->setHTMLTitle( wfMsg( 'versionrequired', $version ) );
569 $this->setRobotpolicy( 'noindex,nofollow' );
570 $this->setArticleRelated( false );
571 $this->mBodytext = '';
572
573 $sk = $wgUser->getSkin();
574 $this->addWikiText( wfMsg( 'versionrequiredtext', $version ) );
575 $this->returnToMain();
576 }
577
578 /**
579 * Display an error page noting that a given permission bit is required.
580 * This should generally replace the sysopRequired, developerRequired etc.
581 * @param string $permission key required
582 */
583 function permissionRequired( $permission ) {
584 global $wgUser;
585
586 $this->setPageTitle( wfMsg( 'badaccess' ) );
587 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
588 $this->setRobotpolicy( 'noindex,nofollow' );
589 $this->setArticleRelated( false );
590 $this->mBodytext = '';
591
592 $sk = $wgUser->getSkin();
593 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ) );
594 $this->addHTML( wfMsgHtml( 'badaccesstext', $ap, $permission ) );
595 $this->returnToMain();
596 }
597
598 /**
599 * @deprecated
600 */
601 function sysopRequired() {
602 global $wgUser;
603
604 $this->setPageTitle( wfMsg( 'sysoptitle' ) );
605 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
606 $this->setRobotpolicy( 'noindex,nofollow' );
607 $this->setArticleRelated( false );
608 $this->mBodytext = '';
609
610 $sk = $wgUser->getSkin();
611 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ), '' );
612 $this->addHTML( wfMsgHtml( 'sysoptext', $ap ) );
613 $this->returnToMain();
614 }
615
616 /**
617 * @deprecated
618 */
619 function developerRequired() {
620 global $wgUser;
621
622 $this->setPageTitle( wfMsg( 'developertitle' ) );
623 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
624 $this->setRobotpolicy( 'noindex,nofollow' );
625 $this->setArticleRelated( false );
626 $this->mBodytext = '';
627
628 $sk = $wgUser->getSkin();
629 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ), '' );
630 $this->addHTML( wfMsgHtml( 'developertext', $ap ) );
631 $this->returnToMain();
632 }
633
634 function loginToUse() {
635 global $wgUser, $wgTitle, $wgContLang;
636
637 $this->setPageTitle( wfMsg( 'loginreqtitle' ) );
638 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
639 $this->setRobotpolicy( 'noindex,nofollow' );
640 $this->setArticleFlag( false );
641 $this->mBodytext = '';
642 $loginpage = Title::makeTitle(NS_SPECIAL, 'Userlogin');
643 $sk = $wgUser->getSkin();
644 $loginlink = $sk->makeKnownLinkObj($loginpage, wfMsg('loginreqlink'),
645 'returnto=' . htmlspecialchars($wgTitle->getPrefixedDBkey()));
646 $this->addHTML( wfMsgHtml( 'loginreqpagetext', $loginlink ) );
647
648 # We put a comment in the .html file so a Sysop can diagnose the page the
649 # user can't see.
650 $this->addHTML( "\n<!--" .
651 $wgContLang->getNsText( $wgTitle->getNamespace() ) .
652 ':' .
653 $wgTitle->getDBkey() . '-->' );
654 $this->returnToMain(); # Flip back to the main page after 10 seconds.
655 }
656
657 function databaseError( $fname, $sql, $error, $errno ) {
658 global $wgUser, $wgCommandLineMode, $wgShowSQLErrors;
659
660 $this->setPageTitle( wfMsgNoDB( 'databaseerror' ) );
661 $this->setRobotpolicy( 'noindex,nofollow' );
662 $this->setArticleRelated( false );
663 $this->enableClientCache( false );
664 $this->mRedirect = '';
665
666 if( !$wgShowSQLErrors ) {
667 $sql = wfMsg( 'sqlhidden' );
668 }
669
670 if ( $wgCommandLineMode ) {
671 $msg = wfMsgNoDB( 'dberrortextcl', htmlspecialchars( $sql ),
672 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
673 } else {
674 $msg = wfMsgNoDB( 'dberrortext', htmlspecialchars( $sql ),
675 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
676 }
677
678 if ( $wgCommandLineMode || !is_object( $wgUser )) {
679 print $msg."\n";
680 wfErrorExit();
681 }
682 $this->mBodytext = $msg;
683 $this->output();
684 wfErrorExit();
685 }
686
687 function readOnlyPage( $source = null, $protected = false ) {
688 global $wgUser, $wgReadOnlyFile, $wgReadOnly;
689
690 $this->setRobotpolicy( 'noindex,nofollow' );
691 $this->setArticleRelated( false );
692
693 if( $protected ) {
694 $this->setPageTitle( wfMsg( 'viewsource' ) );
695 $this->addWikiText( wfMsg( 'protectedtext' ) );
696 } else {
697 $this->setPageTitle( wfMsg( 'readonly' ) );
698 if ( $wgReadOnly ) {
699 $reason = $wgReadOnly;
700 } else {
701 $reason = file_get_contents( $wgReadOnlyFile );
702 }
703 $this->addWikiText( wfMsg( 'readonlytext', $reason ) );
704 }
705
706 if( is_string( $source ) ) {
707 if( strcmp( $source, '' ) == 0 ) {
708 $source = wfMsg( 'noarticletext' );
709 }
710 $rows = $wgUser->getOption( 'rows' );
711 $cols = $wgUser->getOption( 'cols' );
712 $text = "\n<textarea cols='$cols' rows='$rows' readonly='readonly'>" .
713 htmlspecialchars( $source ) . "\n</textarea>";
714 $this->addHTML( $text );
715 }
716
717 $this->returnToMain( false );
718 }
719
720 function fatalError( $message ) {
721 $this->setPageTitle( wfMsg( "internalerror" ) );
722 $this->setRobotpolicy( "noindex,nofollow" );
723 $this->setArticleRelated( false );
724 $this->enableClientCache( false );
725 $this->mRedirect = '';
726
727 $this->mBodytext = $message;
728 $this->output();
729 wfErrorExit();
730 }
731
732 function unexpectedValueError( $name, $val ) {
733 $this->fatalError( wfMsg( 'unexpected', $name, $val ) );
734 }
735
736 function fileCopyError( $old, $new ) {
737 $this->fatalError( wfMsg( 'filecopyerror', $old, $new ) );
738 }
739
740 function fileRenameError( $old, $new ) {
741 $this->fatalError( wfMsg( 'filerenameerror', $old, $new ) );
742 }
743
744 function fileDeleteError( $name ) {
745 $this->fatalError( wfMsg( 'filedeleteerror', $name ) );
746 }
747
748 function fileNotFoundError( $name ) {
749 $this->fatalError( wfMsg( 'filenotfound', $name ) );
750 }
751
752 /**
753 * return from error messages or notes
754 * @param $auto automatically redirect the user after 10 seconds
755 * @param $returnto page title to return to. Default is Main Page.
756 */
757 function returnToMain( $auto = true, $returnto = NULL ) {
758 global $wgUser, $wgOut, $wgRequest;
759
760 if ( $returnto == NULL ) {
761 $returnto = $wgRequest->getText( 'returnto' );
762 }
763 $returnto = htmlspecialchars( $returnto );
764
765 $sk = $wgUser->getSkin();
766 if ( '' == $returnto ) {
767 $returnto = wfMsgForContent( 'mainpage' );
768 }
769 $link = $sk->makeKnownLink( $returnto, '' );
770
771 $r = wfMsg( 'returnto', $link );
772 if ( $auto ) {
773 $titleObj = Title::newFromText( $returnto );
774 $wgOut->addMeta( 'http:Refresh', '10;url=' . $titleObj->escapeFullURL() );
775 }
776 $wgOut->addHTML( "\n<p>$r</p>\n" );
777 }
778
779 /**
780 * This function takes the title (first item of mGoodLinks), categories, existing and broken links for the page
781 * and uses the first 10 of them for META keywords
782 */
783 function addMetaTags () {
784 global $wgLinkCache , $wgOut ;
785 $categories = array_keys ( $wgLinkCache->mCategoryLinks ) ;
786 $good = array_keys ( $wgLinkCache->mGoodLinks ) ;
787 $bad = array_keys ( $wgLinkCache->mBadLinks ) ;
788 $a = array_merge ( array_slice ( $good , 0 , 1 ), $categories, array_slice ( $good , 1 , 9 ) , $bad ) ;
789 $a = array_slice ( $a , 0 , 10 ) ; # 10 keywords max
790 $a = implode ( ',' , $a ) ;
791 $strip = array(
792 "/<.*?>/" => '',
793 "/_/" => ' '
794 );
795 $a = htmlspecialchars(preg_replace(array_keys($strip), array_values($strip),$a ));
796
797 $wgOut->addMeta ( 'KEYWORDS' , $a ) ;
798 }
799
800 /**
801 * @private
802 * @return string
803 */
804 function headElement() {
805 global $wgDocType, $wgDTD, $wgContLanguageCode, $wgOutputEncoding, $wgMimeType;
806 global $wgUser, $wgContLang, $wgRequest, $wgUseTrackbacks, $wgTitle;
807
808 if( $wgMimeType == 'text/xml' || $wgMimeType == 'application/xhtml+xml' || $wgMimeType == 'application/xml' ) {
809 $ret = "<?xml version=\"1.0\" encoding=\"$wgOutputEncoding\" ?>\n";
810 } else {
811 $ret = '';
812 }
813
814 $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
815
816 if ( "" == $this->mHTMLtitle ) {
817 $this->mHTMLtitle = wfMsg( "pagetitle", $this->mPagetitle );
818 }
819
820 $rtl = $wgContLang->isRTL() ? " dir='RTL'" : '';
821 $ret .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"$wgContLanguageCode\" lang=\"$wgContLanguageCode\" $rtl>\n";
822 $ret .= "<head>\n<title>" . htmlspecialchars( $this->mHTMLtitle ) . "</title>\n";
823 array_push( $this->mMetatags, array( "http:Content-type", "$wgMimeType; charset={$wgOutputEncoding}" ) );
824
825 $ret .= $this->getHeadLinks();
826 global $wgStylePath;
827 if( $this->isPrintable() ) {
828 $media = '';
829 } else {
830 $media = "media='print'";
831 }
832 $printsheet = htmlspecialchars( "$wgStylePath/common/wikiprintable.css" );
833 $ret .= "<link rel='stylesheet' type='text/css' $media href='$printsheet' />\n";
834
835 $sk = $wgUser->getSkin();
836 $ret .= $sk->getHeadScripts();
837 $ret .= $this->mScripts;
838 $ret .= $sk->getUserStyles();
839
840 if ($wgUseTrackbacks && $this->isArticleRelated())
841 $ret .= $wgTitle->trackbackRDF();
842
843 $ret .= "</head>\n";
844 return $ret;
845 }
846
847 function getHeadLinks() {
848 global $wgRequest, $wgStylePath;
849 $ret = '';
850 foreach ( $this->mMetatags as $tag ) {
851 if ( 0 == strcasecmp( 'http:', substr( $tag[0], 0, 5 ) ) ) {
852 $a = 'http-equiv';
853 $tag[0] = substr( $tag[0], 5 );
854 } else {
855 $a = 'name';
856 }
857 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\" />\n";
858 }
859 $p = $this->mRobotpolicy;
860 if ( '' == $p ) { $p = 'index,follow'; }
861 $ret .= "<meta name=\"robots\" content=\"$p\" />\n";
862
863 if ( count( $this->mKeywords ) > 0 ) {
864 $strip = array(
865 "/<.*?>/" => '',
866 "/_/" => ' '
867 );
868 $ret .= "<meta name=\"keywords\" content=\"" .
869 htmlspecialchars(preg_replace(array_keys($strip), array_values($strip),implode( ",", $this->mKeywords ))) . "\" />\n";
870 }
871 foreach ( $this->mLinktags as $tag ) {
872 $ret .= '<link';
873 foreach( $tag as $attr => $val ) {
874 $ret .= " $attr=\"" . htmlspecialchars( $val ) . "\"";
875 }
876 $ret .= " />\n";
877 }
878 if( $this->isSyndicated() ) {
879 # FIXME: centralize the mime-type and name information in Feed.php
880 $link = $wgRequest->escapeAppendQuery( 'feed=rss' );
881 $ret .= "<link rel='alternate' type='application/rss+xml' title='RSS 2.0' href='$link' />\n";
882 $link = $wgRequest->escapeAppendQuery( 'feed=atom' );
883 $ret .= "<link rel='alternate' type='application/atom+xml' title='Atom 0.3' href='$link' />\n";
884 }
885
886 return $ret;
887 }
888
889 /**
890 * Run any necessary pre-output transformations on the buffer text
891 */
892 function transformBuffer( $options = 0 ) {
893 }
894
895
896 /**
897 * Turn off regular page output and return an error reponse
898 * for when rate limiting has triggered.
899 * @todo i18n
900 * @access public
901 */
902 function rateLimited() {
903 global $wgOut;
904 $wgOut->disable();
905 wfHttpError( 500, 'Internal Server Error',
906 'Sorry, the server has encountered an internal error. ' .
907 'Please wait a moment and hit "refresh" to submit the request again.' );
908 }
909
910 }
911
912 } // MediaWiki
913
914 ?>