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