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