From ff3ed99f17e6204cc4f6331830c53a084f385d9a Mon Sep 17 00:00:00 2001
From: marcel <marcel@rucksackreinigung.de>
Date: Wed, 23 Aug 2006 23:11:49 +0200
Subject: [PATCH] Added bz2 compression support for Attic

darcs-hash:20060823211149-9c1ae-569f295c33dc798a429a373f48cb09122334ea29.gz
---
 conf/dokuwiki.php |  3 ++-
 inc/common.php    |  4 +++-
 inc/io.php        | 26 ++++++++++++++++++++++++++
 inc/pageutils.php | 13 ++++++++++---
 4 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/conf/dokuwiki.php b/conf/dokuwiki.php
index 48f33a940..a86949225 100644
--- a/conf/dokuwiki.php
+++ b/conf/dokuwiki.php
@@ -69,7 +69,8 @@ $conf['sepchar']     = '_';              //word separator character in page name
                                          //  letter, a digit, '_', '-', or '.'.
 $conf['canonical']   = 0;                //Should all URLs use full canonical http://... style?
 $conf['autoplural']  = 0;                //try (non)plural form of nonexisting files?
-$conf['usegzip']     = 1;                //gzip old revisions?
+$conf['compression'] = 'gz';            //compress old revisions: (0: off) ('gz': gnuzip) ('bz2': bzip)
+					 // bz2 generates smaller files, but needs more cpu-power
 $conf['cachetime']   = 60*60*24;         //maximum age for cachefile in seconds (defaults to a day)
 $conf['purgeonadd']  = 1;                //purge cache when a new file is added (needed for up to date links)
 $conf['locktime']    = 15*60;            //maximum age for lockfiles (defaults to 15 minutes)
diff --git a/inc/common.php b/inc/common.php
index 609e0b077..bed5105fb 100644
--- a/inc/common.php
+++ b/inc/common.php
@@ -1185,7 +1185,9 @@ function getRevisions($id){
   $id   = noNS($id);
   $id   = utf8_encodeFN($id);
   $len  = strlen($id);
-  $xlen = ($conf['usegzip']) ? -7 : -4; // length of extension (.txt.gz or .txt)
+  $xlen = 10; // length of timestamp, strlen(time()) would be more correct, 
+              // but i don't expect dokuwiki still running in 287 years ;)
+              // so this will perform better
 
   $revs = array();
   if (is_dir($revd) && $dh = opendir($revd)) {
diff --git a/inc/io.php b/inc/io.php
index a71162c11..f9fbbd103 100644
--- a/inc/io.php
+++ b/inc/io.php
@@ -91,6 +91,8 @@ function io_readFile($file,$clean=true){
   if(@file_exists($file)){
     if(substr($file,-3) == '.gz'){
       $ret = join('',gzfile($file));
+    }else if(substr($file,-4) == '.bz2'){
+      $ret = bzfile($file);
     }else{
       $ret = join('',file($file));
     }
@@ -101,6 +103,21 @@ function io_readFile($file,$clean=true){
     return $ret;
   }
 }
+/**
+* Returns the content of a .bz2 compressed file as string
+* @author marcel senf <marcel@rucksackreinigung.de>
+*/
+
+function bzfile($file){
+  $bz = bzopen($file,"r");
+  while (!feof($bz)){
+    //8192 seems to be the maximum buffersize?
+	  $str = $str . bzread($bz,8192);
+  }
+  bzclose($bz);
+  return $str;
+}
+
 
 /**
  * Used to write out a DokuWiki page to file, and send IO_WIKIPAGE_WRITE events.
@@ -144,6 +161,7 @@ function _io_writeWikiPage_action($data) {
  * will be appended.
  *
  * Uses gzip if extension is .gz
+ * and bz2 if extension is .bz2
  *
  * @author  Andreas Gohr <andi@splitbrain.org>
  * @return bool true on success
@@ -163,6 +181,14 @@ function io_saveFile($file,$content,$append=false){
     }
     gzwrite($fh, $content);
     gzclose($fh);
+  }else if(substr($file,-4) == '.bz2'){
+    $fh = @bzopen($file,$mode);
+    if(!$fh){
+      msg("Writing $file failed", -1);
+      return false;
+    }
+    bzwrite($fh, $content);
+    bzclose($fh);
   }else{
     $fh = @fopen($file,$mode);
     if(!$fh){
diff --git a/inc/pageutils.php b/inc/pageutils.php
index aacee1b13..2055cf2cc 100644
--- a/inc/pageutils.php
+++ b/inc/pageutils.php
@@ -148,9 +148,16 @@ function wikiFN($id,$rev=''){
     $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
   }else{
     $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
-    if($conf['usegzip'] && !@file_exists($fn)){
-      //return gzip if enabled and plaintext doesn't exist
-      $fn .= '.gz';
+    if($conf['compression']){
+      //test for extensions here, we want to read both compressions
+       if (file_exists($fn . '.gz')){
+          $fn .= '.gz';
+       }else if(file_exists($fn . '.bz2')){
+          $fn .= '.bz2';
+       }else{
+          //file doesnt exist yet, so we take the configured extension
+          $fn .= '.' . $conf['compression'];
+       }
     }
   }
   return $fn;
-- 
GitLab