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