/[drupal]/contributions/modules/yahoo_terms/yahoo_terms.module
ViewVC logotype

Diff of /contributions/modules/yahoo_terms/yahoo_terms.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1.4.5, Wed May 23 20:00:26 2007 UTC revision 1.1.4.6, Mon Jun 25 10:35:31 2007 UTC
# Line 90  function yahoo_terms_text_get_keywords($ Line 90  function yahoo_terms_text_get_keywords($
90  }  }
91    
92  /**  /**
93     * Determine the vocab for the given node
94     */
95    function _yahoo_terms_which_vocab($type) {
96      $vid = variable_get('yahoo_terms_vocab_for_'. $type, FALSE);
97      $vid = ($vid === FALSE) ? variable_get('yahoo_terms_vocab', FALSE) : $vid;  // Fall back to default if needed
98      return $vid;
99    }
100    
101    /**
102   * Parse the given node's body and assign   * Parse the given node's body and assign
103   *   *
104   * @param object $node   * @param object $node
# Line 107  function yahoo_terms_node_assign_keyword Line 116  function yahoo_terms_node_assign_keyword
116    $limit = ($limit === FALSE) ? variable_get('yahoo_terms_taxonomy_limit', -1) : $limit;    $limit = ($limit === FALSE) ? variable_get('yahoo_terms_taxonomy_limit', -1) : $limit;
117    $terms = yahoo_terms_text_get_keywords($text, $query, $limit, $blacklist);    $terms = yahoo_terms_text_get_keywords($text, $query, $limit, $blacklist);
118    $tids = _yahoo_terms_create_vocabulary_items($terms, $vid, $static);    $tids = _yahoo_terms_create_vocabulary_items($terms, $vid, $static);
119    $node->taxonomy = array_merge($node->taxonomy, $tids);    $tids_in_node = array_keys($node->taxonomy);
120    taxonomy_node_save($node->nid, $node->taxonomy);    $tids_fetched = array_keys($tids);
121      // Union of these arrays
122      $tids_now = $tids_in_node + $tids_fetched;
123      taxonomy_node_save($node->nid, $tids_now);
124  }  }
125    
126  /**  /**
# Line 129  function _yahoo_terms_create_vocabulary_ Line 141  function _yahoo_terms_create_vocabulary_
141    }    }
142    $tids = array();    $tids = array();
143    foreach ($keywords as $term) {    foreach ($keywords as $term) {
144      $curr_term = taxonomy_get_term_by_name($term);      $curr_terms = taxonomy_get_term_by_name($term);
145      if (count($curr_term) != 0) {      if (count($curr_terms) != 0) {
146        $curr_term = $curr_term[0];        foreach ($curr_terms as $curr_term) {
147        if ($curr_term->vid == $vid) {          if ($curr_term->vid == $vid) {
148          $tids[] = $curr_term;            $tids[$curr_term->tid] = $curr_term;
149        } else if ($static == FALSE) {          }
150          }
151          if (count($tids) == 0 and $static == FALSE) {
152          $new_term['name'] = $term;          $new_term['name'] = $term;
153          $new_term['vid'] = $vid;          $new_term['vid'] = $vid;
154          taxonomy_save_term($new_term);          taxonomy_save_term($new_term);
155          $tids[] = taxonomy_get_term($new_term['tid']);          $tids[$new_term['tid']] = taxonomy_get_term($new_term['tid']);
156          unset($new_term);          unset($new_term);
157        }        }
158      }      }
# Line 147  function _yahoo_terms_create_vocabulary_ Line 161  function _yahoo_terms_create_vocabulary_
161          $new_term['name'] = $term;          $new_term['name'] = $term;
162          $new_term['vid'] = $vid;          $new_term['vid'] = $vid;
163          taxonomy_save_term($new_term);          taxonomy_save_term($new_term);
164          $tids[] = taxonomy_get_term($new_term['tid']);          $tids[$new_term['tid']] = taxonomy_get_term($new_term['tid']);
165          unset($new_term);          unset($new_term);
166        }        }
167      }      }
# Line 212  function yahoo_terms_perm() { Line 226  function yahoo_terms_perm() {
226   * Implements hook_link()   * Implements hook_link()
227   */   */
228  function yahoo_terms_link($type, $node = NULL, $teaser = FALSE) {  function yahoo_terms_link($type, $node = NULL, $teaser = FALSE) {
229    if ($type == 'node' && $node != NULL) {    $vid = _yahoo_terms_which_vocab($node->type);
230      if(user_access(YAHOO_TERMS_USE) && user_access("administer nodes") && variable_get('yahoo_terms_enable_link', FALSE)) {    $count = db_num_rows(db_query("SELECT vid FROM {vocabulary_node_types} WHERE vid = %d AND type = '%s'", $vid, $node->type));
231        $links["yahoo_terms_extract"] = array('title' => t('extract terms'), 'href' => 'yahoo_terms/'. $node->nid);    if ($count != 0) {
232        if ($type == 'node' && $node != NULL) {
233          if (user_access(YAHOO_TERMS_USE) && user_access("administer nodes") && variable_get('yahoo_terms_enable_link', FALSE)) {
234            $links["yahoo_terms_extract"] = array('title' => t('Extract the terms with Yahoo Term Extractor'), 'href' => 'yahoo_terms/'. $node->nid);
235          }
236      }      }
237    }    }
238    return $links;    return $links;
# Line 223  function yahoo_terms_link($type, $node = Line 241  function yahoo_terms_link($type, $node =
241  /**  /**
242   * Implements hook_menu()   * Implements hook_menu()
243   */   */
244  function yahoo_terms_menu() {  function yahoo_terms_menu($may_cache) {
245    
246    $items = array();    $items = array();
247    $items[] = array('path' => 'yahoo_terms', 'title' => t('Extraction...'),    if ($may_cache) {
248        'callback' => '_yahoo_terms_process_node', 'access' => user_access(YAHOO_TERMS_USE),      $items[] = array('path' => 'yahoo_terms', 'title' => t('Extraction...'),
249        'type' => MENU_CALLBACK);          'callback' => '_yahoo_terms_process_node', 'access' => user_access(YAHOO_TERMS_USE),
250    $items[] = array('path' => 'admin/settings/yahoo_terms', 'title' => t('Yahoo Terms'),          'type' => MENU_CALLBACK);
251        'callback' => 'drupal_get_form', 'callback arguments' => array('yahoo_terms_admin_settings'));      $items[] = array('path' => 'admin/settings/yahoo_terms', 'title' => t('Yahoo Terms'),
252    $items[] = array('path' => 'yahoo_terms/batch', 'title' => t('Yahoo Terms batch processing'),          'callback' => 'drupal_get_form', 'callback arguments' => array('yahoo_terms_admin_settings'));
253        'callback' => 'yahoo_terms_batch_page', 'access' => user_access(YAHOO_TERMS_USE) && user_access('administer nodes'));      $items[] = array('path' => 'yahoo_terms/batch', 'title' => t('Yahoo Terms batch processing'),
254            'callback' => 'yahoo_terms_batch_page', 'access' => user_access(YAHOO_TERMS_USE) && user_access('administer nodes'));
255      }
256    return $items;    return $items;
257  }  }
258    
# Line 243  function yahoo_terms_menu() { Line 264  function yahoo_terms_menu() {
264  function _yahoo_terms_process_node() {  function _yahoo_terms_process_node() {
265    $nid = arg(1);    $nid = arg(1);
266    if (is_numeric($nid)) {    if (is_numeric($nid)) {
267      $vid = variable_get('yahoo_terms_vocab', FALSE);      $node = node_load($nid);
268        $vid = _yahoo_terms_which_vocab($node->type);
269      if ($vid === FALSE) {      if ($vid === FALSE) {
270        return t("Please do the settings of Yahoo Terms module before use.");        return t("Please do the settings of Yahoo Terms module before use.");
271      }      }
     $node = node_load($nid);  
272      yahoo_terms_node_assign_keywords($node, $query, array(), $vid);      yahoo_terms_node_assign_keywords($node, $query, array(), $vid);
273    }    }
274    else {    else {
# Line 262  function _yahoo_terms_process_node() { Line 283  function _yahoo_terms_process_node() {
283  function yahoo_terms_nodeapi(&$node, $op, $vid = NULL, $query = NULL) {  function yahoo_terms_nodeapi(&$node, $op, $vid = NULL, $query = NULL) {
284    switch ($op) {    switch ($op) {
285      case 'terms_tagging':      case 'terms_tagging':
286        $vid = ($vid === NULL) ? variable_get('yahoo_terms_vocab_for_'. $node->type, FALSE) : $vid;        $vid = _yahoo_terms_which_vocab($node->type);
       $vid = ($vid === FALSE) ? variable_get('yahoo_terms_vocab', FALSE) : $vid;  // Fall back to default if needed  
287        if (is_numeric($vid)) {        if (is_numeric($vid)) {
288          $query = ($query === NULL) ? "" : $query;          $query = ($query === NULL) ? "" : $query;
289          yahoo_terms_node_assign_keywords($node, $query, array(), $vid);          yahoo_terms_node_assign_keywords($node, $query, array(), $vid);
# Line 273  function yahoo_terms_nodeapi(&$node, $op Line 293  function yahoo_terms_nodeapi(&$node, $op
293        }        }
294        break;        break;
295      case 'insert':      case 'insert':
296        $vid = variable_get('yahoo_terms_vocab_for_'. $node->type, FALSE);        $vid = _yahoo_terms_which_vocab($node->type);
       $vid = ($vid === FALSE) ? variable_get('yahoo_terms_vocab', FALSE) : $vid;  // Fall back to default if needed  
297        $automatic = variable_get('yahoo_terms_enable_runtime', YAHOO_TERMS_ONLY_MANUALLY);        $automatic = variable_get('yahoo_terms_enable_runtime', YAHOO_TERMS_ONLY_MANUALLY);
298        if (is_numeric($vid) && ($automatic == YAHOO_TERMS_ON_NODE_CREATION || $automatic == YAHOO_TERMS_ON_NODE_CREATION_AND_UPDATE)) {        if (is_numeric($vid) && ($automatic == YAHOO_TERMS_ON_NODE_CREATION || $automatic == YAHOO_TERMS_ON_NODE_CREATION_AND_UPDATE)) {
299          yahoo_terms_node_assign_keywords($node, "", array(), $vid);          yahoo_terms_node_assign_keywords($node, "", array(), $vid);
300        }        }
301        break;        break;
302      case 'update':      case 'update':
303        $vid = variable_get('yahoo_terms_vocab_for_'. $node->type, FALSE);        $vid = _yahoo_terms_which_vocab($node->type);
       $vid = ($vid === FALSE) ? variable_get('yahoo_terms_vocab', FALSE) : $vid;  // Fall back to default if needed  
304        $automatic = variable_get('yahoo_terms_enable_runtime', YAHOO_TERMS_ONLY_MANUALLY);        $automatic = variable_get('yahoo_terms_enable_runtime', YAHOO_TERMS_ONLY_MANUALLY);
305        if (is_numeric($vid) && $automatic == YAHOO_TERMS_ON_NODE_CREATION_AND_UPDATE) {        if (is_numeric($vid) && $automatic == YAHOO_TERMS_ON_NODE_CREATION_AND_UPDATE) {
306          yahoo_terms_node_assign_keywords($node, "", array(), $vid);          yahoo_terms_node_assign_keywords($node, "", array(), $vid);
# Line 299  function yahoo_terms_nodeapi(&$node, $op Line 317  function yahoo_terms_nodeapi(&$node, $op
317    
318  function yahoo_terms_form_alter($form_id, &$form) {  function yahoo_terms_form_alter($form_id, &$form) {
319    if (isset($form['#node_type']) && 'node_type_form' == $form_id) {    if (isset($form['#node_type']) && 'node_type_form' == $form_id) {
     $select_voc = _yahoo_terms_vocab_select();  
320      $node_type = $form['old_type']['#value'];      $node_type = $form['old_type']['#value'];
321        $select_voc = _yahoo_terms_vocab_select($node_type);
322      $form['workflow']['yahoo_terms'] = array(      $form['workflow']['yahoo_terms'] = array(
323        '#type' => 'fieldset',        '#type' => 'fieldset',
324        '#title' => t('Yahoo Terms options specifically for %type', array("%type" => $node_type)),        '#title' => t('Yahoo Terms options specifically for %type', array("%type" => $node_type)),
# Line 371  function yahoo_terms_batch() { Line 389  function yahoo_terms_batch() {
389      '#type' => 'checkbox',      '#type' => 'checkbox',
390      '#default_value' => FALSE      '#default_value' => FALSE
391    );    );
392      $form['yahoo_terms_batch']['limit_number'] = array(
393        '#title' => t('Only process n nodes in one turn'),
394        '#type' => 'select',
395        '#options' => array('0' => 'No limit', '50' => '50', '100' => '100', '500' => '500', '1000' => '1000'),
396        '#default_value' => 0
397      );
398    $form['submit'] = array(    $form['submit'] = array(
399      '#type' => 'submit',      '#type' => 'submit',
400      '#value' => 'Start tagging'      '#value' => 'Start tagging'
# Line 384  function yahoo_terms_batch() { Line 408  function yahoo_terms_batch() {
408  function yahoo_terms_batch_submit($form_id, $form_values) {  function yahoo_terms_batch_submit($form_id, $form_values) {
409    timer_start("yahoo_terms_batch");    timer_start("yahoo_terms_batch");
410    $num_of_processed = 0;    $num_of_processed = 0;
411    $result = db_query("SELECT nid FROM {node} WHERE type = '%s'", $form_values['type']);    $limit_number = $form_values['limit_number'];
412      if ($limit_number == 0) {
413        $result = db_query("SELECT nid FROM {node} WHERE type = '%s'",
414                           $form_values['type']);
415      }
416      else {
417        if (!$form_values['rewrite']) {
418          $result = db_query("SELECT n.nid FROM {node} n LEFT JOIN {term_node} t ON t.nid=n.nid WHERE t.nid IS NULL and type = '%s' ORDER BY created DESC LIMIT %d",
419                              $form_values['type'], $form_values['limit_number']
420          );
421        }
422        else {
423          $result = db_query("SELECT nid FROM {node}
424                              WHERE type = '%s' LIMIT %d",
425                              $form_values['type'], $form_values['limit_number']
426          );
427        }
428      }
429    while ($node = db_fetch_array($result)) {    while ($node = db_fetch_array($result)) {
430      $node = node_load($node['nid']);      $node = node_load($node['nid']);
431      if ( (!isset($node->taxonomy) || count($node->taxonomy) == 0) || $form_values['rewrite']) {      if ( (!isset($node->taxonomy) || count($node->taxonomy) == 0) || $form_values['rewrite']) {
# Line 405  function yahoo_terms_batch_page() { Line 446  function yahoo_terms_batch_page() {
446   *   *
447   * @return array   * @return array
448   */   */
449  function _yahoo_terms_vocab_select() {  function _yahoo_terms_vocab_select($type = NULL) {
450    $vocabularies = db_query('SELECT v.vid, v.name FROM {vocabulary} v WHERE module="taxonomy"');    if ($type == NULL) {
451      $select_voc = array();      $vocabularies = db_query("SELECT v.vid, v.name FROM {vocabulary} v WHERE module='taxonomy'");
452      while ($voc = db_fetch_array($vocabularies)) {    }
453        $select_voc[$voc['vid']] = $voc['name'];    else {
454      }      $vocabularies = db_query("SELECT v.vid, v.name FROM {vocabulary} v LEFT JOIN {vocabulary_node_types} vt ON v.vid = vt.vid  WHERE module='taxonomy' AND vt.type = '%s'", $type);
455      if (count($select_voc) < 1) {    }
456        $select_voc[0] = 'No vocabularies found';    $select_voc = array();
457      }    while ($voc = db_fetch_array($vocabularies)) {
458      return $select_voc;      $select_voc[$voc['vid']] = $voc['name'];
459      }
460      if (count($select_voc) < 1) {
461        $select_voc[0] = 'No vocabularies found';
462      }
463      return $select_voc;
464  }  }
465    
466  /**  /**
# Line 427  function yahoo_terms_cron() { Line 473  function yahoo_terms_cron() {
473      if (variable_get('yahoo_terms_process_existing_'. $type, FALSE) == TRUE) {      if (variable_get('yahoo_terms_process_existing_'. $type, FALSE) == TRUE) {
474        variable_set('yahoo_terms_process_existing_'. $type, FALSE);        variable_set('yahoo_terms_process_existing_'. $type, FALSE);
475        $result = db_query("SELECT nid FROM {node} WHERE type = '%s'", $type);        $result = db_query("SELECT nid FROM {node} WHERE type = '%s'", $type);
476        $vid = variable_get('yahoo_terms_vocab_for_'. $node->type, FALSE);        $vid = _yahoo_terms_which_vocab($type);
       $vid = ($vid === FALSE) ? variable_get('yahoo_terms_vocab', FALSE) : $vid;  // Fall back to default if needed  
477        while ($node = db_fetch_array($result)) {        while ($node = db_fetch_array($result)) {
478          $node = node_load($node['nid']);          $node = node_load($node['nid']);
479          yahoo_terms_node_assign_keywords($node, "", array(), $vid);          yahoo_terms_node_assign_keywords($node, "", array(), $vid);

Legend:
Removed from v.1.1.4.5  
changed lines
  Added in v.1.1.4.6

  ViewVC Help
Powered by ViewVC 1.1.2