Commit 8b9c05d2 authored by Dries's avatar Dries
Browse files

- Bugfix: fixed some SQL queries and tidied up many queries.  Patch by
  Marco.
parent d4a918b8
......@@ -84,7 +84,7 @@ function taxonomy_save_vocabulary($edit) {
$data = array("name" => $edit["name"], "types" => @implode(",", $edit["types"]), "description" => $edit["description"], "multiple" => $edit["multiple"], "required" => $edit["required"], "hierarchy" => $edit["hierarchy"], "relations" => $edit["relations"], "weight" => $edit["weight"]);
if ($edit["vid"] && $edit["name"]) {
db_query("UPDATE vocabulary SET ". _prepare_update($data) ." WHERE vid = '". check_input($edit["vid"]) ."'");
db_query("UPDATE vocabulary SET ". _prepare_update($data) ." WHERE vid = '". check_query($edit["vid"]) ."'");
}
else if ($edit["vid"]) {
taxonomy_del_vocabulary($edit["vid"]);
......@@ -95,8 +95,8 @@ function taxonomy_save_vocabulary($edit) {
}
function taxonomy_del_vocabulary($vid) {
db_query("DELETE FROM vocabulary WHERE vid = '%s'", $vid);
$result = db_query("SELECT tid FROM term_data WHERE vid = '%s'", $vid);
db_query("DELETE FROM vocabulary WHERE vid = '%d'", $vid);
$result = db_query("SELECT tid FROM term_data WHERE vid = '%d'", $vid);
while ($term = db_fetch_object($result)) {
taxonomy_del_term($term->tid);
}
......@@ -147,7 +147,7 @@ function taxonomy_save_term($edit) {
if ($edit["tid"] && $edit["name"]) {
$data = array("name" => $edit["name"], "description" => $edit["description"], "weight" => $edit["weight"]);
db_query("UPDATE term_data SET ". _prepare_update($data) ." WHERE tid = '%s'", $edit["tid"]);
db_query("UPDATE term_data SET ". _prepare_update($data) ." WHERE tid = '%d'", $edit["tid"]);
}
else if ($edit["tid"]) {
taxonomy_del_term($edit["tid"]);
......@@ -163,51 +163,44 @@ function taxonomy_save_term($edit) {
}
// relations (seem very powerful, but I have to understand it completely)
db_query("DELETE FROM term_relation WHERE tid1 = '%s' OR tid2 = '%s'", $edit["tid"], $edit["tid"]);
db_query("DELETE FROM term_relation WHERE tid1 = '%d' OR tid2 = '%d'", $edit["tid"], $edit["tid"]);
if ($edit["relations"]) {
foreach ($edit["relations"] as $related_id) {
if ($related_id != 0) {
$rel_q[] = "('". check_query($edit["tid"]) ."', '". check_query($related_id) ."')";
db_query("INSERT INTO term_relation (tid1, tid2) VALUES ('%d', '%d')", $edit["tid"], $related_id);
}
}
if ($rel_q) {
$related_query = implode(", ", $rel_q);
db_query("INSERT INTO term_relation (tid1, tid2) VALUES $related_query");
}
}
// hierarchy
db_query("DELETE FROM term_hierarchy WHERE tid = '%s'", $edit["tid"]);
db_query("DELETE FROM term_hierarchy WHERE tid = '%d'", $edit["tid"]);
if (!isset($edit["parent"])) {
$edit["parent"] = 0;
}
if (is_array($edit["parent"])) {
foreach ($edit["parent"] as $parent) {
$sql[] = "('". check_query($edit["tid"]) ."', '". check_query($parent) ."')";
db_query("INSERT INTO term_hierarchy (tid, parent) VALUES ('%d', '%d')", $edit["tid"], $parent);
}
db_query("INSERT INTO term_hierarchy (tid, parent) VALUES ". implode(", ", $sql));
}
else {
db_query("INSERT INTO term_hierarchy (tid, parent) VALUES ('%s', '%s')", $edit["tid"], $edit["parent"][0]);
db_query("INSERT INTO term_hierarchy (tid, parent) VALUES ('%d', '%d')", $edit["tid"], $edit["parent"][0]);
}
// synonyms (very cool idea indeed)
db_query("DELETE FROM term_synonym WHERE tid = '%s'", $edit["tid"]);
db_query("DELETE FROM term_synonym WHERE tid = '%d'", $edit["tid"]);
if ($edit["synonyms"]) {
foreach (explode ("\n", $edit["synonyms"]) as $synonym) {
$syn_q[] = "('". check_query($edit["tid"]) ."', '". check_query(chop($synonym)) ."')";
db_query("INSERT INTO term_synonym (tid, name) VALUES ('%d', '%s')", $edit["tid"], chop($synonym));
}
$synonyms_query = implode(", ", $syn_q);
db_query("INSERT INTO term_synonym (tid, name) VALUES $synonyms_query");
}
}
function taxonomy_del_term($tid) {
db_query("DELETE FROM term_data WHERE tid = '%s'", $tid);
db_query("DELETE FROM term_hierarchy WHERE tid = '%s'", $tid);
db_query("DELETE FROM term_relation WHERE tid1 = '%s' OR tid2 = '%s'", $tid, $tid);
db_query("DELETE FROM term_synonym WHERE tid = '%s'", $tid);
db_query("DELETE FROM term_node WHERE tid = '%s'", $tid);
db_query("DELETE FROM term_data WHERE tid = '%d'", $tid);
db_query("DELETE FROM term_hierarchy WHERE tid = '%d'", $tid);
db_query("DELETE FROM term_relation WHERE tid1 = '%d' OR tid2 = '%d'", $tid, $tid);
db_query("DELETE FROM term_synonym WHERE tid = '%d'", $tid);
db_query("DELETE FROM term_node WHERE tid = '%d'", $tid);
}
function taxonomy_overview() {
......@@ -307,12 +300,12 @@ function taxonomy_node_form($type, $node = '') {
function taxonomy_node_has_term($nid, $tid) {
$term_name = db_result(db_query("SELECT name FROM term_data WHERE tid = '%s'", $tid));
return db_result(db_query("SELECT COUNT(n.nid) FROM node n WHERE n.nid = '%s' AND ((n.body LIKE '%%%s%%') OR (n.body LIKE '%%%s%%'))", $nid, $term_name, $term_name));
return db_result(db_query("SELECT COUNT(n.nid) FROM node n WHERE n.nid = '%d' AND ((n.body LIKE '%%%s%%') OR (n.body LIKE '%%%s%%'))", $nid, $term_name, $term_name));
}
// return array of terms of a node beloging to a particular vocabulary identified by $vid
function taxonomy_node_get_terms_by_vocabulary($nid, $vid, $key = "tid") {
$result = db_query("SELECT t.* FROM term_data t, term_node r WHERE t.tid = r.tid AND t.vid = '%s' AND r.nid = '%s' ORDER BY weight", $vid, $nid);
$result = db_query("SELECT t.* FROM term_data t, term_node r WHERE t.tid = r.tid AND t.vid = '%d' AND r.nid = '%d' ORDER BY weight", $vid, $nid);
$terms = array();
while ($term = db_fetch_object($result)) {
$terms[$term->$key] = $term;
......@@ -325,7 +318,7 @@ function taxonomy_node_get_terms($nid, $key = "tid") {
static $terms;
if (!$terms[$nid]) {
$result = db_query("SELECT t.* FROM term_data t, term_node r WHERE r.tid = t.tid AND r.nid = '%s' ORDER BY weight", $nid);
$result = db_query("SELECT t.* FROM term_data t, term_node r WHERE r.tid = t.tid AND r.nid = '%d' ORDER BY weight", $nid);
$terms[$nid] = array();
while ($term = db_fetch_object($result)) {
$terms[$nid][$term->$key] = $term;
......@@ -339,16 +332,15 @@ function taxonomy_node_save($nid, $terms) {
taxonomy_node_delete($nid);
if ($terms) {
foreach ($terms as $t) {
$query[] = "('". check_query($nid) ."', '". check_query($t) ."')";
foreach ($terms as $term) {
db_query("INSERT INTO term_node (nid, tid) VALUES ('%d', '%d')", $nid, $term);
}
db_query("INSERT INTO term_node (nid, tid) VALUES ". implode(", ", $query));
}
}
// clean up terms
function taxonomy_node_delete($nid) {
db_query("DELETE FROM term_node WHERE nid = '%s'", $nid);
db_query("DELETE FROM term_node WHERE nid = '%d'", $nid);
}
// relations: return array of related terms
......@@ -369,7 +361,7 @@ function taxonomy_get_related($tid, $key = "tid") {
// hierarchy: get parent terms
function taxonomy_get_parents($tid, $key = "tid") {
if ($tid) {
$result = db_query("SELECT t.* FROM term_hierarchy h, term_data t WHERE h.parent = t.tid AND h.tid = '%s' ORDER BY weight, name", $tid);
$result = db_query("SELECT t.* FROM term_hierarchy h, term_data t WHERE h.parent = t.tid AND h.tid = '%d' ORDER BY weight, name", $tid);
$parents = array();
while ($parent = db_fetch_object($result)) {
$parents[$parent->$key] = $parent;
......@@ -384,10 +376,10 @@ function taxonomy_get_parents($tid, $key = "tid") {
// hierarchy: get children
function taxonomy_get_children($tid, $vid = 0, $key = "tid") {
if ($vid) {
$result = db_query("SELECT t.* FROM term_hierarchy h, term_data t WHERE t.vid = '%s' AND h.tid = t.tid AND h.parent = '%s' ORDER BY weight, name", $vid, $tid);
$result = db_query("SELECT t.* FROM term_hierarchy h, term_data t WHERE t.vid = '%d' AND h.tid = t.tid AND h.parent = '%d' ORDER BY weight, name", $vid, $tid);
}
else {
$result = db_query("SELECT t.* FROM term_hierarchy h, term_data t WHERE h.tid = t.tid AND parent = '%s' ORDER BY weight", $tid);
$result = db_query("SELECT t.* FROM term_hierarchy h, term_data t WHERE h.tid = t.tid AND parent = '%d' ORDER BY weight", $tid);
}
$children = array();
while ($term = db_fetch_object($result)) {
......@@ -407,7 +399,7 @@ function taxonomy_get_tree($vocabulary_id, &$tree, $parent = 0, $depth = -1, $ke
$depth++;
if ($vocabulary_id) {
if (!$children) {
$result = db_query("SELECT t.*, parent FROM term_data t, term_hierarchy h WHERE t.tid = h.tid AND t.vid = '%s' ORDER BY weight, name", $vocabulary_id);
$result = db_query("SELECT t.*, parent FROM term_data t, term_hierarchy h WHERE t.tid = h.tid AND t.vid = '%d' ORDER BY weight, name", $vocabulary_id);
while ($term = db_fetch_object($result)) {
$children[$term->parent][] = $term->tid;
$terms[$term->tid] = $term;
......@@ -429,7 +421,7 @@ function taxonomy_get_tree($vocabulary_id, &$tree, $parent = 0, $depth = -1, $ke
// synonyms: return array of synonyms
function taxonomy_get_synonyms($tid) {
if ($tid) {
$result = db_query("SELECT name FROM term_synonym WHERE tid = '%s'", $tid);
$result = db_query("SELECT name FROM term_synonym WHERE tid = '%d'", $tid);
while ($synonym = db_fetch_array($result)) {
$synonyms[] = $synonym["name"];
}
......@@ -477,12 +469,12 @@ function _taxonomy_term_children($tid) {
function taxonomy_get_vocabulary($vid) {
// simple cache using a static var?
return db_fetch_object(db_query("SELECT * FROM vocabulary WHERE vid = '%s'", $vid));
return db_fetch_object(db_query("SELECT * FROM vocabulary WHERE vid = '%d'", $vid));
}
function taxonomy_get_term($tid) {
// simple cache using a static var?
return db_fetch_object(db_query("SELECT * FROM term_data WHERE tid = '%s'", $tid));
return db_fetch_object(db_query("SELECT * FROM term_data WHERE tid = '%d'", $tid));
}
/*
......
......@@ -84,7 +84,7 @@ function taxonomy_save_vocabulary($edit) {
$data = array("name" => $edit["name"], "types" => @implode(",", $edit["types"]), "description" => $edit["description"], "multiple" => $edit["multiple"], "required" => $edit["required"], "hierarchy" => $edit["hierarchy"], "relations" => $edit["relations"], "weight" => $edit["weight"]);
if ($edit["vid"] && $edit["name"]) {
db_query("UPDATE vocabulary SET ". _prepare_update($data) ." WHERE vid = '". check_input($edit["vid"]) ."'");
db_query("UPDATE vocabulary SET ". _prepare_update($data) ." WHERE vid = '". check_query($edit["vid"]) ."'");
}
else if ($edit["vid"]) {
taxonomy_del_vocabulary($edit["vid"]);
......@@ -95,8 +95,8 @@ function taxonomy_save_vocabulary($edit) {
}
function taxonomy_del_vocabulary($vid) {
db_query("DELETE FROM vocabulary WHERE vid = '%s'", $vid);
$result = db_query("SELECT tid FROM term_data WHERE vid = '%s'", $vid);
db_query("DELETE FROM vocabulary WHERE vid = '%d'", $vid);
$result = db_query("SELECT tid FROM term_data WHERE vid = '%d'", $vid);
while ($term = db_fetch_object($result)) {
taxonomy_del_term($term->tid);
}
......@@ -147,7 +147,7 @@ function taxonomy_save_term($edit) {
if ($edit["tid"] && $edit["name"]) {
$data = array("name" => $edit["name"], "description" => $edit["description"], "weight" => $edit["weight"]);
db_query("UPDATE term_data SET ". _prepare_update($data) ." WHERE tid = '%s'", $edit["tid"]);
db_query("UPDATE term_data SET ". _prepare_update($data) ." WHERE tid = '%d'", $edit["tid"]);
}
else if ($edit["tid"]) {
taxonomy_del_term($edit["tid"]);
......@@ -163,51 +163,44 @@ function taxonomy_save_term($edit) {
}
// relations (seem very powerful, but I have to understand it completely)
db_query("DELETE FROM term_relation WHERE tid1 = '%s' OR tid2 = '%s'", $edit["tid"], $edit["tid"]);
db_query("DELETE FROM term_relation WHERE tid1 = '%d' OR tid2 = '%d'", $edit["tid"], $edit["tid"]);
if ($edit["relations"]) {
foreach ($edit["relations"] as $related_id) {
if ($related_id != 0) {
$rel_q[] = "('". check_query($edit["tid"]) ."', '". check_query($related_id) ."')";
db_query("INSERT INTO term_relation (tid1, tid2) VALUES ('%d', '%d')", $edit["tid"], $related_id);
}
}
if ($rel_q) {
$related_query = implode(", ", $rel_q);
db_query("INSERT INTO term_relation (tid1, tid2) VALUES $related_query");
}
}
// hierarchy
db_query("DELETE FROM term_hierarchy WHERE tid = '%s'", $edit["tid"]);
db_query("DELETE FROM term_hierarchy WHERE tid = '%d'", $edit["tid"]);
if (!isset($edit["parent"])) {
$edit["parent"] = 0;
}
if (is_array($edit["parent"])) {
foreach ($edit["parent"] as $parent) {
$sql[] = "('". check_query($edit["tid"]) ."', '". check_query($parent) ."')";
db_query("INSERT INTO term_hierarchy (tid, parent) VALUES ('%d', '%d')", $edit["tid"], $parent);
}
db_query("INSERT INTO term_hierarchy (tid, parent) VALUES ". implode(", ", $sql));
}
else {
db_query("INSERT INTO term_hierarchy (tid, parent) VALUES ('%s', '%s')", $edit["tid"], $edit["parent"][0]);
db_query("INSERT INTO term_hierarchy (tid, parent) VALUES ('%d', '%d')", $edit["tid"], $edit["parent"][0]);
}
// synonyms (very cool idea indeed)
db_query("DELETE FROM term_synonym WHERE tid = '%s'", $edit["tid"]);
db_query("DELETE FROM term_synonym WHERE tid = '%d'", $edit["tid"]);
if ($edit["synonyms"]) {
foreach (explode ("\n", $edit["synonyms"]) as $synonym) {
$syn_q[] = "('". check_query($edit["tid"]) ."', '". check_query(chop($synonym)) ."')";
db_query("INSERT INTO term_synonym (tid, name) VALUES ('%d', '%s')", $edit["tid"], chop($synonym));
}
$synonyms_query = implode(", ", $syn_q);
db_query("INSERT INTO term_synonym (tid, name) VALUES $synonyms_query");
}
}
function taxonomy_del_term($tid) {
db_query("DELETE FROM term_data WHERE tid = '%s'", $tid);
db_query("DELETE FROM term_hierarchy WHERE tid = '%s'", $tid);
db_query("DELETE FROM term_relation WHERE tid1 = '%s' OR tid2 = '%s'", $tid, $tid);
db_query("DELETE FROM term_synonym WHERE tid = '%s'", $tid);
db_query("DELETE FROM term_node WHERE tid = '%s'", $tid);
db_query("DELETE FROM term_data WHERE tid = '%d'", $tid);
db_query("DELETE FROM term_hierarchy WHERE tid = '%d'", $tid);
db_query("DELETE FROM term_relation WHERE tid1 = '%d' OR tid2 = '%d'", $tid, $tid);
db_query("DELETE FROM term_synonym WHERE tid = '%d'", $tid);
db_query("DELETE FROM term_node WHERE tid = '%d'", $tid);
}
function taxonomy_overview() {
......@@ -307,12 +300,12 @@ function taxonomy_node_form($type, $node = '') {
function taxonomy_node_has_term($nid, $tid) {
$term_name = db_result(db_query("SELECT name FROM term_data WHERE tid = '%s'", $tid));
return db_result(db_query("SELECT COUNT(n.nid) FROM node n WHERE n.nid = '%s' AND ((n.body LIKE '%%%s%%') OR (n.body LIKE '%%%s%%'))", $nid, $term_name, $term_name));
return db_result(db_query("SELECT COUNT(n.nid) FROM node n WHERE n.nid = '%d' AND ((n.body LIKE '%%%s%%') OR (n.body LIKE '%%%s%%'))", $nid, $term_name, $term_name));
}
// return array of terms of a node beloging to a particular vocabulary identified by $vid
function taxonomy_node_get_terms_by_vocabulary($nid, $vid, $key = "tid") {
$result = db_query("SELECT t.* FROM term_data t, term_node r WHERE t.tid = r.tid AND t.vid = '%s' AND r.nid = '%s' ORDER BY weight", $vid, $nid);
$result = db_query("SELECT t.* FROM term_data t, term_node r WHERE t.tid = r.tid AND t.vid = '%d' AND r.nid = '%d' ORDER BY weight", $vid, $nid);
$terms = array();
while ($term = db_fetch_object($result)) {
$terms[$term->$key] = $term;
......@@ -325,7 +318,7 @@ function taxonomy_node_get_terms($nid, $key = "tid") {
static $terms;
if (!$terms[$nid]) {
$result = db_query("SELECT t.* FROM term_data t, term_node r WHERE r.tid = t.tid AND r.nid = '%s' ORDER BY weight", $nid);
$result = db_query("SELECT t.* FROM term_data t, term_node r WHERE r.tid = t.tid AND r.nid = '%d' ORDER BY weight", $nid);
$terms[$nid] = array();
while ($term = db_fetch_object($result)) {
$terms[$nid][$term->$key] = $term;
......@@ -339,16 +332,15 @@ function taxonomy_node_save($nid, $terms) {
taxonomy_node_delete($nid);
if ($terms) {
foreach ($terms as $t) {
$query[] = "('". check_query($nid) ."', '". check_query($t) ."')";
foreach ($terms as $term) {
db_query("INSERT INTO term_node (nid, tid) VALUES ('%d', '%d')", $nid, $term);
}
db_query("INSERT INTO term_node (nid, tid) VALUES ". implode(", ", $query));
}
}
// clean up terms
function taxonomy_node_delete($nid) {
db_query("DELETE FROM term_node WHERE nid = '%s'", $nid);
db_query("DELETE FROM term_node WHERE nid = '%d'", $nid);
}
// relations: return array of related terms
......@@ -369,7 +361,7 @@ function taxonomy_get_related($tid, $key = "tid") {
// hierarchy: get parent terms
function taxonomy_get_parents($tid, $key = "tid") {
if ($tid) {
$result = db_query("SELECT t.* FROM term_hierarchy h, term_data t WHERE h.parent = t.tid AND h.tid = '%s' ORDER BY weight, name", $tid);
$result = db_query("SELECT t.* FROM term_hierarchy h, term_data t WHERE h.parent = t.tid AND h.tid = '%d' ORDER BY weight, name", $tid);
$parents = array();
while ($parent = db_fetch_object($result)) {
$parents[$parent->$key] = $parent;
......@@ -384,10 +376,10 @@ function taxonomy_get_parents($tid, $key = "tid") {
// hierarchy: get children
function taxonomy_get_children($tid, $vid = 0, $key = "tid") {
if ($vid) {
$result = db_query("SELECT t.* FROM term_hierarchy h, term_data t WHERE t.vid = '%s' AND h.tid = t.tid AND h.parent = '%s' ORDER BY weight, name", $vid, $tid);
$result = db_query("SELECT t.* FROM term_hierarchy h, term_data t WHERE t.vid = '%d' AND h.tid = t.tid AND h.parent = '%d' ORDER BY weight, name", $vid, $tid);
}
else {
$result = db_query("SELECT t.* FROM term_hierarchy h, term_data t WHERE h.tid = t.tid AND parent = '%s' ORDER BY weight", $tid);
$result = db_query("SELECT t.* FROM term_hierarchy h, term_data t WHERE h.tid = t.tid AND parent = '%d' ORDER BY weight", $tid);
}
$children = array();
while ($term = db_fetch_object($result)) {
......@@ -407,7 +399,7 @@ function taxonomy_get_tree($vocabulary_id, &$tree, $parent = 0, $depth = -1, $ke
$depth++;
if ($vocabulary_id) {
if (!$children) {
$result = db_query("SELECT t.*, parent FROM term_data t, term_hierarchy h WHERE t.tid = h.tid AND t.vid = '%s' ORDER BY weight, name", $vocabulary_id);
$result = db_query("SELECT t.*, parent FROM term_data t, term_hierarchy h WHERE t.tid = h.tid AND t.vid = '%d' ORDER BY weight, name", $vocabulary_id);
while ($term = db_fetch_object($result)) {
$children[$term->parent][] = $term->tid;
$terms[$term->tid] = $term;
......@@ -429,7 +421,7 @@ function taxonomy_get_tree($vocabulary_id, &$tree, $parent = 0, $depth = -1, $ke
// synonyms: return array of synonyms
function taxonomy_get_synonyms($tid) {
if ($tid) {
$result = db_query("SELECT name FROM term_synonym WHERE tid = '%s'", $tid);
$result = db_query("SELECT name FROM term_synonym WHERE tid = '%d'", $tid);
while ($synonym = db_fetch_array($result)) {
$synonyms[] = $synonym["name"];
}
......@@ -477,12 +469,12 @@ function _taxonomy_term_children($tid) {
function taxonomy_get_vocabulary($vid) {
// simple cache using a static var?
return db_fetch_object(db_query("SELECT * FROM vocabulary WHERE vid = '%s'", $vid));
return db_fetch_object(db_query("SELECT * FROM vocabulary WHERE vid = '%d'", $vid));
}
function taxonomy_get_term($tid) {
// simple cache using a static var?
return db_fetch_object(db_query("SELECT * FROM term_data WHERE tid = '%s'", $tid));
return db_fetch_object(db_query("SELECT * FROM term_data WHERE tid = '%d'", $tid));
}
/*
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment