MDL-10239:
[moodle-linuxchix.git] / mod / wiki / ewikimoodlelib.php
blob2ef331df24467cddbc9621ca774386a176447176
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,$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 __all__ pages, where each entry is made up
180 of the fields from the database requested with the $args array,
181 e.g. array("flags","meta","lastmodified");
183 case "GETALL":
184 switch ($CFG->dbfamily) {
185 case 'postgres':
186 $sql= "SELECT pagename AS id, ".
187 implode(", ", $args) .
188 " FROM ". $CFG->prefix.EWIKI_DB_TABLE_NAME .
189 " WHERE wiki = ".$wiki_entry->id.
190 " GROUP BY pagename, ".implode(", ", $args);
191 break;
192 default:
193 $sql= "SELECT pagename AS id, ".
194 implode(", ", $args) .
195 " FROM ". $CFG->prefix.EWIKI_DB_TABLE_NAME .
196 " WHERE wiki = ".$wiki_entry->id.
197 " GROUP BY id, version " ;
200 #print "$sql";
201 $result=get_records_sql($sql);
202 $r = new ewiki_dbquery_result($args);
204 $drop = "";
205 #while ($result && ($row = mysql_fetch_array($result, MYSQL_ASSOC))) {
206 # $i = EWIKI_CASE_INSENSITIVE ? strtolower($row["id"]) : $row["id"];
207 # if ($i != $drop) {
208 # $drop = $i;
209 # $r->add($row);
212 #print "<pre>"; print_r($result); print "</pre>";
213 if(!$result) {
214 $result=array();
216 while(list($key, $val) = each($result)) {
217 $row=get_object_vars($val);
218 $i = EWIKI_CASE_INSENSITIVE ? strtolower($row["id"]) : $row["id"];
219 if ($i != $drop) {
220 $drop = $i;
221 $r->add($row);
225 break;
229 /* Returns array of database entries (also arrays), where the one
230 specified column matches the specified content string, for example
231 $args = array("content" => "text...piece")
232 is not guaranteed to only search/return the latest version of a page
234 case "SEARCH":
235 $field = implode("", array_keys($args));
236 $content = strtolower(implode("", $args));
237 if ($field == "id") { $field = "pagename"; }
238 $sql= "SELECT pagename AS id, version, flags" .
239 (EWIKI_DBQUERY_BUFFER && ($field!="pagename") ? ", $field" : "") .
240 " FROM " . $CFG->prefix.EWIKI_DB_TABLE_NAME .
241 " WHERE $field " . sql_ilike() . " '%".anydb_escape_string($content)."%' and wiki=".$wiki_entry->id .
242 " ORDER BY id, version ASC";
243 $result=get_records_sql($sql);
245 $r = new ewiki_dbquery_result(array("id","version",$field));
246 $drop = "";
247 #while ($result && ($row = mysql_fetch_array($result, MYSQL_ASSOC))) {
248 # $i = EWIKI_CASE_INSENSITIVE ? strtolower($row["id"]) : $row["id"];
249 # if ($i != $drop) {
250 # $drop = $i;
251 # $r->add($row);
254 while(list($key, $val) = @each($result)) {
255 $row=get_object_vars($val);
256 $i = EWIKI_CASE_INSENSITIVE ? strtolower($row["id"]) : $row["id"];
257 if ($i != $drop) {
258 $drop = $i;
259 $r->add($row);
262 break;
265 case "DELETE":
266 $id = anydb_escape_string($args["id"]);
267 $version = $args["version"];
269 #mysql_query("DELETE FROM " . EWIKI_DB_TABLE_NAME ."
270 # WHERE pagename='$id' AND version=$version");
271 # print "DELETING wiki:".$wiki_entry->id."Pagename: $id Version: $version <br />\n";
272 delete_records(EWIKI_DB_TABLE_NAME,"wiki", $wiki_entry->id,"pagename",$id,"version",$version);
274 break;
278 case "INIT":
279 #mysql_query("CREATE TABLE " . EWIKI_DB_TABLE_NAME ."
280 # (pagename VARCHAR(160) NOT NULL,
281 # version INTEGER UNSIGNED NOT NULL DEFAULT 0,
282 # flags INTEGER UNSIGNED DEFAULT 0,
283 # content MEDIUMTEXT,
284 # author VARCHAR(100) DEFAULT 'ewiki',
285 # created INTEGER UNSIGNED DEFAULT ".time().",
286 # lastmodified INTEGER UNSIGNED DEFAULT 0,
287 # refs MEDIUMTEXT,
288 # meta MEDIUMTEXT,
289 # hits INTEGER UNSIGNED DEFAULT 0,
290 # PRIMARY KEY id (pagename, version) )
291 # ");
292 #echo mysql_error();
293 break;
295 default:
298 return($r);
301 function anydb_escape_string($s) {
302 global $CFG ;
303 switch ($CFG->dbfamily) {
304 case 'mysql':
305 $s = mysql_escape_string($s);
306 break;
307 case 'postgres':
308 $s = pg_escape_string($s);
309 break;
310 default:
311 $s = addslashes($s);
314 return($s);