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