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

4
/**
5
 * Implements hook_entity_info_alter().
6
 */
7
function xmlsitemap_node_entity_info_alter(&$entity_info) {
8 9
  $entity_info['node']['label'] = t('Content');
  $entity_info['node']['bundle label'] = t('Content type');
10 11 12
  $entity_info['node']['xmlsitemap'] = array(
    'process callback' => 'xmlsitemap_node_xmlsitemap_process_node_links',
  );
13 14
}

Darren Oh's avatar
Darren Oh committed
15
/**
16 17 18
 * Implements hook_cron().
 *
 * Process old nodes not found in the {xmlsitemap} table.
Darren Oh's avatar
Darren Oh committed
19
 */
20
function xmlsitemap_node_cron() {
21 22 23 24 25 26 27
  xmlsitemap_node_xmlsitemap_index_links(xmlsitemap_var('batch_limit'));
}

/**
 * Implements hook_xmlsitemap_index_links().
 */
function xmlsitemap_node_xmlsitemap_index_links($limit) {
28
  if ($types = xmlsitemap_get_link_type_enabled_bundles('node')) {
29
    $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();
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    xmlsitemap_node_xmlsitemap_process_node_links($nids);
  }
}

/**
 * Process node sitemap links.
 *
 * @param $nids
 *   An array of node IDs.
 */
function xmlsitemap_node_xmlsitemap_process_node_links(array $nids) {
  $nodes = node_load_multiple($nids);
  foreach ($nodes as $node) {
    $link = xmlsitemap_node_create_link($node);
    xmlsitemap_save_link($link);
45 46
  }
}
Darren Oh's avatar
Darren Oh committed
47 48

/**
49
 * Implements hook_node_insert().
Darren Oh's avatar
Darren Oh committed
50
 */
51 52
function xmlsitemap_node_node_insert($node) {
  xmlsitemap_node_node_update($node);
Darren Oh's avatar
Darren Oh committed
53 54 55
}

/**
56
 * Implements hook_node_update().
Darren Oh's avatar
Darren Oh committed
57
 */
58 59 60
function xmlsitemap_node_node_update($node) {
  $link = xmlsitemap_node_create_link($node);
  xmlsitemap_save_link($link);
Darren Oh's avatar
Darren Oh committed
61 62 63
}

/**
64
 * Implements hook_node_delete().
Darren Oh's avatar
Darren Oh committed
65
 */
66
function xmlsitemap_node_node_delete($node) {
67
  xmlsitemap_link_delete('node', $node->nid);
68 69 70 71 72 73 74 75
}

/**
 * Implements hook_comment_update().
 */
function xmlsitemap_node_comment_update(stdClass $comment) {
  if ($node = node_load($comment->nid, NULL, TRUE)) {
    xmlsitemap_node_node_update($node);
Darren Oh's avatar
Darren Oh committed
76 77 78
  }
}

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
/**
 * 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);
}

100 101 102 103 104 105 106
/**
 * Implements_hook_node_type_update().
 */
function xmlsitemap_node_node_type_update(stdClass $info) {
  // Cannot perform xmlsitemap_link_bundle_settings_save() here since
  // node_type_form_submit() strips all non-essential data from $info.
  if (!empty($info->old_type) && $info->old_type != $info->type) {
107
    xmlsitemap_link_bundle_rename('node', $info->old_type, $info->type);
108 109 110
  }
}

111 112 113
/**
 * Implements hook_node_type_delete().
 */
114 115
function xmlsitemap_node_node_type_delete(stdClass $info) {
  xmlsitemap_link_bundle_delete('node', $info->type);
116 117
}

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
/**
 * Implements hook_field_extra_fields().
 */
function xmlsitemap_node_field_extra_fields() {
  $extras = array();
  $bundles = xmlsitemap_get_link_type_enabled_bundles('node');
  foreach (node_type_get_names() as $type => $name) {
    if (!in_array($type, $bundles)) {
      continue;
    }
    $extras['node'][$type]['xmlsitemap'] = array(
      'label' => t('XML sitemap'),
      'description' => t('XML sitemap module element'),
      'weight' => 30,
    );
  }
  return $extras;
}

