xmlsitemap_term.module

Tracking 5.x-2.x branch
  1. drupal
    1. 5 contributions/xmlsitemap/xmlsitemap_term/xmlsitemap_term.module

Adds taxonomy terms to the sitemap.

Functions & methods

NameDescription
xmlsitemap_term_form_alterImplementation of hook_form_alter().
xmlsitemap_term_nodeapiImplementation of hook_nodeapi().
xmlsitemap_term_taxonomyImplementation of hook_taxonomy().
xmlsitemap_term_xmlsitemap_descriptionImplementation of hook_xmlsitemap_description().
xmlsitemap_term_xmlsitemap_linksImplementation of hook_xmlsitemap_links().

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Adds taxonomy terms to the sitemap.
  5. */
  6. /**
  7. * @addtogroup xmlsitemap
  8. * @{
  9. */
  10. /*****************************************************************************
  11. * Drupal hooks.
  12. ****************************************************************************/
  13. /**
  14. * Implementation of hook_form_alter().
  15. */
  16. function xmlsitemap_term_form_alter($form_id, &$form) {
  17. if (isset($form['type']['#value']) && $form_id == $form['type']['#value'] .'_node_form') {
  18. $node = $form['#node'];
  19. if (isset($form['type']) && isset($node->nid)) {
  20. if (isset($node->taxonomy)) {
  21. $terms = $node->taxonomy;
  22. }
  23. else {
  24. $terms = taxonomy_node_get_terms($node->nid);
  25. }
  26. $form['xmlsitemap_term_taxonomy'] = array(
  27. '#type' => 'value',
  28. '#value' => $terms,
  29. );
  30. }
  31. }
  32. else {
  33. switch ($form_id) {
  34. case 'taxonomy_form_term':
  35. $priority = isset($form['tid']['#value']) ? db_result(db_query("SELECT priority_override FROM {xmlsitemap_term} WHERE tid = %d", $form['tid']['#value'])) : 'NULL';
  36. if ($priority === FALSE) {
  37. $priority = 'NULL';
  38. }
  39. if (user_access('override term priority')) {
  40. $options = xmlsitemap_priority_options('both');
  41. $default = variable_get('xmlsitemap_term_vocabulary_priority_'. $form['vid']['#value'], '0.5');
  42. $form['xmlsitemap_term_priority'] = array(
  43. '#type' => 'select',
  44. '#title' => t('sitemap priority'),
  45. '#default_value' => $priority,
  46. '#options' => $options,
  47. '#description' => t('The default priority is %priority.', array('%priority' => $options[$default])),
  48. );
  49. $form['submit']['#weight'] = 1;
  50. $form['delete']['#weight'] = 1;
  51. }
  52. else {
  53. $form['xmlsitemap_term_priority'] = array('#type' => 'value', '#value' => $priority);
  54. }
  55. break;
  56. case 'taxonomy_form_vocabulary':
  57. $form['xmlsitemap_term_vocabulary_priority'] = array(
  58. '#type' => 'select',
  59. '#title' => t('sitemap priority'),
  60. '#default_value' => variable_get('xmlsitemap_term_vocabulary_priority_'. $form['vid']['#value'], 0.5),
  61. '#options' => xmlsitemap_priority_options('exclude'),
  62. '#description' => t('This will be the default priority of terms in this vocabulary.'),
  63. );
  64. $form['submit']['#weight'] = 1;
  65. $form['delete']['#weight'] = 1;
  66. break;
  67. }
  68. }
  69. }
  70. /**
  71. * Implementation of hook_nodeapi().
  72. */
  73. function xmlsitemap_term_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  74. switch ($op) {
  75. case 'insert':
  76. if (!empty($node->taxonomy)) {
  77. $terms = array_keys($node->taxonomy);
  78. $query_args = array_merge(array($node->changed), $terms);
  79. }
  80. break;
  81. case 'update':
  82. $taxonomy = empty($node->taxonomy) ? array() : array_keys($node->taxonomy);
  83. $xmlsitemap_term_taxonomy = empty($node->xmlsitemap_term_taxonomy) ? array() : array_keys($node->xmlsitemap_term_taxonomy);
  84. $terms = array_merge(array_diff($taxonomy, $xmlsitemap_term_taxonomy), array_diff($xmlsitemap_term_taxonomy, $taxonomy));
  85. $query_args = array_merge(array($node->changed), $terms);
  86. break;
  87. case 'delete':
  88. if (!empty($node->taxonomy)) {
  89. $terms = array_keys($node->taxonomy);
  90. $query_args = array_merge(array(REQUEST_TIME), $terms);
  91. }
  92. break;
  93. }
  94. if (!empty($terms)) {
  95. db_query("UPDATE {xmlsitemap_term}
  96. SET previously_changed = last_changed, last_changed = %d WHERE tid IN(". xmlsitemap_placeholders($terms, 'int') .")",
  97. $query_args
  98. );
  99. xmlsitemap_flag_sitemap();
  100. }
  101. }
  102. /**
  103. * Implementation of hook_taxonomy().
  104. */
  105. function xmlsitemap_term_taxonomy($op, $type, $array = NULL) {
  106. if ($type == 'vocabulary') {
  107. switch ($op) {
  108. case 'delete':
  109. variable_del('xmlsitemap_term_vocabulary_priority_'. $array['vid']);
  110. xmlsitemap_flag_sitemap();
  111. break;
  112. case 'insert':
  113. case 'update':
  114. if (variable_get('xmlsitemap_term_vocabulary_priority_'. $array['vid'], 0.5) != $array['xmlsitemap_term_vocabulary_priority']) {
  115. variable_set('xmlsitemap_term_vocabulary_priority_'. $array['vid'], $array['xmlsitemap_term_vocabulary_priority']);
  116. xmlsitemap_flag_sitemap();
  117. }
  118. break;
  119. }
  120. }
  121. else {
  122. switch ($op) {
  123. case 'insert':
  124. $priority = isset($array['xmlsitemap_term_priority']) ? $array['xmlsitemap_term_priority'] : 'NULL';
  125. db_query("INSERT INTO {xmlsitemap_term} (tid, last_changed, priority_override) VALUES (%d, %d, %s)",
  126. $array['tid'], REQUEST_TIME, $priority);
  127. break;
  128. case 'update':
  129. if (!isset($array['xmlsitemap_term_priority'])) {
  130. $priority = db_result(db_query("SELECT priority_override FROM {xmlsitemap_term} WHERE tid = %d", $array['tid']));
  131. $array['xmlsitemap_term_priority'] = isset($priority) && $priority !== FALSE ? $priority : 'NULL';
  132. }
  133. db_query("UPDATE {xmlsitemap_term}
  134. SET priority_override = %s
  135. WHERE tid = %d",
  136. $array['xmlsitemap_term_priority'], $array['tid']
  137. );
  138. break;
  139. case 'delete':
  140. db_query("DELETE FROM {xmlsitemap_term} WHERE tid = %d", $array['tid']);
  141. break;
  142. }
  143. xmlsitemap_flag_sitemap();
  144. }
  145. }
  146. /**
  147. * Implementation of hook_xmlsitemap_description().
  148. */
  149. function xmlsitemap_term_xmlsitemap_description() {
  150. return '<dt>'. t('XML sitemap term') .'</dt>'.
  151. '<dd>'. t('It adds <a href="@terms">taxonomy terms</a> (categories) to the sitemap. You can change the default priority when you add or edit a vocabulary, and you can override the default priority when you add or edit individual terms.', array('@terms' => url('admin/content/taxonomy'))) .'</dd>';
  152. }
  153. /**
  154. * Implementation of hook_xmlsitemap_links().
  155. */
  156. function xmlsitemap_term_xmlsitemap_links() {
  157. $excludes = array();
  158. $result = db_query("SELECT vid FROM {vocabulary}");
  159. while ($vocabulary = db_fetch_object($result)) {
  160. if (variable_get('xmlsitemap_term_vocabulary_priority_'. $vocabulary->vid, 0.5) < 0) {
  161. $excludes[] = $vocabulary->vid;
  162. }
  163. }
  164. if (empty($excludes)) {
  165. $excludes = array(0);
  166. }
  167. $tid = 0;
  168. $frontpage = explode('/', drupal_get_normal_path(variable_get('site_frontpage', 'node')));
  169. if (count($frontpage) == 3 && $frontpage[0] == 'taxonomy' && $frontpage[1] == 'term' && is_numeric($frontpage[2])) {
  170. $tid = $frontpage[2];
  171. }
  172. elseif (count($frontpage) == 2 && $frontpage[0] == 'forum' && is_numeric($frontpage[1])) {
  173. $tid = $frontpage[1];
  174. }
  175. $query_args = array_merge($excludes, array($tid));
  176. $result = db_query(db_rewrite_sql("SELECT t.tid, t.vid, v.module, xt.last_changed, xt.previously_changed, xt.priority_override
  177. FROM {term_data} t
  178. LEFT JOIN {vocabulary} v ON t.vid = v.vid
  179. LEFT JOIN {xmlsitemap_term} xt ON t.tid = xt.tid
  180. WHERE (t.vid NOT IN (". xmlsitemap_placeholders($excludes, 'int') .") AND xt.priority_override IS NULL OR xt.priority_override >= 0)
  181. AND t.tid <> %d
  182. GROUP BY t.tid, t.vid, v.module, xt.last_changed, xt.previously_changed, xt.priority_override", 't', 'tid'), $query_args
  183. );
  184. while ($term = db_fetch_object($result)) {
  185. $path = taxonomy_term_path($term);
  186. $alias = drupal_lookup_path('alias', $path);
  187. if ($alias === FALSE) {
  188. $alias = NULL;
  189. }
  190. $url = xmlsitemap_url($path, $alias, NULL, NULL, TRUE);
  191. $age = REQUEST_TIME - $term->last_changed;
  192. $interval = empty($term->previously_changed) ? 0 : $term->last_changed - $term->previously_changed;
  193. if (isset($term->priority_override)) {
  194. $priority = $term->priority_override;
  195. }
  196. else {
  197. $priority = min(variable_get("xmlsitemap_term_vocabulary_priority_$term->vid", 0.5), 1);
  198. }
  199. db_query("INSERT INTO {xmlsitemap} (loc, lastmod, changefreq, priority) VALUES ('%s', %d, %d, %f)", $url, $term->last_changed, max($age, $interval), $priority);
  200. }
  201. }
  202. /**
  203. * @} End of "addtogroup xmlsitemap".
  204. */