PluginProbe
FeedWordPress / 2010.0903
FeedWordPress v2010.0903
trunk 0.8 0.9 0.91 0.95 0.96 0.97 0.98 0.981 0.99 0.991 0.992 0.993 2008.1030 2008.1101 2008.1105 2008.1214 2009.0612 2009.0613 2009.0618 2009.0707 2009.1111 2009.1112 2010.0127 2010.0528 All 65 releases
feedwordpress / magpiefromsimplepie.class.php

magpiefromsimplepie.class.php in FeedWordPress 2010.0903, at magpiefromsimplepie.class.php

779 lines 25.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 require_once(dirname(__FILE__).'/feedtime.class.php');
3
4 /**
5 * class MagpieFromSimplePie: compatibility layer to prevent existing filters
6 * from breaking.
7 *
8 * @since 2010.0203
9 * @version 2010.0612
10 */
11
12 class MagpieFromSimplePie {
13 var $pie;
14 var $originals;
15
16 var $channel;
17 var $items;
18 var $textinput = array ();
19 var $image = array();
20
21 var $feed_type;
22 var $feed_version;
23
24 var $_XMLNS_FAMILIAR = array (
25 'http://www.w3.org/2005/Atom' => 'atom' /* 1.0 */,
26 'http://purl.org/atom/ns#' => 'atom' /* pre-1.0 */,
27 'http://purl.org/rss/1.0/' => 'rss' /* 1.0 */,
28 'http://backend.userland.com/RSS2' => 'rss' /* 2.0 */,
29 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' => 'rdf',
30 'http://www.w3.org/1999/xhtml' => 'xhtml',
31 'http://purl.org/dc/elements/1.1/' => 'dc',
32 'http://purl.org/dc/terms/' => 'dcterms',
33 'http://purl.org/rss/1.0/modules/content/' => 'content',
34 'http://purl.org/rss/1.0/modules/syndication/' => 'sy',
35 'http://purl.org/rss/1.0/modules/taxonomy/' => 'taxo',
36 'http://purl.org/rss/1.0/modules/dc/' => 'dc',
37 'http://wellformedweb.org/CommentAPI/' => 'wfw',
38 'http://webns.net/mvcb/' => 'admin',
39 'http://purl.org/rss/1.0/modules/annotate/' => 'annotate',
40 'http://xmlns.com/foaf/0.1/' => 'foaf',
41 'http://madskills.com/public/xml/rss/module/trackback/' => 'trackback',
42 'http://web.resource.org/cc/' => 'cc',
43 'http://search.yahoo.com/mrss' => 'media',
44 'http://search.yahoo.com/mrss/' => 'media',
45 'http://video.search.yahoo.com/mrss' => 'media',
46 'http://video.search.yahoo.com/mrss/' => 'media',
47 'http://purl.org/syndication/thread/1.0' => 'thr',
48 'http://purl.org/syndication/thread/1.0/' => 'thr',
49 'http://www.w3.org/XML/1998/namespace' => 'xml',
50 'http://www.itunes.com/dtds/podcast-1.0.dtd' => 'itunes',
51 'http://a9.com/-/spec/opensearchrss/1.0/' => 'openSearch',
52 'http://purl.org/rss/1.0/modules/slash/' => 'slash',
53 'http://www.google.com/schemas/reader/atom/' => 'gr',
54 'urn:atom-extension:indexing' => 'indexing',
55 );
56
57 /**
58 * MagpieFromSimplePie constructor
59 *
60 * @param SimplePie $pie The feed to convert to MagpieRSS format.
61 *
62 * @uses MagpieFromSimplePie::processItemData
63 * @uses MagpieFromSimplePie::normalize
64 */
65 function MagpieFromSimplePie ($pie) {
66 $this->pie = $pie;
67 $this->originals = $this->pie->get_items();
68
69 $this->channel = $this->processFeedData($this->pie->data);
70 foreach ($this->originals as $key => $item) :
71 $this->items[$key] = $this->processItemData($item->data);
72 endforeach;
73
74 $this->normalize();
75
76 // In case anyone goes poking around our private members (uh...)
77 $this->feed_type = ($this->is_atom() ? 'Atom' : 'RSS');
78 $this->feed_version = $this->feed_version();
79 $this->encoding = $pie->get_encoding();
80 } /* MagpieFromSimplePie constructor */
81
82 /**
83 * MagpieFromSimplePie::get_item: returns a MagpieRSS format array
84 * which is equivalent to the SimplePie_Item object from which this
85 * object was constructed.
86 *
87 * @return array A MagpieRSS format array representing this feed item.
88 */
89 function get_items () {
90 return $this->items;
91 } /* MagpieFromSimplePie::get_item */
92
93 /**
94 * MagpieFromSimplePie::processFeedData
95 *
96 * @param array $data
97 * @return array
98 *
99 * @uses MagpieFromSimplePie::processChannelData()
100 */
101 function processFeedData ($data) {
102 $ret = array();
103 if (isset($data['child'])) : foreach ($data['child'] as $ns => $elements) :
104 foreach ($elements as $name => $multi) :
105 foreach ($multi as $element) :
106 if ($name=='feed' or $name=='channel') :
107 // Don't need to process these
108 foreach (array(
109 '',
110 'http://www.w3.org/2005/Atom',
111 'http://purl.org/rss/1.0/',
112 'http://backend.userland.com/RSS2'
113 ) as $ns) :
114 if (isset($element['child'][$ns]['entry'])) : unset($element['child'][$ns]['entry']); endif;
115 if (isset($element['child'][$ns]['item'])) : unset($element['child'][$ns]['item']); endif;
116 endforeach;
117
118 $ret = $this->processChannelData($element) + $ret;
119 elseif (in_array(strtolower($name), array('rss', 'rdf'))) :
120 // Drop down to get to <channel> element
121 $ret = $this->processFeedData($element) + $ret;
122 endif;
123 endforeach;
124 endforeach;
125 endforeach; endif;
126 return $ret;
127 } /* MagpieFromSimplePie::processFeedData() */
128
129 /**
130 * MagpieFromSimplePie::processChannelData
131 *
132 * @param array $data
133 * @param array $path
134 * @return array
135 *
136 * @uses MagpieFromSimplePie::handleAttributes
137 * @uses MagpieFromSimplePie::handleChildren
138 */
139 function processChannelData ($data, $path = array()) {
140 $ret = array();
141 $tagPath = strtolower(implode('_', $path));
142
143 // Only process at the right level
144 if (strlen($tagPath) > 0
145 and isset($data['data'])
146 and strlen($data['data']) > 0) :
147 $ret[$tagPath] = $data['data'];
148 endif;
149
150 $ret = $this->handleAttributes($data, $path)
151 + $this->handleChildren($data, $path, 'processChannelData')
152 + $ret;
153
154 return $ret;
155 } /* MagpieFromSimplePie::processChannelData() */
156
157 /**
158 * MagpieFromSimplePie::processItemData
159 *
160 * @param array $data
161 * @param array $path
162 * @return array
163 *
164 * @uses MagpieFromSimplePie::handleAttributes
165 * @uses MagpieFromSimplePie::handleChildren
166 */
167 function processItemData ($data, $path = array()) {
168 $ret = array();
169 $tagPath = strtolower(implode('_', $path));
170
171 if (strlen($tagPath) > 0 and isset($data['data']) and strlen($data['data']) > 0) :
172 $ret[$tagPath] = $data['data'];
173 endif;
174
175 // Set up xml:base to be recorded in array
176 if (isset($data['xml_base_explicit']) and $data['xml_base_explicit']) :
177 $data['attribs']['']['xml:base'] = $data['xml_base'];
178 endif;
179
180 $ret = $this->handleAttributes($data, $path)
181 + $this->handleChildren($data, $path, 'processItemData')
182 + $ret;
183
184 return $ret;
185 } /* MagpieFromSimplePie::processItemData() */
186
187 /**
188 * MagpieFromSimplePie::handleAttributes
189 *
190 * @param array $data
191 * @param array $path
192 * @return array
193 */
194 function handleAttributes ($data, $path) {
195 $tagPath = strtolower(implode('_', $path));
196 $ret = array();
197 if (isset($data['attribs'])) : foreach ($data['attribs'] as $ns => $pairs) :
198 if (isset($this->_XMLNS_FAMILIAR[$ns])) :
199 $ns = $this->_XMLNS_FAMILIAR[$ns];
200 endif;
201
202 foreach ($pairs as $attr => $value) :
203 $attr = strtolower($attr);
204 if ($ns=='rdf' and $attr=='about') :
205 $ret['about'] = $value;
206 elseif (strlen($tagPath) > 0) :
207 if (strlen($ns) > 0 and $this->is_namespaced($ns, /*attrib=*/ true)) :
208 $attr = $ns.':'.$attr;
209 endif;
210
211 $ret[$tagPath.'@'.$attr] = $value;
212 if (isset($ret[$tagPath.'@']) and strlen($ret[$tagPath.'@'])>0) :
213 $ret[$tagPath.'@'] .= ',';
214 else :
215 $ret[$tagPath.'@'] = '';
216 endif;
217 $ret[$tagPath.'@'] .= $attr;
218 endif;
219 endforeach;
220 endforeach; endif;
221 return $ret;
222 } /* MagpieFromSimplePie::handleAttributes() */
223
224 var $inImage = false;
225 var $inTextInput = false;
226
227 /**
228 * MagpieFromSimplePie::handleChildren
229 *
230 * @param array $data
231 * @param array $path
232 * @return array
233 *
234 * @uses MagpieFromSimplePie::get_attrib
235 * @uses MagpieFromSimplePie::is_atom
236 * @uses MagpieFromSimplePie::increment_element
237 * @uses MagpieFromSimplePie::processItemData
238 */
239 function handleChildren ($data, $path = array(), $method = 'processItemData') {
240 $tagPath = strtolower(implode('_', $path));
241 $ret = array();
242 if (isset($data['child'])) : foreach ($data['child'] as $ns => $elements) :
243 if (isset($this->_XMLNS_FAMILIAR[$ns])) :
244 $ns = $this->_XMLNS_FAMILIAR[$ns];
245 endif;
246 if (''==$ns) :
247 $ns = ($this->is_atom() ? 'atom' : 'rss');
248 endif;
249
250 foreach ($elements as $tag => $multi) : foreach ($multi as $element) :
251 $copyOver = NULL;
252
253 if ('image'==$tag and 'rss'==$ns) :
254 $this->inImage = true;
255 $childPath = array();
256 $co = NULL;
257 elseif ('textinput'==strtolower($tag) and 'rss'==$ns) :
258 $this->inTextInput = true;
259 $childPath = array();
260 $co = NULL;
261 else :
262 // Determine tag name; check #; increment #
263 $childTag = strtolower($tag);
264 if ('link'==$tag and 'atom'==$ns) :
265 $rel = $this->get_attrib(
266 /*ns=*/ array('', 'http://www.w3.org/2005/Atom'),
267 /*attr=*/ 'rel',
268 $element
269 );
270 if ($rel != 'alternate') :
271 $childTag .= '_'.$rel;
272 endif;
273 $copyOver = $this->get_attrib(
274 /*ns=*/ array('', 'http://www.w3.org/2005/Atom'),
275 /*attr=*/ 'href',
276 $element
277 );
278 elseif ('content'==$tag and 'atom'==$ns) :
279 $childTag = 'atom_'.$tag;
280 endif;
281
282 $childTag = $this->increment_element($ret, $childTag, $ns, $path);
283 $childPath = $path; $childPath[] = strtolower($childTag);
284
285 if (!is_null($copyOver)) :
286 $co = array();
287 $co[implode('_', $childPath)] = $copyOver;
288 else :
289 $co = NULL;
290 endif;
291 endif;
292
293 $arr = $this->{$method}($element, $childPath);
294 if ($co) :
295 $arr = $co + $arr; // Left-hand overwrites right-hand
296 endif;
297
298 if ($this->inImage) :
299 $this->image = $arr + $this->image;
300
301 // Close tag
302 if ('image'==$tag and 'rss'==$ns) : $this->inImage = false; endif;
303 elseif ($this->inTextInput) :
304 $this->textinput = $arr + $this->textinput;
305
306 // Close tag
307 if ('textinput'==$tag and 'rss'==$ns) : $this->inTextInput = false; endif;
308 elseif ($this->is_namespaced($ns)) :
309 if (!isset($ret[$ns])) : $ret[$ns] = array(); endif;
310 $ret[$ns] = $arr + $ret[$ns];
311 else :
312 $ret = $arr + $ret;
313 endif;
314 endforeach; endforeach;
315
316 endforeach; endif;
317 return $ret;
318 } /* MagpieFromSimplePie::handleChildren() */
319
320 /**
321 * MagpieFromSimplePie::get_attrib
322 *
323 * @param array $namespaces
324 * @param string $attr
325 * @param array $element
326 * @param mixed $default
327 */
328 function get_attrib ($namespaces, $attr, $element, $default = NULL) {
329 $ret = $default;
330 if (isset($element['attribs'])) :
331 foreach ($namespaces as $ns) :
332 if (isset($element['attribs'][$ns])
333 and isset($element['attribs'][$ns][$attr])) :
334 $ret = $element['attribs'][$ns][$attr];
335 break;
336 endif;
337 endforeach;
338 endif;
339 return $ret;
340 } /* MagpieFromSimplePie::get_attrib */
341
342 /**
343 * MagpieFromSimplePie::normalize
344 *
345 * @uses MagpieFromSimplePie::is_atom
346 * @uses MagpieFromSimplePie::is_rss
347 * @uses MagpieFromSimplePie::normalize_element
348 * @uses MagpieFromSimplePie::normalize_author_inheritance
349 * @uses MagpieFromSimplePie::normalize_atom_person
350 * @uses MagpieFromSimplePie::normalize_enclosure
351 * @uses MagpieFromSimplePie::normalize_category
352 * @uses MagpieFromSimplePie::normalize_dc_subject
353 * @uses FeedTime
354 * @uses FeedTime::timestamp
355 */
356 function normalize () {
357 // Normalize channel data
358 if ( $this->is_atom() ) :
359 // Atom 1.0 elements <=> Atom 0.3 elements (Thanks, o brilliant wordsmiths of the Atom 1.0 standard!)
360 if ($this->feed_version() < 1.0) :
361 $this->normalize_element($this->channel, 'tagline', $this->channel, 'subtitle');
362 $this->normalize_element($this->channel, 'copyright', $this->channel, 'rights');
363 $this->normalize_element($this->channel, 'modified', $this->channel, 'updated');
364 else :
365 $this->normalize_element($this->channel, 'subtitle', $this->channel, 'tagline');
366 $this->normalize_element($this->channel, 'rights', $this->channel, 'copyright');
367 $this->normalize_element($this->channel, 'updated', $this->channel, 'modified');
368 endif;
369 $this->normalize_element($this->channel, 'author', $this->channel['dc'], 'creator', 'normalize_atom_person');
370 $this->normalize_element($this->channel, 'contributor', $this->channel['dc'], 'contributor', 'normalize_atom_person');
371
372 // Atom elements to RSS elements
373 $this->normalize_element($this->channel, 'subtitle', $this->channel, 'description');
374
375 if ( isset($this->channel['logo']) ) :
376 $this->normalize_element($this->channel, 'logo', $this->image, 'url');
377 $this->normalize_element($this->channel, 'link', $this->image, 'link');
378 $this->normalize_element($this->channel, 'title', $this->image, 'title');
379 endif;
380
381 elseif ( $this->is_rss() ) :
382 // Normalize image element from where stupid MagpieRSS puts it
383 //$this->normalize_element($this->channel, 'image_title', $this->image, 'title');
384 //$this->normalize_element($this->channel, 'image_link', $this->image, 'link');
385 //$this->normalize_element($this->channel, 'image_url', $this->image, 'url');
386
387 // ... and, gag, textInput
388 //$this->normalize_element($this->channel, 'textinput_title', $this->textinput, 'title');
389 //$this->normalize_element($this->channel, 'textinput_link', $this->textinput, 'link');
390 //$this->normalize_element($this->channel, 'textinput_name', $this->textinput, 'name');
391 //$this->normalize_element($this->channel, 'textinput_description', $this->textinput, 'description');
392
393 // RSS elements to Atom elements
394 $this->normalize_element($this->channel, 'description', $this->channel, 'tagline'); // Atom 0.3
395 $this->normalize_element($this->channel, 'description', $this->channel, 'subtitle'); // Atom 1.0 (yay wordsmithing!)
396 $this->normalize_element($this->image, 'url', $this->channel, 'logo');
397 endif;
398
399 // Now loop through and normalize item data
400 for ( $i = 0; $i < count($this->items); $i++) :
401 $item = $this->items[$i];
402
403 // if atom populate rss fields and normalize 0.3 and 1.0 feeds
404 if ( $this->is_atom() ) :
405 // Atom 1.0 elements <=> Atom 0.3 elements
406 if ($this->feed_version() < 1.0) :
407 $this->normalize_element($item, 'modified', $item, 'updated');
408 $this->normalize_element($item, 'issued', $item, 'published');
409 else :
410 $this->normalize_element($item, 'updated', $item, 'modified');
411 $this->normalize_element($item, 'published', $item, 'issued');
412 endif;
413
414 $this->normalize_author_inheritance($item, $this->originals[$i]);
415
416 // Atom elements to RSS elements
417 $this->normalize_element($item, 'author', $item['dc'], 'creator', 'normalize_atom_person');
418 $this->normalize_element($item, 'contributor', $item['dc'], 'contributor', 'normalize_atom_person');
419 $this->normalize_element($item, 'summary', $item, 'description');
420 $this->normalize_element($item, 'atom_content', $item['content'], 'encoded');
421 $this->normalize_element($item, 'link_enclosure', $item, 'enclosure', 'normalize_enclosure');
422
423 // Categories
424 if ( isset($item['category#']) ) : // Atom 1.0 categories to dc:subject and RSS 2.0 categories
425 $this->normalize_element($item, 'category', $item['dc'], 'subject', 'normalize_category');
426 elseif ( isset($item['dc']['subject#']) ) : // dc:subject to Atom 1.0 and RSS 2.0 categories
427 $this->normalize_element($item['dc'], 'subject', $item, 'category', 'normalize_dc_subject');
428 endif;
429
430 // Normalized item timestamp
431 $item_date = (isset($item['published']) ) ? $item['published'] : $item['updated'];
432 elseif ( $this->is_rss() ) :
433 // RSS elements to Atom elements
434 $this->normalize_element($item, 'description', $item, 'summary');
435 $this->normalize_element($item, 'enclosure', $item, 'link_enclosure', 'normalize_enclosure');
436
437 // Categories
438 if ( isset($item['category#']) ) : // RSS 2.0 categories to dc:subject and Atom 1.0 categories
439 $this->normalize_element($item, 'category', $item['dc'], 'subject', 'normalize_category');
440 elseif ( isset($item['dc']['subject#']) ) : // dc:subject to Atom 1.0 and RSS 2.0 categories
441 $this->normalize_element($item['dc'], 'subject', $item, 'category', 'normalize_dc_subject');
442 endif;
443
444 // Normalized item timestamp
445 if (isset($item['pubdate'])) :
446 $item_date = $item['pubdate'];
447 elseif (isset($item['dc']['date'])) :
448 $item_date = $item['dc']['date'];
449 else :
450 $item_date = null;
451 endif;
452 endif;
453
454 if ( $item_date ) :
455 $date_timestamp = new FeedTime($item_date);
456
457 if (!$date_timestamp->failed()) :
458 $item['date_timestamp'] = $date_timestamp->timestamp();
459 endif;
460 endif;
461
462 $this->items[$i] = $item;
463 endfor;
464 } /* MagpieFromSimplePie::normalize() */
465
466 /**
467 * MagpieFromSimplePie::normalize_author_inheritance
468 *
469 * @param SimplePie_Item $original
470 *
471 * @uses SimplePie_Item::get_authors
472 * @uses SimplePie_Author::get_name
473 * @uses SimplePie_Author::get_link
474 * @uses SimplePie_Author::get_email
475 * @uses MagpieFromSimplePie::increment_element
476 */
477 function normalize_author_inheritance (&$item, $original) {
478 // "If an atom:entry element does not contain
479 // atom:author elements, then the atom:author elements
480 // of the contained atom:source element are considered
481 // to apply. In an Atom Feed Document, the atom:author
482 // elements of the containing atom:feed element are
483 // considered to apply to the entry if there are no
484 // atom:author elements in the locations described
485 // above." <http://atompub.org/2005/08/17/draft-ietf-atompub-format-11.html#rfc.section.4.2.1>
486 if (!isset($item["author#"])) :
487 $authors = $original->get_authors();
488 foreach ($authors as $author) :
489 $tag = $this->increment_element($item, 'author', 'atom', array());
490 $item[$tag] = $item["{$tag}_name"] = $author->get_name();
491 if ($author->get_link()) : $item["{$tag}_uri"] = $item["{$tag}_url"] = $author->get_link(); endif;
492 if ($author->get_email()) : $item["{$tag}_email"] = $author->get_email(); endif;
493 endforeach;
494 endif;
495 } /* MagpieFromSimplePie::normalize_author_inheritance() */
496
497 /**
498 * MagpieFromSimplePie::normalize_element
499 * Simplify the logic for normalize(). Makes sure that count of elements
500 * and each of multiple elements is normalized properly. If you need to
501 * mess with things like attributes or change formats or the like, pass
502 * it a callback to handle each element.
503 *
504 * @param array &$source
505 * @param string $from
506 * @param array &$dest
507 * @param string $to
508 * @param mixed $via
509 */
510 function normalize_element (&$source, $from, &$dest, $to, $via = NULL) {
511 if (isset($source[$from]) or isset($source["{$from}#"])) :
512 if (isset($source["{$from}#"])) :
513 $n = $source["{$from}#"];
514 $dest["{$to}#"] = $source["{$from}#"];
515 else :
516 $n = 1;
517 endif;
518
519 for ($i = 1; $i <= $n; $i++) :
520 if (isset($via) and is_callable(array($this, $via))) : // custom callback for ninja attacks
521 $this->{$via}($source, $from, $dest, $to, $i);
522 else : // just make it the same
523 $from_id = $this->element_id($from, $i);
524 $to_id = $this->element_id($to, $i);
525
526 if (isset($source[$from_id])) : // Avoid PHP notice nastygrams
527 $dest[$to_id] = $source[$from_id];
528 endif;
529 endif;
530 endfor;
531 endif;
532 } /* MagpieFromSimplePie::normalize_element */
533
534 /**
535 * MagpieFromSimplePie::normalize_enclosure
536 *
537 * @param array &$source
538 * @param string $from
539 * @param array &$dest
540 * @param string $to
541 * @param int $i
542 *
543 * @uses MagpieFromSimplePie::element_id
544 */
545 function normalize_enclosure (&$source, $from, &$dest, $to, $i) {
546 $id_from = $this->element_id($from, $i);
547 $id_to = $this->element_id($to, $i);
548 if (isset($source["{$id_from}@"])) :
549 foreach (explode(',', $source["{$id_from}@"]) as $attr) :
550 if ($from=='link_enclosure' and $attr=='href') : // from Atom
551 $dest["{$id_to}@url"] = $source["{$id_from}@{$attr}"];
552 $dest["{$id_to}"] = $source["{$id_from}@{$attr}"];
553 elseif ($from=='enclosure' and $attr=='url') : // from RSS
554 $dest["{$id_to}@href"] = $source["{$id_from}@{$attr}"];
555 $dest["{$id_to}"] = $source["{$id_from}@{$attr}"];
556 else :
557 $dest["{$id_to}@{$attr}"] = $source["{$id_from}@{$attr}"];
558 endif;
559 endforeach;
560 endif;
561 } /* MagpieFromSimplePie::normalize_enclosure */
562
563 /**
564 * MagpieFromSimplePie::normalize_atom_person
565 *
566 * @param array &$source
567 * @param string $person
568 * @param array &$dest
569 * @param string $to
570 * @param int $i
571 *
572 *
573 * @uses MagpieFromSimplePie::element_id
574 */
575 function normalize_atom_person (&$source, $person, &$dest, $to, $i) {
576 $id = $this->element_id($person, $i);
577 $id_to = $this->element_id($to, $i);
578
579 // Atom 0.3 <=> Atom 1.0
580 if ($this->feed_version() >= 1.0) :
581 $used = 'uri'; $norm = 'url';
582 else :
583 $used = 'url'; $norm = 'uri';
584 endif;
585
586 if (isset($source["{$id}_{$used}"])) :
587 $dest["{$id_to}_{$norm}"] = $source["{$id}_{$used}"];
588 endif;
589
590 // Atom to RSS 2.0 and Dublin Core
591 // RSS 2.0 person strings should be valid e-mail addresses if possible.
592 if (isset($source["{$id}_email"])) :
593 $rss_author = $source["{$id}_email"];
594 endif;
595 if (isset($source["{$id}_name"])) :
596 $rss_author = $source["{$id}_name"]
597 . (isset($rss_author) ? " <$rss_author>" : '');
598 endif;
599 if (isset($rss_author)) :
600 $source[$id] = $rss_author; // goes to top-level author or contributor
601 $dest[$id_to] = $rss_author; // goes to dc:creator or dc:contributor
602 endif;
603 } /* MagpieFromSimplePie::normalize_atom_person */
604
605 /**
606 * MagpieFromSimplePie::normalize_category: Normalize Atom 1.0 and
607 * RSS 2.0 categories to Dublin Core...
608 *
609 * @param array &$source
610 * @param string $from
611 * @param array &$dest
612 * @param string $to
613 * @param int $i
614 *
615 * @uses MagpieFromSimplePie::element_id
616 * @uses MagpieFromSimplePie::is_rss
617 */
618 function normalize_category (&$source, $from, &$dest, $to, $i) {
619 $cat_id = $this->element_id($from, $i);
620 $dc_id = $this->element_id($to, $i);
621
622 // first normalize category elements: Atom 1.0 <=> RSS 2.0
623 if ( isset($source["{$cat_id}@term"]) ) : // category identifier
624 $source[$cat_id] = $source["{$cat_id}@term"];
625 elseif ( $this->is_rss() ) :
626 $source["{$cat_id}@term"] = $source[$cat_id];
627 endif;
628
629 if ( isset($source["{$cat_id}@scheme"]) ) : // URI to taxonomy
630 $source["{$cat_id}@domain"] = $source["{$cat_id}@scheme"];
631 elseif ( isset($source["{$cat_id}@domain"]) ) :
632 $source["{$cat_id}@scheme"] = $source["{$cat_id}@domain"];
633 endif;
634
635 // Now put the identifier into dc:subject
636 $dest[$dc_id] = $source[$cat_id];
637 } /* MagpieFromSimplePie::normalize_category */
638
639 /**
640 * MagpieFromSimplePie::normalize_dc_subject: Normalize Dublin Core
641 * "subject" elements to Atom 1.0 and RSS 2.0 categories.
642 *
643 * @param array &$source
644 * @param string $from
645 * @param array &$dest
646 * @param string $to
647 * @param int $i
648 *
649 * @uses MagpieFromSimplePie::element_id
650 */
651 function normalize_dc_subject (&$source, $from, &$dest, $to, $i) {
652 $dc_id = $this->element_id($from, $i);
653 $cat_id = $this->element_id($to, $i);
654
655 $dest[$cat_id] = $source[$dc_id]; // RSS 2.0
656 $dest["{$cat_id}@term"] = $source[$dc_id]; // Atom 1.0
657 }
658
659 /**
660 * MagpieFromSimplePie::element_id
661 * Magic ID function for multiple elemenets.
662 * Can be called as static MagpieRSS::element_id()
663 *
664 * @param string $el
665 * @param int $counter
666 * @return string
667 */
668 function element_id ($el, $counter) {
669 return $el . (($counter > 1) ? '#'.$counter : '');
670 } /* MagpieFromSimplePie::element_id */
671
672 /**
673 * MagpieFromSimplePie::increment_element
674 *
675 * @param array &$data
676 * @param string $childTag
677 * @param string $ns
678 * @param array $path
679 */
680 function increment_element (&$data, $childTag, $ns, $path) {
681 $counterIndex = strtolower(implode('_', array_merge($path, array($childTag.'#'))));
682 if ($this->is_namespaced($ns)) :
683 if (!isset($data[$ns])) : $data[$ns] = array(); endif;
684 if (!isset($data[$ns][$counterIndex])) : $data[$ns][$counterIndex] = 0; endif;
685 $data[$ns][$counterIndex] += 1;
686 $N = $data[$ns][$counterIndex];
687 else :
688 if (!isset($data[$counterIndex])) : $data[$counterIndex] = 0; endif;
689 $data[$counterIndex] += 1;
690 $N = $data[$counterIndex];
691 endif;
692
693 if ($N > 1) :
694 $childTag .= '#'.$N;
695 endif;
696 return $childTag;
697 } /* MagpieFromSimplePie::increment_element */
698
699 /**
700 * MagpieFromSimplePie::is_namespaced
701 *
702 * @param string $ns
703 * @return bool
704 *
705 * @uses MagpieFromSimplePie::is_atom
706 * @uses MagpieFromSimplePie::is_rdf
707 */
708 function is_namespaced ($ns, $attribute = false) {
709 // Atom vs. RSS
710 if ($this->is_atom()) : $root = array('', 'atom');
711 else : $root = array('', 'rss');
712 endif;
713
714 // RDF formats; namespaced in attribs but not in elements
715 if (!$attribute and $this->is_rdf()) :
716 $root[] = 'rdf';
717 endif;
718
719 return !in_array(strtolower($ns), $root);
720 } /* MagpieFromSimplePie::is_namespaced */
721
722 /**
723 * MagpieFromSimplePie::is_atom
724 *
725 * @return bool
726 */
727 function is_atom () {
728 return $this->pie->get_type() & SIMPLEPIE_TYPE_ATOM_ALL;
729 } /* MagpieFromSimplePie::increment_element */
730
731 /**
732 * MagpieFromSimplePie::is_rss
733 *
734 * @return bool
735 */
736 function is_rss () {
737 return $this->pie->get_type() & SIMPLEPIE_TYPE_RSS_ALL;
738 } /* MagpieFromSimplePie::is_rss */
739
740 /**
741 * MagpieFromSimplePie::is_rdf
742 *
743 * @return bool
744 */
745 function is_rdf () {
746 return $this->pie->get_type() & SIMPLEPIE_TYPE_RSS_RDF;
747 } /* MagpieFromSimplePie::is_rdf */
748
749 /**
750 * MagpieFromSimplePie::feed_version
751 *
752 * @return float
753 */
754 function feed_version () {
755 $map = array (
756 SIMPLEPIE_TYPE_ATOM_10 => 1.0,
757 SIMPLEPIE_TYPE_ATOM_03 => 0.3,
758 SIMPLEPIE_TYPE_RSS_090 => 0.90,
759 SIMPLEPIE_TYPE_RSS_091 => 0.91,
760 SIMPLEPIE_TYPE_RSS_092 => 0.92,
761 SIMPLEPIE_TYPE_RSS_093 => 0.93,
762 SIMPLEPIE_TYPE_RSS_094 => 0.94,
763 SIMPLEPIE_TYPE_RSS_10 => 1.0,
764 SIMPLEPIE_TYPE_RSS_20 => 2.0,
765 );
766
767 $ret = NULL; $type = $this->pie->get_type();
768 foreach ($map as $flag => $version) :
769 if ($type & $flag) :
770 $ret = $version;
771 break;
772 endif;
773 endforeach;
774 return $ret;
775 } /* MagpieFromSimplePie::feed_version */
776
777 } /* class MagpieFromSimplePie */
778
779