Split parser from OutputPage into Parser.php, corrected spelling of "suppress", a...
[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
19 function OutputPage()
20 {
21 $this->mHeaders = $this->mCookies = $this->mMetatags =
22 $this->mKeywords = $this->mLinktags = array();
23 $this->mHTMLtitle = $this->mPagetitle = $this->mBodytext =
24 $this->mRedirect = $this->mLastModified =
25 $this->mSubtitle = $this->mDebugtext = $this->mRobotpolicy =
26 $this->mOnloadHandler = "";
27 $this->mIsArticleRelated = $this->mIsarticle = $this->mPrintable = true;
28 $this->mSuppressQuickbar = $this->mPrintable = false;
29 $this->mLanguageLinks = array();
30 $this->mCategoryLinks = array() ;
31 $this->mDoNothing = false;
32 $this->mContainsOldMagic = $this->mContainsNewMagic = 0;
33 }
34
35 function addHeader( $name, $val ) { array_push( $this->mHeaders, "$name: $val" ) ; }
36 function addCookie( $name, $val ) { array_push( $this->mCookies, array( $name, $val ) ); }
37 function redirect( $url ) { $this->mRedirect = $url; }
38
39 # To add an http-equiv meta tag, precede the name with "http:"
40 function addMeta( $name, $val ) { array_push( $this->mMetatags, array( $name, $val ) ); }
41 function addKeyword( $text ) { array_push( $this->mKeywords, $text ); }
42 function addLink( $rel, $rev, $target ) { array_push( $this->mLinktags, array( $rel, $rev, $target ) ); }
43
44 # checkLastModified tells the client to use the client-cached page if
45 # possible. If sucessful, the OutputPage is disabled so that
46 # any future call to OutputPage->output() have no effect. The method
47 # returns true iff cache-ok headers was sent.
48 function checkLastModified ( $timestamp )
49 {
50 global $wgLang, $wgCachePages, $wgUser;
51 if( !$wgCachePages ) {
52 wfDebug( "CACHE DISABLED\n", false );
53 return;
54 }
55 if( preg_match( '/MSIE ([1-4]|5\.0)/', $_SERVER["HTTP_USER_AGENT"] ) ) {
56 # IE 5.0 has probs with our caching
57 wfDebug( "-- bad client, not caching\n", false );
58 return;
59 }
60 if( $wgUser->getOption( "nocache" ) ) {
61 wfDebug( "USER DISABLED CACHE\n", false );
62 return;
63 }
64
65 $lastmod = gmdate( "D, j M Y H:i:s", wfTimestamp2Unix(
66 max( $timestamp, $wgUser->mTouched ) ) ) . " GMT";
67
68 if( !empty( $_SERVER["HTTP_IF_MODIFIED_SINCE"] ) ) {
69 # IE sends sizes after the date like this:
70 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
71 # this breaks strtotime().
72 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
73 $ismodsince = wfUnix2Timestamp( strtotime( $modsince ) );
74 wfDebug( "-- client send If-Modified-Since: " . $modsince . "\n", false );
75 wfDebug( "-- we might send Last-Modified : $lastmod\n", false );
76
77 if( ($ismodsince >= $timestamp ) and $wgUser->validateCache( $ismodsince ) ) {
78 # Make sure you're in a place you can leave when you call us!
79 header( "HTTP/1.0 304 Not Modified" );
80 $this->mLastModified = $lastmod;
81 $this->sendCacheControl();
82 wfDebug( "CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
83 $this->disable();
84 return true;
85 } else {
86 wfDebug( "READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
87 $this->mLastModified = $lastmod;
88 }
89 } else {
90 wfDebug( "We're confused.\n", false );
91 $this->mLastModified = $lastmod;
92 }
93 }
94
95 function setRobotpolicy( $str ) { $this->mRobotpolicy = $str; }
96 function setHTMLtitle( $name ) { $this->mHTMLtitle = $name; }
97 function setPageTitle( $name ) { $this->mPagetitle = $name; }
98 function getPageTitle() { return $this->mPagetitle; }
99 function setSubtitle( $str ) { $this->mSubtitle = $str; }
100 function getSubtitle() { return $this->mSubtitle; }
101 function isArticle() { return $this->mIsarticle; }
102 function setPrintable() { $this->mPrintable = true; }
103 function isPrintable() { return $this->mPrintable; }
104 function setOnloadHandler( $js ) { $this->mOnloadHandler = $js; }
105 function getOnloadHandler() { return $this->mOnloadHandler; }
106 function disable() { $this->mDoNothing = true; }
107
108 function setArticleRelated( $v )
109 {
110 $this->mIsArticleRelated = $v;
111 if ( !$v ) {
112 $this->mIsarticle = false;
113 }
114 }
115 function setArticleFlag( $v ) {
116 $this->mIsarticle = $v;
117 if ( $v ) {
118 $this->mIsArticleRelated = $v;
119 }
120 }
121
122 function isArticleRelated()
123 {
124 return $this->mIsArticleRelated;
125 }
126
127 function getLanguageLinks() {
128 global $wgTitle, $wgLanguageCode;
129 global $wgDBconnection, $wgDBname;
130 return $this->mLanguageLinks;
131 }
132 function suppressQuickbar() { $this->mSuppressQuickbar = true; }
133 function isQuickbarSuppressed() { return $this->mSuppressQuickbar; }
134
135 function addHTML( $text ) { $this->mBodytext .= $text; }
136 function addHeadtext( $text ) { $this->mHeadtext .= $text; }
137 function debug( $text ) { $this->mDebugtext .= $text; }
138
139 # First pass--just handle <nowiki> sections, pass the rest off
140 # to doWikiPass2() which does all the real work.
141 #
142 # $cacheArticle - assume this text is the main text for the given article
143 #
144 function addWikiText( $text, $linestart = true, $cacheArticle = NULL )
145 {
146 global $wgParser, $wgParserCache, $wgUser;
147
148 $parserOutput = false;
149 if ( $cacheArticle ) {
150 $parserOutput = $wgParserCache->get( $cacheArticle, $wgUser );
151 }
152
153 if ( $parserOutput === false ) {
154 $parserOutput = $wgParser->parse( $text );
155 if ( $cacheArticle ) {
156 $wgParserCache->save( $parserOutput, $cacheArticle, $wgUser );
157 }
158 }
159
160 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
161 $this->mCategoryLinks += $parserOutput->getCategoryLinks();
162
163 $this->addHTML( $parserOutput->getText() );
164
165 }
166
167 # Set the maximum cache time on the Squid in seconds
168 function setSquidMaxage( $maxage ) {
169 global $wgSquidMaxage;
170 $wgSquidMaxage = $maxage;
171 }
172
173 function sendCacheControl() {
174 global $wgUseSquid, $wgUseESI, $wgSquidMaxage;
175 # FIXME: This header may cause trouble with some versions of Internet Explorer
176 header( "Vary: Accept-Encoding, Cookie" );
177 if( $this->mLastModified != "" ) {
178 if( $wgUseSquid && ! isset( $_COOKIE[ini_get( "session.name") ] ) &&
179 ! $this->isPrintable() )
180 {
181 if ( $wgUseESI ) {
182 # We'll purge the proxy cache explicitly, but require end user agents
183 # to revalidate against the proxy on each visit.
184 # Surrogate-Control controls our Squid, Cache-Control downstream caches
185 wfDebug( "** proxy caching with ESI; {$this->mLastModified} **\n", false );
186 # start with a shorter timeout for initial testing
187 # header( 'Surrogate-Control: max-age=2678400+2678400, content="ESI/1.0"');
188 header( 'Surrogate-Control: max-age='.$wgSquidMaxage.'+'.$wgSquidMaxage.', content="ESI/1.0"');
189 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
190 } else {
191 # We'll purge the proxy cache for anons explicitly, but require end user agents
192 # to revalidate against the proxy on each visit.
193 # IMPORTANT! The Squid needs to replace the Cache-Control header with
194 # Cache-Control: s-maxage=0, must-revalidate, max-age=0
195 wfDebug( "** local proxy caching; {$this->mLastModified} **\n", false );
196 # start with a shorter timeout for initial testing
197 # header( "Cache-Control: s-maxage=2678400, must-revalidate, max-age=0" );
198 header( 'Cache-Control: s-maxage='.$wgSquidMaxage.', must-revalidate, max-age=0' );
199 }
200 } else {
201 # We do want clients to cache if they can, but they *must* check for updates
202 # on revisiting the page.
203 wfDebug( "** private caching; {$this->mLastModified} **\n", false );
204 header( "Expires: -1" );
205 header( "Cache-Control: private, must-revalidate, max-age=0" );
206 }
207 header( "Last-modified: {$this->mLastModified}" );
208 } else {
209 wfDebug( "** no caching **\n", false );
210 header( "Expires: -1" );
211 header( "Cache-Control: no-cache" );
212 header( "Pragma: no-cache" );
213 header( "Last-modified: " . gmdate( "D, j M Y H:i:s" ) . " GMT" );
214 }
215 }
216
217 # Finally, all the text has been munged and accumulated into
218 # the object, let's actually output it:
219 #
220 function output()
221 {
222 global $wgUser, $wgLang, $wgDebugComments, $wgCookieExpiration;
223 global $wgInputEncoding, $wgOutputEncoding, $wgLanguageCode;
224 if( $this->mDoNothing ){
225 return;
226 }
227 $fname = "OutputPage::output";
228 wfProfileIn( $fname );
229
230 $sk = $wgUser->getSkin();
231
232 $this->sendCacheControl();
233
234 header( "Content-type: text/html; charset={$wgOutputEncoding}" );
235 header( "Content-language: {$wgLanguageCode}" );
236
237 if ( "" != $this->mRedirect ) {
238 if( substr( $this->mRedirect, 0, 4 ) != "http" ) {
239 # Standards require redirect URLs to be absolute
240 global $wgServer;
241 $this->mRedirect = $wgServer . $this->mRedirect;
242 }
243 header( "Location: {$this->mRedirect}" );
244 return;
245 }
246
247 $exp = time() + $wgCookieExpiration;
248 foreach( $this->mCookies as $name => $val ) {
249 setcookie( $name, $val, $exp, "/" );
250 }
251
252 $sk->outputPage( $this );
253 # flush();
254 }
255
256 function out( $ins )
257 {
258 global $wgInputEncoding, $wgOutputEncoding, $wgLang;
259 if ( 0 == strcmp( $wgInputEncoding, $wgOutputEncoding ) ) {
260 $outs = $ins;
261 } else {
262 $outs = $wgLang->iconv( $wgInputEncoding, $wgOutputEncoding, $ins );
263 if ( false === $outs ) { $outs = $ins; }
264 }
265 print $outs;
266 }
267
268 function setEncodings()
269 {
270 global $wgInputEncoding, $wgOutputEncoding;
271 global $wgUser, $wgLang;
272
273 $wgInputEncoding = strtolower( $wgInputEncoding );
274
275 if( $wgUser->getOption( 'altencoding' ) ) {
276 $wgLang->setAltEncoding();
277 return;
278 }
279
280 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
281 $wgOutputEncoding = strtolower( $wgOutputEncoding );
282 return;
283 }
284
285 /*
286 # This code is unused anyway!
287 # Commenting out. --bv 2003-11-15
288
289 $a = explode( ",", $_SERVER['HTTP_ACCEPT_CHARSET'] );
290 $best = 0.0;
291 $bestset = "*";
292
293 foreach ( $a as $s ) {
294 if ( preg_match( "/(.*);q=(.*)/", $s, $m ) ) {
295 $set = $m[1];
296 $q = (float)($m[2]);
297 } else {
298 $set = $s;
299 $q = 1.0;
300 }
301 if ( $q > $best ) {
302 $bestset = $set;
303 $best = $q;
304 }
305 }
306 #if ( "*" == $bestset ) { $bestset = "iso-8859-1"; }
307 if ( "*" == $bestset ) { $bestset = $wgOutputEncoding; }
308 $wgOutputEncoding = strtolower( $bestset );
309
310 # Disable for now
311 #
312 */
313 $wgOutputEncoding = $wgInputEncoding;
314 }
315
316 # Returns a HTML comment with the elapsed time since request.
317 # This method has no side effects.
318 function reportTime()
319 {
320 global $wgRequestTime;
321
322 $now = wfTime();
323 list( $usec, $sec ) = explode( " ", $wgRequestTime );
324 $start = (float)$sec + (float)$usec;
325 $elapsed = $now - $start;
326 $com = sprintf( "<!-- Time since request: %01.2f secs. -->",
327 $elapsed );
328 return $com;
329 }
330
331 # Note: these arguments are keys into wfMsg(), not text!
332 #
333 function errorpage( $title, $msg )
334 {
335 global $wgTitle;
336
337 $this->mDebugtext .= "Original title: " .
338 $wgTitle->getPrefixedText() . "\n";
339 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
340 $this->setPageTitle( wfMsg( $title ) );
341 $this->setRobotpolicy( "noindex,nofollow" );
342 $this->setArticleRelated( false );
343
344 $this->mBodytext = "";
345 $this->addHTML( "<p>" . wfMsg( $msg ) . "\n" );
346 $this->returnToMain( false );
347
348 $this->output();
349 wfAbruptExit();
350 }
351
352 function sysopRequired()
353 {
354 global $wgUser;
355
356 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
357 $this->setPageTitle( wfMsg( "sysoptitle" ) );
358 $this->setRobotpolicy( "noindex,nofollow" );
359 $this->setArticleRelated( false );
360 $this->mBodytext = "";
361
362 $sk = $wgUser->getSkin();
363 $ap = $sk->makeKnownLink( wfMsg( "administrators" ), "" );
364 $this->addHTML( wfMsg( "sysoptext", $ap ) );
365 $this->returnToMain();
366 }
367
368 function developerRequired()
369 {
370 global $wgUser;
371
372 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
373 $this->setPageTitle( wfMsg( "developertitle" ) );
374 $this->setRobotpolicy( "noindex,nofollow" );
375 $this->setArticleRelated( false );
376 $this->mBodytext = "";
377
378 $sk = $wgUser->getSkin();
379 $ap = $sk->makeKnownLink( wfMsg( "administrators" ), "" );
380 $this->addHTML( wfMsg( "developertext", $ap ) );
381 $this->returnToMain();
382 }
383
384 function databaseError( $fname, &$conn )
385 {
386 global $wgUser, $wgCommandLineMode;
387
388 $this->setPageTitle( wfMsgNoDB( "databaseerror" ) );
389 $this->setRobotpolicy( "noindex,nofollow" );
390 $this->setArticleRelated( false );
391
392 if ( $wgCommandLineMode ) {
393 $msg = wfMsgNoDB( "dberrortextcl" );
394 } else {
395 $msg = wfMsgNoDB( "dberrortext" );
396 }
397
398 $msg = str_replace( "$1", htmlspecialchars( $conn->lastQuery() ), $msg );
399 $msg = str_replace( "$2", htmlspecialchars( $fname ), $msg );
400 $msg = str_replace( "$3", $conn->lastErrno(), $msg );
401 $msg = str_replace( "$4", htmlspecialchars( $conn->lastError() ), $msg );
402
403 if ( $wgCommandLineMode || !is_object( $wgUser )) {
404 print "$msg\n";
405 wfAbruptExit();
406 }
407 $sk = $wgUser->getSkin();
408 $shlink = $sk->makeKnownLink( wfMsgNoDB( "searchhelppage" ),
409 wfMsgNoDB( "searchingwikipedia" ) );
410 $msg = str_replace( "$5", $shlink, $msg );
411 $this->mBodytext = $msg;
412 $this->output();
413 wfAbruptExit();
414 }
415
416 function readOnlyPage( $source = "", $protected = false )
417 {
418 global $wgUser, $wgReadOnlyFile;
419
420 $this->setRobotpolicy( "noindex,nofollow" );
421 $this->setArticleRelated( false );
422
423 if( $protected ) {
424 $this->setPageTitle( wfMsg( "viewsource" ) );
425 $this->addWikiText( wfMsg( "protectedtext" ) );
426 } else {
427 $this->setPageTitle( wfMsg( "readonly" ) );
428 $reason = file_get_contents( $wgReadOnlyFile );
429 $this->addHTML( wfMsg( "readonlytext", $reason ) );
430 }
431
432 if($source) {
433 $rows = $wgUser->getOption( "rows" );
434 $cols = $wgUser->getOption( "cols" );
435 $text .= "</p>\n<textarea cols='$cols' rows='$rows' readonly>" .
436 htmlspecialchars( $source ) . "\n</textarea>";
437 $this->addHTML( $text );
438 }
439
440 $this->returnToMain( false );
441 }
442
443 function fatalError( $message )
444 {
445 $this->setPageTitle( wfMsg( "internalerror" ) );
446 $this->setRobotpolicy( "noindex,nofollow" );
447 $this->setArticleRelated( false );
448
449 $this->mBodytext = $message;
450 $this->output();
451 wfAbruptExit();
452 }
453
454 function unexpectedValueError( $name, $val )
455 {
456 $this->fatalError( wfMsg( "unexpected", $name, $val ) );
457 }
458
459 function fileCopyError( $old, $new )
460 {
461 $this->fatalError( wfMsg( "filecopyerror", $old, $new ) );
462 }
463
464 function fileRenameError( $old, $new )
465 {
466 $this->fatalError( wfMsg( "filerenameerror", $old, $new ) );
467 }
468
469 function fileDeleteError( $name )
470 {
471 $this->fatalError( wfMsg( "filedeleteerror", $name ) );
472 }
473
474 function fileNotFoundError( $name )
475 {
476 $this->fatalError( wfMsg( "filenotfound", $name ) );
477 }
478
479 function returnToMain( $auto = true )
480 {
481 global $wgUser, $wgOut, $returnto;
482
483 $sk = $wgUser->getSkin();
484 if ( "" == $returnto ) {
485 $returnto = wfMsg( "mainpage" );
486 }
487 $link = $sk->makeKnownLink( $returnto, "" );
488
489 $r = wfMsg( "returnto", $link );
490 if ( $auto ) {
491 $wgOut->addMeta( "http:Refresh", "10;url=" .
492 wfLocalUrlE( wfUrlencode( $returnto ) ) );
493 }
494 $wgOut->addHTML( "\n<p>$r\n" );
495 }
496
497 /* private */ function headElement()
498 {
499 global $wgDocType, $wgDTD, $wgUser, $wgLanguageCode, $wgOutputEncoding, $wgLang;
500
501 $ret = "<!DOCTYPE HTML PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
502
503 if ( "" == $this->mHTMLtitle ) {
504 $this->mHTMLtitle = $this->mPagetitle;
505 }
506 $rtl = $wgLang->isRTL() ? " dir='RTL'" : "";
507 $ret .= "<html lang=\"$wgLanguageCode\"$rtl><head><title>{$this->mHTMLtitle}</title>\n";
508 array_push( $this->mMetatags, array( "http:Content-type", "text/html; charset={$wgOutputEncoding}" ) );
509 foreach ( $this->mMetatags as $tag ) {
510 if ( 0 == strcasecmp( "http:", substr( $tag[0], 0, 5 ) ) ) {
511 $a = "http-equiv";
512 $tag[0] = substr( $tag[0], 5 );
513 } else {
514 $a = "name";
515 }
516 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\">\n";
517 }
518 $p = $this->mRobotpolicy;
519 if ( "" == $p ) { $p = "index,follow"; }
520 $ret .= "<meta name=\"robots\" content=\"$p\">\n";
521
522 if ( count( $this->mKeywords ) > 0 ) {
523 $ret .= "<meta name=\"keywords\" content=\"" .
524 implode( ",", $this->mKeywords ) . "\">\n";
525 }
526 foreach ( $this->mLinktags as $tag ) {
527 $ret .= "<link ";
528 if ( "" != $tag[0] ) { $ret .= "rel=\"{$tag[0]}\" "; }
529 if ( "" != $tag[1] ) { $ret .= "rev=\"{$tag[1]}\" "; }
530 $ret .= "href=\"{$tag[2]}\">\n";
531 }
532 $sk = $wgUser->getSkin();
533 $ret .= $sk->getHeadScripts();
534 $ret .= $sk->getUserStyles();
535
536 $ret .= "</head>\n";
537 return $ret;
538 }
539 }
540 ?>