/[drupal]/contributions/sandbox/alex_b/feedapi_node_update/feedapi_node_update.module
ViewVC logotype

Contents of /contributions/sandbox/alex_b/feedapi_node_update/feedapi_node_update.module

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


Revision 1.1 - (show annotations) (download) (as text)
Thu Jan 17 18:57:24 2008 UTC (22 months, 1 week ago) by alexb
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/x-php
Add feedapi node update module to sandbox. This module updates a non x feed deduping installation to an x feed deduping one.
1 <?php
2 /**
3 * @abstract Update module for feedapi_node.module.
4 *
5 * Configures and converts an existing feedapi installation to use
6 * cross feed duplicate checking. When cross feed duplicate checking is enabled,
7 * feedapi does not create a feed item, if it exists on ANY feed, whereas normally,
8 * duplicates are only searched for _within_ the feed item's feed.
9 *
10 * This module converts all existing feeds to use cross feed duplicate checking on cron.
11 *
12 */
13
14 define("FEEDAPI_NODE_UPDATE_CRON_TIMEOUT", 60);
15
16 function feedapi_node_update_cron() {
17 $worked = FALSE;
18 $start_time = time();
19 while (time() < (FEEDAPI_NODE_UPDATE_CRON_TIMEOUT + $start_time) ) {
20 // Set all feedapi enabled content types to x feed de-duping.
21 if (!variable_get('feedapi_node_update_node_types', 0)) {
22 variable_set('feedapi_node_update_node_types', 1);
23 $worked = TRUE;
24 $types = array_keys(node_get_types('types'));
25 foreach ($types as $type) {
26 if ($settings = _feedapi_get_settings(array('node_type' => $type))) {
27 $settings['processors']['feedapi_node']['x_dedupe'] = 1;
28 _feedapi_store_settings(array('node_type' => $type), $settings);
29 }
30 }
31 }
32
33 // Set all feeds to x feed de-duping.
34 $result = db_query_range(
35 'SELECT DISTINCT n.title, n.type, n.nid
36 FROM {feedapi_node_item_feed} ff
37 JOIN {node} n ON n.nid = ff.feed_nid
38 WHERE n.nid > %d
39 ORDER BY n.nid ASC', variable_get('feedapi_node_update_last_nid', 0), 0, 100);
40 $last_nid = 0;
41 while ($feed = db_fetch_object($result)) {
42 $worked = TRUE;
43 if ($settings = _feedapi_get_settings(array('node_type' => $feed->type, 'nid' => $feed->nid))) {
44 $settings['processors']['feedapi_node']['x_dedupe'] = 1;
45 _feedapi_store_settings(array('node_type' => $feed->type, 'nid' => $feed->nid), $settings);
46 }
47 else {
48 watchdog('feedapi_node_update', t("No settings for feed found."), WATCHDOG_ERROR, l($feed->title, "node/". $feed->nid));
49 }
50 $last_nid = $feed->nid;
51 $results = TRUE;
52 }
53 if ($last_nid) {
54 variable_set('feedapi_node_update_last_nid', $last_nid);
55 }
56 else {
57 break;
58 }
59 }
60
61 // Remove x feed duplicate feed items.
62 foreach (array('url', 'guid') as $field) {
63 $result = db_query(
64 "SELECT *,
65 GROUP_CONCAT(fni.nid ORDER BY fni.nid ASC SEPARATOR ',') AS feed_item_nids,
66 COUNT(*) AS cnt
67 FROM {feedapi_node_item} fni
68 GROUP BY fni.%s
69 ORDER BY cnt DESC", $field);
70 while ($dupe_set = db_fetch_object($result)) {
71 // drupal_set_message(dprint_r($dupe_set, true));
72 if ($dupe_set->cnt < 2) {
73 break;
74 }
75 if (time() > (FEEDAPI_NODE_UPDATE_CRON_TIMEOUT + $start_time)) {
76 break;
77 }
78 $worked = TRUE;
79 $feed_item_nids = explode(',', $dupe_set->feed_item_nids);
80 $first_nid = array_shift($feed_item_nids);
81 $first_node = node_load($first_nid);
82
83 // Get feeds associated to all feed items except the remaining feed item.
84 // We need to associate those feeds to the remaining feed item.
85 $r = db_query('SELECT feed_nid FROM feedapi_node_item_feed WHERE feed_item_nid IN (%s)', implode(", ", $feed_item_nids));
86
87 while ($feed = db_fetch_object($r)) {
88 $first_node->feedapi_node->feed_nids[$feed->feed_nid] = $feed->feed_nid;
89 }
90 node_save($first_node);
91
92 // Delete all feed item nodes except the first one.
93 foreach ($feed_item_nids as $nid) {
94 // drupal_set_message("DELETE $nid");
95 node_delete($nid);
96 }
97 }
98 }
99
100 if (!$worked) {
101 watchdog('feedapi_node_update', t("Ran feedapi_node_update_cron() but update already done. You can turn off feedapi_node_update module now."));
102 }
103 }

  ViewVC Help
Powered by ViewVC 1.1.2