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