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