tracker.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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, first for the user posting.
  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. // Update the times for all the other users tracking the post.
  248. db_update('tracker_user')
  249. ->condition('nid', $nid)
  250. ->fields(array(
  251. 'changed' => $changed,
  252. 'published' => $node->status,
  253. ))
  254. ->execute();
  255. }
  256. /**
  257. * Determines the max timestamp between $node->changed and the last comment.
  258. *
  259. * @param $nid
  260. * A node ID.
  261. *
  262. * @return
  263. * The $node->changed timestamp, or most recent comment timestamp, whichever
  264. * is the greatest.
  265. */
  266. function _tracker_calculate_changed($nid) {
  267. $changed = db_query('SELECT changed FROM {node} WHERE nid = :nid', array(':nid' => $nid), array('target' => 'slave'))->fetchField();
  268. $latest_comment = db_query_range('SELECT cid, changed FROM {comment} WHERE nid = :nid AND status = :status ORDER BY changed DESC', 0, 1, array(
  269. ':nid' => $nid,
  270. ':status' => COMMENT_PUBLISHED,
  271. ), array('target' => 'slave'))->fetchObject();
  272. if ($latest_comment && $latest_comment->changed > $changed) {
  273. $changed = $latest_comment->changed;
  274. }
  275. return $changed;
  276. }
  277. /**
  278. * Cleans up indexed data when nodes or comments are removed.
  279. *
  280. * @param $nid
  281. * The node ID.
  282. * @param $uid
  283. * The author of the node or comment.
  284. * @param $changed
  285. * The last changed timestamp of the node.
  286. */
  287. function _tracker_remove($nid, $uid = NULL, $changed = NULL) {
  288. $node = db_query('SELECT nid, status, uid, changed FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
  289. // The user only keeps his or her subscription if both of the following are true:
  290. // (1) The node exists.
  291. // (2) The user is either the node author or has commented on the node.
  292. $keep_subscription = FALSE;
  293. if ($node) {
  294. // Self-authorship is one reason to keep the user's subscription.
  295. $keep_subscription = ($node->uid == $uid);
  296. // Comments are a second reason to keep the user's subscription.
  297. if (!$keep_subscription) {
  298. // Check if the user has commented at least once on the given nid.
  299. $keep_subscription = db_query_range('SELECT COUNT(*) FROM {comment} WHERE nid = :nid AND uid = :uid AND status = :status', 0, 1, array(
  300. ':nid' => $nid,
  301. ':uid' => $uid,
  302. ':status' => COMMENT_PUBLISHED,
  303. ))->fetchField();
  304. }
  305. // If we haven't found a reason to keep the user's subscription, delete it.
  306. if (!$keep_subscription) {
  307. db_delete('tracker_user')
  308. ->condition('nid', $nid)
  309. ->condition('uid', $uid)
  310. ->execute();
  311. }
  312. // Now we need to update the (possibly) changed timestamps for other users
  313. // and the node itself.
  314. // We only need to do this if the removed item has a timestamp that equals
  315. // or exceeds the listed changed timestamp for the node.
  316. $tracker_node = db_query('SELECT nid, changed FROM {tracker_node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
  317. if ($tracker_node && $changed >= $tracker_node->changed) {
  318. // If we're here, the item being removed is *possibly* the item that
  319. // established the node's changed timestamp.
  320. // We just have to recalculate things from scratch.
  321. $changed = _tracker_calculate_changed($nid);
  322. // And then we push the out the new changed timestamp to our denormalized
  323. // tables.
  324. db_update('tracker_node')
  325. ->fields(array(
  326. 'changed' => $changed,
  327. 'published' => $node->status,
  328. ))
  329. ->condition('nid', $nid)
  330. ->execute();
  331. db_update('tracker_node')
  332. ->fields(array(
  333. 'changed' => $changed,
  334. 'published' => $node->status,
  335. ))
  336. ->condition('nid', $nid)
  337. ->execute();
  338. }
  339. }
  340. else {
  341. // If the node doesn't exist, remove everything.
  342. db_delete('tracker_node')
  343. ->condition('nid', $nid)
  344. ->execute();
  345. db_delete('tracker_user')
  346. ->condition('nid', $nid)
  347. ->execute();
  348. }
  349. }