Projet

Général

Profil

Paste
Télécharger (12,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / tracker / tracker.module @ 76597ebf

1
<?php
2

    
3
/**
4
 * @file
5
 * Tracks recent content posted by a user or users.
6
 */
7

    
8
/**
9
 * Implements hook_help().
10
 */
11
function tracker_help($path, $arg) {
12
  switch ($path) {
13
    case 'admin/help#tracker':
14
      $output = '<h3>' . t('About') . '</h3>';
15
      $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>';
16
      $output .= '<h3>' . t('Uses') . '</h3>';
17
      $output .= '<dl>';
18
      $output .= '<dt>' . t('Navigation') . '</dt>';
19
      $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>';
20
      $output .= '<dt>' . t('Tracking new and updated site content') . '</dt>';
21
      $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>';
22
      $output .= '<dt>' . t('Tracking user-specific content') . '</dt>';
23
      $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>';
24
      $output .= '</dl>';
25
      return $output;
26
  }
27
}
28

    
29
/**
30
 * Implements hook_menu().
31
 */
32
function tracker_menu() {
33
  $items['tracker'] = array(
34
    'title' => 'Recent content',
35
    'page callback' => 'tracker_page',
36
    'access arguments' => array('access content'),
37
    'weight' => 1,
38
    'file' => 'tracker.pages.inc',
39
  );
40
  $items['tracker/all'] = array(
41
    'title' => 'All recent content',
42
    'type' => MENU_DEFAULT_LOCAL_TASK,
43
  );
44
  $items['tracker/%user_uid_optional'] = array(
45
    'title' => 'My recent content',
46
    'page callback' => 'tracker_page',
47
    'access callback' => '_tracker_myrecent_access',
48
    'access arguments' => array(1),
49
    'page arguments' => array(1),
50
    'type' => MENU_LOCAL_TASK,
51
    'file' => 'tracker.pages.inc',
52
  );
53

    
54
  $items['user/%user/track'] = array(
55
    'title' => 'Track',
56
    'page callback' => 'tracker_page',
57
    'page arguments' => array(1, TRUE),
58
    'access callback' => '_tracker_user_access',
59
    'access arguments' => array(1),
60
    'type' => MENU_LOCAL_TASK,
61
    'file' => 'tracker.pages.inc',
62
  );
63
  $items['user/%user/track/content'] = array(
64
    'title' => 'Track content',
65
    'type' => MENU_DEFAULT_LOCAL_TASK,
66
  );
67

    
68
  return $items;
69
}
70

    
71
/**
72
 * Implements hook_cron().
73
 *
74
 * Updates tracking information for any items still to be tracked. The variable
75
 * 'tracker_index_nid' is set to ((the last node ID that was indexed) - 1) and
76
 * used to select the nodes to be processed. If there are no remaining nodes to
77
 * process, 'tracker_index_nid' will be 0.
78
 */
79
function tracker_cron() {
80
  $max_nid = variable_get('tracker_index_nid', 0);
81
  $batch_size = variable_get('tracker_batch_size', 1000);
82
  if ($max_nid > 0) {
83
    $last_nid = FALSE;
84
    $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'));
85

    
86
    $count = 0;
87

    
88
    foreach ($result as $row) {
89
      // Calculate the changed timestamp for this node.
90
      $changed = _tracker_calculate_changed($row->nid);
91

    
92
      // Remove existing data for this node.
93
      db_delete('tracker_node')
94
        ->condition('nid', $row->nid)
95
        ->execute();
96
      db_delete('tracker_user')
97
        ->condition('nid', $row->nid)
98
        ->execute();
99

    
100
      // Insert the node-level data.
101
      db_insert('tracker_node')
102
        ->fields(array(
103
          'nid' => $row->nid,
104
          'published' => $row->status,
105
          'changed' => $changed,
106
        ))
107
        ->execute();
108

    
109
      // Insert the user-level data for the node's author.
110
      db_insert('tracker_user')
111
        ->fields(array(
112
          'nid' => $row->nid,
113
          'published' => $row->status,
114
          'changed' => $changed,
115
          'uid' => $row->uid,
116
        ))
117
        ->execute();
118

    
119
      $query = db_select('comment', 'c', array('target' => 'slave'));
120
      // Force PostgreSQL to do an implicit cast by adding 0.
121
      $query->addExpression('0 + :changed', 'changed', array(':changed' => $changed));
122
      $query->addField('c', 'status', 'published');
123
      $query
124
        ->distinct()
125
        ->fields('c', array('uid', 'nid'))
126
        ->condition('c.nid', $row->nid)
127
        ->condition('c.uid', $row->uid, '<>')
128
        ->condition('c.status', COMMENT_PUBLISHED);
129

    
130
      // Insert the user-level data for the commenters (except if a commenter
131
      // is the node's author).
132
      db_insert('tracker_user')
133
        ->from($query)
134
        ->execute();
135

    
136
      // Note that we have indexed at least one node.
137
      $last_nid = $row->nid;
138

    
139
      $count++;
140
    }
141

    
142
    if ($last_nid !== FALSE) {
143
      // Prepare a starting point for the next run.
144
      variable_set('tracker_index_nid', $last_nid - 1);
145

    
146
      watchdog('tracker', 'Indexed %count content items for tracking.', array('%count' => $count));
147
    }
148
    else {
149
      // If all nodes have been indexed, set to zero to skip future cron runs.
150
      variable_set('tracker_index_nid', 0);
151
    }
152
  }
153
}
154

    
155
/**
156
 * Access callback for tracker/%user_uid_optional.
157
 */
158
function _tracker_myrecent_access($account) {
159
  // This path is only allowed for authenticated users looking at their own content.
160
  return $account->uid && ($GLOBALS['user']->uid == $account->uid) && user_access('access content');
161
}
162

    
163
/**
164
 * Access callback for user/%user/track.
165
 */
166
function _tracker_user_access($account) {
167
  return user_view_access($account) && user_access('access content');
168
}
169

    
170
/**
171
 * Implements hook_node_insert().
172
 *
173
 * Adds new tracking information for this node since it's new.
174
 */
175
function tracker_node_insert($node, $arg = 0) {
176
  _tracker_add($node->nid, $node->uid, $node->changed);
177
}
178

    
179
/**
180
 * Implements hook_node_update().
181
 *
182
 * Adds tracking information for this node since it's been updated.
183
 */
184
function tracker_node_update($node, $arg = 0) {
185
  _tracker_add($node->nid, $node->uid, $node->changed);
186
}
187

    
188
/**
189
 * Implements hook_node_delete().
190
 *
191
 * Deletes tracking information for a node.
192
 */
193
function tracker_node_delete($node, $arg = 0) {
194
  db_delete('tracker_node')
195
    ->condition('nid', $node->nid)
196
    ->execute();
197
  db_delete('tracker_user')
198
    ->condition('nid', $node->nid)
199
    ->execute();
200
}
201

    
202
/**
203
 * Implements hook_comment_update().
204
 *
205
 * Comment module doesn't call hook_comment_unpublish() when saving individual
206
 * comments so we need to check for those here.
207
 */
208
function tracker_comment_update($comment) {
209
  // comment_save() calls hook_comment_publish() for all published comments
210
  // so we need to handle all other values here.
211
  if ($comment->status != COMMENT_PUBLISHED) {
212
    _tracker_remove($comment->nid, $comment->uid, $comment->changed);
213
  }
214
}
215

    
216
/**
217
 * Implements hook_comment_publish().
218
 *
219
 * This actually handles the insert and update of published nodes since
220
 * comment_save() calls hook_comment_publish() for all published comments.
221
 */
222
function tracker_comment_publish($comment) {
223
  _tracker_add($comment->nid, $comment->uid, $comment->changed);
224
}
225

    
226
/**
227
 * Implements hook_comment_unpublish().
228
 */
229
function tracker_comment_unpublish($comment) {
230
  _tracker_remove($comment->nid, $comment->uid, $comment->changed);
231
}
232

    
233
/**
234
 * Implements hook_comment_delete().
235
 */
236
function tracker_comment_delete($comment) {
237
  _tracker_remove($comment->nid, $comment->uid, $comment->changed);
238
}
239

    
240
/**
241
 * Updates indexing tables when a node is added, updated, or commented on.
242
 *
243
 * @param $nid
244
 *   A node ID.
245
 * @param $uid
246
 *   The node or comment author.
247
 * @param $changed
248
 *   The node updated timestamp or comment timestamp.
249
 */
250
function _tracker_add($nid, $uid, $changed) {
251
  $node = db_query('SELECT nid, status, uid, changed FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
252

    
253
  // Adding a comment can only increase the changed timestamp, so our
254
  // calculation here is simple.
255
  $changed = max($node->changed, $changed);
256

    
257
  // Update the node-level data.
258
  db_merge('tracker_node')
259
    ->key(array('nid' => $nid))
260
    ->fields(array(
261
      'changed' => $changed,
262
      'published' => $node->status,
263
    ))
264
    ->execute();
265

    
266
  // Create or update the user-level data, first for the user posting.
267
  db_merge('tracker_user')
268
    ->key(array(
269
      'nid' => $nid,
270
      'uid' => $uid,
271
    ))
272
    ->fields(array(
273
      'changed' => $changed,
274
      'published' => $node->status,
275
    ))
276
    ->execute();
277
  // Update the times for all the other users tracking the post.
278
  db_update('tracker_user')
279
    ->condition('nid', $nid)
280
    ->fields(array(
281
      'changed' => $changed,
282
      'published' => $node->status,
283
    ))
284
    ->execute();
285
}
286

    
287
/**
288
 * Determines the max timestamp between $node->changed and the last comment.
289
 *
290
 * @param $nid
291
 *   A node ID.
292
 *
293
 * @return
294
 *  The $node->changed timestamp, or most recent comment timestamp, whichever
295
 *  is the greatest.
296
 */
297
function _tracker_calculate_changed($nid) {
298
  $changed = db_query('SELECT changed FROM {node} WHERE nid = :nid', array(':nid' => $nid), array('target' => 'slave'))->fetchField();
299
  $latest_comment = db_query_range('SELECT cid, changed FROM {comment} WHERE nid = :nid AND status = :status ORDER BY changed DESC', 0, 1, array(
300
    ':nid' => $nid,
301
    ':status' => COMMENT_PUBLISHED,
302
  ), array('target' => 'slave'))->fetchObject();
303
  if ($latest_comment && $latest_comment->changed > $changed) {
304
    $changed = $latest_comment->changed;
305
  }
306
  return $changed;
307
}
308

    
309
/**
310
 * Cleans up indexed data when nodes or comments are removed.
311
 *
312
 * @param $nid
313
 *  The node ID.
314
 * @param $uid
315
 *   The author of the node or comment.
316
 * @param $changed
317
 *   The last changed timestamp of the node.
318
 */
319
function _tracker_remove($nid, $uid = NULL, $changed = NULL) {
320
  $node = db_query('SELECT nid, status, uid, changed FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
321

    
322
  // The user only keeps his or her subscription if both of the following are true:
323
  //   (1) The node exists.
324
  //   (2) The user is either the node author or has commented on the node.
325
  $keep_subscription = FALSE;
326

    
327
  if ($node) {
328
    // Self-authorship is one reason to keep the user's subscription.
329
    $keep_subscription = ($node->uid == $uid);
330

    
331
    // Comments are a second reason to keep the user's subscription.
332
    if (!$keep_subscription) {
333
      // Check if the user has commented at least once on the given nid.
334
      $keep_subscription = db_query_range('SELECT COUNT(*) FROM {comment} WHERE nid = :nid AND uid = :uid AND status = :status', 0, 1, array(
335
        ':nid' => $nid,
336
        ':uid' => $uid,
337
        ':status' => COMMENT_PUBLISHED,
338
      ))->fetchField();
339
    }
340

    
341
    // If we haven't found a reason to keep the user's subscription, delete it.
342
    if (!$keep_subscription) {
343
      db_delete('tracker_user')
344
        ->condition('nid', $nid)
345
        ->condition('uid', $uid)
346
        ->execute();
347
    }
348

    
349
    // Now we need to update the (possibly) changed timestamps for other users
350
    // and the node itself.
351
    // We only need to do this if the removed item has a timestamp that equals
352
    // or exceeds the listed changed timestamp for the node.
353
    $tracker_node = db_query('SELECT nid, changed FROM {tracker_node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
354
    if ($tracker_node && $changed >= $tracker_node->changed) {
355
      // If we're here, the item being removed is *possibly* the item that
356
      // established the node's changed timestamp.
357

    
358
      // We just have to recalculate things from scratch.
359
      $changed = _tracker_calculate_changed($nid);
360

    
361
      // And then we push the out the new changed timestamp to our denormalized
362
      // tables.
363
      db_update('tracker_node')
364
        ->fields(array(
365
          'changed' => $changed,
366
          'published' => $node->status,
367
        ))
368
        ->condition('nid', $nid)
369
        ->execute();
370
      db_update('tracker_node')
371
        ->fields(array(
372
          'changed' => $changed,
373
          'published' => $node->status,
374
        ))
375
        ->condition('nid', $nid)
376
        ->execute();
377
    }
378
  }
379
  else {
380
    // If the node doesn't exist, remove everything.
381
    db_delete('tracker_node')
382
      ->condition('nid', $nid)
383
      ->execute();
384
    db_delete('tracker_user')
385
      ->condition('nid', $nid)
386
      ->execute();
387
  }
388
}