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

Contents of /contributions/modules/feedapi_node_discussion/feedapi_node_discussion.module

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


Revision 1.11 - (show annotations) (download) (as text)
Mon May 5 18:51:48 2008 UTC (18 months, 3 weeks ago) by mustafau
Branch: MAIN
CVS Tags: HEAD
Changes since 1.10: +2 -1 lines
File MIME type: text/x-php
Operate on insert and update.
1 <?php
2 // $Id: feedapi_node_discussion.module,v 1.10 2008/05/05 18:38:01 mustafau Exp $
3
4 /**
5 * @file
6 */
7
8 /**
9 * Implementation of hook_help().
10 */
11 function feedapi_node_discussion_help($path, $arg) {
12 switch ($path) {
13 case 'admin/help#feedapi_node_discussion':
14 return '<p>'. t('Finds out who links to who between FeedAPI Node items.') .'</p>';
15 }
16 }
17
18 /**
19 * Implementation of hook_menu().
20 */
21 function feedapi_node_discussion_menu() {
22 $items['admin/settings/feedapi_node_discussion'] = array(
23 'title' => 'FeedAPI Node Discussion settings',
24 'description' => 'Control how FeedAPI Node Discussion works.',
25 'page callback' => 'drupal_get_form',
26 'page arguments' => array('feedapi_node_discussion_admin_settings'),
27 'access arguments' => array('administer site configuration'),
28 );
29
30 return $items;
31 }
32
33 /**
34 * Menu callback.
35 */
36 function feedapi_node_discussion_admin_settings() {
37 $options = array();
38 foreach (node_get_types() as $type => $name) {
39 $options[$type] = $name->name;
40 }
41
42 $form['discussion_content_types'] = array(
43 '#type' => 'checkboxes',
44 '#title' => t('Disable/Enable discussion for content types'),
45 '#default_value' => variable_get('discussion_content_types', array()),
46 '#options' => $options,
47 '#description' => t('A discussion section will be created for each node of selected content types.')
48 );
49
50 return system_settings_form($form);
51 }
52
53 /**
54 * Invokes hook_feedapi_node_discussion() in every module.
55 *
56 * We cannot use module_invoke() for this, because the arguments need to
57 * be passed by reference.
58 */
59 function feedapi_node_discussion_invoke(&$node, $op, $links = array()) {
60 foreach (module_list() as $module) {
61 $function = $module .'_feedapi_node_discussion';
62 if (function_exists($function)) {
63 $function($node, $op, $links);
64 }
65 }
66 }
67
68 /**
69 * Enter description here...
70 *
71 * @param unknown_type $type
72 * @return unknown
73 */
74 function feedapi_node_discussion_enabled($type) {
75 $enabled_types = variable_get('discussion_content_types', array());
76 if (isset($enabled_types[$type])) {
77 return TRUE;
78 }
79
80 return FALSE;
81 }
82
83 /**
84 * Implementation of hook_nodeapi().
85 */
86 function feedapi_node_discussion_nodeapi($node, $op, $arg = 0) {
87 if (!feedapi_node_discussion_enabled($node->type)) {
88 return;
89 }
90
91 switch ($op) {
92 case 'view':
93 $discussion = array();
94 $result = db_query("SELECT * FROM {feedapi_node_discussion} WHERE tid = %d", $node->nid);
95 while ($from = db_fetch_array($result)) {
96 $from_node = node_load($from['fid']);
97 if (is_object($from_node)) {
98 $discussion[] = array(l($from_node->title, 'node/'. $from['fid']));
99 }
100 }
101
102 $num_link = count($discussion);
103 if ($num_link > 0) {
104 // $node->discussion = $num_link;
105 $fieldset = array(
106 '#title' => t('Discussion (!count)', array('!count' => $num_link)),
107 '#collapsible' => TRUE,
108 '#collapsed' => TRUE,
109 '#value' => theme('table', NULL, $discussion),
110 );
111 $node->content['discussion'] = array(
112 '#value' => theme('fieldset', $fieldset),
113 '#weight' => 100,
114 );
115 }
116 break;
117
118 case 'insert':
119 case 'update':
120 // feedapi_node_discussion_insert($node);
121 $links = _feedapi_node_discussion_extract_links($node);
122 // module_invoke_all('feedapi_node_discussion', $node, $links, 'insert');
123 if ($links != FALSE && count($links) > 0) {
124 feedapi_node_discussion_invoke($node, $op, $links);
125 }
126 break;
127
128 case 'delete':
129 db_query('DELETE FROM {feedapi_node_discussion} WHERE fid = %d OR tid = %d', $node->nid, $node->nid);
130 // module_invoke_all('feedapi_node_discussion', $node, array(), 'delete');
131 // feedapi_node_discussion_invoke($node, 'delete');
132 break;
133 }
134 }
135
136 /**
137 * Enter description here...
138 *
139 * @param unknown_type $url
140 * @return unknown
141 */
142 //function _feedapi_node_discussion_item_exists($url) {
143 // return db_result(db_query("SELECT nid FROM {feedapi_node_item} WHERE url = '%s'", $url));
144 //}
145 function _feedapi_node_discussion_item_exists($url, $reset = FALSE) {
146 static $records;
147
148 if ($reset || !isset($records[$url])) {
149 $result = db_query("SELECT feed_nid, nid FROM {feedapi_node_item} WHERE url = '%s'", $url);
150 while ($raw = db_fetch_array($result)) {
151 $records[$url] = $raw;
152 }
153 }
154 return isset($records[$url]) ? $records[$url] : FALSE;
155 }
156
157 /**
158 * Enter description here...
159 *
160 * @param unknown_type $fid
161 * @param unknown_type $tid
162 */
163 function feedapi_node_discussion_write_record($fid, $tid) {
164 // Insert.
165 db_query('INSERT INTO {feedapi_node_discussion} (fid, tid) VALUES (%d, %d)', $fid, $tid);
166 }
167
168 /**
169 * Enter description here...
170 *
171 * @param unknown_type $content
172 * @return unknown
173 */
174 function _feedapi_node_discussion_extract_links($node) {
175 $pattern = '~(<a[^>]+)/?>~i';
176 $result = @preg_match_all($pattern, $node->body, $matches);
177 if ($result === FALSE) {
178 return FALSE;
179 }
180
181 $links = array();
182 if (isset($matches[1]) && count($matches[1]) > 0) {
183 foreach ($matches[1] as $link) {
184 // force string to be an utf-8 one
185 if (!mb_check_encoding($link, 'UTF-8')) {
186 $link = mb_convert_encoding($link, 'UTF-8');
187 }
188 $xml = @simplexml_load_string(rtrim($link, ' /') .' />');
189 if ($xml === FALSE) {
190 continue;
191 }
192 $attributes = $xml->attributes();
193 if (isset($attributes['rel']) && @preg_match('~^(nofollow)~i', $attributes['rel'])) {
194 continue;
195 }
196 if (!isset($attributes['href'])) {
197 continue;
198 }
199
200 $url = (string) $attributes['href'];
201 if (!valid_url($url, TRUE)) {
202 continue;
203 }
204 $links[] = $url;
205 }
206 }
207
208 return $links;
209 }
210
211 ///**
212 // * Enter description here...
213 // *
214 // * @param unknown_type $feed
215 // */
216 //function feedapi_node_discussion_insert($node) {
217 //// $pattern = '~(<a[^>]+)/? >~i';
218 //// $result = @preg_match_all($pattern, $node->body, $matches);
219 //// if ($result === FALSE) {
220 //// return;
221 //// }
222 ////
223 //// if (isset($matches[1]) && count($matches[1]) > 0) {
224 //// foreach ($matches[1] as $link) {
225 //// // force string to be an utf-8 one
226 //// if (!mb_check_encoding($link, 'UTF-8')) {
227 //// $link = mb_convert_encoding($link, 'UTF-8');
228 //// }
229 //// $xml = @simplexml_load_string(rtrim($link, ' /') .' />');
230 //// if ($xml === FALSE) {
231 //// continue;
232 //// }
233 //// $attributes = $xml->attributes();
234 //// if (isset($attributes['rel']) && @preg_match('~^(nofollow)~i', $attributes['rel'])) {
235 //// continue;
236 //// }
237 //// if (!isset($attributes['href'])) {
238 //// continue;
239 //// }
240 ////
241 //// $url = (string) $attributes['href'];
242 //// if (!valid_url($url, TRUE)) {
243 //// continue;
244 //// }
245 //
246 // $links = _feedapi_node_discussion_extract_links($node->body);
247 // foreach ($links as $link) {
248 // // If this is a valid url to an item which was saved before.
249 // $to_nid = _feedapi_node_discussion_item_exists($link);
250 // if ($to_nid) {
251 // if ($to_nid == $node->nid) {
252 // continue;
253 // }
254 // // Check whether this discussion was saved before.
255 // $result = db_result(db_query("SELECT fid FROM {feedapi_node_discussion} WHERE fid = %d AND tid = %d", $node->nid, $to_nid));
256 // if ($result === FALSE) {
257 // // Get feed nid for both from item and to item.
258 // $from_feed = array();
259 // $to_feed = array();
260 // foreach ($node->feedapi_node->feed_nids as $n) {
261 // $from_feed[] = $n;
262 // }
263 //// $from_result = db_query("SELECT feed_nid FROM {feedapi_node_item_feed} WHERE feed_item_nid = %d", $node->nid);
264 //// $to_result = db_query("SELECT feed_nid FROM {feedapi_node_item_feed} WHERE feed_item_nid = %d", $to_nid);
265 //// while ($f = db_fetch_array(db_query("SELECT * FROM {feedapi_node_item_feed} WHERE feed_item_nid = %d", $node->nid))) {
266 // while ($f = db_fetch_array(db_query("SELECT * FROM {feedapi_node_item} WHERE feed_item_nid = %d", $node->nid))) {
267 // $from_feed[] = $f['feed_nid'];
268 // }
269 //// while ($t = db_fetch_array(db_query("SELECT * FROM {feedapi_node_item_feed} WHERE feed_item_nid = %d", $to_nid))) {
270 // while ($t = db_fetch_array(db_query("SELECT * FROM {feedapi_node_item_feed} WHERE feed_item_nid = %d", $to_nid))) {
271 // $to_feed[] = $t['feed_nid'];
272 // }
273 //
274 // $intersect = array_intersect($from_feed, $to_feed);
275 // $continue = TRUE;
276 // foreach ($intersect as $i) {
277 // $continue = FALSE;
278 // }
279 //
280 //// $continue = TRUE;
281 //// foreach ($from_feed as $ff) {
282 //// foreach ($to_feed as $tf) {
283 //// if ($ff == $tf) {
284 //// $continue = FALSE;
285 //// }
286 //// }
287 //// if (in_array($ff, $to_feed)) {
288 //// $continue = FALSE;
289 //// }
290 //// }
291 //
292 // if ($continue) {
293 // feedapi_node_discussion_write_record($node->nid, $to_nid);
294 //
295 //// $promoted = db_result(db_query("SELECT promote FROM {node} WHERE nid = %d", $to_nid));
296 // $promoted = node_load($to_nid);
297 // if ($promoted->promote == 0 /* && strlen($promoted->teaser > 40) */ ) {
298 // db_query("UPDATE {node} SET created = %d, promote = 1 WHERE nid = %d", time(), $to_nid);
299 // }
300 // }
301 // }
302 // }
303 // }
304 //// }
305 //// }
306 //}
307
308 function feedapi_node_discussion_feedapi_node_discussion(&$node, $op, $links) {
309 switch ($op) {
310 case 'insert':
311 case 'update':
312 foreach ($links as $link) {
313 // If this is a valid url to an item which was saved before.
314 $to = _feedapi_node_discussion_item_exists($link);
315 $to_nid = $to['nid'];
316 if (!$to_nid) {
317 continue;
318 }
319 if ($to_nid == $node->nid) {
320 continue;
321 }
322
323 // Check whether this discussion was saved before.
324 $result = db_result(db_query("SELECT fid FROM {feedapi_node_discussion} WHERE fid = %d AND tid = %d", $node->nid, $to_nid));
325 if ($result === FALSE) {
326 // Get feed nid for both from item and to item.
327 // $from_feed = db_result(db_query("SELECT feed_nid FROM {feedapi_node_item} WHERE nid = %d", $node->nid));
328 // $to_feed = db_result(db_query("SELECT feed_nid FROM {feedapi_node_item} WHERE nid = %d", $to_nid));
329 $from_feed = $node->feedapi->feed_nid;
330 $to_feed = $to['feed_nid'];
331 if ($from_feed == $to_feed) {
332 continue;
333 }
334
335 feedapi_node_discussion_write_record($node->nid, $to_nid);
336
337 // $promoted = db_result(db_query("SELECT promote FROM {node} WHERE nid = %d", $to_nid));
338 // $promoted = node_load($to_nid);
339 // if ($promoted->promote == 0 /* && strlen($promoted->teaser > 40) */ ) {
340 // db_query("UPDATE {node} SET promote = 1 WHERE nid = %d", $to_nid);
341 // }
342 }
343 }
344 break;
345 }
346 }

  ViewVC Help
Powered by ViewVC 1.1.2