2 /* MySQL database backend
3 (Glue between Moodle and ewiki Database)
5 Adapted by Michael Schneider
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;
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
33 # Ugly, but we need to choose which wiki we are about to change/read
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"
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 ";
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) {
59 $r=get_object_vars($result_obj);
60 $r["id"] = $r["pagename"];
61 unset($r["pagename"]);
62 $r["meta"] = @unserialize
($r["meta"]);
68 /* Increases the hit counter for the page name given in $args array
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);
77 /* Stores the $data array into the database, while not overwriting
78 existing entries (using WRITE); returns 0 on failure and 1 if
87 $args["pagename"] = $args["id"];
90 if (is_array($args["meta"])) {
91 $args["meta"] = serialize($args["meta"]);
95 #foreach ($args as $index => $value) {
96 # if (is_int($index)) {
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)) {
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"]);
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);
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".
142 foreach (array_values($args) as $id) {
145 $select .= ($select ?
" OR " : "") .
146 "(pagename='" . anydb_escape_string($id) . "')";
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;
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";
173 $result=get_records_sql($sql);
174 while(list($key, $val) = each($result)) {
175 $r[$key]=$val->versioncount
;
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");
184 switch ($CFG->dbfamily
) {
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);
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 " ;
201 $result=get_records_sql($sql);
202 $r = new ewiki_dbquery_result($args);
205 #while ($result && ($row = mysql_fetch_array($result, MYSQL_ASSOC))) {
206 # $i = EWIKI_CASE_INSENSITIVE ? strtolower($row["id"]) : $row["id"];
212 #print "<pre>"; print_r($result); print "</pre>";
216 while(list($key, $val) = each($result)) {
217 $row=get_object_vars($val);
218 $i = EWIKI_CASE_INSENSITIVE ?
strtolower($row["id"]) : $row["id"];
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
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));
247 #while ($result && ($row = mysql_fetch_array($result, MYSQL_ASSOC))) {
248 # $i = EWIKI_CASE_INSENSITIVE ? strtolower($row["id"]) : $row["id"];
254 while(list($key, $val) = @each
($result)) {
255 $row=get_object_vars($val);
256 $i = EWIKI_CASE_INSENSITIVE ?
strtolower($row["id"]) : $row["id"];
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);
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,
289 # hits INTEGER UNSIGNED DEFAULT 0,
290 # PRIMARY KEY id (pagename, version) )
301 function anydb_escape_string($s) {
303 switch ($CFG->dbfamily
) {
305 $s = mysql_escape_string($s);
308 $s = pg_escape_string($s);