Commit 62f68fc8 authored by Dave Reid's avatar Dave Reid
Browse files

by Dave Reid: Got basic functionality working for xmlsitemap_node.

parent 37c543f5
...@@ -289,7 +289,9 @@ function xmlsitemap_add_form_type_summary(&$form, $type, $items) { ...@@ -289,7 +289,9 @@ function xmlsitemap_add_form_type_summary(&$form, $type, $items) {
} }
$form[$type['type']]['list'] = array( $form[$type['type']]['list'] = array(
'#value' => theme('table', $header, $rows), '#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
); );
} }
......
// $Id$ // $Id$
Drupal.verticalTabs = Drupal.verticalTabs || {}; (function ($) {
Drupal.verticalTabs.xmlsitemap = function() { Drupal.behaviors.xmlsitemapFieldsetSummaries = {
var vals = []; attach: function (context) {
$('fieldset#edit-xmlsitemap', context).setSummary(function (context) {
var vals = [];
// Inclusion select field. // Inclusion select field.
var status = $('#edit-xmlsitemap-status option:selected').text(); var status = $('#edit-xmlsitemap-status option:selected').text();
vals.push(Drupal.t('Inclusion: @value', { '@value': status })); vals.push(Drupal.t('Inclusion: @value', { '@value': status }));
// Priority select field. // Priority select field.
var priority = $('#edit-xmlsitemap-priority option:selected').text(); var priority = $('#edit-xmlsitemap-priority option:selected').text();
vals.push(Drupal.t('Priority: @value', { '@value': priority })); vals.push(Drupal.t('Priority: @value', { '@value': priority }));
return vals.join('<br />'); return vals.join('<br />');
} });
}
};
})(jQuery);
...@@ -700,7 +700,7 @@ function xmlsitemap_restore_user() { ...@@ -700,7 +700,7 @@ function xmlsitemap_restore_user() {
function xmlsitemap_process_form_link_options($form, &$form_state) { function xmlsitemap_process_form_link_options($form, &$form_state) {
$link = &$form_state['values']['xmlsitemap']; $link = &$form_state['values']['xmlsitemap'];
$fields = array('status' => 1, 'priority' => 0.5); $fields = array('status' => 0, 'priority' => 0.5);
foreach ($fields as $field => $default) { foreach ($fields as $field => $default) {
if ($link[$field] === 'default') { if ($link[$field] === 'default') {
......
...@@ -6,4 +6,4 @@ core = 7.x ...@@ -6,4 +6,4 @@ core = 7.x
dependencies[] = xmlsitemap dependencies[] = xmlsitemap
files[] = xmlsitemap_node.module files[] = xmlsitemap_node.module
files[] = xmlsitemap_node.install files[] = xmlsitemap_node.install
;files[] = xmlsitemap_node.test files[] = xmlsitemap_node.test
// $Id$ // $Id$
Drupal.verticalTabs = Drupal.verticalTabs || {}; (function ($) {
Drupal.verticalTabs.xmlsitemap = function() { Drupal.behaviors.xmlsitemapFieldsetSummaries = {
var vals = []; attach: function (context) {
$('fieldset#edit-xmlsitemap', context).setSummary(function (context) {
var vals = [];
// Inclusion select field. // Inclusion select field.
var status = $('#edit-xmlsitemap-node-status option:selected').text(); var status = $('#edit-xmlsitemap-node-status option:selected').text();
vals.push(Drupal.t('Inclusion: @value', { '@value': status })); vals.push(Drupal.t('Inclusion: @value', { '@value': status }));
// Priority select field. // Priority select field.
var priority = $('#edit-xmlsitemap-node-priority option:selected').text(); var priority = $('#edit-xmlsitemap-node-priority option:selected').text();
vals.push(Drupal.t('Priority: @value', { '@value': priority })); vals.push(Drupal.t('Priority: @value', { '@value': priority }));
return vals.join('<br />'); return vals.join('<br />');
} });
}
};
})(jQuery);
...@@ -15,9 +15,9 @@ function xmlsitemap_node_cron() { ...@@ -15,9 +15,9 @@ function xmlsitemap_node_cron() {
*/ */
function xmlsitemap_node_xmlsitemap_index_links($limit) { function xmlsitemap_node_xmlsitemap_index_links($limit) {
if ($types = xmlsitemap_node_get_types()) { if ($types = xmlsitemap_node_get_types()) {
$nids = db_query_range("SELECT n.nid FROM {node} n LEFT JOIN {xmlsitemap} x ON x.type = 'node' AND n.nid = x.id WHERE x.id IS NULL AND n.type IN (:types) ORDER BY n.nid DESC", 0, $limit, array(':types' => $types)); $nids = db_query_range("SELECT n.nid FROM {node} n LEFT JOIN {xmlsitemap} x ON x.type = 'node' AND n.nid = x.id WHERE x.id IS NULL AND n.type IN (:types) ORDER BY n.nid DESC", 0, $limit, array(':types' => $types))->fetchCol();
foreach ($nids as $nid) { $nodes = node_load_multiple($nids);
$node = node_load($nid, NULL, TRUE); foreach ($nodes as $node) {
$link = xmlsitemap_node_create_link($node); $link = xmlsitemap_node_create_link($node);
xmlsitemap_save_link($link); xmlsitemap_save_link($link);
} }
...@@ -25,64 +25,66 @@ function xmlsitemap_node_xmlsitemap_index_links($limit) { ...@@ -25,64 +25,66 @@ function xmlsitemap_node_xmlsitemap_index_links($limit) {
} }
/** /**
* Implements hook_nodeapi(). * Implements hook_node_insert().
*/ */
function xmlsitemap_node_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { function xmlsitemap_node_node_insert($node) {
switch ($op) { xmlsitemap_node_node_update($node);
case 'load':
if ($data = xmlsitemap_load_link(array('type' => 'node', 'id' => $node->nid))) {
return array('xmlsitemap' => $data);
}
break;
case 'insert':
$link = xmlsitemap_node_create_link($node);
xmlsitemap_save_link($link);
break;
case 'update':
$link = xmlsitemap_node_create_link($node);
if (!empty($node->revision)) {
// Update the change frequency.
xmlsitemap_recalculate_changefreq($link);
}
xmlsitemap_save_link($link);
break;
case 'delete':
xmlsitemap_delete_link(array('type' => 'node', 'id' => $node->nid));
break;
}
} }
/** /**
* Implements hook_comment(). * Implements hook_node_update().
*/ */
function xmlsitemap_node_comment($comment, $op) { function xmlsitemap_node_node_update($node) {
switch ($op) { $link = xmlsitemap_node_create_link($node);
case 'delete': xmlsitemap_save_link($link);
case 'publish':
case 'unpublish':
$comment = (object) $comment;
if ($node = node_load($comment->nid, NULL, TRUE)) {
$link = xmlsitemap_node_create_link($node);
if ($op == 'publish') {
xmlsitemap_recalculate_changefreq($link);
}
xmlsitemap_save_link($link);
}
break;
}
} }
/** /**
* Implements hook_node_type(). * Implements hook_node_delete().
*/ */
function xmlsitemap_node_node_type($op, $info) { function xmlsitemap_node_node_delete($node) {
if ($op == 'delete') { xmlsitemap_delete_link(array('type' => 'node', 'id' => $node->nid));
variable_del('xmlsitemap_node_status_' . $info->type); }
variable_del('xmlsitemap_node_priority_' . $info->type);
//xmlsitemap_delete_link(array('type' => 'node', 'subtype' => $info->type)); /**
* Implements hook_comment_update().
*/
function xmlsitemap_node_comment_update(stdClass $comment) {
if ($node = node_load($comment->nid, NULL, TRUE)) {
xmlsitemap_node_node_update($node);
} }
} }
/**
* Implements hook_comment_publish().
*/
function xmlsitemap_node_comment_publish(stdClass $comment) {
xmlsitemap_node_comment_update($comment);
}
/**
* Implements hook_comment_unpublish().
*/
function xmlsitemap_node_comment_unpublish(stdClass $comment) {
xmlsitemap_node_comment_update($comment);
}
/**
* Implements hook_comment_delete().
*/
function xmlsitemap_node_comment_delete(stdClass $comment) {
xmlsitemap_node_comment_update($comment);
}
/**
* Implements hook_node_type_delete().
*/
function xmlsitemap_node_node_type_delete($info) {
variable_del('xmlsitemap_node_status_' . $info->type);
variable_del('xmlsitemap_node_priority_' . $info->type);
//xmlsitemap_delete_link(array('type' => 'node', 'subtype' => $info->type));
}
/** /**
* Implements hook_form_FORM_ID_alter(). * Implements hook_form_FORM_ID_alter().
* *
...@@ -100,7 +102,7 @@ function xmlsitemap_node_form_xmlsitemap_settings_form_alter(&$form, $form_state ...@@ -100,7 +102,7 @@ function xmlsitemap_node_form_xmlsitemap_settings_form_alter(&$form, $form_state
foreach ($node_types as $node_type => $node_type_name) { foreach ($node_types as $node_type => $node_type_name) {
$node_types[$node_type] = array( $node_types[$node_type] = array(
'name' => drupal_ucfirst($node_type_name), 'name' => drupal_ucfirst($node_type_name),
'link' => 'admin/content/node-type/' . str_replace('_', '-', $node_type), 'link' => 'admin/structure/types/manage/' . str_replace('_', '-', $node_type),
'status' => variable_get('xmlsitemap_node_status_' . $node_type, 0), 'status' => variable_get('xmlsitemap_node_status_' . $node_type, 0),
'priority' => variable_get('xmlsitemap_node_priority_' . $node_type, 0.5), 'priority' => variable_get('xmlsitemap_node_priority_' . $node_type, 0.5),
); );
...@@ -172,24 +174,8 @@ function xmlsitemap_node_form_alter(&$form, $form_state, $form_id) { ...@@ -172,24 +174,8 @@ function xmlsitemap_node_form_alter(&$form, $form_state, $form_id) {
$form['xmlsitemap']['#access'] |= user_access('administer nodes'); $form['xmlsitemap']['#access'] |= user_access('administer nodes');
$form['xmlsitemap']['#weight'] = 30; $form['xmlsitemap']['#weight'] = 30;
if (user_access('administer content types')) { if (user_access('administer content types')) {
$form['xmlsitemap']['#description'] = t('The default XML sitemap settings for this content type can be changed <a href="@link-type">here</a>.', array('@link-type' => url('admin/content/node-type/' . $node->type, array('query' => drupal_get_destination())))); $form['xmlsitemap']['#description'] = t('The default XML sitemap settings for this content type can be changed <a href="@link-type">here</a>.', array('@link-type' => url('admin/structure/types/manage/' . str_replace('_', '-', $node->type), array('query' => drupal_get_destination()))));
} }
//// The following values should not exist for new nodes.
//if ($node->nid) {
// $form['xmlsitemap']['lastmod'] = array(
// '#type' => 'value',
// '#value' => $node->xmlsitemap['lastmod'],
// );
// $form['xmlsitemap']['changefreq'] = array(
// '#type' => 'value',
// '#value' => $node->xmlsitemap['changefreq'],
// );
// $form['xmlsitemap']['changecount'] = array(
// '#type' => 'value',
// '#value' => $node->xmlsitemap['changecount'],
// );
//}
} }
} }
...@@ -203,9 +189,9 @@ function xmlsitemap_node_xmlsitemap_links($offset = 0, $limit = 0) { ...@@ -203,9 +189,9 @@ function xmlsitemap_node_xmlsitemap_links($offset = 0, $limit = 0) {
$sql = "SELECT n.nid FROM {node} n WHERE n.nid > :offset AND n.type IN (:types) ORDER BY n.nid"; $sql = "SELECT n.nid FROM {node} n WHERE n.nid > :offset AND n.type IN (:types) ORDER BY n.nid";
$args = array(':offset' => $offset, ':types' => $types); $args = array(':offset' => $offset, ':types' => $types);
$nids = ($limit ? db_query_range($sql, 0, $limit, $args) : db_query($sql, $args)); $nids = ($limit ? db_query_range($sql, 0, $limit, $args) : db_query($sql, $args));
$nodes = node_load_multiple($nids);
foreach ($nids as $nid) { foreach ($nodes as $node) {
$node = node_load($nid, NULL, TRUE);
$links[] = xmlsitemap_node_create_link($node); $links[] = xmlsitemap_node_create_link($node);
} }
} }
...@@ -252,16 +238,17 @@ function xmlsitemap_node_get_types() { ...@@ -252,16 +238,17 @@ function xmlsitemap_node_get_types() {
} }
/** /**
* Implements hook_content_extra_fields(). * Implements hook_field_extra_fields().
*
* This is so that the XML sitemap fieldset is sortable on the node edit form.
*/ */
function xmlsitemap_node_content_extra_fields() { function xmlsitemap_node_field_extra_fields() {
$extras['xmlsitemap'] = array( $extras = array();
'label' => t('XML sitemap'), foreach (xmlsitemap_node_get_types() as $type) {
'description' => t('XML sitemap module form.'), $extras['node'][$type]['xmlsitemap'] = array(
'weight' => 30, 'label' => t('XML sitemap'),
); 'description' => t('XML sitemap module element'),
'weight' => 30,
);
}
return $extras; return $extras;
} }
...@@ -277,7 +264,7 @@ function xmlsitemap_node_get_timestamps($node) { ...@@ -277,7 +264,7 @@ function xmlsitemap_node_get_timestamps($node) {
static $timestamps = array(); static $timestamps = array();
if (!isset($timestamps[$node->nid])) { if (!isset($timestamps[$node->nid])) {
$timestamps[$node->nid] = db_query("SELECT c.timestamp FROM {comments} c WHERE c.nid = %d AND c.status = %d UNION ALL SELECT nr.timestamp FROM {node_revisions} nr WHERE nr.nid = %d", $node->nid, COMMENT_PUBLISHED, $node->nid)->fetchCol(); $timestamps[$node->nid] = db_query("SELECT c.created FROM {comment} c WHERE c.nid = :nid AND c.status = :status UNION ALL SELECT nr.timestamp FROM {node_revision} nr WHERE nr.nid = :nid2", array(':nid' => $node->nid, ':status' => COMMENT_PUBLISHED, ':nid2' => $node->nid))->fetchCol();
} }
return $timestamps[$node->nid]; return $timestamps[$node->nid];
...@@ -292,8 +279,11 @@ function xmlsitemap_node_get_timestamps($node) { ...@@ -292,8 +279,11 @@ function xmlsitemap_node_get_timestamps($node) {
* A node object. * A node object.
*/ */
function xmlsitemap_node_create_link(&$node) { function xmlsitemap_node_create_link(&$node) {
if (!isset($node->xmlsitemap)) { if (!isset($node->xmlsitemap) || !is_array($node->xmlsitemap)) {
$node->xmlsitemap = array(); $node->xmlsitemap = array();
if ($node->nid && $link = xmlsitemap_load_link(array('type' => 'node', 'id' => $node->nid))) {
$node->xmlsitemap = $link;
}
} }
$node->xmlsitemap += array( $node->xmlsitemap += array(
...@@ -307,10 +297,13 @@ function xmlsitemap_node_create_link(&$node) { ...@@ -307,10 +297,13 @@ function xmlsitemap_node_create_link(&$node) {
'priority' => variable_get('xmlsitemap_node_priority_' . $node->type, 0.5), 'priority' => variable_get('xmlsitemap_node_priority_' . $node->type, 0.5),
'priority_default' => variable_get('xmlsitemap_node_priority_' . $node->type, 0.5), 'priority_default' => variable_get('xmlsitemap_node_priority_' . $node->type, 0.5),
'priority_override' => 0, 'priority_override' => 0,
'changefreq' => $node->nid ? xmlsitemap_calculate_changefreq(xmlsitemap_node_get_timestamps($node)) : 0,
'changecount' => $node->nid ? count(xmlsitemap_node_get_timestamps($node)) - 1 : 0,
); );
// Always recalculate changefreq and changecount.
$timestamps = xmlsitemap_node_get_timestamps($node);
$node->xmlsitemap['changefreq'] = $node->nid ? xmlsitemap_calculate_changefreq($timestamps) : 0;
$node->xmlsitemap['changecount'] = $node->nid ? count($timestamps) - 1 : 0;
// The following values must always be checked because they are volatile. // The following values must always be checked because they are volatile.
$node->xmlsitemap['lastmod'] = isset($node->changed) ? $node->changed : REQUEST_TIME; $node->xmlsitemap['lastmod'] = isset($node->changed) ? $node->changed : REQUEST_TIME;
$node->xmlsitemap['access'] = $node->nid ? (bool) node_access('view', $node, drupal_anonymous_user()) : 1; $node->xmlsitemap['access'] = $node->nid ? (bool) node_access('view', $node, drupal_anonymous_user()) : 1;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment