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