View source
<?php
require_once './' . drupal_get_path('module', 'contentanalysis') . "/includes/seo_friend.inc";
function contentanalysis_menu() {
$items = array();
$items['admin/settings/contentanalysis'] = array(
'title' => 'Content analysis',
'description' => 'Analyze and optimize node content.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'contentanalysis_admin_settings',
),
'access callback' => 'user_access',
'access arguments' => array(
'admin content analysis',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'contentanalysis.admin.inc',
);
$items['admin/content/contentanalysis'] = array(
'title' => 'Content analysis',
'page callback' => 'contentanalysis_page_analyzer',
'access callback' => 'user_access',
'access arguments' => array(
'perform content analysis',
),
'type' => MENU_LOCAL_TASK,
);
$items['contentanalysis/analyze_js'] = array(
'title' => '',
'page callback' => 'contentanalysis_analyze_js',
'access arguments' => array(
'perform content analysis',
),
'type' => MENU_CALLBACK,
);
$items['contentanalysis/ajax/analysis'] = array(
'title' => '',
'page callback' => 'contentanalysis_modal_popup',
'access arguments' => array(
'perform content analysis',
),
'type' => MENU_CALLBACK,
);
$items['contentanalysis/ajax/analysis_only'] = array(
'title' => '',
'page callback' => 'contentanalysis_modal_popup_only',
'access arguments' => array(
'perform content analysis',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function contentanalysis_perm() {
return array(
'perform content analysis',
'admin content analysis',
);
}
function contentanalysis_modal_popup() {
ctools_include('ajax');
ctools_include('modal');
$title = t('Content Analysis Results');
$html = contentanalysis_analyze();
$output[] = ctools_modal_command_display($title, $html);
if (!$output) {
$output = array();
$output[] = ctools_ajax_command_append('#modal-message', '<div id="message">test text</div>');
$output[] = ctools_modal_command_dismiss();
}
ctools_ajax_render($output);
}
function contentanalysis_modal_popup_only() {
ctools_include('ajax');
ctools_include('modal');
$title = t('Content Analysis Results');
$html = contentanalysis_analyze_popup_only();
$output[] = ctools_modal_command_display($title, $html);
if (!$output) {
$output = array();
$output[] = ctools_ajax_command_append('#modal-message', '<div id="message">test text</div>');
$output[] = ctools_modal_command_dismiss();
}
ctools_ajax_render($output);
}
function contentanalysis_analyze() {
return '<div id="analysis-modal">' . theme_progress_bar(0, t('Analyzing Content')) . '</div>' . $output . '<script>contentanalysis_full_analysis();</script>';
}
function contentanalysis_analyze_popup_only() {
return '<div id="analysis-modal">' . theme_progress_bar(0, t('Analyzing Content')) . '</div>' . $output . '<script>contentanalysis_dialog_analysis();</script>';
}
function contentanalysis_page_analyzer() {
drupal_add_css(drupal_get_path('module', 'contentanalysis') . '/contentanalysis.admin.css');
$output .= drupal_get_form('contentanalysis_page_analyzer_form', $analysis);
return $output;
}
function contentanalysis_page_analyzer_form($form_state, $analysis) {
$form['instructions'] = array(
'#value' => t('You can specify content to be analyzed using one of the three below methods. Use only one method. Either enter text content, use a numeric node id, or provide a URL to an existing webpage.'),
);
$form['input'] = array(
'#type' => 'textarea',
'#title' => t('Content'),
'#default_value' => $analysis ? $analysis['#context']['inputs']['text'] : '',
);
$form['input_nid'] = array(
'#type' => 'textfield',
'#title' => t('Node ID'),
'#default_value' => $analysis ? $analysis['#context']['inputs']['nid'] : '',
);
$form['input_url'] = array(
'#type' => 'textfield',
'#title' => t('URL'),
'#default_value' => $analysis ? $analysis['#context']['inputs']['url'] : '',
);
$form = array_merge($form, contentanalysis_analysis_form($form_state, $analysis, array(), 'admin'));
return $form;
}
function contentanalysis_analysis_form($form_state, $analysis = array(), $node = array(), $mode = 'admin') {
drupal_add_js(drupal_get_path('module', 'contentanalysis') . '/contentanalysis.js');
ctools_include('ajax');
ctools_include('modal');
ctools_modal_add_js();
ctools_add_js('ajax-responder');
$fields = array();
drupal_add_css(drupal_get_path('module', 'contentanalysis') . '/contentanalysis.css');
$display = variable_get('contentanalysis_display', array(
'sections',
'main',
));
$analyzers = module_invoke_all('contentanalysis_analyzers');
drupal_alter('contentanalysis_analyzers', $analyzers);
$analyzer_modules = array();
foreach ($analyzers as $aid => $analyzer) {
if ($analyzer['module']) {
$analyzer_modules[$aid] = array(
'aid' => $aid,
'module' => $analyzer['module'],
);
}
}
if (!($code = $_SESSION['contentanalysis']['code'])) {
$code = md5(rand());
$_SESSION['contentanalysis']['code'] = $code;
}
$report_style = variable_get('contentanalysis_node_report_display', 'both');
$display_inline = 1;
$display_dialog = 1;
if ($report_style == 'dialog') {
$display_inline = 0;
}
elseif ($report_style == 'inline') {
$display_dialog = 0;
}
static $js_added;
if (!$js_added) {
drupal_add_js(array(
'contentanalysis' => array(
'analyze_callback' => variable_get('contentanalysis_host', '') . base_path() . 'contentanalysis/analyze_js',
'base_path' => base_path(),
'path_to_module' => base_path() . drupal_get_path('module', 'contentanalysis'),
'nid' => (int) $node->nid ? $node->nid : -1,
'node_type' => $node->type,
'display_sections' => $display['sections'] ? 1 : 0,
'display_main' => $display['main'] ? 1 : 0,
'analyze_on_start' => variable_get('contentanalysis_analyze_on_start', '0'),
'analyzer_modules' => $analyzer_modules,
'code' => $code,
'display_dialog' => $display_dialog,
'display_inline' => $display_inline,
),
), 'setting');
$js_added = TRUE;
}
$analyzers = module_invoke_all('contentanalysis_analyzers');
drupal_alter('contentanalysis_analyzers', $analyzers);
$fields['analyzers'] = contentanalysis_get_analyzer_form_element($analysis, $analyzers);
foreach ($analyzers as $aid => $analyzer) {
if ($analyzer['form elements callback']) {
$e = call_user_func($analyzer['form elements callback'], $form_state, $analysis, $node);
if (is_array($e)) {
$fs = array(
'#type' => 'fieldset',
'#title' => $analyzer['title'] . ' ' . t('analyzer options'),
'#tree' => TRUE,
);
$fields[$aid] = array_merge($fs, $e);
}
}
}
if ($mode == 'node-edit') {
$form = $fields;
$report_style = variable_get('contentanalysis_node_report_display', 'both');
if ($report_style == 'both') {
$form['clickme'] = array(
'#type' => 'markup',
'#value' => '<div id="contentanalysis-buttons">' . ($output = ctools_ajax_text_button(t('Analyze content'), 'contentanalysis/ajax/analysis', t('Content Analysis'), 'contentanalysis-button', 'ctools-use-modal') . '</div>'),
);
}
elseif ($report_style == 'dialog') {
$form['clickme'] = array(
'#type' => 'markup',
'#value' => '<div id="contentanalysis-buttons">' . ($output = ctools_ajax_text_button(t('Analyze content'), 'contentanalysis/ajax/analysis_only', t('Content Analysis'), 'contentanalysis-button', 'ctools-use-modal') . '</div>'),
);
}
elseif ($report_style == 'inline') {
$form['clickme'] = array(
'#type' => 'markup',
'#value' => '<div id="contentanalysis-buttons">' . l(t('Analyze content'), base_path() . 'contentanalysis/analyze_js', array(
'attributes' => array(
'id' => 'contentanalysis-ininline-analysis-button',
'class' => "contentanalysis-analyze-content contentanalysis-button",
"onclick" => "contentanalysis_inline_analysis(); return (false);",
),
)) . '</div>',
);
}
else {
$v = '<div id="contentanalysis-buttons">';
$v .= t('Analyze content') . ': ';
$v .= ctools_ajax_text_button(t('Full reports'), 'contentanalysis/ajax/analysis', t('Content Analysis'), 'contentanalysis-button', 'ctools-use-modal');
$v .= ' ';
$v .= ctools_ajax_text_button(t('Main report'), 'contentanalysis/ajax/analysis_only', t('Content Analysis'), 'contentanalysis-button', 'ctools-use-modal');
$v .= ' ';
$v .= '<a href="#" id="contentanalysis-ininline-analysis-button" class="contentanalysis-analyze-content contentanalysis-button" onclick="contentanalysis_inline_analysis(); return (false);">' . t('Inline reports') . '</a>';
$v .= '</div>';
$form['clickme'] = array(
'#type' => 'markup',
'#value' => $v,
);
}
}
elseif ($mode == 'admin') {
$form = $fields;
$form['clickme'] = array(
'#type' => 'markup',
'#value' => '<div>' . ($output = ctools_ajax_text_button(t('Analyze content'), 'contentanalysis/ajax/analysis', t('Content Analysis'), 'contentanalysis-button', 'ctools-use-modal') . '</div>'),
);
}
else {
$form['clickme'] = array(
'#type' => 'markup',
'#value' => '<div>' . ($output = ctools_ajax_text_button(t('Analyze content'), 'contentanalysis/ajax/analysis', t('Content Analysis'), 'contentanalysis-button', 'ctools-use-modal') . '</div>'),
);
$form['contentanalysis_options'] = array(
'#title' => t('options'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['contentanalysis_options'] = array_merge($form['contentanalysis_options'], $fields);
}
return $form;
}
function contentanalysis_get_analyzer_form_element($params = array(), $analyzers = array()) {
if (empty($analyzers)) {
$analyzers = module_invoke_all('contentanalysis_analyzers');
drupal_alter('contentanalysis_analyzers', $analyzers);
}
if ($analyzers) {
$options = array();
$defaults = array();
foreach ($analyzers as $aid => $analyzer) {
$options[$aid] = $analyzer['title'];
$defaults[] = $aid;
}
$default_value = array();
if ($analysis && is_array($analysis['#context']['analyzers'])) {
foreach ($analysis['#context']['inputs']['analyzers'] as $aid => $item) {
$default_value[] = $aid;
}
}
else {
$default_value = variable_get('contentanalysis_default_analyzers', $defaults);
}
$analyser_override = '';
if (count($options) == 1) {
$k = array_keys($options);
$element = array(
'#type' => 'hidden',
'#value' => $k[0],
'#prefix' => '<div id="contentanalysis_analyzers_override">',
'#suffix' => '</div>',
);
}
else {
$element = array(
'#type' => 'checkboxes',
'#title' => $params['title'] ? $params['title'] : t('Analyzers'),
'#description' => $params['description'] ? $params['description'] : t('Select analyzers for content.'),
'#options' => $options,
'#default_value' => $default_value,
'#prefix' => '<div id="contentanalysis_analyzers">',
'#suffix' => '</div>',
);
}
}
else {
}
return $element;
}
function contentanalysis_analyze_js() {
$analysis = array();
if ($_SESSION['contentanalysis']['code'] != $_POST['code']) {
exit;
}
$context = contentanalysis_get_default_context();
$context['form_id'] = 'contentanalysis_analyze_js';
$context['inputs']['nid'] = $_POST['nid'] == '-1' ? NULL : $_POST['nid'];
$context['inputs']['url'] = $_POST['url'] == '-1' ? NULL : $_POST['url'];
$context['inputs']['title'] = $_POST['title'] == '-1' ? NULL : $_POST['title'];
$context['inputs']['page_title'] = $_POST['page_title'] == '-1' ? NULL : $_POST['page_title'];
$context['inputs']['body'] = $_POST['body'] == '-1' ? NULL : $_POST['body'];
$context['inputs']['meta_keywords'] = $_POST['meta_keywords'] == '-1' ? NULL : $_POST['meta_keywords'];
$context['inputs']['meta_description'] = $_POST['meta_description'] == '-1' ? NULL : $_POST['meta_description'];
$context['inputs']['page'] = $_POST['page'] == '-1' ? NULL : $_POST['page'];
$context['inputs']['node_type'] = $_POST['node_type'] == '-1' ? NULL : $_POST['node_type'];
$context['inputs']['body_input_filter'] = $_POST['body_input_filter'] == '-1' ? NULL : $_POST['body_input_filter'];
$context['inputs']['hidden'] = $_POST['hidden'] == '-1' ? NULL : $_POST['hidden'];
$context['inputs']['action'] = $_POST['action'] == '-1' ? NULL : $_POST['action'];
$context['nid'] = $context['inputs']['nid'];
$context['url'] = $context['inputs']['url'];
$context['source'] = $_POST['source'];
$a = explode(',', $_POST['analyzers']);
$context['inputs']['analyzers'] = array();
if (is_array($a)) {
foreach ($a as $aid) {
$context['inputs']['analyzers'][$aid] = $aid;
}
}
$analyzers = module_invoke_all('contentanalysis_analyzers');
foreach ($_POST as $k => $v) {
if (substr($k, 0, 3) == 'ao_') {
list($t, $aid, $vk) = explode('_', $k);
$context['inputs']['analyzer_options'][$aid][$vk] = $v;
}
}
$context['title'] = $context['inputs']['title'];
$context['page_title'] = $context['inputs']['page_title'];
$context['body'] = $context['inputs']['body'];
$context['meta_keywords'] = $context['inputs']['meta_keywords'];
$context['meta_description'] = $context['inputs']['meta_description'];
$context['page'] = $context['inputs']['page'];
if ($c = module_invoke_all('contentanalysis_analyze_context', $context)) {
$context = $c;
}
if ($context['options']['exit']) {
$analysis['#context'] = $context;
}
else {
if ($_POST['source'] == 'admin-form' && $context['url'] && !$context['nid'] && !$context['body']) {
$context['page'] = file_get_contents($context['url']);
}
$analysis = contentanalysis_do_analysis($context);
drupal_alter('contentanalysis_analyzers', $analyzers);
foreach ($analyzers as $aid => $analyzer) {
if ($analyzer['admin form submit callback']) {
call_user_func($analyzer['admin form submit callback'], $form, $form_state);
}
}
$analysis = contentanalysis_build_analysis_content($analysis);
contentanalysis_save_status($analysis);
}
$output = array();
$output['main']['output'] = theme_contentanalysis_analysis($analysis);
$output['page_title']['output'] = theme_contentanalysis_section_analysis($analysis, 'page_title');
$output['body']['output'] = theme_contentanalysis_section_analysis($analysis, 'body');
$output['meta_keywords']['output'] = theme_contentanalysis_section_analysis($analysis, 'meta_keywords');
$output['meta_description']['output'] = theme_contentanalysis_section_analysis($analysis, 'meta_description');
$output['inputs']['analyzers'] = implode(",", $analysis['#context']['inputs']['analyzers']);
$output['inputs']['action'] = $analysis['#context']['inputs']['action'];
drupal_json($output);
}
function contentanalysis_save_status($analysis) {
$aid = $analysis['#context']['aid'];
foreach ($analysis as $anid => $analysisi) {
if (substr($anid, 0, 1) == '#' || !is_array($analysisi)) {
continue;
}
if ($analysisi['#status'] != 'status' || $analysisi['#score']) {
$statusi = array(
'error' => 0,
'warning' => 1,
'complete' => 2,
);
$set = '';
if ($analysisi['#score']) {
$set = ', score = %f';
}
$sql = 'UPDATE {contentanalysis_status} SET status = "%s", statusi = %d ' . $set . '
WHERE aid = %d AND analyzer = "%s"';
if ($analysisi['#score']) {
db_query($sql, $analysisi['#status'], $statusi[$analysisi['#status']], $analysisi['#score'], $aid, $anid);
}
else {
db_query($sql, $analysisi['#status'], $statusi[$analysisi['#status']], $aid, $anid);
}
if (!db_affected_rows()) {
if ($analysisi['#score']) {
$sql = 'INSERT INTO {contentanalysis_status}
(aid, analyzer, status, statusi, score)
VALUES (%d, "%s", "%s", %d, %f)';
db_query($sql, $aid, $anid, $analysisi['#status'], $statusi[$analysisi['#status']], $analysisi['#score']);
}
else {
$sql = 'INSERT INTO {contentanalysis_status}
(aid, analyzer, status, statusi)
VALUES (%d, "%s", "%s", %d)';
db_query($sql, $aid, $anid, $analysisi['#status'], $statusi[$analysisi['#status']]);
}
}
}
}
}
function contentanalysis_get_default_context() {
$context = array(
'aid' => NULL,
'form_id' => NULL,
'source' => NULL,
'nid' => NULL,
'path' => NULL,
'url' => NULL,
'page' => NULL,
'title' => NULL,
'node_body' => NULL,
'body' => NULL,
'body_notags' => NULL,
'page_title' => NULL,
'meta_keywords' => NULL,
'meta_description' => NULL,
'analyzers' => array(),
);
return $context;
}
function contentanalysis_parse_context($the_context) {
$context = contentanalysis_get_default_context();
if (is_array($the_context)) {
$context = array_merge($context, $the_context);
}
if ($context['source'] == 'node-edit-form') {
return contentanalysis_parse_context_node_edit($context);
}
elseif ($context['nid'] > 0) {
return contentanalysis_parse_context_node_load($context);
}
elseif ($context['body']) {
$context['body_notags'] = strip_tags($context['body']);
}
elseif ($context['page']) {
return contentanalysis_parse_context_page($context);
}
return $context;
}
function contentanalysis_parse_context_node_edit($context) {
if ($context['nid'] && is_numeric($context['nid'])) {
$context['node'] = node_load($context['nid']);
}
if (!$context['node']) {
global $user;
$context['node'] = new stdClass();
if ($context['nid'] && is_numeric($context['nid'])) {
$context['node']->nid = $context['nid'];
}
$context['node']->status = 1;
$context['node']->uid = $user->uid;
$context['node']->created = time();
$context['node']->changed = time();
$context['node']->promote = 1;
$context['node']->sticky = 0;
$context['node']->language = 'en';
}
$context['node']->type = $context['inputs']['node_type'];
$context['node']->title = $context['title'];
$context['node']->body = $context['body'];
$context['node']->teaser = node_teaser($context['body']);
$context['node']->format = $context['inputs']['body_input_filter'];
$context['node_body'] = $context['body'];
if (variable_get('contentanalysis_node_parse_nodetitle_prepend', 1)) {
$h = $context['title'];
$p = variable_get('contentanalysis_node_parse_nodetitle_tags', '<h2>[node_title]</h2>');
if (strpos($p, '[node_title]') !== FALSE) {
$h = str_replace('[node_title]', $h, $p);
}
$context['body'] = "<h1>" . $h . "</h1> " . $context['body'];
}
$context['body_notags'] = strip_tags($context['body']);
if (!$context['page_title']) {
$context['page_title'] = $context['title'];
}
if (is_null($context['path'])) {
$path = 'node/' . $context['nid'];
$path_alias = drupal_get_path_alias($path);
$context['path'] = $path_alias ? $path_alias : $path;
}
if (is_null($context['url'])) {
$url = 'http';
if ($_SERVER["HTTPS"] == "on") {
$url .= "s";
}
$url .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"];
}
else {
$url .= $_SERVER["SERVER_NAME"];
}
$url .= base_path();
$url .= $context['path'];
$context['url'] = $url;
}
if (module_exists('nodewords')) {
$default_nodewords = nodewords_load_tags();
if (is_null($context['meta_description'])) {
if (function_exists('nodewords_metatag_from_node_content')) {
$description = nodewords_metatag_from_node_content($context['node'], '');
}
elseif (function_exists('nodewords_metatag_from_node_content')) {
$description = nodewords_metatag_from_teaser($context['node'], '');
}
if ($description) {
$context['meta_description'] = $description;
}
else {
$context['meta_description'] = $default_nodewords['description']['value'];
}
}
if (is_null($context['meta_keywords'])) {
$context['meta_keywords'] = $default_nodewords['keywords']['value'];
}
}
return $context;
}
function contentanalysis_parse_context_node_load($context) {
dsm($context);
if (!$context['nid']) {
return $context;
}
$node = node_load($context['nid']);
$context['node'] = $node;
$context['title'] = !is_null($context['title']) ? $context['title'] : $node->title;
if (is_null($context['page_title'])) {
$context_title = $context['title'];
$context_page_title = $context['title'];
if (module_exists('page_title')) {
$page_title_pattern = variable_get('page_title_type_' . (isset($node->type) ? $node->type : ''), '');
if (empty($page_title_pattern)) {
$page_title_pattern = variable_get('page_title_default', '[page-title] | [site-name]');
}
$page_title_pattern = str_replace('[page-title]', $context_page_title && !is_null($context_page_title) ? $context_page_title : $context_title, $page_title_pattern);
$page_title_pattern = str_replace('[title]', check_plain($context_title), $page_title_pattern);
$page_title_pattern = str_replace('[title-raw]', $context_title, $page_title_pattern);
$types = array();
if (isset($node)) {
$types['node'] = $node;
}
$types['page_title'] = NULL;
$context['page_title'] = token_replace_multiple($page_title_pattern, $types);
}
}
else {
$context['page_title'] = $context['title'] ? $context['title'] : $node->title;
}
if (is_null($context['node_body'])) {
$context['node_body'] = $node->body;
}
if (is_null($context['body'])) {
$context['body'] = $context['node_body'];
}
if (variable_get('contentanalysis_node_parse_nodetitle_prepend', 1)) {
$h = $context['title'];
$p = variable_get('contentanalysis_node_parse_nodetitle_tags', '<h2>[node_title]</h2>');
if (strpos($p, '[node_title]') !== FALSE) {
$h = str_replace('[node_title]', $h, $p);
}
$context['body'] = "<h1>" . $h . "</h1> " . $context['body'];
}
if (is_null($context['body_notags'])) {
$context['body_notags'] = strip_tags($context['body']);
}
if (isset($node->nodewords)) {
if (is_null($context['meta_keywords'])) {
$context['meta_keywords'] = $context['inputs']['meta_keywords'] ? $context['inputs']['meta_keywords'] : $node->nodewords['keywords'];
}
if (is_null($context['meta_description'])) {
$context['meta_description'] = $context['inputs']['meta_description'] ? $context['inputs']['meta_description'] : $node->nodewords['description'];
}
}
if (is_null($context['path'])) {
$path = 'node/' . $context['nid'];
$path_alias = drupal_get_path_alias($path);
$context['path'] = $path_alias ? $path_alias : $path;
}
if (is_null($context['url'])) {
$url = 'http';
if ($_SERVER["HTTPS"] == "on") {
$url .= "s";
}
$url .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"];
}
else {
$url .= $_SERVER["SERVER_NAME"];
}
$url .= base_path();
$url .= $context['path'];
$context['url'] = $url;
}
return $context;
}
function contentanalysis_parse_context_page($context) {
if (!$context['page']) {
return $context;
}
$count = preg_match('/<title>(.+?)<\\/title>/isx', $context['page'], $match);
$context['page_title'] = $match[1];
$count = preg_match("|<meta[^>]*keywords[^>]*content=\"([^>]+)\"[^>]*>|Ui", $context['page'], $match);
$context['meta_keywords'] = $match[1];
$count = preg_match("|<meta[^>]*description[^>]*content=\"([^>]+)\"[^>]*>|Ui", $context['page'], $match);
$context['meta_description'] = $match[1];
$count = preg_match('/(<body.*>)(.+?)(<\\/body>)/ismU', $context['page'], $match);
$context['body'] = $match[2];
$context['body_notags'] = strip_tags($context['body']);
return $context;
}
function contentanalysis_do_analysis($context, $analyzers = -1, $analyzers_params = array()) {
$analysis_struc = array(
'#title' => '',
'#status' => 'status',
'content' => array(),
'stats' => array(),
'tests' => array(),
'messages' => array(),
'page_title' => array(
'#title' => t('Page title'),
'#status' => 'status',
),
'body' => array(
'#title' => t('Body'),
'#status' => 'status',
),
'meta_keywords' => array(
'#title' => t('Meta keywords'),
'#status' => 'status',
),
'meta_description' => array(
'#title' => t('Meta description'),
'#status' => 'status',
),
);
if ($context['source'] == 'admin') {
if (!$context['inputs']['nid'] && !$context['inputs']['body'] && !$context['inputs']['url']) {
$analysis = $analysis_struc;
$analysis['messages'] = contentanalysis_format_message(t('No input was submitted. Please enter content, a Node ID or URL.'), 'error');
return $analysis;
}
}
$context = contentanalysis_parse_context($context);
drupal_alter('contentanalysis_context_parser', $context);
$save = $context['options']['dontsave'] ? 0 : 1;
$context['aid'] = contentanalysis_get_aid($context, $save);
if ($context['source'] == 'admin-form' && is_null($context['inputs']['url']) && is_null($context['inputs']['nid'])) {
$analysis_struc['body']['#title'] = t('Text');
unset($analysis_struc['page_title']);
unset($analysis_struc['meta_keywords']);
unset($analysis_struc['meta_description']);
}
$analysis = array();
$analysis['#context'] = $context;
$analyzers = module_invoke_all('contentanalysis_analyzers');
drupal_alter('contentanalysis_analyzers', $analyzers);
foreach ($analyzers as $aid => $analyzer) {
if ($context['inputs']['analyzers'][$aid]) {
$analysis_struc['#title'] = $analyzer['title'];
$analysis[$aid] = call_user_func($analyzer['callback'], $context, $analysis_struc, $analyzers_params[$aid]);
}
}
if (variable_get('contentanalysis_analyzerchecklist', 1)) {
$analysis_struc['#title'] = t('Analyzer checklist');
$analysis['contentanalysis'] = contentanalysis_checklist_analyzer($context, $analysis_struc);
}
return $analysis;
}
function contentanalysis_checklist_analyzer($context, $analysis) {
$analysis['content'][] = contentanalysis_format_content(t('The content analysis module enables a suite of analyzers to help you develop better, more optimized content. <br> <br> List of available analyzer modules:'), -5);
$modules = array(
array(
'name' => t('Content optimizer'),
'module' => 'contentoptimizer',
'description' => t('Quick search engine optimization (SEO) analysis to assure your content follows basic SEO guidelines.'),
'recommended' => TRUE,
'link' => 'http://www.drupal.org/project/contentoptimizer',
),
array(
'name' => t('Scribe SEO'),
'module' => 'scribeseo',
'description' => t('Advanced search engine optimization (SEO) analysis. Features content scoring, keyword extraction, sample SERP listing and optimization recommendations.'),
'recommended' => TRUE,
'link' => 'http://www.drupal.org/project/scribeseo',
),
array(
'name' => t('Readability'),
'module' => 'readability',
'description' => t('Grades content for readability using five popular automated readability standards.'),
'recommended' => TRUE,
'link' => 'http://www.drupal.org/project/readability',
),
array(
'name' => t('Alchemy'),
'module' => 'alchemy',
'description' => t('Extracts keywords, concepts and entities from content using the Alchemy service.'),
'recommended' => TRUE,
'link' => 'http://www.drupal.org/project/alchemy',
),
array(
'name' => t('Keyword research'),
'module' => 'kwresearch',
'description' => t('Provides search popularity and other statistics for researching keyword phrases.'),
'recommended' => TRUE,
'link' => 'http://www.drupal.org/project/kwresearch',
),
array(
'name' => t('W3C analyzer'),
'module' => 'w3canalyzer',
'description' => t('Validates that content is W3C compliant.'),
'recommended' => TRUE,
'link' => 'http://www.drupal.org/project/w3canalyzer',
),
);
foreach ($modules as $module) {
$status = 'status';
$msg_vars = array(
'@module_name' => $module['name'],
'!enabled_status' => '',
);
if (module_exists($module['module'])) {
$msg_vars['!enabled_status'] = t('enabled');
$link = FALSE;
if ($module['recommended']) {
$status = 'pass';
}
}
else {
$msg_vars['!enabled_status'] = t('not enabled');
$link = l(t('Click here to download.'), $module['link'], array(
'target' => '_drupalorg',
));
if ($module['recommended']) {
$status = 'warning';
}
}
$msg = t('<strong>@module_name</strong> is !enabled_status.', $msg_vars);
if ($link) {
$msg .= ' ' . $link;
}
$analysis['messages'][] = contentanalysis_format_message($msg, $status);
$analysis['messages'][] = contentanalysis_format_message($module['description'], 'status');
}
return $analysis;
}
function contentanalysis_build_analysis_content($analysis) {
$ignore_keys = array(
'content',
'context',
'stats',
'tests',
'messages',
);
foreach ($analysis as $aid => $analysisi) {
if (substr($aid, 0, 1) == '#' || !is_array($analysisi)) {
continue;
}
$content = theme_contentanalysis_status_report($analysisi);
$analysis[$aid]['content'][] = array(
'#value' => $content,
'#weight' => 0,
);
$analysis[$aid]['sections'] = array();
foreach ($analysisi as $sid => $analysisis) {
if (substr($sid, 0, 1) == '#' || in_array($sid, $ignore_keys)) {
continue;
}
$content = '';
$analysis[$aid]['sections'][] = $sid;
$content = theme_contentanalysis_status_report($analysisi, $sid);
if ($content) {
if (!is_array($analysis[$aid][$sid]['content'])) {
$analysis[$aid][$sid]['content'] = array();
}
$analysis[$aid][$sid]['content'][] = array(
'#value' => $content,
'#weight' => 0,
);
}
}
}
$tabs = '';
$con = '';
foreach ($analysis as $aid => $analysisi) {
$ti = 0;
if (substr($aid, 0, 1) == '#' || !is_array($analysisi)) {
continue;
}
$analysis[$aid]['output'] = '<div id="contentanalysis-report-' . $aid . '" class="contentanalysis-report">';
if ($analysisi['content'][0]['#tab']) {
$tabs = '<h3 id="contentanalysis-report-tab-' . $aid . '-0" class="contentanalysis-report-tab">' . $analysisi['content'][0]['#value'] . "</h3>\n";
}
else {
$tabs = '<h3 id="contentanalysis-report-tab-' . $aid . '-0" class="contentanalysis-report-tab">' . t('Report') . "</h3>\n";
}
uasort($analysisi['content'], "element_sort");
$i = 0;
$con = '<div id="contentanalysis-report-results-' . $aid . '-' . $ti . '" class="contentanalysis-results-section">';
foreach ($analysisi['content'] as $item) {
if ($item['#tab']) {
if ($i == 0) {
continue;
}
$ti++;
$con .= '</div>';
$tabs .= '<h3 id="contentanalysis-report-tab-' . $aid . '-' . $ti . '" class="contentanalysis-report-tab">' . $item['#value'] . "</h3>\n";
$con .= '<div id="contentanalysis-report-results-' . $aid . '-' . $ti . '" class="contentanalysis-results-section">';
}
else {
$con .= $item['#value'];
}
$i++;
}
$con .= '</div>';
$analysis[$aid]['output'] .= '<div id="contentanalysis-report-tabs-' . $aid . '" class="contentanalysis-report-tabs">';
$analysis[$aid]['output'] .= $tabs;
$analysis[$aid]['output'] .= '</div>';
$analysis[$aid]['output'] .= '<div id="contentanalysis-report-results-' . $aid . '" class="contentanalysis-results">';
$analysis[$aid]['output'] .= $con;
$analysis[$aid]['output'] .= '</div>';
$analysis[$aid]['output'] .= "</div>\n";
foreach ($analysisi['sections'] as $sid) {
if (!is_array($analysisi[$sid]['content'])) {
continue;
}
uasort($analysisi[$sid]['content'], "element_sort");
foreach ($analysisi[$sid]['content'] as $content) {
$analysis[$aid][$sid]['output'] .= $content['#value'];
}
}
}
return $analysis;
}
function theme_contentanalysis_analysis($analysis) {
$titles = array();
$bodies = array();
if (count($analysis['#context']['messages']) > 0) {
$bodies[] = theme_contentanalysis_messages($analysis['#context']['messages']);
}
foreach ($analysis as $aid => $analysisi) {
if (!is_array($analysisi) || substr($aid, 0, 1) == '#') {
continue;
}
$titles[] = '<h3 id="contentanalysis-analyzer-tab-' . $aid . '" class="analyzer">' . $analysisi['#title'] . "</h3>\n";
$bodies[] = '<div class="analyzer-analysis"><h2>' . $analysisi['#title'] . '</h2>' . $analysisi['output'] . '</div>';
}
$out .= '<h2 class="analyzers">Analyzers</h2>';
$out .= '<div class="analyzers">' . implode($titles) . '</div>';
$out .= '<div class="analysis-results">' . implode($bodies) . '</div>';
return $out;
}
function theme_contentanalysis_section_analysis($analysis, $section) {
if ($analysis['#context']['inputs']['action'] != 'refresh') {
$form['report-' . $section] = array(
'#type' => 'fieldset',
'#title' => t('Content analysis report'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#attributes' => array(
'class' => 'contentanalysis_section_analysis analysis-results contentanalysis-results',
),
);
}
$i = 0;
foreach ($analysis as $aid => $analysisi) {
if (!is_array($analysisi) || substr($aid, 0, 1) == '#' || !is_array($analysisi[$section]) || !$analysisi[$section]['output']) {
continue;
}
$img = base_path() . '/' . drupal_get_path('module', 'contentanalysis') . '/icons/refresh.png';
$form['report-' . $section][$aid . '-report'] = array(
'#type' => 'item',
'#title' => $analysisi['#title'],
'#value' => $analysisi[$section]['output'],
'#prefix' => '<div id="contentanalysis-report-' . $aid . '-' . $section . '" class="contentanalysis-report-' . $aid . '">',
'#suffix' => '</div>',
'#weight' => $i++,
);
}
$output = $i ? drupal_render($form) : '';
return $output;
}
function theme_contentanalysis_status_report($analysisi, $show = 'all') {
$out = '';
$rows = array();
if ($show == 'all' || $show == 'general') {
$stats = theme_contentanalysis_analysis_stats($analysisi['stats']);
$msgs = theme_contentanalysis_analysis_messages($analysisi['messages']);
$status = $analysisi['#status'] ? $analysisi['#status'] : 'status';
if ($stats || $msgs) {
$rows[] = array(
array(
'data' => t('General'),
'class' => 'section ' . $status,
),
array(
'data' => $stats . $msgs,
'class' => 'messages',
),
);
}
}
if ($show == 'all' || $show == 'sections') {
foreach ($analysisi as $sid => $analysisis) {
$stats = theme_contentanalysis_analysis_stats($analysisis['stats']);
$msgs = theme_contentanalysis_analysis_messages($analysisis['messages']);
$status = $analysisis['#status'] ? $analysisis['#status'] : 'status';
if ($stats || $msgs) {
$rows[] = array(
array(
'data' => $analysisis['#title'],
'class' => 'section ' . $status,
),
array(
'data' => $stats . $msgs,
'class' => 'messages',
),
);
}
}
}
elseif ($analysisis = $analysisi[$show]) {
$stats = theme_contentanalysis_analysis_stats($analysisis['stats']);
$msgs = theme_contentanalysis_analysis_messages($analysisis['messages']);
$status = $analysisis['#status'] ? $analysisis['#status'] : 'status';
if ($stats || $msgs) {
$rows[] = array(
array(
'data' => $analysisis['#title'],
'class' => 'section ' . $status,
),
array(
'data' => $stats . $msgs,
'class' => 'messages',
),
);
}
}
if (count($rows) > 0) {
$header = array(
array(
'data' => t('Sections'),
),
array(
'data' => t('Analysis'),
),
);
$out .= theme_table($header, $rows, array(
'class' => 'status_report',
));
}
return $out;
}
function theme_contentanalysis_analysis_stats($stats) {
if (!$stats || !is_array($stats)) {
return '';
}
$output .= '<div class="contentanalysis_stats">' . "\n";
$i = 0;
foreach ($stats as $sid => $item) {
$output .= ($i ? ', ' : '') . $item['#title'] . '=' . number_format($item['#value'], $item['#decimals'] ? $item['#decimals'] : 0);
$i++;
}
$output .= "</div>\n";
return $output;
}
function theme_contentanalysis_analysis_messages($msgs) {
if (!$msgs || !is_array($msgs)) {
return '';
}
foreach ($msgs as $mid => $item) {
$items[] = array(
'class' => $item['#status'],
'data' => $item['#value'],
);
}
return theme_item_list($items, NULL, 'ul', array(
'class' => 'contentanalysis_messages',
));
}
function theme_contentanalysis_messages($msgs) {
if (!$msgs || !is_array($msgs)) {
return '';
}
foreach ($msgs as $mid => $item) {
$out .= '<div class="messages ' . $item['#status'] . '">';
$out .= $item['#value'];
$out .= '</div>';
}
return $out;
}
function contentanalysis_format_stat($title, $value, $decimals = 0) {
return array(
'#title' => $title,
'#value' => $value,
'#decimals' => $decimals,
);
}
function contentanalysis_format_message($message, $type = 'status') {
return array(
'#value' => $message,
'#status' => $type,
);
}
function contentanalysis_format_content($value, $weight = -1, $tab = FALSE) {
$ret = array(
'#value' => $value,
'#weight' => $weight,
);
if ($tab) {
$ret['#tab'] = TRUE;
}
return $ret;
}
function contentanalysis_form_alter(&$form, $form_state, $form_id) {
if (isset($form['type']['#value']) && $form['type']['#value'] . '_node_form' == $form_id && variable_get('contentanalysis_type_' . $form['#node']->type . '_enable', 1)) {
if (!user_access('perform content analysis')) {
return '';
}
$node = $form['#node'];
$form['contentanalysis'] = array(
'#type' => 'fieldset',
'#title' => t('Content analysis'),
'#collapsible' => TRUE,
'#collapsed' => variable_get('contentanalysis_node_edit_form_collapsed', 0),
'#attributes' => array(
'class' => 'contentanalysis',
),
);
$form['contentanalysis'] = array_merge($form['contentanalysis'], contentanalysis_analysis_form($form_state, array(), $node, 'node-edit'));
$form['#submit'][] = 'contentanalysis_node_submit';
}
if ($form_id == 'node_type_form') {
$form['contentanalysis'] = array(
'#type' => 'fieldset',
'#title' => t('Content Analysis Settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
$form['contentanalysis']['enable'] = array(
'#type' => 'checkboxes',
'#title' => t('Content Analysis'),
'#description' => t('If checked, the <em>Content Analysis</em> fieldset will appear on the node edit form for those who have permission to perform content analysis.'),
'#options' => array(
'enable' => t('Enable'),
),
'#default_value' => variable_get('contentanalysis_type_' . $form['#node_type']->type . '_enable', 1) ? array(
'enable',
) : array(),
);
$form['#submit'][] = 'contentanalysis_node_type_form_submit';
}
}
function contentanalysis_node_type_form_submit($form, &$form_state) {
$enable = $form_state['values']['contentanalysis']['enable']['enable'] ? 1 : 0;
variable_set('contentanalysis_type_' . $form_state['values']['type'] . '_enable', $enable);
}
function contentanalysis_node_submit($form, &$form_state) {
$analyzers = module_invoke_all('contentanalysis_analyzers');
drupal_alter('contentanalysis_analyzers', $analyzers);
foreach ($analyzers as $aid => $analyzer) {
if ($analyzer['node form submit callback']) {
$analysis[$aid] = call_user_func($analyzer['node form submit callback'], $form, $form_state);
}
}
}
function contentanalysis_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks['launch']['info'] = t('Content analysis');
$blocks['launch']['cache'] = BLOCK_NO_CACHE;
return $blocks;
case 'view':
if (arg(0) == 'admin' || arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'edit' || !user_access('perform content analysis')) {
return '';
}
if (!(arg(0) == 'node' && arg(2) == 'edit' || arg(1) == 'add')) {
$block['content'] = drupal_get_form('contentanalysis_analysis_form', array(), array(), 'block');
}
return $block;
}
}
function contentanalysis_get_aid($context, $save = 1) {
$aid = NULL;
$where = '';
if ($context['source'] == 'node-edit-form' && is_null($context['nid'])) {
return $aid;
}
if ($context['aid'] > 0) {
$where = 'aid = %d';
$wherev = $context['aid'];
}
elseif ($context['nid'] > 0) {
$where = 'nid = %d';
$wherev = $context['nid'];
}
elseif (!is_null($context['path'])) {
$where = 'path = "%s"';
$wherev = $context['path'];
}
elseif (!is_null($context['url'])) {
$where = 'url = "%s"';
$wherev = $context['url'];
}
if ($where && $save) {
$sql = '
SELECT *
FROM {contentanalysis}
WHERE ' . $where . '
';
$result = db_fetch_object(db_query($sql, $wherev));
if ($result->aid) {
$aid = $result->aid;
$sql = '
UPDATE {contentanalysis}
SET last_analysis = %d,
nid = %d,
path = "%s",
url = "%s"
WHERE aid = %d
';
db_query($sql, time(), $context['nid'], $context['path'], $context['url'], $context['aid']);
}
else {
$sql = '
INSERT INTO {contentanalysis}
(aid, last_analysis, nid, path, url)
VALUES
(NULL,%d,%d,"%s","%s")
';
db_query($sql, time(), $context['nid'], $context['path'], $context['url']);
$aid = db_last_insert_id('contentanalysis', 'aid');
}
}
return $aid;
}
function contentanalysis_get_aid_by_nid($nid) {
$sql = '
SELECT aid
FROM {contentanalysis}
WHERE nid = %d
';
return db_result(db_query($sql, $nid));
}
function contentanalysis_get_aid_by_url($url) {
$sql = '
SELECT aid
FROM {contentanalysis}
WHERE url = "%s"
';
return db_result(db_query($sql, $url));
}