diff -u'rNF^function' tagadelic/tagadelic.module tagadelic_new/tagadelic.module --- tagadelic/tagadelic.module 2006-06-13 21:01:53.000000000 -0400 +++ tagadelic_new/tagadelic.module 2006-08-14 11:25:43.000000000 -0400 @@ -38,7 +38,12 @@ function tagadelic_menu($may_cache) { 'callback' => 'tagadelic_page_chunk', 'access' => user_access('access content'), 'type' => MENU_CALLBACK); - foreach (taxonomy_get_vocabularies($type = NULL) as $vocabulary) { + $items[] = array('title' => t('update tagadelic'), + 'path' => "admin/settings/tagadelic/update", + 'callback' => 'tagadelic_update', + 'access' => user_access('administer taxonomy'), + 'type' => MENU_NORMAL_ITEM); + foreach (taxonomy_get_vocabularies($type = NULL) as $vocabulary) { $items[] = array('title' => $vocabulary->name, 'path' => "tagadelic/chunk/$vocabulary->vid", 'callback' => 'tagadelic_page_chunk', @@ -51,14 +56,149 @@ function tagadelic_menu($may_cache) { /** * Implementation of hook_nodeapi - * Yuo will have a nice variable in $node available for processing tags! + * You will have a nice variable in $node available for processing tags! */ function tagadelic_nodeapi($node, $op) { - if ($op == 'load') { +/* if ($op == 'load') { $output['tags'] = tagadelic_node_get_terms($node); return $output; } +*/ + + switch ($op) { + case 'load': + $output['tags'] = tagadelic_node_get_terms($node); + return $output; + break; + case 'insert': + case 'update': + tagadelic_index_node('save', $node); + break; + case 'delete': + tagadelic_index_node('delete', $node); + break; + } +} + +/** + * Implementation of hook_taxonomy + **/ + +function tagadelic_taxonomy($op, $type, $object = NULL) { + switch ($op) { + case 'delete': + tagadelic_delete_term($type, $object); + break; + case 'update': + if ($type == 'term') { + $sql = "UPDATE {tagadelic} SET name = '%s' WHERE tid = %d"; + db_query($sql, $object['name'], $object['tid']); + } + break; + } +} + +/** + * Store tagedalic variables in a table rather than on-the-fly + **/ + +function tagadelic_index_node($op, $node) { + $tags = array(); + if(!empty($node->taxonomy)) { + foreach ($node->taxonomy as $key => $value) { + if (is_numeric($key) && is_array($value)) { + foreach ($value as $k => $v) { + $tags[] = taxonomy_get_term($v); + } + } + } + } + + if (!empty($node->taxonomy['tags'])) { + foreach ($node->taxonomy['tags'] as $vid => $tax) { + $words = explode(',', $tax); + foreach ($words as $term) { + $data = taxonomy_get_term_by_name($term); + if(!empty($data)) { + foreach ($data as $item) { + if ($item->vid == $vid) { + $tags[] = $item; + } + } + } + } + } + } + switch ($op) { + case 'save': + case 'delete': + $tagwords = array(); + if (!empty($tags)) { + foreach ($tags as $term) { + if ($term->tid > 0) { + $check = db_fetch_object(db_query("SELECT node_count FROM {tagadelic} WHERE tid = %d", $term->tid)); + $sql = "SELECT COUNT(nid) as count FROM {term_node} WHERE tid = %d"; + $count = db_queryd($sql, $term->tid); + $term->node_count = db_fetch_object($count)->count; + if (empty($check)) { + $query = "INSERT into {tagadelic} (tid, name, vid, node_count) VALUES (%d, '%s', %d, %d)"; + db_queryd($query, $term->tid, $term->name, $term->vid, $term->node_count); + } + else { + $query = "UPDATE {tagadelic} SET node_count = %d WHERE tid = %d"; + db_queryd($query, $term->node_count, $term->tid); + } + } + } + } + break; + } +} + +/** + * Store tagedelic variables in a table rather than on-the-fly + **/ + +function tagadelic_delete_term($type, $object) { + switch ($type) { + case 'term': + $sql = "DELETE from {tagadelic} WHERE tid = %d"; + db_query($sql, $object['tid']); + break; + case 'vocabulary': + $sql = "DELETE from {tagadelic} WHERE vid = %d"; + db_query($sql, $object['vid']); + break; + } +} + +/** + * Update tagadelic terms to the new database table + **/ + +function tagadelic_update() { + // get all terms + $terms = array(); + + // empty the tagadelic table + db_queryd("TRUNCATE TABLE {tagadelic}"); + + // get new term data + $sql = "SELECT tid, vid, name FROM {term_data}"; + $result = db_query($sql); + + while ($term = db_fetch_object($result)) { + $sql = "SELECT COUNT(nid) as node_count FROM {term_node} WHERE tid = %d"; + $term->node_count = db_fetch_object(db_query($sql, $term->tid))->node_count; + if ($term->node_count > 0) { + $query = "INSERT into {tagadelic} (tid, name, vid, node_count) VALUES (%d, '%s', %d, %d)"; + db_query($query, $term->tid, $term->name, $term->vid, $term->node_count); + } + } + drupal_set_message(t('Tagadelic updated successfully.')); + drupal_goto('admin/settings/tagadelic'); } + /** * Implementation of hook_settings @@ -177,19 +317,21 @@ function tagadelic_tags_lists($node) { * API that returns an array with weighted tags * This is the hard part. People with better ideas are very very welcome to send these to ber@webschuur.com. Distribution is one thing that needs attention. */ -function tagadelic_get_weighted_tags($vids, $steps = 6, $size = 60) { +function tagadelic_get_weighted_tags($vids, $steps = 10, $size = 1000) { if (!is_array($vids) || count($vids) == 0) { return array(); } // Fetch tags - $result = db_query_range('SELECT COUNT(*) AS count, d.tid, d.name, d.vid FROM {term_data} d INNER JOIN {term_node} n ON d.tid = n.tid WHERE d.vid IN ('. substr(str_repeat('%d,', count($vids)), 0, -1) .') GROUP BY d.tid, d.name ORDER BY count DESC', $vids, 0, $size); + // $result = db_query_range('SELECT COUNT(*) AS count, d.tid, d.name, d.vid FROM {term_data} d INNER JOIN {term_node} n ON d.tid = n.tid WHERE d.vid IN ('. substr(str_repeat('%d,', count($vids)), 0, -1) .') GROUP BY d.tid, d.name ORDER BY count DESC', $vids, 0, $size); + $result = db_query_range("SELECT tid, name, vid, node_count FROM {tagadelic} WHERE vid IN (". substr(str_repeat('%d,', count($vids)), 0, -1) .")", $vids, 0, $size); // Find minimum and maximum log-count. $tags = array(); $min = 1e9; $max = -1e9; while ($tag = db_fetch_object($result)) { + $tag->count = $tag->node_count; $tag->number_of_posts = $tag->count; $tag->count = log($tag->count); $min = min($min, $tag->count);