tracker.module

  1. 7.x drupal-7.x/modules/tracker/tracker.module
  2. 6.x drupal-6.x/modules/tracker/tracker.module

Tracks recent content posted by a user or users.

File

drupal-7.x/modules/tracker/tracker.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Tracks recent content posted by a user or users.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function tracker_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#tracker':
  12. $output = '<h3>' . t('About') . '</h3>';
  13. $output .= '<p>' . t('The Tracker module displays the most recently added and updated content on your site, and allows you to follow new content created by each user. This module has no configuration options. For more information, see the online handbook entry for <a href="@tracker">Tracker module</a>.', array('@tracker' => 'http://drupal.org/documentation/modules/tracker/')) . '</p>';
  14. $output .= '<h3>' . t('Uses') . '</h3>';
  15. $output .= '<dl>';
  16. $output .= '<dt>' . t('Navigation') . '</dt>';
  17. $output .= '<dd>' . t('The Tracker module adds a new menu item to the Navigation menu, called <em>Recent content</em>. You can configure menu items via the <a href="@menus">Menus administration page</a>.', array('@menus' => url('admin/structure/menu'))) . '</dd>';
  18. $output .= '<dt>' . t('Tracking new and updated site content') . '</dt>';
  19. $output .= '<dd>' . t("The <a href='@recent'>Recent content</a> page shows new and updated content in reverse chronological order, listing the content type, title, author's name, number of comments, and time of last update. Content is considered updated when changes occur in the text, or when new comments are added. The <em>My recent content</em> tab limits the list to the currently logged-in user.", array('@recent' => url('tracker'))) . '</dd>';
  20. $output .= '<dt>' . t('Tracking user-specific content') . '</dt>';
  21. $output .= '<dd>' . t("To follow a specific user's new and updated content, select the <em>Track</em> tab from the user's profile page.") . '</dd>';
  22. $output .= '</dl>';
  23. return $output;
  24. }
  25. }
  26. /**
  27. * Implements hook_menu().
  28. */
  29. function tracker_menu() {
  30. $items['tracker'] = array(
  31. 'title' => 'Recent content',
  32. 'page callback' => 'tracker_page',
  33. 'access arguments' => array('access content'),
  34. 'weight' => 1,
  35. 'file' => 'tracker.pages.inc',
  36. );
  37. $items['tracker/all'] = array(
  38. 'title' => 'All recent content',
  39. 'type' => MENU_DEFAULT_LOCAL_TASK,
  40. );
  41. $items['tracker/%user_uid_optional'] = array(
  42. 'title' => 'My recent content',
  43. 'page callback' => 'tracker_page',
  44. 'access callback' => '_tracker_myrecent_access',
  45. 'access arguments' => array(1),
  46. 'page arguments' => array(1),
  47. 'type' => MENU_LOCAL_TASK,
  48. 'file' => 'tracker.pages.inc',
  49. );
  50. $items['user/%user/track'] = array(
  51. 'title' => 'Track',
  52. 'page callback' => 'tracker_page',
  53. 'page arguments' => array(1, TRUE),
  54. 'access callback' => '_tracker_user_access',
  55. 'access arguments' => array(1),
  56. 'type' => MENU_LOCAL_TASK,
  57. 'file' => 'tracker.pages.inc',
  58. );
  59. $items['user/%user/track/content'] = array(
  60. 'title' => 'Track content',
  61. 'type' => MENU_DEFAULT_LOCAL_TASK,
  62. );
  63. return $items;
  64. }
  65. /**
  66. * Implements hook_cron().
  67. *
  68. * Updates tracking information for any items still to be tracked. The variable
  69. * 'tracker_index_nid' is set to ((the last node ID that was indexed) - 1) and
  70. * used to select the nodes to be processed. If there are no remaining nodes to
  71. * process, 'tracker_index_nid' will be 0.
  72. */
  73. function tracker_cron() {
  74. $max_nid = variable_get('tracker_index_nid', 0);
  75. $batch_size = variable_get('tracker_batch_size', 1000);
  76. if ($max_nid > 0) {
  77. $last_nid = FALSE;
  78. $result = db_query_range('SELECT nid, uid, status FROM {node} WHERE nid <= :max_nid ORDER BY nid DESC', 0, $batch_size, array(':max_nid' => $max_nid), array('target' => 'slave'));
  79. $count = 0;
  80. foreach ($result as $row) {
  81. // Calculate the changed timestamp for this node.
  82. $changed = _tracker_calculate_changed($row->nid);
  83. // Remove existing data for this node.
  84. db_delete('tracker_node')
  85. ->condition('nid', $row->nid)
  86. ->execute();
  87. db_delete('tracker_user')
  88. ->condition('nid', $row->nid)
  89. ->execute();
  90. // Insert the node-level data.
  91. db_insert('tracker_node')
  92. ->fields(array(
  93. 'nid' => $row->nid,
  94. 'published' => $row->status,
  95. 'changed' => $changed,
  96. ))
  97. ->execute();
  98. // Insert the user-level data for the node's author.
  99. db_insert('tracker_user')
  100. ->fields(array(
  101. 'nid' => $row->nid,
  102. 'published' => $row->status,
  103. 'changed' => $changed,
  104. 'uid' => $row->uid,
  105. ))
  106. ->execute();
  107. $query = db_select('comment', 'c', array('target' => 'slave'));
  108. // Force PostgreSQL to do an implicit cast by adding 0.
  109. $query->addExpression('0 + :changed', 'changed', array(':changed' => $changed));
  110. $query->addField('c', 'status', 'published');
  111. $query
  112. ->distinct()
  113. ->fields('c', array('uid', 'nid'))
  114. ->condition('c.nid', $row->nid)
  115. ->condition('c.uid', $row->uid, '<>')
  116. ->condition('c.status', COMMENT_PUBLISHED);
  117. // Insert the user-level data for the commenters (except if a commenter
  118. // is the node's author).
  119. db_insert('tracker_user')
  120. ->from($query)
  121. ->execute();
  122. // Note that we have indexed at least one node.
  123. $last_nid = $row->nid;
  124. $count++;
  125. }
  126. if ($last_nid !== FALSE) {
  127. // Prepare a starting point for the next run.
  128. variable_set('tracker_index_nid', $last_nid - 1);
  129. watchdog('tracker', 'Indexed %count content items for tracking.', array('%count' => $count));
  130. }
  131. else {
  132. // If all nodes have been indexed, set to zero to skip future cron runs.
  133. variable_set('tracker_index_nid', 0);
  134. }
  135. }
  136. }
  137. /**
  138. * Access callback for tracker/%user_uid_optional.
  139. */
  140. function _tracker_myrecent_access($account) {
  141. // This path is only allowed for authenticated users looking at their own content.
  142. return $account->uid && ($GLOBALS['user']->uid == $account->uid) && user_access('access content');
  143. }
  144. /**
  145. * Access callback for user/%user/track.
  146. */
  147. function _tracker_user_access($account) {
  148. return user_view_access($account) && user_access('access content');
  149. }
  150. /**
  151. * Implements hook_node_insert().
  152. *
  153. * Adds new tracking information for this node since it's new.
  154. */
  155. function tracker_node_insert($node, $arg = 0) {
  156. _tracker_add($node->nid, $node->uid, $node->changed);
  157. }
  158. /**
  159. * Implements hook_node_update().
  160. *
  161. * Adds tracking information for this node since it's been updated.
  162. */
  163. function tracker_node_update($node, $arg = 0) {
  164. _tracker_add($node->nid, $node->uid, $node->changed);
  165. }
  166. /**
  167. * Implements hook_node_delete().
  168. *
  169. * Deletes tracking information for a node.
  170. */
  171. function tracker_node_delete($node, $arg = 0) {
  172. db_delete('tracker_node')
  173. ->condition('nid', $node->nid)
  174. ->execute();
  175. db_delete('tracker_user')
  176. ->condition('nid', $node->nid)
  177. ->execute();
  178. }
  179. /**
  180. * Implements hook_comment_update().
  181. *
  182. * Comment module doesn't call hook_comment_unpublish() when saving individual
  183. * comments so we need to check for those here.
  184. */
  185. function tracker_comment_update($comment) {
  186. // comment_save() calls hook_comment_publish() for all published comments
  187. // so we need to handle all other values here.
  188. if ($comment->status != COMMENT_PUBLISHED) {
  189. _tracker_remove($comment->nid, $comment->uid, $comment->changed);
  190. }
  191. }
  192. /**
  193. * Implements hook_comment_publish().
  194. *
  195. * This actually handles the insert and update of published nodes since
  196. * comment_save() calls hook_comment_publish() for all published comments.
  197. */
  198. function tracker_comment_publish($comment) {
  199. _tracker_add($comment->nid, $comment->uid, $comment->changed);
  200. }
  201. /**
  202. * Implements hook_comment_unpublish().
  203. */
  204. function tracker_comment_unpublish($comment) {
  205. _tracker_remove($comment->nid, $comment->uid, $comment->changed);
  206. }
  207. /**
  208. * Implements hook_comment_delete().
  209. */
  210. function tracker_comment_delete($comment) {
  211. _tracker_remove($comment->nid, $comment->uid, $comment->changed);
  212. }
  213. /**
  214. * Updates indexing tables when a node is added, updated, or commented on.
  215. *
  216. * @param $nid
  217. * A node ID.
  218. * @param $uid
  219. * The node or comment author.
  220. * @param $changed
  221. * The node updated timestamp or comment timestamp.
  222. */
  223. function _tracker_add($nid, $uid, $changed) {
  224. $node = db_query('SELECT nid, status, uid, changed FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
  225. // Adding a comment can only increase the changed timestamp, so our
  226. // calculation here is simple.
  227. $changed = max($node->changed, $changed);
  228. // Update the node-level data.
  229. db_merge('tracker_node')
  230. ->key(array('nid' => $nid))
  231. ->fields(array(
  232. 'changed' => $changed,
  233. 'published' => $node->status,
  234. ))
  235. ->execute();
  236. // Create or update the user-level data.
  237. db_merge('tracker_user')
  238. ->key(array(
  239. 'nid' => $nid,
  240. 'uid' => $uid,
  241. ))
  242. ->fields(array(
  243. 'changed' => $changed,
  244. 'published' => $node->status,
  245. ))
  246. ->execute();
  247. }
  248. /**
  249. * Determines the max timestamp between $node->changed and the last comment.
  250. *
  251. * @param $nid
  252. * A node ID.
  253. *
  254. * @return
  255. * The $node->changed timestamp, or most recent comment timestamp, whichever
  256. * is the greatest.
  257. */
  258. function _tracker_calculate_changed($nid) {
  259. $changed = db_query('SELECT changed FROM {node} WHERE nid = :nid', array(':nid' => $nid), array('target' => 'slave'))->fetchField();
  260. $latest_comment = db_query_range('SELECT cid, changed FROM {comment} WHERE nid = :nid AND status = :status ORDER BY changed DESC', 0, 1, array(
  261. ':nid' => $nid,
  262. ':status' => COMMENT_PUBLISHED,
  263. ), array('target' => 'slave'))->fetchObject();
  264. if ($latest_comment && $latest_comment->changed > $changed) {
  265. $changed = $latest_comment->changed;
  266. }
  267. return $changed;
  268. }
  269. /**
  270. * Cleans up indexed data when nodes or comments are removed.
  271. *
  272. * @param $nid
  273. * The node ID.
  274. * @param $uid
  275. * The author of the node or comment.
  276. * @param $changed
  277. * The last changed timestamp of the node.
  278. */
  279. function _tracker_remove($nid, $uid = NULL, $changed = NULL) {
  280. $node = db_query('SELECT nid, status, uid, changed FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
  281. // The user only keeps his or her subscription if both of the following are true:
  282. // (1) The node exists.
  283. // (2) The user is either the node author or has commented on the node.
  284. $keep_subscription = FALSE;
  285. if ($node) {
  286. // Self-authorship is one reason to keep the user's subscription.
  287. $keep_subscription = ($node->uid == $uid);
  288. // Comments are a second reason to keep the user's subscription.
  289. if (!$keep_subscription) {
  290. // Check if the user has commented at least once on the given nid.
  291. $keep_subscription = db_query_range('SELECT COUNT(*) FROM {comment} WHERE nid = :nid AND uid = :uid AND status = :status', 0, 1, array(
  292. ':nid' => $nid,
  293. ':uid' => $uid,
  294. ':status' => COMMENT_PUBLISHED,
  295. ))->fetchField();
  296. }
  297. // If we haven't found a reason to keep the user's subscription, delete it.
  298. if (!$keep_subscription) {
  299. db_delete('tracker_user')
  300. ->condition('nid', $nid)
  301. ->condition('uid', $uid)
  302. ->execute();
  303. }
  304. // Now we need to update the (possibly) changed timestamps for other users
  305. // and the node itself.
  306. // We only need to do this if the removed item has a timestamp that equals
  307. // or exceeds the listed changed timestamp for the node.
  308. $tracker_node = db_query('SELECT nid, changed FROM {tracker_node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
  309. if ($tracker_node && $changed >= $tracker_node->changed) {
  310. // If we're here, the item being removed is *possibly* the item that
  311. // established the node's changed timestamp.
  312. // We just have to recalculate things from scratch.
  313. $changed = _tracker_calculate_changed($nid);
  314. // And then we push the out the new changed timestamp to our denormalized
  315. // tables.
  316. db_update('tracker_node')
  317. ->fields(array(
  318. 'changed' => $changed,
  319. 'published' => $node->status,
  320. ))
  321. ->condition('nid', $nid)
  322. ->execute();
  323. db_update('tracker_node')
  324. ->fields(array(
  325. 'changed' => $changed,
  326. 'published' => $node->status,
  327. ))
  328. ->condition('nid', $nid)
  329. ->execute();
  330. }
  331. }
  332. else {
  333. // If the node doesn't exist, remove everything.
  334. db_delete('tracker_node')
  335. ->condition('nid', $nid)
  336. ->execute();
  337. db_delete('tracker_user')
  338. ->condition('nid', $nid)
  339. ->execute();
  340. }
  341. }