/[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, Tue Apr 17 19:14:17 2007 UTC revision 1.2, Tue Nov 25 08:55:37 2008 UTC
# Line 35  define('YAHOO_TERMS_ON_NODE_CREATION', 1 Line 35  define('YAHOO_TERMS_ON_NODE_CREATION', 1
35   * Extract terms on nodeapi - creation and update op   * Extract terms on nodeapi - creation and update op
36   */   */
37  define('YAHOO_TERMS_ON_NODE_CREATION_AND_UPDATE', 2);  define('YAHOO_TERMS_ON_NODE_CREATION_AND_UPDATE', 2);
   
 function yahoo_terms_help($section) {  
   switch ($section) {  
      case 'admin/modules#description':  
        return t('Extract terms from English language texts and nodes and save into the taxonomy hierarchy');  
        break;  
    }  
 }  
38  /**  /**
39   * Parse the given text with the term extractor service.   * Parse the given text with the term extractor service.
40   *   *
# Line 98  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 115  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, $tids);    $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 136  function _yahoo_terms_create_vocabulary_ Line 140  function _yahoo_terms_create_vocabulary_
140      return FALSE;      return FALSE;
141    }    }
142    $tids = array();    $tids = array();
143    foreach ($keywords as $term) {    if (is_array($keywords)) {
144      $curr_term = taxonomy_get_term_by_name($term);      foreach ($keywords as $term) {
145      if (count($curr_term) != 0) {        $curr_terms = taxonomy_get_term_by_name($term);
146        $curr_term = $curr_term[0];        if (count($curr_terms) != 0) {
147        if ($curr_term->vid == $vid) {          foreach ($curr_terms as $curr_term) {
148          $tids[] = $curr_term;            if ($curr_term->vid == $vid) {
149        } else if ($static == FALSE) {              $tids[$curr_term->tid] = $curr_term;
150          $new_term['name'] = $term;            }
151          $new_term['vid'] = $vid;          }
152          taxonomy_save_term($new_term);          if (count($tids) == 0 and $static == FALSE) {
153          $tids[] = taxonomy_get_term($new_term['tid']);            $new_term['name'] = $term;
154          unset($new_term);            $new_term['vid'] = $vid;
155              taxonomy_save_term($new_term);
156              $tids[$new_term['tid']] = taxonomy_get_term($new_term['tid']);
157              unset($new_term);
158            }
159        }        }
160      }        else {
161      else {          if ($static == FALSE) {
162        if ($static == FALSE) {            $new_term['name'] = $term;
163          $new_term['name'] = $term;            $new_term['vid'] = $vid;
164          $new_term['vid'] = $vid;            taxonomy_save_term($new_term);
165          taxonomy_save_term($new_term);            $tids[$new_term['tid']] = taxonomy_get_term($new_term['tid']);
166          $tids[] = taxonomy_get_term($new_term['tid']);            unset($new_term);
167          unset($new_term);          }
168        }        }
169      }      }
170    }    }
# Line 166  function _yahoo_terms_create_vocabulary_ Line 174  function _yahoo_terms_create_vocabulary_
174  /**  /**
175   * The settings form   * The settings form
176   */   */
177  function yahoo_terms_settings() {  function yahoo_terms_admin_settings() {
178    $form['yahoo_terms_appid'] = array(    $form['yahoo_terms_appid'] = array(
179      '#title' => t('Yahoo Application ID'),      '#title' => t('Yahoo Application ID'),
180      '#description' => t('An Application ID is a string that uniquely identifies your application.      '#description' => t('An Application ID is a string that uniquely identifies your application.
# Line 178  function yahoo_terms_settings() { Line 186  function yahoo_terms_settings() {
186    );    );
187    
188    $form['yahoo_terms_taxonomy_limit'] = array(    $form['yahoo_terms_taxonomy_limit'] = array(
189      '#title' => t('Maximal number of terms'),      '#title' => t('Maximum number of terms'),
190      '#description' => t('Maximal number of terms which are associated to one article. -1 means no limit.'),      '#description' => t('Maximum number of terms per node. -1 means no limit.'),
191      '#type' => 'textfield',      '#type' => 'textfield',
192      '#required' => TRUE,      '#required' => TRUE,
193      '#default_value' => variable_get('yahoo_terms_taxonomy_limit', -1),      '#default_value' => variable_get('yahoo_terms_taxonomy_limit', -1),
194    );    );
195    $form['yahoo_terms_enable_link'] = array(    $form['yahoo_terms_enable_link'] = array(
196      '#title' => t('Show per-node manual term extraction link'),      '#title' => t('Show manual term extraction link on nodes'),
197      '#description' => t('Maximal number of terms which are associated to one article. -1 means no limit.'),      '#description' => t('If checked, an "extract terms" link will show on every node.'),
198      '#type' => 'checkbox',      '#type' => 'checkbox',
199      '#default_value' => variable_get('yahoo_terms_enable_link', FALSE),      '#default_value' => variable_get('yahoo_terms_enable_link', FALSE),
200    );    );
# Line 200  function yahoo_terms_settings() { Line 208  function yahoo_terms_settings() {
208    $select_voc = _yahoo_terms_vocab_select();    $select_voc = _yahoo_terms_vocab_select();
209    $form['yahoo_terms_vocab'] = array(    $form['yahoo_terms_vocab'] = array(
210      '#title' => t('Default vocabulary for Yahoo Terms'),      '#title' => t('Default vocabulary for Yahoo Terms'),
211      '#description' => t('If not a different vocabulary is specified for a leech, this one will be used.'),      '#description' => t('If no other module specifies a different vocabulary, this one will be used.'),
212      '#type' => 'select',      '#type' => 'select',
213      '#options' => $select_voc,      '#options' => $select_voc,
214      '#required' => TRUE,      '#required' => TRUE,
215      '#default_value' => variable_get('yahoo_terms_vocab', ''),      '#default_value' => variable_get('yahoo_terms_vocab', ''),
216    );    );
217    return $form;    return system_settings_form($form);
218  }  }
219    
220  /**  /**
# Line 220  function yahoo_terms_perm() { Line 228  function yahoo_terms_perm() {
228   * Implements hook_link()   * Implements hook_link()
229   */   */
230  function yahoo_terms_link($type, $node = NULL, $teaser = FALSE) {  function yahoo_terms_link($type, $node = NULL, $teaser = FALSE) {
231    if ($type == 'node' && $node != NULL) {    $vid = _yahoo_terms_which_vocab($node->type);
232      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));
233        $links[] = l(t('Extract the terms with Yahoo Term Extractor'), 'yahoo_terms/'. $node->nid);    if ($count != 0) {
234        if ($type == 'node' && $node != NULL) {
235          if (user_access(YAHOO_TERMS_USE) && user_access("administer nodes") && variable_get('yahoo_terms_enable_link', FALSE)) {
236            $links["yahoo_terms_extract"] = array('title' => t('Extract terms'), 'href' => 'yahoo_terms/'. $node->nid);
237          }
238      }      }
239    }    }
240    return $links;    return $links;
# Line 231  function yahoo_terms_link($type, $node = Line 243  function yahoo_terms_link($type, $node =
243  /**  /**
244   * Implements hook_menu()   * Implements hook_menu()
245   */   */
246  function yahoo_terms_menu() {  function yahoo_terms_menu($may_cache) {
247    
248    $items = array();    $items = array();
249    $items[] = array('path' => 'yahoo_terms', 'title' => t('Extraction...'),    if ($may_cache) {
250        'callback' => '_yahoo_terms_process_node', 'access' => user_access(YAHOO_TERMS_USE),      $items[] = array('path' => 'yahoo_terms', 'title' => t('Extraction...'),
251        'type' => MENU_CALLBACK);          'callback' => '_yahoo_terms_process_node', 'access' => user_access(YAHOO_TERMS_USE),
252    $items[] = array('path' => 'yahoo_terms/batch', 'title' => t('Yahoo Terms batch processing'),          'type' => MENU_CALLBACK);
253        'callback' => 'yahoo_terms_batch', 'access' => user_access(YAHOO_TERMS_USE) && user_access('administer nodes'));      $items[] = array('path' => 'admin/settings/yahoo_terms', 'title' => t('Yahoo Terms'),
254            'callback' => 'drupal_get_form', 'callback arguments' => array('yahoo_terms_admin_settings'));
255        $items[] = array('path' => 'yahoo_terms/batch', 'title' => t('Yahoo Terms batch processing'),
256            'callback' => 'yahoo_terms_batch_page', 'access' => user_access(YAHOO_TERMS_USE) && user_access('administer nodes'));
257      }
258    return $items;    return $items;
259  }  }
260    
# Line 249  function yahoo_terms_menu() { Line 266  function yahoo_terms_menu() {
266  function _yahoo_terms_process_node() {  function _yahoo_terms_process_node() {
267    $nid = arg(1);    $nid = arg(1);
268    if (is_numeric($nid)) {    if (is_numeric($nid)) {
269      $vid = variable_get('yahoo_terms_vocab', FALSE);      $node = node_load($nid);
270        $vid = _yahoo_terms_which_vocab($node->type);
271      if ($vid === FALSE) {      if ($vid === FALSE) {
272        return t("Please do the settings of Yahoo Terms module before use.");        return t("Please do the settings of Yahoo Terms module before use.");
273      }      }
     $node = node_load($nid);  
274      yahoo_terms_node_assign_keywords($node, $query, array(), $vid);      yahoo_terms_node_assign_keywords($node, $query, array(), $vid);
275    }    }
276    else {    else {
# Line 265  function _yahoo_terms_process_node() { Line 282  function _yahoo_terms_process_node() {
282  /**  /**
283   * Implements of hook_nodeapi()   * Implements of hook_nodeapi()
284   */   */
285  function yahoo_terms_nodeapi(&$node, $op) {  function yahoo_terms_nodeapi(&$node, $op, $vid = NULL, $query = NULL) {
286    switch ($op) {    switch ($op) {
287      case 'terms_tagging':      case 'terms_tagging':
288        $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  
289        if (is_numeric($vid)) {        if (is_numeric($vid)) {
290          yahoo_terms_node_assign_keywords($node, "", array(), $vid);          $query = ($query === NULL) ? "" : $query;
291            yahoo_terms_node_assign_keywords($node, $query, array(), $vid);
292        }        }
293        else {        else {
294          return;          return;
295        }        }
296        break;        break;
297      case 'insert':      case 'insert':
298        $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  
299        $automatic = variable_get('yahoo_terms_enable_runtime', YAHOO_TERMS_ONLY_MANUALLY);        $automatic = variable_get('yahoo_terms_enable_runtime', YAHOO_TERMS_ONLY_MANUALLY);
300        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)) {
301          yahoo_terms_node_assign_keywords($node, "", array(), $vid);          yahoo_terms_node_assign_keywords($node, "", array(), $vid);
302        }        }
303        break;        break;
304      case 'update':      case 'update':
305        $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  
306        $automatic = variable_get('yahoo_terms_enable_runtime', YAHOO_TERMS_ONLY_MANUALLY);        $automatic = variable_get('yahoo_terms_enable_runtime', YAHOO_TERMS_ONLY_MANUALLY);
307        if (is_numeric($vid) && $automatic == YAHOO_TERMS_ON_NODE_CREATION_AND_UPDATE) {        if (is_numeric($vid) && $automatic == YAHOO_TERMS_ON_NODE_CREATION_AND_UPDATE) {
308          yahoo_terms_node_assign_keywords($node, "", array(), $vid);          yahoo_terms_node_assign_keywords($node, "", array(), $vid);
# Line 302  function yahoo_terms_nodeapi(&$node, $op Line 317  function yahoo_terms_nodeapi(&$node, $op
317    }    }
318  }  }
319    
320  function yahoo_terms_form_alter($form_id, &$form) {  function yahoo_terms_form_alter($form_id, &$form) {
321    if (strpos($form_id, '_node_settings') !== FALSE) {    if (isset($form['#node_type']) && 'node_type_form' == $form_id) {
322      $node_type = str_replace('_node_settings', '', $form_id);      $node_type = $form['old_type']['#value'];
323      $select_voc = _yahoo_terms_vocab_select();      $select_voc = _yahoo_terms_vocab_select($node_type);
324      $form['workflow']['yahoo_terms'] = array(      $form['workflow']['yahoo_terms'] = array(
325        '#type' => 'fieldset',        '#type' => 'fieldset',
326        '#title' => t('Yahoo Terms options specifically for %type', array("%type" => $node_type)),        '#title' => t('Yahoo Terms options specifically for %type', array("%type" => $node_type)),
327        '#collapsible' => TRUE,        '#collapsible' => TRUE,
328        '#collapsed' => FALSE        '#collapsed' => FALSE
329      );      );
330      $form['workflow']['yahoo_terms']['yahoo_terms_taxonomy_limit_for_'. $node_type] = array(      $form['workflow']['yahoo_terms']['yahoo_terms_taxonomy_limit_for'] = array(
331        '#title' => t('Maximal number of terms'),        '#title' => t('Maximal number of terms'),
332        '#description' => t('Maximal number of terms which are associated to one article. -1 means no limit.'),        '#description' => t('Maximal number of terms which are associated to one article. -1 means no limit.'),
333        '#type' => 'textfield',        '#type' => 'textfield',
334        '#required' => TRUE,        '#required' => TRUE,
335        '#default_value' => variable_get('yahoo_terms_taxonomy_limit_for_'. $node_type, -1),        '#default_value' => variable_get('yahoo_terms_taxonomy_limit_for_'. $node_type, -1),
336      );      );
337      $form['workflow']['yahoo_terms']['yahoo_terms_vocab_for_'. $node_type] = array(      $form['workflow']['yahoo_terms']['yahoo_terms_vocab_for'] = array(
338        '#title' => t('Default vocabulary for Yahoo Terms'),        '#title' => t('Default vocabulary for Yahoo Terms'),
339        '#description' => t('This is the vocabulary where the terms will be created'),        '#description' => t('This is the vocabulary where the terms will be created'),
340        '#type' => 'select',        '#type' => 'select',
# Line 327  function yahoo_terms_form_alter($form_id Line 342  function yahoo_terms_form_alter($form_id
342        '#required' => TRUE,        '#required' => TRUE,
343        '#default_value' => variable_get('yahoo_terms_vocab_for_'. $node_type, ''),        '#default_value' => variable_get('yahoo_terms_vocab_for_'. $node_type, ''),
344      );      );
345      $form['workflow']['yahoo_terms']['yahoo_terms_process_existing_'. $node_type] = array(      $form['workflow']['yahoo_terms']['yahoo_terms_process_existing'] = array(
346        '#title' => t('Process existing content in this type'),        '#title' => t('Process existing content of this type'),
347        '#description' => t('This function is done in cron-time, so a working and regularly called cron is        '#description' => t('This function is done on cron time, so a working and regularly called cron is
348                             required. All the assigned terms are dropped and replaced with the extracted ones.                             required. All previously assigned terms are dropped and replaced with the extracted ones.
349                             After the cron run this option will be unchecked, so if you check it on, it will                             After cron run this option will be unchecked, so if you check it, it will
350                             be processed once.'),                             be processed once.'),
351        '#type' => 'checkbox',        '#type' => 'checkbox',
352        '#return_value' => TRUE,        '#return_value' => TRUE,
# Line 376  function yahoo_terms_batch() { Line 391  function yahoo_terms_batch() {
391      '#type' => 'checkbox',      '#type' => 'checkbox',
392      '#default_value' => FALSE      '#default_value' => FALSE
393    );    );
394      $form['yahoo_terms_batch']['limit_number'] = array(
395        '#title' => t('Only process n nodes in one turn'),
396        '#type' => 'select',
397        '#options' => array('0' => 'No limit', '50' => '50', '100' => '100', '500' => '500', '1000' => '1000'),
398        '#default_value' => 0
399      );
400    $form['submit'] = array(    $form['submit'] = array(
401      '#type' => 'submit',      '#type' => 'submit',
402      '#value' => 'Start tagging'      '#value' => 'Start tagging'
403    );    );
404    return drupal_get_form('yahoo_terms_batch', $form);    return $form;
405  }  }
406    
407  /**  /**
# Line 389  function yahoo_terms_batch() { Line 410  function yahoo_terms_batch() {
410  function yahoo_terms_batch_submit($form_id, $form_values) {  function yahoo_terms_batch_submit($form_id, $form_values) {
411    timer_start("yahoo_terms_batch");    timer_start("yahoo_terms_batch");
412    $num_of_processed = 0;    $num_of_processed = 0;
413    $result = db_query("SELECT nid FROM {node} WHERE type = '%s'", $form_values['type']);    $limit_number = $form_values['limit_number'];
414      if ($limit_number == 0) {
415        $result = db_query("SELECT nid FROM {node} WHERE type = '%s'",
416                           $form_values['type']);
417      }
418      else {
419        if (!$form_values['rewrite']) {
420          $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",
421                              $form_values['type'], $form_values['limit_number']
422          );
423        }
424        else {
425          $result = db_query("SELECT nid FROM {node}
426                              WHERE type = '%s' LIMIT %d",
427                              $form_values['type'], $form_values['limit_number']
428          );
429        }
430      }
431    while ($node = db_fetch_array($result)) {    while ($node = db_fetch_array($result)) {
432      $node = node_load($node['nid']);      $node = node_load($node['nid']);
433      if ( (!isset($node->taxonomy) || count($node->taxonomy) == 0) || $form_values['rewrite']) {      if ( (!isset($node->taxonomy) || count($node->taxonomy) == 0) || $form_values['rewrite']) {
# Line 401  function yahoo_terms_batch_submit($form_ Line 439  function yahoo_terms_batch_submit($form_
439    drupal_set_message(t("%number node(s) are processed in %sec second", array("%number" => $num_of_processed, "%sec" => ($timer / 1000))));    drupal_set_message(t("%number node(s) are processed in %sec second", array("%number" => $num_of_processed, "%sec" => ($timer / 1000))));
440  }  }
441    
442    function yahoo_terms_batch_page() {
443      return drupal_get_form('yahoo_terms_batch');
444    }
445    
446  /**  /**
447   * Prepare an array to pass to the Forms API to select a vocabulary. For internal use   * Prepare an array to pass to the Forms API to select a vocabulary. For internal use
448   *   *
449   * @return array   * @return array
450   */   */
451  function _yahoo_terms_vocab_select() {  function _yahoo_terms_vocab_select($type = NULL) {
452    $vocabularies = db_query('SELECT v.vid, v.name FROM {vocabulary} v WHERE module="taxonomy"');    if ($type == NULL) {
453        $vocabularies = db_query("SELECT v.vid, v.name FROM {vocabulary} v WHERE module='taxonomy'");
454      }
455      else {
456        $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);
457      }
458      $select_voc = array();
459    while ($voc = db_fetch_array($vocabularies)) {    while ($voc = db_fetch_array($vocabularies)) {
460      $select_voc[$voc['vid']] = $voc['name'];      $select_voc[$voc['vid']] = $voc['name'];
461    }    }
# Line 427  function yahoo_terms_cron() { Line 475  function yahoo_terms_cron() {
475      if (variable_get('yahoo_terms_process_existing_'. $type, FALSE) == TRUE) {      if (variable_get('yahoo_terms_process_existing_'. $type, FALSE) == TRUE) {
476        variable_set('yahoo_terms_process_existing_'. $type, FALSE);        variable_set('yahoo_terms_process_existing_'. $type, FALSE);
477        $result = db_query("SELECT nid FROM {node} WHERE type = '%s'", $type);        $result = db_query("SELECT nid FROM {node} WHERE type = '%s'", $type);
478        $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  
479        while ($node = db_fetch_array($result)) {        while ($node = db_fetch_array($result)) {
480          $node = node_load($node['nid']);          $node = node_load($node['nid']);
481          yahoo_terms_node_assign_keywords($node, "", array(), $vid);          yahoo_terms_node_assign_keywords($node, "", array(), $vid);

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

  ViewVC Help
Powered by ViewVC 1.1.2