You are here

function wordstream_get_keyword_data in WordStream Keyword Tools 6

Same name and namespace in other branches
  1. 7 wordstream.module \wordstream_get_keyword_data()

Generates keyword search volume data from WordStream

Parameters

$keywords:

$msgs:

$params:

3 calls to wordstream_get_keyword_data()
wordstream_keyword_stats_report_page in ./wordstream.module
Displays keyword stats report
wordstream_kwresearch_get_keyword_stats in ./wordstream.module
wordstream_stats_report_form_submit in ./wordstream.module
Stats report form submit handler.

File

./wordstream.module, line 292

Code

function wordstream_get_keyword_data($keywords, &$msgs, $params) {
  $type = $params['type'] ? $params['type'] : NULL;
  $adult_filter = $params['adult_filter'] ? 1 : 0;

  //$limit = ($params['limit'])?$params['limit']:NULL;
  if (is_null($adult_filter)) {
    $adult_filter = variable_get('kwresearch_adult_filter', 'remove_dubious');
  }

  //print "include_plurals=$include_plurals, adult_filter=$adult_filter";
  if (!$type) {
    $type = 'keywords';
    $params['type'] = 'keywords';
  }
  $last_cache = variable_get('wordstream_last_cache', 0);
  $cachecheck = FALSE;
  if (time - $last_cache > variable_get('wordstream_cache_time', 604800)) {
    $cachecheck = TRUE;
  }
  $sql = '
    SELECT *
    FROM {wordstream_cache}
    WHERE
    keyphrase = "%s" AND
    adult_filter = %d AND
    api_call = "%s"
  ';
  $dbresult = db_fetch_array(db_query($sql, $keywords, $adult_filter, $type));
  if ($dbresult) {
    $dbret = unserialize($dbresult['data']);
    if (!$cachecheck) {
      return $dbret;

      //return array_slice($dbret, 0, $limit);
    }
  }
  $wordstream = wordstream_include_api_class();
  if (!$wordstream) {
    return array();
  }
  if ($type == 'keywords') {
    $apiret = $wordstream
      ->getKeywords($keywords);
  }

  //dsm($apiret);
  if (!is_array($apiret)) {
    $errmsg = t('Expected return of type array, but got [@type]', array(
      '@type' => gettype($apiret),
    ));
    $msgs[] = kwresearch_format_message($errmsg, 'error');
    return FALSE;
  }

  // if keywords longer than 255, don't cache
  if (strlen($keywords) > 255) {
    return $apiret;
  }

  //dsm($apiret);
  if (!$dbresult) {
    $sql = '
      INSERT INTO {wordstream_cache}
      (keyphrase, adult_filter, api_call, updated, data)
      VALUES ("%s", %d, "%s", %d,"%s")
    ';

    //dsm(sprintf($sql, $keywords, $adult_filter, $type, time(), serialize($apiret)));
    $query = db_query($sql, $keywords, $adult_filter, $type, time(), serialize($apiret));
  }
  elseif (!$dbret) {
    $sql = '
      UPDATE {wordstream_cache}
      SET
        time = %d,
        data = "%s"
      WHERE
        keyphrase = "%s" AND
        adult_filter = %d AND
        api_call = "%s"
    ';

    //dsm(sprintf($sql, time(), serialize($apiret), $keywords, $adult_filter, $type));
    $query = db_query($sql, time(), serialize($apiret), $keywords, $adult_filter, $type);
  }
  elseif (array_slice($dbret, 0, 10) != array_slice($apiret, 0, 10)) {

    // empty cache, it is out of date
    $sql = '
      DELETE FROM {wordstream_data}
    ';

    //dsm(sprintf($sql));
    $query = db_query($sql);
    drupal_set_message(t('Keyword cache reset.'));
    variable_set('wordstream_last_cache', time());

    // update new data set search ratios
    scribeseo_update_kwresearch_searches_ratio();
  }
  return $apiret;
}