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