diff options
Diffstat (limited to 'modules/node.module')
-rw-r--r-- | modules/node.module | 105 |
1 files changed, 10 insertions, 95 deletions
diff --git a/modules/node.module b/modules/node.module index 557251781..b21c4ea5e 100644 --- a/modules/node.module +++ b/modules/node.module @@ -585,7 +585,7 @@ function node_search($op = 'search', $keys = null) { $remaining = db_result(db_query('SELECT COUNT(*) FROM {node} n LEFT JOIN {node_comment_statistics} c ON n.nid = c.nid WHERE n.status = 1 AND n.moderate = 0 AND (n.created > %d OR n.changed > %d OR c.last_comment_timestamp > %d) ORDER BY GREATEST(n.created, n.changed, c.last_comment_timestamp) ASC', $last, $last, $last)); return array('remaining' => $remaining, 'total' => $total); case 'search': - list($join, $where) = _node_rewrite_sql(); + list($join, $where) = _db_rewrite_sql(); $find = do_search($keys, 'node', 'INNER JOIN {node} n ON n.nid = i.sid '. $join .' INNER JOIN {users} u ON n.uid = u.uid', 'n.status = 1 AND '. $where); $results = array(); foreach ($find as $item) { @@ -1018,7 +1018,7 @@ function node_feed($nodes = 0, $channel = array()) { global $base_url, $locale; if (!$nodes) { - $nodes = db_query_range(node_rewrite_sql('SELECT n.nid FROM {node} n WHERE n.promote = 1 AND n.status = 1 ORDER BY n.created DESC'), 0, 15); + $nodes = db_query_range(db_rewrite_sql('SELECT n.nid FROM {node} n WHERE n.promote = 1 AND n.status = 1 ORDER BY n.created DESC'), 0, 15); } while ($node = db_fetch_object($nodes)) { @@ -1484,7 +1484,7 @@ function node_delete($edit) { * Generate a listing of promoted nodes. */ function node_page_default() { - $result = pager_query(node_rewrite_sql('SELECT n.nid, n.sticky, n.created FROM {node} n WHERE n.promote = 1 AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC'), variable_get('default_nodes_main', 10)); + $result = pager_query(db_rewrite_sql('SELECT n.nid, n.sticky, n.created FROM {node} n WHERE n.promote = 1 AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC'), variable_get('default_nodes_main', 10)); if (db_num_rows($result)) { drupal_set_html_head('<link rel="alternate" type="application/rss+xml" title="RSS" href="'. url('node/feed', NULL, NULL, TRUE) .'" />'); @@ -1808,100 +1808,15 @@ function node_access_grants($op, $uid = NULL) { */ /** - * Implementation of hook_node_rewrite_sql + * Implementation of hook_db_rewrite_sql */ -function node_node_rewrite_sql() { - $return['join'] = node_access_join_sql(); - $return['where'] = node_access_where_sql(); - $return['distinct'] = !empty($return['join']); - return $return; -} - -/** - * Helper function for node_rewrite_sql. - * - * Collects JOIN and WHERE statements via hook_sql. - * Decides whether to select nid or DISTINCT(nid) - * - * @param $query - * query to be rewritten - * @param $nid_alias - * Alias of the table which has the nid field for this query. Defaults to 'n'. - * @param $args - * array of additional args - * @return - * An associative array: join => join statements, where => where statements, nid_to_select => nid or DISTINCT(nid) - */ -function _node_rewrite_sql($query = '', $nid_alias = 'n', $args = array()) { - $where = array(); - $join = array(); - $distinct = FALSE; - foreach (module_implements('node_rewrite_sql') as $module) { - $result = module_invoke($module, 'node_rewrite_sql', $query, $nid_alias, $args); - if (is_array($result)) { - if (isset($result['where'])) { - $where[] .= $result['where']; - } - if (isset($result['join'])) { - $join[] .= $result['join']; - } - if (isset($result['distinct']) && $result['distinct']) { - $distinct = TRUE; - } - } - elseif (isset($result)) { - $where[] .= $result; - } - } - - $where = empty($where) ? '' : '('. implode(') AND (',$where).')'; - $join = empty($join) ? '' : implode(' ',$join); - - return array($join, $where, $distinct ? 'DISTINCT('. $nid_alias .'.nid)' : $nid_alias .'.nid'); -} - -/** - * Rewrites node queries. - * - * @param $query - * query to be rewritten - * @param $nid_alias - * Alias of the table which has the nid field for this query. Defaults to 'n'. - * @param $args - * an array of arguments, passed to the implementations of hook_node_rewrite_sql - * @return - * The original query with JOIN and WHERE statements inserted from hook_node_rewrite_sql implementations. nid is rewritten if needed. - */ -function node_rewrite_sql($query, $nid_alias = 'n', $args = array()) { - list($join, $where, $nid_to_select) = _node_rewrite_sql($query, $nid_alias, $args); - - // (?<!text) is a negative look-behind (no need to rewrite queries that already use DISTINCT). - $query = preg_replace('/(SELECT.*)('. $nid_alias .'\.)?(?<!DISTINCT\()(?<!DISTINCT\('. $nid_alias .'\.)nid(.*FROM)/AUsi', '\1'. $nid_to_select .'\3', $query); - - $query = preg_replace('|FROM[^[:upper:]/,]+|','\0 '. $join .' ', $query); - if (strpos($query, 'WHERE')) { - $replace = 'WHERE'; - $add = 'AND'; - } - elseif (strpos($query, 'GROUP')) { - $replace = 'GROUP'; - $add = 'GROUP'; - } - elseif (strpos($query, 'ORDER')) { - $replace = 'ORDER'; - $add = 'ORDER'; - } - elseif (strpos($query, 'LIMIT')) { - $replace = 'LIMIT'; - $add = 'LIMIT'; - } - else { - $query .= ' WHERE '. $where; - } - if (isset($replace)) { - $query = str_replace($replace, 'WHERE '. $where .' '. $add .' ', $query); +function node_db_rewrite_sql($query, $primary_table) { + if ($primary_table=='n') { + $return['join'] = node_access_join_sql(); + $return['where'] = node_access_where_sql(); + $return['distinct'] = !empty($return['join']); + return $return; } - return $query; } ?> |