typo and minor tweak to permanent redirect
[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, $responsecode = '302' ) { $this->mRedirect = $url; $this->mRedirectCode = $responsecode; }
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
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;
245 }
246 if( $this->mRdirectCode == '301') {
247 header("HTTP/1.1 {$this->mRedirectCode} Moved Permanently");
248 $this->mLastModified = gmdate( "D, j M Y H:i:s", wfTimestamp2Unix(
249 max( $timestamp, $wgUser->mTouched ) ) ) . " GMT";
250 }
251
252 $this->sendCacheControl();
253
254 header( "Location: {$this->mRedirect}" );
255 return;
256 }
257
258 $this->sendCacheControl();
259
260 header( "Content-type: text/html; charset={$wgOutputEncoding}" );
261 header( "Content-language: {$wgLanguageCode}" );
262
263 $exp = time() + $wgCookieExpiration;
264 foreach( $this->mCookies as $name => $val ) {
265 setcookie( $name, $val, $exp, "/" );
266 }
267
268 $sk->outputPage( $this );
269 # flush();
270 }
271
272 function out( $ins )
273 {
274 global $wgInputEncoding, $wgOutputEncoding, $wgLang;
275 if ( 0 == strcmp( $wgInputEncoding, $wgOutputEncoding ) ) {
276 $outs = $ins;
277 } else {
278 $outs = $wgLang->iconv( $wgInputEncoding, $wgOutputEncoding, $ins );
279 if ( false === $outs ) { $outs = $ins; }
280 }
281 print $outs;
282 }
283
284 function setEncodings()
285 {
286 global $wgInputEncoding, $wgOutputEncoding;
287 global $wgUser, $wgLang;
288
289 $wgInputEncoding = strtolower( $wgInputEncoding );
290
291 if( $wgUser->getOption( 'altencoding' ) ) {
292 $wgLang->setAltEncoding();
293 return;
294 }
295
296 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
297 $wgOutputEncoding = strtolower( $wgOutputEncoding );
298 return;
299 }
300
301 /*
302 # This code is unused anyway!
303 # Commenting out. --bv 2003-11-15
304
305 $a = explode( ",", $_SERVER['HTTP_ACCEPT_CHARSET'] );
306 $best = 0.0;
307 $bestset = "*";
308
309 foreach ( $a as $s ) {
310 if ( preg_match( "/(.*);q=(.*)/", $s, $m ) ) {
311 $set = $m[1];
312 $q = (float)($m[2]);
313 } else {
314 $set = $s;
315 $q = 1.0;
316 }
317 if ( $q > $best ) {
318 $bestset = $set;
319 $best = $q;
320 }
321 }
322 #if ( "*" == $bestset ) { $bestset = "iso-8859-1"; }
323 if ( "*" == $bestset ) { $bestset = $wgOutputEncoding; }
324 $wgOutputEncoding = strtolower( $bestset );
325
326 # Disable for now
327 #
328 */
329 $wgOutputEncoding = $wgInputEncoding;
330 }
331
332 # Returns a HTML comment with the elapsed time since request.
333 # This method has no side effects.
334 function reportTime()
335 {
336 global $wgRequestTime;
337
338 $now = wfTime();
339 list( $usec, $sec ) = explode( " ", $wgRequestTime );
340 $start = (float)$sec + (float)$usec;
341 $elapsed = $now - $start;
342 $com = sprintf( "<!-- Time since request: %01.2f secs. -->",
343 $elapsed );
344 return $com;
345 }
346
347 # Note: these arguments are keys into wfMsg(), not text!
348 #
349 function errorpage( $title, $msg )
350 {
351 global $wgTitle;
352
353 $this->mDebugtext .= "Original title: " .
354 $wgTitle->getPrefixedText() . "\n";
355 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
356 $this->setPageTitle( wfMsg( $title ) );
357 $this->setRobotpolicy( "noindex,nofollow" );
358 $this->setArticleRelated( false );
359
360 $this->mBodytext = "";
361 $this->addHTML( "<p>" . wfMsg( $msg ) . "\n" );
362 $this->returnToMain( false );
363
364 $this->output();
365 wfAbruptExit();
366 }
367
368 function sysopRequired()
369 {
370 global $wgUser;
371
372 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
373 $this->setPageTitle( wfMsg( "sysoptitle" ) );
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( "sysoptext", $ap ) );
381 $this->returnToMain();
382 }
383
384 function developerRequired()
385 {
386 global $wgUser;
387
388 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
389 $this->setPageTitle( wfMsg( "developertitle" ) );
390 $this->setRobotpolicy( "noindex,nofollow" );
391 $this->setArticleRelated( false );
392 $this->mBodytext = "";
393
394 $sk = $wgUser->getSkin();
395 $ap = $sk->makeKnownLink( wfMsg( "administrators" ), "" );
396 $this->addHTML( wfMsg( "developertext", $ap ) );
397 $this->returnToMain();
398 }
399
400 function databaseError( $fname, &$conn )
401 {
402 global $wgUser, $wgCommandLineMode;
403
404 $this->setPageTitle( wfMsgNoDB( "databaseerror" ) );
405 $this->setRobotpolicy( "noindex,nofollow" );
406 $this->setArticleRelated( false );
407
408 if ( $wgCommandLineMode ) {
409 $msg = wfMsgNoDB( "dberrortextcl" );
410 } else {
411 $msg = wfMsgNoDB( "dberrortext" );
412 }
413
414 $msg = str_replace( "$1", htmlspecialchars( $conn->lastQuery() ), $msg );
415 $msg = str_replace( "$2", htmlspecialchars( $fname ), $msg );
416 $msg = str_replace( "$3", $conn->lastErrno(), $msg );
417 $msg = str_replace( "$4", htmlspecialchars( $conn->lastError() ), $msg );
418
419 if ( $wgCommandLineMode || !is_object( $wgUser )) {
420 print "$msg\n";
421 wfAbruptExit();
422 }
423 $sk = $wgUser->getSkin();
424 $shlink = $sk->makeKnownLink( wfMsgNoDB( "searchhelppage" ),
425 wfMsgNoDB( "searchingwikipedia" ) );
426 $msg = str_replace( "$5", $shlink, $msg );
427 $this->mBodytext = $msg;
428 $this->output();
429 wfAbruptExit();
430 }
431
432 function readOnlyPage( $source = "", $protected = false )
433 {
434 global $wgUser, $wgReadOnlyFile;
435
436 $this->setRobotpolicy( "noindex,nofollow" );
437 $this->setArticleRelated( false );
438
439 if( $protected ) {
440 $this->setPageTitle( wfMsg( "viewsource" ) );
441 $this->addWikiText( wfMsg( "protectedtext" ) );
442 } else {
443 $this->setPageTitle( wfMsg( "readonly" ) );
444 $reason = file_get_contents( $wgReadOnlyFile );
445 $this->addHTML( wfMsg( "readonlytext", $reason ) );
446 }
447
448 if($source) {
449 $rows = $wgUser->getOption( "rows" );
450 $cols = $wgUser->getOption( "cols" );
451 $text .= "</p>\n<textarea cols='$cols' rows='$rows' readonly>" .
452 htmlspecialchars( $source ) . "\n</textarea>";
453 $this->addHTML( $text );
454 }
455
456 $this->returnToMain( false );
457 }
458
459 function fatalError( $message )
460 {
461 $this->setPageTitle( wfMsg( "internalerror" ) );
462 $this->setRobotpolicy( "noindex,nofollow" );
463 $this->setArticleRelated( false );
464
465 $this->mBodytext = $message;
466 $this->output();
467 wfAbruptExit();
468 }
469
470 function unexpectedValueError( $name, $val )
471 {
472 $this->fatalError( wfMsg( "unexpected", $name, $val ) );
473 }
474
475 function fileCopyError( $old, $new )
476 {
477 $this->fatalError( wfMsg( "filecopyerror", $old, $new ) );
478 }
479
480 function fileRenameError( $old, $new )
481 {
482 $this->fatalError( wfMsg( "filerenameerror", $old, $new ) );
483 }
484
485 function fileDeleteError( $name )
486 {
487 $this->fatalError( wfMsg( "filedeleteerror", $name ) );
488 }
489
490 function fileNotFoundError( $name )
491 {
492 $this->fatalError( wfMsg( "filenotfound", $name ) );
493 }
494
495 function returnToMain( $auto = true )
496 {
497 global $wgUser, $wgOut, $returnto;
498
499 $sk = $wgUser->getSkin();
500 if ( "" == $returnto ) {
501 $returnto = wfMsg( "mainpage" );
502 }
503 $link = $sk->makeKnownLink( $returnto, "" );
504
505 $r = wfMsg( "returnto", $link );
506 if ( $auto ) {
507 $wgOut->addMeta( "http:Refresh", "10;url=" .
508 wfLocalUrlE( wfUrlencode( $returnto ) ) );
509 }
510 $wgOut->addHTML( "\n<p>$r\n" );
511 }
512
513 /* private */ function headElement()
514 {
515 global $wgDocType, $wgDTD, $wgUser, $wgLanguageCode, $wgOutputEncoding, $wgLang;
516
517 $ret = "<!DOCTYPE HTML PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
518
519 if ( "" == $this->mHTMLtitle ) {
520 $this->mHTMLtitle = $this->mPagetitle;
521 }
522 $rtl = $wgLang->isRTL() ? " dir='RTL'" : "";
523 $ret .= "<html lang=\"$wgLanguageCode\"$rtl><head><title>{$this->mHTMLtitle}</title>\n";
524 array_push( $this->mMetatags, array( "http:Content-type", "text/html; charset={$wgOutputEncoding}" ) );
525 foreach ( $this->mMetatags as $tag ) {
526 if ( 0 == strcasecmp( "http:", substr( $tag[0], 0, 5 ) ) ) {
527 $a = "http-equiv";
528 $tag[0] = substr( $tag[0], 5 );
529 } else {
530 $a = "name";
531 }
532 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\">\n";
533 }
534 $p = $this->mRobotpolicy;
535 if ( "" == $p ) { $p = "index,follow"; }
536 $ret .= "<meta name=\"robots\" content=\"$p\">\n";
537
538 if ( count( $this->mKeywords ) > 0 ) {
539 $ret .= "<meta name=\"keywords\" content=\"" .
540 implode( ",", $this->mKeywords ) . "\">\n";
541 }
542 foreach ( $this->mLinktags as $tag ) {
543 $ret .= "<link ";
544 if ( "" != $tag[0] ) { $ret .= "rel=\"{$tag[0]}\" "; }
545 if ( "" != $tag[1] ) { $ret .= "rev=\"{$tag[1]}\" "; }
546 $ret .= "href=\"{$tag[2]}\">\n";
547 }
548 $sk = $wgUser->getSkin();
549 $ret .= $sk->getHeadScripts();
550 $ret .= $sk->getUserStyles();
551
552 $ret .= "</head>\n";
553 return $ret;
554 }
555 }
556 ?>