|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 /* MySQL database backend 00003 (Glue between Moodle and ewiki Database) 00004 00005 Adapted by Michael Schneider 00006 */ 00007 00008 defined('MOODLE_INTERNAL') || die(); 00010 $ewiki_plugins["database"][0] = "ewiki_database_moodle"; 00011 00013 //define("EWIKI_NAME", $wiki_entry->pagename); COMMENTED BY PIGUI BECOUSE OF MIGRATION 00014 00015 define("EWIKI_CONTROL_LINE", 0); 00016 define("EWIKI_LIST_LIMIT", 25); 00017 //define("EWIKI_DEFAULT_LANG", current_language()); COMMENTED BY PIGUI BECOUSE OF MIGRATION 00018 define("EWIKI_HTML_CHARS", 1); 00019 define("EWIKI_DB_TABLE_NAME", "wiki_pages_old"); 00020 00021 00022 function ewiki_database_moodle($action, &$args, $sw1, $sw2) { 00023 global $wiki, $wiki_entry, $CFG, $DB; 00024 #-- result array 00025 $r = array(); 00026 00027 switch($action) { 00028 00029 /* Returns database entry as array for the page whose name was given 00030 with the "id" key in the $args array, usually fetches the latest 00031 version of a page, unless a specific "version" was requested in 00032 the $args array. 00033 */ 00034 # Ugly, but we need to choose which wiki we are about to change/read 00035 case "GET": 00036 $params = array('id'=>$args["id"]); 00037 if ($version = 0 + @$args["version"]) { 00038 $params['version'] = $version; 00039 $versionsql = "AND version = :version"; 00040 } else { 00041 $versionsql = ""; 00042 00043 } 00044 00045 # $result = mysql_query("SELECT * FROM " . EWIKI_DB_TABLE_NAME 00046 # . " WHERE (pagename=$id) $version ORDER BY version DESC LIMIT 1" 00047 #); 00048 #if ($result && ($r = mysql_fetch_array($result, MYSQL_ASSOC))) { 00049 # $r["id"] = $r["pagename"]; 00050 # unset($r["pagename"]); 00051 #} 00052 #if (strlen($r["meta"])) { 00053 # $r["meta"] = @unserialize($r["meta"]); 00054 #} 00055 00056 $select="(pagename=:id) AND wiki= :weid $versionsql "; 00057 $params['weid'] = $wiki_entry->id; 00058 $sort="version DESC"; 00059 00060 if ($result_arr = $DB->get_records_select(EWIKI_DB_TABLE_NAME, $select, $params, $sort,"*",0,1)) { 00061 //Iterate to get the first (and unique!) 00062 foreach ($result_arr as $obj) { 00063 $result_obj = $obj; 00064 } 00065 } 00066 if($result_obj) { 00067 //Convert to array 00068 $r=get_object_vars($result_obj); 00069 $r["id"] = $r["pagename"]; 00070 unset($r["pagename"]); 00071 $r["meta"] = @unserialize($r["meta"]); 00072 } 00073 break; 00074 00075 00076 00077 /* Increases the hit counter for the page name given in $args array 00078 with "id" index key. 00079 */ 00080 case "HIT": 00081 #mysql_query("UPDATE " . EWIKI_DB_TABLE_NAME . " SET hits=(hits+1) WHERE pagename='" . anydb_escape_string($args["id"]) . "'"); 00082 # $DB->set_field does not work because of the "hits+1" construct 00083 #print "DO ".anydb__escape_string($args["id"]); exit; 00084 $DB->execute("UPDATE {".EWIKI_DB_TABLE_NAME."} SET hits=(hits+1) WHERE pagename=? and wiki=?", array($args["id"], $wiki_entry->id)); 00085 break; 00086 /* Stores the $data array into the database, while not overwriting 00087 existing entries (using WRITE); returns 0 on failure and 1 if 00088 saved correctly. 00089 */ 00090 case "OVERWRITE": 00091 $COMMAND = "REPLACE"; 00092 break; 00093 00094 case "WRITE": 00095 $COMMAND="WRITE"; 00096 $args["pagename"] = $args["id"]; 00097 unset($args["id"]); 00098 00099 if (is_array($args["meta"])) { 00100 $args["meta"] = serialize($args["meta"]); 00101 } 00102 00103 #$sql1 = $sql2 = ""; 00104 #foreach ($args as $index => $value) { 00105 # if (is_int($index)) { 00106 # continue; 00107 # } 00108 # $a = ($sql1 ? ', ' : ''); 00109 # $sql1 .= $a . $index; 00110 # $sql2 .= $a . "'" . anydb_escape_string($value) . "'"; 00111 #} 00112 00113 #strlen(@$COMMAND) || ($COMMAND = "INSERT"); 00114 00115 foreach ($args as $index => $value) { 00116 if (is_int($index)) { 00117 continue; 00118 } 00119 $args[$index] =anydb_escape_string($value); 00120 } 00121 $args["wiki"]=$wiki_entry->id; 00122 00123 # Check if Record exists 00124 if($COMMAND=="REPLACE") { 00125 if ($DB->count_records(EWIKI_DB_TABLE_NAME, array("wiki"=>$wiki_entry->id,"pagename"=>$args["pagename"],"version"=>$args["version"]))) { 00126 $DB->delete_record(EWIKI_DB_TABLE_NAME, array("wiki"=>$wiki_entry->id,"pagename"=>$args["pagename"],"version"=>$args["version"])); 00127 } 00128 } 00129 00130 # Write 00131 $result=$DB->insert_record(EWIKI_DB_TABLE_NAME,(object)$args,false); 00132 00133 #$result = mysql_query("$COMMAND INTO " . EWIKI_DB_TABLE_NAME . 00134 # " (" . $sql1 . ") VALUES (" . $sql2 . ")" 00135 #); 00136 #return($result && mysql_affected_rows() ?1:0); 00137 00138 return $result; 00139 break; 00140 00141 00142 00143 /* Checks for existence of the WikiPages whose names are given in 00144 the $args array. Returns an array with the specified WikiPageNames 00145 associated with values of "0" or "1" (stating if the page exists 00146 in the database). For images/binary db entries returns the "meta" 00147 field instead of an "1". 00148 */ 00149 case "FIND": 00150 $select = ""; 00151 $params = array(); 00152 $p=0; 00153 foreach (array_values($args) as $id) { 00154 if (strlen($id)) { 00155 $p++; 00156 $pname = "par$p"; 00157 $r[$id] = 0; 00158 $select .= ($select ? " OR " : "") . 00159 "(pagename= :$pname)"; 00160 $params[$pname] = $id; 00161 } 00162 } 00163 if($select) { 00164 $select = "(".$select.") AND wiki= :weid "; 00165 $params['weid'] = $wiki_entry->id; 00166 $result = $DB->get_records_select(EWIKI_DB_TABLE_NAME,$select, $params); 00167 00168 while(list($key, $val) = @each($result)) { 00169 $r[$val->pagename]=strpos($val->meta, 's:5:"image"') ? $val->meta : 1; 00170 } 00171 } 00172 break; 00173 00174 /* Counts the number of Versions 00175 */ 00176 case "COUNTVERSIONS": 00177 $sql= "SELECT pagename AS id, count(*) as versioncount". 00178 " FROM {".EWIKI_DB_TABLE_NAME."} 00179 WHERE wiki = ?". 00180 " GROUP BY pagename"; 00181 00182 #print "$sql"; 00183 $result=$DB->get_records_sql($sql, array($wiki_entry->id)); 00184 while(list($key, $val) = each($result)) { 00185 $r[$key]=$val->versioncount; 00186 } 00187 break; 00188 00189 /* Returns an array of the lastest versions of __all__ pages, 00190 where each entry is made up of the fields from the database 00191 requested with the $args array, e.g. 00192 array("flags","meta","lastmodified"); 00193 */ 00194 case "GETALL": 00195 switch ($DB->get_db_family()) { 00196 case 'postgres': 00197 // All but the latest version eliminated by DISTINCT 00198 // ON (pagename) 00199 $sql= "SELECT DISTINCT ON (pagename) pagename AS id, ". 00200 implode(", ", $args) . 00201 " FROM {".EWIKI_DB_TABLE_NAME."}". 00202 " WHERE wiki = ?". 00203 " ORDER BY pagename, version DESC"; 00204 break; 00205 case 'mysql': 00206 // All but the latest version eliminated by 00207 // mysql-specific GROUP BY-semantics 00208 $sql= "SELECT pagename AS id, ". 00209 implode(", ", $args) . 00210 " FROM {".EWIKI_DB_TABLE_NAME."}". 00211 " WHERE wiki = ?". 00212 " GROUP BY id, version DESC " ; 00213 default: 00214 // All but the latest version are here eliminated in 00215 // $DB->get_records_sql, since it will return an array 00216 // with only one result per id-field value. Note, 00217 // that for this to work the query needs to order the 00218 // records ascending by version, so later versions 00219 // will overwrite previous ones in 00220 // recordset_to_array. This is not pretty. 00221 $sql= "SELECT pagename AS id, ". 00222 implode(", ", $args) . 00223 " FROM {".EWIKI_DB_TABLE_NAME."}". 00224 " WHERE wiki = ?". 00225 " ORDER BY version"; 00226 } 00227 00228 $result = $DB->get_records_sql($sql, array($wiki_entry->id)); 00229 $r = new ewiki_dbquery_result($args); 00230 00231 if ($result) { 00232 foreach($result as $val) { 00233 $r->add(get_object_vars($val)); 00234 } 00235 } 00236 00237 break; 00238 00239 00240 00241 /* Returns array of database entries (also arrays), where the one 00242 specified column matches the specified content string, for example 00243 $args = array("content" => "text...piece") 00244 is not guaranteed to only search/return the latest version of a page 00245 */ 00246 case "SEARCH": 00247 $field = implode("", array_keys($args)); 00248 $content = strtolower(implode("", $args)); 00249 if ($field == "id") { $field = "pagename"; } 00250 $sql= "SELECT pagename AS id, version, flags" . 00251 (EWIKI_DBQUERY_BUFFER && ($field!="pagename") ? ", $field" : "") . 00252 " FROM {".EWIKI_DB_TABLE_NAME."}". 00253 " WHERE " . $DB->sql_like($field, '?', false) . " and wiki= ?". 00254 " ORDER BY id, version ASC"; 00255 $result=$DB->get_records_sql($sql, array("%$content%", $wiki_entry->id)); 00256 00257 $r = new ewiki_dbquery_result(array("id","version",$field)); 00258 $drop = ""; 00259 #while ($result && ($row = mysql_fetch_array($result, MYSQL_ASSOC))) { 00260 # $i = EWIKI_CASE_INSENSITIVE ? strtolower($row["id"]) : $row["id"]; 00261 # if ($i != $drop) { 00262 # $drop = $i; 00263 # $r->add($row); 00264 # } 00265 #} 00266 while(list($key, $val) = @each($result)) { 00267 $row=get_object_vars($val); 00268 $i = EWIKI_CASE_INSENSITIVE ? strtolower($row["id"]) : $row["id"]; 00269 if ($i != $drop) { 00270 $drop = $i; 00271 $r->add($row); 00272 } 00273 } 00274 break; 00275 00276 00277 case "DELETE": 00278 $id = $args["id"]; 00279 $version = $args["version"]; 00280 00281 #mysql_query("DELETE FROM " . EWIKI_DB_TABLE_NAME ." 00282 # WHERE pagename='$id' AND version=$version"); 00283 # print "DELETING wiki:".$wiki_entry->id."Pagename: $id Version: $version <br />\n"; 00284 $DB->delete_records(EWIKI_DB_TABLE_NAME, array("wiki"=>$wiki_entry->id,"pagename"=>$id,"version"=>$version)); 00285 00286 break; 00287 00288 00289 00290 case "INIT": 00291 #mysql_query("CREATE TABLE " . EWIKI_DB_TABLE_NAME ." 00292 # (pagename VARCHAR(160) NOT NULL, 00293 # version INTEGER UNSIGNED NOT NULL DEFAULT 0, 00294 # flags INTEGER UNSIGNED DEFAULT 0, 00295 # content MEDIUMTEXT, 00296 # author VARCHAR(100) DEFAULT 'ewiki', 00297 # created INTEGER UNSIGNED DEFAULT ".time().", 00298 # lastmodified INTEGER UNSIGNED DEFAULT 0, 00299 # refs MEDIUMTEXT, 00300 # meta MEDIUMTEXT, 00301 # hits INTEGER UNSIGNED DEFAULT 0, 00302 # PRIMARY KEY id (pagename, version) ) 00303 # "); 00304 #echo mysql_error(); 00305 break; 00306 00307 default: 00308 } 00309 00310 return($r); 00311 } 00312 00313 function anydb_escape_string($s) { 00314 return($s); 00315 } 00316