changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / db / zip.php
1 <?php
2
3 /*
4    Beware, this is a fun plugin!  It is supposed to work, but not
5    recommended for seriously big installations.  This database backend
6    stores all your pages in a ZIP file, it requires the standard util
7    "zip" ('pkzip.exe' may not work).
8
9    You must set EWIKI_DB_ZIP and EWIKI_TMP to writable locations (the
10    directory in which the single ZIP file resides must be world-
11    writable).
12    This database plugin is _CASE_INSENSITIVE always, hit counting isn't
13    done. Eventually you even have to create an empty ZIP file yourself.
14    And this will only run in UNIX environments!
15 */
16
17 define("EWIKI_DB_ZIP", "/tmp/database.zip");
18 define("EWIKI_CASE_INSENSITIVE", "always");
19
20 #-- reg
21 $ewiki_plugins["database"][0] = "ewiki_db_zip";
22
23 #-- backend
24 class ewiki_db_zip {
25
26    var $tmp = EWIKI_TMP;
27    var $util = "zip ";
28    var $util_un = "unzip ";
29    var $util_get = "unzip -q -C -p ";
30    var $util_add = "zip -j -q -u ";
31    var $zip = EWIKI_DB_ZIP;
32    var $QUIET = "2>/dev/null";
33
34
35    function ewiki_db_zip() {
36    }
37
38
39    function GET($id, $version=false) {
40       $fn = $this->FN($id);
41       if (!$version) {
42          $version = 0 + trim(`$this->util_get $this->zip $fn $this->QUIET`);
43       }
44       if ($version) {
45          $fn .= ".$version";
46          $r = `$this->util_get $this->zip $fn `;
47          $r = unserialize($r);
48       }
49       return($r);
50    }
51
52
53    function WRITE($hash, $overwrite=0) {
54       $fn = "$this->tmp/" . $this->FN($hash["id"]);
55       $fn2 = "$fn." . $hash["version"];
56       if ($f = fopen($fn2, "w")) {
57          fwrite($f, serialize($hash));     // unsafe, to say mildly
58          fclose($f);
59          if ($f = fopen($fn, "w")) {
60             fwrite($f, $hash["version"]);
61             fclose($f);
62             #-- add to zip
63             $r = `$this->util_add $this->zip $fn2 $fn $this->QUIET`;
64             $r = !$r;
65             @unlink($fn);
66          }
67          @unlink($fn2);
68       }
69       return($r);
70    }
71    
72    
73    function HIT($id) {
74       // nop
75    }
76    
77    
78    function FIND($list) {
79       $r = array();
80       foreach ($list as $id) if ($id) {
81          $r[$id] = 0;
82          $fn = $this->FN($id);
83          if ($ver = `$this->util_get $this->zip $fn $this->QUIET`) {
84             $r[$id] = 1;
85          }
86       }
87       return($r);
88    }
89
90
91    function GETALL($fields, $mask=0, $filter=0) {
92       $r = new ewiki_dbquery_result($fields);
93       foreach (explode("\n", `$this->util_un -l $this->zip | cut -b 29-290`) as $id) {
94          if (!strpos($id, ".") || !preg_match('/\.\d+$/', $id)) {
95             $r->entries[] = rawurldecode($id);
96          }
97       }
98       return($r);
99    }
100
101
102    function SEARCH($field, $content, $ci="i", $regex=0, $mask=0, $filter=0) {
103       $r = new ewiki_dbquery_result($args);
104       if ($ci && !$regex) {
105          $content = strtolower($content);
106       }
107       $ALL = $this->GETALL(array($field), $mask, $filter);
108       while ($row = $ALL->get()) {
109          if ($regex) {
110             $match = preg_match("\007$content\007$ci", $row["field"]);
111          }
112          elseif ($ci) {
113             $match = strpos(strtolower($row[$field]), $content) !== false;
114          }
115          else {
116             $match = strpos($row[$field], $content) !== false;
117          }
118          if ($match) {
119             $r->add($row);
120          }
121       }
122       return($r);
123    }
124
125
126 #   function DELETE($id, $version) {
127 #   }
128
129
130    function INIT() {
131       if (!file_exists($this->zip) || !filesize($this->zip)) {
132          $f = fopen($this->zip, "wb");
133          fwrite($f, "PK\005\006".str_repeat("\000", 18));
134          fclose($f);
135       }
136       if (!is_writeable($this->zip)) {
137          echo "error db_zip: $this->zip is not writeable!\n";
138       }
139    }
140
141
142    function FN($id) {
143       $id = ewiki_lowercase($id);
144       return(rawurlencode($id));
145    }
146
147
148 } // end of class
149
150
151 ?>