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