* Fix explicit s-maxage=0 on raw pages; should help with proxy issues in
[lhc/web/wiklou.git] / includes / OutputPage.php
index a0e77e4..510b9eb 100644 (file)
@@ -1,23 +1,19 @@
 <?php
+if ( ! defined( 'MEDIAWIKI' ) )
+       die( -1 );
 /**
  * @package MediaWiki
  */
 
-/**
- * This is not a valid entry point, perform no further processing unless MEDIAWIKI is defined
- */
-if( defined( 'MEDIAWIKI' ) ) {
-
-# See design.txt
-
-if($wgUseTeX) require_once( 'Math.php' );
+if ( $wgUseTeX )
+       require_once 'Math.php';
 
 /**
  * @todo document
  * @package MediaWiki
  */
 class OutputPage {
-       var $mHeaders, $mCookies, $mMetatags, $mKeywords;
+       var $mHeaders, $mMetatags, $mKeywords;
        var $mLinktags, $mPagetitle, $mBodytext, $mDebugtext;
        var $mHTMLtitle, $mRobotpolicy, $mIsarticle, $mPrintable;
        var $mSubtitle, $mRedirect, $mStatusCode;
@@ -39,7 +35,7 @@ class OutputPage {
         * Initialise private variables
         */
        function OutputPage() {
-               $this->mHeaders = $this->mCookies = $this->mMetatags =
+               $this->mHeaders = $this->mMetatags =
                $this->mKeywords = $this->mLinktags = array();
                $this->mHTMLtitle = $this->mPagetitle = $this->mBodytext =
                $this->mRedirect = $this->mLastModified =
@@ -59,10 +55,9 @@ class OutputPage {
        }
 
        function addHeader( $name, $val ) { array_push( $this->mHeaders, $name.': '.$val ) ; }
-       function addCookie( $name, $val ) { array_push( $this->mCookies, array( $name, $val ) ); }
        function redirect( $url, $responsecode = '302' ) { $this->mRedirect = $url; $this->mRedirectCode = $responsecode; }
        function setStatusCode( $statusCode ) { $this->mStatusCode = $statusCode; }
-       
+
        # To add an http-equiv meta tag, precede the name with "http:"
        function addMeta( $name, $val ) { array_push( $this->mMetatags, array( $name, $val ) ); }
        function addKeyword( $text ) { array_push( $this->mKeywords, $text ); }
@@ -93,7 +88,7 @@ class OutputPage {
         * returns true iff cache-ok headers was sent.
         */
        function checkLastModified ( $timestamp ) {
-               global $wgLang, $wgCachePages, $wgUser;
+               global $wgCachePages, $wgCacheEpoch, $wgUser;
                if ( !$timestamp || $timestamp == '19700101000000' ) {
                        wfDebug( "CACHE DISABLED, NO TIMESTAMP\n" );
                        return;
@@ -108,7 +103,7 @@ class OutputPage {
                }
 
                $timestamp=wfTimestamp(TS_MW,$timestamp);
-               $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched ) );
+               $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched, $wgCacheEpoch ) );
 
                if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
                        # IE sends sizes after the date like this:
@@ -119,17 +114,17 @@ class OutputPage {
                        $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
                        wfDebug( "-- client send If-Modified-Since: " . $modsince . "\n", false );
                        wfDebug( "--  we might send Last-Modified : $lastmod\n", false );
-                       if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) ) {
+                       if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
                                # Make sure you're in a place you can leave when you call us!
                                header( "HTTP/1.0 304 Not Modified" );
                                $this->mLastModified = $lastmod;
                                $this->sendCacheControl();
-                               wfDebug( "CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
+                               wfDebug( "CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
                                $this->disable();
                                @ob_end_clean(); // Don't output compressed blob
                                return true;
                        } else {
-                               wfDebug( "READY  client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
+                               wfDebug( "READY  client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
                                $this->mLastModified = $lastmod;
                        }
                } else {
@@ -142,20 +137,17 @@ class OutputPage {
                global $action;
                switch($action) {
                        case 'edit':
-                               return wfMsg('edit');
-                       case 'history':
-                               return wfMsg('history_short');
+                       case 'delete':
                        case 'protect':
-                               return wfMsg('protect');
                        case 'unprotect':
-                               return wfMsg('unprotect');
-                       case 'delete':
-                               return wfMsg('delete');
                        case 'watch':
-                               return wfMsg('watch');
                        case 'unwatch':
-                               return wfMsg('unwatch');
+                               // Display title is already customized
+                               return '';
+                       case 'history':
+                               return wfMsg('history_short');
                        case 'submit':
+                               // FIXME: bug 2735; not correct for special pages etc
                                return wfMsg('preview');
                        case 'info':
                                return wfMsg('info_short');
@@ -176,7 +168,7 @@ class OutputPage {
                                $name .= ' - '.$taction;
                        }
                }
-               
+
                $this->setHTMLTitle( wfMsg( 'pagetitle', $name ) );
        }
        function getHTMLTitle() { return $this->mHTMLtitle; }
@@ -218,11 +210,30 @@ class OutputPage {
        function getCategoryLinks() {
                return $this->mCategoryLinks;
        }
-       function addCategoryLinks($newLinkArray) {
-               $this->mCategoryLinks += $newLinkArray;
+
+       /**
+        * Add an array of categories, with names in the keys
+        */
+       function addCategoryLinks($categories) {
+               global $wgUser, $wgContLang;
+
+               # Add the links to the link cache in a batch
+               $arr = array( NS_CATEGORY => $categories );
+               $lb = new LinkBatch;
+               $lb->setArray( $arr );
+               $lb->execute();
+
+               $sk =& $wgUser->getSkin();
+               foreach ( $categories as $category => $arbitrary ) {
+                       $title = Title::makeTitleSafe( NS_CATEGORY, $category );
+                       $text = $wgContLang->convertHtml( $title->getText() );
+                       $this->mCategoryLinks[] = $sk->makeLinkObj( $title, $text );
+               }
        }
-       function setCategoryLinks($newLinkArray) {
-               $this->mCategoryLinks += $newLinkArray;
+
+       function setCategoryLinks($categories) {
+               $this->mCategoryLinks = array();
+               $this->addCategoryLinks($categories);
        }
 
        function suppressQuickbar() { $this->mSuppressQuickbar = true; }
@@ -233,10 +244,15 @@ class OutputPage {
        function getHTML() { return $this->mBodytext; }
        function debug( $text ) { $this->mDebugtext .= $text; }
 
+       /* @deprecated */
        function setParserOptions( $options ) {
+               return $this->ParserOptions( $options );
+       }
+
+       function ParserOptions( $options = null ) {
                return wfSetVar( $this->mParserOptions, $options );
        }
-       
+
        /**
         * Set the revision ID which will be seen by the wiki text parser
         * for things such as embedded {{REVISIONID}} variable use.
@@ -263,14 +279,23 @@ class OutputPage {
        }
 
        function addWikiTextTitle($text, &$title, $linestart) {
-               global $wgParser, $wgUseTidy;
+               global $wgParser;
                $parserOutput = $wgParser->parse( $text, $title, $this->mParserOptions,
                        $linestart, true, $this->mRevisionId );
+               $this->addParserOutput( $parserOutput );
+       }
+
+       function addParserOutputNoText( &$parserOutput ) {
                $this->mLanguageLinks += $parserOutput->getLanguageLinks();
-               $this->mCategoryLinks += $parserOutput->getCategoryLinks();
+               $this->addCategoryLinks( $parserOutput->getCategories() );
+               $this->addKeywords( $parserOutput );
                if ( $parserOutput->getCacheTime() == -1 ) {
                        $this->enableClientCache( false );
                }
+       }
+
+       function addParserOutput( &$parserOutput ) {
+               $this->addParserOutputNoText( $parserOutput );
                $this->addHTML( $parserOutput->getText() );
        }
 
@@ -278,26 +303,36 @@ class OutputPage {
         * Add wikitext to the buffer, assuming that this is the primary text for a page view
         * Saves the text into the parser cache if possible
         */
-       function addPrimaryWikiText( $text, $cacheArticle ) {
-               global $wgParser, $wgParserCache, $wgUser, $wgUseTidy;
+       function addPrimaryWikiText( $text, $article, $cache = true ) {
+               global $wgParser, $wgUser;
 
-               $parserOutput = $wgParser->parse( $text, $cacheArticle->mTitle,
+               $this->mParserOptions->setTidy(true);
+               $parserOutput = $wgParser->parse( $text, $article->mTitle,
                        $this->mParserOptions, true, true, $this->mRevisionId );
-
-               $text = $parserOutput->getText();
-
-               if ( $cacheArticle && $parserOutput->getCacheTime() != -1 ) {
-                       $wgParserCache->save( $parserOutput, $cacheArticle, $wgUser );
+               $this->mParserOptions->setTidy(false);
+               if ( $cache && $article && $parserOutput->getCacheTime() != -1 ) {
+                       $parserCache =& ParserCache::singleton();
+                       $parserCache->save( $parserOutput, $article, $wgUser );
                }
 
-               $this->mLanguageLinks += $parserOutput->getLanguageLinks();
-               $this->mCategoryLinks += $parserOutput->getCategoryLinks();
-               if ( $parserOutput->getCacheTime() == -1 ) {
-                       $this->enableClientCache( false );
-               }
-               $this->addHTML( $text );
+               $this->addParserOutputNoText( $parserOutput );
+               $text = $parserOutput->getText();
+               wfRunHooks( 'OutputPageBeforeHTML',array( &$this, &$text ) );
+               $parserOutput->setText( $text );
+               $this->addHTML( $parserOutput->getText() );
+       }
+
+       /**
+        * For anything that isn't primary text or interface message
+        */
+       function addSecondaryWikiText( $text, $linestart = true ) {
+               global $wgTitle;
+               $this->mParserOptions->setTidy(true);
+               $this->addWikiTextTitle($text, $wgTitle, $linestart);
+               $this->mParserOptions->setTidy(false);
        }
 
+
        /**
         * Add the output of a QuickTemplate to the output buffer
         * @param QuickTemplate $template
@@ -305,7 +340,7 @@ class OutputPage {
        function addTemplate( &$template ) {
                ob_start();
                $template->execute();
-               $this->addHtml( ob_get_contents() );
+               $this->addHTML( ob_get_contents() );
                ob_end_clean();
        }
 
@@ -326,12 +361,15 @@ class OutputPage {
         * @return bool
         */
        function tryParserCache( $article, $user ) {
-               global $wgParserCache;
-               $parserOutput = $wgParserCache->get( $article, $user );
+               $parserCache =& ParserCache::singleton();
+               $parserOutput = $parserCache->get( $article, $user );
                if ( $parserOutput !== false ) {
                        $this->mLanguageLinks += $parserOutput->getLanguageLinks();
-                       $this->mCategoryLinks += $parserOutput->getCategoryLinks();
-                       $this->addHTML( $parserOutput->getText() );
+                       $this->addCategoryLinks( $parserOutput->getCategories() );
+                       $this->addKeywords( $parserOutput );
+                       $text = $parserOutput->getText();
+                       wfRunHooks( 'OutputPageBeforeHTML', array( &$this, &$text ) );
+                       $this->addHTML( $text );
                        $t = $parserOutput->getTitleText();
                        if( !empty( $t ) ) {
                                $this->setPageTitle( $t );
@@ -365,7 +403,7 @@ class OutputPage {
        }
 
        function sendCacheControl() {
-               global $wgUseSquid, $wgUseESI;
+               global $wgUseSquid, $wgUseESI, $wgSquidMaxage;
 
                if ($this->mETag)
                        header("ETag: $this->mETag");
@@ -420,9 +458,8 @@ class OutputPage {
         * the object, let's actually output it:
         */
        function output() {
-               global $wgUser, $wgLang, $wgDebugComments, $wgCookieExpiration;
-               global $wgInputEncoding, $wgOutputEncoding, $wgContLanguageCode;
-               global $wgDebugRedirects, $wgMimeType, $wgProfiler;
+               global $wgUser, $wgOutputEncoding;
+               global $wgContLanguageCode, $wgDebugRedirects, $wgMimeType, $wgProfiler;
 
                if( $this->mDoNothing ){
                        return;
@@ -516,19 +553,12 @@ class OutputPage {
                # Buffer output; final headers may depend on later processing
                ob_start();
 
-               $this->transformBuffer();
-
                # Disable temporary placeholders, so that the skin produces HTML
                $sk->postParseLinkColour( false );
 
                header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
                header( 'Content-language: '.$wgContLanguageCode );
 
-               $exp = time() + $wgCookieExpiration;
-               foreach( $this->mCookies as $name => $val ) {
-                       setcookie( $name, $val, $exp, '/' );
-               }
-
                if ($this->mArticleBodyOnly) {
                        $this->out($this->mBodytext);
                } else {
@@ -583,6 +613,31 @@ class OutputPage {
                return $time;
        }
 
+       /**
+        * Produce a "user is blocked" page
+        */
+       function blockedPage() {
+               global $wgUser, $wgContLang;
+
+               $this->setPageTitle( wfMsg( 'blockedtitle' ) );
+               $this->setRobotpolicy( 'noindex,nofollow' );
+               $this->setArticleRelated( false );
+
+               $id = $wgUser->blockedBy();
+               $reason = $wgUser->blockedFor();
+               $ip = wfGetIP();
+
+               if ( is_numeric( $id ) ) {
+                       $name = User::whoIs( $id );
+               } else {
+                       $name = $id;
+               }
+               $link = '[[' . $wgContLang->getNsText( NS_USER ) . ":{$name}|{$name}]]";
+
+               $this->addWikiText( wfMsg( 'blockedtext', $link, $reason, $ip, $name ) );
+               $this->returnToMain( false );
+       }
+
        /**
         * Note: these arguments are keys into wfMsg(), not text!
         */
@@ -621,7 +676,6 @@ class OutputPage {
                $this->setArticleRelated( false );
                $this->mBodytext = '';
 
-               $sk = $wgUser->getSkin();
                $this->addWikiText( wfMsg( 'versionrequiredtext', $version ) );
                $this->returnToMain();
        }
@@ -692,7 +746,7 @@ class OutputPage {
                $this->mBodytext = '';
                $loginpage = Title::makeTitle(NS_SPECIAL, 'Userlogin');
                $sk = $wgUser->getSkin();
-               $loginlink = $sk->makeKnownLinkObj($loginpage, wfMsg('loginreqlink'), 
+               $loginlink = $sk->makeKnownLinkObj($loginpage, wfMsg('loginreqlink'),
                        'returnto=' . htmlspecialchars($wgTitle->getPrefixedDBkey()));
                $this->addHTML( wfMsgHtml( 'loginreqpagetext', $loginlink ) );
 
@@ -756,7 +810,12 @@ class OutputPage {
 
                if( is_string( $source ) ) {
                        if( strcmp( $source, '' ) == 0 ) {
-                               $source = wfMsg( $wgUser->isLoggedIn() ? 'noarticletext' : 'noarticletextanon' );
+                               global $wgTitle ;
+                               if ( $wgTitle->getNamespace() == NS_MEDIAWIKI ) {
+                                       $source = wfMsgWeirdKey ( $wgTitle->getText() ) ;
+                               } else {
+                                       $source = wfMsg( $wgUser->isLoggedIn() ? 'noarticletext' : 'noarticletextanon' );
+                               }
                        }
                        $rows = $wgUser->getOption( 'rows' );
                        $cols = $wgUser->getOption( 'cols' );
@@ -817,7 +876,7 @@ class OutputPage {
                if ( '' == $returnto ) {
                        $returnto = wfMsgForContent( 'mainpage' );
                }
-               $link = $sk->makeKnownLink( $returnto, '' );
+               $link = $sk->makeLinkObj( Title::newFromText( $returnto ), '' );
 
                $r = wfMsg( 'returnto', $link );
                if ( $auto ) {
@@ -831,25 +890,23 @@ class OutputPage {
         * This function takes the title (first item of mGoodLinks), categories, existing and broken links for the page
         * and uses the first 10 of them for META keywords
         */
-       function addMetaTags () {
-               global $wgLinkCache , $wgOut ;
-               $categories = array_keys ( $wgLinkCache->mCategoryLinks ) ;
-               $good = array_keys ( $wgLinkCache->mGoodLinks ) ;
-               $bad = array_keys ( $wgLinkCache->mBadLinks ) ;
-               $a = array_merge ( array_slice ( $good , 0 , 1 ), $categories, array_slice ( $good , 1 , 9 ) , $bad ) ;
-               $a = array_slice ( $a , 0 , 10 ) ; # 10 keywords max
-               $a = implode ( ',' , $a ) ;
-               $strip = array(
-                       "/<.*?>/" => '',
-                       "/_/" => ' '
-               );
-               $a = htmlspecialchars(preg_replace(array_keys($strip), array_values($strip),$a ));
-
-               $wgOut->addMeta ( 'KEYWORDS' , $a ) ;
+       function addKeywords( &$parserOutput ) {
+               global $wgTitle;
+               $this->addKeyword( $wgTitle->getPrefixedText() );
+               $count = 1;
+               $links2d =& $parserOutput->getLinks();
+               foreach ( $links2d as $ns => $dbkeys ) {
+                       foreach( $dbkeys as $dbkey => $id ) {
+                               $this->addKeyword( $dbkey );
+                               if ( ++$count > 10 ) {
+                                       break 2;
+                               }
+                       }
+               }
        }
 
        /**
-        * @private
+        * @access private
         * @return string
         */
        function headElement() {
@@ -907,9 +964,13 @@ class OutputPage {
                        }
                        $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\" />\n";
                }
+
                $p = $this->mRobotpolicy;
-               if ( '' == $p ) { $p = 'index,follow'; }
-               $ret .= "<meta name=\"robots\" content=\"$p\" />\n";
+               if( $p !== '' && $p != 'index,follow' ) {
+                       // http://www.robotstxt.org/wc/meta-user.html
+                       // Only show if it's different from the default robots policy
+                       $ret .= "<meta name=\"robots\" content=\"$p\" />\n";
+               }
 
                if ( count( $this->mKeywords ) > 0 ) {
                        $strip = array(
@@ -937,13 +998,6 @@ class OutputPage {
                return $ret;
        }
 
-       /**
-        * Run any necessary pre-output transformations on the buffer text
-        */
-       function transformBuffer( $options = 0 ) {
-       }
-
-
        /**
         * Turn off regular page output and return an error reponse
         * for when rate limiting has triggered.
@@ -959,7 +1013,4 @@ class OutputPage {
        }
 
 }
-
-} // MediaWiki
-
 ?>