I had a fix for this bug sitting in my working copy for over a week without getting...
[lhc/web/wiklou.git] / includes / OutputPage.php
1 <?php
2 /**
3 *
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 debug( $text ) { $this->mDebugtext .= $text; }
227
228 function setParserOptions( $options )
229 {
230 return wfSetVar( $this->mParserOptions, $options );
231 }
232
233 /**
234 * Convert wikitext to HTML and add it to the buffer
235 */
236 function addWikiText( $text, $linestart = true )
237 {
238 global $wgParser, $wgTitle;
239
240 $parserOutput = $wgParser->parse( $text, $wgTitle, $this->mParserOptions, $linestart );
241 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
242 $this->mCategoryLinks += $parserOutput->getCategoryLinks();
243 $this->addHTML( $parserOutput->getText() );
244 }
245
246 /**
247 * Add wikitext to the buffer, assuming that this is the primary text for a page view
248 * Saves the text into the parser cache if possible
249 */
250 function addPrimaryWikiText( $text, $cacheArticle ) {
251 global $wgParser, $wgParserCache, $wgUser, $wgTitle;
252
253 $parserOutput = $wgParser->parse( $text, $wgTitle, $this->mParserOptions, true );
254
255 # Replace link holders
256 $text = $parserOutput->getText();
257 $this->replaceLinkHolders( $text );
258 $parserOutput->setText( $text );
259
260 if ( $cacheArticle ) {
261 $wgParserCache->save( $parserOutput, $cacheArticle, $wgUser );
262 }
263
264 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
265 $this->mCategoryLinks += $parserOutput->getCategoryLinks();
266 $this->addHTML( $text );
267 }
268
269 function tryParserCache( $article, $user ) {
270 global $wgParserCache;
271 $parserOutput = $wgParserCache->get( $article, $user );
272 if ( $parserOutput !== false ) {
273 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
274 $this->mCategoryLinks += $parserOutput->getCategoryLinks();
275 $this->addHTML( $parserOutput->getText() );
276 return true;
277 } else {
278 return false;
279 }
280 }
281
282 /**
283 * Set the maximum cache time on the Squid in seconds
284 */
285 function setSquidMaxage( $maxage ) {
286 $this->mSquidMaxage = $maxage;
287 }
288
289 /**
290 * Use enableClientCache(false) to force it to send nocache headers
291 */
292 function enableClientCache( $state ) {
293 return wfSetVar( $this->mEnableClientCache, $state );
294 }
295
296 function sendCacheControl() {
297 global $wgUseSquid, $wgUseESI;
298 # FIXME: This header may cause trouble with some versions of Internet Explorer
299 header( 'Vary: Accept-Encoding, Cookie' );
300 if( $this->mEnableClientCache ) {
301 if( $wgUseSquid && ! isset( $_COOKIE[ini_get( 'session.name') ] ) &&
302 ! $this->isPrintable() && $this->mSquidMaxage != 0 )
303 {
304 if ( $wgUseESI ) {
305 # We'll purge the proxy cache explicitly, but require end user agents
306 # to revalidate against the proxy on each visit.
307 # Surrogate-Control controls our Squid, Cache-Control downstream caches
308 wfDebug( "** proxy caching with ESI; {$this->mLastModified} **\n", false );
309 # start with a shorter timeout for initial testing
310 # header( 'Surrogate-Control: max-age=2678400+2678400, content="ESI/1.0"');
311 header( 'Surrogate-Control: max-age='.$wgSquidMaxage.'+'.$this->mSquidMaxage.', content="ESI/1.0"');
312 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
313 } else {
314 # We'll purge the proxy cache for anons explicitly, but require end user agents
315 # to revalidate against the proxy on each visit.
316 # IMPORTANT! The Squid needs to replace the Cache-Control header with
317 # Cache-Control: s-maxage=0, must-revalidate, max-age=0
318 wfDebug( "** local proxy caching; {$this->mLastModified} **\n", false );
319 # start with a shorter timeout for initial testing
320 # header( "Cache-Control: s-maxage=2678400, must-revalidate, max-age=0" );
321 header( 'Cache-Control: s-maxage='.$this->mSquidMaxage.', must-revalidate, max-age=0' );
322 }
323 } else {
324 # We do want clients to cache if they can, but they *must* check for updates
325 # on revisiting the page.
326 wfDebug( "** private caching; {$this->mLastModified} **\n", false );
327 header( "Expires: -1" );
328 header( "Cache-Control: private, must-revalidate, max-age=0" );
329 }
330 if($this->mLastModified) header( "Last-modified: {$this->mLastModified}" );
331 } else {
332 wfDebug( "** no caching **\n", false );
333
334 # In general, the absence of a last modified header should be enough to prevent
335 # the client from using its cache. We send a few other things just to make sure.
336 header( 'Expires: -1' );
337 header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
338 header( 'Pragma: no-cache' );
339 }
340 }
341
342 /**
343 * Finally, all the text has been munged and accumulated into
344 * the object, let's actually output it:
345 */
346 function output()
347 {
348 global $wgUser, $wgLang, $wgDebugComments, $wgCookieExpiration;
349 global $wgInputEncoding, $wgOutputEncoding, $wgLanguageCode;
350 global $wgDebugRedirects, $wgMimeType, $wgProfiler;
351 if( $this->mDoNothing ){
352 return;
353 }
354 $fname = 'OutputPage::output';
355 wfProfileIn( $fname );
356
357 $sk = $wgUser->getSkin();
358
359 if ( '' != $this->mRedirect ) {
360 if( substr( $this->mRedirect, 0, 4 ) != 'http' ) {
361 # Standards require redirect URLs to be absolute
362 global $wgServer;
363 $this->mRedirect = $wgServer . $this->mRedirect;
364 }
365 if( $this->mRedirectCode == '301') {
366 if( !$wgDebugRedirects ) {
367 header("HTTP/1.1 {$this->mRedirectCode} Moved Permanently");
368 }
369 $this->mLastModified = gmdate( 'D, j M Y H:i:s' ) . ' GMT';
370 }
371
372 $this->sendCacheControl();
373
374 if( $wgDebugRedirects ) {
375 $url = htmlspecialchars( $this->mRedirect );
376 print "<html>\n<head>\n<title>Redirect</title>\n</head>\n<body>\n";
377 print "<p>Location: <a href=\"$url\">$url</a></p>\n";
378 print "</body>\n</html>\n";
379 } else {
380 header( 'Location: '.$this->mRedirect );
381 }
382 if ( isset( $wgProfiler ) ) { wfDebug( $wgProfiler->getOutput() ); }
383 return;
384 }
385
386
387 $this->sendCacheControl();
388 # Perform link colouring
389 $this->transformBuffer();
390 $this->replaceLinkHolders( $this->mSubtitle );
391
392 # Disable temporary placeholders, so that the skin produces HTML
393 $sk->postParseLinkColour( false );
394
395 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
396 header( 'Content-language: '.$wgLanguageCode );
397
398 $exp = time() + $wgCookieExpiration;
399 foreach( $this->mCookies as $name => $val ) {
400 setcookie( $name, $val, $exp, '/' );
401 }
402
403 $sk->outputPage( $this );
404 # flush();
405 }
406
407 function out( $ins ) {
408 global $wgInputEncoding, $wgOutputEncoding, $wgLang;
409 if ( 0 == strcmp( $wgInputEncoding, $wgOutputEncoding ) ) {
410 $outs = $ins;
411 } else {
412 $outs = $wgLang->iconv( $wgInputEncoding, $wgOutputEncoding, $ins );
413 if ( false === $outs ) { $outs = $ins; }
414 }
415 print $outs;
416 }
417
418 function setEncodings() {
419 global $wgInputEncoding, $wgOutputEncoding;
420 global $wgUser, $wgLang;
421
422 $wgInputEncoding = strtolower( $wgInputEncoding );
423
424 if( $wgUser->getOption( 'altencoding' ) ) {
425 $wgLang->setAltEncoding();
426 return;
427 }
428
429 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
430 $wgOutputEncoding = strtolower( $wgOutputEncoding );
431 return;
432 }
433
434 /*
435 # This code is unused anyway!
436 # Commenting out. --bv 2003-11-15
437
438 $a = explode( ",", $_SERVER['HTTP_ACCEPT_CHARSET'] );
439 $best = 0.0;
440 $bestset = "*";
441
442 foreach ( $a as $s ) {
443 if ( preg_match( "/(.*);q=(.*)/", $s, $m ) ) {
444 $set = $m[1];
445 $q = (float)($m[2]);
446 } else {
447 $set = $s;
448 $q = 1.0;
449 }
450 if ( $q > $best ) {
451 $bestset = $set;
452 $best = $q;
453 }
454 }
455 #if ( "*" == $bestset ) { $bestset = "iso-8859-1"; }
456 if ( "*" == $bestset ) { $bestset = $wgOutputEncoding; }
457 $wgOutputEncoding = strtolower( $bestset );
458
459 # Disable for now
460 #
461 */
462 $wgOutputEncoding = $wgInputEncoding;
463 }
464
465 /**
466 * Returns a HTML comment with the elapsed time since request.
467 * This method has no side effects.
468 */
469 function reportTime() {
470 global $wgRequestTime;
471
472 $now = wfTime();
473 list( $usec, $sec ) = explode( ' ', $wgRequestTime );
474 $start = (float)$sec + (float)$usec;
475 $elapsed = $now - $start;
476
477 # Use real server name if available, so we know which machine
478 # in a server farm generated the current page.
479 if ( function_exists( 'posix_uname' ) ) {
480 $uname = @posix_uname();
481 } else {
482 $uname = false;
483 }
484 if( is_array( $uname ) && isset( $uname['nodename'] ) ) {
485 $hostname = $uname['nodename'];
486 } else {
487 # This may be a virtual server.
488 $hostname = $_SERVER['SERVER_NAME'];
489 }
490 $com = sprintf( "<!-- Served by %s in %01.2f secs. -->",
491 $hostname, $elapsed );
492 return $com;
493 }
494
495 /**
496 * Note: these arguments are keys into wfMsg(), not text!
497 */
498 function errorpage( $title, $msg ) {
499 global $wgTitle;
500
501 $this->mDebugtext .= 'Original title: ' .
502 $wgTitle->getPrefixedText() . "\n";
503 $this->setPageTitle( wfMsg( $title ) );
504 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
505 $this->setRobotpolicy( 'noindex,nofollow' );
506 $this->setArticleRelated( false );
507 $this->enableClientCache( false );
508 $this->mRedirect = '';
509
510 $this->mBodytext = '';
511 $this->addHTML( '<p>' . wfMsg( $msg ) . "</p>\n" );
512 $this->returnToMain( false );
513
514 $this->output();
515 wfErrorExit();
516 }
517
518 function sysopRequired() {
519 global $wgUser;
520
521 $this->setPageTitle( wfMsg( 'sysoptitle' ) );
522 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
523 $this->setRobotpolicy( 'noindex,nofollow' );
524 $this->setArticleRelated( false );
525 $this->mBodytext = '';
526
527 $sk = $wgUser->getSkin();
528 $ap = $sk->makeKnownLink( wfMsg( 'administrators' ), '' );
529 $this->addHTML( wfMsg( 'sysoptext', $ap ) );
530 $this->returnToMain();
531 }
532
533 function developerRequired() {
534 global $wgUser;
535
536 $this->setPageTitle( wfMsg( 'developertitle' ) );
537 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
538 $this->setRobotpolicy( 'noindex,nofollow' );
539 $this->setArticleRelated( false );
540 $this->mBodytext = '';
541
542 $sk = $wgUser->getSkin();
543 $ap = $sk->makeKnownLink( wfMsg( 'administrators' ), '' );
544 $this->addHTML( wfMsg( 'developertext', $ap ) );
545 $this->returnToMain();
546 }
547
548 function loginToUse() {
549 global $wgUser, $wgTitle, $wgLang;
550
551 $this->setPageTitle( wfMsg( 'loginreqtitle' ) );
552 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
553 $this->setRobotpolicy( 'noindex,nofollow' );
554 $this->setArticleFlag( false );
555 $this->mBodytext = '';
556 $this->addWikiText( wfMsg( 'loginreqtext' ) );
557
558 # We put a comment in the .html file so a Sysop can diagnose the page the
559 # user can't see.
560 $this->addHTML( "\n<!--" .
561 $wgLang->getNsText( $wgTitle->getNamespace() ) .
562 ':' .
563 $wgTitle->getDBkey() . '-->' );
564 $this->returnToMain(); # Flip back to the main page after 10 seconds.
565 }
566
567 function databaseError( $fname, $sql, $error, $errno ) {
568 global $wgUser, $wgCommandLineMode;
569
570 $this->setPageTitle( wfMsgNoDB( 'databaseerror' ) );
571 $this->setRobotpolicy( 'noindex,nofollow' );
572 $this->setArticleRelated( false );
573 $this->enableClientCache( false );
574 $this->mRedirect = '';
575
576 if ( $wgCommandLineMode ) {
577 $msg = wfMsgNoDB( 'dberrortextcl', htmlspecialchars( $sql ),
578 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
579 } else {
580 $msg = wfMsgNoDB( 'dberrortext', htmlspecialchars( $sql ),
581 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
582 }
583
584 if ( $wgCommandLineMode || !is_object( $wgUser )) {
585 print $msg."\n";
586 wfErrorExit();
587 }
588 $this->mBodytext = $msg;
589 $this->output();
590 wfErrorExit();
591 }
592
593 function readOnlyPage( $source = null, $protected = false ) {
594 global $wgUser, $wgReadOnlyFile;
595
596 $this->setRobotpolicy( 'noindex,nofollow' );
597 $this->setArticleRelated( false );
598
599 if( $protected ) {
600 $this->setPageTitle( wfMsg( 'viewsource' ) );
601 $this->addWikiText( wfMsg( 'protectedtext' ) );
602 } else {
603 $this->setPageTitle( wfMsg( 'readonly' ) );
604 $reason = file_get_contents( $wgReadOnlyFile );
605 $this->addWikiText( wfMsg( 'readonlytext', $reason ) );
606 }
607
608 if( is_string( $source ) ) {
609 if( strcmp( $source, '' ) == 0 ) {
610 $source = wfMsg( 'noarticletext' );
611 }
612 $rows = $wgUser->getOption( 'rows' );
613 $cols = $wgUser->getOption( 'cols' );
614 $text = "\n<textarea cols='$cols' rows='$rows' readonly='readonly'>" .
615 htmlspecialchars( $source ) . "\n</textarea>";
616 $this->addHTML( $text );
617 }
618
619 $this->returnToMain( false );
620 }
621
622 function fatalError( $message ) {
623 $this->setPageTitle( wfMsg( "internalerror" ) );
624 $this->setRobotpolicy( "noindex,nofollow" );
625 $this->setArticleRelated( false );
626 $this->enableClientCache( false );
627 $this->mRedirect = '';
628
629 $this->mBodytext = $message;
630 $this->output();
631 wfErrorExit();
632 }
633
634 function unexpectedValueError( $name, $val ) {
635 $this->fatalError( wfMsg( 'unexpected', $name, $val ) );
636 }
637
638 function fileCopyError( $old, $new ) {
639 $this->fatalError( wfMsg( 'filecopyerror', $old, $new ) );
640 }
641
642 function fileRenameError( $old, $new ) {
643 $this->fatalError( wfMsg( 'filerenameerror', $old, $new ) );
644 }
645
646 function fileDeleteError( $name ) {
647 $this->fatalError( wfMsg( 'filedeleteerror', $name ) );
648 }
649
650 function fileNotFoundError( $name ) {
651 $this->fatalError( wfMsg( 'filenotfound', $name ) );
652 }
653
654 /**
655 * return from error messages or notes
656 * @param $auto automatically redirect the user after 10 seconds
657 * @param $returnto page title to return to. Default is Main Page.
658 */
659 function returnToMain( $auto = true, $returnto = NULL ) {
660 global $wgUser, $wgOut, $wgRequest;
661
662 if ( $returnto == NULL ) {
663 $returnto = $wgRequest->getText( 'returnto' );
664 }
665
666 $sk = $wgUser->getSkin();
667 if ( '' == $returnto ) {
668 $returnto = wfMsg( 'mainpage' );
669 }
670 $link = $sk->makeKnownLink( $returnto, '' );
671
672 $r = wfMsg( 'returnto', $link );
673 if ( $auto ) {
674 $titleObj = Title::newFromText( $returnto );
675 $wgOut->addMeta( 'http:Refresh', '10;url=' . $titleObj->escapeFullURL() );
676 }
677 $wgOut->addHTML( "\n<p>$r</p>\n" );
678 }
679
680 /**
681 * This function takes the existing and broken links for the page
682 * and uses the first 10 of them for META keywords
683 */
684 function addMetaTags () {
685 global $wgLinkCache , $wgOut ;
686 $good = array_keys ( $wgLinkCache->mGoodLinks ) ;
687 $bad = array_keys ( $wgLinkCache->mBadLinks ) ;
688 $a = array_merge ( $good , $bad ) ;
689 $a = array_slice ( $a , 0 , 10 ) ; # 10 keywords max
690 $a = implode ( ',' , $a ) ;
691 $strip = array(
692 "/<.*?" . ">/" => '',
693 "/[_]/" => ' '
694 );
695 $a = htmlspecialchars(preg_replace(array_keys($strip), array_values($strip),$a ));
696
697 $wgOut->addMeta ( 'KEYWORDS' , $a ) ;
698 }
699
700 /**
701 * @private
702 */
703 function headElement() {
704 global $wgDocType, $wgDTD, $wgLanguageCode, $wgOutputEncoding, $wgMimeType;
705 global $wgUser, $wgLang, $wgRequest;
706
707 $xml = ($wgMimeType == 'text/xml');
708 if( $xml ) {
709 $ret = "<" . "?xml version=\"1.0\" encoding=\"$wgOutputEncoding\" ?" . ">\n";
710 } else {
711 $ret = '';
712 }
713
714 $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
715
716 if ( "" == $this->mHTMLtitle ) {
717 $this->mHTMLtitle = wfMsg( "pagetitle", $this->mPagetitle );
718 }
719 if( $xml ) {
720 $xmlbits = "xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\"";
721 } else {
722 $xmlbits = '';
723 }
724 $rtl = $wgLang->isRTL() ? " dir='RTL'" : '';
725 $ret .= "<html $xmlbits lang=\"$wgLanguageCode\" $rtl>\n";
726 $ret .= "<head>\n<title>" . htmlspecialchars( $this->mHTMLtitle ) . "</title>\n";
727 array_push( $this->mMetatags, array( "http:Content-type", "$wgMimeType; charset={$wgOutputEncoding}" ) );
728
729 $ret .= $this->getHeadLinks();
730 global $wgStylePath;
731 if( $this->isPrintable() ) {
732 $media = '';
733 } else {
734 $media = "media='print'";
735 }
736 $printsheet = htmlspecialchars( "$wgStylePath/common/wikiprintable.css" );
737 $ret .= "<link rel='stylesheet' type='text/css' $media href='$printsheet' />\n";
738
739 $sk = $wgUser->getSkin();
740 $ret .= $sk->getHeadScripts();
741 $ret .= $this->mScripts;
742 $ret .= $sk->getUserStyles();
743
744 $ret .= "</head>\n";
745 return $ret;
746 }
747
748 function getHeadLinks() {
749 global $wgRequest, $wgStylePath;
750 $ret = '';
751 foreach ( $this->mMetatags as $tag ) {
752 if ( 0 == strcasecmp( 'http:', substr( $tag[0], 0, 5 ) ) ) {
753 $a = 'http-equiv';
754 $tag[0] = substr( $tag[0], 5 );
755 } else {
756 $a = 'name';
757 }
758 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\" />\n";
759 }
760 $p = $this->mRobotpolicy;
761 if ( '' == $p ) { $p = 'index,follow'; }
762 $ret .= "<meta name=\"robots\" content=\"$p\" />\n";
763
764 if ( count( $this->mKeywords ) > 0 ) {
765 $strip = array(
766 "/<.*?" . ">/" => '',
767 "/[_]/" => ' '
768 );
769 $ret .= "<meta name=\"keywords\" content=\"" .
770 htmlspecialchars(preg_replace(array_keys($strip), array_values($strip),implode( ",", $this->mKeywords ))) . "\" />\n";
771 }
772 foreach ( $this->mLinktags as $tag ) {
773 $ret .= '<link';
774 foreach( $tag as $attr => $val ) {
775 $ret .= " $attr=\"" . htmlspecialchars( $val ) . "\"";
776 }
777 $ret .= " />\n";
778 }
779 if( $this->isSyndicated() ) {
780 # FIXME: centralize the mime-type and name information in Feed.php
781 $link = $wgRequest->escapeAppendQuery( 'feed=rss' );
782 $ret .= "<link rel='alternate' type='application/rss+xml' title='RSS 2.0' href='$link' />\n";
783 $link = $wgRequest->escapeAppendQuery( 'feed=atom' );
784 $ret .= "<link rel='alternate' type='application/rss+atom' title='Atom 0.3' href='$link' />\n";
785 }
786 # FIXME: get these working
787 # $fix = htmlspecialchars( $wgStylePath . "/ie-png-fix.js" );
788 # $ret .= "<!--[if gte IE 5.5000]><script type='text/javascript' src='$fix'>< /script><![endif]-->";
789 return $ret;
790 }
791
792 /**
793 * Run any necessary pre-output transformations on the buffer text
794 */
795 function transformBuffer( $options = 0 ) {
796 $this->replaceLinkHolders( $this->mBodytext, $options );
797 }
798
799 /**
800 * Replace <!--LINK--> link placeholders with actual links, in the buffer
801 * Placeholders created in Skin::makeLinkObj()
802 * Returns an array of links found, indexed by PDBK:
803 * 0 - broken
804 * 1 - normal link
805 * 2 - stub
806 * $options is a bit field, RLH_FOR_UPDATE to select for update
807 */
808 function replaceLinkHolders( &$text, $options = 0 ) {
809 global $wgUser, $wgLinkCache, $wgUseOldExistenceCheck;
810
811 if ( $wgUseOldExistenceCheck ) {
812 return array();
813 }
814
815 $fname = 'OutputPage::replaceLinkHolders';
816 wfProfileIn( $fname );
817
818 $titles = array();
819 $pdbks = array();
820 $colours = array();
821
822 # Get placeholders from body
823 wfProfileIn( $fname.'-match' );
824 preg_match_all( "/<!--LINK (.*?) (.*?) (.*?) (.*?)-->/", $text, $tmpLinks );
825 wfProfileOut( $fname.'-match' );
826
827 if ( !empty( $tmpLinks[0] ) ) {
828 wfProfileIn( $fname.'-check' );
829 $dbr =& wfGetDB( DB_SLAVE );
830 $cur = $dbr->tableName( 'cur' );
831 $sk = $wgUser->getSkin();
832 $threshold = $wgUser->getOption('stubthreshold');
833
834 $namespaces =& $tmpLinks[1];
835 $dbkeys =& $tmpLinks[2];
836 $queries =& $tmpLinks[3];
837 $texts =& $tmpLinks[4];
838
839 # Sort by namespace
840 asort( $namespaces );
841
842 # Generate query
843 $query = false;
844 foreach ( $namespaces as $key => $val ) {
845 # Make title object
846 $dbk = $dbkeys[$key];
847 $title = $titles[$key] = Title::makeTitleSafe( $val, $dbk );
848
849 # Skip invalid entries.
850 # Result will be ugly, but prevents crash.
851 if ( is_null( $title ) ) {
852 continue;
853 }
854 $pdbk = $pdbks[$key] = $title->getPrefixedDBkey();
855
856 # Check if it's in the link cache already
857 if ( $wgLinkCache->getGoodLinkID( $pdbk ) ) {
858 $colours[$pdbk] = 1;
859 } elseif ( $wgLinkCache->isBadLink( $pdbk ) ) {
860 $colours[$pdbk] = 0;
861 } else {
862 # Not in the link cache, add it to the query
863 if ( !isset( $current ) ) {
864 $current = $val;
865 $query = "SELECT cur_id, cur_namespace, cur_title";
866 if ( $threshold > 0 ) {
867 $query .= ", LENGTH(cur_text) AS cur_len, cur_is_redirect";
868 }
869 $query .= " FROM $cur WHERE (cur_namespace=$val AND cur_title IN(";
870 } elseif ( $current != $val ) {
871 $current = $val;
872 $query .= ")) OR (cur_namespace=$val AND cur_title IN(";
873 } else {
874 $query .= ', ';
875 }
876
877 $query .= $dbr->addQuotes( $dbkeys[$key] );
878 }
879 }
880 if ( $query ) {
881 $query .= '))';
882 if ( $options & RLH_FOR_UPDATE ) {
883 $query .= ' FOR UPDATE';
884 }
885
886 $res = $dbr->query( $query, $fname );
887
888 # Fetch data and form into an associative array
889 # non-existent = broken
890 # 1 = known
891 # 2 = stub
892 while ( $s = $dbr->fetchObject($res) ) {
893 $title = Title::makeTitle( $s->cur_namespace, $s->cur_title );
894 $pdbk = $title->getPrefixedDBkey();
895 $wgLinkCache->addGoodLink( $s->cur_id, $pdbk );
896
897 if ( $threshold > 0 ) {
898 $size = $s->cur_len;
899 if ( $s->cur_is_redirect || $s->cur_namespace != 0 || $length < $threshold ) {
900 $colours[$pdbk] = 1;
901 } else {
902 $colours[$pdbk] = 2;
903 }
904 } else {
905 $colours[$pdbk] = 1;
906 }
907 }
908 }
909 wfProfileOut( $fname.'-check' );
910
911 # Construct search and replace arrays
912 wfProfileIn( $fname.'-construct' );
913 global $outputReplace;
914 $outputReplace = array();
915 foreach ( $namespaces as $key => $ns ) {
916 $pdbk = $pdbks[$key];
917 $searchkey = $tmpLinks[0][$key];
918 $title = $titles[$key];
919 if ( empty( $colours[$pdbk] ) ) {
920 $wgLinkCache->addBadLink( $pdbk );
921 $colours[$pdbk] = 0;
922 $outputReplace[$searchkey] = $sk->makeBrokenLinkObj( $title, $texts[$key], $queries[$key] );
923 } elseif ( $colours[$pdbk] == 1 ) {
924 $outputReplace[$searchkey] = $sk->makeKnownLinkObj( $title, $texts[$key], $queries[$key] );
925 } elseif ( $colours[$pdbk] == 2 ) {
926 $outputReplace[$searchkey] = $sk->makeStubLinkObj( $title, $texts[$key], $queries[$key] );
927 }
928 }
929 wfProfileOut( $fname.'-construct' );
930
931 # Do the thing
932 wfProfileIn( $fname.'-replace' );
933
934 $text = preg_replace_callback(
935 '/(<!--LINK .*? .*? .*? .*?-->)/',
936 "outputReplaceMatches",
937 $text);
938 wfProfileOut( $fname.'-replace' );
939 }
940 wfProfileOut( $fname );
941 return $colours;
942 }
943 }
944
945 function &outputReplaceMatches($matches) {
946 global $outputReplace;
947 return $outputReplace[$matches[1]];
948 }
949
950 }
951 ?>