? 906824-1_per_term.patch
? extractor.install
Index: extractor.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/extractor/extractor.module,v
retrieving revision 1.4
diff -u -p -r1.4 extractor.module
--- extractor.module	8 Sep 2010 03:05:36 -0000	1.4
+++ extractor.module	9 Sep 2010 18:19:10 -0000
@@ -40,44 +40,106 @@ function extractor_feeds_plugins() {
 }
 
 /**
- * Implementation of hook_menu().
+ * Implementation of hook_form_taxonomy_form_vocabulary_alter().
  */
-function extractor_menu() {
-  $items = array();
-  $items['admin/settings/extractor'] = array(
-    'title' => 'Extractor',
-    'description' => 'Set vocabulary for simple term extractor API.',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('extractor_settings_form'),
-    'access callback' => 'user_access',
-    'access arguments' => array('administer site configuration'),
+function extractor_form_taxonomy_form_vocabulary_alter(&$form, &$form_state) {
+  $form['extractor'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Term extraction'),
+    '#collapsible' => TRUE,
   );
-  return $items;
+  $lookup = FALSE;
+  $vocabularies = variable_get('extractor_simple_vocabularies', array());
+  if ($vocabularies[$form['vid']['#value']] || $vocabularies[$form['module']['#value']]) {
+    $lookup = TRUE;
+  }
+  $form['extractor']['extractor_lookup'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Use all terms of this vocabulary for extraction'),
+    '#description' => t('If checked new posts containing terms of this vocabulary will be automatically tagged with them. You can alternatively pick single terms for extraction on term forms.'),
+    '#default_value' => $lookup,
+  );
+  $form['#submit'][] = 'extractor_form_vocabulary_submit';
 }
 
 /**
- * Menu callback for administration form.
+ * Submit handler for extractor_form_taxonomy_form_vocabulary_alter().
  */
-function extractor_settings_form() {
-  $form = array();
+function extractor_form_vocabulary_submit($form, &$form_state) {
+  $vocabularies = variable_get('extractor_simple_vocabularies', array());
+  unset($vocabularies[$form_state['values']['module']], $vocabularies[$form_state['values']['vid']]);
 
-  $options = array(0 => t('Select a vocabulary'));
-  foreach (taxonomy_get_vocabularies() as $vid => $vocab) {
-    if (strpos($vocab->module, 'features_') === 0) {
-      $options[$vocab->module] = $vocab->name;
-    }
-    else {
-      $options[$vid] = $vocab->name;
-    }
+  if (!empty($form_state['values']['extractor_lookup'])) {
+    $id = strpos($form_state['values']['module'], 'features_') === 0 ? $form_state['values']['module'] : $form_state['values']['vid'];
+    $vocabularies[$id] = TRUE;
   }
-  $form['extractor_simple_vid'] = array(
-    '#type' => 'select',
-    '#title' => t('Vocabulary'),
-    '#description' => t('Select vocabulary for simple term extraction engine. Terms in this vocabulary will be looked up in text to be extracted.'),
-    '#options' => $options,
-    '#default_value' => variable_get('extractor_simple_vid', 0),
+  variable_set('extractor_simple_vocabularies', $vocabularies);
+}
+
+/**
+ * Implementation of specific hook_form_alter().
+ */
+function extractor_form_taxonomy_form_term_alter(&$form, &$form_state) {
+  $form['submit']['#weight'] = isset($form['submit']['#weight']) ? $form['submit']['#weight'] + 1 : 1;
+  $form['delete']['#weight'] = isset($form['delete']['#weight']) ? $form['submit']['#weight'] + 1 : 1;
+  $form['extractor'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Term extraction'),
+    '#collapsible' => TRUE,
+  );
+
+  if (extractor_use_vocabulary($form['vid']['#value'])) {
+    $description = t('All terms of this vocabulary are used for term extraction. You can change this setting on the vocabulary form.');
+    $default = $disabled = TRUE;
+  }
+  else {
+    $description = t('If checked new posts containing this term will be automatically tagged with it.');
+    $default = isset($form['tid']['#value']) ? extractor_use_term($form['tid']['#value']) : 0;
+    $disabled = FALSE;
+  }
+  $form['extractor']['extractor_lookup'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Use for term extraction'),
+    '#description' => $description,
+    '#default_value' => $default,
+    '#disabled' => $disabled,
   );
-  return system_settings_form($form);
+}
+
+/**
+ * Implementation of hook_taxonomy().
+ */
+function extractor_taxonomy($op = NULL, $type = NULL, $term = NULL){
+  if ($type =='term' && $term) {
+    switch ($op) {
+      case 'delete':
+        db_query("DELETE FROM {extractor_lookup} WHERE tid = %d", $term['tid']);
+        break;
+      case 'update':
+        db_query("DELETE FROM {extractor_lookup} WHERE tid = %d", $term['tid']);
+      case 'insert':
+        if (!empty($term['extractor_lookup'])) {
+          drupal_write_record('extractor_lookup', $term);
+        }
+        break;
+    }
+  }
+}
+
+/**
+ * Determine whether a term should be used for term extraction or not.
+ */
+function extractor_use_term($tid) {
+  return db_result(db_query("SELECT tid FROM {extractor_lookup} WHERE tid = %d", $tid));
+}
+
+/**
+ * Determine whether a term should be used for term extraction or not.
+ */
+function extractor_use_vocabulary($vid) {
+  $vocabulary = taxonomy_vocabulary_load($vid);
+  $vocabularies = variable_get('extractor_simple_vocabularies', $vocabularies);
+  return (isset($vocabularies[$vocabulary->vid]) || isset($vocabularies[$vocabulary->module]));
 }
 
 /**
Index: libraries/extractor_simple.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/extractor/libraries/extractor_simple.inc,v
retrieving revision 1.2
diff -u -p -r1.2 extractor_simple.inc
--- libraries/extractor_simple.inc	8 Sep 2010 03:05:36 -0000	1.2
+++ libraries/extractor_simple.inc	9 Sep 2010 18:19:10 -0000
@@ -87,17 +87,21 @@ function _extractor_simple_lookup($word)
     return array();
   }
 
+  $vids = array();
+  foreach ($vocabulary as taxonomy_get_vocabularies()) {
+    if (extractor_use_vocabulary($vocabulary->vid)) {
+      $vids[$vocabulary->vid] = $vocabulary->vid;
+    }
+  }
   if (!$loaded) {
-    if ($vid = variable_get('extractor_simple_vid', FALSE)) {
-      if (strpos($vid, 'features_') === 0) {
-        $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module = '%s'", $vid));
-      }
+    if (empty($vid)) {
+      $result = db_query_range("SELECT td.tid, td.name FROM {term_data} td JOIN {extractor_lookup} e ON td.tid = e.tid", 0, 2000);
     }
-    if (is_numeric($vid)) {
-      $result = db_query_range('SELECT tid, name FROM {term_data} WHERE vid = %d', $vid, 0, 2000);
-      while ($term = db_fetch_object($result)) {
-        $term_cache[strtolower($term->name[0])][$term->name] = $term;
-      }
+    else {
+      $result = db_query_range("SELECT td.tid, td.name FROM {term_data} td LEFT JOIN {extractor_lookup} e ON td.tid = e.tid WHERE td.vid IN (%s) OR e.tid IS NOT NULL", implode(', ', $vids), 0, 2000);
+    }
+    while ($term = db_fetch_object($result)) {
+      $term_cache[strtolower($term->name[0])][$term->name] = $term;
     }
     $loaded = TRUE;
   }
