Commit 7add598e authored by Dries's avatar Dries

- Patch #51850 by chx, webchick et al: fixed various problems with...

- Patch #51850 by chx, webchick et al: fixed various problems with db_rewrite_sql, made db_rewrite_sql slightly more robust.
parent 46ad2619
......@@ -234,17 +234,17 @@ function _db_rewrite_sql($query = '', $primary_table = 'n', $primary_field = 'ni
$result = module_invoke($module, 'db_rewrite_sql', $query, $primary_table, $primary_field, $args);
if (isset($result) && is_array($result)) {
if (isset($result['where'])) {
$where[] .= $result['where'];
$where[] = $result['where'];
}
if (isset($result['join'])) {
$join[] .= $result['join'];
$join[] = $result['join'];
}
if (isset($result['distinct']) && $result['distinct']) {
$distinct = TRUE;
}
}
elseif (isset($result)) {
$where[] .= $result;
$where[] = $result;
}
}
......@@ -255,7 +255,8 @@ function _db_rewrite_sql($query = '', $primary_table = 'n', $primary_field = 'ni
}
/**
* Rewrites node, taxonomy and comment queries. Use it for listing queries.
* Rewrites node, taxonomy and comment queries. Use it for listing queries. Do not
* use FROM table1, table2 syntax, use JOIN instead.
*
* @param $query
* Query to be rewritten.
......@@ -277,11 +278,11 @@ function db_rewrite_sql($query, $primary_table = 'n', $primary_field = 'nid', $
$query = preg_replace('/(SELECT.*)('. $primary_table .'\.)?(?<!DISTINCT\()(?<!DISTINCT\('. $primary_table .'\.)'. $primary_field .'(.*FROM)/AUsi', '\1'. $field_to_select .'\3', $query);
}
if (!empty($join)) {
$query = preg_replace('/LEFT |RIGHT |INNER |WHERE|GROUP|ORDER|$/', $join .' \0', $query, 1);
}
if (!empty($where)) {
if (!empty($where) || !empty($join)) {
if (!empty($where)) {
$new = " WHERE $where ";
}
$new = " $join $new";
if (strpos($query, 'WHERE')) {
$replace = 'WHERE';
$add = 'AND';
......@@ -299,10 +300,10 @@ function db_rewrite_sql($query, $primary_table = 'n', $primary_field = 'nid', $
$add = 'LIMIT';
}
else {
$query .= ' WHERE '. $where;
$query .= $new;
}
if (isset($replace)) {
$query = str_replace($replace, 'WHERE '. $where .' '. $add .' ', $query);
$query = str_replace($replace, "$new $add ", $query);
}
}
......
......@@ -686,7 +686,7 @@ function forum_get_forums($tid = 0) {
// This query does not use full ANSI syntax since MySQL 3.x does not support
// table1 INNER JOIN table2 INNER JOIN table3 ON table2_criteria ON table3_criteria
// used to join node_comment_statistics to users.
$sql = "SELECT n.nid, l.last_comment_timestamp, IF(l.last_comment_uid != 0, cu.name, l.last_comment_name) as last_comment_name, l.last_comment_uid FROM {node} n, {node_comment_statistics} l, {users} cu, {term_node} r WHERE n.nid = r.nid AND r.tid = %d AND n.status = 1 AND n.type = 'forum' AND l.last_comment_uid = cu.uid AND n.nid = l.nid ORDER BY l.last_comment_timestamp DESC";
$sql = "SELECT n.nid, f.tid, n.title, n.sticky, u.name, u.uid, n.created AS timestamp, n.comment AS comment_mode, l.last_comment_timestamp, IF(l.last_comment_uid != 0, cu.name, l.last_comment_name) AS last_comment_name, l.last_comment_uid, l.comment_count AS num_comments FROM {node} n INNER JOIN {users} u ON n.uid = u.uid INNER JOIN {term_node} r ON n.nid = r.nid INNER JOIN {forum} f ON n.vid = f.vid INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {users} cu ON l.last_comment_uid = cu.uid WHERE n.status = 1 AND r.tid = %d";
$sql = db_rewrite_sql($sql);
$topic = db_fetch_object(db_query_range($sql, $forum->tid, 0, 1));
......
......@@ -686,7 +686,7 @@ function forum_get_forums($tid = 0) {
// This query does not use full ANSI syntax since MySQL 3.x does not support
// table1 INNER JOIN table2 INNER JOIN table3 ON table2_criteria ON table3_criteria
// used to join node_comment_statistics to users.
$sql = "SELECT n.nid, l.last_comment_timestamp, IF(l.last_comment_uid != 0, cu.name, l.last_comment_name) as last_comment_name, l.last_comment_uid FROM {node} n, {node_comment_statistics} l, {users} cu, {term_node} r WHERE n.nid = r.nid AND r.tid = %d AND n.status = 1 AND n.type = 'forum' AND l.last_comment_uid = cu.uid AND n.nid = l.nid ORDER BY l.last_comment_timestamp DESC";
$sql = "SELECT n.nid, f.tid, n.title, n.sticky, u.name, u.uid, n.created AS timestamp, n.comment AS comment_mode, l.last_comment_timestamp, IF(l.last_comment_uid != 0, cu.name, l.last_comment_name) AS last_comment_name, l.last_comment_uid, l.comment_count AS num_comments FROM {node} n INNER JOIN {users} u ON n.uid = u.uid INNER JOIN {term_node} r ON n.nid = r.nid INNER JOIN {forum} f ON n.vid = f.vid INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {users} cu ON l.last_comment_uid = cu.uid WHERE n.status = 1 AND r.tid = %d";
$sql = db_rewrite_sql($sql);
$topic = db_fetch_object(db_query_range($sql, $forum->tid, 0, 1));
......
......@@ -655,7 +655,7 @@ function taxonomy_form_alter($form_id, &$form) {
* Find all terms associated to the given node, within one vocabulary.
*/
function taxonomy_node_get_terms_by_vocabulary($nid, $vid, $key = 'tid') {
$result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t, {term_node} r WHERE t.tid = r.tid AND t.vid = %d AND r.nid = %d ORDER BY weight', 't', 'tid'), $vid, $nid);
$result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.nid = %d ORDER BY weight', 't', 'tid'), $vid, $nid);
$terms = array();
while ($term = db_fetch_object($result)) {
$terms[$term->$key] = $term;
......@@ -789,7 +789,7 @@ function taxonomy_get_related($tid, $key = 'tid') {
*/
function taxonomy_get_parents($tid, $key = 'tid') {
if ($tid) {
$result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_hierarchy} h, {term_data} t WHERE h.parent = t.tid AND h.tid = %d ORDER BY weight, name', 't', 'tid'), $tid);
$result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t INNER JOIN {term_hierarchy} h ON h.parent = t.tid WHERE h.tid = %d ORDER BY weight, name', 't', 'tid'), $tid);
$parents = array();
while ($parent = db_fetch_object($result)) {
$parents[$parent->$key] = $parent;
......@@ -822,10 +822,10 @@ function taxonomy_get_parents_all($tid) {
*/
function taxonomy_get_children($tid, $vid = 0, $key = 'tid') {
if ($vid) {
$result = db_query(db_rewrite_sql('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', 't', 'tid'), $vid, $tid);
$result = db_query(db_rewrite_sql('SELECT t.* FROM {term_data} t INNER JOIN {term_hierarchy} h ON h.tid = t.tid WHERE t.vid = %d AND h.parent = %d ORDER BY weight, name', 't', 'tid'), $vid, $tid);
}
else {
$result = db_query(db_rewrite_sql('SELECT t.* FROM {term_hierarchy} h, {term_data} t WHERE h.tid = t.tid AND parent = %d ORDER BY weight, name', 't', 'tid'), $tid);
$result = db_query(db_rewrite_sql('SELECT t.* FROM {term_data} t INNER JOIN {term_hierarchy} h ON h.tid = t.tid WHERE parent = %d ORDER BY weight, name', 't', 'tid'), $tid);
}
$children = array();
while ($term = db_fetch_object($result)) {
......@@ -864,7 +864,7 @@ function taxonomy_get_tree($vid, $parent = 0, $depth = -1, $max_depth = NULL) {
if (!isset($children[$vid])) {
$children[$vid] = array();
$result = db_query(db_rewrite_sql('SELECT t.tid, t.*, parent FROM {term_data} t, {term_hierarchy} h WHERE t.tid = h.tid AND t.vid = %d ORDER BY weight, name', 't', 'tid'), $vid);
$result = db_query(db_rewrite_sql('SELECT t.tid, t.*, parent FROM {term_data} t INNER JOIN {term_hierarchy} h ON t.tid = h.tid WHERE t.vid = %d ORDER BY weight, name', 't', 'tid'), $vid);
while ($term = db_fetch_object($result)) {
$children[$vid][$term->parent][] = $term->tid;
$parents[$vid][$term->tid][] = $term->parent;
......@@ -927,7 +927,7 @@ function taxonomy_term_count_nodes($tid, $type = 0) {
$result = db_query(db_rewrite_sql('SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 GROUP BY t.tid'));
}
else {
$result = db_query(db_rewrite_sql("SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t, {node} n WHERE t.nid = n.nid AND n.status = 1 AND n.type = '%s' GROUP BY t.tid"), $type);
$result = db_query(db_rewrite_sql("SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND n.type = '%s' GROUP BY t.tid"), $type);
}
while ($term = db_fetch_object($result)) {
$count[$type][$term->tid] = $term->c;
......
......@@ -655,7 +655,7 @@ function taxonomy_form_alter($form_id, &$form) {
* Find all terms associated to the given node, within one vocabulary.
*/
function taxonomy_node_get_terms_by_vocabulary($nid, $vid, $key = 'tid') {
$result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t, {term_node} r WHERE t.tid = r.tid AND t.vid = %d AND r.nid = %d ORDER BY weight', 't', 'tid'), $vid, $nid);
$result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.nid = %d ORDER BY weight', 't', 'tid'), $vid, $nid);
$terms = array();
while ($term = db_fetch_object($result)) {
$terms[$term->$key] = $term;
......@@ -789,7 +789,7 @@ function taxonomy_get_related($tid, $key = 'tid') {
*/
function taxonomy_get_parents($tid, $key = 'tid') {
if ($tid) {
$result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_hierarchy} h, {term_data} t WHERE h.parent = t.tid AND h.tid = %d ORDER BY weight, name', 't', 'tid'), $tid);
$result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t INNER JOIN {term_hierarchy} h ON h.parent = t.tid WHERE h.tid = %d ORDER BY weight, name', 't', 'tid'), $tid);
$parents = array();
while ($parent = db_fetch_object($result)) {
$parents[$parent->$key] = $parent;
......@@ -822,10 +822,10 @@ function taxonomy_get_parents_all($tid) {
*/
function taxonomy_get_children($tid, $vid = 0, $key = 'tid') {
if ($vid) {
$result = db_query(db_rewrite_sql('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', 't', 'tid'), $vid, $tid);
$result = db_query(db_rewrite_sql('SELECT t.* FROM {term_data} t INNER JOIN {term_hierarchy} h ON h.tid = t.tid WHERE t.vid = %d AND h.parent = %d ORDER BY weight, name', 't', 'tid'), $vid, $tid);
}
else {
$result = db_query(db_rewrite_sql('SELECT t.* FROM {term_hierarchy} h, {term_data} t WHERE h.tid = t.tid AND parent = %d ORDER BY weight, name', 't', 'tid'), $tid);
$result = db_query(db_rewrite_sql('SELECT t.* FROM {term_data} t INNER JOIN {term_hierarchy} h ON h.tid = t.tid WHERE parent = %d ORDER BY weight, name', 't', 'tid'), $tid);
}
$children = array();
while ($term = db_fetch_object($result)) {
......@@ -864,7 +864,7 @@ function taxonomy_get_tree($vid, $parent = 0, $depth = -1, $max_depth = NULL) {
if (!isset($children[$vid])) {
$children[$vid] = array();
$result = db_query(db_rewrite_sql('SELECT t.tid, t.*, parent FROM {term_data} t, {term_hierarchy} h WHERE t.tid = h.tid AND t.vid = %d ORDER BY weight, name', 't', 'tid'), $vid);
$result = db_query(db_rewrite_sql('SELECT t.tid, t.*, parent FROM {term_data} t INNER JOIN {term_hierarchy} h ON t.tid = h.tid WHERE t.vid = %d ORDER BY weight, name', 't', 'tid'), $vid);
while ($term = db_fetch_object($result)) {
$children[$vid][$term->parent][] = $term->tid;
$parents[$vid][$term->tid][] = $term->parent;
......@@ -927,7 +927,7 @@ function taxonomy_term_count_nodes($tid, $type = 0) {
$result = db_query(db_rewrite_sql('SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 GROUP BY t.tid'));
}
else {
$result = db_query(db_rewrite_sql("SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t, {node} n WHERE t.nid = n.nid AND n.status = 1 AND n.type = '%s' GROUP BY t.tid"), $type);
$result = db_query(db_rewrite_sql("SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND n.type = '%s' GROUP BY t.tid"), $type);
}
while ($term = db_fetch_object($result)) {
$count[$type][$term->tid] = $term->c;
......
Markdown is supported
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