Darren Oh's avatar
Darren Oh committed
137
/**
138 139 140
 * Implements hook_form_FORM_ID_alter().
 *
 * @see node_type_form()
141
 * @see xmlsitemap_add_link_bundle_settings()
Darren Oh's avatar
Darren Oh committed
142
 */
143 144 145 146
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');
147
  xmlsitemap_add_link_bundle_settings($form, $form_state, 'node', $node_type);
148 149 150 151 152 153

  // 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');
}

154
function xmlsitemap_node_type_form_submit($form, &$form_state) {
155
  $node_type = $form_state['values']['old_type'];
156
  xmlsitemap_link_bundle_settings_save('node', $node_type, $form_state['values']['xmlsitemap']);
157

158 159
  // Do not allow node_type_form_submit() to run variable_set() on this.
  unset($form_state['values']['xmlsitemap']);
Alberto Paderno's avatar
Alberto Paderno committed
160
}
Darren Oh's avatar
Darren Oh committed
161 162

/**
163 164 165 166 167
 * 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
168
 */
169
function xmlsitemap_node_form_alter(&$form, $form_state, $form_id) {
170
  if (!empty($form['#node_edit_form'])) {
171 172
    // Add the link options.
    module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
173
    xmlsitemap_add_form_link_options($form, 'node', $form['type']['#value'], $form['nid']['#value']);
174 175
    $form['xmlsitemap']['#access'] |= user_access('administer nodes');
    $form['xmlsitemap']['#weight'] = 30;
Darren Oh's avatar
Darren Oh committed
176 177 178
  }
}

179 180 181 182 183 184 185 186 187 188 189 190
/**
 * 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])) {
191
    $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();
192
  }
193 194

  return $timestamps[$node->nid];
195
}
196 197 198 199 200 201 202 203 204

/**
 * Create a sitemap link from a node.
 *
 * The link will be saved as $node->xmlsitemap.
 *
 * @param $node
 *   A node object.
 */
205
function xmlsitemap_node_create_link(stdClass &$node) {
206
  if (!isset($node->xmlsitemap) || !is_array($node->xmlsitemap)) {
207
    $node->xmlsitemap = array();
208
    if ($node->nid && $link = xmlsitemap_link_load('node', $node->nid)) {
209 210
      $node->xmlsitemap = $link;
    }
211 212
  }

213
  $settings = xmlsitemap_link_bundle_load('node', $node->type);
214

215 216 217 218 219
  $node->xmlsitemap += array(
    'type' => 'node',
    'id' => $node->nid,
    'subtype' => $node->type,
    'loc' => 'node/'. $node->nid,
220 221
    'status' => $settings['status'],
    'status_default' => $settings['status'],
222
    'status_override' => 0,
223 224
    'priority' => $settings['priority'],
    'priority_default' => $settings['priority'],
225 226 227
    'priority_override' => 0,
  );

228 229 230 231 232
  // 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;

233 234 235 236 237
  // Node access must be reset since it a user may have changed published status, etc.
  $access = &drupal_static('node_access');
  unset($access[0][$node->nid]);
  //node_access_acquire_grants($node);

238 239
  // The following values must always be checked because they are volatile.
  $node->xmlsitemap['lastmod'] = isset($node->changed) ? $node->changed : REQUEST_TIME;
240
  $node->xmlsitemap['access'] = $node->nid ? xmlsitemap_node_view_access($node, drupal_anonymous_user()) : 1;
241
  $node->xmlsitemap['language'] = isset($node->language) ? $node->language : LANGUAGE_NONE;
242 243 244

  return $node->xmlsitemap;
}
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 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 362 363 364 365 366 367 368 369

