function wordstream_get_keyword_data in WordStream Keyword Tools 7
Same name and namespace in other branches
- 6 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 - @todo Please document this function.
- wordstream_stats_report_form_submit in ./
wordstream.module - Stats report form submit handler.
File
- ./
wordstream.module, line 283
Code
function wordstream_get_keyword_data($keywords, &$msgs, $params) {
//dsm($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('adult_filter', WORDSTREAM_ADULT_FILTER_DEFAULT);
}
if (!$type) {
$type = 'keywords';
$params['type'] = 'keywords';
}
$query = db_select('wordstream_cache', 'c')
->fields('c')
->condition('keyphrase', $keywords)
->condition('adult_filter', $adult_filter)
->condition('api_call', $type);
$dbresult = $query
->execute()
->fetchObject();
if ($dbresult) {
$dbret = unserialize($dbresult->data);
if (time() - $dbresult->updated < variable_get('wordstream_cache_time', WORDSTREAM_CACHE_TIME)) {
return $dbret;
//return array_slice($dbret, 0, $limit);
}
}
$wordstream = wordstream_include_api_class();
if (!$wordstream) {
return FALSE;
}
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) {
$query = db_insert('wordstream_cache')
->fields(array(
'keyphrase' => $keywords,
'adult_filter' => $adult_filter,
'api_call' => $type,
'updated' => time(),
'data' => serialize($apiret),
));
$id = $query
->execute();
}
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, REQUEST_TIME, serialize($apiret), $keywords, $adult_filter, $type);
*/
$query = db_update('wordstream_cache')
->condition('keyphrase', $keywords)
->condition('adult_filter', $adult_filter)
->condition('api_call', $type)
->fields(array(
'updated' => time(),
'data' => serialize($apiret),
));
$query
->execute();
}
elseif (array_slice($dbret, 0, 10) != array_slice($apiret, 0, 10)) {
// empty cache, it is out of date
/*
$sql = '
DELETE FROM {wordstream_cache}
';
//dsm(sprintf($sql));
$query = db_query($sql);
*/
db_delete('wordstream_cache')
->execute();
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;
}