xmlsitemap_node.module 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /**
  3. * @file
  4. * Default file for XML sitemap node.
  5. */
  6. /**
  7. * Implements hook_entity_info_alter().
  8. */
  9. function xmlsitemap_node_entity_info_alter(array &$entity_info) {
  10. $entity_info['node']['label'] = t('Content');
  11. $entity_info['node']['bundle label'] = t('Content type');
  12. $entity_info['node']['xmlsitemap'] = array(
  13. 'process callback' => 'xmlsitemap_node_xmlsitemap_process_node_links',
  14. );
  15. }
  16. /**
  17. * Implements hook_cron().
  18. *
  19. * Process old nodes not found in the {xmlsitemap} table.
  20. */
  21. function xmlsitemap_node_cron() {
  22. $limit = xmlsitemap_var('batch_limit');
  23. // Process nodes that have been queued in hook_node_update().
  24. $queue = DrupalQueue::get('xmlsitemap_node');
  25. while ($limit > 0 && $item = $queue->claimItem()) {
  26. $limit--;
  27. try {
  28. $node = node_load($item->data);
  29. // The node could have been deleted in the meantime, skip XML sitemap
  30. // updates in this case.
  31. if ($node) {
  32. $link = xmlsitemap_node_create_link($node);
  33. xmlsitemap_link_save($link, array($link['type'] => $node));
  34. }
  35. $queue->deleteItem($item);
  36. }
  37. catch (Exception $e) {
  38. // In case of exception log it and leave the item in the queue
  39. // to be processed again later.
  40. watchdog_exception('xmlsitemap_node', $e);
  41. }
  42. }
  43. // Add nodes that are missing from the {xmlsitemap} table.
  44. // This catches nodes that were created prior to this module being enabled.
  45. xmlsitemap_node_xmlsitemap_index_links($limit);
  46. }
  47. /**
  48. * Implements hook_xmlsitemap_index_links().
  49. */
  50. function xmlsitemap_node_xmlsitemap_index_links($limit) {
  51. if ($types = xmlsitemap_get_link_type_enabled_bundles('node')) {
  52. $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();
  53. xmlsitemap_node_xmlsitemap_process_node_links($nids);
  54. }
  55. }
  56. /**
  57. * Process node sitemap links.
  58. *
  59. * @param array $nids
  60. * An array of node IDs.
  61. */
  62. function xmlsitemap_node_xmlsitemap_process_node_links(array $nids) {
  63. // Load no more than 15 nodes at a time.
  64. if (count($nids) >= 1) {
  65. $nids_chunks = array_chunk($nids, 15);
  66. foreach ($nids_chunks as $chunk) {
  67. $nodes = node_load_multiple($chunk);
  68. foreach ($nodes as $node) {
  69. $link = xmlsitemap_node_create_link($node);
  70. xmlsitemap_link_save($link, array($link['type'] => $node));
  71. }
  72. // Flush each entity from the load cache after processing, to avoid
  73. // exceeding PHP memory limits if $nids is large.
  74. entity_get_controller('node')->resetCache($chunk);
  75. }
  76. }
  77. }
  78. /**
  79. * Implements hook_node_insert().
  80. */
  81. function xmlsitemap_node_node_insert(stdClass $node) {
  82. xmlsitemap_node_node_update($node);
  83. }
  84. /**
  85. * Implements hook_node_update().
  86. */
  87. function xmlsitemap_node_node_update(stdClass $node) {
  88. // Save a sitemap link with revoked access until the node permissions are
  89. // checked in the cron.
  90. $link = xmlsitemap_node_create_link($node);
  91. xmlsitemap_link_presave($link, array($link['type'] => $node));
  92. // Node access can not be accurately determined in hook_node_update() because
  93. // node grants have not yet been written to the table, so we defer checking
  94. // node access permissions and process the sitemap link during cron.
  95. $queue = DrupalQueue::get('xmlsitemap_node');
  96. $queue->createItem($node->nid);
  97. }
  98. /**
  99. * Implements hook_node_delete().
  100. */
  101. function xmlsitemap_node_node_delete(stdClass $node) {
  102. xmlsitemap_link_delete('node', $node->nid);
  103. }
  104. /**
  105. * Implements hook_comment_update().
  106. */
  107. function xmlsitemap_node_comment_update(stdClass $comment) {
  108. if ($node = entity_load_unchanged('node', $comment->nid)) {
  109. xmlsitemap_node_node_update($node);
  110. }
  111. }
  112. /**
  113. * Implements hook_comment_publish().
  114. */
  115. function xmlsitemap_node_comment_publish(stdClass $comment) {
  116. xmlsitemap_node_comment_update($comment);
  117. }
  118. /**
  119. * Implements hook_comment_unpublish().
  120. */
  121. function xmlsitemap_node_comment_unpublish(stdClass $comment) {
  122. xmlsitemap_node_comment_update($comment);
  123. }
  124. /**
  125. * Implements hook_comment_delete().
  126. */
  127. function xmlsitemap_node_comment_delete(stdClass $comment) {
  128. xmlsitemap_node_comment_update($comment);
  129. }
  130. /**
  131. * Implements hook_field_extra_fields().
  132. */
  133. function xmlsitemap_node_field_extra_fields() {
  134. $extras = array();
  135. foreach (node_type_get_names() as $type => $name) {
  136. $extras['node'][$type]['form']['xmlsitemap'] = array(
  137. 'label' => t('XML sitemap'),
  138. 'description' => t('XML sitemap module element'),
  139. 'weight' => 30,
  140. );
  141. }
  142. return $extras;
  143. }
  144. /**
  145. * Implements hook_form_FORM_ID_alter().
  146. *
  147. * @see node_type_form()
  148. * @see xmlsitemap_add_link_bundle_settings()
  149. */
  150. function xmlsitemap_node_form_node_type_form_alter(array &$form, array $form_state) {
  151. $node_type = isset($form['#node_type']->type) ? $form['#node_type']->type : '';
  152. module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
  153. xmlsitemap_add_link_bundle_settings($form, $form_state, 'node', $node_type);
  154. }
  155. /**
  156. * Implements hook_form_alter().
  157. *
  158. * @codingStandardsIgnoreLine
  159. *
  160. * Add the XML sitemap individual link options for a node.
  161. *
  162. * @see xmlsitemap_add_form_link_options()
  163. */
  164. function xmlsitemap_node_form_node_form_alter(array &$form, array &$form_state) {
  165. // Add the link options.
  166. module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
  167. xmlsitemap_add_form_link_options($form, 'node', $form['type']['#value'], $form['nid']['#value']);
  168. $form['xmlsitemap']['#weight'] = 30;
  169. }
  170. /**
  171. * Fetch all the timestamps for when a node was changed.
  172. *
  173. * @param object $node
  174. * A node object.
  175. *
  176. * @return array
  177. * An array of UNIX timestamp integers.
  178. */
  179. function xmlsitemap_node_get_timestamps(stdClass $node) {
  180. $timestamps = &drupal_static(__FUNCTION__, array());
  181. if (!isset($timestamps[$node->nid])) {
  182. $timestamps[$node->nid] = db_query("SELECT nr.timestamp FROM {node_revision} nr WHERE nr.nid = :nid", array(':nid' => $node->nid))->fetchCol();
  183. if (module_exists('comment')) {
  184. $comment_timestamps = db_query("SELECT c.created FROM {comment} c WHERE c.nid = :nid AND c.status = :status", array(':nid' => $node->nid, ':status' => COMMENT_PUBLISHED))->fetchCol();
  185. $timestamps[$node->nid] = array_merge($timestamps[$node->nid], $comment_timestamps);
  186. }
  187. }
  188. return $timestamps[$node->nid];
  189. }
  190. /**
  191. * Create a sitemap link from a node.
  192. *
  193. * The link will be saved as $node->xmlsitemap.
  194. *
  195. * @param object $node
  196. * A node object.
  197. */
  198. function xmlsitemap_node_create_link(stdClass $node) {
  199. if (!isset($node->xmlsitemap) || !is_array($node->xmlsitemap)) {
  200. $node->xmlsitemap = array();
  201. if ($node->nid && $link = xmlsitemap_link_load('node', $node->nid)) {
  202. $node->xmlsitemap = $link;
  203. }
  204. }
  205. $settings = xmlsitemap_link_bundle_load('node', $node->type);
  206. $uri = entity_uri('node', $node);
  207. $node->xmlsitemap += array(
  208. 'type' => 'node',
  209. 'id' => $node->nid,
  210. 'subtype' => $node->type,
  211. 'status' => $settings['status'],
  212. 'status_default' => $settings['status'],
  213. 'status_override' => 0,
  214. 'priority' => $settings['priority'],
  215. 'priority_default' => $settings['priority'],
  216. 'priority_override' => 0,
  217. );
  218. // Always recalculate changefreq and changecount.
  219. $timestamps = xmlsitemap_node_get_timestamps($node);
  220. $node->xmlsitemap['changefreq'] = $node->nid ? xmlsitemap_calculate_changefreq($timestamps) : 0;
  221. $node->xmlsitemap['changecount'] = $node->nid ? count($timestamps) - 1 : 0;
  222. // The following values must always be checked because they are volatile.
  223. $node->xmlsitemap['loc'] = $uri['path'];
  224. $node->xmlsitemap['lastmod'] = count($timestamps) ? max($timestamps) : 0;
  225. $node->xmlsitemap['access'] = $node->nid ? xmlsitemap_node_view_access($node, drupal_anonymous_user()) : 1;
  226. $node->xmlsitemap['language'] = isset($node->language) ? $node->language : LANGUAGE_NONE;
  227. return $node->xmlsitemap;
  228. }
  229. /**
  230. * Determine whether a user may view the specified node.
  231. *
  232. * @param object $node
  233. * The node object on which the operation is to be performed, or node type
  234. * (e.g. 'forum') for "create" operation.
  235. * @param object $account
  236. * Optional, a user object representing the user for whom the operation is to
  237. * be performed. Determines access for a user other than the current user.
  238. *
  239. * @return bool
  240. * TRUE if the operation may be performed, FALSE otherwise.
  241. *
  242. * This is for all intesive purposes a copy of Drupal 7's node_access()
  243. * function.
  244. */
  245. function xmlsitemap_node_view_access($node, $account = NULL) {
  246. global $user;
  247. $op = 'view';
  248. $rights = &drupal_static(__FUNCTION__, array());
  249. if (!$node || !in_array($op, array('view', 'update', 'delete', 'create'), TRUE)) {
  250. // If there was no node to check against, or the $op was not one of the
  251. // supported ones, we return access denied.
  252. return FALSE;
  253. }
  254. // If no user object is supplied, the access check is for the current user.
  255. if (empty($account)) {
  256. $account = $user;
  257. }
  258. // $node may be either an object or a node type. Since node types cannot be
  259. // an integer, use either nid or type as the static cache id.
  260. // $cid = is_object($node) ? $node->nid : $node;
  261. // If we've already checked access for this node, user and op, return from
  262. // cache.
  263. if (isset($rights[$account->uid][$node->nid])) {
  264. return $rights[$account->uid][$node->nid];
  265. }
  266. if (user_access('bypass node access', $account)) {
  267. $rights[$account->uid][$node->nid] = TRUE;
  268. return TRUE;
  269. }
  270. if (!user_access('access content', $account)) {
  271. $rights[$account->uid][$node->nid] = FALSE;
  272. return FALSE;
  273. }
  274. // We grant access to the node if both of the following conditions are met:
  275. // - No modules say to deny access.
  276. // - At least one module says to grant access.
  277. // If no module specified either allow or deny, we fall back to the
  278. // node_access table.
  279. $access = module_invoke_all('node_access', $node, $op, $account);
  280. if (in_array(NODE_ACCESS_DENY, $access, TRUE)) {
  281. $rights[$account->uid][$node->nid] = FALSE;
  282. return FALSE;
  283. }
  284. elseif (in_array(NODE_ACCESS_ALLOW, $access, TRUE)) {
  285. $rights[$account->uid][$node->nid] = TRUE;
  286. return TRUE;
  287. }
  288. // Check if authors can view their own unpublished nodes.
  289. if ($op == 'view' && !$node->status && user_access('view own unpublished content', $account) && $account->uid == $node->uid && $account->uid != 0) {
  290. $rights[$account->uid][$node->nid] = TRUE;
  291. return TRUE;
  292. }
  293. // If the module did not override the access rights, use those set in the
  294. // node_access table.
  295. if ($op != 'create' && $node->nid) {
  296. if (module_implements('node_grants')) {
  297. $query = db_select('node_access');
  298. $query->addExpression('1');
  299. $query->condition('grant_' . $op, 1, '>=');
  300. $nids = db_or()->condition('nid', $node->nid);
  301. if ($node->status) {
  302. $nids->condition('nid', 0);
  303. }
  304. $query->condition($nids);
  305. $query->range(0, 1);
  306. // Fetch the node grants and allow other modules to alter them
  307. // (D7 backport).
  308. $grants = &drupal_static(__FUNCTION__ . ':grants', array());
  309. if (!isset($grants[$account->uid][$op])) {
  310. // Indicate that this is our special function in the grants.
  311. $account->xmlsitemap_node_access = TRUE;
  312. $grants[$account->uid][$op] = node_access_grants($op, $account);
  313. // Remove the special indicator.
  314. unset($account->xmlsitemap_node_access);
  315. }
  316. $grant_condition = db_or();
  317. foreach ($grants[$account->uid][$op] as $realm => $gids) {
  318. foreach ($gids as $gid) {
  319. $grant_condition->condition(db_and()
  320. ->condition('gid', $gid)
  321. ->condition('realm', $realm)
  322. );
  323. }
  324. }
  325. if (count($grant_condition) > 0) {
  326. $query->condition($grant_condition);
  327. }
  328. $result = (bool) $query->execute()->fetchField();
  329. $rights[$account->uid][$node->nid] = $result;
  330. return $result;
  331. }
  332. elseif (is_object($node) && $op == 'view' && $node->status) {
  333. // If no modules implement hook_node_grants(), the default behaviour is to
  334. // allow all users to view published nodes, so reflect that here.
  335. $rights[$account->uid][$node->nid] = TRUE;
  336. return TRUE;
  337. }
  338. }
  339. return FALSE;
  340. }
  341. /**
  342. * Implements hook_features_pipe_COMPONENT_alter().
  343. *
  344. * Add variables to exported node types.
  345. */
  346. function xmlsitemap_node_features_pipe_node_alter(&$pipe, $data, $export) {
  347. if (!empty($data)) {
  348. foreach ($data as $node_type) {
  349. $pipe['variable'][] = "xmlsitemap_settings_node_{$node_type}";
  350. }
  351. }
  352. }