Parser improvements: global variable destruction
[lhc/web/wiklou.git] / includes / OutputPage.php
1 <?php
2 # See design.doc
3
4 if($wgUseTeX) include_once( "Math.php" );
5
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;
12
13 var $mSuppressQuickbar;
14 var $mOnloadHandler;
15 var $mDoNothing;
16 var $mContainsOldMagic, $mContainsNewMagic;
17 var $mIsArticleRelated;
18 var $mParserOptions;
19
20 function OutputPage()
21 {
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 );
35 }
36
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 ) { $this->mRedirect = $url; }
40
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 ) ); }
45
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 )
51 {
52 global $wgLang, $wgCachePages, $wgUser;
53 if( !$wgCachePages ) {
54 wfDebug( "CACHE DISABLED\n", false );
55 return;
56 }
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;
61 }
62 if( $wgUser->getOption( "nocache" ) ) {
63 wfDebug( "USER DISABLED CACHE\n", false );
64 return;
65 }
66
67 $lastmod = gmdate( "D, j M Y H:i:s", wfTimestamp2Unix(
68 max( $timestamp, $wgUser->mTouched ) ) ) . " GMT";
69
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 );
78
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;
90 }
91 } else {
92 wfDebug( "We're confused.\n", false );
93 $this->mLastModified = $lastmod;
94 }
95 }
96
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; }
109
110 function setArticleRelated( $v )
111 {
112 $this->mIsArticleRelated = $v;
113 if ( !$v ) {
114 $this->mIsarticle = false;
115 }
116 }
117 function setArticleFlag( $v ) {
118 $this->mIsarticle = $v;
119 if ( $v ) {
120 $this->mIsArticleRelated = $v;
121 }
122 }
123
124 function isArticleRelated()
125 {
126 return $this->mIsArticleRelated;
127 }
128
129 function getLanguageLinks() {
130 global $wgTitle, $wgLanguageCode;
131 global $wgDBconnection, $wgDBname;
132 return $this->mLanguageLinks;
133 }
134 function suppressQuickbar() { $this->mSuppressQuickbar = true; }
135 function isQuickbarSuppressed() { return $this->mSuppressQuickbar; }
136
137 function addHTML( $text ) { $this->mBodytext .= $text; }
138 function addHeadtext( $text ) { $this->mHeadtext .= $text; }
139 function debug( $text ) { $this->mDebugtext .= $text; }
140
141 function setParserOptions( $options )
142 {
143 return wfSetVar( $this->mParserOptions, $options );
144 }
145
146 # First pass--just handle <nowiki> sections, pass the rest off
147 # to doWikiPass2() which does all the real work.
148 #
149 # $cacheArticle - assume this text is the main text for the given article
150 #
151 function addWikiText( $text, $linestart = true, $cacheArticle = NULL )
152 {
153 global $wgParser, $wgParserCache, $wgUser, $wgTitle;
154
155 $parserOutput = false;
156 if ( $cacheArticle ) {
157 $parserOutput = $wgParserCache->get( $cacheArticle, $wgUser );
158 }
159
160 if ( $parserOutput === false ) {
161 $parserOutput = $wgParser->parse( $text, $wgTitle, $this->mParserOptions, $linestart );
162 if ( $cacheArticle ) {
163 $wgParserCache->save( $parserOutput, $cacheArticle, $wgUser );
164 }
165 }
166
167 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
168 $this->mCategoryLinks += $parserOutput->getCategoryLinks();
169
170 $this->addHTML( $parserOutput->getText() );
171
172 }
173
174 # Set the maximum cache time on the Squid in seconds
175 function setSquidMaxage( $maxage ) {
176 global $wgSquidMaxage;
177 $wgSquidMaxage = $maxage;
178 }
179
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() )
187 {
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' );
206 }
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" );
213 }
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" );
221 }
222 }
223
224 # Finally, all the text has been munged and accumulated into
225 # the object, let's actually output it:
226 #
227 function output()
228 {
229 global $wgUser, $wgLang, $wgDebugComments, $wgCookieExpiration;
230 global $wgInputEncoding, $wgOutputEncoding, $wgLanguageCode;
231 if( $this->mDoNothing ){
232 return;
233 }
234 $fname = "OutputPage::output";
235 wfProfileIn( $fname );
236
237 $sk = $wgUser->getSkin();
238
239 $this->sendCacheControl();
240
241 header( "Content-type: text/html; charset={$wgOutputEncoding}" );
242 header( "Content-language: {$wgLanguageCode}" );
243
244 if ( "" != $this->mRedirect ) {
245 if( substr( $this->mRedirect, 0, 4 ) != "http" ) {
246 # Standards require redirect URLs to be absolute
247 global $wgServer;
248 $this->mRedirect = $wgServer . $this->mRedirect;
249 }
250 header( "Location: {$this->mRedirect}" );
251 return;
252 }
253
254 $exp = time() + $wgCookieExpiration;
255 foreach( $this->mCookies as $name => $val ) {
256 setcookie( $name, $val, $exp, "/" );
257 }
258
259 $sk->outputPage( $this );
260 # flush();
261 }
262
263 function out( $ins )
264 {
265 global $wgInputEncoding, $wgOutputEncoding, $wgLang;
266 if ( 0 == strcmp( $wgInputEncoding, $wgOutputEncoding ) ) {
267 $outs = $ins;
268 } else {
269 $outs = $wgLang->iconv( $wgInputEncoding, $wgOutputEncoding, $ins );
270 if ( false === $outs ) { $outs = $ins; }
271 }
272 print $outs;
273 }
274
275 function setEncodings()
276 {
277 global $wgInputEncoding, $wgOutputEncoding;
278 global $wgUser, $wgLang;
279
280 $wgInputEncoding = strtolower( $wgInputEncoding );
281
282 if( $wgUser->getOption( 'altencoding' ) ) {
283 $wgLang->setAltEncoding();
284 return;
285 }
286
287 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
288 $wgOutputEncoding = strtolower( $wgOutputEncoding );
289 return;
290 }
291
292 /*
293 # This code is unused anyway!
294 # Commenting out. --bv 2003-11-15
295
296 $a = explode( ",", $_SERVER['HTTP_ACCEPT_CHARSET'] );
297 $best = 0.0;
298 $bestset = "*";
299
300 foreach ( $a as $s ) {
301 if ( preg_match( "/(.*);q=(.*)/", $s, $m ) ) {
302 $set = $m[1];
303 $q = (float)($m[2]);
304 } else {
305 $set = $s;
306 $q = 1.0;
307 }
308 if ( $q > $best ) {
309 $bestset = $set;
310 $best = $q;
311 }
312 }
313 #if ( "*" == $bestset ) { $bestset = "iso-8859-1"; }
314 if ( "*" == $bestset ) { $bestset = $wgOutputEncoding; }
315 $wgOutputEncoding = strtolower( $bestset );
316
317 # Disable for now
318 #
319 */
320 $wgOutputEncoding = $wgInputEncoding;
321 }
322
323 # Returns a HTML comment with the elapsed time since request.
324 # This method has no side effects.
325 function reportTime()
326 {
327 global $wgRequestTime;
328
329 $now = wfTime();
330 list( $usec, $sec ) = explode( " ", $wgRequestTime );
331 $start = (float)$sec + (float)$usec;
332 $elapsed = $now - $start;
333 $com = sprintf( "<!-- Time since request: %01.2f secs. -->",
334 $elapsed );
335 return $com;
336 }
337
338 # Note: these arguments are keys into wfMsg(), not text!
339 #
340 function errorpage( $title, $msg )
341 {
342 global $wgTitle;
343
344 $this->mDebugtext .= "Original title: " .
345 $wgTitle->getPrefixedText() . "\n";
346 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
347 $this->setPageTitle( wfMsg( $title ) );
348 $this->setRobotpolicy( "noindex,nofollow" );
349 $this->setArticleRelated( false );
350
351 $this->mBodytext = "";
352 $this->addHTML( "<p>" . wfMsg( $msg ) . "\n" );
353 $this->returnToMain( false );
354
355 $this->output();
356 wfAbruptExit();
357 }
358
359 function sysopRequired()
360 {
361 global $wgUser;
362
363 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
364 $this->setPageTitle( wfMsg( "sysoptitle" ) );
365 $this->setRobotpolicy( "noindex,nofollow" );
366 $this->setArticleRelated( false );
367 $this->mBodytext = "";
368
369 $sk = $wgUser->getSkin();
370 $ap = $sk->makeKnownLink( wfMsg( "administrators" ), "" );
371 $this->addHTML( wfMsg( "sysoptext", $ap ) );
372 $this->returnToMain();
373 }
374
375 function developerRequired()
376 {
377 global $wgUser;
378
379 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
380 $this->setPageTitle( wfMsg( "developertitle" ) );
381 $this->setRobotpolicy( "noindex,nofollow" );
382 $this->setArticleRelated( false );
383 $this->mBodytext = "";
384
385 $sk = $wgUser->getSkin();
386 $ap = $sk->makeKnownLink( wfMsg( "administrators" ), "" );
387 $this->addHTML( wfMsg( "developertext", $ap ) );
388 $this->returnToMain();
389 }
390
391 function databaseError( $fname, &$conn )
392 {
393 global $wgUser, $wgCommandLineMode;
394
395 $this->setPageTitle( wfMsgNoDB( "databaseerror" ) );
396 $this->setRobotpolicy( "noindex,nofollow" );
397 $this->setArticleRelated( false );
398
399 if ( $wgCommandLineMode ) {
400 $msg = wfMsgNoDB( "dberrortextcl" );
401 } else {
402 $msg = wfMsgNoDB( "dberrortext" );
403 }
404
405 $msg = str_replace( "$1", htmlspecialchars( $conn->lastQuery() ), $msg );
406 $msg = str_replace( "$2", htmlspecialchars( $fname ), $msg );
407 $msg = str_replace( "$3", $conn->lastErrno(), $msg );
408 $msg = str_replace( "$4", htmlspecialchars( $conn->lastError() ), $msg );
409
410 if ( $wgCommandLineMode || !is_object( $wgUser )) {
411 print "$msg\n";
412 wfAbruptExit();
413 }
414 $sk = $wgUser->getSkin();
415 $shlink = $sk->makeKnownLink( wfMsgNoDB( "searchhelppage" ),
416 wfMsgNoDB( "searchingwikipedia" ) );
417 $msg = str_replace( "$5", $shlink, $msg );
418 $this->mBodytext = $msg;
419 $this->output();
420 wfAbruptExit();
421 }
422
423 function readOnlyPage( $source = "", $protected = false )
424 {
425 global $wgUser, $wgReadOnlyFile;
426
427 $this->setRobotpolicy( "noindex,nofollow" );
428 $this->setArticleRelated( false );
429
430 if( $protected ) {
431 $this->setPageTitle( wfMsg( "viewsource" ) );
432 $this->addWikiText( wfMsg( "protectedtext" ) );
433 } else {
434 $this->setPageTitle( wfMsg( "readonly" ) );
435 $reason = file_get_contents( $wgReadOnlyFile );
436 $this->addHTML( wfMsg( "readonlytext", $reason ) );
437 }
438
439 if($source) {
440 $rows = $wgUser->getOption( "rows" );
441 $cols = $wgUser->getOption( "cols" );
442 $text .= "</p>\n<textarea cols='$cols' rows='$rows' readonly>" .
443 htmlspecialchars( $source ) . "\n</textarea>";
444 $this->addHTML( $text );
445 }
446
447 $this->returnToMain( false );
448 }
449
450 function fatalError( $message )
451 {
452 $this->setPageTitle( wfMsg( "internalerror" ) );
453 $this->setRobotpolicy( "noindex,nofollow" );
454 $this->setArticleRelated( false );
455
456 $this->mBodytext = $message;
457 $this->output();
458 wfAbruptExit();
459 }
460
461 function unexpectedValueError( $name, $val )
462 {
463 $this->fatalError( wfMsg( "unexpected", $name, $val ) );
464 }
465
466 function fileCopyError( $old, $new )
467 {
468 $this->fatalError( wfMsg( "filecopyerror", $old, $new ) );
469 }
470
471 function fileRenameError( $old, $new )
472 {
473 $this->fatalError( wfMsg( "filerenameerror", $old, $new ) );
474 }
475
476 function fileDeleteError( $name )
477 {
478 $this->fatalError( wfMsg( "filedeleteerror", $name ) );
479 }
480
481 function fileNotFoundError( $name )
482 {
483 $this->fatalError( wfMsg( "filenotfound", $name ) );
484 }
485
486 function returnToMain( $auto = true )
487 {
488 global $wgUser, $wgOut, $returnto;
489
490 $sk = $wgUser->getSkin();
491 if ( "" == $returnto ) {
492 $returnto = wfMsg( "mainpage" );
493 }
494 $link = $sk->makeKnownLink( $returnto, "" );
495
496 $r = wfMsg( "returnto", $link );
497 if ( $auto ) {
498 $wgOut->addMeta( "http:Refresh", "10;url=" .
499 wfLocalUrlE( wfUrlencode( $returnto ) ) );
500 }
501 $wgOut->addHTML( "\n<p>$r\n" );
502 }
503
504 /* private */ function headElement()
505 {
506 global $wgDocType, $wgDTD, $wgUser, $wgLanguageCode, $wgOutputEncoding, $wgLang;
507
508 $ret = "<!DOCTYPE HTML PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
509
510 if ( "" == $this->mHTMLtitle ) {
511 $this->mHTMLtitle = $this->mPagetitle;
512 }
513 $rtl = $wgLang->isRTL() ? " dir='RTL'" : "";
514 $ret .= "<html lang=\"$wgLanguageCode\"$rtl><head><title>{$this->mHTMLtitle}</title>\n";
515 array_push( $this->mMetatags, array( "http:Content-type", "text/html; charset={$wgOutputEncoding}" ) );
516 foreach ( $this->mMetatags as $tag ) {
517 if ( 0 == strcasecmp( "http:", substr( $tag[0], 0, 5 ) ) ) {
518 $a = "http-equiv";
519 $tag[0] = substr( $tag[0], 5 );
520 } else {
521 $a = "name";
522 }
523 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\">\n";
524 }
525 $p = $this->mRobotpolicy;
526 if ( "" == $p ) { $p = "index,follow"; }
527 $ret .= "<meta name=\"robots\" content=\"$p\">\n";
528
529 if ( count( $this->mKeywords ) > 0 ) {
530 $ret .= "<meta name=\"keywords\" content=\"" .
531 implode( ",", $this->mKeywords ) . "\">\n";
532 }
533 foreach ( $this->mLinktags as $tag ) {
534 $ret .= "<link ";
535 if ( "" != $tag[0] ) { $ret .= "rel=\"{$tag[0]}\" "; }
536 if ( "" != $tag[1] ) { $ret .= "rev=\"{$tag[1]}\" "; }
537 $ret .= "href=\"{$tag[2]}\">\n";
538 }
539 $sk = $wgUser->getSkin();
540 $ret .= $sk->getHeadScripts();
541 $ret .= $sk->getUserStyles();
542
543 $ret .= "</head>\n";
544 return $ret;
545 }
546 }
547 ?>