/[drupal]/contributions/modules/recent_changes/recent_changes.module
ViewVC logotype

Diff of /contributions/modules/recent_changes/recent_changes.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.4, Wed Mar 7 03:20:03 2007 UTC revision 1.5, Wed Apr 4 23:54:17 2007 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: recent_changes.module,v 1.3 2007/02/23 01:22:31 roetzi Exp $  // $Id: recent_changes.module,v 1.4 2007/03/07 03:20:03 roetzi Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 41  function recent_changes_menu($may_cache) Line 41  function recent_changes_menu($may_cache)
41  function recent_changes_view() {  function recent_changes_view() {
42    // Print feed if requested or display normal page otherwise    // Print feed if requested or display normal page otherwise
43    if (arg(1) == 'feed') {    if (arg(1) == 'feed') {
44      return recent_changes_feed();      return recent_changes_feed(arg(2));
45    }    }
46    else {    else {
47      return recent_changes_page();      return recent_changes_page();
# Line 54  function recent_changes_view() { Line 54  function recent_changes_view() {
54  function recent_changes_page() {  function recent_changes_page() {
55    $output = '';    $output = '';
56    
57    // Add rss feed and stylesheet to page.    // Add stylesheet for page.
   drupal_add_feed(url('recent_changes/feed', null, null, true), t('recent changes of !site', array('!site' => variable_get('site_name', 'drupal'))));  
58    drupal_add_css(drupal_get_path('module', 'recent_changes').'/recent_changes.css');    drupal_add_css(drupal_get_path('module', 'recent_changes').'/recent_changes.css');
59    
60    // Show filter if enabled    // Show filter if enabled
# Line 74  function recent_changes_page() { Line 73  function recent_changes_page() {
73      $node_type = '';      $node_type = '';
74    }    }
75    
76      // Add feed
77      if ($node_type) {
78        // Filter by node type is active
79        drupal_add_feed(url('recent_changes/feed/' . $node_type, null, null, true), t('recent changes of !site filtered by node type !node_type', array('!site' => variable_get('site_name', 'drupal'), '!node_type' => $node_type)));
80      }
81      else {
82        // No filtering
83        drupal_add_feed(url('recent_changes/feed', null, null, true), t('recent changes of !site', array('!site' => variable_get('site_name', 'drupal'))));
84      }
85    
86    // Header for recent changes table.    // Header for recent changes table.
87    $header = array(    $header = array(
88      '', // operations      '', // operations
# Line 87  function recent_changes_page() { Line 96  function recent_changes_page() {
96    
97    // Build SQL query depending on node type filter and comments filter.    // Build SQL query depending on node type filter and comments filter.
98    
   // Node type filter  
   $node_type_query = ($node_type ? " WHERE n.type = '$node_type'" : '');  
   
99    // Query to get all revisions of the selected node types    // Query to get all revisions of the selected node types
100    $revisions_query = "SELECT n.type, r.nid, r.vid, 0 AS cid, r.title, r.timestamp, r.log, r.uid, u.name FROM {node_revisions} r LEFT JOIN {node} n ON r.nid = n.nid LEFT JOIN {users} u ON r.uid = u.uid$node_type_query";    $revisions_query = recent_changes_revisions_query($node_type);
   
101    if (module_exists('comment') && $show_comments) {    if (module_exists('comment') && $show_comments) {
102      // Query to get all comments of the selected node types      // Query to get all comments of the selected node types
103      $comments_query = "SELECT n.type, c.nid, 0 AS vid, c.cid, n.title, c.timestamp, c.subject AS log, c.uid, c.name FROM {comments} c LEFT JOIN {node} n ON c.nid = n.nid$node_type_query";      $comments_query = recent_changes_comments_query($node_type);
104      // Final query as a combination of node revisions and comments      // Final query as a combination of node revisions and comments
105      $sql = "($revisions_query) UNION ALL ($comments_query) ORDER BY timestamp DESC";      $sql = "($revisions_query) UNION ALL ($comments_query) ORDER BY timestamp DESC";
106      if ($node_type) {      if ($node_type) {
107        // Number of total nodes and comments if a specific node type is selected        // Number of total nodes and comments if a specific node type is selected
108        $count_sql = "SELECT (SELECT COUNT(*) FROM {node_revisions} r JOIN {node} n ON r.nid = n.nid $node_type_query) + (SELECT COUNT(*) FROM {comments} c LEFT JOIN {node} n ON c.nid = n.nid $node_type_query)";        $count_sql = "SELECT (SELECT COUNT(*) FROM {node_revisions} r JOIN {node} n ON r.nid = n.nid WHERE n.type = '" . check_plain($node_type) . "') + (SELECT COUNT(*) FROM {comments} c LEFT JOIN {node} n ON c.nid = n.nid WHERE n.type = '" . check_plain($node_type) . "')";
109      }      }
110      else {      else {
111        // Number of total nodes and comments if all node types are shown        // Number of total nodes and comments if all node types are shown
112        $count_sql = "SELECT (SELECT COUNT(*) FROM {node_revisions}) + (SELECT COUNT(*) FROM {comments})";        $count_sql = "SELECT (SELECT COUNT(*) FROM {node_revisions}) + (SELECT COUNT(*) FROM {comments})";
113      }      }
114        // Check MySQL version
115        $version = db_version();
116        if ($GLOBALS['db_type'] == 'mysql' && version_compare($version, '4.1.0') < 0) {
117          // MySQL before 4.1.0 cannot execute the subqueries. Just ignore comments for total count.
118          if ($node_type) {
119            // Number of total node revisions if a specific node type is selected
120            $count_sql = "SELECT COUNT(*) FROM {node_revisions} r JOIN {node} n ON r.nid = n.nid WHERE n.type = '" . check_plain($node_type) . "'";
121          }
122          else {
123            // Number of total node revisions if all node types are shown
124            $count_sql = "SELECT COUNT(*) FROM {node_revisions}";
125          }
126        }
127    }    }
128    else {    else {
129      // Query to select all node revisions      // Query to select all node revisions
130      $sql = "$revisions_query ORDER BY timestamp DESC";      $sql = "$revisions_query ORDER BY timestamp DESC";
131      if ($node_type) {      if ($node_type) {
132        // Number of total nodes if a specific node type is selected        // Number of total node revisions if a specific node type is selected
133        $count_sql = "SELECT COUNT(*) FROM {node_revisions} r JOIN {node} n ON r.nid = n.nid $node_type_query";        $count_sql = "SELECT COUNT(*) FROM {node_revisions} r JOIN {node} n ON r.nid = n.nid WHERE n.type = '" . check_plain($node_type) . "'";
134      }      }
135      else {      else {
136        // Number of total nodes if all node types are shown        // Number of total node revisions if all node types are shown
137        $count_sql = "SELECT COUNT(*) FROM {node_revisions}";        $count_sql = "SELECT COUNT(*) FROM {node_revisions}";
138      }      }
139    }    }
140    
141    // SQL result provided by pager implementation    // SQL result provided by pager implementation
142    $result = pager_query($sql, recent_changes_count_per_page(), 0, $count_sql);    $result = pager_query($sql, recent_changes_count_per_page(), 0, $count_sql);
143    
# Line 250  function recent_changes_filter_form() { Line 269  function recent_changes_filter_form() {
269  /**  /**
270   * RSS feed of recent changes.   * RSS feed of recent changes.
271   */   */
272  function recent_changes_feed() {  function recent_changes_feed($node_type = NULL) {
273    global $base_url;    global $base_url;
274    
275    // Select all node revisions (no filter)    // Query to get all revisions of the selected node types
276    $revisions_query = "SELECT r.nid, r.vid, 0 AS cid, r.title, r.timestamp, r.log, r.uid, u.name FROM {node_revisions} r LEFT JOIN {users} u ON r.uid = u.uid";    $revisions_query = recent_changes_revisions_query($node_type);
277    if (module_exists('comment')) {    if (module_exists('comment')) {
278      // Select all comments      // Query to get all comments of the selected node types
279      $comments_query = "SELECT c.nid, 0 AS vid, c.cid, n.title, c.timestamp, '' AS log, c.uid, c.name FROM {comments} c LEFT JOIN {node} n ON c.nid = n.nid";      $comments_query = recent_changes_comments_query($node_type);
280      // Final query      // Final query as a combination of node revisions and comments
281      $sql = "($revisions_query) UNION ALL ($comments_query) ORDER BY timestamp DESC LIMIT %d";      $sql = "($revisions_query) UNION ALL ($comments_query) ORDER BY timestamp DESC LIMIT %d";
282    }    }
283    else {    else {
     // Final query  
284      $sql = "$revisions_query ORDER BY timestamp DESC LIMIT %d";      $sql = "$revisions_query ORDER BY timestamp DESC LIMIT %d";
285    }    }
286    $result = db_query($sql, variable_get('feed_default_items', 10));    $result = db_query($sql, variable_get('feed_default_items', 10));
287    
288    // Type of RSS feed: 'title', 'teaser', 'full'    // Type of RSS feed: 'title', 'teaser', 'full'
289    $item_length = variable_get('feed_item_length', 'teaser');    $item_length = variable_get('feed_item_length', 'teaser');
290    // Is node teaser displayed (only used if $item_length not 'title')    // Is node teaser displayed (only used if $item_length not 'title')
# Line 325  function recent_changes_feed() { Line 344  function recent_changes_feed() {
344        $item_text = '';        $item_text = '';
345        if ($old_entry->vid && module_exists('diff')) {        if ($old_entry->vid && module_exists('diff')) {
346          // If diff module exists, put a diff in the RSS feed          // If diff module exists, put a diff in the RSS feed
347          $item_text  = '<table style="width:100%">';          $rows = array();
348          $item_text .= '<thead><tr><th></th><th>Revision of '. format_date($old_entry->timestamp) .'</th><th></th><th>Revision of '. format_date($item->timestamp) .'</th></tr></thead>';          $header = array(
349          $item_text .= _diff_table_body($old_node, $node);            '',
350          $item_text .= '</table>';            t('Revision of %date', array('%date' => format_date($old_entry->timestamp))),
351              '',
352              t('Revision of %date', array('%date' => format_date($item->timestamp)))
353            );
354            $rows = _diff_body_rows($old_node, $node);
355            $item_text .= theme('table', $header, $rows, array('style' => 'width:100%'));
356          // Replace css classes from diff with actual style elements since we cannot reference          // Replace css classes from diff with actual style elements since we cannot reference
357          // a css file from the rss feed.          // a css file from the rss feed.
358          $patterns = array(          $patterns = array(
# Line 391  function recent_changes_feed() { Line 415  function recent_changes_feed() {
415    // RSS channel information    // RSS channel information
416    $channel = array(    $channel = array(
417      'version'     => '2.0',      'version'     => '2.0',
     'title'       => variable_get('site_name', 'drupal') .' - ' . t('Recent changes'),  
418      'link'        => $base_url,      'link'        => $base_url,
419      'description' => t('Recent changes of nodes and comments on %site', array('%site' => variable_get('site_name', 'drupal'))),      'description' => t('Recent changes of nodes and comments on %site', array('%site' => variable_get('site_name', 'drupal'))),
420      'language'    => $GLOBALS['locale'],      'language'    => $GLOBALS['locale'],
421    );    );
422      if ($node_type) {
423        $channel['title'] = variable_get('site_name', 'drupal') .' - ' . t('Recent changes filtered by !node_type', array('!node_type' => $node_type));
424      }
425      else {
426        $channel['title'] = variable_get('site_name', 'drupal') .' - ' . t('Recent changes');
427      }
428    // Construct actual RSS feed text    // Construct actual RSS feed text
429    $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";    $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
430    $output .= "<rss version=\"". $channel["version"] . "\" xml:base=\"". $base_url ."\" ". implode(' ', $namespaces) .">\n";    $output .= "<rss version=\"". $channel["version"] . "\" xml:base=\"". $base_url ."\" ". implode(' ', $namespaces) .">\n";
# Line 441  function recent_changes_show_comments($v Line 470  function recent_changes_show_comments($v
470    }    }
471    variable_set('recent_changes_show_comments', $value);    variable_set('recent_changes_show_comments', $value);
472  }  }
473    
474    
475    /**
476     * SQL query for all node revisions.
477     */
478    function recent_changes_revisions_query($node_type = NULL) {
479      $result = 'SELECT n.type, r.nid, r.vid, 0 AS cid, r.title, r.timestamp, r.log, r.uid, u.name FROM {node_revisions} r LEFT JOIN {node} n ON r.nid = n.nid LEFT JOIN {users} u ON r.uid = u.uid';
480      // Change for query if access right restrictions apply
481      list($join, $where, $distinct) = _db_rewrite_sql($result, 'r', 'nid');
482      if ($node_type) {
483        // Filter on node type
484        $where .= " AND n.type = '" . check_plain($node_type) . "'";
485      }
486      if ($join) {
487        $result .= ' ' . $join;
488      }
489      if ($where) {
490        $result .= ' WHERE ' . $where;
491      }
492      return $result;
493    }
494    
495    /**
496     * SQL query for all comments.
497     */
498    function recent_changes_comments_query($node_type = NULL) {
499      $result = 'SELECT n.type, c.nid, 0 AS vid, c.cid, n.title, c.timestamp, c.subject AS log, c.uid, c.name FROM {comments} c LEFT JOIN {node} n ON c.nid = n.nid';
500      list($join, $where, $distinct) = _db_rewrite_sql($result, 'c', 'nid');
501      if ($node_type) {
502        $where .= " AND n.type = '" . check_plain($node_type) . "'";
503      }
504      if ($join) {
505        $result .= ' ' . $join;
506      }
507      if ($where) {
508        $result .= ' WHERE ' . $where;
509      }
510      return $result;
511    }

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

  ViewVC Help
Powered by ViewVC 1.1.2