777039a881a02212b3e5bbc3c4aa35653edf508a
[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
391 # Disable temporary placeholders, so that the skin produces HTML
392 $sk->postParseLinkColour( false );
393
394 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
395 header( 'Content-language: '.$wgLanguageCode );
396
397 $exp = time() + $wgCookieExpiration;
398 foreach( $this->mCookies as $name => $val ) {
399 setcookie( $name, $val, $exp, '/' );
400 }
401
402 $sk->outputPage( $this );
403 # flush();
404 }
405
406 function out( $ins ) {
407 global $wgInputEncoding, $wgOutputEncoding, $wgLang;
408 if ( 0 == strcmp( $wgInputEncoding, $wgOutputEncoding ) ) {
409 $outs = $ins;
410 } else {
411 $outs = $wgLang->iconv( $wgInputEncoding, $wgOutputEncoding, $ins );
412 if ( false === $outs ) { $outs = $ins; }
413 }
414 print $outs;
415 }
416
417 function setEncodings() {
418 global $wgInputEncoding, $wgOutputEncoding;
419 global $wgUser, $wgLang;
420
421 $wgInputEncoding = strtolower( $wgInputEncoding );
422
423 if( $wgUser->getOption( 'altencoding' ) ) {
424 $wgLang->setAltEncoding();
425 return;
426 }
427
428 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
429 $wgOutputEncoding = strtolower( $wgOutputEncoding );
430 return;
431 }
432
433 /*
434 # This code is unused anyway!
435 # Commenting out. --bv 2003-11-15
436
437 $a = explode( ",", $_SERVER['HTTP_ACCEPT_CHARSET'] );
438 $best = 0.0;
439 $bestset = "*";
440
441 foreach ( $a as $s ) {
442 if ( preg_match( "/(.*);q=(.*)/", $s, $m ) ) {
443 $set = $m[1];
444 $q = (float)($m[2]);
445 } else {
446 $set = $s;
447 $q = 1.0;
448 }
449 if ( $q > $best ) {
450 $bestset = $set;
451 $best = $q;
452 }
453 }
454 #if ( "*" == $bestset ) { $bestset = "iso-8859-1"; }
455 if ( "*" == $bestset ) { $bestset = $wgOutputEncoding; }
456 $wgOutputEncoding = strtolower( $bestset );
457
458 # Disable for now
459 #
460 */
461 $wgOutputEncoding = $wgInputEncoding;
462 }
463
464 /**
465 * Returns a HTML comment with the elapsed time since request.
466 * This method has no side effects.
467 */
468 function reportTime() {
469 global $wgRequestTime;
470
471 $now = wfTime();
472 list( $usec, $sec ) = explode( ' ', $wgRequestTime );
473 $start = (float)$sec + (float)$usec;
474 $elapsed = $now - $start;
475
476 # Use real server name if available, so we know which machine
477 # in a server farm generated the current page.
478 if ( function_exists( 'posix_uname' ) ) {
479 $uname = @posix_uname();
480 } else {
481 $uname = false;
482 }
483 if( is_array( $uname ) && isset( $uname['nodename'] ) ) {
484 $hostname = $uname['nodename'];
485 } else {
486 # This may be a virtual server.
487 $hostname = $_SERVER['SERVER_NAME'];
488 }
489 $com = sprintf( "<!-- Served by %s in %01.2f secs. -->",
490 $hostname, $elapsed );
491 return $com;
492 }
493
494 /**
495 * Note: these arguments are keys into wfMsg(), not text!
496 */
497 function errorpage( $title, $msg ) {
498 global $wgTitle;
499
500 $this->mDebugtext .= 'Original title: ' .
501 $wgTitle->getPrefixedText() . "\n";
502 $this->setPageTitle( wfMsg( $title ) );
503 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
504 $this->setRobotpolicy( 'noindex,nofollow' );
505 $this->setArticleRelated( false );
506 $this->enableClientCache( false );
507 $this->mRedirect = '';
508
509 $this->mBodytext = '';
510 $this->addHTML( '<p>' . wfMsg( $msg ) . "</p>\n" );
511 $this->returnToMain( false );
512
513 $this->output();
514 wfErrorExit();
515 }
516
517 function sysopRequired() {
518 global $wgUser;
519
520 $this->setPageTitle( wfMsg( 'sysoptitle' ) );
521 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
522 $this->setRobotpolicy( 'noindex,nofollow' );
523 $this->setArticleRelated( false );
524 $this->mBodytext = '';
525
526 $sk = $wgUser->getSkin();
527 $ap = $sk->makeKnownLink( wfMsg( 'administrators' ), '' );
528 $this->addHTML( wfMsg( 'sysoptext', $ap ) );
529 $this->returnToMain();
530 }
531
532 function developerRequired() {
533 global $wgUser;
534
535 $this->setPageTitle( wfMsg( 'developertitle' ) );
536 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
537 $this->setRobotpolicy( 'noindex,nofollow' );
538 $this->setArticleRelated( false );
539 $this->mBodytext = '';
540
541 $sk = $wgUser->getSkin();
542 $ap = $sk->makeKnownLink( wfMsg( 'administrators' ), '' );
543 $this->addHTML( wfMsg( 'developertext', $ap ) );
544 $this->returnToMain();
545 }
546
547 function loginToUse() {
548 global $wgUser, $wgTitle, $wgLang;
549
550 $this->setPageTitle( wfMsg( 'loginreqtitle' ) );
551 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
552 $this->setRobotpolicy( 'noindex,nofollow' );
553 $this->setArticleFlag( false );
554 $this->mBodytext = '';
555 $this->addWikiText( wfMsg( 'loginreqtext' ) );
556
557 # We put a comment in the .html file so a Sysop can diagnose the page the
558 # user can't see.
559 $this->addHTML( "\n<!--" .
560 $wgLang->getNsText( $wgTitle->getNamespace() ) .
561 ':' .
562 $wgTitle->getDBkey() . '-->' );
563 $this->returnToMain(); # Flip back to the main page after 10 seconds.
564 }
565
566 function databaseError( $fname, $sql, $error, $errno ) {
567 global $wgUser, $wgCommandLineMode;
568
569 $this->setPageTitle( wfMsgNoDB( 'databaseerror' ) );
570 $this->setRobotpolicy( 'noindex,nofollow' );
571 $this->setArticleRelated( false );
572 $this->enableClientCache( false );
573 $this->mRedirect = '';
574
575 if ( $wgCommandLineMode ) {
576 $msg = wfMsgNoDB( 'dberrortextcl', htmlspecialchars( $sql ),
577 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
578 } else {
579 $msg = wfMsgNoDB( 'dberrortext', htmlspecialchars( $sql ),
580 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
581 }
582
583 if ( $wgCommandLineMode || !is_object( $wgUser )) {
584 print $msg."\n";
585 wfErrorExit();
586 }
587 $this->mBodytext = $msg;
588 $this->output();
589 wfErrorExit();
590 }
591
592 function readOnlyPage( $source = null, $protected = false ) {
593 global $wgUser, $wgReadOnlyFile;
594
595 $this->setRobotpolicy( 'noindex,nofollow' );
596 $this->setArticleRelated( false );
597
598 if( $protected ) {
599 $this->setPageTitle( wfMsg( 'viewsource' ) );
600 $this->addWikiText( wfMsg( 'protectedtext' ) );
601 } else {
602 $this->setPageTitle( wfMsg( 'readonly' ) );
603 $reason = file_get_contents( $wgReadOnlyFile );
604 $this->addWikiText( wfMsg( 'readonlytext', $reason ) );
605 }
606
607 if( is_string( $source ) ) {
608 if( strcmp( $source, '' ) == 0 ) {
609 $source = wfMsg( 'noarticletext' );
610 }
611 $rows = $wgUser->getOption( 'rows' );
612 $cols = $wgUser->getOption( 'cols' );
613 $text = "\n<textarea cols='$cols' rows='$rows' readonly='readonly'>" .
614 htmlspecialchars( $source ) . "\n</textarea>";
615 $this->addHTML( $text );
616 }
617
618 $this->returnToMain( false );
619 }
620
621 function fatalError( $message ) {
622 $this->setPageTitle( wfMsg( "internalerror" ) );
623 $this->setRobotpolicy( "noindex,nofollow" );
624 $this->setArticleRelated( false );
625 $this->enableClientCache( false );
626 $this->mRedirect = '';
627
628 $this->mBodytext = $message;
629 $this->output();
630 wfErrorExit();
631 }
632
633 function unexpectedValueError( $name, $val ) {
634 $this->fatalError( wfMsg( 'unexpected', $name, $val ) );
635 }
636
637 function fileCopyError( $old, $new ) {
638 $this->fatalError( wfMsg( 'filecopyerror', $old, $new ) );
639 }
640
641 function fileRenameError( $old, $new ) {
642 $this->fatalError( wfMsg( 'filerenameerror', $old, $new ) );
643 }
644
645 function fileDeleteError( $name ) {
646 $this->fatalError( wfMsg( 'filedeleteerror', $name ) );
647 }
648
649 function fileNotFoundError( $name ) {
650 $this->fatalError( wfMsg( 'filenotfound', $name ) );
651 }
652
653 /**
654 * return from error messages or notes
655 * @param $auto automatically redirect the user after 10 seconds
656 * @param $returnto page title to return to. Default is Main Page.
657 */
658 function returnToMain( $auto = true, $returnto = NULL ) {
659 global $wgUser, $wgOut, $wgRequest;
660
661 if ( $returnto == NULL ) {
662 $returnto = $wgRequest->getText( 'returnto' );
663 }
664
665 $sk = $wgUser->getSkin();
666 if ( '' == $returnto ) {
667 $returnto = wfMsg( 'mainpage' );
668 }
669 $link = $sk->makeKnownLink( $returnto, '' );
670
671 $r = wfMsg( 'returnto', $link );
672 if ( $auto ) {
673 $titleObj = Title::newFromText( $returnto );
674 $wgOut->addMeta( 'http:Refresh', '10;url=' . $titleObj->escapeFullURL() );
675 }
676 $wgOut->addHTML( "\n<p>$r</p>\n" );
677 }
678
679 /**
680 * This function takes the existing and broken links for the page
681 * and uses the first 10 of them for META keywords
682 */
683 function addMetaTags () {
684 global $wgLinkCache , $wgOut ;
685 $good = array_keys ( $wgLinkCache->mGoodLinks ) ;
686 $bad = array_keys ( $wgLinkCache->mBadLinks ) ;
687 $a = array_merge ( $good , $bad ) ;
688 $a = array_slice ( $a , 0 , 10 ) ; # 10 keywords max
689 $a = implode ( ',' , $a ) ;
690 $strip = array(
691 "/<.*?" . ">/" => '',
692 "/[_]/" => ' '
693 );
694 $a = htmlspecialchars(preg_replace(array_keys($strip), array_values($strip),$a ));
695
696 $wgOut->addMeta ( 'KEYWORDS' , $a ) ;
697 }
698
699 /**
700 * @private
701 */
702 function headElement() {
703 global $wgDocType, $wgDTD, $wgLanguageCode, $wgOutputEncoding, $wgMimeType;
704 global $wgUser, $wgLang, $wgRequest;
705
706 $xml = ($wgMimeType == 'text/xml');
707 if( $xml ) {
708 $ret = "<" . "?xml version=\"1.0\" encoding=\"$wgOutputEncoding\" ?" . ">\n";
709 } else {
710 $ret = '';
711 }
712
713 $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
714
715 if ( "" == $this->mHTMLtitle ) {
716 $this->mHTMLtitle = wfMsg( "pagetitle", $this->mPagetitle );
717 }
718 if( $xml ) {
719 $xmlbits = "xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\"";
720 } else {
721 $xmlbits = '';
722 }
723 $rtl = $wgLang->isRTL() ? " dir='RTL'" : '';
724 $ret .= "<html $xmlbits lang=\"$wgLanguageCode\" $rtl>\n";
725 $ret .= "<head>\n<title>" . htmlspecialchars( $this->mHTMLtitle ) . "</title>\n";
726 array_push( $this->mMetatags, array( "http:Content-type", "$wgMimeType; charset={$wgOutputEncoding}" ) );
727
728 $ret .= $this->getHeadLinks();
729 global $wgStylePath;
730 if( $this->isPrintable() ) {
731 $media = '';
732 } else {
733 $media = "media='print'";
734 }
735 $printsheet = htmlspecialchars( "$wgStylePath/common/wikiprintable.css" );
736 $ret .= "<link rel='stylesheet' type='text/css' $media href='$printsheet' />\n";
737
738 $sk = $wgUser->getSkin();
739 $ret .= $sk->getHeadScripts();
740 $ret .= $this->mScripts;
741 $ret .= $sk->getUserStyles();
742
743 $ret .= "</head>\n";
744 return $ret;
745 }
746
747 function getHeadLinks() {
748 global $wgRequest, $wgStylePath;
749 $ret = '';
750 foreach ( $this->mMetatags as $tag ) {
751 if ( 0 == strcasecmp( 'http:', substr( $tag[0], 0, 5 ) ) ) {
752 $a = 'http-equiv';
753 $tag[0] = substr( $tag[0], 5 );
754 } else {
755 $a = 'name';
756 }
757 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\" />\n";
758 }
759 $p = $this->mRobotpolicy;
760 if ( '' == $p ) { $p = 'index,follow'; }
761 $ret .= "<meta name=\"robots\" content=\"$p\" />\n";
762
763 if ( count( $this->mKeywords ) > 0 ) {
764 $strip = array(
765 "/<.*?" . ">/" => '',
766 "/[_]/" => ' '
767 );
768 $ret .= "<meta name=\"keywords\" content=\"" .
769 htmlspecialchars(preg_replace(array_keys($strip), array_values($strip),implode( ",", $this->mKeywords ))) . "\" />\n";
770 }
771 foreach ( $this->mLinktags as $tag ) {
772 $ret .= '<link';
773 foreach( $tag as $attr => $val ) {
774 $ret .= " $attr=\"" . htmlspecialchars( $val ) . "\"";
775 }
776 $ret .= " />\n";
777 }
778 if( $this->isSyndicated() ) {
779 # FIXME: centralize the mime-type and name information in Feed.php
780 $link = $wgRequest->escapeAppendQuery( 'feed=rss' );
781 $ret .= "<link rel='alternate' type='application/rss+xml' title='RSS 2.0' href='$link' />\n";
782 $link = $wgRequest->escapeAppendQuery( 'feed=atom' );
783 $ret .= "<link rel='alternate' type='application/rss+atom' title='Atom 0.3' href='$link' />\n";
784 }
785 # FIXME: get these working
786 # $fix = htmlspecialchars( $wgStylePath . "/ie-png-fix.js" );
787 # $ret .= "<!--[if gte IE 5.5000]><script type='text/javascript' src='$fix'>< /script><![endif]-->";
788 return $ret;
789 }
790
791 /**
792 * Run any necessary pre-output transformations on the buffer text
793 */
794 function transformBuffer( $options = 0 ) {
795 $this->replaceLinkHolders( $this->mBodytext, $options );
796 }
797
798 /**
799 * Replace <!--LINK--> link placeholders with actual links, in the buffer
800 * Placeholders created in Skin::makeLinkObj()
801 * Returns an array of links found, indexed by PDBK:
802 * 0 - broken
803 * 1 - normal link
804 * 2 - stub
805 * $options is a bit field, RLH_FOR_UPDATE to select for update
806 */
807 function replaceLinkHolders( &$text, $options = 0 ) {
808 global $wgUser, $wgLinkCache, $wgUseOldExistenceCheck;
809
810 if ( $wgUseOldExistenceCheck ) {
811 return array();
812 }
813
814 $fname = 'OutputPage::replaceLinkHolders';
815 wfProfileIn( $fname );
816
817 $titles = array();
818 $pdbks = array();
819 $colours = array();
820
821 # Get placeholders from body
822 wfProfileIn( $fname.'-match' );
823 preg_match_all( "/<!--LINK (.*?) (.*?) (.*?) (.*?)-->/", $text, $tmpLinks );
824 wfProfileOut( $fname.'-match' );
825
826 if ( !empty( $tmpLinks[0] ) ) {
827 wfProfileIn( $fname.'-check' );
828 $dbr =& wfGetDB( DB_SLAVE );
829 $cur = $dbr->tableName( 'cur' );
830 $sk = $wgUser->getSkin();
831 $threshold = $wgUser->getOption('stubthreshold');
832
833 $namespaces =& $tmpLinks[1];
834 $dbkeys =& $tmpLinks[2];
835 $queries =& $tmpLinks[3];
836 $texts =& $tmpLinks[4];
837
838 # Sort by namespace
839 asort( $namespaces );
840
841 # Generate query
842 $query = false;
843 foreach ( $namespaces as $key => $val ) {
844 # Make title object
845 $dbk = $dbkeys[$key];
846 $title = $titles[$key] = Title::makeTitleSafe( $val, $dbk );
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( $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 ( $namespaces as $key => $ns ) {
915 $pdbk = $pdbks[$key];
916 $searchkey = $tmpLinks[0][$key];
917 $title = $titles[$key];
918 if ( empty( $colours[$pdbk] ) ) {
919 $wgLinkCache->addBadLink( $pdbk );
920 $colours[$pdbk] = 0;
921 $outputReplace[$searchkey] = $sk->makeBrokenLinkObj( $title, $texts[$key], $queries[$key] );
922 } elseif ( $colours[$pdbk] == 1 ) {
923 $outputReplace[$searchkey] = $sk->makeKnownLinkObj( $title, $texts[$key], $queries[$key] );
924 } elseif ( $colours[$pdbk] == 2 ) {
925 $outputReplace[$searchkey] = $sk->makeStubLinkObj( $title, $texts[$key], $queries[$key] );
926 }
927 }
928 wfProfileOut( $fname.'-construct' );
929
930 # Do the thing
931 wfProfileIn( $fname.'-replace' );
932
933 $text = preg_replace_callback(
934 '/(<!--LINK .*? .*? .*? .*?-->)/',
935 "outputReplaceMatches",
936 $text);
937 wfProfileOut( $fname.'-replace' );
938 }
939 wfProfileOut( $fname );
940 return $colours;
941 }
942 }
943
944 function &outputReplaceMatches($matches) {
945 global $outputReplace;
946 return $outputReplace[$matches[1]];
947 }
948
949 }
950 ?>