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

Contents of /contributions/modules/intervalquery/intervalquery.module

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


Revision 1.6 - (show annotations) (download) (as text)
Mon Dec 31 05:24:33 2007 UTC (22 months, 3 weeks ago) by larham
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Changes since 1.5: +2 -5 lines
File MIME type: text/x-php
cosmetic
1 <?php
2
3 // $Id: $
4
5 /**
6 * @file intervalquery module
7 * The "intervalquery" module provides an API for requesting
8 * info created or modified in a given interval:
9 * between start/end dates. It is licensed under GPLv2.
10 *
11 * @author Larry Hamel, Codeguild.com
12 */
13
14 /**
15 * Implementation of hook_help.
16 * Provide a few help tips
17 */
18 function intervalquery_help($section) {
19 switch ($section) {
20 case 'admin/modules#name' :
21 $ret = 'Intervalquery XML-RPC module';
22 break;
23 case 'admin/modules#description' :
24 $ret = 'Module for answering an XML-RPC query for information created or updated in a given interval';
25 break;
26 case 'admin/help#rpc' :
27 $ret = 'Help for the Intervalquery XML-RPC module';
28 break;
29 }
30 return $ret;
31 }
32
33 /**
34 * Implementation of hook_settings.
35 * Define a drupal setting for the allowed IP address
36 */
37 function intervalquery_settings() {
38 $form['rpc_allowed_ip'] = array (
39 '#type' => 'textfield',
40 '#title' => t('Allowed IP address in canonical format'),
41 '#default_value' => variable_get('rpc_allowed_ip', '127.0.0.1'),
42 '#size' => 15,
43 '#maxlength' => 15,
44 '#description' => t('RPC calls to this module from any other IP will be rejected. ')
45 . t('This does NOT affect other XML-RPC modules.'));
46
47 return $form;
48 }
49
50 function intervalquery_nodes($params) {
51 $from = _intervalquery_get_from($params);
52 $until = _intervalquery_get_until($params);
53
54 return array (
55 'nodes' => _interval_query_nodes($from, $until)
56 );
57 }
58
59 function intervalquery_comments($params) {
60 $from = _intervalquery_get_from($params);
61 $until = _intervalquery_get_until($params);
62
63 return array (
64 'comments' => _interval_query_comments($from, $until)
65 );
66 }
67
68 function intervalquery_users($params) {
69 $from = _intervalquery_get_from($params);
70 $until = _intervalquery_get_until($params);
71
72 return array (
73 'users' => _interval_query_users($from, $until)
74 );
75 }
76
77 function intervalquery_all($params) {
78 $from = _intervalquery_get_from($params);
79 $until = _intervalquery_get_until($params);
80
81 return array (
82 'users' => _interval_query_users($from, $until),
83 'nodes' => _interval_query_nodes($from, $until),
84 'comments' => _interval_query_comments($from, $until),
85 );
86 }
87
88 function _intervalquery_get_from($params) {
89 $from = 0;
90
91 // did we get from param?
92 $fromparam = $params['from'];
93 if ( $fromparam ) {
94 // convert from iso to unix time
95 $from = intervalquery_iso2timestamp($fromparam);
96 }
97
98 return $from;
99 }
100
101 function _intervalquery_get_until($params) {
102 $until = 0;
103
104 // did we get until param?
105 $untilparam = $params['until'];
106 if ( $untilparam ) {
107 // convert from iso to unix time
108 $until = intervalquery_iso2timestamp($untilparam);
109 }
110
111 return $until;
112 }
113
114
115 function _interval_query_nodes($from, $until) {
116 $package = array();
117
118 $package['from'] = intervalquery_timestamp2iso($from);
119 $package['until'] = intervalquery_timestamp2iso($until);
120
121 $count = db_result(db_query("SELECT COUNT(n.nid) FROM {node} n WHERE n.changed >= %d AND n.changed <= %d", $from, $until));
122 $package['count'] = "$count";
123
124 $nodes = array();
125 if ($rows = db_query("SELECT * FROM {node} n WHERE n.changed >= %d AND n.changed <= %d", $from, $until)) {
126 while ($anode = db_fetch_object($rows)) {
127 // must load to get latest body
128 // TODO get latest body from join in query above !!!!!!!!!!!!
129 $mynode = node_load($anode->nid);
130
131 $nid = $anode->nid;
132 $body = $mynode->body; // get latest version;
133
134 // must convert to array so that we can add on body to node metadata
135 $nodeasarray = (array) $anode;
136 $nodeasarray['body'] = $body;
137
138 // convert dates
139 $nodeasarray['created'] = intervalquery_timestamp2iso($nodeasarray['created']);
140 $nodeasarray['changed'] = intervalquery_timestamp2iso($nodeasarray['changed']);
141
142 $nodes[$nid] = $nodeasarray;
143 }
144 }
145
146 $package['nodes'] = $nodes;
147 return $package;
148 }
149
150 function _interval_query_comments($from, $until) {
151 $package = array();
152
153 $package['from'] = intervalquery_timestamp2iso($from);
154 $package['until'] = intervalquery_timestamp2iso($until);
155
156 $count = db_result(db_query("SELECT COUNT(c.cid) FROM {comments} c WHERE c.timestamp >= %d AND c.timestamp <= %d", $from, $until));
157 $package['count'] = "$count";
158
159 $comments = array();
160 if ($rows = db_query("SELECT * FROM {comments} c WHERE c.timestamp >= %d AND c.timestamp <= %d", $from, $until)) {
161 while ($item = db_fetch_object($rows)) {
162 $asarray = (array) $item;
163 $asarray['timestamp'] = intervalquery_timestamp2iso($asarray['timestamp']);
164 $comments[$item->cid] = $asarray;
165 }
166 }
167
168 $package['comments'] = $comments;
169 return $package;
170 }
171
172 function _interval_query_users($from, $until) {
173 $package = array();
174
175 $package['from'] = intervalquery_timestamp2iso($from);
176 $package['until'] = intervalquery_timestamp2iso($until);
177
178 $count = db_result(db_query("SELECT COUNT(u.uid) FROM {users} u WHERE u.created >= %d AND u.created <= %d", $from, $until));
179 $package['count'] = "$count";
180
181 $users = array();
182 if ($rows = db_query("SELECT * FROM {users} u WHERE u.created >= %d AND u.created <= %d", $from, $until)) {
183 while ($item = db_fetch_object($rows)) {
184 $asarray = (array) $item;
185 $asarray['created'] = intervalquery_timestamp2iso($asarray['created']);
186 $asarray['pass'] = "";
187 $users[$item->cid] = $asarray;
188 }
189 }
190
191 $package['comments'] = $users;
192 return $package;
193 }
194
195 /**
196 * Implementation of hook_xmlrpc
197 */
198 function intervalquery_xmlrpc() {
199 // TODO get var setting
200 // if (variable_get('rpc_allowed_ip', '') != $_SERVER['REMOTE_ADDR']) {
201 if ('127.0.0.1' == $_SERVER['REMOTE_ADDR']) {
202
203 $ret = array (
204 'intervalquery.nodes' => 'intervalquery_nodes',
205 'intervalquery.comments' => 'intervalquery_comments',
206 'intervalquery.users' => 'intervalquery_users',
207 'intervalquery.all' => 'intervalquery_all'
208 );
209
210 } else {
211 $ret = array (
212 'intervalquery.nodes' => 'intervalquery_rpc_ip_ban',
213 'intervalquery.comments' => 'intervalquery_rpc_ip_ban',
214 'intervalquery.users' => 'intervalquery_rpc_ip_ban',
215 'intervalquery.all' => 'intervalquery_rpc_ip_ban'
216 );
217 }
218
219 return $ret;
220 }
221
222 /**
223 * Return an error message.
224 */
225 function intervalquery_rpc_ip_ban() {
226 return t("This address is not allowed to access this RPC service");
227 }
228
229 /**
230 * convert timestamp to iso 8601
231 */
232 function intervalquery_timestamp2iso($timestamp) {
233 return date('Y-m-d\TH:i:sO', $timestamp);
234 }
235
236
237 /**
238 * convert (extended) ISO 8601 compliant date string to unix timestamp
239 *
240 * @param string $datestr ISO 8601 compliant date string
241 */
242 function intervalquery_iso2timestamp($datestr){
243 $eregStr =
244 '([0-9]{4})-'. // centuries & years CCYY-
245 '([0-9]{2})-'. // months MM-
246 '([0-9]{2})'. // days DD
247 'T'. // separator T
248 '([0-9]{2}):'. // hours hh:
249 '([0-9]{2}):'. // minutes mm:
250 '([0-9]{2})(\.[0-9]+)?'. // seconds ss.ss...
251 '(Z|[+\-][0-9]{2}:?[0-9]{2})?'; // Z to indicate UTC, -/+HH:MM:SS.SS... for local tz's
252 if(ereg($eregStr,$datestr,$regs)){
253 // not utc
254 if($regs[8] != 'Z'){
255 $op = substr($regs[8],0,1);
256 $h = substr($regs[8],1,2);
257 $m = substr($regs[8],strlen($regs[8])-2,2);
258 if($op == '-'){
259 $regs[4] = $regs[4] + $h;
260 $regs[5] = $regs[5] + $m;
261 } elseif($op == '+'){
262 $regs[4] = $regs[4] - $h;
263 $regs[5] = $regs[5] - $m;
264 }
265 }
266 return gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
267 } else {
268 return false;
269 }
270 }

  ViewVC Help
Powered by ViewVC 1.1.2