xmlsitemap_node.module 11.4 KB
Newer Older
Darren Oh's avatar
Darren Oh committed
1 2 3 4
<?php
// $Id$

/**
5 6 7
 * Implements hook_cron().
 *
 * Process old nodes not found in the {xmlsitemap} table.
Darren Oh's avatar
Darren Oh committed
8
 */
9 10
function xmlsitemap_node_cron() {
  // Start with the most recently changed nodes first.
11 12
  $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 ORDER BY n.changed DESC", 0, xmlsitemap_var('batch_limit'));
  foreach ($nids as $nid) {
13 14 15 16 17
    $node = node_load($nid, NULL, TRUE);
    $link = xmlsitemap_node_create_link($node);
    xmlsitemap_save_link($link);
  }
}
Darren Oh's avatar
Darren Oh committed
18 19

/**
20
 * Implements hook_nodeapi().
Darren Oh's avatar
Darren Oh committed
21
 */
22 23 24 25 26
function xmlsitemap_node_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'load':
      if ($data = xmlsitemap_load_link(array('type' => 'node', 'id' => $node->nid))) {
        return array('xmlsitemap' => $data);
Darren Oh's avatar
Darren Oh committed
27
      }
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
      break;
    case 'insert':
      $link = xmlsitemap_node_create_link($node);
      xmlsitemap_save_link($link);
      break;
    case 'update':
      $link = xmlsitemap_node_create_link($node);
      if ($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;
Darren Oh's avatar
Darren Oh committed
44 45 46 47
  }
}

/**
48
 * Implements hook_comment().
Darren Oh's avatar
Darren Oh committed
49
 */
50 51 52 53 54 55 56 57 58 59 60 61
function xmlsitemap_node_comment($comment, $op) {
  switch ($op) {
    case 'delete':
    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);
Darren Oh's avatar
Darren Oh committed
62
      }
63
      break;
Darren Oh's avatar
Darren Oh committed
64 65 66 67
  }
}

/**
68
 * Implements hook_node_type().
Darren Oh's avatar
Darren Oh committed
69
 */
70 71 72 73 74
function xmlsitemap_node_node_type($op, $info) {
  if ($op == 'delete') {
    variable_del('xmlsitemap_node_status_' . $info->type);
    variable_del('xmlsitemap_node_priority_' . $info->type);
    //xmlsitemap_delete_link(array('type' => 'node', 'subtype' => $info->type));
Darren Oh's avatar
Darren Oh committed
75 76 77 78
  }
}

/**
79 80 81
 * Implements hook_form_FORM_ID_alter().
 *
 * Show a summary of content types on the XML sitemap settings page.
Darren Oh's avatar
Darren Oh committed
82
 */
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
function xmlsitemap_node_form_xmlsitemap_settings_form_alter(&$form, $form_state) {
  $type = array(
    'type' => 'node',
    'title' => t('Content types'),
    'item_title' => t('Content type'),
    'access' => user_access('administer content types'),
  );

  $node_types = node_get_types('names');
  foreach ($node_types as $node_type => $node_type_name) {
    $node_types[$node_type] = array(
      'name' => drupal_ucfirst($node_type_name),
      'link' => 'admin/content/node-type/' . str_replace('_', '-', $node_type),
      'status' => variable_get('xmlsitemap_node_status_' . $node_type, 0),
      'priority' => variable_get('xmlsitemap_node_priority_' . $node_type, 0.5),
    );
Darren Oh's avatar
Darren Oh committed
99
  }
100 101 102

  xmlsitemap_add_form_type_summary($form, $type, $node_types);
  $form['node']['#weight'] = 30;
Darren Oh's avatar
Darren Oh committed
103 104 105
}

/**
106 107 108 109
 * Implements hook_form_FORM_ID_alter().
 *
 * @see node_type_form()
 * @see xmlsitemap_add_form_type_options()
Darren Oh's avatar
Darren Oh committed
110
 */
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
function xmlsitemap_node_form_node_type_form_alter(&$form, $form_state) {
  $node_type = isset($form['#node_type']->type) ? $form['#node_type']->type : '';

  module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
  $options = array(
    'status' => variable_get('xmlsitemap_node_status_' . $node_type, 0),
    'priority' => variable_get('xmlsitemap_node_priority_' . $node_type, 0.5),
  );
  xmlsitemap_add_form_type_options($form, 'node', $options);
  $form['xmlsitemap']['#attached']['js']['vertical-tabs'] = drupal_get_path('module', 'xmlsitemap_node') . '/xmlsitemap_node.js';

  // Add our submit handler before node_type_form_submit() so we can compare
  // the old and new values.
  array_unshift($form['#submit'], 'xmlsitemap_node_type_form_submit');
}

