/[drupal]/contributions/modules/bot/bot_drupal/bot_drupal.module
ViewVC logotype

Contents of /contributions/modules/bot/bot_drupal/bot_drupal.module

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


Revision 1.8 - (show annotations) (download) (as text)
Fri Dec 29 01:02:23 2006 UTC (2 years, 11 months ago) by morbus
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
Changes since 1.7: +24 -9 lines
File MIME type: text/x-php
Issue metadata and IRC count per suggestions by ChrisKennedy.
1 <?php
2 // $Id: bot_drupal.module,v 1.7 2006/11/18 04:06:42 morbus Exp $
3
4 /**
5 * @file
6 * Enables various Drupal developer friendly actions.
7 */
8
9 /**
10 * Implementation of hook_help().
11 */
12 function bot_drupal_help($section) {
13 switch ($section) {
14 case 'irc:features':
15 return array(t('Drupal URLs'));
16 case 'irc:features#drupal_urls':
17 return t('Displays the title of drupal.org URLs; multiple URLs in a single message are acceptable. Also supports node IDs such as "#12345" or "4321", but only if that is the entirety of the message and the number is at least 4 digits. Popular and recent URLs are tracked at <!url>.', array('!url' => url('bot/drupal_urls', NULL, NULL, TRUE)));
18 case 'bot/drupal_urls':
19 return '<p>'.t('View the most recent and popular URLs mentioned in the Drupal IRC channels.').'</p>';
20 }
21 }
22
23 /**
24 * Implementation of hook_menu().
25 */
26 function bot_drupal_menu($may_cache) {
27 $items = array();
28
29 if ($may_cache) {
30 $items[] = array(
31 'access' => user_access('access content'),
32 'callback' => 'bot_drupal_urls_overview',
33 'description' => t('View the most recent and popular URLs mentioned in the Drupal IRC channels.'),
34 'path' => 'bot/drupal_urls',
35 'title' => t('Drupal URLs'),
36 );
37 }
38
39 return $items;
40 }
41
42 /**
43 * Listen for Drupal URLs or a node ID and respond with the title.
44 *
45 * @param $data
46 * The regular $data object prepared by the IRC library.
47 * @param $from_query
48 * Boolean; whether this was a queried request.
49 */
50 function bot_drupal_irc_msg_channel($data, $from_query = 0) {
51 $to = $from_query ? $data->nick : $data->channel;
52 $message = $data->message;
53
54 // this is a *.drupal.org URL or what appears to be a recent nid.
55 if (preg_match_all('/(http:\/\/(groups.)?drupal.org\/node\/\d+)/i', $data->message, $url_matches) ||
56 preg_match('/^#?(\d{4,})$/', $data->message, $url_matches)) { // #12345 or 5432.
57
58 // any numbers we fake into a URL for the match looping.
59 if (!is_array($url_matches[1]) && is_numeric($url_matches[1])) {
60 $url_matches[1] = array('http://drupal.org/node/'. $url_matches[1]);
61 }
62
63 // retrieve each URL.
64 foreach ($url_matches[1] as $url) {
65 $result = drupal_http_request($url);
66
67 // we'll always display a title, so grab that first for database storage.
68 preg_match('/<title>(.*?) \| (groups.)?drupal.org<\/title>/', $result->data, $title_match);
69 if (!$title_match[1]) { $title_match[1] = '<'.t('unable to determine title').'>'; }
70
71 // update with last seen, count, and title. times seen is displayed in messages.
72 $count = db_result(db_query('SELECT count FROM {bot_urls} WHERE url = "%s"', $url));
73 if ($count) { db_query('UPDATE {bot_urls} SET title = "%s", count = count + 1, last_seen = %d WHERE url = "%s"', decode_entities($title_match[1]), time(), $url);
74 } else { db_query('INSERT INTO {bot_urls} (url, title, count, last_seen) VALUES ("%s", "%s", %d, %d)', $url, decode_entities($title_match[1]), 1, time()); }
75 $count = $count ? $count : 1; // if this is the first time, set the count 1 for later display.
76 $count = format_plural($count, '1 IRC mention', '@count IRC mentions'); // said later display.
77 $message = "$url => ". decode_entities($title_match[1]) ." => $count";
78
79 // get some metadata about project issue URLs. tested as of 2006-12-28.
80 preg_match('/<td>Project:<\/td><td>(.*)<\/td>/', $result->data, $project_match);
81 if ($project_match[1]) { // we'll only do further matches if this is a project of some sort.
82 preg_match('/<td>Component:<\/td><td>(.*)<\/td>/', $result->data, $component_match);
83 preg_match('/<td>Priority:<\/td><td>(.*)<\/td>/', $result->data, $priority_match);
84 preg_match('/<td>Status:<\/td><td>(.*)<\/td>/', $result->data, $status_match);
85 $message = "$url => ". decode_entities($title_match[1]) .' => '. implode(', ', array($project_match[1], $component_match[1], $priority_match[1], $status_match[1], $count));
86 }
87
88 bot_message($data->channel, $message);
89 }
90 }
91 }
92
93 /**
94 * All responses are available via a query.
95 */
96 function bot_drupal_irc_msg_query($data) {
97 bot_drupal_irc_msg_channel($data, 1);
98 }
99
100 /**
101 * Display top ten lists of the most recent and popular.
102 */
103 function bot_drupal_urls_overview() {
104 $output = NULL; $headers = array(t('URL or Title'), t('#'), t('Last Seen'));
105 drupal_add_css(drupal_get_path('module', 'bot_drupal') .'/bot_drupal.css');
106
107 $output .= '<h2>'.t('Popular URLs in the last 14 days').'</h2>';
108 $results = pager_query('SELECT * FROM {bot_urls} WHERE last_seen >= %d ORDER BY count DESC, last_seen DESC', 10, 1, NULL, time() - 60*60*24*14);
109 $rows = array(); while ($result = db_fetch_object($results)) { // titles could possibly be blank.
110 $rows[] = array(array('data' => l($result->title ? $result->title : $result->url, $result->url), 'class' => 'title'),
111 array('data' => $result->count, 'class' => 'count'), array('data' => format_date($result->last_seen), 'class' => 'last_seen'));
112 } $output .= theme('table', $headers, $rows, array('id' => 'drupal_urls_popular')) . theme('pager', NULL, 10, 1);
113
114 $output .= '<h2>'.t('Most recent URLs').'</h2>';
115 $results = pager_query('SELECT * FROM {bot_urls} ORDER BY last_seen DESC', 10, 2);
116 $rows = array(); while ($result = db_fetch_object($results)) { // titles could possibly be blank.
117 $rows[] = array(array('data' => l($result->title ? $result->title : $result->url, $result->url), 'class' => 'title'),
118 array('data' => $result->count, 'class' => 'count'), array('data' => format_date($result->last_seen), 'class' => 'last_seen'));
119 } $output .= theme('table', $headers, $rows, array('id' => 'drupal_urls_recent')) . theme('pager', NULL, 10, 2);
120
121 return $output;
122 }
123

  ViewVC Help
Powered by ViewVC 1.1.2