/[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.2, Tue Nov 25 08:55:37 2008 UTC revision 1.3, Tue Nov 25 08:59:27 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2    // $Id$
3  /**  /**
4   * @file   * @file
5   * Provides an interface to other modules to use Yahoo Term Extractor service   * Provides an interface to other modules to use Yahoo Term Extractor service
# Line 7  Line 8 
8   *   *
9   * @author   * @author
10   * Aron Novak <aron at novaak dot net>   * Aron Novak <aron at novaak dot net>
11   *   *
12   */   */
13    
14  /**  /**
# Line 74  function yahoo_terms_text_get_keywords($ Line 75  function yahoo_terms_text_get_keywords($
75    }    }
76    if ($limit != -1) {    if ($limit != -1) {
77      $result = array_slice($result['ResultSet']['Result'], 0, $limit);      $result = array_slice($result['ResultSet']['Result'], 0, $limit);
78    } else {    }
79      else {
80      $result = $result['ResultSet']['Result'];      $result = $result['ResultSet']['Result'];
81    }    }
82    $result = array_unique($result);    $result = array_unique($result);
83    // Drop the blacklist items    // Drop the blacklist items
84    if (count($blacklist) > 0) {    if (count($blacklist) > 0) {
85      foreach($result as $id => $term) {      foreach ($result as $id => $term) {
86        if(in_array($term, $blacklist)) {        if (in_array($term, $blacklist)) {
87          unset($result[$id]);          unset($result[$id]);
88        }        }
89      }      }
# Line 99  function _yahoo_terms_which_vocab($type) Line 101  function _yahoo_terms_which_vocab($type)
101  }  }
102    
103  /**  /**
104   * Parse the given node's body and assign   * Parse the given node's body and assign
105   *   *
106   * @param object $node   * @param object $node
107   * The node to be parsed   * The node to be parsed
# Line 116  function yahoo_terms_node_assign_keyword Line 118  function yahoo_terms_node_assign_keyword
118    $limit = ($limit === FALSE) ? variable_get('yahoo_terms_taxonomy_limit', -1) : $limit;    $limit = ($limit === FALSE) ? variable_get('yahoo_terms_taxonomy_limit', -1) : $limit;
119    $terms = yahoo_terms_text_get_keywords($text, $query, $limit, $blacklist);    $terms = yahoo_terms_text_get_keywords($text, $query, $limit, $blacklist);
120    $tids = _yahoo_terms_create_vocabulary_items($terms, $vid, $static);    $tids = _yahoo_terms_create_vocabulary_items($terms, $vid, $static);
   $tids_in_node = array_keys($node->taxonomy);  
121    $tids_fetched = array_keys($tids);    $tids_fetched = array_keys($tids);
122    // Union of these arrays    if ($node->taxonomy) {
123    $tids_now = $tids_in_node + $tids_fetched;      $tids_in_node = array_keys($node->taxonomy);
124    taxonomy_node_save($node->nid, $tids_now);      // Union of these arrays
125        $tids_now = $tids_in_node + $tids_fetched;
126      }
127      else {
128        // Union of these arrays
129        $tids_now = $tids_fetched;
130      }
131      taxonomy_node_save($node, $tids_now);
132    
133  }  }
134    
135  /**  /**
# Line 177  function _yahoo_terms_create_vocabulary_ Line 186  function _yahoo_terms_create_vocabulary_
186  function yahoo_terms_admin_settings() {  function yahoo_terms_admin_settings() {
187    $form['yahoo_terms_appid'] = array(    $form['yahoo_terms_appid'] = array(
188      '#title' => t('Yahoo Application ID'),      '#title' => t('Yahoo Application ID'),
189      '#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. Think of it as a User-Agent string. If you have multiple applications, you must use a different ID for each one.'),
                          Think of it as a User-Agent string. If you have multiple applications,  
                          you must use a different ID for each one.'),  
190      '#type' => 'textfield',      '#type' => 'textfield',
191      '#required' => TRUE,      '#required' => TRUE,
192      '#default_value' => variable_get('yahoo_terms_appid', ''),      '#default_value' => variable_get('yahoo_terms_appid', ''),
# Line 227  function yahoo_terms_perm() { Line 234  function yahoo_terms_perm() {
234  /**  /**
235   * Implements hook_link()   * Implements hook_link()
236   */   */
237  function yahoo_terms_link($type, $node = NULL, $teaser = FALSE) {  // function yahoo_terms_link($type, $node = NULL, $teaser = FALSE) {
238    $vid = _yahoo_terms_which_vocab($node->type);  //   $vid = _yahoo_terms_which_vocab($node->type);
239    $count = db_num_rows(db_query("SELECT vid FROM {vocabulary_node_types} WHERE vid = %d AND type = '%s'", $vid, $node->type));  //   // $count = 1;
240    if ($count != 0) {  //   $count = db_result(db_query("SELECT COUNT(vid) FROM {vocabulary_node_types} WHERE vid = %d AND type = '%s')", $vid, $node->type));
241      if ($type == 'node' && $node != NULL) {  //   if ($count != 0) {
242        if (user_access(YAHOO_TERMS_USE) && user_access("administer nodes") && variable_get('yahoo_terms_enable_link', FALSE)) {  //     if ($type == 'node' && $node != NULL) {
243          $links["yahoo_terms_extract"] = array('title' => t('Extract terms'), 'href' => 'yahoo_terms/'. $node->nid);  //       if (user_access(YAHOO_TERMS_USE) && user_access("administer nodes") && variable_get('yahoo_terms_enable_link', FALSE)) {
244        }  //         $links["yahoo_terms_extract"] = array('title' => t('Extract terms'), 'href' => 'yahoo_terms/'. $node->nid);
245      }  //       }
246    }  //     }
247    return $links;  //   }
248  }  //   return $links;
249    // }
250    
251  /**  /**
252   * Implements hook_menu()   * Implements hook_menu()
253   */   */
254  function yahoo_terms_menu($may_cache) {  function yahoo_terms_menu() {
255    
256    $items = array();    $items = array();
257    if ($may_cache) {    $items['yahoo_terms'] = array(
258      $items[] = array('path' => 'yahoo_terms', 'title' => t('Extraction...'),      'title' => t('Extraction...'),
259          'callback' => '_yahoo_terms_process_node', 'access' => user_access(YAHOO_TERMS_USE),      'page callback' => '_yahoo_terms_process_node',
260          'type' => MENU_CALLBACK);      'access callback' => 'user_access',
261      $items[] = array('path' => 'admin/settings/yahoo_terms', 'title' => t('Yahoo Terms'),      'access arguments' => array('YAHOO_TERMS_USE'),
262          'callback' => 'drupal_get_form', 'callback arguments' => array('yahoo_terms_admin_settings'));      'type' => MENU_CALLBACK
263      $items[] = array('path' => 'yahoo_terms/batch', 'title' => t('Yahoo Terms batch processing'),      );
264          'callback' => 'yahoo_terms_batch_page', 'access' => user_access(YAHOO_TERMS_USE) && user_access('administer nodes'));    $items['admin/settings/yahoo_terms'] = array(
265    }      'title' => t('Yahoo Terms'),
266        'page callback' => 'drupal_get_form',
267        'page arguments' => array('yahoo_terms_admin_settings'),
268        'access callback' => 'user_access',
269        'access arguments' => array('YAHOO_TERMS_USE')
270        );
271      $items['yahoo_terms/batch'] = array(
272        'title' => t('Yahoo Terms batch processing'),
273        'page callback' => 'yahoo_terms_batch_page',
274        'access callback' =>  'user_access',
275        'access arguments' => array('YAHOO_TERMS_USE') // TODO: The D5 version had 'administer nodes' as a permission.
276        );
277    return $items;    return $items;
278  }  }
279    
# Line 317  function yahoo_terms_nodeapi(&$node, $op Line 336  function yahoo_terms_nodeapi(&$node, $op
336    }    }
337  }  }
338    
339  function yahoo_terms_form_alter($form_id, &$form) {  function yahoo_terms_form_alter(&$form, &$form_state, $form_id) {
340    if (isset($form['#node_type']) && 'node_type_form' == $form_id) {    if (isset($form['#node_type']) && 'node_type_form' == $form_id) {
341      $node_type = $form['old_type']['#value'];      $node_type = $form['old_type']['#value'];
342      $select_voc = _yahoo_terms_vocab_select($node_type);      $select_voc = _yahoo_terms_vocab_select($node_type);
# Line 352  function yahoo_terms_form_alter($form_id Line 371  function yahoo_terms_form_alter($form_id
371        '#return_value' => TRUE,        '#return_value' => TRUE,
372        '#required' => FALSE,        '#required' => FALSE,
373        '#default_value' => variable_get('yahoo_terms_process_existing_'. $node_type, FALSE),        '#default_value' => variable_get('yahoo_terms_process_existing_'. $node_type, FALSE),
374      );      );
375    }    }
376  }  }
377    
# Line 401  function yahoo_terms_batch() { Line 420  function yahoo_terms_batch() {
420      '#type' => 'submit',      '#type' => 'submit',
421      '#value' => 'Start tagging'      '#value' => 'Start tagging'
422    );    );
423      $form['#submit'] = array('yahoo_terms_batch_submit_handler');
424    return $form;    return $form;
425  }  }
426    
427  /**  /**
428   * Batch tagging a specific content type   * Batch tagging a specific content type
429   */   */
430  function yahoo_terms_batch_submit($form_id, $form_values) {  function yahoo_terms_batch_submit_handler($form, &$form_state) {
431    timer_start("yahoo_terms_batch");    timer_start("yahoo_terms_batch");
432    $num_of_processed = 0;    $num_of_processed = 0;
433    $limit_number = $form_values['limit_number'];    $limit_number = $form_state['values']['limit_number'];
434      // dprint_r($form_state);
435    if ($limit_number == 0) {    if ($limit_number == 0) {
436      $result = db_query("SELECT nid FROM {node} WHERE type = '%s'",      $result = db_query("SELECT nid FROM {node} WHERE type = '%s'",
437                         $form_values['type']);      $form_state['values']['type']);
438    }    }
439    else {    else {
440      if (!$form_values['rewrite']) {      if (!$form_state['values']['rewrite']) {
441        $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",        $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",
442                            $form_values['type'], $form_values['limit_number']        $form_state['values']['type'], $form_state['values']['limit_number']
443        );        );
444      }      }
445      else {      else {
446        $result = db_query("SELECT nid FROM {node}        $result = db_query("SELECT nid FROM {node}
447                            WHERE type = '%s' LIMIT %d",                            WHERE type = '%s' LIMIT %d",
448                            $form_values['type'], $form_values['limit_number']                            $form_state['values']['type'], $form_state['values']['limit_number']
449        );        );
450      }      }
451    }    }
452    while ($node = db_fetch_array($result)) {    while ($node = db_fetch_array($result)) {
453      $node = node_load($node['nid']);      $node = node_load($node['nid']);
454      if ( (!isset($node->taxonomy) || count($node->taxonomy) == 0) || $form_values['rewrite']) {      if ((!isset($node->taxonomy) || count($node->taxonomy) == 0) || $form_state['values']['rewrite']) {
455        yahoo_terms_node_assign_keywords($node, "", array(), $form_values['vid']);        yahoo_terms_node_assign_keywords($node, "", array(), $form_state['values']['vid']);
456        $num_of_processed++;        $num_of_processed++;
457      }      }
458    }    }
# Line 460  function _yahoo_terms_vocab_select($type Line 481  function _yahoo_terms_vocab_select($type
481      $select_voc[$voc['vid']] = $voc['name'];      $select_voc[$voc['vid']] = $voc['name'];
482    }    }
483    if (count($select_voc) < 1) {    if (count($select_voc) < 1) {
484      $select_voc[0] = 'No vocabularies found';      $select_voc[0] = 'No vocabularies found';
485    }    }
486    return $select_voc;    return $select_voc;
487  }  }
# Line 471  function _yahoo_terms_vocab_select($type Line 492  function _yahoo_terms_vocab_select($type
492   */   */
493  function yahoo_terms_cron() {  function yahoo_terms_cron() {
494    $types = node_get_types();    $types = node_get_types();
495    foreach($types as $type => $type_object) {    foreach ($types as $type => $type_object) {
496      if (variable_get('yahoo_terms_process_existing_'. $type, FALSE) == TRUE) {      if (variable_get('yahoo_terms_process_existing_'. $type, FALSE) == TRUE) {
497        variable_set('yahoo_terms_process_existing_'. $type, FALSE);        variable_set('yahoo_terms_process_existing_'. $type, FALSE);
498        $result = db_query("SELECT nid FROM {node} WHERE type = '%s'", $type);        $result = db_query("SELECT nid FROM {node} WHERE type = '%s'", $type);

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

  ViewVC Help
Powered by ViewVC 1.1.2