cache tweaks
[lhc/web/wiklou.git] / includes / Article.php
index 996ef23..17226b3 100644 (file)
@@ -50,7 +50,7 @@ class Article {
 
        function getContent( $noredir = false )
        {
-               global $action,$wgTitle; # From query string
+               global $action,$section,$count,$wgTitle; # From query string
                wfProfileIn( "Article::getContent" );
 
                if ( 0 == $this->getID() ) {
@@ -75,7 +75,20 @@ class Article {
                                ) 
                                {
                                return $this->mContent . "\n" .wfMsg("anontalkpagetext"); }
-                       else {
+                       else {                          
+                               if($action=="edit") {
+                                       if($section!="") {
+
+                                               $secs=preg_split("/(^=+.*?=+)/m",
+                                                $this->mContent, -1,
+                                                PREG_SPLIT_DELIM_CAPTURE);
+                                               if($section==0) {
+                                                       return trim($secs[0]);
+                                               } else {
+                                                       return trim($secs[$section*2-1] . $secs[$section*2]);
+                                               }
+                                       }
+                               }
                                return $this->mContent;
                        }
                }
@@ -367,10 +380,12 @@ class Article {
                global $wgOut, $wgUser, $wgTitle;
                global $wpTextbox1, $wpSummary, $wpWatchthis;
                global $wpSave, $wpPreview;
-               global $wpMinoredit, $wpEdittime, $wpTextbox2;
-               global $oldid, $redirect;
+               global $wpMinoredit, $wpEdittime, $wpTextbox2, $wpSection;
+               global $oldid, $redirect, $section;
                global $wgLang;
 
+               if(isset($wpSection)) { $section=$wpSection; }
+
                $sk = $wgUser->getSkin();
                $isConflict = false;
                $wpTextbox1 = rtrim ( $wpTextbox1 ) ; # To avoid text getting longer on each preview
@@ -424,7 +439,7 @@ class Article {
                        }
                        if ( ! $isConflict ) {
                                # All's well: update the article here
-                               $this->updateArticle( $wpTextbox1, $wpSummary, $wpMinoredit, $wpWatchthis );
+                               $this->updateArticle( $wpTextbox1, $wpSummary, $wpMinoredit, $wpWatchthis, $wpSection );
                                return;
                        }
                }
@@ -451,6 +466,8 @@ class Article {
                } else {
                        $s = str_replace( "$1", $wgTitle->getPrefixedText(),
                          wfMsg( "editing" ) );
+
+                       if($section!="") { $s.=wfMsg("sectionedit");}
                        $wgOut->setPageTitle( $s );
                        if ( $oldid ) {
                                $this->setOldSubtitle();
@@ -536,6 +553,7 @@ class Article {
                                $wgOut->addHTML($previewhead);
                                $wgOut->addWikiText( $this->preSaveTransform( $previewtext ) ."\n\n");
                        }
+                       $wgOut->addHTML( "<br clear=\"all\" />\n" );
                }
                $wgOut->addHTML( "
 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
@@ -552,6 +570,7 @@ name=\"wpSummary\" maxlength=200 size=60><br>
 <input tabindex=6 type=submit value=\"{$prev}\" name=\"wpPreview\">
 <em>{$cancel}</em> | <em>{$edithelp}</em>
 <br><br>{$copywarn}
+<input type=hidden value=\"{$section}\" name=\"wpSection\">
 <input type=hidden value=\"{$wpEdittime}\" name=\"wpEdittime\">\n" );
 
                if ( $isConflict ) {
@@ -591,6 +610,8 @@ name=\"wpSummary\" maxlength=200 size=60><br>
 
                $now = wfTimestampNow();
                $won = wfInvertTimestamp( $now );
+               wfSeedRandom();
+               $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
                $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
                  "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
                  "cur_restrictions,cur_user_text,cur_is_redirect," .
@@ -599,7 +620,7 @@ name=\"wpSummary\" maxlength=200 size=60><br>
                  wfStrencode( $summary ) . "', '" .
                  $wgUser->getID() . "', '{$now}', " .
                  ( $isminor ? 1 : 0 ) . ", 0, '', '" .
-                 wfStrencode( $wgUser->getName() ) . "', $redir, 1, RAND(), '{$now}', '{$won}')";
+                 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
                $res = wfQuery( $sql, $fname );
 
                $newid = wfInsertId();
@@ -625,12 +646,21 @@ name=\"wpSummary\" maxlength=200 size=60><br>
                $this->showArticle( $text, wfMsg( "newarticle" ) );
        }
 
-       function updateArticle( $text, $summary, $minor, $watchthis )
+       function updateArticle( $text, $summary, $minor, $watchthis, $section )
        {
                global $wgOut, $wgUser, $wgTitle, $wgLinkCache;
                global $wgDBtransactions;
                $fname = "Article::updateArticle";
 
+               // insert updated section into old text if we have only edited part 
+               // of the article
+               if ($section != "") {
+                       $oldtext=$this->getContent();
+                       $secs=preg_split("/(^=+.*?=+)/m",$oldtext,-1,PREG_SPLIT_DELIM_CAPTURE);
+                       $secs[$section*2]=$text."\n\n"; // replace with edited
+                       if($section) { $secs[$section*2-1]=""; } // erase old headline
+                       $text=join("",$secs);           
+               }
                if ( $this->mMinorEdit ) { $me1 = 1; } else { $me1 = 0; }
                if ( $minor ) { $me2 = 1; } else { $me2 = 0; }          
                if ( preg_match( "/^(#redirect[^\\n]+)/i", $text, $m ) ) {
@@ -721,10 +751,18 @@ name=\"wpSummary\" maxlength=200 size=60><br>
 
        function showArticle( $text, $subtitle )
        {
-               global $wgOut, $wgTitle, $wgUser, $wgLinkCache;
+               global $wgOut, $wgTitle, $wgUser, $wgLinkCache, $wgUseBetterLinksUpdate;
 
                $wgLinkCache = new LinkCache();
-               $wgOut->addWikiText( $text ); # Just to update links
+
+               # Get old version of link table to allow incremental link updates
+               if ( $wgUseBetterLinksUpdate ) {
+                       $wgLinkCache->preFill( $wgTitle );
+                       $wgLinkCache->clear();
+               }
+
+               # Now update the link cache by parsing the text
+               $wgOut->addWikiText( $text );
 
                $this->editUpdates( $text );
                if( preg_match( "/^#redirect/i", $text ) )
@@ -1018,7 +1056,7 @@ name=\"wpSummary\" maxlength=200 size=60><br>
                        # we select the text because it might be useful below
                        $sql="SELECT old_text FROM old WHERE old_namespace=0 and old_title='" . wfStrencode($wgTitle->getPrefixedDBkey())."' ORDER BY inverse_timestamp LIMIT 1";
                        $res=wfQuery($sql,$fname);
-                       if( $old=wfFetchObject($res)) {
+                       if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
                                $skin=$wgUser->getSkin();
                                $wgOut->addHTML("<B>".wfMsg("historywarning"));
                                $wgOut->addHTML( $skin->historyLink() ."</B><P>");
@@ -1045,10 +1083,10 @@ name=\"wpSummary\" maxlength=200 size=60><br>
                                
                                # this should not happen, since it is not possible to store an empty, new
                                # page. Let's insert a standard text in case it does, though
-                               if($length==0) { $wpreason=wfmsg("exblank");}
+                               if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
                                
                                
-                               if($length < 500) {
+                               if($length < 500 && !$wpReason) {
                                                                                
                                        # comment field=255, let's grep the first 150 to have some user
                                        # space left
@@ -1500,7 +1538,8 @@ name=\"wpSummary\" maxlength=200 size=60><br>
                if(isset($wgLocaltimezone)) {
                        $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
                }
-               $d = $wgLang->timeanddate( wfTimestampNow(), false ) .
+               /* Note: this is an ugly timezone hack for the European wikis */
+               $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
                  " (" . date( "T" ) . ")";
                if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
 
@@ -1602,17 +1641,18 @@ name=\"wpSummary\" maxlength=200 size=60><br>
                global $wgUser, $wgCacheEpoch;
                if(!file_exists( $fn = $this->fileCacheName() ) ) return false;
                $cachetime = wfUnix2Timestamp( filemtime( $fn ) );
-               $good = ( $this->mTouched <= $cachetime ) &&
-                       ($wgCacheEpoch <= $cachetime );
+               $good = (( $this->mTouched <= $cachetime ) &&
+                       ($wgCacheEpoch <= $cachetime ));
         wfDebug(" isFileCacheGood() - cachetime $cachetime, touched {$this->mTouched} epoch {$wgCacheEpoch}, good $good\n");
                return $good;
        }
 
        function loadFromFileCache() {
-               global $wgUseGzip;
+               global $wgUseGzip, $wgOut;
                wfDebug(" loadFromFileCache()\n");
                $filename=$this->fileCacheName();
                $filenamegz = "{$filename}.gz";
+               $wgOut->sendCacheControl();
                if( $wgUseGzip
                        && wfClientAcceptsGzip()
                        && file_exists( $filenamegz)
@@ -1628,12 +1668,13 @@ name=\"wpSummary\" maxlength=200 size=60><br>
        function saveToFileCache( $text ) {
                global $wgUseGzip, $wgCompressByDefault;
                
-        wfDebug(" saveToFileCache()\n");
+        wfDebug(" saveToFileCache()\n", false);
                $filename=$this->fileCacheName();
                 $mydir2=substr($filename,0,strrpos($filename,"/")); # subdirectory level 2
                $mydir1=substr($mydir2,0,strrpos($mydir2,"/")); # subdirectory level 1
-               if(!file_exists($mydir1)) { mkdir($mydir1,0777); } # create if necessary
-               if(!file_exists($mydir2)) { mkdir($mydir2,0777); }                      
+               if(!file_exists($mydir1)) { mkdir($mydir1,0775); } # create if necessary
+               if(!file_exists($mydir2)) { mkdir($mydir2,0775); }
+               
                $f = fopen( $filename, "w" );
                if($f) {
                        $now = wfTimestampNow();
@@ -1664,9 +1705,12 @@ name=\"wpSummary\" maxlength=200 size=60><br>
                                } else {
                                        wfDebug("  failed to write gzip, still sending\n" );
                                }
-                               header( "Content-Encoding: gzip" );
-                               header( "Vary: Accept-Encoding" );
-                               return $gzout;
+                               if(wfClientAcceptsGzip()) {
+                                       header( "Content-Encoding: gzip" );
+                                       header( "Vary: Accept-Encoding" );
+                                       wfDebug("  sending NEW gzip now...\n" );
+                                       return $gzout;
+                               }
                        }
                }
                return $text;