]> git.wh0rd.org - tt-rss.git/blame - include/rssfuncs.php
update_rss_feed: bailout if cache is considered valid, but older than last_updated
[tt-rss.git] / include / rssfuncs.php
CommitLineData
2c08214a 1<?php
09e8bdfd
AD
2 define('DAEMON_UPDATE_LOGIN_LIMIT', 30);
3 define('DAEMON_FEED_LIMIT', 100);
0f556e3e 4 define('DAEMON_SLEEP_INTERVAL', 60);
09e8bdfd 5
79178062
AD
6 function update_feedbrowser_cache($link) {
7
8 $result = db_query($link, "SELECT feed_url, site_url, title, COUNT(id) AS subscribers
9 FROM ttrss_feeds WHERE (SELECT COUNT(id) = 0 FROM ttrss_feeds AS tf
10 WHERE tf.feed_url = ttrss_feeds.feed_url
11 AND (private IS true OR auth_login != '' OR auth_pass != '' OR feed_url LIKE '%:%@%/%'))
12 GROUP BY feed_url, site_url, title ORDER BY subscribers DESC LIMIT 1000");
13
14 db_query($link, "BEGIN");
15
16 db_query($link, "DELETE FROM ttrss_feedbrowser_cache");
17
18 $count = 0;
19
20 while ($line = db_fetch_assoc($result)) {
3972bf59
AD
21 $subscribers = db_escape_string($link, $line["subscribers"]);
22 $feed_url = db_escape_string($link, $line["feed_url"]);
23 $title = db_escape_string($link, $line["title"]);
24 $site_url = db_escape_string($link, $line["site_url"]);
79178062
AD
25
26 $tmp_result = db_query($link, "SELECT subscribers FROM
27 ttrss_feedbrowser_cache WHERE feed_url = '$feed_url'");
28
29 if (db_num_rows($tmp_result) == 0) {
30
31 db_query($link, "INSERT INTO ttrss_feedbrowser_cache
32 (feed_url, site_url, title, subscribers) VALUES ('$feed_url',
33 '$site_url', '$title', '$subscribers')");
34
35 ++$count;
36
37 }
38
39 }
40
41 db_query($link, "COMMIT");
42
43 return $count;
44
45 }
46
47
2c08214a
AD
48 /**
49 * Update a feed batch.
50 * Used by daemons to update n feeds by run.
51 * Only update feed needing a update, and not being processed
52 * by another process.
53 *
54 * @param mixed $link Database link
55 * @param integer $limit Maximum number of feeds in update batch. Default to DAEMON_FEED_LIMIT.
56 * @param boolean $from_http Set to true if you call this function from http to disable cli specific code.
57 * @param boolean $debug Set to false to disable debug output. Default to true.
58 * @return void
59 */
60 function update_daemon_common($link, $limit = DAEMON_FEED_LIMIT, $from_http = false, $debug = true) {
61 // Process all other feeds using last_updated and interval parameters
62
61c1812f
AD
63 define('PREFS_NO_CACHE', true);
64
2c08214a 65 // Test if the user has loggued in recently. If not, it does not update its feeds.
09e8bdfd 66 if (!SINGLE_USER_MODE && DAEMON_UPDATE_LOGIN_LIMIT > 0) {
2c08214a
AD
67 if (DB_TYPE == "pgsql") {
68 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'";
69 } else {
70 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)";
71 }
72 } else {
73 $login_thresh_qpart = "";
74 }
75
76 // Test if the feed need a update (update interval exceded).
77 if (DB_TYPE == "pgsql") {
78 $update_limit_qpart = "AND ((
79 ttrss_feeds.update_interval = 0
80 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
81 ) OR (
82 ttrss_feeds.update_interval > 0
83 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL)
941e48a4
AD
84 ) OR ttrss_feeds.last_updated IS NULL
85 OR last_updated = '1970-01-01 00:00:00')";
2c08214a
AD
86 } else {
87 $update_limit_qpart = "AND ((
88 ttrss_feeds.update_interval = 0
89 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
90 ) OR (
91 ttrss_feeds.update_interval > 0
92 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE)
941e48a4
AD
93 ) OR ttrss_feeds.last_updated IS NULL
94 OR last_updated = '1970-01-01 00:00:00')";
2c08214a
AD
95 }
96
97 // Test if feed is currently being updated by another process.
98 if (DB_TYPE == "pgsql") {
99 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '5 minutes')";
100 } else {
101 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 5 MINUTE))";
102 }
103
104 // Test if there is a limit to number of updated feeds
105 $query_limit = "";
106 if($limit) $query_limit = sprintf("LIMIT %d", $limit);
107
108 $random_qpart = sql_random_function();
109
110 // We search for feed needing update.
1c4421fc 111 $result = db_query($link, "SELECT DISTINCT ttrss_feeds.feed_url
2c08214a
AD
112 FROM
113 ttrss_feeds, ttrss_users, ttrss_user_prefs
114 WHERE
115 ttrss_feeds.owner_uid = ttrss_users.id
116 AND ttrss_users.id = ttrss_user_prefs.owner_uid
117 AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
118 $login_thresh_qpart $update_limit_qpart
1c4421fc 119 $updstart_thresh_qpart
0d6943b0 120 ORDER BY feed_url $query_limit");
2c08214a
AD
121
122 $user_prefs_cache = array();
123
ca5ff2d9 124 if($debug) _debug(sprintf("Scheduled %d feeds to update...", db_num_rows($result)));
2c08214a
AD
125
126 // Here is a little cache magic in order to minimize risk of double feed updates.
127 $feeds_to_update = array();
128 while ($line = db_fetch_assoc($result)) {
1c4421fc 129 array_push($feeds_to_update, db_escape_string($link, $line['feed_url']));
2c08214a
AD
130 }
131
132 // We update the feed last update started date before anything else.
133 // There is no lag due to feed contents downloads
134 // It prevent an other process to update the same feed.
1c4421fc
AD
135
136 if(count($feeds_to_update) > 0) {
137 $feeds_quoted = array();
138
139 foreach ($feeds_to_update as $feed) {
140 array_push($feeds_quoted, "'" . db_escape_string($link, $feed) . "'");
141 }
142
2c08214a 143 db_query($link, sprintf("UPDATE ttrss_feeds SET last_update_started = NOW()
1c4421fc 144 WHERE feed_url IN (%s)", implode(',', $feeds_quoted)));
2c08214a
AD
145 }
146
3c696512 147 expire_cached_files($debug);
2a91b6ff 148 expire_lock_files($debug);
3c696512 149
2c08214a 150 // For each feed, we call the feed update function.
1c4421fc
AD
151 foreach ($feeds_to_update as $feed) {
152 if($debug) _debug("Base feed: $feed");
2c08214a 153
1c4421fc 154 //update_rss_feed($link, $line["id"], true);
2c08214a 155
1c4421fc
AD
156 // since we have the data cached, we can deal with other feeds with the same url
157
158 $tmp_result = db_query($link, "SELECT ttrss_feeds.feed_url,ttrss_feeds.id,last_updated
159 FROM ttrss_feeds, ttrss_users WHERE
160 ttrss_users.id = ttrss_feeds.owner_uid AND
30ac8d1f
AD
161 feed_url = '".db_escape_string($link, $feed)."' AND
162 ttrss_feeds.update_interval != -1
1c4421fc
AD
163 $login_thresh_qpart
164 ORDER BY feed_url $query_limit");
165
166 if (db_num_rows($tmp_result) > 0) {
167 while ($tline = db_fetch_assoc($tmp_result)) {
32b05702 168 if($debug) _debug(" => " . $tline["last_updated"] . ", " . $tline["id"]);
1c4421fc
AD
169 update_rss_feed($link, $tline["id"], true);
170 }
171 }
2c08214a
AD
172 }
173
f1611683
AD
174 require_once "digest.php";
175
2c08214a 176 // Send feed digests by email if needed.
8437c066 177 send_headlines_digests($link, $debug);
2c08214a
AD
178
179 } // function update_daemon_common
180
87764a50 181 // ignore_daemon is not used
0e4a7d7a 182 function update_rss_feed($link, $feed, $ignore_daemon = false, $no_cache = false,
2c08214a
AD
183 $override_url = false) {
184
185 require_once "lib/simplepie/simplepie.inc";
2c08214a 186
2c08214a
AD
187 $debug_enabled = defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug'];
188
2c08214a
AD
189 if ($debug_enabled) {
190 _debug("update_rss_feed: start");
191 }
192
87764a50 193 $result = db_query($link, "SELECT id,update_interval,auth_login,
0a3fd79b 194 feed_url,auth_pass,cache_images,last_updated,
5321e775 195 mark_unread_on_update, owner_uid,
87764a50
AD
196 pubsub_state
197 FROM ttrss_feeds WHERE id = '$feed'");
2c08214a
AD
198
199 if (db_num_rows($result) == 0) {
200 if ($debug_enabled) {
201 _debug("update_rss_feed: feed $feed NOT FOUND/SKIPPED");
202 }
203 return false;
204 }
205
2c08214a
AD
206 $last_updated = db_fetch_result($result, 0, "last_updated");
207 $owner_uid = db_fetch_result($result, 0, "owner_uid");
208 $mark_unread_on_update = sql_bool_to_bool(db_fetch_result($result,
209 0, "mark_unread_on_update"));
2c08214a
AD
210 $pubsub_state = db_fetch_result($result, 0, "pubsub_state");
211
212 db_query($link, "UPDATE ttrss_feeds SET last_update_started = NOW()
213 WHERE id = '$feed'");
214
215 $auth_login = db_fetch_result($result, 0, "auth_login");
216 $auth_pass = db_fetch_result($result, 0, "auth_pass");
217
2c08214a
AD
218 $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images"));
219 $fetch_url = db_fetch_result($result, 0, "feed_url");
220
3972bf59 221 $feed = db_escape_string($link, $feed);
2c08214a 222
f074ffe9 223 if ($override_url) $fetch_url = $override_url;
2c08214a 224
f074ffe9 225 $date_feed_processed = date('Y-m-d H:i');
2c08214a 226
4f9cbdff 227 $cache_filename = CACHE_DIR . "/simplepie/" . sha1($fetch_url) . ".feed";
f074ffe9
AD
228
229 // Ignore cache if new feed or manual update.
230 $cache_age = ($no_cache || is_null($last_updated) || $last_updated == '1970-01-01 00:00:00') ?
231 30 : get_feed_update_interval($link, $feed) * 60;
2c08214a
AD
232
233 if ($debug_enabled) {
f074ffe9
AD
234 _debug("update_rss_feed: cache filename: $cache_filename exists: " . file_exists($cache_filename));
235 _debug("update_rss_feed: cache age: $cache_age; no cache: $no_cache");
2c08214a
AD
236 }
237
f074ffe9 238 $cached_feed_data_hash = false;
2c08214a 239
4f9cbdff
AD
240 $rss = false;
241 $rss_hash = false;
7a01dc77 242 $cache_timestamp = file_exists($cache_filename) ? filemtime($cache_filename) : 0;
31623bfa 243 $last_updated_timestamp = strtotime($last_updated);
4f9cbdff 244
f074ffe9
AD
245 if (file_exists($cache_filename) &&
246 is_readable($cache_filename) &&
247 !$auth_login && !$auth_pass &&
248 filemtime($cache_filename) > time() - $cache_age) {
2c08214a 249
f074ffe9
AD
250 if ($debug_enabled) {
251 _debug("update_rss_feed: using local cache.");
252 }
be574731 253
17e74b21
AD
254 if ($cache_timestamp > $last_updated_timestamp) {
255 @$rss_data = file_get_contents($cache_filename);
f074ffe9 256
17e74b21
AD
257 if ($rss_data) {
258 $rss_hash = sha1($rss_data);
259 @$rss = unserialize($rss_data);
260 }
261 } else {
262 if ($debug_enabled) {
263 _debug("update_rss_feed: local cache valid and older than last_updated, nothing to do.");
264 }
265 return;
4f9cbdff 266 }
f074ffe9 267 }
017401dd 268
4f9cbdff 269 if (!$rss) {
017401dd 270
4f9cbdff
AD
271 if (!$feed_data) {
272 if ($debug_enabled) {
31623bfa 273 _debug("update_rss_feed: fetching [$fetch_url] (ts: $cache_timestamp/$last_updated_timestamp)");
4f9cbdff 274 }
017401dd 275
4f9cbdff 276 $feed_data = fetch_file_contents($fetch_url, false,
31623bfa
AD
277 $auth_login, $auth_pass, false, $no_cache ? 15 : 45,
278 max($last_updated_timestamp, $cache_timestamp));
96f98cb0 279
4f9cbdff
AD
280 if ($debug_enabled) {
281 _debug("update_rss_feed: fetch done.");
282 }
017401dd 283
4f9cbdff 284 }
017401dd 285
4f9cbdff
AD
286 if (!$feed_data) {
287 global $fetch_last_error;
7a01dc77 288 global $fetch_last_error_code;
f074ffe9
AD
289
290 if ($debug_enabled) {
7a01dc77 291 _debug("update_rss_feed: unable to fetch: $fetch_last_error [$fetch_last_error_code]");
f074ffe9
AD
292 }
293
7a01dc77
AD
294 $error_escaped = '';
295
296 // If-Modified-Since
297 if ($fetch_last_error_code != 304) {
298 $error_escaped = db_escape_string($link, $fetch_last_error);
299 } else {
300 if ($debug_enabled) {
301 _debug("update_rss_feed: source claims data not modified, nothing to do.");
302 }
303 }
4f9cbdff
AD
304
305 db_query($link,
306 "UPDATE ttrss_feeds SET last_error = '$error_escaped',
307 last_updated = NOW() WHERE id = '$feed'");
308
309 return;
f074ffe9
AD
310 }
311 }
312
017401dd 313 $pluginhost = new PluginHost($link);
be178857 314 $pluginhost->set_debug($debug_enabled);
ab457a9c
AD
315 $user_plugins = get_pref($link, "_ENABLED_PLUGINS", $owner_uid);
316
317 $pluginhost->load(PLUGINS, $pluginhost::KIND_ALL);
318 $pluginhost->load($user_plugins, $pluginhost::KIND_USER, $owner_uid);
319 $pluginhost->load_data();
017401dd
AD
320
321 foreach ($pluginhost->get_hooks($pluginhost::HOOK_FEED_FETCHED) as $plugin) {
322 $feed_data = $plugin->hook_feed_fetched($feed_data);
323 }
324
4f9cbdff
AD
325 if (!$rss) {
326 $rss = new SimplePie();
327 $rss->set_sanitize_class("SanitizeDummy");
328 // simplepie ignores the above and creates default sanitizer anyway,
329 // so let's override it...
330 $rss->sanitize = new SanitizeDummy();
331 $rss->set_output_encoding('UTF-8');
332 $rss->set_raw_data($feed_data);
333 $rss->enable_cache(false);
334
335 @$rss->init();
5de51df7
AD
336 }
337
2c08214a
AD
338// print_r($rss);
339
3972bf59 340 $feed = db_escape_string($link, $feed);
2c08214a 341
19b3992b 342 if (!$rss->error()) {
2c08214a 343
4f9cbdff
AD
344 // cache data for later
345 if (!$auth_pass && !$auth_login && is_writable(CACHE_DIR . "/simplepie")) {
346 $rss_data = serialize($rss);
347 $new_rss_hash = sha1($rss_data);
348
349 if ($new_rss_hash != $rss_hash) {
350 if ($debug_enabled) {
351 _debug("update_rss_feed: saving $cache_filename");
352 }
353 @file_put_contents($cache_filename, serialize($rss));
354 }
355 }
356
d2a421e3 357 // We use local pluginhost here because we need to load different per-user feed plugins
4412b877
AD
358 $pluginhost->run_hooks($pluginhost::HOOK_FEED_PARSED, "hook_feed_parsed", $rss);
359
2c08214a
AD
360 if ($debug_enabled) {
361 _debug("update_rss_feed: processing feed data...");
362 }
363
364// db_query($link, "BEGIN");
365
382268c6
AD
366 if (DB_TYPE == "pgsql") {
367 $favicon_interval_qpart = "favicon_last_checked < NOW() - INTERVAL '12 hour'";
368 } else {
369 $favicon_interval_qpart = "favicon_last_checked < DATE_SUB(NOW(), INTERVAL 12 HOUR)";
370 }
371
750cfcd2 372 $result = db_query($link, "SELECT title,site_url,owner_uid,
382268c6
AD
373 (favicon_last_checked IS NULL OR $favicon_interval_qpart) AS
374 favicon_needs_check
2c08214a
AD
375 FROM ttrss_feeds WHERE id = '$feed'");
376
377 $registered_title = db_fetch_result($result, 0, "title");
2c08214a 378 $orig_site_url = db_fetch_result($result, 0, "site_url");
382268c6
AD
379 $favicon_needs_check = sql_bool_to_bool(db_fetch_result($result, 0,
380 "favicon_needs_check"));
2c08214a
AD
381
382 $owner_uid = db_fetch_result($result, 0, "owner_uid");
383
3972bf59 384 $site_url = db_escape_string($link, mb_substr(rewrite_relative_url($fetch_url, $rss->get_link()), 0, 245));
2c08214a
AD
385
386 if ($debug_enabled) {
387 _debug("update_rss_feed: checking favicon...");
388 }
389
f2798eb6
AD
390 if ($favicon_needs_check) {
391 check_feed_favicon($site_url, $feed, $link);
392
393 db_query($link, "UPDATE ttrss_feeds SET favicon_last_checked = NOW()
394 WHERE id = '$feed'");
395 }
2c08214a
AD
396
397 if (!$registered_title || $registered_title == "[Unknown]") {
398
3972bf59 399 $feed_title = db_escape_string($link, $rss->get_title());
2c08214a
AD
400
401 if ($debug_enabled) {
402 _debug("update_rss_feed: registering title: $feed_title");
403 }
404
405 db_query($link, "UPDATE ttrss_feeds SET
406 title = '$feed_title' WHERE id = '$feed'");
407 }
408
0cf81637 409 if ($site_url && $orig_site_url != $site_url) {
2c08214a
AD
410 db_query($link, "UPDATE ttrss_feeds SET
411 site_url = '$site_url' WHERE id = '$feed'");
412 }
413
2c08214a 414 if ($debug_enabled) {
b24504b1 415 _debug("update_rss_feed: loading filters & labels...");
2c08214a
AD
416 }
417
418 $filters = load_filters($link, $feed, $owner_uid);
b24504b1 419 $labels = get_all_labels($link, $owner_uid);
2c08214a 420
67bd0b1f
AD
421 if ($debug_enabled) {
422 //print_r($filters);
423 _debug("update_rss_feed: " . count($filters) . " filters loaded.");
424 }
2c08214a 425
19b3992b 426 $items = $rss->get_items();
2c08214a 427
19b3992b 428 if (!is_array($items)) {
2c08214a 429 if ($debug_enabled) {
19b3992b 430 _debug("update_rss_feed: no articles found.");
2c08214a
AD
431 }
432
433 db_query($link, "UPDATE ttrss_feeds
434 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
435
436 return; // no articles
437 }
438
439 if ($pubsub_state != 2 && PUBSUBHUBBUB_ENABLED) {
440
441 if ($debug_enabled) _debug("update_rss_feed: checking for PUSH hub...");
442
443 $feed_hub_url = false;
2c08214a 444
19b3992b 445 $links = $rss->get_links('hub');
2c08214a 446
19b3992b
AD
447 if ($links && is_array($links)) {
448 foreach ($links as $l) {
449 $feed_hub_url = $l;
450 break;
2c08214a
AD
451 }
452 }
453
454 if ($debug_enabled) _debug("update_rss_feed: feed hub url: $feed_hub_url");
455
456 if ($feed_hub_url && function_exists('curl_init') &&
457 !ini_get("open_basedir")) {
458
459 require_once 'lib/pubsubhubbub/subscriber.php';
460
461 $callback_url = get_self_url_prefix() .
462 "/public.php?op=pubsub&id=$feed";
463
464 $s = new Subscriber($feed_hub_url, $callback_url);
465
466 $rc = $s->subscribe($fetch_url);
467
468 if ($debug_enabled)
469 _debug("update_rss_feed: feed hub url found, subscribe request sent.");
470
471 db_query($link, "UPDATE ttrss_feeds SET pubsub_state = 1
472 WHERE id = '$feed'");
473 }
474 }
475
476 if ($debug_enabled) {
477 _debug("update_rss_feed: processing articles...");
478 }
479
19b3992b 480 foreach ($items as $item) {
5d56d100 481 if ($_REQUEST['xdebug'] == 3) {
2c08214a
AD
482 print_r($item);
483 }
484
19b3992b
AD
485 $entry_guid = $item->get_id();
486 if (!$entry_guid) $entry_guid = $item->get_link();
487 if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title());
2c08214a
AD
488
489 if ($debug_enabled) {
490 _debug("update_rss_feed: guid $entry_guid");
491 }
492
493 if (!$entry_guid) continue;
494
3a4c8973
AD
495 $entry_guid = "$owner_uid,$entry_guid";
496
2c08214a
AD
497 $entry_timestamp = "";
498
19b3992b 499 $entry_timestamp = strtotime($item->get_date());
2c08214a 500
30123fe6 501 if ($entry_timestamp == -1 || !$entry_timestamp || $entry_timestamp > time()) {
2c08214a
AD
502 $entry_timestamp = time();
503 $no_orig_date = 'true';
504 } else {
505 $no_orig_date = 'false';
506 }
507
508 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
509
510 if ($debug_enabled) {
511 _debug("update_rss_feed: date $entry_timestamp [$entry_timestamp_fmt]");
512 }
513
19b3992b 514 $entry_title = $item->get_title();
1b35d30c 515
5d56d100 516 $entry_link = rewrite_relative_url($site_url, $item->get_link());
2c08214a
AD
517
518 if ($debug_enabled) {
519 _debug("update_rss_feed: title $entry_title");
520 _debug("update_rss_feed: link $entry_link");
521 }
522
523 if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);;
524
19b3992b
AD
525 $entry_content = $item->get_content();
526 if (!$entry_content) $entry_content = $item->get_description();
2c08214a
AD
527
528 if ($_REQUEST["xdebug"] == 2) {
529 print "update_rss_feed: content: ";
487f0750 530 print $entry_content;
3c696512 531 print "\n";
2c08214a
AD
532 }
533
19b3992b 534 $entry_comments = $item->data["comments"];
2c08214a 535
19b3992b
AD
536 if ($item->get_author()) {
537 $entry_author_item = $item->get_author();
538 $entry_author = $entry_author_item->get_name();
539 if (!$entry_author) $entry_author = $entry_author_item->get_email();
2c08214a 540
3972bf59 541 $entry_author = db_escape_string($link, $entry_author);
2c08214a
AD
542 }
543
3972bf59 544 $entry_guid = db_escape_string($link, mb_substr($entry_guid, 0, 245));
2c08214a 545
3972bf59
AD
546 $entry_comments = db_escape_string($link, mb_substr($entry_comments, 0, 245));
547 $entry_author = db_escape_string($link, mb_substr($entry_author, 0, 245));
2c08214a 548
19b3992b 549 $num_comments = $item->get_item_tags('http://purl.org/rss/1.0/modules/slash/', 'comments');
83e6e313 550
19b3992b
AD
551 if (is_array($num_comments) && is_array($num_comments[0])) {
552 $num_comments = (int) $num_comments[0]["data"];
2c08214a 553 } else {
19b3992b 554 $num_comments = 0;
2c08214a
AD
555 }
556
2c08214a 557 if ($debug_enabled) {
83e6e313 558 _debug("update_rss_feed: num_comments: $num_comments");
2c08214a
AD
559 _debug("update_rss_feed: looking for tags [1]...");
560 }
561
562 // parse <category> entries into tags
563
564 $additional_tags = array();
565
19b3992b 566 $additional_tags_src = $item->get_categories();
2c08214a 567
19b3992b
AD
568 if (is_array($additional_tags_src)) {
569 foreach ($additional_tags_src as $tobj) {
570 array_push($additional_tags, $tobj->get_term());
2c08214a 571 }
19b3992b 572 }
2c08214a 573
19b3992b
AD
574 if ($debug_enabled) {
575 _debug("update_rss_feed: category tags:");
576 print_r($additional_tags);
2c08214a
AD
577 }
578
579 if ($debug_enabled) {
580 _debug("update_rss_feed: looking for tags [2]...");
581 }
582
fa6fbd36 583 $entry_tags = array_unique($additional_tags);
2c08214a
AD
584
585 for ($i = 0; $i < count($entry_tags); $i++)
586 $entry_tags[$i] = mb_strtolower($entry_tags[$i], 'utf-8');
587
588 if ($debug_enabled) {
6aff7845
AD
589 //_debug("update_rss_feed: unfiltered tags found:");
590 //print_r($entry_tags);
2c08214a
AD
591 }
592
2c08214a 593 if ($debug_enabled) {
70caff48 594 _debug("update_rss_feed: done collecting data.");
2c08214a
AD
595 }
596
cc85704f 597 // TODO: less memory-hungry implementation
19c73507 598
19b3992b
AD
599 if ($debug_enabled) {
600 _debug("update_rss_feed: applying plugin filters..");
601 }
cc85704f 602
e02555c1
AD
603 // FIXME not sure if owner_uid is a good idea here, we may have a base entry without user entry (?)
604 $result = db_query($link, "SELECT plugin_data,title,content,link,tag_cache,author FROM ttrss_entries, ttrss_user_entries
3972bf59 605 WHERE ref_id = id AND guid = '".db_escape_string($link, $entry_guid)."' AND owner_uid = $owner_uid");
b30abdad
AD
606
607 if (db_num_rows($result) != 0) {
608 $entry_plugin_data = db_fetch_result($result, 0, "plugin_data");
e02555c1
AD
609 $stored_article = array("title" => db_fetch_result($result, 0, "title"),
610 "content" => db_fetch_result($result, 0, "content"),
611 "link" => db_fetch_result($result, 0, "link"),
612 "tags" => explode(",", db_fetch_result($result, 0, "tag_cache")),
613 "author" => db_fetch_result($result, 0, "author"));
b30abdad
AD
614 } else {
615 $entry_plugin_data = "";
e02555c1 616 $stored_article = array();
b30abdad
AD
617 }
618
455b1401 619 $article = array("owner_uid" => $owner_uid, // read only
b30abdad 620 "guid" => $entry_guid, // read only
19b3992b
AD
621 "title" => $entry_title,
622 "content" => $entry_content,
623 "link" => $entry_link,
624 "tags" => $entry_tags,
b30abdad 625 "plugin_data" => $entry_plugin_data,
e02555c1
AD
626 "author" => $entry_author,
627 "stored" => $stored_article);
cc85704f 628
19b3992b
AD
629 foreach ($pluginhost->get_hooks($pluginhost::HOOK_ARTICLE_FILTER) as $plugin) {
630 $article = $plugin->hook_article_filter($article);
cc85704f
AD
631 }
632
19b3992b 633 $entry_tags = $article["tags"];
3972bf59
AD
634 $entry_guid = db_escape_string($link, $entry_guid);
635 $entry_title = db_escape_string($link, $article["title"]);
636 $entry_author = db_escape_string($link, $article["author"]);
637 $entry_link = db_escape_string($link, $article["link"]);
638 $entry_plugin_data = db_escape_string($link, $article["plugin_data"]);
f935d98e
AD
639 $entry_content = $article["content"]; // escaped below
640
b30abdad
AD
641
642 if ($debug_enabled) {
643 _debug("update_rss_feed: plugin data: $entry_plugin_data");
644 }
2bbd6994 645
0a3fd79b 646 if ($cache_images && is_writable(CACHE_DIR . '/images'))
f0bd8e65 647 cache_images($entry_content, $site_url, $debug_enabled);
0a3fd79b 648
3972bf59 649 $entry_content = db_escape_string($link, $entry_content, false);
1f45c857 650
9e222305 651 $content_hash = "SHA1:" . sha1($entry_content);
cc85704f 652
2c08214a
AD
653 db_query($link, "BEGIN");
654
9e222305
AD
655 $result = db_query($link, "SELECT id FROM ttrss_entries
656 WHERE guid = '$entry_guid'");
657
2c08214a
AD
658 if (db_num_rows($result) == 0) {
659
660 if ($debug_enabled) {
9e222305 661 _debug("update_rss_feed: base guid [$entry_guid] not found");
2c08214a
AD
662 }
663
664 // base post entry does not exist, create it
665
666 $result = db_query($link,
667 "INSERT INTO ttrss_entries
668 (title,
669 guid,
670 link,
671 updated,
672 content,
673 content_hash,
87764a50 674 cached_content,
2c08214a
AD
675 no_orig_date,
676 date_updated,
677 date_entered,
678 comments,
679 num_comments,
b30abdad 680 plugin_data,
2c08214a
AD
681 author)
682 VALUES
683 ('$entry_title',
684 '$entry_guid',
685 '$entry_link',
686 '$entry_timestamp_fmt',
687 '$entry_content',
688 '$content_hash',
0a3fd79b 689 '',
2c08214a
AD
690 $no_orig_date,
691 NOW(),
be574731 692 '$date_feed_processed',
2c08214a
AD
693 '$entry_comments',
694 '$num_comments',
b30abdad 695 '$entry_plugin_data',
2c08214a 696 '$entry_author')");
e8291805
AD
697
698 $article_labels = array();
699
2c08214a
AD
700 } else {
701 // we keep encountering the entry in feeds, so we need to
702 // update date_updated column so that we don't get horrible
703 // dupes when the entry gets purged and reinserted again e.g.
704 // in the case of SLOW SLOW OMG SLOW updating feeds
705
706 $base_entry_id = db_fetch_result($result, 0, "id");
707
708 db_query($link, "UPDATE ttrss_entries SET date_updated = NOW()
709 WHERE id = '$base_entry_id'");
e8291805
AD
710
711 $article_labels = get_article_labels($link, $base_entry_id, $owner_uid);
2c08214a
AD
712 }
713
714 // now it should exist, if not - bad luck then
715
716 $result = db_query($link, "SELECT
b30abdad 717 id,content_hash,no_orig_date,title,plugin_data,
2c08214a
AD
718 ".SUBSTRING_FOR_DATE."(date_updated,1,19) as date_updated,
719 ".SUBSTRING_FOR_DATE."(updated,1,19) as updated,
0a3fd79b 720 num_comments
2c08214a
AD
721 FROM
722 ttrss_entries
723 WHERE guid = '$entry_guid'");
724
725 $entry_ref_id = 0;
726 $entry_int_id = 0;
727
728 if (db_num_rows($result) == 1) {
729
730 if ($debug_enabled) {
9e222305 731 _debug("update_rss_feed: base guid [$entry_guid] found, checking for user record");
2c08214a
AD
732 }
733
734 // this will be used below in update handler
735 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
736 $orig_title = db_fetch_result($result, 0, "title");
737 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
738 $orig_date_updated = strtotime(db_fetch_result($result,
739 0, "date_updated"));
b30abdad 740 $orig_plugin_data = db_fetch_result($result, 0, "plugin_data");
2c08214a
AD
741
742 $ref_id = db_fetch_result($result, 0, "id");
743 $entry_ref_id = $ref_id;
744
745 // check for user post link to main table
746
747 // do we allow duplicate posts with same GUID in different feeds?
748 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
749 $dupcheck_qpart = "AND (feed_id = '$feed' OR feed_id IS NULL)";
750 } else {
751 $dupcheck_qpart = "";
752 }
753
754 /* Collect article tags here so we could filter by them: */
755
756 $article_filters = get_article_filters($filters, $entry_title,
4021d61a 757 $entry_content, $entry_link, $entry_timestamp, $entry_author,
2c08214a
AD
758 $entry_tags);
759
760 if ($debug_enabled) {
761 _debug("update_rss_feed: article filters: ");
762 if (count($article_filters) != 0) {
763 print_r($article_filters);
764 }
765 }
766
767 if (find_article_filter($article_filters, "filter")) {
768 db_query($link, "COMMIT"); // close transaction in progress
769 continue;
770 }
771
772 $score = calculate_article_score($article_filters);
773
774 if ($debug_enabled) {
775 _debug("update_rss_feed: initial score: $score");
776 }
777
778 $query = "SELECT ref_id, int_id FROM ttrss_user_entries WHERE
779 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
780 $dupcheck_qpart";
781
782// if ($_REQUEST["xdebug"]) print "$query\n";
783
784 $result = db_query($link, $query);
785
786 // okay it doesn't exist - create user entry
787 if (db_num_rows($result) == 0) {
788
789 if ($debug_enabled) {
790 _debug("update_rss_feed: user record not found, creating...");
791 }
792
793 if ($score >= -500 && !find_article_filter($article_filters, 'catchup')) {
794 $unread = 'true';
795 $last_read_qpart = 'NULL';
796 } else {
797 $unread = 'false';
798 $last_read_qpart = 'NOW()';
799 }
800
801 if (find_article_filter($article_filters, 'mark') || $score > 1000) {
802 $marked = 'true';
803 } else {
804 $marked = 'false';
805 }
806
807 if (find_article_filter($article_filters, 'publish')) {
808 $published = 'true';
809 } else {
810 $published = 'false';
811 }
812
2ea9bbfd
AD
813 // N-grams
814
815 if (DB_TYPE == "pgsql" and defined('_NGRAM_TITLE_DUPLICATE_THRESHOLD')) {
816
817 $result = db_query($link, "SELECT COUNT(*) AS similar FROM
818 ttrss_entries,ttrss_user_entries
819 WHERE ref_id = id AND updated >= NOW() - INTERVAL '7 day'
820 AND similarity(title, '$entry_title') >= "._NGRAM_TITLE_DUPLICATE_THRESHOLD."
821 AND owner_uid = $owner_uid");
822
823 $ngram_similar = db_fetch_result($result, 0, "similar");
824
825 if ($debug_enabled) {
826 _debug("update_rss_feed: N-gram similar results: $ngram_similar");
827 }
828
829 if ($ngram_similar > 0) {
830 $unread = 'false';
831 }
832 }
833
7873d588
AD
834 $last_marked = ($marked == 'true') ? 'NOW()' : 'NULL';
835 $last_published = ($published == 'true') ? 'NOW()' : 'NULL';
836
2c08214a
AD
837 $result = db_query($link,
838 "INSERT INTO ttrss_user_entries
839 (ref_id, owner_uid, feed_id, unread, last_read, marked,
7873d588
AD
840 published, score, tag_cache, label_cache, uuid,
841 last_marked, last_published)
2c08214a 842 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
7873d588
AD
843 $last_read_qpart, $marked, $published, '$score', '', '',
844 '', $last_marked, $last_published)");
2c08214a
AD
845
846 if (PUBSUBHUBBUB_HUB && $published == 'true') {
847 $rss_link = get_self_url_prefix() .
848 "/public.php?op=rss&id=-2&key=" .
849 get_feed_access_key($link, -2, false, $owner_uid);
850
851 $p = new Publisher(PUBSUBHUBBUB_HUB);
852
853 $pubsub_result = $p->publish_update($rss_link);
854 }
855
856 $result = db_query($link,
857 "SELECT int_id FROM ttrss_user_entries WHERE
858 ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND
859 feed_id = '$feed' LIMIT 1");
860
861 if (db_num_rows($result) == 1) {
862 $entry_int_id = db_fetch_result($result, 0, "int_id");
863 }
864 } else {
865 if ($debug_enabled) {
866 _debug("update_rss_feed: user record FOUND");
867 }
868
869 $entry_ref_id = db_fetch_result($result, 0, "ref_id");
870 $entry_int_id = db_fetch_result($result, 0, "int_id");
871 }
872
873 if ($debug_enabled) {
874 _debug("update_rss_feed: RID: $entry_ref_id, IID: $entry_int_id");
875 }
876
877 $post_needs_update = false;
878 $update_insignificant = false;
879
880 if ($orig_num_comments != $num_comments) {
881 $post_needs_update = true;
882 $update_insignificant = true;
883 }
884
b30abdad
AD
885 if ($entry_plugin_data != $orig_plugin_data) {
886 $post_needs_update = true;
887 $update_insignificant = true;
888 }
889
2c08214a
AD
890 if ($content_hash != $orig_content_hash) {
891 $post_needs_update = true;
892 $update_insignificant = false;
893 }
894
3972bf59 895 if (db_escape_string($link, $orig_title) != $entry_title) {
2c08214a
AD
896 $post_needs_update = true;
897 $update_insignificant = false;
898 }
899
900 // if post needs update, update it and mark all user entries
901 // linking to this post as updated
902 if ($post_needs_update) {
903
904 if (defined('DAEMON_EXTENDED_DEBUG')) {
905 _debug("update_rss_feed: post $entry_guid needs update...");
906 }
907
908// print "<!-- post $orig_title needs update : $post_needs_update -->";
909
910 db_query($link, "UPDATE ttrss_entries
911 SET title = '$entry_title', content = '$entry_content',
912 content_hash = '$content_hash',
913 updated = '$entry_timestamp_fmt',
b30abdad
AD
914 num_comments = '$num_comments',
915 plugin_data = '$entry_plugin_data'
2c08214a
AD
916 WHERE id = '$ref_id'");
917
918 if (!$update_insignificant) {
919 if ($mark_unread_on_update) {
920 db_query($link, "UPDATE ttrss_user_entries
921 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
2c08214a
AD
922 }
923 }
924 }
925 }
926
927 db_query($link, "COMMIT");
928
929 if ($debug_enabled) {
930 _debug("update_rss_feed: assigning labels...");
931 }
932
92c14e9d 933 assign_article_to_label_filters($link, $entry_ref_id, $article_filters,
b24504b1 934 $owner_uid, $article_labels);
2c08214a
AD
935
936 if ($debug_enabled) {
937 _debug("update_rss_feed: looking for enclosures...");
938 }
939
940 // enclosures
941
942 $enclosures = array();
943
19b3992b 944 $encs = $item->get_enclosures();
2c08214a 945
19b3992b
AD
946 if (is_array($encs)) {
947 foreach ($encs as $e) {
948 $e_item = array(
949 $e->link, $e->type, $e->length);
2c08214a 950 array_push($enclosures, $e_item);
2c08214a
AD
951 }
952 }
953
2c08214a
AD
954 if ($debug_enabled) {
955 _debug("update_rss_feed: article enclosures:");
956 print_r($enclosures);
957 }
958
959 db_query($link, "BEGIN");
960
961 foreach ($enclosures as $enc) {
3972bf59
AD
962 $enc_url = db_escape_string($link, $enc[0]);
963 $enc_type = db_escape_string($link, $enc[1]);
964 $enc_dur = db_escape_string($link, $enc[2]);
2c08214a
AD
965
966 $result = db_query($link, "SELECT id FROM ttrss_enclosures
967 WHERE content_url = '$enc_url' AND post_id = '$entry_ref_id'");
968
969 if (db_num_rows($result) == 0) {
970 db_query($link, "INSERT INTO ttrss_enclosures
971 (content_url, content_type, title, duration, post_id) VALUES
972 ('$enc_url', '$enc_type', '', '$enc_dur', '$entry_ref_id')");
973 }
974 }
975
976 db_query($link, "COMMIT");
977
978 // check for manual tags (we have to do it here since they're loaded from filters)
979
980 foreach ($article_filters as $f) {
6aff7845 981 if ($f["type"] == "tag") {
2c08214a 982
6aff7845 983 $manual_tags = trim_array(explode(",", $f["param"]));
2c08214a
AD
984
985 foreach ($manual_tags as $tag) {
986 if (tag_is_valid($tag)) {
987 array_push($entry_tags, $tag);
988 }
989 }
990 }
991 }
992
993 // Skip boring tags
994
995 $boring_tags = trim_array(explode(",", mb_strtolower(get_pref($link,
996 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8')));
997
998 $filtered_tags = array();
999 $tags_to_cache = array();
1000
1001 if ($entry_tags && is_array($entry_tags)) {
1002 foreach ($entry_tags as $tag) {
1003 if (array_search($tag, $boring_tags) === false) {
1004 array_push($filtered_tags, $tag);
1005 }
1006 }
1007 }
1008
1009 $filtered_tags = array_unique($filtered_tags);
1010
1011 if ($debug_enabled) {
1012 _debug("update_rss_feed: filtered article tags:");
1013 print_r($filtered_tags);
1014 }
1015
1016 // Save article tags in the database
1017
1018 if (count($filtered_tags) > 0) {
1019
1020 db_query($link, "BEGIN");
1021
1022 foreach ($filtered_tags as $tag) {
1023
1024 $tag = sanitize_tag($tag);
3972bf59 1025 $tag = db_escape_string($link, $tag);
2c08214a
AD
1026
1027 if (!tag_is_valid($tag)) continue;
1028
1029 $result = db_query($link, "SELECT id FROM ttrss_tags
1030 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
1031 owner_uid = '$owner_uid' LIMIT 1");
1032
1033 if ($result && db_num_rows($result) == 0) {
1034
1035 db_query($link, "INSERT INTO ttrss_tags
1036 (owner_uid,tag_name,post_int_id)
1037 VALUES ('$owner_uid','$tag', '$entry_int_id')");
1038 }
1039
1040 array_push($tags_to_cache, $tag);
1041 }
1042
1043 /* update the cache */
1044
1045 $tags_to_cache = array_unique($tags_to_cache);
1046
3972bf59 1047 $tags_str = db_escape_string($link, join(",", $tags_to_cache));
2c08214a
AD
1048
1049 db_query($link, "UPDATE ttrss_user_entries
1050 SET tag_cache = '$tags_str' WHERE ref_id = '$entry_ref_id'
1051 AND owner_uid = $owner_uid");
1052
1053 db_query($link, "COMMIT");
1054 }
1055
b24504b1
AD
1056 if (get_pref($link, "AUTO_ASSIGN_LABELS", $owner_uid, false)) {
1057 if ($debug_enabled) {
1058 _debug("update_rss_feed: auto-assigning labels...");
1059 }
1060
1061 foreach ($labels as $label) {
227d5e41 1062 $caption = preg_quote($label["caption"]);
b24504b1 1063
9811276d 1064 if ($caption && preg_match("/\b$caption\b/i", "$tags_str " . strip_tags($entry_content) . " $entry_title")) {
b24504b1
AD
1065 if (!labels_contains_caption($article_labels, $caption)) {
1066 label_add_article($link, $entry_ref_id, $caption, $owner_uid);
1067 }
1068 }
1069 }
1070 }
1071
2c08214a
AD
1072 if ($debug_enabled) {
1073 _debug("update_rss_feed: article processed");
1074 }
1075 }
1076
1077 if (!$last_updated) {
1078 if ($debug_enabled) {
1079 _debug("update_rss_feed: new feed, catching it up...");
1080 }
1081 catchup_feed($link, $feed, false, $owner_uid);
1082 }
1083
1084 if ($debug_enabled) {
1085 _debug("purging feed...");
1086 }
1087
1088 purge_feed($link, $feed, 0, $debug_enabled);
1089
1090 db_query($link, "UPDATE ttrss_feeds
f2798eb6 1091 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
2c08214a
AD
1092
1093// db_query($link, "COMMIT");
1094
1095 } else {
1096
3972bf59 1097 $error_msg = db_escape_string($link, mb_substr($rss->error(), 0, 245));
2c08214a
AD
1098
1099 if ($debug_enabled) {
1100 _debug("update_rss_feed: error fetching feed: $error_msg");
1101 }
1102
2c08214a
AD
1103 db_query($link,
1104 "UPDATE ttrss_feeds SET last_error = '$error_msg',
1105 last_updated = NOW() WHERE id = '$feed'");
1106 }
1107
19b3992b 1108 unset($rss);
2c08214a
AD
1109
1110 if ($debug_enabled) {
1111 _debug("update_rss_feed: done");
1112 }
1113
1114 }
1115
3c696512
AD
1116 function cache_images($html, $site_url, $debug) {
1117 $cache_dir = CACHE_DIR . "/images";
1118
1119 libxml_use_internal_errors(true);
1120
1121 $charset_hack = '<head>
1122 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
1123 </head>';
1124
1125 $doc = new DOMDocument();
1126 $doc->loadHTML($charset_hack . $html);
1127 $xpath = new DOMXPath($doc);
1128
1129 $entries = $xpath->query('(//img[@src])');
1130
1131 foreach ($entries as $entry) {
1132 if ($entry->hasAttribute('src')) {
1133 $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
1134
1135 $local_filename = CACHE_DIR . "/images/" . sha1($src) . ".png";
1136
1137 if ($debug) _debug("cache_images: downloading: $src to $local_filename");
1138
1139 if (!file_exists($local_filename)) {
1140 $file_content = fetch_file_contents($src);
1141
b8379e69 1142 if ($file_content && strlen($file_content) > 1024) {
3c696512
AD
1143 file_put_contents($local_filename, $file_content);
1144 }
1145 }
1146
1147 if (file_exists($local_filename)) {
1148 $entry->setAttribute('src', SELF_URL_PATH . '/image.php?url=' .
487f0750 1149 base64_encode($src));
3c696512
AD
1150 }
1151 }
1152 }
1153
1154 $node = $doc->getElementsByTagName('body')->item(0);
1155
cc38c8e5 1156 return $doc->saveXML($node);
3c696512
AD
1157 }
1158
2a91b6ff
AD
1159 function expire_lock_files($debug) {
1160 if ($debug) _debug("Removing old lock files...");
1161
1162 $num_deleted = 0;
1163
1164 if (is_writable(LOCK_DIRECTORY)) {
1165 $files = glob(LOCK_DIRECTORY . "/*.lock");
1166
1167 if ($files) {
1168 foreach ($files as $file) {
1169 if (!file_is_locked($file) && time() - filemtime($file) > 86400*2) {
1170 unlink($file);
1171 ++$num_deleted;
1172 }
1173 }
1174 }
1175 }
1176
1177 if ($debug) _debug("Removed $num_deleted files.");
1178 }
1179
3c696512 1180 function expire_cached_files($debug) {
19b3992b 1181 foreach (array("simplepie", "images", "export") as $dir) {
3c696512 1182 $cache_dir = CACHE_DIR . "/$dir";
2c08214a 1183
3c696512 1184 if ($debug) _debug("Expiring $cache_dir");
2c08214a 1185
3c696512
AD
1186 $num_deleted = 0;
1187
1188 if (is_writable($cache_dir)) {
1189 $files = glob("$cache_dir/*");
1190
2a91b6ff 1191 if ($files) {
2ab20c31
AD
1192 foreach ($files as $file) {
1193 if (time() - filemtime($file) > 86400*7) {
1194 unlink($file);
3c696512 1195
2ab20c31
AD
1196 ++$num_deleted;
1197 }
3c696512
AD
1198 }
1199 }
2a91b6ff 1200 }
3c696512
AD
1201
1202 if ($debug) _debug("Removed $num_deleted files.");
1203 }
1204 }
2c08214a 1205
a3e0bdcf
AD
1206 /**
1207 * Source: http://www.php.net/manual/en/function.parse-url.php#104527
1208 * Returns the url query as associative array
1209 *
1210 * @param string query
1211 * @return array params
1212 */
1213 function convertUrlQuery($query) {
1214 $queryParts = explode('&', $query);
1215
1216 $params = array();
1217
1218 foreach ($queryParts as $param) {
1219 $item = explode('=', $param);
1220 $params[$item[0]] = $item[1];
1221 }
1222
1223 return $params;
1224 }
92c14e9d
AD
1225
1226 function get_article_filters($filters, $title, $content, $link, $timestamp, $author, $tags) {
1227 $matches = array();
1228
1229 foreach ($filters as $filter) {
1230 $match_any_rule = $filter["match_any_rule"];
a3a896a1 1231 $inverse = $filter["inverse"];
92c14e9d
AD
1232 $filter_match = false;
1233
1234 foreach ($filter["rules"] as $rule) {
1235 $match = false;
1236 $reg_exp = $rule["reg_exp"];
a3a896a1 1237 $rule_inverse = $rule["inverse"];
92c14e9d
AD
1238
1239 if (!$reg_exp)
1240 continue;
1241
1242 switch ($rule["type"]) {
1243 case "title":
1244 $match = @preg_match("/$reg_exp/i", $title);
1245 break;
1246 case "content":
d03ae73e
AD
1247 // we don't need to deal with multiline regexps
1248 $content = preg_replace("/[\r\n\t]/", "", $content);
1249
92c14e9d
AD
1250 $match = @preg_match("/$reg_exp/i", $content);
1251 break;
1252 case "both":
d03ae73e
AD
1253 // we don't need to deal with multiline regexps
1254 $content = preg_replace("/[\r\n\t]/", "", $content);
1255
72d1d067 1256 $match = (@preg_match("/$reg_exp/i", $title) || @preg_match("/$reg_exp/i", $content));
92c14e9d
AD
1257 break;
1258 case "link":
1259 $match = @preg_match("/$reg_exp/i", $link);
1260 break;
1261 case "author":
1262 $match = @preg_match("/$reg_exp/i", $author);
1263 break;
1264 case "tag":
1265 $tag_string = join(",", $tags);
1266 $match = @preg_match("/$reg_exp/i", $tag_string);
1267 break;
1268 }
1269
a3a896a1
AD
1270 if ($rule_inverse) $match = !$match;
1271
92c14e9d
AD
1272 if ($match_any_rule) {
1273 if ($match) {
1274 $filter_match = true;
1275 break;
1276 }
1277 } else {
1278 $filter_match = $match;
1279 if (!$match) {
1280 break;
1281 }
1282 }
1283 }
1284
a3a896a1
AD
1285 if ($inverse) $filter_match = !$filter_match;
1286
92c14e9d
AD
1287 if ($filter_match) {
1288 foreach ($filter["actions"] AS $action) {
1289 array_push($matches, $action);
1290 }
1291 }
1292 }
1293
1294 return $matches;
1295 }
1296
1297 function find_article_filter($filters, $filter_name) {
1298 foreach ($filters as $f) {
1299 if ($f["type"] == $filter_name) {
1300 return $f;
1301 };
1302 }
1303 return false;
1304 }
1305
1306 function find_article_filters($filters, $filter_name) {
1307 $results = array();
1308
1309 foreach ($filters as $f) {
1310 if ($f["type"] == $filter_name) {
1311 array_push($results, $f);
1312 };
1313 }
1314 return $results;
1315 }
1316
1317 function calculate_article_score($filters) {
1318 $score = 0;
1319
1320 foreach ($filters as $f) {
1321 if ($f["type"] == "score") {
1322 $score += $f["param"];
1323 };
1324 }
1325 return $score;
1326 }
1327
b24504b1
AD
1328 function labels_contains_caption($labels, $caption) {
1329 foreach ($labels as $label) {
1330 if ($label[1] == $caption) {
1331 return true;
1332 }
1333 }
1334
1335 return false;
1336 }
1337
1338 function assign_article_to_label_filters($link, $id, $filters, $owner_uid, $article_labels) {
92c14e9d
AD
1339 foreach ($filters as $f) {
1340 if ($f["type"] == "label") {
b24504b1
AD
1341 if (!labels_contains_caption($article_labels, $f["param"])) {
1342 label_add_article($link, $id, $f["param"], $owner_uid);
1343 }
1344 }
92c14e9d
AD
1345 }
1346 }
87764a50 1347
87d7e850
AD
1348 function make_guid_from_title($title) {
1349 return preg_replace("/[ \"\',.:;]/", "-",
1350 mb_strtolower(strip_tags($title), 'utf-8'));
1351 }
1352
1353
2c08214a 1354?>