function xmlsitemap_node_type_form_submit($form, $form_state) {
  $node_type = $form_state['values']['old_type'];
  $new_status = $form_state['values']['xmlsitemap_node_status'];
  $new_priority = $form_state['values']['xmlsitemap_node_priority'];
  $new_type = $form_state['values']['type'];

  if ($new_status != variable_get('xmlsitemap_node_status_' . $node_type, 0)) {
    xmlsitemap_update_links(array('status' => $new_status), array('type' => 'node', 'subtype' => $node_type, 'status_override' => 0));
Darren Oh's avatar
Darren Oh committed
135
  }
136 137 138 139
  if ($new_priority != variable_get('xmlsitemap_node_priority_' . $node_type, 0.5)) {
    xmlsitemap_update_links(array('priority' => $new_priority), array('type' => 'node', 'subtype' => $node_type, 'priority_override' => 0));
  }
<<<<<<< xmlsitemap_node.module
140 141 142 143 144
  if (isset($node->priority_override)) {
    $priority = $node->priority_override;
  }
  else {
    $priority = variable_get("xmlsitemap_node_type_priority_$node->type", 0.5);
Darren Oh's avatar
Darren Oh committed
145 146 147 148 149 150 151 152
    if ($node->promote) {
      $priority += $promote_priority;
    }
    if (!empty($maxcomments)) {
      $priority += $node->comment_count / $maxcomments * $comment_priority;
    }
    $priority = round($priority, 1);
    $priority = min($priority, 0.9);
153 154 155 156
=======
  if ($node_type != $new_type) {
    xmlsitemap_update_links(array('subtype' => $new_type), array('type' => 'node', 'subtype' => $node_type));
>>>>>>> 1.19.2.20.2.73
Darren Oh's avatar
Darren Oh committed
157
  }
Alberto Paderno's avatar
Alberto Paderno committed
158
}
Darren Oh's avatar
Darren Oh committed
159 160

/**
161 162 163 164 165
 * Implements hook_form_alter().
 *
 * Add the XML sitemap individual link options for a node.
 *
 * @see xmlsitemap_add_form_link_options()
Darren Oh's avatar
Darren Oh committed
166
 */
167 168 169 170 171 172 173 174
function xmlsitemap_node_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
    $node = drupal_clone($form['#node']);
    if (!isset($node->nid)) {
      // Handle new nodes that do not have a value for nid yet.
      $node->nid = NULL;
    }
    $link = xmlsitemap_node_create_link($node);
Darren Oh's avatar
Darren Oh committed
175

176 177 178
    // Add the link options.
    module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
    xmlsitemap_add_form_link_options($form, $link);
Darren Oh's avatar
Darren Oh committed
179

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    $form['xmlsitemap']['#access'] |= user_access('administer nodes');
    $form['xmlsitemap']['#weight'] = 30;
    if (user_access('administer content types')) {
      $form['xmlsitemap']['priority']['#description'] .= ' ' . t('The default priority 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()))));
    }

    //// 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'],
    //  );
    //}
Darren Oh's avatar
Darren Oh committed
201 202 203 204
  }
}

/**
205
 * Implements hook_xmlsitemap_links().
Darren Oh's avatar
Darren Oh committed
206
 */
207 208 209 210
function xmlsitemap_node_xmlsitemap_links($offset = 0, $limit = 0) {
  $links = array();

  $sql = "SELECT n.nid FROM {node} n WHERE n.nid > %d ORDER BY n.nid";
211
  $nids = ($limit ? db_query_range($sql, $offset, 0, $limit) : db_query($sql, $offset));
212

213
  foreach ($nids as $nid) {
214 215
    $node = node_load($nid, NULL, TRUE);
    $links[] = xmlsitemap_node_create_link($node);
Darren Oh's avatar
Darren Oh committed
216
  }
217 218

  return $links;
Darren Oh's avatar
Darren Oh committed
219 220 221
}

/**
222
 * Implements hook_xmlsitemap_links_batch_info().
Darren Oh's avatar
Darren Oh committed
223
 */
