Set language to en for tests.
[mediawiki.git] / includes / OutputPage.php
blobdcfe19f2d1b7185b0ad2b346ca6cbd010ff44b0e
1 <?php
2 /**
3 * @version $Id$
4 * @package MediaWiki
5 */
7 /**
8 * This is not a valid entry point, perform no further processing unless MEDIAWIKI is defined
9 */
10 if( defined( 'MEDIAWIKI' ) ) {
12 # See design.doc
14 if($wgUseTeX) require_once( 'Math.php' );
16 define( 'RLH_FOR_UPDATE', 1 );
18 /**
19 * @todo document
20 * @package MediaWiki
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;
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;
39 /**
40 * Constructor
41 * Initialise private variables
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 = '';
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; }
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; }
71 function addLink( $linkarr ) {
72 # $linkarr should be an associative array of attributes. We'll escape on output.
73 array_push( $this->mLinktags, $linkarr );
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;
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.
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;
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;
102 if( $wgUser->getOption( 'nocache' ) ) {
103 wfDebug( "USER DISABLED CACHE\n", false );
104 return;
107 $lastmod = gmdate( 'D, j M Y H:i:s', wfTimestamp(TS_UNIX, max( $timestamp, $wgUser->mTouched ) ) ) . ' GMT';
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;
129 } else {
130 wfDebug( "We're confused.\n", false );
131 $this->mLastModified = $lastmod;
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 '';
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;
172 $this->setHTMLTitle( $name . ' - ' . wfMsg( 'wikititlesuffix' ) );
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; }
187 function setArticleRelated( $v ) {
188 $this->mIsArticleRelated = $v;
189 if ( !$v ) {
190 $this->mIsarticle = false;
193 function setArticleFlag( $v ) {
194 $this->mIsarticle = $v;
195 if ( $v ) {
196 $this->mIsArticleRelated = $v;
200 function isArticleRelated() { return $this->mIsArticleRelated; }
202 function getLanguageLinks() { return $this->mLanguageLinks; }
203 function addLanguageLinks($newLinkArray) {
204 $this->mLanguageLinks += $newLinkArray;
206 function setLanguageLinks($newLinkArray) {
207 $this->mLanguageLinks = $newLinkArray;
210 function getCategoryLinks() {
211 return $this->mCategoryLinks;
213 function addCategoryLinks($newLinkArray) {
214 $this->mCategoryLinks += $newLinkArray;
216 function setCategoryLinks($newLinkArray) {
217 $this->mCategoryLinks += $newLinkArray;
220 function suppressQuickbar() { $this->mSuppressQuickbar = true; }
221 function isQuickbarSuppressed() { return $this->mSuppressQuickbar; }
223 function addHTML( $text ) { $this->mBodytext .= $text; }
224 function clearHTML() { $this->mBodytext = ''; }
225 function debug( $text ) { $this->mDebugtext .= $text; }
227 function setParserOptions( $options ) {
228 return wfSetVar( $this->mParserOptions, $options );
232 * Convert wikitext to HTML and add it to the buffer
234 function addWikiText( $text, $linestart = true ) {
235 global $wgParser, $wgTitle;
237 $parserOutput = $wgParser->parse( $text, $wgTitle, $this->mParserOptions, $linestart );
238 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
239 $this->mCategoryLinks += $parserOutput->getCategoryLinks();
240 $this->addHTML( $parserOutput->getText() );
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
247 function addPrimaryWikiText( $text, $cacheArticle ) {
248 global $wgParser, $wgParserCache, $wgUser, $wgTitle;
250 $parserOutput = $wgParser->parse( $text, $wgTitle, $this->mParserOptions, true );
252 # Replace link holders
253 $text = $parserOutput->getText();
254 $this->replaceLinkHolders( $text );
255 $parserOutput->setText( $text );
257 if ( $cacheArticle ) {
258 $wgParserCache->save( $parserOutput, $cacheArticle, $wgUser );
261 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
262 $this->mCategoryLinks += $parserOutput->getCategoryLinks();
263 $this->addHTML( $text );
267 * @param $article
268 * @param $user
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;
284 * Set the maximum cache time on the Squid in seconds
285 * @param $maxage
287 function setSquidMaxage( $maxage ) {
288 $this->mSquidMaxage = $maxage;
292 * Use enableClientCache(false) to force it to send nocache headers
293 * @param $state
295 function enableClientCache( $state ) {
296 return wfSetVar( $this->mEnableClientCache, $state );
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 )
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' );
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" );
333 if($this->mLastModified) header( "Last-modified: {$this->mLastModified}" );
334 } else {
335 wfDebug( "** no caching **\n", false );
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' );
346 * Finally, all the text has been munged and accumulated into
347 * the object, let's actually output it:
349 function output() {
350 global $wgUser, $wgLang, $wgDebugComments, $wgCookieExpiration;
351 global $wgInputEncoding, $wgOutputEncoding, $wgContLanguageCode;
352 global $wgDebugRedirects, $wgMimeType, $wgProfiler;
353 if( $this->mDoNothing ){
354 return;
356 $fname = 'OutputPage::output';
357 wfProfileIn( $fname );
359 $sk = $wgUser->getSkin();
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;
367 if( $this->mRedirectCode == '301') {
368 if( !$wgDebugRedirects ) {
369 header("HTTP/1.1 {$this->mRedirectCode} Moved Permanently");
371 $this->mLastModified = gmdate( 'D, j M Y H:i:s' ) . ' GMT';
374 $this->sendCacheControl();
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 );
384 if ( isset( $wgProfiler ) ) { wfDebug( $wgProfiler->getOutput() ); }
385 return;
389 $this->sendCacheControl();
390 # Perform link colouring
391 $this->transformBuffer();
392 $this->replaceLinkHolders( $this->mSubtitle );
394 # Disable temporary placeholders, so that the skin produces HTML
395 $sk->postParseLinkColour( false );
397 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
398 header( 'Content-language: '.$wgContLanguageCode );
400 $exp = time() + $wgCookieExpiration;
401 foreach( $this->mCookies as $name => $val ) {
402 setcookie( $name, $val, $exp, '/' );
405 $sk->outputPage( $this );
406 # flush();
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; }
417 print $outs;
420 function setEncodings() {
421 global $wgInputEncoding, $wgOutputEncoding;
422 global $wgUser, $wgLang;
424 $wgInputEncoding = strtolower( $wgInputEncoding );
426 if( $wgUser->getOption( 'altencoding' ) ) {
427 $wgLang->setAltEncoding();
428 return;
431 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
432 $wgOutputEncoding = strtolower( $wgOutputEncoding );
433 return;
437 # This code is unused anyway!
438 # Commenting out. --bv 2003-11-15
440 $a = explode( ",", $_SERVER['HTTP_ACCEPT_CHARSET'] );
441 $best = 0.0;
442 $bestset = "*";
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;
452 if ( $q > $best ) {
453 $bestset = $set;
454 $best = $q;
457 #if ( "*" == $bestset ) { $bestset = "iso-8859-1"; }
458 if ( "*" == $bestset ) { $bestset = $wgOutputEncoding; }
459 $wgOutputEncoding = strtolower( $bestset );
461 # Disable for now
464 $wgOutputEncoding = $wgInputEncoding;
468 * Returns a HTML comment with the elapsed time since request.
469 * This method has no side effects.
471 function reportTime() {
472 global $wgRequestTime;
474 $now = wfTime();
475 list( $usec, $sec ) = explode( ' ', $wgRequestTime );
476 $start = (float)$sec + (float)$usec;
477 $elapsed = $now - $start;
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;
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'];
492 $com = sprintf( "<!-- Served by %s in %01.2f secs. -->",
493 $hostname, $elapsed );
494 return $com;
498 * Note: these arguments are keys into wfMsg(), not text!
500 function errorpage( $title, $msg ) {
501 global $wgTitle;
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->suppressQuickbar();
511 $this->enableClientCache( false );
512 $this->mRedirect = '';
514 $this->mBodytext = '';
515 $this->addHTML( '<p>' . wfMsg( $msg ) . "</p>\n" );
516 $this->returnToMain( false );
518 $this->output();
519 wfErrorExit();
522 function sysopRequired() {
523 global $wgUser;
525 $this->setPageTitle( wfMsg( 'sysoptitle' ) );
526 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
527 $this->setRobotpolicy( 'noindex,nofollow' );
528 $this->setArticleRelated( false );
529 $this->mBodytext = '';
531 $sk = $wgUser->getSkin();
532 $ap = $sk->makeKnownLink( wfMsg( 'administrators' ), '' );
533 $this->addHTML( wfMsg( 'sysoptext', $ap ) );
534 $this->returnToMain();
537 function developerRequired() {
538 global $wgUser;
540 $this->setPageTitle( wfMsg( 'developertitle' ) );
541 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
542 $this->setRobotpolicy( 'noindex,nofollow' );
543 $this->setArticleRelated( false );
544 $this->mBodytext = '';
546 $sk = $wgUser->getSkin();
547 $ap = $sk->makeKnownLink( wfMsg( 'administrators' ), '' );
548 $this->addHTML( wfMsg( 'developertext', $ap ) );
549 $this->returnToMain();
552 function loginToUse() {
553 global $wgUser, $wgTitle, $wgLang;
555 $this->setPageTitle( wfMsg( 'loginreqtitle' ) );
556 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
557 $this->setRobotpolicy( 'noindex,nofollow' );
558 $this->setArticleFlag( false );
559 $this->mBodytext = '';
560 $this->addWikiText( wfMsg( 'loginreqtext' ) );
562 # We put a comment in the .html file so a Sysop can diagnose the page the
563 # user can't see.
564 $this->addHTML( "\n<!--" .
565 $wgLang->getNsText( $wgTitle->getNamespace() ) .
566 ':' .
567 $wgTitle->getDBkey() . '-->' );
568 $this->returnToMain(); # Flip back to the main page after 10 seconds.
571 function databaseError( $fname, $sql, $error, $errno ) {
572 global $wgUser, $wgCommandLineMode;
574 $this->setPageTitle( wfMsgNoDB( 'databaseerror' ) );
575 $this->setRobotpolicy( 'noindex,nofollow' );
576 $this->setArticleRelated( false );
577 $this->enableClientCache( false );
578 $this->mRedirect = '';
580 if ( $wgCommandLineMode ) {
581 $msg = wfMsgNoDB( 'dberrortextcl', htmlspecialchars( $sql ),
582 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
583 } else {
584 $msg = wfMsgNoDB( 'dberrortext', htmlspecialchars( $sql ),
585 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
588 if ( $wgCommandLineMode || !is_object( $wgUser )) {
589 print $msg."\n";
590 wfErrorExit();
592 $this->mBodytext = $msg;
593 $this->output();
594 wfErrorExit();
597 function readOnlyPage( $source = null, $protected = false ) {
598 global $wgUser, $wgReadOnlyFile;
600 $this->setRobotpolicy( 'noindex,nofollow' );
601 $this->setArticleRelated( false );
603 if( $protected ) {
604 $this->setPageTitle( wfMsg( 'viewsource' ) );
605 $this->addWikiText( wfMsg( 'protectedtext' ) );
606 } else {
607 $this->setPageTitle( wfMsg( 'readonly' ) );
608 $reason = file_get_contents( $wgReadOnlyFile );
609 $this->addWikiText( wfMsg( 'readonlytext', $reason ) );
612 if( is_string( $source ) ) {
613 if( strcmp( $source, '' ) == 0 ) {
614 $source = wfMsg( 'noarticletext' );
616 $rows = $wgUser->getOption( 'rows' );
617 $cols = $wgUser->getOption( 'cols' );
618 $text = "\n<textarea cols='$cols' rows='$rows' readonly='readonly'>" .
619 htmlspecialchars( $source ) . "\n</textarea>";
620 $this->addHTML( $text );
623 $this->returnToMain( false );
626 function fatalError( $message ) {
627 $this->setPageTitle( wfMsg( "internalerror" ) );
628 $this->setRobotpolicy( "noindex,nofollow" );
629 $this->setArticleRelated( false );
630 $this->enableClientCache( false );
631 $this->mRedirect = '';
633 $this->mBodytext = $message;
634 $this->output();
635 wfErrorExit();
638 function unexpectedValueError( $name, $val ) {
639 $this->fatalError( wfMsg( 'unexpected', $name, $val ) );
642 function fileCopyError( $old, $new ) {
643 $this->fatalError( wfMsg( 'filecopyerror', $old, $new ) );
646 function fileRenameError( $old, $new ) {
647 $this->fatalError( wfMsg( 'filerenameerror', $old, $new ) );
650 function fileDeleteError( $name ) {
651 $this->fatalError( wfMsg( 'filedeleteerror', $name ) );
654 function fileNotFoundError( $name ) {
655 $this->fatalError( wfMsg( 'filenotfound', $name ) );
659 * return from error messages or notes
660 * @param $auto automatically redirect the user after 10 seconds
661 * @param $returnto page title to return to. Default is Main Page.
663 function returnToMain( $auto = true, $returnto = NULL ) {
664 global $wgUser, $wgOut, $wgRequest;
666 if ( $returnto == NULL ) {
667 $returnto = $wgRequest->getText( 'returnto' );
670 $sk = $wgUser->getSkin();
671 if ( '' == $returnto ) {
672 $returnto = wfMsg( 'mainpage' );
674 $link = $sk->makeKnownLink( $returnto, '' );
676 $r = wfMsg( 'returnto', $link );
677 if ( $auto ) {
678 $titleObj = Title::newFromText( $returnto );
679 $wgOut->addMeta( 'http:Refresh', '10;url=' . $titleObj->escapeFullURL() );
681 $wgOut->addHTML( "\n<p>$r</p>\n" );
685 * This function takes the existing and broken links for the page
686 * and uses the first 10 of them for META keywords
688 function addMetaTags () {
689 global $wgLinkCache , $wgOut ;
690 $good = array_keys ( $wgLinkCache->mGoodLinks ) ;
691 $bad = array_keys ( $wgLinkCache->mBadLinks ) ;
692 $a = array_merge ( $good , $bad ) ;
693 $a = array_slice ( $a , 0 , 10 ) ; # 10 keywords max
694 $a = implode ( ',' , $a ) ;
695 $strip = array(
696 "/<.*?" . ">/" => '',
697 "/[_]/" => ' '
699 $a = htmlspecialchars(preg_replace(array_keys($strip), array_values($strip),$a ));
701 $wgOut->addMeta ( 'KEYWORDS' , $a ) ;
705 * @private
707 function headElement() {
708 global $wgDocType, $wgDTD, $wgLanguageCode, $wgOutputEncoding, $wgMimeType;
709 global $wgUser, $wgLang, $wgRequest;
711 $xml = ($wgMimeType == 'text/xml');
712 if( $xml ) {
713 $ret = "<" . "?xml version=\"1.0\" encoding=\"$wgOutputEncoding\" ?" . ">\n";
714 } else {
715 $ret = '';
718 $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
720 if ( "" == $this->mHTMLtitle ) {
721 $this->mHTMLtitle = wfMsg( "pagetitle", $this->mPagetitle );
723 if( $xml ) {
724 $xmlbits = "xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\"";
725 } else {
726 $xmlbits = '';
728 $rtl = $wgLang->isRTL() ? " dir='RTL'" : '';
729 $ret .= "<html $xmlbits lang=\"$wgLanguageCode\" $rtl>\n";
730 $ret .= "<head>\n<title>" . htmlspecialchars( $this->mHTMLtitle ) . "</title>\n";
731 array_push( $this->mMetatags, array( "http:Content-type", "$wgMimeType; charset={$wgOutputEncoding}" ) );
733 $ret .= $this->getHeadLinks();
734 global $wgStylePath;
735 if( $this->isPrintable() ) {
736 $media = '';
737 } else {
738 $media = "media='print'";
740 $printsheet = htmlspecialchars( "$wgStylePath/common/wikiprintable.css" );
741 $ret .= "<link rel='stylesheet' type='text/css' $media href='$printsheet' />\n";
743 $sk = $wgUser->getSkin();
744 $ret .= $sk->getHeadScripts();
745 $ret .= $this->mScripts;
746 $ret .= $sk->getUserStyles();
748 $ret .= "</head>\n";
749 return $ret;
752 function getHeadLinks() {
753 global $wgRequest, $wgStylePath;
754 $ret = '';
755 foreach ( $this->mMetatags as $tag ) {
756 if ( 0 == strcasecmp( 'http:', substr( $tag[0], 0, 5 ) ) ) {
757 $a = 'http-equiv';
758 $tag[0] = substr( $tag[0], 5 );
759 } else {
760 $a = 'name';
762 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\" />\n";
764 $p = $this->mRobotpolicy;
765 if ( '' == $p ) { $p = 'index,follow'; }
766 $ret .= "<meta name=\"robots\" content=\"$p\" />\n";
768 if ( count( $this->mKeywords ) > 0 ) {
769 $strip = array(
770 "/<.*?" . ">/" => '',
771 "/[_]/" => ' '
773 $ret .= "<meta name=\"keywords\" content=\"" .
774 htmlspecialchars(preg_replace(array_keys($strip), array_values($strip),implode( ",", $this->mKeywords ))) . "\" />\n";
776 foreach ( $this->mLinktags as $tag ) {
777 $ret .= '<link';
778 foreach( $tag as $attr => $val ) {
779 $ret .= " $attr=\"" . htmlspecialchars( $val ) . "\"";
781 $ret .= " />\n";
783 if( $this->isSyndicated() ) {
784 # FIXME: centralize the mime-type and name information in Feed.php
785 $link = $wgRequest->escapeAppendQuery( 'feed=rss' );
786 $ret .= "<link rel='alternate' type='application/rss+xml' title='RSS 2.0' href='$link' />\n";
787 $link = $wgRequest->escapeAppendQuery( 'feed=atom' );
788 $ret .= "<link rel='alternate' type='application/rss+atom' title='Atom 0.3' href='$link' />\n";
790 # FIXME: get these working
791 # $fix = htmlspecialchars( $wgStylePath . "/ie-png-fix.js" );
792 # $ret .= "<!--[if gte IE 5.5000]><script type='text/javascript' src='$fix'>< /script><![endif]-->";
793 return $ret;
797 * Run any necessary pre-output transformations on the buffer text
799 function transformBuffer( $options = 0 ) {
800 $this->replaceLinkHolders( $this->mBodytext, $options );
804 * Replace <!--LINK--> link placeholders with actual links, in the buffer
805 * Placeholders created in Skin::makeLinkObj()
806 * Returns an array of links found, indexed by PDBK:
807 * 0 - broken
808 * 1 - normal link
809 * 2 - stub
810 * $options is a bit field, RLH_FOR_UPDATE to select for update
812 function replaceLinkHolders( &$text, $options = 0 ) {
813 global $wgUser, $wgLinkCache, $wgUseOldExistenceCheck, $wgLinkHolders;
815 if ( $wgUseOldExistenceCheck ) {
816 return array();
819 $fname = 'OutputPage::replaceLinkHolders';
820 wfProfileIn( $fname );
822 $pdbks = array();
823 $colours = array();
825 #if ( !empty( $tmpLinks[0] ) ) { #TODO
826 if ( !empty( $wgLinkHolders['namespaces'] ) ) {
827 wfProfileIn( $fname.'-check' );
828 $dbr =& wfGetDB( DB_SLAVE );
829 $cur = $dbr->tableName( 'cur' );
830 $sk = $wgUser->getSkin();
831 $threshold = $wgUser->getOption('stubthreshold');
833 # Sort by namespace
834 asort( $wgLinkHolders['namespaces'] );
836 # Generate query
837 $query = false;
838 foreach ( $wgLinkHolders['namespaces'] as $key => $val ) {
839 # Make title object
840 $title = $wgLinkHolders['titles'][$key];
842 # Skip invalid entries.
843 # Result will be ugly, but prevents crash.
844 if ( is_null( $title ) ) {
845 continue;
847 $pdbk = $pdbks[$key] = $title->getPrefixedDBkey();
849 # Check if it's in the link cache already
850 if ( $wgLinkCache->getGoodLinkID( $pdbk ) ) {
851 $colours[$pdbk] = 1;
852 } elseif ( $wgLinkCache->isBadLink( $pdbk ) ) {
853 $colours[$pdbk] = 0;
854 } else {
855 # Not in the link cache, add it to the query
856 if ( !isset( $current ) ) {
857 $current = $val;
858 $query = "SELECT cur_id, cur_namespace, cur_title";
859 if ( $threshold > 0 ) {
860 $query .= ", LENGTH(cur_text) AS cur_len, cur_is_redirect";
862 $query .= " FROM $cur WHERE (cur_namespace=$val AND cur_title IN(";
863 } elseif ( $current != $val ) {
864 $current = $val;
865 $query .= ")) OR (cur_namespace=$val AND cur_title IN(";
866 } else {
867 $query .= ', ';
870 $query .= $dbr->addQuotes( $wgLinkHolders['dbkeys'][$key] );
873 if ( $query ) {
874 $query .= '))';
875 if ( $options & RLH_FOR_UPDATE ) {
876 $query .= ' FOR UPDATE';
879 $res = $dbr->query( $query, $fname );
881 # Fetch data and form into an associative array
882 # non-existent = broken
883 # 1 = known
884 # 2 = stub
885 while ( $s = $dbr->fetchObject($res) ) {
886 $title = Title::makeTitle( $s->cur_namespace, $s->cur_title );
887 $pdbk = $title->getPrefixedDBkey();
888 $wgLinkCache->addGoodLink( $s->cur_id, $pdbk );
890 if ( $threshold > 0 ) {
891 $size = $s->cur_len;
892 if ( $s->cur_is_redirect || $s->cur_namespace != 0 || $length < $threshold ) {
893 $colours[$pdbk] = 1;
894 } else {
895 $colours[$pdbk] = 2;
897 } else {
898 $colours[$pdbk] = 1;
902 wfProfileOut( $fname.'-check' );
904 # Construct search and replace arrays
905 wfProfileIn( $fname.'-construct' );
906 global $outputReplace;
907 $outputReplace = array();
908 foreach ( $wgLinkHolders['namespaces'] as $key => $ns ) {
909 $pdbk = $pdbks[$key];
910 $searchkey = '<!--LINK '.$key.'-->';
911 $title = $wgLinkHolders['titles'][$key];
912 if ( empty( $colours[$pdbk] ) ) {
913 $wgLinkCache->addBadLink( $pdbk );
914 $colours[$pdbk] = 0;
915 $outputReplace[$searchkey] = $sk->makeBrokenLinkObj( $title,
916 $wgLinkHolders['texts'][$key],
917 $wgLinkHolders['queries'][$key] );
918 } elseif ( $colours[$pdbk] == 1 ) {
919 $outputReplace[$searchkey] = $sk->makeKnownLinkObj( $title,
920 $wgLinkHolders['texts'][$key],
921 $wgLinkHolders['queries'][$key] );
922 } elseif ( $colours[$pdbk] == 2 ) {
923 $outputReplace[$searchkey] = $sk->makeStubLinkObj( $title,
924 $wgLinkHolders['texts'][$key],
925 $wgLinkHolders['queries'][$key] );
928 wfProfileOut( $fname.'-construct' );
930 # Do the thing
931 wfProfileIn( $fname.'-replace' );
933 $text = preg_replace_callback(
934 '/(<!--LINK .*?-->)/',
935 "outputReplaceMatches",
936 $text);
937 wfProfileOut( $fname.'-replace' );
939 wfProfileOut( $fname );
940 return $colours;
944 function &outputReplaceMatches($matches) {
945 global $outputReplace;
946 return $outputReplace[$matches[1]];