You are here

alchemy.module in Alchemy 6

Same filename and directory in other branches
  1. 7 alchemy.module

DO NOT COMMIT TO DRUPAL CONTRIB! THIS MODULE IS FOR DEVELOPMENT ONLY.

File

alchemy.module
View source
<?php

/**
 * @file
 *  DO NOT COMMIT TO DRUPAL CONTRIB! THIS MODULE IS FOR DEVELOPMENT ONLY. 
 * 
 */
define('ALCHEMY_DEBUG', FALSE);

// set to true for debug mode, false for production

/**
 * Implementation of hook_menu().
 */
function alchemy_menu() {
  $items = array();
  $items['alchemy/util'] = array(
    'title' => t('Alchemy Util'),
    'page callback' => 'alchemy_util',
    'access callback' => 'user_access',
    'access arguments' => array(
      'access alchemy',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  $items['alchemy'] = array(
    'title' => t('Alchemy Keywords'),
    'page callback' => 'alchemy_get_elements_from_node',
    'access callback' => 'user_access',
    'access arguments' => array(
      'access alchemy',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/settings/alchemy'] = array(
    'title' => t('Alchemy'),
    'description' => 'Settings for Alchemy.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'alchemy_admin_settings',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'alchemy.admin.inc',
  );
  if (function_exists('alchemy_contentanalysis_menu') || function_exists('alchemy_tagging_suggest_menu')) {
    $items['admin/settings/alchemy/general'] = $items['admin/settings/alchemy'];
    $items['admin/settings/alchemy/general']['title'] = t('General');
    $items['admin/settings/alchemy/general']['type'] = MENU_LOCAL_TASK;
    $items['admin/settings/alchemy/general']['weight'] = -10;
  }
  return $items;
}
function alchemy_util($node = NULL, $type = NULL) {
  if (!$node) {
    $node = 1;
  }
  if (!$type) {
    $type = 'keywords';
  }
  $r = alchemy_get_elements_from_node($node, $type);
  dsm($r);
  print_r($r);
  return 'OK';
}

/**
 * 
 * @param node object or nid $node
 */
function alchemy_get_elements_from_node($node, $type = 'keywords', $output = 'normalized', $use_cache = FALSE) {

  // chech if node argument is a node or a nid
  if (!is_object($node) && is_numeric($node)) {
    $node = node_load($node);
  }
  if (!$node) {
    return array();
  }
  $text = strip_tags($node->title . " " . $node->body);
  $elements = alchemy_get_elements($text, $type, $output = 'normalized', $node->nid, TRUE);
  return $elements;
}
function alchemy_get_elements($text, $type = 'keywords', $output = 'normalized', $cid = 0, $use_cache = FALSE) {

  //dsm("alchemy_get_elements($text, $type, $output, $cid, $use_cache)");
  if ($use_cache && $cid) {
    $resultstr = alchemy_cache_get($cid, $type);

    //watchdog('alchemy', 'fectch from cache: ' . $resultstr);
  }
  if (!$resultstr) {
    $alchemyObj = alchemy_new_alchemy_obj();

    // Extract topic keywords from a text string.
    switch ($type) {
      case 'entities':
        $resultstr = $alchemyObj
          ->TextGetRankedNamedEntities($text);
        break;
      case 'categories':
        $resultstr = $alchemyObj
          ->TextGetCategory($text);
        break;
      case 'concepts':
        $resultstr = $alchemyObj
          ->TextGetRankedConcepts($text);
        break;
      case 'keywords':
        $resultstr = $alchemyObj
          ->TextGetRankedKeywords($text);
        break;
    }
    if ($cid) {
      alchemy_cache_set($cid, $type, $resultstr);
    }
  }
  if ($output == 'xml') {
    return $resultstr;
  }
  $result = alchemy_xml2array($resultstr);
  if ($output == 'array') {
    return $result;
  }
  $elements = array();
  switch ($type) {
    case 'entities':
      $elms = $result->entities->entity;
      break;
    case 'categories':

      // TODO

      //print_r($result);
      $elms = array();
      break;
    case 'concepts':
      $elms = $result->concepts->concept;
      break;
    case 'keywords':
      $elms = $result->keywords->keyword;
      break;
  }

  //print_r($elms);
  if (is_object($elms)) {
    foreach ($elms as $v) {
      $hash = (string) $v->text;
      $elements[$hash] = array(
        'term' => (string) $v->text,
        'relevance' => (double) $v->relevance,
      );
      if ($v->type) {
        $elements[$hash]['type'] = (string) $v->type;
      }
      if ($v->count) {
        $elements[$hash]['count'] = (string) $v->count;
      }
      if ($v->disambiguated) {
        $elements[$hash]['disambiguated'] = $v->disambiguated;
      }
    }
  }
  return $elements;
}

/**
 * Includes the Alchemy API Class, checking first to see if it exists.
 */
function alchemy_include_alchemy_class() {
  $file = './' . drupal_get_path('module', 'alchemy') . '/AlchemyAPI/module/AlchemyAPI.php';
  if (!file_exists($file)) {
    $msg = t('The Alchemy module requires the Alchemy SDK. Use the PHP version of the SDK');
    $msg .= l(t('Download the SDK here.'), 'http://www.alchemyapi.com/tools/', array(
      'attributes' => array(
        'target' => 'alchemy',
      ),
    ));
    $msg .= "<br><br>";
    $msg .= t(' Download the files and place them in a folder named "AlchemyAPI" under the alchemy module directory.');
    if ($analysis) {
      $analysis['messages'][] = contentanalysis_format_message($msg, 'error');
    }
    else {
      drupal_set_message($msg, 'error');
    }
    return FALSE;
  }
  require_once $file;
  return TRUE;
}
function alchemy_new_alchemy_obj() {
  static $alchemyObj;
  if ($alchemyObj) {
    return $alchemyObj;
  }

  // Load the AlchemyAPI module code.
  alchemy_include_alchemy_class();

  // Create an AlchemyAPI object.
  $alchemyObj = new AlchemyAPI();
  $apikey = alchemy_get_apikey();
  if (!$apikey) {
    return FALSE;
  }
  $alchemyObj
    ->setAPIKey($apikey);
  return $alchemyObj;
}
function alchemy_get_message_apimissing() {
  $msg = t('You must set the Alchemy API key in order to use Alchemy. !settings_link.', array(
    '!settings_link' => l(t('Set key here'), 'admin/settings/alchemy', array(
      'attributes' => array(
        'target',
        'settings',
      ),
    )),
  ));
  return $msg;
}

/**
 * Returns api key sent in alchemy settings admin
 */
function alchemy_get_apikey() {
  $apikey = variable_get('alchemy_apikey', '');
  if (!$apikey) {
    $msg = alchemy_get_message_apimissing();
    drupal_set_message($msg);
    return false;
  }
  return $apikey;
}

/**
 * Utility function to convert xml to a php array
 * @param xml string $xmlstr
 */
function alchemy_xml2array($xmlstr) {
  $xmlarr = new SimpleXMLElement($xmlstr);
  return $xmlarr;
}
function alchemy_cache_get($cid, $command) {
  $sql = '
    SELECT *
    FROM {alchemy_cache}
    WHERE nid = %d AND command = "%s"
  ';
  if (!($row = db_fetch_object(db_query($sql, $cid, $command)))) {
    return FALSE;
  }
  return $row->data;
}
function alchemy_cache_set($cid, $command, $data) {
  if (is_numeric($cid)) {
    $sql = '
      DELETE FROM {alchemy_cache}
      WHERE nid = %d AND command = "%s"
    ';
    db_query($sql, $cid, $command);
    $sql = '
      INSERT INTO {alchemy_cache}
      (nid, path, created, command, data)
      VALUES (%d, "%s", %d, "%s","%s")
    ';
    db_query($sql, $cid, "", time(), $command, $data);
  }
  return TRUE;
}

Functions

Namesort descending Description
alchemy_cache_get
alchemy_cache_set
alchemy_get_apikey Returns api key sent in alchemy settings admin
alchemy_get_elements
alchemy_get_elements_from_node
alchemy_get_message_apimissing
alchemy_include_alchemy_class Includes the Alchemy API Class, checking first to see if it exists.
alchemy_menu Implementation of hook_menu().
alchemy_new_alchemy_obj
alchemy_util
alchemy_xml2array Utility function to convert xml to a php array

Constants

Namesort descending Description
ALCHEMY_DEBUG @file DO NOT COMMIT TO DRUPAL CONTRIB! THIS MODULE IS FOR DEVELOPMENT ONLY.