224 225
function xmlsitemap_node_xmlsitemap_links_batch_info() {
  return array(
226
    'max' => db_query("SELECT COUNT(n.nid) FROM {node} n")->fetchField(),
227
  );
Darren Oh's avatar
Darren Oh committed
228 229 230
}

/**
231
 * Implements hook_xmlsitemap_link_info().
Darren Oh's avatar
Darren Oh committed
232
 */
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
function xmlsitemap_node_xmlsitemap_link_info() {
  return array(
    'node' => array(
      'purge' => TRUE,
      'table' => 'node',
      'id' => 'nid',
      'subtype' => 'type',
      'subtypes' => xmlsitemap_node_get_types(),
    ),
  );
}

/**
 * Fetch an array of node types to be included in the sitemap.
 */
function xmlsitemap_node_get_types() {
  $node_types = array_keys(node_get_types('names'));
  foreach ($node_types as $index => $node_type) {
    if (!variable_get('xmlsitemap_node_status_' . $node_type, 0)) {
      unset($node_types[$index]);
    }
Darren Oh's avatar
Darren Oh committed
254
  }
255
  return $node_types;
Darren Oh's avatar
Darren Oh committed
256
}
257 258
<<<<<<< xmlsitemap_node.module
=======
Darren Oh's avatar
Darren Oh committed
259

260
/**
261 262 263
 * Implements hook_content_extra_fields().
 *
 * This is so that the XML sitemap fieldset is sortable on the node edit form.
264
 */
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
function xmlsitemap_node_content_extra_fields() {
  $extras['xmlsitemap'] = array(
    'label' => t('XML sitemap'),
    'description' => t('XML sitemap module form.'),
    'weight' => 30,
  );
  return $extras;
}

/**
 * Fetch all the timestamps for when a node was changed.
 *
 * @param $node
 *   A node object.
 * @return
 *   An array of UNIX timestamp integers.
 */
function xmlsitemap_node_get_timestamps($node) {
  static $timestamps = array();

  if (!isset($timestamps[$node->nid])) {
286
    $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();
287
  }
288 289

  return $timestamps[$node->nid];
290
}
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

/**
 * Create a sitemap link from a node.
 *
 * The link will be saved as $node->xmlsitemap.
 *
 * @param $node
 *   A node object.
 */
function xmlsitemap_node_create_link(&$node) {
  if (!isset($node->xmlsitemap)) {
    $node->xmlsitemap = array();
  }

  $node->xmlsitemap += array(
    'type' => 'node',
    'id' => $node->nid,
    'subtype' => $node->type,
    'loc' => 'node/'. $node->nid,
    'status' => variable_get('xmlsitemap_node_status_' . $node->type, 0),
    'status_default' => variable_get('xmlsitemap_node_status_' . $node->type, 0),
    'status_override' => 0,
    'priority' => variable_get('xmlsitemap_node_priority_' . $node->type, 0.5),
    'priority_default' => variable_get('xmlsitemap_node_priority_' . $node->type, 0.5),
    '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,
  );

  // The following values must always be checked because they are volatile.
  $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['language'] = isset($node->language) ? $node->language : '';

  return $node->xmlsitemap;
}

/**
 * Internal default variables for xmlsitemap_node_var().
 */
function xmlsitemap_node_variables() {
  $defaults = array();
  $node_types = array_keys(node_get_types('names'));
  foreach ($node_types as $node_type) {
    $defaults['xmlsitemap_node_priority_' . $node_type] = 0.5;
    $defaults['xmlsitemap_node_status_' . $node_type] = 0;
    // @todo Remove the variable.
    $defaults['xmlsitemap_node_update_' . $node_type] = NULL;
  }
  return $defaults;
}

///**
// * Internal implementation of variable_get().
// */
//function xmlsitemap_node_var($name, $default = NULL) {
//  static $defaults;
//  if (!isset($defaults)) {
//    $defaults = xmlsitemap_node_variables();
//  }
//
//  $name = 'xmlsitemap_node_'. $name;
//
//  // @todo Remove when stable.
//  if (!isset($defaults[$name])) {
//    trigger_error(t('Default variable for %variable not found.', array('%variable' => $name)));
//  }
//
//  return variable_get($name, isset($default) || !isset($defaults[$name]) ? $default : $defaults[$name]);
//}
>>>>>>> 1.19.2.20.2.73