View source
<?php
DEFINE('WORDSTREAM_LINK_API_ACCOUNT', 'http://www.wordstream.com/keyword-tool-api-drupal');
DEFINE('WORDSTREAM_ADULT_FILTER_DEFAULT', TRUE);
DEFINE('WORDSTREAM_CACHE_TIME', 604800);
function wordstream_menu() {
$items = array();
$items['admin/config/search/wordstream'] = array(
'title' => 'WordStream',
'description' => 'Configure WordStream API and options.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'wordstream_admin_settings',
),
'access callback' => 'user_access',
'access arguments' => array(
'admin wordstream',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'wordstream.admin.inc',
);
$items['admin/structure/wordstream'] = array(
'title' => 'WordStream',
'description' => 'Research keywords using WordStream API.',
'page callback' => 'wordstream_keyword_stats_report_page',
'page arguments' => array(
'keywords',
),
'access callback' => 'user_access',
'access arguments' => array(
'access wordstream reports',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'wordstream.admin.inc',
);
$items['admin/structure/wordstream/keywords'] = array(
'title' => 'Keywords tool',
'page callback' => 'wordstream_keyword_stats_report_page',
'page arguments' => array(
'keywords',
),
'access callback' => 'user_access',
'access arguments' => array(
'access wordstream reports',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'wordstream.admin.inc',
);
$items['wordstream/util'] = array(
'title' => 'WordStream util',
'page callback' => 'wordstream_util',
'access callback' => TRUE,
'access arguments' => array(
'access wordstream reports',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function wordstream_permission() {
return array(
'admin wordstream' => array(
'title' => t('admin wordstream'),
'description' => t('TODO Add a description for \'admin wordstream\''),
),
'access wordstream reports' => array(
'title' => t('access wordstream reports'),
'description' => t('TODO Add a description for \'access wordstream reports\''),
),
);
}
function wordstream_util() {
$r = variable_get('wordstream_adult_filter', WORDSTREAM_ADULT_FILTER_DEFAULT);
return "x=" . $r;
}
function wordstream_include_api_class($username = NULL, $password = NULL) {
include_once drupal_get_path('module', 'wordstream') . '/class.wordstream.inc';
$un = $username ? $username : variable_get('wordstream_username', '');
$pw = $password ? $password : variable_get('wordstream_password', '');
if ($un && $pw) {
$wordstream = new WordStream($un, $pw);
if (!$_SESSION['wordstream']['sid']) {
$ret = $wordstream
->login();
list($un, $sid) = explode(':', $ret);
$_SESSION['wordstream']['sid'] = $sid;
}
}
else {
return FALSE;
}
return $wordstream;
}
function wordstream_keyword_stats_report_page($type = 'keywords', $keyword = NULL) {
if (module_exists('kwresearch')) {
drupal_add_css(drupal_get_path('module', 'kwresearch') . '/kwresearch.css');
drupal_add_js(drupal_get_path('module', 'kwresearch') . '/kwresearch.js');
}
if (isset($_SESSION['wordstream'])) {
$analysis = $_SESSION['wordstream']['analysis'];
$_SESSION['wordstream']['analysis'] = '';
}
$stats = TRUE;
if (!$analysis && $keyword) {
$analysis = array();
$analysis['inputs']['keyword'] = $keyword;
$analysis['analysis'] = wordstream_get_keyword_data($keyword, $msgs);
if (!empty($msgs)) {
$analysis['messages'] = $msgs;
}
}
if ($analysis) {
$analysis['inputs']['operations'] = TRUE;
}
$output = render(drupal_get_form('wordstream_stats_report_form', $analysis));
if ($analysis) {
$output .= '<div id="wordstream-report" style="clear: both;">';
$output .= '<h3>' . t('Report') . '</h3>';
$output .= theme_wordstream_stats_report($analysis);
$output .= '</div>';
if (is_array($analysis['analysis'])) {
foreach ($analysis['analysis'] as $kw => $v) {
if (isset($v['term'])) {
$site_kw[$v['term']] = array(
'kid' => (int) $v['kid'],
'priority' => (int) $v['priority'],
'value' => (int) $v['value'],
'page_count' => (int) $v['page_count'],
);
}
}
}
}
if (module_exists('kwresearch')) {
$site_priority_options = kwresearch_get_priority_options();
drupal_add_js(array(
'kwresearch' => array(
'form' => 'admin_keyword_stats',
'analyze_path' => base_path() . 'admin/content/kwresearch/keyword_report/',
'keyword_edit_path' => base_path() . 'admin/content/kwresearch/keyword_list/edit/',
'return_destination' => 'admin/content/kwresearch/keyword_report/' . $keyword,
'toggle_site_keyword_callback' => base_path() . 'kwresearch/toggle_site_keyword_js',
'module_path' => base_path() . drupal_get_path('module', 'kwresearch'),
'enable_site_keyword_tool' => user_access('kwresearch admin site keywords'),
'site_keywords_data' => $site_kw,
'site_priority_options' => $site_priority_options,
),
), array(
'type' => 'setting',
'scope' => JS_DEFAULT,
));
}
return $output;
}
function wordstream_stats_report_form($form, $form_state, $analysis, $ajax = FALSE) {
$form['pre_inputs'] = array(
'#type' => 'markup',
'#value' => '<div id="wordstream-form-inputs">',
);
$form['wordstream_keyword'] = array(
'#type' => 'textfield',
'#title' => t('Keyword'),
'#default_value' => $analysis && $analysis['inputs']['wordstream_keyword'] ? $analysis['inputs']['wordstream_keyword'] : '',
);
$form['wordstream_stats_report_options'] = array(
'#type' => 'fieldset',
'#title' => t('Filters'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['wordstream_stats_report_options'] = array_merge($form['wordstream_stats_report_options'], wordstream_stats_report_form_options($analysis));
$form['post_inputs'] = array(
'#type' => 'markup',
'#value' => '</div>',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
function wordstream_stats_report_form_options($analysis = NULL) {
$defvals = isset($analysis['inputs']) ? $analysis['inputs'] : array();
$form['wordstream_adult_filter'] = array(
'#type' => 'checkbox',
'#title' => t('Adult keyword filter default'),
'#return_value' => 'on',
'#default_value' => isset($defvals['wordstream_adult_filter']) ? $defvals['wordstream_adult_filter'] : variable_get('wordstream_adult_filter', WORDSTREAM_ADULT_FILTER_DEFAULT),
);
return $form;
}
function wordstream_stats_report_form_submit($form, &$form_state) {
$analysis['inputs'] = $form_state['values'];
$params = array(
'type' => 'keywords',
'adult_filter' => $analysis['inputs']['wordstream_adult_filter'],
);
$analysis['analysis'] = wordstream_get_keyword_data($form_state['values']['wordstream_keyword'], $msgs, $params);
if (!empty($msgs)) {
$analysis['messages'] = $msgs;
}
if ($analysis['analysis']) {
$_SESSION['wordstream']['analysis'] = $analysis;
}
return;
}
function wordstream_get_keyword_data($keywords, &$msgs, $params) {
$type = $params['type'] ? $params['type'] : NULL;
$adult_filter = $params['adult_filter'] ? 1 : 0;
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;
}
}
$wordstream = wordstream_include_api_class();
if (!$wordstream) {
return FALSE;
}
if ($type == 'keywords') {
$apiret = $wordstream
->getKeywords($keywords);
}
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 (strlen($keywords) > 255) {
return $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) {
$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)) {
db_delete('wordstream_cache')
->execute();
drupal_set_message(t('Keyword cache reset.'));
variable_set('wordstream_last_cache', time());
}
return $apiret;
}
function theme_wordstream_stats_report($analysis) {
$hdrs = array(
t('Term'),
t('Frequency'),
);
$rows = array();
foreach ($analysis['analysis'] as $v) {
$row = array(
$v[0],
number_format($v[1]),
);
$rows[] = array(
'data' => $row,
'id' => 'kid-' . (isset($v['kid']) ? $v['kid'] : 'n'),
);
}
$attr = array(
'id' => 'kwresearch-result-table-' . check_plain(str_replace(' ', '-', isset($analysis['inputs']['keyword']) ? $analysis['inputs']['keyword'] : '')),
'class' => array(
'kwresearch-result-table',
),
);
$output = '<div id="kwresearch-result-block-' . check_plain(str_replace(' ', '-', isset($analysis['inputs']['keyword']) ? $analysis['inputs']['keyword'] : '')) . '" class="kwresearch-result-block">';
$vars = array(
'header' => $hdrs,
'rows' => $rows,
'attributes' => $attr,
);
$output .= theme('table', $vars);
return $output;
}
function wordstream_kwresearch_sources() {
$sources['wordstream'] = array(
'title' => t('WordStream'),
'module' => 'wordstream',
'form_options' => 'wordstream_stats_report_form_options',
'stats_callback' => 'wordstream_kwresearch_get_keyword_stats',
'searches_ratio' => variable_get('wordstream_kwresearch_searches_ratio', 256),
'stats_report_columns' => array(
'wordstream_searches' => t('WS count'),
),
'stats_report_values_callback' => 'wordstream_kwresearch_format_stats_values',
);
return $sources;
}
function wordstream_update_kwresearch_searches_ratio() {
if (time() > variable_get('wordstream_kwresearch_searches_ratio_updated', 0) + WORDSTREAM_CACHE_TIME) {
$apiret = xmlrpc('http://www.leveltendesign.com/xmlrpc.php', 'l10seoapi.searches_ratio', 'wordstream');
if (is_numeric($apiret) && $apiret > 0) {
variable_set('wordstream_kwresearch_searches_ratio', (double) $apiret);
}
variable_set('wordstream_kwresearch_searches_ratio_updated', time());
}
}
function wordstream_kwresearch_get_keyword_stats($keywords, $params = NULL) {
$data = wordstream_get_keyword_data($keywords, $msgs, $params);
$ret = array();
if (is_array($data)) {
foreach ($data as $v) {
$ret[$v[0]] = array(
'searches' => $v[1],
);
}
}
return $ret;
}
function wordstream_kwresearch_format_stats_values($values, $keyword = '', $type = 'term') {
switch ($type) {
case 'wordstream_searches':
if ($values['wordstream_searches'] > 0) {
$output = $values['wordstream_searches'] == 'NA' ? 'NA' : number_format($values['wordstream_searches']);
}
else {
$output = $values['wordstream_searches'];
}
break;
default:
return FALSE;
}
return $output;
}