Fix a possible race condition in the PaintWeb DML code.
[moodle/mihaisucan.git] / mod / wiki / ewikimoodlelib.php
blobdb9a7ffa57ea052b777f6e7fd8430691b634f738
1 <?php
2 /* MySQL database backend
3 (Glue between Moodle and ewiki Database)
5 Adapted by Michael Schneider
6 */
8 /// Glue
9 $ewiki_plugins["database"][0] = "ewiki_database_moodle";
11 /// #-- predefine some of the configuration constants
12 define("EWIKI_NAME", $wiki_entry->pagename);
14 define("EWIKI_CONTROL_LINE", 0);
15 define("EWIKI_LIST_LIMIT", 25);
16 define("EWIKI_DEFAULT_LANG", current_language());
17 define("EWIKI_HTML_CHARS", 1);
18 define("EWIKI_DB_TABLE_NAME", "wiki_pages");
21 function ewiki_database_moodle($action, &$args, $sw1, $sw2) {
22 global $wiki, $wiki_entry, $CFG;
23 #-- result array
24 $r = array();
26 switch($action) {
28 /* Returns database entry as array for the page whose name was given
29 with the "id" key in the $args array, usually fetches the latest
30 version of a page, unless a specific "version" was requested in
31 the $args array.
33 # Ugly, but we need to choose which wiki we are about to change/read
34 case "GET":
35 $id = "'" . anydb_escape_string($args["id"]) . "'";
36 ($version = 0 + @$args["version"]) and ($version = "AND (version=$version)") or ($version="");
38 # $result = mysql_query("SELECT * FROM " . EWIKI_DB_TABLE_NAME
39 # . " WHERE (pagename=$id) $version ORDER BY version DESC LIMIT 1"
40 #);
41 #if ($result && ($r = mysql_fetch_array($result, MYSQL_ASSOC))) {
42 # $r["id"] = $r["pagename"];
43 # unset($r["pagename"]);
45 #if (strlen($r["meta"])) {
46 # $r["meta"] = @unserialize($r["meta"]);
49 $select="(pagename=$id) AND wiki=".$wiki_entry->id." $version ";
50 $sort="version DESC";
51 if ($result_arr = get_records_select(EWIKI_DB_TABLE_NAME, $select,$sort,"*",0,1)) {
52 //Iterate to get the first (and unique!)
53 foreach ($result_arr as $obj) {
54 $result_obj = $obj;
57 if($result_obj) {
58 //Convert to array
59 $r=get_object_vars($result_obj);
60 $r["id"] = $r["pagename"];
61 unset($r["pagename"]);
62 $r["meta"] = @unserialize($r["meta"]);
64 break;
68 /* Increases the hit counter for the page name given in $args array
69 with "id" index key.
71 case "HIT":
72 #mysql_query("UPDATE " . EWIKI_DB_TABLE_NAME . " SET hits=(hits+1) WHERE pagename='" . anydb_escape_string($args["id"]) . "'");
73 # set_field does not work because of the "hits+1" construct
74 #print "DO ".anydb__escape_string($args["id"]); exit;
75 execute_sql("UPDATE " .$CFG->prefix.EWIKI_DB_TABLE_NAME . " SET hits=(hits+1) WHERE pagename='" . anydb_escape_string($args["id"]) . "' and wiki=".$wiki_entry->id, 0);
76 break;
77 /* Stores the $data array into the database, while not overwriting
78 existing entries (using WRITE); returns 0 on failure and 1 if
79 saved correctly.
81 case "OVERWRITE":
82 $COMMAND = "REPLACE";
83 break;
85 case "WRITE":
86 $COMMAND="WRITE";
87 $args["pagename"] = $args["id"];
88 unset($args["id"]);
90 if (is_array($args["meta"])) {
91 $args["meta"] = serialize($args["meta"]);
94 #$sql1 = $sql2 = "";
95 #foreach ($args as $index => $value) {
96 # if (is_int($index)) {
97 # continue;
98 # }
99 # $a = ($sql1 ? ', ' : '');
100 # $sql1 .= $a . $index;
101 # $sql2 .= $a . "'" . anydb_escape_string($value) . "'";
104 #strlen(@$COMMAND) || ($COMMAND = "INSERT");
106 foreach ($args as $index => $value) {
107 if (is_int($index)) {
108 continue;
110 $args[$index] =anydb_escape_string($value);
112 $args["wiki"]=$wiki_entry->id;
114 # Check if Record exists
115 if($COMMAND=="REPLACE") {
116 if(count_records(EWIKI_DB_TABLE_NAME,"wiki", $wiki_entry->id,"pagename",$args["pagename"],"version",$args["version"])) {
117 delete_record(EWIKI_DB_TABLE_NAME,"wiki", $wiki_entry->id,"pagename",$args["pagename"],"version",$args["version"]);
121 # Write
122 $result=insert_record(EWIKI_DB_TABLE_NAME,(object)$args,false);
124 #$result = mysql_query("$COMMAND INTO " . EWIKI_DB_TABLE_NAME .
125 # " (" . $sql1 . ") VALUES (" . $sql2 . ")"
127 #return($result && mysql_affected_rows() ?1:0);
129 return $result;
130 break;
134 /* Checks for existence of the WikiPages whose names are given in
135 the $args array. Returns an array with the specified WikiPageNames
136 associated with values of "0" or "1" (stating if the page exists
137 in the database). For images/binary db entries returns the "meta"
138 field instead of an "1".
140 case "FIND":
141 $select = "";
142 foreach (array_values($args) as $id) {
143 if (strlen($id)) {
144 $r[$id] = 0;
145 $select .= ($select ? " OR " : "") .
146 "(pagename='" . anydb_escape_string($id) . "')";
149 if($select) {
150 $select = "(".$select.") AND wiki=".$wiki_entry->id;
151 $result = get_records_select(EWIKI_DB_TABLE_NAME,$select);
152 #$sql = "SELECT pagename AS id, meta FROM " .
153 # EWIKI_DB_TABLE_NAME . " WHERE $sql "
155 #while ($result && ($row = mysql_fetch_row($result))) {
156 # $r[$row[0]] = strpos($row[1], 's:5:"image"') ? $row[1] : 1;
158 while(list($key, $val) = @each($result)) {
159 $r[$val->pagename]=strpos($val->meta, 's:5:"image"') ? $val->meta : 1;
162 break;
164 /* Counts the number of Versions
166 case "COUNTVERSIONS":
167 $sql= "SELECT pagename AS id, count(*) as versioncount".
168 " FROM ". $CFG->prefix.EWIKI_DB_TABLE_NAME .
169 " WHERE wiki = ".$wiki_entry->id.
170 " GROUP BY pagename";
172 #print "$sql";
173 $result=get_records_sql($sql);
174 while(list($key, $val) = each($result)) {
175 $r[$key]=$val->versioncount;
177 break;
179 /* Returns an array of the lastest versions of __all__ pages,
180 where each entry is made up of the fields from the database
181 requested with the $args array, e.g.
182 array("flags","meta","lastmodified");
184 case "GETALL":
185 switch ($CFG->dbfamily) {
186 case 'postgres':
187 // All but the latest version eliminated by DISTINCT
188 // ON (pagename)
189 $sql= "SELECT DISTINCT ON (pagename) pagename AS id, ".
190 implode(", ", $args) .
191 " FROM ". $CFG->prefix.EWIKI_DB_TABLE_NAME .
192 " WHERE wiki = ".$wiki_entry->id.
193 " ORDER BY pagename, version DESC";
194 break;
195 case 'mysql':
196 // All but the latest version eliminated by
197 // mysql-specific GROUP BY-semantics
198 $sql= "SELECT pagename AS id, ".
199 implode(", ", $args) .
200 " FROM ". $CFG->prefix.EWIKI_DB_TABLE_NAME .
201 " WHERE wiki = ".$wiki_entry->id.
202 " GROUP BY id, version DESC " ;
203 default:
204 // All but the latest version are here eliminated in
205 // get_records_sql, since it will return an array
206 // with only one result per id-field value. Note,
207 // that for this to work the query needs to order the
208 // records ascending by version, so later versions
209 // will overwrite previous ones in
210 // recordset_to_array. This is not pretty.
211 $sql= "SELECT pagename AS id, ".
212 implode(", ", $args) .
213 " FROM ". $CFG->prefix.EWIKI_DB_TABLE_NAME .
214 " WHERE wiki = ".$wiki_entry->id.
215 " ORDER BY version";
218 $result=get_records_sql($sql);
219 $r = new ewiki_dbquery_result($args);
221 if ($result) {
222 foreach($result as $val) {
223 $r->add(get_object_vars($val));
227 break;
231 /* Returns array of database entries (also arrays), where the one
232 specified column matches the specified content string, for example
233 $args = array("content" => "text...piece")
234 is not guaranteed to only search/return the latest version of a page
236 case "SEARCH":
237 $field = implode("", array_keys($args));
238 $content = strtolower(implode("", $args));
239 if ($field == "id") { $field = "pagename"; }
240 $sql= "SELECT pagename AS id, version, flags" .
241 (EWIKI_DBQUERY_BUFFER && ($field!="pagename") ? ", $field" : "") .
242 " FROM " . $CFG->prefix.EWIKI_DB_TABLE_NAME .
243 " WHERE $field " . sql_ilike() . " '%".anydb_escape_string($content)."%' and wiki=".$wiki_entry->id .
244 " ORDER BY id, version ASC";
245 $result=get_records_sql($sql);
247 $r = new ewiki_dbquery_result(array("id","version",$field));
248 $drop = "";
249 #while ($result && ($row = mysql_fetch_array($result, MYSQL_ASSOC))) {
250 # $i = EWIKI_CASE_INSENSITIVE ? strtolower($row["id"]) : $row["id"];
251 # if ($i != $drop) {
252 # $drop = $i;
253 # $r->add($row);
256 while(list($key, $val) = @each($result)) {
257 $row=get_object_vars($val);
258 $i = EWIKI_CASE_INSENSITIVE ? strtolower($row["id"]) : $row["id"];
259 if ($i != $drop) {
260 $drop = $i;
261 $r->add($row);
264 break;
267 case "DELETE":
268 $id = anydb_escape_string($args["id"]);
269 $version = $args["version"];
271 #mysql_query("DELETE FROM " . EWIKI_DB_TABLE_NAME ."
272 # WHERE pagename='$id' AND version=$version");
273 # print "DELETING wiki:".$wiki_entry->id."Pagename: $id Version: $version <br />\n";
274 delete_records(EWIKI_DB_TABLE_NAME,"wiki", $wiki_entry->id,"pagename",$id,"version",$version);
276 break;
280 case "INIT":
281 #mysql_query("CREATE TABLE " . EWIKI_DB_TABLE_NAME ."
282 # (pagename VARCHAR(160) NOT NULL,
283 # version INTEGER UNSIGNED NOT NULL DEFAULT 0,
284 # flags INTEGER UNSIGNED DEFAULT 0,
285 # content MEDIUMTEXT,
286 # author VARCHAR(100) DEFAULT 'ewiki',
287 # created INTEGER UNSIGNED DEFAULT ".time().",
288 # lastmodified INTEGER UNSIGNED DEFAULT 0,
289 # refs MEDIUMTEXT,
290 # meta MEDIUMTEXT,
291 # hits INTEGER UNSIGNED DEFAULT 0,
292 # PRIMARY KEY id (pagename, version) )
293 # ");
294 #echo mysql_error();
295 break;
297 default:
300 return($r);
303 function anydb_escape_string($s) {
304 return(addslashes($s));