-
Notifications
You must be signed in to change notification settings - Fork 3
/
fetch.php
164 lines (149 loc) · 4.2 KB
/
fetch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
class com_meego_planet_fetch
{
public static function get_feeds()
{
$q = new midgard_query_select
(
new midgard_query_storage('com_meego_planet_feed')
);
$q->execute();
return $q->list_objects();
}
public static function fetch_feed(com_meego_planet_feed $feed, $index, $item_callback)
{
if (!is_callable($item_callback))
{
throw InvalidArgumentException('Item processing callback must be a valid function');
}
midgardmvc_core::get_instance()->component->load_library('Feed');
try
{
$fetched = ezcFeed::parse($feed->feed);
}
catch (Exception $e)
{
midgardmvc_core::get_instance()->log(__CLASS__, $e->getMessage(), 'info');
return false;
}
$items = $fetched->item;
array_walk($items, $item_callback, $feed);
self::remove_missing($items, $feed);
}
public static function remove_missing(array $items, com_meego_planet_feed $feed)
{
$urls = array_map
(
'com_meego_planet_fetch::get_item_link',
$items
);
array_walk
(
com_meego_planet_utils::get_items_for_feed($feed),
function ($item) use ($urls)
{
if (!in_array($item->url, $urls))
{
$item->delete();
}
}
);
}
public static function get_item_link(ezcFeedEntryElement $item)
{
return $item->link[0]->href;
}
public static function import_item(ezcFeedEntryElement $feed_item, $index, com_meego_planet_feed $feed)
{
if (!self::get_item_link($feed_item))
{
return;
}
$item = self::get_item_by_url(self::get_item_link($feed_item));
$item->feed = $feed->id;
$dirty = false;
if (self::set_item_value($item, 'title', $feed_item->title->text))
{
$dirty = true;
}
if (isset($feed_item->description)) {
if (self::set_item_value($item, 'content', $feed_item->description->text))
{
$dirty = true;
}
}
if (!is_null($feed_item->published))
{
if (self::set_item_value($item->metadata, 'published', $feed_item->published->date))
{
$dirty = true;
}
}
elseif (!$item->guid)
{
if (self::set_item_value($item->metadata, 'published', new DateTime()))
{
$dirty = true;
}
}
if (!$dirty)
{
return;
}
if (strlen($item->url))
{
if (!$item->guid)
{
// New item
$item->create();
return;
}
$item->update();
}
}
private static function get_item_by_url($url)
{
$q = new midgard_query_select
(
new midgard_query_storage('com_meego_planet_item')
);
$q->add_order(new midgard_query_property('metadata.score'), SORT_DESC);
$q->set_constraint
(
new midgard_query_constraint
(
new midgard_query_property('url'),
'=',
new midgard_query_value($url)
)
);
$q->execute();
if ($q->resultscount == 0)
{
// New item
$item = new com_meego_planet_item();
$item->url = $url;
return $item;
}
$list_of_items = $q->list_objects();
return $list_of_items[0];
}
private static function set_item_value($item, $property, $value)
{
if ($item->$property instanceof midgard_datetime)
{
if ($item->$property->getTimestamp() == $value->getTimestamp())
{
return false;
}
$item->$property->setTimestamp($value->getTimestamp());
return true;
}
if ($item->$property == $value)
{
return false;
}
$item->$property = $value;
return true;
}
}