allow external clearing of mBodytext, without destroying OutputPage object,
[lhc/web/wiklou.git] / includes / OutputPage.php
1 <?php
2 /**
3 * $Id$
4 * @package MediaWiki
5 */
6
7 /**
8 * This is not a valid entry point, perform no further processing unless MEDIAWIKI is defined
9 */
10 if( defined( 'MEDIAWIKI' ) ) {
11
12 # See design.doc
13
14 if($wgUseTeX) require_once( 'Math.php' );
15
16 define( 'RLH_FOR_UPDATE', 1 );
17
18 /**
19 * @todo document
20 * @package MediaWiki
21 */
22 class OutputPage {
23 var $mHeaders, $mCookies, $mMetatags, $mKeywords;
24 var $mLinktags, $mPagetitle, $mBodytext, $mDebugtext;
25 var $mHTMLtitle, $mRobotpolicy, $mIsarticle, $mPrintable;
26 var $mSubtitle, $mRedirect;
27 var $mLastModified, $mCategoryLinks;
28 var $mScripts, $mLinkColours;
29
30 var $mSuppressQuickbar;
31 var $mOnloadHandler;
32 var $mDoNothing;
33 var $mContainsOldMagic, $mContainsNewMagic;
34 var $mIsArticleRelated;
35 var $mParserOptions;
36 var $mShowFeedLinks = false;
37 var $mEnableClientCache = true;
38
39 function OutputPage()
40 {
41 $this->mHeaders = $this->mCookies = $this->mMetatags =
42 $this->mKeywords = $this->mLinktags = array();
43 $this->mHTMLtitle = $this->mPagetitle = $this->mBodytext =
44 $this->mRedirect = $this->mLastModified =
45 $this->mSubtitle = $this->mDebugtext = $this->mRobotpolicy =
46 $this->mOnloadHandler = "";
47 $this->mIsArticleRelated = $this->mIsarticle = $this->mPrintable = true;
48 $this->mSuppressQuickbar = $this->mPrintable = false;
49 $this->mLanguageLinks = array();
50 $this->mCategoryLinks = array() ;
51 $this->mDoNothing = false;
52 $this->mContainsOldMagic = $this->mContainsNewMagic = 0;
53 $this->mParserOptions = ParserOptions::newFromUser( $temp = NULL );
54 $this->mSquidMaxage = 0;
55 $this->mScripts = "";
56 }
57
58 function addHeader( $name, $val ) { array_push( $this->mHeaders, "$name: $val" ) ; }
59 function addCookie( $name, $val ) { array_push( $this->mCookies, array( $name, $val ) ); }
60 function redirect( $url, $responsecode = '302' ) { $this->mRedirect = $url; $this->mRedirectCode = $responsecode; }
61
62 # To add an http-equiv meta tag, precede the name with "http:"
63 function addMeta( $name, $val ) { array_push( $this->mMetatags, array( $name, $val ) ); }
64 function addKeyword( $text ) { array_push( $this->mKeywords, $text ); }
65 function addScript( $script ) { $this->mScripts .= $script; }
66 function getScript() { return $this->mScripts; }
67
68 function addLink( $linkarr ) {
69 # $linkarr should be an associative array of attributes. We'll escape on output.
70 array_push( $this->mLinktags, $linkarr );
71 }
72
73 function addMetadataLink( $linkarr ) {
74 # note: buggy CC software only reads first "meta" link
75 static $haveMeta = false;
76 $linkarr['rel'] = ($haveMeta) ? 'alternate meta' : 'meta';
77 $this->addLink( $linkarr );
78 $haveMeta = true;
79 }
80
81 /**
82 * checkLastModified tells the client to use the client-cached page if
83 * possible. If sucessful, the OutputPage is disabled so that
84 * any future call to OutputPage->output() have no effect. The method
85 * returns true iff cache-ok headers was sent.
86 */
87 function checkLastModified ( $timestamp )
88 {
89 global $wgLang, $wgCachePages, $wgUser;
90 $timestamp=wfTimestamp(TS_MW,$timestamp);
91 if( !$wgCachePages ) {
92 wfDebug( "CACHE DISABLED\n", false );
93 return;
94 }
95 if( preg_match( '/MSIE ([1-4]|5\.0)/', $_SERVER["HTTP_USER_AGENT"] ) ) {
96 # IE 5.0 has probs with our caching
97 wfDebug( "-- bad client, not caching\n", false );
98 return;
99 }
100 if( $wgUser->getOption( 'nocache' ) ) {
101 wfDebug( "USER DISABLED CACHE\n", false );
102 return;
103 }
104
105 $lastmod = gmdate( "D, j M Y H:i:s", wfTimestamp(TS_UNIX, max( $timestamp, $wgUser->mTouched ) ) ) . " GMT";
106
107 if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
108 # IE sends sizes after the date like this:
109 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
110 # this breaks strtotime().
111 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
112 $ismodsince = wfTimestamp( TS_MW, strtotime( $modsince ) );
113 wfDebug( "-- client send If-Modified-Since: " . $modsince . "\n", false );
114 wfDebug( "-- we might send Last-Modified : $lastmod\n", false );
115 if( ($ismodsince >= $timestamp ) and $wgUser->validateCache( $ismodsince ) ) {
116 # Make sure you're in a place you can leave when you call us!
117 header( "HTTP/1.0 304 Not Modified" );
118 $this->mLastModified = $lastmod;
119 $this->sendCacheControl();
120 wfDebug( "CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
121 $this->disable();
122 return true;
123 } else {
124 wfDebug( "READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
125 $this->mLastModified = $lastmod;
126 }
127 } else {
128 wfDebug( "We're confused.\n", false );
129 $this->mLastModified = $lastmod;
130 }
131 }
132
133 function getPageTitleActionText () {
134 global $action;
135 switch($action) {
136 case 'edit':
137 return wfMsg('edit');
138 case 'history':
139 return wfMsg('history_short');
140 case 'protect':
141 return wfMsg('protect');
142 case 'unprotect':
143 return wfMsg('unprotect');
144 case 'delete':
145 return wfMsg('delete');
146 case 'watch':
147 return wfMsg('watch');
148 case 'unwatch':
149 return wfMsg('unwatch');
150 case 'submit':
151 return wfMsg('preview');
152 case 'info':
153 return wfMsg('info_short');
154 default:
155 return '';
156 }
157 }
158 function setRobotpolicy( $str ) { $this->mRobotpolicy = $str; }
159 function setHTMLTitle( $name ) {$this->mHTMLtitle = $name; }
160 function setPageTitle( $name ) {
161 global $action;
162 $this->mPagetitle = $name;
163 if(!empty($action)) {
164 $taction = $this->getPageTitleActionText();
165 if( !empty( $taction ) ) {
166 $name .= ' - '.$taction;
167 }
168 }
169 $this->setHTMLTitle( $name . ' - ' . wfMsg( 'wikititlesuffix' ) );
170 }
171 function getHTMLTitle() { return $this->mHTMLtitle; }
172 function getPageTitle() { return $this->mPagetitle; }
173 function setSubtitle( $str ) { $this->mSubtitle = $str; }
174 function getSubtitle() { return $this->mSubtitle; }
175 function isArticle() { return $this->mIsarticle; }
176 function setPrintable() { $this->mPrintable = true; }
177 function isPrintable() { return $this->mPrintable; }
178 function setSyndicated( $show = true ) { $this->mShowFeedLinks = $show; }
179 function isSyndicated() { return $this->mShowFeedLinks; }
180 function setOnloadHandler( $js ) { $this->mOnloadHandler = $js; }
181 function getOnloadHandler() { return $this->mOnloadHandler; }
182 function disable() { $this->mDoNothing = true; }
183
184 function setArticleRelated( $v )
185 {
186 $this->mIsArticleRelated = $v;
187 if ( !$v ) {
188 $this->mIsarticle = false;
189 }
190 }
191 function setArticleFlag( $v ) {
192 $this->mIsarticle = $v;
193 if ( $v ) {
194 $this->mIsArticleRelated = $v;
195 }
196 }
197
198 function isArticleRelated()
199 {
200 return $this->mIsArticleRelated;
201 }
202
203 function getLanguageLinks() {
204 return $this->mLanguageLinks;
205 }
206 function addLanguageLinks($newLinkArray) {
207 $this->mLanguageLinks += $newLinkArray;
208 }
209 function setLanguageLinks($newLinkArray) {
210 $this->mLanguageLinks = $newLinkArray;
211 }
212 function getCategoryLinks() {
213 return $this->mCategoryLinks;
214 }
215 function addCategoryLinks($newLinkArray) {
216 $this->mCategoryLinks += $newLinkArray;
217 }
218 function setCategoryLinks($newLinkArray) {
219 $this->mCategoryLinks += $newLinkArray;
220 }
221
222 function suppressQuickbar() { $this->mSuppressQuickbar = true; }
223 function isQuickbarSuppressed() { return $this->mSuppressQuickbar; }
224
225 function addHTML( $text ) { $this->mBodytext .= $text; }
226 function clearHTML() { $this->mBodytext = ''; }
227 function debug( $text ) { $this->mDebugtext .= $text; }
228
229 function setParserOptions( $options )
230 {
231 return wfSetVar( $this->mParserOptions, $options );
232 }
233
234 /**
235 * Convert wikitext to HTML and add it to the buffer
236 */
237 function addWikiText( $text, $linestart = true )
238 {
239 global $wgParser, $wgTitle;
240
241 $parserOutput = $wgParser->parse( $text, $wgTitle, $this->mParserOptions, $linestart );
242 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
243 $this->mCategoryLinks += $parserOutput->getCategoryLinks();
244 $this->addHTML( $parserOutput->getText() );
245 }
246
247 /**
248 * Add wikitext to the buffer, assuming that this is the primary text for a page view
249 * Saves the text into the parser cache if possible
250 */
251 function addPrimaryWikiText( $text, $cacheArticle ) {
252 global $wgParser, $wgParserCache, $wgUser, $wgTitle;
253
254 $parserOutput = $wgParser->parse( $text, $wgTitle, $this->mParserOptions, true );
255
256 # Replace link holders
257 $text = $parserOutput->getText();
258 $this->replaceLinkHolders( $text );
259 $parserOutput->setText( $text );
260
261 if ( $cacheArticle ) {
262 $wgParserCache->save( $parserOutput, $cacheArticle, $wgUser );
263 }
264
265 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
266 $this->mCategoryLinks += $parserOutput->getCategoryLinks();
267 $this->addHTML( $text );
268 }
269
270 function tryParserCache( $article, $user ) {
271 global $wgParserCache;
272 $parserOutput = $wgParserCache->get( $article, $user );
273 if ( $parserOutput !== false ) {
274 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
275 $this->mCategoryLinks += $parserOutput->getCategoryLinks();
276 $this->addHTML( $parserOutput->getText() );
277 return true;
278 } else {
279 return false;
280 }
281 }
282
283 /**
284 * Set the maximum cache time on the Squid in seconds
285 */
286 function setSquidMaxage( $maxage ) {
287 $this->mSquidMaxage = $maxage;
288 }
289
290 /**
291 * Use enableClientCache(false) to force it to send nocache headers
292 */
293 function enableClientCache( $state ) {
294 return wfSetVar( $this->mEnableClientCache, $state );
295 }
296
297 function sendCacheControl() {
298 global $wgUseSquid, $wgUseESI;
299 # FIXME: This header may cause trouble with some versions of Internet Explorer
300 header( 'Vary: Accept-Encoding, Cookie' );
301 if( $this->mEnableClientCache ) {
302 if( $wgUseSquid && ! isset( $_COOKIE[ini_get( 'session.name') ] ) &&
303 ! $this->isPrintable() && $this->mSquidMaxage != 0 )
304 {
305 if ( $wgUseESI ) {
306 # We'll purge the proxy cache explicitly, but require end user agents
307 # to revalidate against the proxy on each visit.
308 # Surrogate-Control controls our Squid, Cache-Control downstream caches
309 wfDebug( "** proxy caching with ESI; {$this->mLastModified} **\n", false );
310 # start with a shorter timeout for initial testing
311 # header( 'Surrogate-Control: max-age=2678400+2678400, content="ESI/1.0"');
312 header( 'Surrogate-Control: max-age='.$wgSquidMaxage.'+'.$this->mSquidMaxage.', content="ESI/1.0"');
313 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
314 } else {
315 # We'll purge the proxy cache for anons explicitly, but require end user agents
316 # to revalidate against the proxy on each visit.
317 # IMPORTANT! The Squid needs to replace the Cache-Control header with
318 # Cache-Control: s-maxage=0, must-revalidate, max-age=0
319 wfDebug( "** local proxy caching; {$this->mLastModified} **\n", false );
320 # start with a shorter timeout for initial testing
321 # header( "Cache-Control: s-maxage=2678400, must-revalidate, max-age=0" );
322 header( 'Cache-Control: s-maxage='.$this->mSquidMaxage.', must-revalidate, max-age=0' );
323 }
324 } else {
325 # We do want clients to cache if they can, but they *must* check for updates
326 # on revisiting the page.
327 wfDebug( "** private caching; {$this->mLastModified} **\n", false );
328 header( "Expires: -1" );
329 header( "Cache-Control: private, must-revalidate, max-age=0" );
330 }
331 if($this->mLastModified) header( "Last-modified: {$this->mLastModified}" );
332 } else {
333 wfDebug( "** no caching **\n", false );
334
335 # In general, the absence of a last modified header should be enough to prevent
336 # the client from using its cache. We send a few other things just to make sure.
337 header( 'Expires: -1' );
338 header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
339 header( 'Pragma: no-cache' );
340 }
341 }
342
343 /**
344 * Finally, all the text has been munged and accumulated into
345 * the object, let's actually output it:
346 */
347 function output()
348 {
349 global $wgUser, $wgLang, $wgDebugComments, $wgCookieExpiration;
350 global $wgInputEncoding, $wgOutputEncoding, $wgLanguageCode;
351 global $wgDebugRedirects, $wgMimeType, $wgProfiler;
352 if( $this->mDoNothing ){
353 return;
354 }
355 $fname = 'OutputPage::output';
356 wfProfileIn( $fname );
357
358 $sk = $wgUser->getSkin();
359
360 if ( '' != $this->mRedirect ) {
361 if( substr( $this->mRedirect, 0, 4 ) != 'http' ) {
362 # Standards require redirect URLs to be absolute
363 global $wgServer;
364 $this->mRedirect = $wgServer . $this->mRedirect;
365 }
366 if( $this->mRedirectCode == '301') {
367 if( !$wgDebugRedirects ) {
368 header("HTTP/1.1 {$this->mRedirectCode} Moved Permanently");
369 }
370 $this->mLastModified = gmdate( 'D, j M Y H:i:s' ) . ' GMT';
371 }
372
373 $this->sendCacheControl();
374
375 if( $wgDebugRedirects ) {
376 $url = htmlspecialchars( $this->mRedirect );
377 print "<html>\n<head>\n<title>Redirect</title>\n</head>\n<body>\n";
378 print "<p>Location: <a href=\"$url\">$url</a></p>\n";
379 print "</body>\n</html>\n";
380 } else {
381 header( 'Location: '.$this->mRedirect );
382 }
383 if ( isset( $wgProfiler ) ) { wfDebug( $wgProfiler->getOutput() ); }
384 return;
385 }
386
387
388 $this->sendCacheControl();
389 # Perform link colouring
390 $this->transformBuffer();
391 $this->replaceLinkHolders( $this->mSubtitle );
392
393 # Disable temporary placeholders, so that the skin produces HTML
394 $sk->postParseLinkColour( false );
395
396 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
397 header( 'Content-language: '.$wgLanguageCode );
398
399 $exp = time() + $wgCookieExpiration;
400 foreach( $this->mCookies as $name => $val ) {
401 setcookie( $name, $val, $exp, '/' );
402 }
403
404 $sk->outputPage( $this );
405 # flush();
406 }
407
408 function out( $ins ) {
409 global $wgInputEncoding, $wgOutputEncoding, $wgLang;
410 if ( 0 == strcmp( $wgInputEncoding, $wgOutputEncoding ) ) {
411 $outs = $ins;
412 } else {
413 $outs = $wgLang->iconv( $wgInputEncoding, $wgOutputEncoding, $ins );
414 if ( false === $outs ) { $outs = $ins; }
415 }
416 print $outs;
417 }
418
419 function setEncodings() {
420 global $wgInputEncoding, $wgOutputEncoding;
421 global $wgUser, $wgLang;
422
423 $wgInputEncoding = strtolower( $wgInputEncoding );
424
425 if( $wgUser->getOption( 'altencoding' ) ) {
426 $wgLang->setAltEncoding();
427 return;
428 }
429
430 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
431 $wgOutputEncoding = strtolower( $wgOutputEncoding );
432 return;
433 }
434
435 /*
436 # This code is unused anyway!
437 # Commenting out. --bv 2003-11-15
438
439 $a = explode( ",", $_SERVER['HTTP_ACCEPT_CHARSET'] );
440 $best = 0.0;
441 $bestset = "*";
442
443 foreach ( $a as $s ) {
444 if ( preg_match( "/(.*);q=(.*)/", $s, $m ) ) {
445 $set = $m[1];
446 $q = (float)($m[2]);
447 } else {
448 $set = $s;
449 $q = 1.0;
450 }
451 if ( $q > $best ) {
452 $bestset = $set;
453 $best = $q;
454 }
455 }
456 #if ( "*" == $bestset ) { $bestset = "iso-8859-1"; }
457 if ( "*" == $bestset ) { $bestset = $wgOutputEncoding; }
458 $wgOutputEncoding = strtolower( $bestset );
459
460 # Disable for now
461 #
462 */
463 $wgOutputEncoding = $wgInputEncoding;
464 }
465
466 /**
467 * Returns a HTML comment with the elapsed time since request.
468 * This method has no side effects.
469 */
470 function reportTime() {
471 global $wgRequestTime;
472
473 $now = wfTime();
474 list( $usec, $sec ) = explode( ' ', $wgRequestTime );
475 $start = (float)$sec + (float)$usec;
476 $elapsed = $now - $start;
477
478 # Use real server name if available, so we know which machine
479 # in a server farm generated the current page.
480 if ( function_exists( 'posix_uname' ) ) {
481 $uname = @posix_uname();
482 } else {
483 $uname = false;
484 }
485 if( is_array( $uname ) && isset( $uname['nodename'] ) ) {
486 $hostname = $uname['nodename'];
487 } else {
488 # This may be a virtual server.
489 $hostname = $_SERVER['SERVER_NAME'];
490 }
491 $com = sprintf( "<!-- Served by %s in %01.2f secs. -->",
492 $hostname, $elapsed );
493 return $com;
494 }
495
496 /**
497 * Note: these arguments are keys into wfMsg(), not text!
498 */
499 function errorpage( $title, $msg ) {
500 global $wgTitle;
501
502 $this->mDebugtext .= 'Original title: ' .
503 $wgTitle->getPrefixedText() . "\n";
504 $this->setPageTitle( wfMsg( $title ) );
505 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
506 $this->setRobotpolicy( 'noindex,nofollow' );
507 $this->setArticleRelated( false );
508 $this->enableClientCache( false );
509 $this->mRedirect = '';
510
511 $this->mBodytext = '';
512 $this->addHTML( '<p>' . wfMsg( $msg ) . "</p>\n" );
513 $this->returnToMain( false );
514
515 $this->output();
516 wfErrorExit();
517 }
518
519 function sysopRequired() {
520 global $wgUser;
521
522 $this->setPageTitle( wfMsg( 'sysoptitle' ) );
523 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
524 $this->setRobotpolicy( 'noindex,nofollow' );
525 $this->setArticleRelated( false );
526 $this->mBodytext = '';
527
528 $sk = $wgUser->getSkin();
529 $ap = $sk->makeKnownLink( wfMsg( 'administrators' ), '' );
530 $this->addHTML( wfMsg( 'sysoptext', $ap ) );
531 $this->returnToMain();
532 }
533
534 function developerRequired() {
535 global $wgUser;
536
537 $this->setPageTitle( wfMsg( 'developertitle' ) );
538 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
539 $this->setRobotpolicy( 'noindex,nofollow' );
540 $this->setArticleRelated( false );
541 $this->mBodytext = '';
542
543 $sk = $wgUser->getSkin();
544 $ap = $sk->makeKnownLink( wfMsg( 'administrators' ), '' );
545 $this->addHTML( wfMsg( 'developertext', $ap ) );
546 $this->returnToMain();
547 }
548
549 function loginToUse() {
550 global $wgUser, $wgTitle, $wgLang;
551
552 $this->setPageTitle( wfMsg( 'loginreqtitle' ) );
553 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
554 $this->setRobotpolicy( 'noindex,nofollow' );
555 $this->setArticleFlag( false );
556 $this->mBodytext = '';
557 $this->addWikiText( wfMsg( 'loginreqtext' ) );
558
559 # We put a comment in the .html file so a Sysop can diagnose the page the
560 # user can't see.
561 $this->addHTML( "\n<!--" .
562 $wgLang->getNsText( $wgTitle->getNamespace() ) .
563 ':' .
564 $wgTitle->getDBkey() . '-->' );
565 $this->returnToMain(); # Flip back to the main page after 10 seconds.
566 }
567
568 function databaseError( $fname, $sql, $error, $errno ) {
569 global $wgUser, $wgCommandLineMode;
570
571 $this->setPageTitle( wfMsgNoDB( 'databaseerror' ) );
572 $this->setRobotpolicy( 'noindex,nofollow' );
573 $this->setArticleRelated( false );
574 $this->enableClientCache( false );
575 $this->mRedirect = '';
576
577 if ( $wgCommandLineMode ) {
578 $msg = wfMsgNoDB( 'dberrortextcl', htmlspecialchars( $sql ),
579 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
580 } else {
581 $msg = wfMsgNoDB( 'dberrortext', htmlspecialchars( $sql ),
582 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
583 }
584
585 if ( $wgCommandLineMode || !is_object( $wgUser )) {
586 print $msg."\n";
587 wfErrorExit();
588 }
589 $this->mBodytext = $msg;
590 $this->output();
591 wfErrorExit();
592 }
593
594 function readOnlyPage( $source = null, $protected = false ) {
595 global $wgUser, $wgReadOnlyFile;
596
597 $this->setRobotpolicy( 'noindex,nofollow' );
598 $this->setArticleRelated( false );
599
600 if( $protected ) {
601 $this->setPageTitle( wfMsg( 'viewsource' ) );
602 $this->addWikiText( wfMsg( 'protectedtext' ) );
603 } else {
604 $this->setPageTitle( wfMsg( 'readonly' ) );
605 $reason = file_get_contents( $wgReadOnlyFile );
606 $this->addWikiText( wfMsg( 'readonlytext', $reason ) );
607 }
608
609 if( is_string( $source ) ) {
610 if( strcmp( $source, '' ) == 0 ) {
611 $source = wfMsg( 'noarticletext' );
612 }
613 $rows = $wgUser->getOption( 'rows' );
614 $cols = $wgUser->getOption( 'cols' );
615 $text = "\n<textarea cols='$cols' rows='$rows' readonly='readonly'>" .
616 htmlspecialchars( $source ) . "\n</textarea>";
617 $this->addHTML( $text );
618 }
619
620 $this->returnToMain( false );
621 }
622
623 function fatalError( $message ) {
624 $this->setPageTitle( wfMsg( "internalerror" ) );
625 $this->setRobotpolicy( "noindex,nofollow" );
626 $this->setArticleRelated( false );
627 $this->enableClientCache( false );
628 $this->mRedirect = '';
629
630 $this->mBodytext = $message;
631 $this->output();
632 wfErrorExit();
633 }
634
635 function unexpectedValueError( $name, $val ) {
636 $this->fatalError( wfMsg( 'unexpected', $name, $val ) );
637 }
638
639 function fileCopyError( $old, $new ) {
640 $this->fatalError( wfMsg( 'filecopyerror', $old, $new ) );
641 }
642
643 function fileRenameError( $old, $new ) {
644 $this->fatalError( wfMsg( 'filerenameerror', $old, $new ) );
645 }
646
647 function fileDeleteError( $name ) {
648 $this->fatalError( wfMsg( 'filedeleteerror', $name ) );
649 }
650
651 function fileNotFoundError( $name ) {
652 $this->fatalError( wfMsg( 'filenotfound', $name ) );
653 }
654
655 /**
656 * return from error messages or notes
657 * @param $auto automatically redirect the user after 10 seconds
658 * @param $returnto page title to return to. Default is Main Page.
659 */
660 function returnToMain( $auto = true, $returnto = NULL ) {
661 global $wgUser, $wgOut, $wgRequest;
662
663 if ( $returnto == NULL ) {
664 $returnto = $wgRequest->getText( 'returnto' );
665 }
666
667 $sk = $wgUser->getSkin();
668 if ( '' == $returnto ) {
669 $returnto = wfMsg( 'mainpage' );
670 }
671 $link = $sk->makeKnownLink( $returnto, '' );
672
673 $r = wfMsg( 'returnto', $link );
674 if ( $auto ) {
675 $titleObj = Title::newFromText( $returnto );
676 $wgOut->addMeta( 'http:Refresh', '10;url=' . $titleObj->escapeFullURL() );
677 }
678 $wgOut->addHTML( "\n<p>$r</p>\n" );
679 }
680
681 /**
682 * This function takes the existing and broken links for the page
683 * and uses the first 10 of them for META keywords
684 */
685 function addMetaTags () {
686 global $wgLinkCache , $wgOut ;
687 $good = array_keys ( $wgLinkCache->mGoodLinks ) ;
688 $bad = array_keys ( $wgLinkCache->mBadLinks ) ;
689 $a = array_merge ( $good , $bad ) ;
690 $a = array_slice ( $a , 0 , 10 ) ; # 10 keywords max
691 $a = implode ( ',' , $a ) ;
692 $strip = array(
693 "/<.*?" . ">/" => '',
694 "/[_]/" => ' '
695 );
696 $a = htmlspecialchars(preg_replace(array_keys($strip), array_values($strip),$a ));
697
698 $wgOut->addMeta ( 'KEYWORDS' , $a ) ;
699 }
700
701 /**
702 * @private
703 */
704 function headElement() {
705 global $wgDocType, $wgDTD, $wgLanguageCode, $wgOutputEncoding, $wgMimeType;
706 global $wgUser, $wgLang, $wgRequest;
707
708 $xml = ($wgMimeType == 'text/xml');
709 if( $xml ) {
710 $ret = "<" . "?xml version=\"1.0\" encoding=\"$wgOutputEncoding\" ?" . ">\n";
711 } else {
712 $ret = '';
713 }
714
715 $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
716
717 if ( "" == $this->mHTMLtitle ) {
718 $this->mHTMLtitle = wfMsg( "pagetitle", $this->mPagetitle );
719 }
720 if( $xml ) {
721 $xmlbits = "xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\"";
722 } else {
723 $xmlbits = '';
724 }
725 $rtl = $wgLang->isRTL() ? " dir='RTL'" : '';
726 $ret .= "<html $xmlbits lang=\"$wgLanguageCode\" $rtl>\n";
727 $ret .= "<head>\n<title>" . htmlspecialchars( $this->mHTMLtitle ) . "</title>\n";
728 array_push( $this->mMetatags, array( "http:Content-type", "$wgMimeType; charset={$wgOutputEncoding}" ) );
729
730 $ret .= $this->getHeadLinks();
731 global $wgStylePath;
732 if( $this->isPrintable() ) {
733 $media = '';
734 } else {
735 $media = "media='print'";
736 }
737 $printsheet = htmlspecialchars( "$wgStylePath/common/wikiprintable.css" );
738 $ret .= "<link rel='stylesheet' type='text/css' $media href='$printsheet' />\n";
739
740 $sk = $wgUser->getSkin();
741 $ret .= $sk->getHeadScripts();
742 $ret .= $this->mScripts;
743 $ret .= $sk->getUserStyles();
744
745 $ret .= "</head>\n";
746 return $ret;
747 }
748
749 function getHeadLinks() {
750 global $wgRequest, $wgStylePath;
751 $ret = '';
752 foreach ( $this->mMetatags as $tag ) {
753 if ( 0 == strcasecmp( 'http:', substr( $tag[0], 0, 5 ) ) ) {
754 $a = 'http-equiv';
755 $tag[0] = substr( $tag[0], 5 );
756 } else {
757 $a = 'name';
758 }
759 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\" />\n";
760 }
761 $p = $this->mRobotpolicy;
762 if ( '' == $p ) { $p = 'index,follow'; }
763 $ret .= "<meta name=\"robots\" content=\"$p\" />\n";
764
765 if ( count( $this->mKeywords ) > 0 ) {
766 $strip = array(
767 "/<.*?" . ">/" => '',
768 "/[_]/" => ' '
769 );
770 $ret .= "<meta name=\"keywords\" content=\"" .
771 htmlspecialchars(preg_replace(array_keys($strip), array_values($strip),implode( ",", $this->mKeywords ))) . "\" />\n";
772 }
773 foreach ( $this->mLinktags as $tag ) {
774 $ret .= '<link';
775 foreach( $tag as $attr => $val ) {
776 $ret .= " $attr=\"" . htmlspecialchars( $val ) . "\"";
777 }
778 $ret .= " />\n";
779 }
780 if( $this->isSyndicated() ) {
781 # FIXME: centralize the mime-type and name information in Feed.php
782 $link = $wgRequest->escapeAppendQuery( 'feed=rss' );
783 $ret .= "<link rel='alternate' type='application/rss+xml' title='RSS 2.0' href='$link' />\n";
784 $link = $wgRequest->escapeAppendQuery( 'feed=atom' );
785 $ret .= "<link rel='alternate' type='application/rss+atom' title='Atom 0.3' href='$link' />\n";
786 }
787 # FIXME: get these working
788 # $fix = htmlspecialchars( $wgStylePath . "/ie-png-fix.js" );
789 # $ret .= "<!--[if gte IE 5.5000]><script type='text/javascript' src='$fix'>< /script><![endif]-->";
790 return $ret;
791 }
792
793 /**
794 * Run any necessary pre-output transformations on the buffer text
795 */
796 function transformBuffer( $options = 0 ) {
797 $this->replaceLinkHolders( $this->mBodytext, $options );
798 }
799
800 /**
801 * Replace <!--LINK--> link placeholders with actual links, in the buffer
802 * Placeholders created in Skin::makeLinkObj()
803 * Returns an array of links found, indexed by PDBK:
804 * 0 - broken
805 * 1 - normal link
806 * 2 - stub
807 * $options is a bit field, RLH_FOR_UPDATE to select for update
808 */
809 function replaceLinkHolders( &$text, $options = 0 ) {
810 global $wgUser, $wgLinkCache, $wgUseOldExistenceCheck;
811
812 if ( $wgUseOldExistenceCheck ) {
813 return array();
814 }
815
816 $fname = 'OutputPage::replaceLinkHolders';
817 wfProfileIn( $fname );
818
819 $titles = array();
820 $pdbks = array();
821 $colours = array();
822
823 # Get placeholders from body
824 wfProfileIn( $fname.'-match' );
825 preg_match_all( "/<!--LINK (.*?) (.*?) (.*?) (.*?)-->/", $text, $tmpLinks );
826 wfProfileOut( $fname.'-match' );
827
828 if ( !empty( $tmpLinks[0] ) ) {
829 wfProfileIn( $fname.'-check' );
830 $dbr =& wfGetDB( DB_SLAVE );
831 $cur = $dbr->tableName( 'cur' );
832 $sk = $wgUser->getSkin();
833 $threshold = $wgUser->getOption('stubthreshold');
834
835 $namespaces =& $tmpLinks[1];
836 $dbkeys =& $tmpLinks[2];
837 $queries =& $tmpLinks[3];
838 $texts =& $tmpLinks[4];
839
840 # Sort by namespace
841 asort( $namespaces );
842
843 # Generate query
844 $query = false;
845 foreach ( $namespaces as $key => $val ) {
846 # Make title object
847 $dbk = $dbkeys[$key];
848 $title = $titles[$key] = Title::makeTitleSafe( $val, $dbk );
849
850 # Skip invalid entries.
851 # Result will be ugly, but prevents crash.
852 if ( is_null( $title ) ) {
853 continue;
854 }
855 $pdbk = $pdbks[$key] = $title->getPrefixedDBkey();
856
857 # Check if it's in the link cache already
858 if ( $wgLinkCache->getGoodLinkID( $pdbk ) ) {
859 $colours[$pdbk] = 1;
860 } elseif ( $wgLinkCache->isBadLink( $pdbk ) ) {
861 $colours[$pdbk] = 0;
862 } else {
863 # Not in the link cache, add it to the query
864 if ( !isset( $current ) ) {
865 $current = $val;
866 $query = "SELECT cur_id, cur_namespace, cur_title";
867 if ( $threshold > 0 ) {
868 $query .= ", LENGTH(cur_text) AS cur_len, cur_is_redirect";
869 }
870 $query .= " FROM $cur WHERE (cur_namespace=$val AND cur_title IN(";
871 } elseif ( $current != $val ) {
872 $current = $val;
873 $query .= ")) OR (cur_namespace=$val AND cur_title IN(";
874 } else {
875 $query .= ', ';
876 }
877
878 $query .= $dbr->addQuotes( $dbkeys[$key] );
879 }
880 }
881 if ( $query ) {
882 $query .= '))';
883 if ( $options & RLH_FOR_UPDATE ) {
884 $query .= ' FOR UPDATE';
885 }
886
887 $res = $dbr->query( $query, $fname );
888
889 # Fetch data and form into an associative array
890 # non-existent = broken
891 # 1 = known
892 # 2 = stub
893 while ( $s = $dbr->fetchObject($res) ) {
894 $title = Title::makeTitle( $s->cur_namespace, $s->cur_title );
895 $pdbk = $title->getPrefixedDBkey();
896 $wgLinkCache->addGoodLink( $s->cur_id, $pdbk );
897
898 if ( $threshold > 0 ) {
899 $size = $s->cur_len;
900 if ( $s->cur_is_redirect || $s->cur_namespace != 0 || $length < $threshold ) {
901 $colours[$pdbk] = 1;
902 } else {
903 $colours[$pdbk] = 2;
904 }
905 } else {
906 $colours[$pdbk] = 1;
907 }
908 }
909 }
910 wfProfileOut( $fname.'-check' );
911
912 # Construct search and replace arrays
913 wfProfileIn( $fname.'-construct' );
914 global $outputReplace;
915 $outputReplace = array();
916 foreach ( $namespaces as $key => $ns ) {
917 $pdbk = $pdbks[$key];
918 $searchkey = $tmpLinks[0][$key];
919 $title = $titles[$key];
920 if ( empty( $colours[$pdbk] ) ) {
921 $wgLinkCache->addBadLink( $pdbk );
922 $colours[$pdbk] = 0;
923 $outputReplace[$searchkey] = $sk->makeBrokenLinkObj( $title, $texts[$key], $queries[$key] );
924 } elseif ( $colours[$pdbk] == 1 ) {
925 $outputReplace[$searchkey] = $sk->makeKnownLinkObj( $title, $texts[$key], $queries[$key] );
926 } elseif ( $colours[$pdbk] == 2 ) {
927 $outputReplace[$searchkey] = $sk->makeStubLinkObj( $title, $texts[$key], $queries[$key] );
928 }
929 }
930 wfProfileOut( $fname.'-construct' );
931
932 # Do the thing
933 wfProfileIn( $fname.'-replace' );
934
935 $text = preg_replace_callback(
936 '/(<!--LINK .*? .*? .*? .*?-->)/',
937 "outputReplaceMatches",
938 $text);
939 wfProfileOut( $fname.'-replace' );
940 }
941 wfProfileOut( $fname );
942 return $colours;
943 }
944 }
945
946 function &outputReplaceMatches($matches) {
947 global $outputReplace;
948 return $outputReplace[$matches[1]];
949 }
950
951 }
952 ?>