2 require_once("include/html2bbcode.php");
3 require_once("include/items.php");
6 * @brief Read a RSS/RDF/Atom feed and create an item entry for it
8 * @param string $xml The feed data
9 * @param array $importer The user record of the importer
10 * @param array $contact The contact record of the feed
11 * @param string $hub Unused dummy value for compatibility reasons
12 * @param bool $simulate If enabled, no data is imported
14 * @return array In simulation mode it returns the header and the first item
16 function feed_import($xml,$importer,&$contact, &$hub, $simulate = false) {
21 logger("Import Atom/RSS feed '".$contact["name"]."' (Contact ".$contact["id"].") for user ".$importer["uid"], LOGGER_DEBUG);
23 logger("Test Atom/RSS feed", LOGGER_DEBUG);
26 logger('XML is empty.', LOGGER_DEBUG);
30 $doc = new DOMDocument();
32 $xpath = new DomXPath($doc);
33 $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
34 $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
35 $xpath->registerNamespace('content', "http://purl.org/rss/1.0/modules/content/");
36 $xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
37 $xpath->registerNamespace('rss', "http://purl.org/rss/1.0/");
38 $xpath->registerNamespace('media', "http://search.yahoo.com/mrss/");
39 $xpath->registerNamespace('poco', NAMESPACE_POCO);
44 if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
45 $author["author-link"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:link/text()')->item(0)->nodeValue;
46 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:title/text()')->item(0)->nodeValue;
48 if ($author["author-name"] == "")
49 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:description/text()')->item(0)->nodeValue;
51 $entries = $xpath->query('/rdf:RDF/rss:item');
55 if ($xpath->query('/atom:feed')->length > 0) {
56 $alternate = $xpath->query("atom:link[@rel='alternate']")->item(0)->attributes;
57 if (is_object($alternate))
58 foreach($alternate AS $attributes)
59 if ($attributes->name == "href")
60 $author["author-link"] = $attributes->textContent;
62 if ($author["author-link"] == "")
63 $author["author-link"] = $author["author-id"];
65 if ($author["author-link"] == "") {
66 $self = $xpath->query("atom:link[@rel='self']")->item(0)->attributes;
68 foreach($self AS $attributes)
69 if ($attributes->name == "href")
70 $author["author-link"] = $attributes->textContent;
73 if ($author["author-link"] == "")
74 $author["author-link"] = $xpath->evaluate('/atom:feed/atom:id/text()')->item(0)->nodeValue;
76 $author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue;
78 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue;
80 if ($author["author-name"] == "")
81 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue;
83 if ($author["author-name"] == "")
84 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue;
86 $value = $xpath->evaluate('atom:author/poco:displayName/text()')->item(0)->nodeValue;
88 $author["author-name"] = $value;
91 $author["author-id"] = $xpath->evaluate('/atom:feed/atom:author/atom:uri/text()')->item(0)->nodeValue;
93 $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()')->item(0)->nodeValue;
95 $author["author-nick"] = $value;
97 $value = $xpath->evaluate('atom:author/poco:address/poco:formatted/text()', $context)->item(0)->nodeValue;
99 $author["author-location"] = $value;
101 $value = $xpath->evaluate('atom:author/poco:note/text()')->item(0)->nodeValue;
103 $author["author-about"] = $value;
107 $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
109 $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
111 $entries = $xpath->query('/atom:feed/atom:entry');
115 if ($xpath->query('/rss/channel')->length > 0) {
116 $author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
118 $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
119 $author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
121 if ($author["author-name"] == "")
122 $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
124 if ($author["author-name"] == "")
125 $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
127 $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
129 $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
131 $entries = $xpath->query('/rss/channel/item');
135 $author["author-link"] = $contact["url"];
137 if ($author["author-name"] == "")
138 $author["author-name"] = $contact["name"];
140 $author["author-avatar"] = $contact["thumb"];
142 $author["owner-link"] = $contact["url"];
143 $author["owner-name"] = $contact["name"];
144 $author["owner-avatar"] = $contact["thumb"];
148 $header["uid"] = $importer["uid"];
149 $header["network"] = NETWORK_FEED;
150 $header["type"] = "remote";
152 $header["origin"] = 0;
153 $header["gravity"] = GRAVITY_PARENT;
154 $header["private"] = 2;
155 $header["verb"] = ACTIVITY_POST;
156 $header["object-type"] = ACTIVITY_OBJ_NOTE;
158 $header["contact-id"] = $contact["id"];
160 if(!strlen($contact["notify"])) {
161 // one way feed - no remote comment ability
162 $header["last-child"] = 0;
165 if (!is_object($entries)) {
166 logger("There are no entries in this feed.", LOGGER_DEBUG);
172 $entrylist = array();
174 foreach ($entries AS $entry)
175 $entrylist[] = $entry;
177 foreach (array_reverse($entrylist) AS $entry) {
178 $item = array_merge($header, $author);
180 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
182 if ($item["title"] == "")
183 $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
185 if ($item["title"] == "")
186 $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
188 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
189 if (!is_object($alternate))
190 $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
192 if (is_object($alternate))
193 foreach($alternate AS $attributes)
194 if ($attributes->name == "href")
195 $item["plink"] = $attributes->textContent;
197 if ($item["plink"] == "")
198 $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
200 if ($item["plink"] == "")
201 $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
203 $item["plink"] = original_url($item["plink"]);
205 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
207 if ($item["uri"] == "")
208 $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
210 if ($item["uri"] == "")
211 $item["uri"] = $item["plink"];
213 $item["parent-uri"] = $item["uri"];
215 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
217 if ($published == "")
218 $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
220 if ($published == "")
221 $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
223 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
226 $updated = $published;
228 if ($published != "")
229 $item["created"] = $published;
232 $item["edited"] = $updated;
234 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
237 $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
240 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
243 $item["author-name"] = $creator;
246 $item["edited"] = $item["created"] = $pubDate;
248 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
251 $item["author-name"] = $creator;
254 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s', '%s')",
255 intval($importer["uid"]), dbesc($item["uri"]), dbesc(NETWORK_FEED), dbesc(NETWORK_DFRN));
257 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
263 // <category>Ausland</category>
264 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
266 $attachments = array();
268 $enclosures = $xpath->query("enclosure", $entry);
269 foreach ($enclosures AS $enclosure) {
275 foreach($enclosure->attributes AS $attributes) {
276 if ($attributes->name == "url")
277 $href = $attributes->textContent;
278 elseif ($attributes->name == "length")
279 $length = $attributes->textContent;
280 elseif ($attributes->name == "type")
281 $type = $attributes->textContent;
283 if(strlen($item["attach"]))
284 $item["attach"] .= ',';
286 $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
288 $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
291 if ($contact["fetch_further_information"]) {
294 // Handle enclosures and treat them as preview picture
295 foreach ($attachments AS $attachment)
296 if ($attachment["type"] == "image/jpeg")
297 $preview = $attachment["link"];
299 $item["body"] = $item["title"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
300 $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
302 $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
303 unset($item["attach"]);
305 $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
308 $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
311 $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
314 $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
316 // remove the content of the title if it is identically to the body
317 // This helps with auto generated titles e.g. from tumblr
318 if (title_is_body($item["title"], $body))
321 $item["body"] = html2bbcode($body);
325 logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
327 $notify = item_is_remote_self($contact, $item);
329 // Distributed items should have a well formatted URI.
330 // Additionally we have to avoid conflicts with identical URI between imported feeds and these items.
333 unset($item['parent-uri']);
336 $id = item_store($item, false, $notify);
338 logger("Feed for contact ".$contact["url"]." stored under id ".$id);
347 return array("header" => $author, "items" => $items);