/**
 * Determine whether a user may view the specified node.
 *
 * @param $node
 *   The node object on which the operation is to be performed, or node type
 *   (e.g. 'forum') for "create" operation.
 * @param $account
 *   Optional, a user object representing the user for whom the operation is to
 *   be performed. Determines access for a user other than the current user.
 * @return
 *   TRUE if the operation may be performed, FALSE otherwise.
 *
 * This is for all intesive purposes a copy of Drupal 7's node_access() function.
 * It invokes a backport of Drupal 7's hook_node_grants_alter() specifically
 * for use with XML sitemap.
 */
function xmlsitemap_node_view_access($node, $account = NULL) {
  global $user;

  $op = 'view';
  $rights = &drupal_static(__FUNCTION__, array());

  if (!$node || !in_array($op, array('view', 'update', 'delete', 'create'), TRUE)) {
    // If there was no node to check against, or the $op was not one of the
    // supported ones, we return access denied.
    return FALSE;
  }
  // If no user object is supplied, the access check is for the current user.
  if (empty($account)) {
    $account = $user;
  }

  // $node may be either an object or a node type. Since node types cannot be
  // an integer, use either nid or type as the static cache id.
  //$cid = is_object($node) ? $node->nid : $node;

  // If we've already checked access for this node, user and op, return from
  // cache.
  if (isset($rights[$account->uid][$node->nid])) {
    return $rights[$account->uid][$node->nid];
  }

  if (user_access('bypass node access', $account)) {
    $rights[$account->uid][$node->nid] = TRUE;
    return TRUE;
  }

  if (!user_access('access content', $account)) {
    $rights[$account->uid][$node->nid] = FALSE;
    return FALSE;
  }

  // We grant access to the node if both of the following conditions are met:
  // - No modules say to deny access.
  // - At least one module says to grant access.
  // If no module specified either allow or deny, we fall back to the
  // node_access table.
  $access = module_invoke_all('node_access', $node, $op, $account);
  if (in_array(NODE_ACCESS_DENY, $access, TRUE)) {
    $rights[$account->uid][$node->nid] = FALSE;
    return FALSE;
  }
  elseif (in_array(NODE_ACCESS_ALLOW, $access, TRUE)) {
    $rights[$account->uid][$node->nid] = TRUE;
    return TRUE;
  }

  // Check if authors can view their own unpublished nodes.
  if ($op == 'view' && !$node->status && user_access('view own unpublished content', $account) && $account->uid == $node->uid && $account->uid != 0) {
    $rights[$account->uid][$node->nid] = TRUE;
    return TRUE;
  }

  // If the module did not override the access rights, use those set in the
  // node_access table.
  if ($op != 'create' && $node->nid) {
    if (module_implements('node_grants')) {
      $query = db_select('node_access');
      $query->addExpression('1');
      $query->condition('grant_' . $op, 1, '>=');
      $nids = db_or()->condition('nid', $node->nid);
      if ($node->status) {
        $nids->condition('nid', 0);
      }
      $query->condition($nids);
      $query->range(0, 1);

      // Fetch the node grants and allow other modules to alter them (D7 backport).
      $grants = &drupal_static(__FUNCTION__ . ':grants', array());
      if (!isset($grants[$account->uid][$op])) {
        // Indicate that this is our special function in the grants.
        $account->xmlsitemap_node_access = TRUE;
        $grants[$account->uid][$op] = node_access_grants($op, $account);
        // Remove the special indicator.
        unset($account->xmlsitemap_node_access);
      }

      $grant_condition = db_or();
      foreach ($grants[$account->uid][$op] as $realm => $gids) {
        foreach ($gids as $gid) {
          $grant_condition->condition(db_and()
            ->condition('gid', $gid)
            ->condition('realm', $realm)
          );
        }
      }
      if (count($grant_condition) > 0) {
        $query->condition($grant_condition);
      }

      $result = (bool) $query->execute()->fetchField();
      $rights[$account->uid][$node->nid] = $result;
      return $result;
    }
    elseif (is_object($node) && $op == 'view' && $node->status) {
      // If no modules implement hook_node_grants(), the default behaviour is to
      // allow all users to view published nodes, so reflect that here.
      $rights[$account->uid][$node->nid] = TRUE;
      return TRUE;
    }
  }

  return FALSE;
}