Index: modules/tracker/tracker.install
===================================================================
RCS file: modules/tracker/tracker.install
diff -N modules/tracker/tracker.install
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ modules/tracker/tracker.install 22 Jan 2009 12:52:48 -0000
@@ -0,0 +1,108 @@
+ $max_nid)));
+ }
+}
+
+/**
+ * Implementation of hook_schema().
+ */
+function tracker_schema() {
+ $schema['tracker_node'] = array(
+ 'description' => 'Stores information about node.',
+ 'fields' => array(
+ 'nid' => array(
+ 'description' => 'The nid of the node.',
+ 'type' => 'int',
+ 'unsigned' => TRUE,
+ 'not null' => TRUE,
+ 'default' => 0,
+ ),
+ 'published' => array(
+ 'description' => 'Boolean indicating whether the node is published.',
+ 'type' => 'int',
+ 'not null' => FALSE,
+ 'default' => 0,
+ 'size' => 'tiny',
+ ),
+ 'changed' => array(
+ 'description' => 'The Unix timestamp when the node was most recently saved.',
+ 'type' => 'int',
+ 'unsigned' => TRUE,
+ 'not null' => TRUE,
+ 'default' => 0,
+ ),
+ ),
+ 'indexes' => array(
+ 'tracker' => array('published', 'changed'),
+ ),
+ 'primary key' => array('nid'),
+ );
+
+ $schema['tracker_user'] = array(
+ 'description' => 'Stores information about node per user.',
+ 'fields' => array(
+ 'nid' => array(
+ 'description' => 'The id of the node.',
+ 'type' => 'int',
+ 'unsigned' => TRUE,
+ 'not null' => TRUE,
+ 'default' => 0,
+ ),
+ 'uid' => array(
+ 'description' => 'The id of the user.',
+ 'type' => 'int',
+ 'unsigned' => TRUE,
+ 'not null' => TRUE,
+ 'default' => 0,
+ ),
+ 'published' => array(
+ 'description' => 'Boolean indicating whether the node is published.',
+ 'type' => 'int',
+ 'not null' => FALSE,
+ 'default' => 0,
+ 'size' => 'tiny',
+ ),
+ 'changed' => array(
+ 'description' => 'The Unix timestamp when the node was most recently saved.',
+ 'type' => 'int',
+ 'unsigned' => TRUE,
+ 'not null' => TRUE,
+ 'default' => 0,
+ ),
+ ),
+ 'indexes' => array(
+ 'tracker' => array('uid', 'published', 'changed'),
+ ),
+ 'primary key' => array('nid', 'uid'),
+ );
+
+ return $schema;
+}
Index: modules/tracker/tracker.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/tracker/tracker.module,v
retrieving revision 1.157
diff -u -p -r1.157 tracker.module
--- modules/tracker/tracker.module 6 May 2008 12:18:51 -0000 1.157
+++ modules/tracker/tracker.module 22 Jan 2009 12:52:48 -0000
@@ -31,10 +31,13 @@ function tracker_menu() {
);
$items['tracker/all'] = array(
'title' => 'All recent posts',
+ 'page callback' => 'tracker_page',
+ 'access arguments' => array('access content'),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['tracker/%user_uid_optional'] = array(
'title' => 'My recent posts',
+ 'page callback' => 'tracker_page',
'access callback' => '_tracker_myrecent_access',
'access arguments' => array(1),
'page arguments' => array(1),
@@ -53,10 +56,79 @@ function tracker_menu() {
'title' => 'Track posts',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
+
+ $items['admin/settings/tracker'] = array(
+ 'title' => 'Tracker',
+ 'description' => 'Settings for the tracker module',
+ 'page callback' => 'drupal_get_form',
+ 'page arguments' => array('tracker_admin_settings'),
+ 'access arguments' => array('administer tracker')
+ );
+
return $items;
}
/**
+ * Implementation of hook_perm().
+ */
+function tracker_perm() {
+ return array(
+ 'administer tracker' => array(
+ 'title' => t('Administer tracker'),
+ 'description' => t('Manage tracker administration settings.'),
+ ),
+ );
+}
+
+/**
+ * Implementation of hook_cron().
+ */
+function tracker_cron() {
+ $max_nid = variable_get('tracker_index_nid', 0);
+ $batch_size = variable_get('tracker_batch_size', 1000);
+ if ($max_nid > 0) {
+ $last_nid = FALSE;
+ $result = db_query_range('SELECT nid, uid, status FROM {node} WHERE nid <= :max_nid ORDER BY nid DESC', array(':max_nid' => $max_nid) , 0, $batch_size);
+
+ $count = 0;
+
+ foreach ($result as $row) {
+ // Calculate the changed timestamp for this node.
+ $changed = _tracker_calculate_changed($row->nid);
+
+ // Remove existing data for this node.
+ db_delete('tracker_node')->condition('nid', $row->nid)->execute();
+ db_delete('tracker_user')->condition('nid', $row->nid)->execute();
+
+ // Insert the node-level data.
+ db_insert('tracker_node')->fields(array('nid', 'published', 'changed'))->values(array($row->nid, $row->status, $changed))->execute();
+
+ // Insert the user-level data for the node's author.
+ db_insert('tracker_user')->fields(array('nid', 'published', 'uid', 'changed'))->values(array($row->nid, $row->status, $row->uid, $changed))->execute();
+
+ // Insert the user-level data for the commenters (except if a commenter is the node's author).
+ db_query('INSERT INTO {tracker_user} (nid, published, uid, changed) SELECT DISTINCT %d AS nid, %d AS published, uid, %d AS changed FROM {comment} WHERE nid = %d AND uid <> %d AND status = %d', $row->nid, $row->status, $changed, $row->nid, $row->uid, COMMENT_PUBLISHED);
+
+ // Note that we have indexed at least one node.
+ $last_nid = $row->nid;
+
+ ++$count;
+ }
+
+ if ($last_nid !== FALSE) {
+ // Prepare a starting point for the next run
+ variable_set('tracker_index_nid', $last_nid - 1);
+
+ watchdog('tracker', t('Indexed %count nodes for tracking.', array('%count' => $count)));
+ }
+ else {
+ // If all nodes have been indexed, set to zero to skip future cron runs
+ variable_set('tracker_index_nid', 0);
+ }
+ }
+}
+
+/**
* Access callback for tracker/%user_uid_optional
*/
function _tracker_myrecent_access($account) {
@@ -71,3 +143,178 @@ function _tracker_user_access($account)
return user_view_access($account) && user_access('access content');
}
+/**
+ * Menu callback argument. Prints a listing of active nodes on the site.
+ */
+function tracker_admin_settings() {
+ $form = array();
+
+ $max_nid = variable_get('tracker_index_nid', 0);
+
+ if ($max_nid) {
+ $form['max_nid'] = array(
+ '#markup' => t('Max node ID for indexing on the next cron run: @max', array('@max' => $max_nid)),
+ );
+ }
+ else {
+ $form['max_nid'] = array(
+ '#markup' => t('Existing nodes have finished tracker indexing.'),
+ );
+ }
+
+ $form['tracker_batch_size'] = array(
+ '#title' => t('Batch size'),
+ '#description' => t('Number of nodes to index during each cron run.'),
+ '#type' => 'textfield',
+ '#size' => 6,
+ '#maxlength' => 7,
+ '#default_value' => variable_get('tracker_batch_size', 1000),
+ );
+
+ return system_settings_form($form);
+}
+
+/**
+ * Implementation of hook_form_alter().
+ */
+function tracker_form_alter(&$form, $form_state, $form_id) {
+ if ($form_id == 'node_admin_nodes') {
+ $form['#submit']['tracker2_batch_node_alter'] = array();
+ }
+}
+
+function tracker_batch_node_alter($form, &$form_state) {
+ $op = $form_state['values']['operation'];
+ foreach($form_state['values']['nodes'] as $nid => $selected) {
+ if ($selected) {
+ if ($op == 'publish') {
+ db_update('tracker_node')->fields(array('published' => 1))->condition('nid', $nid)->execute;
+ db_update('tracker_user')->fields(array('published' => 1))->condition('nid', $nid)->execute();
+ }
+ else if ($op == 'unpublish') {
+ db_update('tracker_node')->fields(array('published' => 0))->condition('nid', $nid)->execute();
+ db_update('tracker_user')->fields(array('published' => 0))->condition('nid', $nid)->execute();;
+ }
+ else if ($op == 'delete') {
+ _tracker_remove($nid);
+ }
+ }
+ }
+}
+
+/**
+ * Implementation of hook_nodeapi_insert().
+ */
+function tracker_nodeapi_insert(&$node, $arg = 0) {
+ _tracker_add($node->nid, $node->uid, $node->changed);
+}
+
+/**
+ * Implementation of hook_nodeapi_update().
+ */
+function tracker_nodeapi_update(&$node, $arg = 0) {
+ _tracker_add($node->nid, $node->uid, $node->changed);
+}
+
+/**
+ * Implementation of hook_nodeapi_delete().
+ */
+function tracker_nodeapi_delete(&$node, $arg = 0) {
+ _tracker_remove($node->nid, $node->uid, $node->changed);
+}
+
+/**
+ * Implementation of hook_comment().
+ */
+function tracker_comment(&$a1, $op) {
+ $comment = (array) $a1;
+ if ($op == 'insert' || $op == 'update' || $op == 'publish') {
+ if ($comment['status'] == COMMENT_PUBLISHED) {
+ _tracker_add($comment['nid'], $comment['uid'], $comment['timestamp']);
+ }
+ else {
+ _tracker_remove($comment['nid'], $comment['uid'], $comment['timestamp']);
+ }
+ }
+ else if ($op == 'delete' || $op == 'unpublish') {
+ _tracker_remove($comment['nid'], $comment['uid'], $comment['timestamp']);
+ }
+}
+
+function _tracker_add($nid, $uid, $changed) {
+ $node = db_query('SELECT nid, status, uid, changed FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
+
+ // Adding a comment can only increase the changed timestamp, so our calculation here is easy.
+ $changed = max($node->changed, $changed);
+
+ // Update the node-level data.
+ $exists = db_query('SELECT COUNT(*) FROM {tracker_node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
+ if ($exists) {
+ db_update('tracker_node')->fields(array('changed' => $changed, 'published' => $node->status))->condition('nid', $nid)->execute();
+ }
+ else {
+ db_insert('tracker_node')->fields(array('changed', 'published', 'nid'))->values(array($changed, $node->status, $nid))->execute();
+ }
+
+ // Create or update the user-level data
+ db_update('tracker_user')->fields(array('changed' => $changed, 'published' => $node->status))->condition('nid', $nid)->execute();
+ $exists = db_query('SELECT COUNT(*) FROM {tracker_user} WHERE nid = :nid AND uid = :uid', array(':nid' => $nid, ':uid' => $uid))->fetchField();
+ if (!$exists) {
+ db_insert('tracker_user')->fields(array('changed', 'published', 'nid', 'uid'))->values(array($changed, $node->status, $nid, $uid))->execute();
+ }
+}
+
+function _tracker_calculate_changed($nid) {
+ $changed = db_query('SELECT changed FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
+ $latest_comment = db_query_range('SELECT cid, timestamp FROM {comment} WHERE nid = :nid AND status = :status ORDER BY timestamp DESC', array(':nid' => $nid, ':status' => COMMENT_PUBLISHED), 0, 1)->fetchObject();
+ if ($latest_comment && $latest_comment->timestamp > $changed) {
+ $changed = $latest_comment->timestamp;
+ }
+ return $changed;
+}
+
+function _tracker_remove($nid, $uid = NULL, $changed = NULL) {
+ $node = db_query('SELECT nid, status, uid, changed FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
+
+ // The user only keeps his or her subscription if both of the following are true:
+ // (1) The node exists.
+ // (2) The user is either the node author or has commented on the node.
+ $keep_subscription = FALSE;
+
+ if ($node) {
+ // Self-authorship is one reason to keep the user's subscription.
+ $keep_subscription = ($node->uid == $uid);
+
+ // Comments are a second reason to keep the user's subscription.
+ if (!$keep_subscription) {
+ // Check if the user has commented at least once on the given nid
+ $keep_subscription = db_query_range('SELECT COUNT(*) FROM {comments} WHERE nid = :nid AND uid = :uid AND status = 0', array(':nid' => $nid, ':uid' => $uid), 0, 1)->fetchField();
+ }
+
+ // If we haven't found a reason to keep the user's subscription, delete it.
+ if (!$keep_subscription) {
+ db_delete('tracker_user')->condition('nid', $nid)->condition('uid', $uid)->execute();
+ }
+
+ // Now we need to update the (possibly) changed timestamps for other users and the node itself.
+
+ // We only need to do this if the removed item has a timestamp that equals
+ // or exceeds the listed changed timestamp for the node
+ $tracker_node = db_query('SELECT nid, changed FROM {tracker_node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
+ if ($tracker_node && $changed >= $tracker_node->changed) {
+ // If we're here, the item being removed is *possibly* the item that established the node's changed timestamp.
+
+ // We just have to recalculate things from scratch.
+ $changed = _tracker_calculate_changed($nid);
+
+ // And then we push the out the new changed timestamp to our denormalized tables.
+ db_update('tracker_node')->fields(array('changed' => $changed, 'published' => $node->status))->condition('nid', $nid)->execute();
+ db_update('tracker_user')->fields(array('changed' => $changed, 'published' => $node->status))->condition('nid', $nid)->execute();
+ }
+ }
+ else {
+ // If the node doesn't exist, remove everything
+ db_delete('tracker_node')->condition('nid', $nid)->execute();
+ db_delete('tracker_user')->condition('nid', $nid)->execute();
+ }
+}
Index: modules/tracker/tracker.pages.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/tracker/tracker.pages.inc,v
retrieving revision 1.14
diff -u -p -r1.14 tracker.pages.inc
--- modules/tracker/tracker.pages.inc 30 Dec 2008 16:43:19 -0000 1.14
+++ modules/tracker/tracker.pages.inc 22 Jan 2009 12:52:48 -0000
@@ -21,44 +21,60 @@ function tracker_page($account = NULL, $
// here and not in the menu definition.
drupal_set_title($account->name);
}
- // TODO: These queries are very expensive, see http://drupal.org/node/105639
- $sql = 'SELECT DISTINCT(n.nid), n.title, n.type, n.changed, n.uid, u.name, GREATEST(n.changed, l.last_comment_timestamp) AS last_updated, l.comment_count FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {users} u ON n.uid = u.uid LEFT JOIN {comment} c ON n.nid = c.nid AND (c.status = %d OR c.status IS NULL) WHERE n.status = 1 AND (n.uid = %d OR c.uid = %d) ORDER BY last_updated DESC';
- $sql = db_rewrite_sql($sql);
- $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n LEFT JOIN {comment} c ON n.nid = c.nid AND (c.status = %d OR c.status IS NULL) WHERE n.status = 1 AND (n.uid = %d OR c.uid = %d)';
- $sql_count = db_rewrite_sql($sql_count);
- $result = pager_query($sql, 25, 0, $sql_count, COMMENT_PUBLISHED, $account->uid, $account->uid);
+ $sql = 'SELECT tu.nid, tu.changed AS last_activity FROM {tracker_user} tu WHERE tu.published = 1 AND tu.uid = %d ORDER BY tu.changed DESC';
+ $sql = db_rewrite_sql($sql, 'tu');
+ $sql_count = 'SELECT COUNT(tu.nid) FROM {tracker_user} tu WHERE tu.published = 1 AND tu.uid = %d';
+ $sql_count = db_rewrite_sql($sql_count, 'tu');
+ $result = pager_query($sql, 25, 0, $sql_count, $account->uid);
}
else {
- $sql = 'SELECT DISTINCT(n.nid), n.title, n.type, n.changed, n.uid, u.name, GREATEST(n.changed, l.last_comment_timestamp) AS last_updated, l.comment_count FROM {node} n INNER JOIN {users} u ON n.uid = u.uid INNER JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE n.status = 1 ORDER BY last_updated DESC';
- $sql = db_rewrite_sql($sql);
+ $sql = 'SELECT tn.nid, tn.changed AS last_activity FROM {tracker_node} tn WHERE tn.published = 1 ORDER BY tn.changed DESC';
+ $sql = db_rewrite_sql($sql, 'tn');
$sql_count = 'SELECT COUNT(n.nid) FROM {node} n WHERE n.status = 1';
$sql_count = db_rewrite_sql($sql_count);
$result = pager_query($sql, 25, 0, $sql_count);
}
- $rows = array();
+ // This array acts as a placeholder for the data selected later
+ // while keeping the correct order.
+ $nodes = array();
while ($node = db_fetch_object($result)) {
- // Determine the number of comments:
- $comments = 0;
- if ($node->comment_count) {
- $comments = $node->comment_count;
-
- if ($new = comment_num_new($node->nid)) {
- $comments .= '
';
- $comments .= l(format_plural($new, '1 new', '@count new'), "node/$node->nid", array('query' => comment_new_page_count($node->comment_count, $new, $node), 'fragment' => 'new'));
- }
+ $nodes[$node->nid] = $node;
+ }
+
+ if (!empty($nodes)) {
+ // Now, get the data and put into the placeholder array
+ $placeholders = implode(',', array_fill(0, count($nodes), '%d'));
+ $result = db_query("SELECT n.nid, n.title, n.type, n.changed, n.uid, u.name, l.comment_count FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {users} u ON n.uid = u.uid WHERE n.nid IN (:nids)", array(':nids' => array_keys($nodes)));
+ foreach ($result as $node) {
+ $node->last_activity = $nodes[$node->nid]->last_activity;
+ $nodes[$node->nid] = $node;
}
- $rows[] = array(
- check_plain(node_get_types('name', $node->type)),
- l($node->title, "node/$node->nid") . ' ' . theme('mark', node_mark($node->nid, $node->changed)),
- theme('username', $node),
- array('class' => 'replies', 'data' => $comments),
- t('!time ago', array('!time' => format_interval(REQUEST_TIME - $node->last_updated)))
- );
- }
+ // Finally display the data
+ $rows = array();
+ foreach ($nodes as $node) {
+ // Determine the number of comments:
+ $comments = 0;
+ if ($node->comment_count) {
+ $comments = $node->comment_count;
+
+ if ($new = comment_num_new($node->nid)) {
+ $comments .= '
';
+ $comments .= l(format_plural($new, '1 new', '@count new'), 'node/'. $node->nid, array('fragment' => 'new'));
+ }
+ }
- if (!$rows) {
+ $rows[] = array(
+ check_plain(node_get_types('name', $node->type)),
+ l($node->title, "node/$node->nid") .' '. theme('mark', node_mark($node->nid, $node->changed)),
+ theme('username', $node),
+ array('class' => 'replies', 'data' => $comments),
+ t('!time ago', array('!time' => format_interval(time() - $node->last_activity)))
+ );
+ }
+ }
+ else {
$rows[] = array(array('data' => t('No posts available.'), 'colspan' => '5'));
}
Index: modules/translation/translation.api.php
===================================================================
RCS file: modules/translation/translation.api.php
diff -N modules/translation/translation.api.php
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ modules/translation/translation.api.php 22 Jan 2009 12:52:48 -0000
@@ -0,0 +1,17 @@
+/**
+ * Respond to changes in a node's translation set.
+ *
+ * The node object is extended with a 'translation_change' array containing
+ * both the old and new tnid.
+ *
+ * @param $node
+ * The node being acted upon.
+ */
+function hook_nodeapi_translation_change($node) {
+ $update = db_update('my_table')
+ ->fields(array(
+ 'tnid' => $node->translation_change['new_tnid'],
+ ))
+ ->condition('tnid', $node->tnid)
+ ->execute();
+}