More globals and uninitialized variables fixes. Added WebRequest ($wgRequest)
[mediawiki.git] / includes / OutputPage.php
blob7288b242fcf4d26d821f569804abab7b0c982989
1 <?php
2 # See design.doc
4 if($wgUseTeX) include_once( "Math.php" );
6 class OutputPage {
7 var $mHeaders, $mCookies, $mMetatags, $mKeywords;
8 var $mLinktags, $mPagetitle, $mBodytext, $mDebugtext;
9 var $mHTMLtitle, $mRobotpolicy, $mIsarticle, $mPrintable;
10 var $mSubtitle, $mRedirect, $mHeadtext;
11 var $mLastModified, $mCategoryLinks;
13 var $mSuppressQuickbar;
14 var $mOnloadHandler;
15 var $mDoNothing;
16 var $mContainsOldMagic, $mContainsNewMagic;
17 var $mIsArticleRelated;
18 var $mParserOptions;
20 function OutputPage()
22 $this->mHeaders = $this->mCookies = $this->mMetatags =
23 $this->mKeywords = $this->mLinktags = array();
24 $this->mHTMLtitle = $this->mPagetitle = $this->mBodytext =
25 $this->mRedirect = $this->mLastModified =
26 $this->mSubtitle = $this->mDebugtext = $this->mRobotpolicy =
27 $this->mOnloadHandler = "";
28 $this->mIsArticleRelated = $this->mIsarticle = $this->mPrintable = true;
29 $this->mSuppressQuickbar = $this->mPrintable = false;
30 $this->mLanguageLinks = array();
31 $this->mCategoryLinks = array() ;
32 $this->mDoNothing = false;
33 $this->mContainsOldMagic = $this->mContainsNewMagic = 0;
34 $this->mParserOptions = ParserOptions::newFromUser( $temp = NULL );
37 function addHeader( $name, $val ) { array_push( $this->mHeaders, "$name: $val" ) ; }
38 function addCookie( $name, $val ) { array_push( $this->mCookies, array( $name, $val ) ); }
39 function redirect( $url, $responsecode = '302' ) { $this->mRedirect = $url; $this->mRedirectCode = $responsecode; }
41 # To add an http-equiv meta tag, precede the name with "http:"
42 function addMeta( $name, $val ) { array_push( $this->mMetatags, array( $name, $val ) ); }
43 function addKeyword( $text ) { array_push( $this->mKeywords, $text ); }
44 function addLink( $rel, $rev, $target ) { array_push( $this->mLinktags, array( $rel, $rev, $target ) ); }
46 # checkLastModified tells the client to use the client-cached page if
47 # possible. If sucessful, the OutputPage is disabled so that
48 # any future call to OutputPage->output() have no effect. The method
49 # returns true iff cache-ok headers was sent.
50 function checkLastModified ( $timestamp )
52 global $wgLang, $wgCachePages, $wgUser;
53 if( !$wgCachePages ) {
54 wfDebug( "CACHE DISABLED\n", false );
55 return;
57 if( preg_match( '/MSIE ([1-4]|5\.0)/', $_SERVER["HTTP_USER_AGENT"] ) ) {
58 # IE 5.0 has probs with our caching
59 wfDebug( "-- bad client, not caching\n", false );
60 return;
62 if( $wgUser->getOption( "nocache" ) ) {
63 wfDebug( "USER DISABLED CACHE\n", false );
64 return;
67 $lastmod = gmdate( "D, j M Y H:i:s", wfTimestamp2Unix(
68 max( $timestamp, $wgUser->mTouched ) ) ) . " GMT";
70 if( !empty( $_SERVER["HTTP_IF_MODIFIED_SINCE"] ) ) {
71 # IE sends sizes after the date like this:
72 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
73 # this breaks strtotime().
74 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
75 $ismodsince = wfUnix2Timestamp( strtotime( $modsince ) );
76 wfDebug( "-- client send If-Modified-Since: " . $modsince . "\n", false );
77 wfDebug( "-- we might send Last-Modified : $lastmod\n", false );
79 if( ($ismodsince >= $timestamp ) and $wgUser->validateCache( $ismodsince ) ) {
80 # Make sure you're in a place you can leave when you call us!
81 header( "HTTP/1.0 304 Not Modified" );
82 $this->mLastModified = $lastmod;
83 $this->sendCacheControl();
84 wfDebug( "CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
85 $this->disable();
86 return true;
87 } else {
88 wfDebug( "READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
89 $this->mLastModified = $lastmod;
91 } else {
92 wfDebug( "We're confused.\n", false );
93 $this->mLastModified = $lastmod;
97 function setRobotpolicy( $str ) { $this->mRobotpolicy = $str; }
98 function setHTMLtitle( $name ) { $this->mHTMLtitle = $name; }
99 function setPageTitle( $name ) { $this->mPagetitle = $name; }
100 function getPageTitle() { return $this->mPagetitle; }
101 function setSubtitle( $str ) { $this->mSubtitle = $str; }
102 function getSubtitle() { return $this->mSubtitle; }
103 function isArticle() { return $this->mIsarticle; }
104 function setPrintable() { $this->mPrintable = true; }
105 function isPrintable() { return $this->mPrintable; }
106 function setOnloadHandler( $js ) { $this->mOnloadHandler = $js; }
107 function getOnloadHandler() { return $this->mOnloadHandler; }
108 function disable() { $this->mDoNothing = true; }
110 function setArticleRelated( $v )
112 $this->mIsArticleRelated = $v;
113 if ( !$v ) {
114 $this->mIsarticle = false;
117 function setArticleFlag( $v ) {
118 $this->mIsarticle = $v;
119 if ( $v ) {
120 $this->mIsArticleRelated = $v;
124 function isArticleRelated()
126 return $this->mIsArticleRelated;
129 function getLanguageLinks() {
130 global $wgTitle, $wgLanguageCode;
131 global $wgDBconnection, $wgDBname;
132 return $this->mLanguageLinks;
134 function suppressQuickbar() { $this->mSuppressQuickbar = true; }
135 function isQuickbarSuppressed() { return $this->mSuppressQuickbar; }
137 function addHTML( $text ) { $this->mBodytext .= $text; }
138 function addHeadtext( $text ) { $this->mHeadtext .= $text; }
139 function debug( $text ) { $this->mDebugtext .= $text; }
141 function setParserOptions( $options )
143 return wfSetVar( $this->mParserOptions, $options );
146 # First pass--just handle <nowiki> sections, pass the rest off
147 # to doWikiPass2() which does all the real work.
149 # $cacheArticle - assume this text is the main text for the given article
151 function addWikiText( $text, $linestart = true, $cacheArticle = NULL )
153 global $wgParser, $wgParserCache, $wgUser, $wgTitle;
155 $parserOutput = false;
156 if ( $cacheArticle ) {
157 $parserOutput = $wgParserCache->get( $cacheArticle, $wgUser );
160 if ( $parserOutput === false ) {
161 $parserOutput = $wgParser->parse( $text, $wgTitle, $this->mParserOptions, $linestart );
162 if ( $cacheArticle ) {
163 $wgParserCache->save( $parserOutput, $cacheArticle, $wgUser );
167 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
168 $this->mCategoryLinks += $parserOutput->getCategoryLinks();
170 $this->addHTML( $parserOutput->getText() );
174 # Set the maximum cache time on the Squid in seconds
175 function setSquidMaxage( $maxage ) {
176 global $wgSquidMaxage;
177 $wgSquidMaxage = $maxage;
180 function sendCacheControl() {
181 global $wgUseSquid, $wgUseESI, $wgSquidMaxage;
182 # FIXME: This header may cause trouble with some versions of Internet Explorer
183 header( "Vary: Accept-Encoding, Cookie" );
184 if( $this->mLastModified != "" ) {
185 if( $wgUseSquid && ! isset( $_COOKIE[ini_get( "session.name") ] ) &&
186 ! $this->isPrintable() )
188 if ( $wgUseESI ) {
189 # We'll purge the proxy cache explicitly, but require end user agents
190 # to revalidate against the proxy on each visit.
191 # Surrogate-Control controls our Squid, Cache-Control downstream caches
192 wfDebug( "** proxy caching with ESI; {$this->mLastModified} **\n", false );
193 # start with a shorter timeout for initial testing
194 # header( 'Surrogate-Control: max-age=2678400+2678400, content="ESI/1.0"');
195 header( 'Surrogate-Control: max-age='.$wgSquidMaxage.'+'.$wgSquidMaxage.', content="ESI/1.0"');
196 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
197 } else {
198 # We'll purge the proxy cache for anons explicitly, but require end user agents
199 # to revalidate against the proxy on each visit.
200 # IMPORTANT! The Squid needs to replace the Cache-Control header with
201 # Cache-Control: s-maxage=0, must-revalidate, max-age=0
202 wfDebug( "** local proxy caching; {$this->mLastModified} **\n", false );
203 # start with a shorter timeout for initial testing
204 # header( "Cache-Control: s-maxage=2678400, must-revalidate, max-age=0" );
205 header( 'Cache-Control: s-maxage='.$wgSquidMaxage.', must-revalidate, max-age=0' );
207 } else {
208 # We do want clients to cache if they can, but they *must* check for updates
209 # on revisiting the page.
210 wfDebug( "** private caching; {$this->mLastModified} **\n", false );
211 header( "Expires: -1" );
212 header( "Cache-Control: private, must-revalidate, max-age=0" );
214 header( "Last-modified: {$this->mLastModified}" );
215 } else {
216 wfDebug( "** no caching **\n", false );
217 header( "Expires: -1" );
218 header( "Cache-Control: no-cache" );
219 header( "Pragma: no-cache" );
220 header( "Last-modified: " . gmdate( "D, j M Y H:i:s" ) . " GMT" );
224 # Finally, all the text has been munged and accumulated into
225 # the object, let's actually output it:
227 function output()
229 global $wgUser, $wgLang, $wgDebugComments, $wgCookieExpiration;
230 global $wgInputEncoding, $wgOutputEncoding, $wgLanguageCode;
231 global $wgDebugRedirects;
232 if( $this->mDoNothing ){
233 return;
235 $fname = "OutputPage::output";
236 wfProfileIn( $fname );
238 $sk = $wgUser->getSkin();
240 if ( "" != $this->mRedirect ) {
241 if( substr( $this->mRedirect, 0, 4 ) != "http" ) {
242 # Standards require redirect URLs to be absolute
243 global $wgServer;
244 $this->mRedirect = $wgServer . $this->mRedirect;
246 if( $this->mRedirectCode == '301') {
247 if( !$wgDebugRedirects ) {
248 header("HTTP/1.1 {$this->mRedirectCode} Moved Permanently");
250 $this->mLastModified = gmdate( "D, j M Y H:i:s" ) . " GMT";
253 $this->sendCacheControl();
255 if( $wgDebugRedirects ) {
256 $url = htmlspecialchars( $this->mRedirect );
257 print "<html>\n<head>\n<title>Redirect</title>\n</head>\n<body>\n";
258 print "<p>Location: <a href=\"$url\">$url</a></p>\n";
259 print "</body>\n</html>\n";
260 } else {
261 header( "Location: {$this->mRedirect}" );
263 return;
267 $this->sendCacheControl();
269 header( "Content-type: text/html; charset={$wgOutputEncoding}" );
270 header( "Content-language: {$wgLanguageCode}" );
272 $exp = time() + $wgCookieExpiration;
273 foreach( $this->mCookies as $name => $val ) {
274 setcookie( $name, $val, $exp, "/" );
277 $sk->outputPage( $this );
278 # flush();
281 function out( $ins )
283 global $wgInputEncoding, $wgOutputEncoding, $wgLang;
284 if ( 0 == strcmp( $wgInputEncoding, $wgOutputEncoding ) ) {
285 $outs = $ins;
286 } else {
287 $outs = $wgLang->iconv( $wgInputEncoding, $wgOutputEncoding, $ins );
288 if ( false === $outs ) { $outs = $ins; }
290 print $outs;
293 function setEncodings()
295 global $wgInputEncoding, $wgOutputEncoding;
296 global $wgUser, $wgLang;
298 $wgInputEncoding = strtolower( $wgInputEncoding );
300 if( $wgUser->getOption( 'altencoding' ) ) {
301 $wgLang->setAltEncoding();
302 return;
305 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
306 $wgOutputEncoding = strtolower( $wgOutputEncoding );
307 return;
311 # This code is unused anyway!
312 # Commenting out. --bv 2003-11-15
314 $a = explode( ",", $_SERVER['HTTP_ACCEPT_CHARSET'] );
315 $best = 0.0;
316 $bestset = "*";
318 foreach ( $a as $s ) {
319 if ( preg_match( "/(.*);q=(.*)/", $s, $m ) ) {
320 $set = $m[1];
321 $q = (float)($m[2]);
322 } else {
323 $set = $s;
324 $q = 1.0;
326 if ( $q > $best ) {
327 $bestset = $set;
328 $best = $q;
331 #if ( "*" == $bestset ) { $bestset = "iso-8859-1"; }
332 if ( "*" == $bestset ) { $bestset = $wgOutputEncoding; }
333 $wgOutputEncoding = strtolower( $bestset );
335 # Disable for now
338 $wgOutputEncoding = $wgInputEncoding;
341 # Returns a HTML comment with the elapsed time since request.
342 # This method has no side effects.
343 function reportTime()
345 global $wgRequestTime;
347 $now = wfTime();
348 list( $usec, $sec ) = explode( " ", $wgRequestTime );
349 $start = (float)$sec + (float)$usec;
350 $elapsed = $now - $start;
351 $com = sprintf( "<!-- Time since request: %01.2f secs. -->",
352 $elapsed );
353 return $com;
356 # Note: these arguments are keys into wfMsg(), not text!
358 function errorpage( $title, $msg )
360 global $wgTitle;
362 $this->mDebugtext .= "Original title: " .
363 $wgTitle->getPrefixedText() . "\n";
364 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
365 $this->setPageTitle( wfMsg( $title ) );
366 $this->setRobotpolicy( "noindex,nofollow" );
367 $this->setArticleRelated( false );
369 $this->mBodytext = "";
370 $this->addHTML( "<p>" . wfMsg( $msg ) . "\n" );
371 $this->returnToMain( false );
373 $this->output();
374 wfAbruptExit();
377 function sysopRequired()
379 global $wgUser;
381 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
382 $this->setPageTitle( wfMsg( "sysoptitle" ) );
383 $this->setRobotpolicy( "noindex,nofollow" );
384 $this->setArticleRelated( false );
385 $this->mBodytext = "";
387 $sk = $wgUser->getSkin();
388 $ap = $sk->makeKnownLink( wfMsg( "administrators" ), "" );
389 $this->addHTML( wfMsg( "sysoptext", $ap ) );
390 $this->returnToMain();
393 function developerRequired()
395 global $wgUser;
397 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
398 $this->setPageTitle( wfMsg( "developertitle" ) );
399 $this->setRobotpolicy( "noindex,nofollow" );
400 $this->setArticleRelated( false );
401 $this->mBodytext = "";
403 $sk = $wgUser->getSkin();
404 $ap = $sk->makeKnownLink( wfMsg( "administrators" ), "" );
405 $this->addHTML( wfMsg( "developertext", $ap ) );
406 $this->returnToMain();
409 function loginToUse()
411 global $wgUser, $wgTitle, $wgLang;
413 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
414 $this->setPageTitle( wfMsg( "loginreqtitle" ) );
415 $this->setRobotpolicy( "noindex,nofollow" );
416 $this->setArticleFlag( false );
417 $this->mBodytext = "";
418 $this->addWikiText( wfMsg( "loginreqtext" ) );
420 # We put a comment in the .html file so a Sysop can diagnose the page the
421 # user can't see.
422 $this->addHTML( "\n<!--" .
423 $wgLang->getNsText( $wgTitle->getNamespace() ) .
424 ":" .
425 $wgTitle->getDBkey() . "-->" );
426 $this->returnToMain(); # Flip back to the main page after 10 seconds.
429 function databaseError( $fname, &$conn )
431 global $wgUser, $wgCommandLineMode;
433 $this->setPageTitle( wfMsgNoDB( "databaseerror" ) );
434 $this->setRobotpolicy( "noindex,nofollow" );
435 $this->setArticleRelated( false );
437 if ( $wgCommandLineMode ) {
438 $msg = wfMsgNoDB( "dberrortextcl" );
439 } else {
440 $msg = wfMsgNoDB( "dberrortext" );
443 $msg = str_replace( "$1", htmlspecialchars( $conn->lastQuery() ), $msg );
444 $msg = str_replace( "$2", htmlspecialchars( $fname ), $msg );
445 $msg = str_replace( "$3", $conn->lastErrno(), $msg );
446 $msg = str_replace( "$4", htmlspecialchars( $conn->lastError() ), $msg );
448 if ( $wgCommandLineMode || !is_object( $wgUser )) {
449 print "$msg\n";
450 wfAbruptExit();
452 $sk = $wgUser->getSkin();
453 $shlink = $sk->makeKnownLink( wfMsgNoDB( "searchhelppage" ),
454 wfMsgNoDB( "searchingwikipedia" ) );
455 $msg = str_replace( "$5", $shlink, $msg );
456 $this->mBodytext = $msg;
457 $this->output();
458 wfAbruptExit();
461 function readOnlyPage( $source = "", $protected = false )
463 global $wgUser, $wgReadOnlyFile;
465 $this->setRobotpolicy( "noindex,nofollow" );
466 $this->setArticleRelated( false );
468 if( $protected ) {
469 $this->setPageTitle( wfMsg( "viewsource" ) );
470 $this->addWikiText( wfMsg( "protectedtext" ) );
471 } else {
472 $this->setPageTitle( wfMsg( "readonly" ) );
473 $reason = file_get_contents( $wgReadOnlyFile );
474 $this->addHTML( wfMsg( "readonlytext", $reason ) );
477 if($source) {
478 $rows = $wgUser->getOption( "rows" );
479 $cols = $wgUser->getOption( "cols" );
480 $text .= "</p>\n<textarea cols='$cols' rows='$rows' readonly>" .
481 htmlspecialchars( $source ) . "\n</textarea>";
482 $this->addHTML( $text );
485 $this->returnToMain( false );
488 function fatalError( $message )
490 $this->setPageTitle( wfMsg( "internalerror" ) );
491 $this->setRobotpolicy( "noindex,nofollow" );
492 $this->setArticleRelated( false );
494 $this->mBodytext = $message;
495 $this->output();
496 wfAbruptExit();
499 function unexpectedValueError( $name, $val )
501 $this->fatalError( wfMsg( "unexpected", $name, $val ) );
504 function fileCopyError( $old, $new )
506 $this->fatalError( wfMsg( "filecopyerror", $old, $new ) );
509 function fileRenameError( $old, $new )
511 $this->fatalError( wfMsg( "filerenameerror", $old, $new ) );
514 function fileDeleteError( $name )
516 $this->fatalError( wfMsg( "filedeleteerror", $name ) );
519 function fileNotFoundError( $name )
521 $this->fatalError( wfMsg( "filenotfound", $name ) );
524 function returnToMain( $auto = true )
526 global $wgUser, $wgOut, $returnto;
528 $sk = $wgUser->getSkin();
529 if ( "" == $returnto ) {
530 $returnto = wfMsg( "mainpage" );
532 $link = $sk->makeKnownLink( $returnto, "" );
534 $r = wfMsg( "returnto", $link );
535 if ( $auto ) {
536 $titleObj = Title::newFromText( $returnto );
537 $wgOut->addMeta( "http:Refresh", "10;url=" . $titleObj->escapeFullURL() );
539 $wgOut->addHTML( "\n<p>$r\n" );
542 /* private */ function headElement()
544 global $wgDocType, $wgDTD, $wgUser, $wgLanguageCode, $wgOutputEncoding, $wgLang;
546 $ret = "<!DOCTYPE HTML PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
548 if ( "" == $this->mHTMLtitle ) {
549 $this->mHTMLtitle = $this->mPagetitle;
551 $rtl = $wgLang->isRTL() ? " dir='RTL'" : "";
552 $ret .= "<html lang=\"$wgLanguageCode\"$rtl><head><title>{$this->mHTMLtitle}</title>\n";
553 array_push( $this->mMetatags, array( "http:Content-type", "text/html; charset={$wgOutputEncoding}" ) );
554 foreach ( $this->mMetatags as $tag ) {
555 if ( 0 == strcasecmp( "http:", substr( $tag[0], 0, 5 ) ) ) {
556 $a = "http-equiv";
557 $tag[0] = substr( $tag[0], 5 );
558 } else {
559 $a = "name";
561 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\">\n";
563 $p = $this->mRobotpolicy;
564 if ( "" == $p ) { $p = "index,follow"; }
565 $ret .= "<meta name=\"robots\" content=\"$p\">\n";
567 if ( count( $this->mKeywords ) > 0 ) {
568 $ret .= "<meta name=\"keywords\" content=\"" .
569 implode( ",", $this->mKeywords ) . "\">\n";
571 foreach ( $this->mLinktags as $tag ) {
572 $ret .= "<link ";
573 if ( "" != $tag[0] ) { $ret .= "rel=\"{$tag[0]}\" "; }
574 if ( "" != $tag[1] ) { $ret .= "rev=\"{$tag[1]}\" "; }
575 $ret .= "href=\"{$tag[2]}\">\n";
577 $sk = $wgUser->getSkin();
578 $ret .= $sk->getHeadScripts();
579 $ret .= $sk->getUserStyles();
581 $ret .= "</head>\n";
582 return $ret;