You are here

alchemy.module in Alchemy 7

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

$Id: alchemy.module,v 1.1.2.3 2010/11/17 21:17:48 tomdude48 Exp $

File

alchemy.module
View source
<?php

/**
 * @file
 * $Id: alchemy.module,v 1.1.2.3 2010/11/17 21:17:48 tomdude48 Exp $
 */

// Set to true for debug mode, false for production.
define('ALCHEMY_DEBUG', FALSE);

/**
 * Implements hook_menu().
 */
function alchemy_menu() {
  $items = array();
  $items['alchemy/util'] = array(
    'title' => 'Alchemy Util',
    'page callback' => 'alchemy_util',
    'access callback' => 'user_access',
    'access arguments' => array(
      'access alchemy',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  $items['alchemy'] = array(
    'title' => 'Alchemy Keywords',
    'page callback' => 'alchemy_get_elements_from_node',
    'access callback' => 'user_access',
    'access arguments' => array(
      'access alchemy',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/config/services/alchemy'] = array(
    'title' => '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/config/services/alchemy/general'] = $items['admin/config/services/alchemy'];
    $items['admin/config/services/alchemy/general']['title'] = 'General';
    $items['admin/config/services/alchemy/general']['type'] = MENU_LOCAL_TASK;
    $items['admin/config/services/alchemy/general']['weight'] = -10;
  }
  return $items;
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function alchemy_util($node = NULL, $type = NULL) {

  // $ret = libraries_get_path('AlchemyAPI');
  return 'OK';
}

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

  // Check if node argument is a node or a nid.
  if (!is_object($node) && is_numeric($node)) {
    $node = node_load($node);
  }
  if (!$node) {
    return array();
  }

  // $r = render($node->body);
  $text = strip_tags($node->title . " " . $node->body['und'][0]['value']);
  $elements = alchemy_get_elements($text, $type, $output = 'normalized', $node->nid, TRUE);
  return $elements;
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function alchemy_get_elements($text, $type = 'keywords', $output = 'normalized', $cid = 0, $use_cache = FALSE) {

  // Clean tags.
  $text = strip_tags($text);
  $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
  $text = str_ireplace('&nbsp;', ' ', $text);
  if ($use_cache && $cid) {
    $resultstr = alchemy_cache_get($cid, $type);
  }
  if (!isset($resultstr) || empty($resultstr)) {
    if (!($alchemy_obj = alchemy_new_alchemy_obj())) {
      return FALSE;
    }

    // Extract topic keywords from a text string.
    try {
      switch ($type) {
        case 'entities':
          $resultstr = $alchemy_obj
            ->TextGetRankedNamedEntities($text);
          break;
        case 'categories':
          $resultstr = $alchemy_obj
            ->TextGetCategory($text);
          break;
        case 'concepts':
          $resultstr = $alchemy_obj
            ->TextGetRankedConcepts($text);
          break;
        case 'keywords':
          $resultstr = $alchemy_obj
            ->TextGetRankedKeywords($text);
          break;
      }
    } catch (Exception $exc) {
      drupal_set_message($exc
        ->getMessage(), 'warning');
      return;
    }
    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.
      $elms = array();
      break;
    case 'concepts':
      $elms = $result->concepts->concept;
      break;
    case 'keywords':
      $elms = $result->keywords->keyword;
      break;
  }
  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() {

  // Check if another module has not already included the AlchemyAPI class.
  $ver = '';
  if (variable_get('alchemy_usecurl', 0)) {
    $ver = '_CURL';
  }
  if (class_exists('AlchemyAPI')) {
    return TRUE;
  }
  $files[] = './' . drupal_get_path('module', 'alchemy') . '/AlchemyAPI/module/AlchemyAPI' . $ver . '.php';
  if (module_exists('libraries') && ($p = libraries_get_path('AlchemyAPI'))) {
    $files[] = './' . $p . '/module/AlchemyAPI' . $ver . '.php';
  }
  foreach ($files as $f) {
    if (file_exists($f)) {
      $file = $f;
      break;
    }
  }
  if (!$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/developers/sdks/', 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 DRUPAL_ROOT . '/' . $file;
  return TRUE;
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function alchemy_new_alchemy_obj() {
  static $alchemy_obj;
  if ($alchemy_obj) {
    return $alchemy_obj;
  }

  // Load the AlchemyAPI module code.
  if (!alchemy_include_alchemy_class()) {
    return FALSE;
  }

  // Create an AlchemyAPI object.
  $alchemy_obj = new AlchemyAPI();
  $apikey = alchemy_get_apikey();
  if (!$apikey) {
    return FALSE;
  }
  $alchemy_obj
    ->setAPIKey($apikey);
  return $alchemy_obj;
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
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/config/services/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 = check_plain(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;
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function alchemy_cache_get($cid, $command) {
  $query = db_select('alchemy_cache', 'c')
    ->fields('c', array(
    'data',
  ))
    ->condition('c.nid', $cid)
    ->condition('c.command', $command);
  if ($data = $query
    ->execute()
    ->fetchField()) {
    return $data;
  }
  return FALSE;
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function alchemy_cache_set($cid, $command, $data) {
  if (is_numeric($cid)) {
    alchemy_cache_clear($cid, $command);
    $fields = array(
      'nid' => $cid,
      'path' => '',
      'created' => REQUEST_TIME,
      'command' => $command,
      'data' => $data,
    );
    $query = db_insert('alchemy_cache')
      ->fields($fields);
    $query
      ->execute();
  }
  return TRUE;
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function alchemy_cache_clear($cid, $command) {
  if (is_numeric($cid)) {
    $query = db_delete('alchemy_cache')
      ->condition('nid', $cid)
      ->condition('command', $command);
    $query
      ->execute();
  }
  return TRUE;
}

/**
 * Generates type options and default settings.
 * @param $defaults
 */
function alchemy_get_types($defaults = FALSE) {
  if ($defaults) {
    $types = array(
      'concepts',
      'entities',
      'keywords',
    );
  }
  else {
    $types = array(
      'concepts' => t('Concepts'),
      'entities' => t('Entities'),
      'keywords' => t('Keywords'),
    );
  }
  return $types;
}

Functions

Namesort descending Description
alchemy_cache_clear @todo Please document this function.
alchemy_cache_get @todo Please document this function.
alchemy_cache_set @todo Please document this function.
alchemy_get_apikey Returns api key sent in alchemy settings admin.
alchemy_get_elements @todo Please document this function.
alchemy_get_elements_from_node
alchemy_get_message_apimissing @todo Please document this function.
alchemy_get_types Generates type options and default settings.
alchemy_include_alchemy_class Includes the Alchemy API Class, checking first to see if it exists.
alchemy_menu Implements hook_menu().
alchemy_new_alchemy_obj @todo Please document this function.
alchemy_util @todo Please document this function.
alchemy_xml2array Utility function to convert xml to a php array.

Constants

Namesort descending Description
ALCHEMY_DEBUG