Zend_Feed_Reader.xml 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 17618 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="zend.feed.reader">
  5. <title>Zend_Feed_Reader</title>
  6. <sect2 id="zend.feed.reader.introduction">
  7. <title>Einführung</title>
  8. <para>
  9. <classname>Zend_Feed_Reader</classname> ist eine Komponente die verwendet wird um
  10. <acronym>RSS</acronym> und Atom Feeds jeder Version zu konsumieren, inklusive
  11. <acronym>RDF</acronym>/<acronym>RSS</acronym> 1.0, <acronym>RSS</acronym> 2.0 und
  12. Atom 0.3/1.0. Die <acronym>API</acronym> für das Empfangen von Feed Daten ist relativ
  13. einfach da <classname>Zend_Feed_Reader</classname> in der Lage ist jeden Feed eines
  14. jeden Typs mit Hilfe der <acronym>API</acronym> nach den angefragten Informationen zu
  15. durchsuchen. Wenn die typischen Elemente die diese Informationen enthalten nicht
  16. vorhanden sind, werden diese adaptiert und statt dessen auf eine Vielzahl von
  17. alternativen Elementen zurück gegriffen. Diese Fähigkeit, von Alternativen auszuwählen,
  18. verhindert das Benutzer Ihren eigenen astrakten Layer über die Komponente legen müssen
  19. damit Sie nützlich ist, oder beliebig tiefes Wissen des zugrundeliegenden Standard,
  20. aktueller alternativen und namespaces Erweiterungen haben müssen.
  21. </para>
  22. <para>
  23. Intern arbeitet <classname>Zend_Feed_Reader</classname> fast komplett auf Basis der
  24. Erstellung von XPath Abfragen gegen das Dokument Objekt Modell des Feed
  25. <acronym>XML</acronym>'s. Das <acronym>DOM</acronym> wird nicht durch eine gekettete
  26. Eigenschaften <acronym>API</acronym> wie bei <classname>Zend_Feed</classname> bekannt
  27. gegeben und durch die darunterliegenden <classname>DOMDocument</classname>,
  28. <classname>DOMElement</classname> und <classname>DOMXPath</classname> Objekte für eine
  29. externe Manipulation bekannt gegeben. Dieser Singular Weg des Parsens ist Konsistent
  30. und die Komponente bietet ein Plugin System um dem Feed hinzuzufügen und eine Eintrags
  31. Level <acronym>API</acronym> durch das Schreiben von Erweiterungen auf einer ähnlichen
  32. Basis.
  33. </para>
  34. <para>
  35. Geschwindigkeit wird auf drei Wegen bereitgestellt. Erstens unterstützt
  36. <classname>Zend_Feed_Reader</classname> das Cachen durch Verwendung von
  37. <classname>Zend_Cache</classname> um eine Kopie des Originalen Feed
  38. <acronym>XML</acronym> zu halten. Das erlaubt es Netzwerk Anfragen für eine Feed
  39. <acronym>URI</acronym> zu überspringen wenn der Cache gültig ist. Zweitens wird die
  40. Feed und Eintrag- Level <acronym>API</acronym> durch einen internen Cache gesichert
  41. (nicht persistent) damit wiederholte <acronym>API</acronym> Aufrufe für den gleichen
  42. Feed eine zusätzliche Verwendung von <acronym>DOM</acronym>/XPath verhindert.
  43. Drittens erlaubt das Importieren von Feeds von einer <acronym>URI</acronym> den
  44. Vorteil von konditionellen <acronym>HTTP</acronym> GET Anfragen was es Servern
  45. erlaubt eine leere 304 Anfrage auszulösen wenn der angefragte Fed seit der Zeit zu der
  46. er das letzte Mal angefragt wurde, nicht verändert wurde. Im letzten Fall hält eine
  47. Instanz von <classname>Zend_Cache</classname> den zuletzt empfangenen Feed zusammen mit
  48. dem ETag und dem Last-Modified Header Werten die in der <acronym>HTTP</acronym>
  49. Antwort gesendet wurde.
  50. </para>
  51. <para>
  52. Relativ zu <classname>Zend_Feed</classname> wurde
  53. <classname>Zend_Feed_Reader</classname> als frei stehender Ersatz für
  54. <classname>Zend_Feed</classname> formuliert der aber nicht mit
  55. <classname>Zend_Feed</classname> rückwärts kompatibel ist. Aber es ist eine Alternative
  56. die einer anderen Ideologie folgt die darin fokusiert ist einfach verwendbar zu sein,
  57. flexibel, konsistent und durch das Plugin System erweiterbar.
  58. <classname>Zend_Feed_Reader</classname> ist auch nicht dazu fähig Feeds zu erstellen,
  59. das wird aber zu einem späteren Zeitpunkt hinzugefügt.
  60. </para>
  61. </sect2>
  62. <sect2 id="zend.feed.reader.import">
  63. <title>Feeds importieren</title>
  64. <para>
  65. Das importieren eines Feeds mit <classname>Zend_Feed_Reader</classname> ist zu
  66. <classname>Zend_Feed</classname> nicht sehr unterschiedlich. Feeds können von einem
  67. String, einer Datei, <acronym>URI</acronym> oder einer Instanz des Typs
  68. <classname>Zend_Feed_Abstract</classname> importiert werden. Das importieren von einer
  69. <acronym>URI</acronym> kann zusätzlich eine konditionelle <acronym>HTTP</acronym>
  70. GET Anfrage benützen. Wenn das importieren fehlschlägt, wird eine Exception geworfen.
  71. Das Endergebnis wird ein Objekt des Typs
  72. <classname>Zend_Feed_Reader_FeedInterface</classname> sein, die Core Implementation
  73. von <classname>Zend_Feed_Reader_Feed_Rss</classname> und
  74. <classname>Zend_Feed_Reader_Feed_Atom</classname> (<classname>Zend_Feed</classname>
  75. hat alle kurzen Namen genommen!). Beide Objekte unterstützen mehrere (alle
  76. existierenden) Versionen dieser breiten Feed Typen.
  77. </para>
  78. <para>
  79. Im folgenden Beispiel importieren wir einen
  80. <acronym>RDF</acronym>/<acronym>RSS</acronym> 1.0 Feed und extrahieren einige
  81. grundsätzliche Information die dann in einer Datenbank oder wo anders gespeichert
  82. werden können.
  83. </para>
  84. <programlisting language="php"><![CDATA[
  85. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  86. $data = array(
  87. 'title' => $feed->getTitle(),
  88. 'link' => $feed->getLink(),
  89. 'dateModified' => $feed->getDateModified(),
  90. 'description' => $feed->getDescription(),
  91. 'language' => $feed->getLanguage(),
  92. 'entries' => array(),
  93. );
  94. foreach ($feed as $entry) {
  95. $edata = array(
  96. 'title' => $entry->getTitle(),
  97. 'description' => $entry->getDescription(),
  98. 'dateModified' => $entry->getDateModified(),
  99. 'author' => $entry->getAuthor(),
  100. 'link' => $entry->getLink(),
  101. 'content' => $entry->getContent()
  102. );
  103. $data['entries'][] = $edata;
  104. }
  105. ]]></programlisting>
  106. <para>
  107. Das obige Beispiel demonstriert die <acronym>API</acronym> von
  108. <classname>Zend_Feed_Reader</classname> und es demonstriert auch einige seiner
  109. internen Operationen. In Wirklichkeit hat der ausgewählte <acronym>RDF</acronym> Feed
  110. keine nativen Daten oder Author Elemente, trotzdem verwendet er das Dublin Core 1.1
  111. Modul welches Namespaced Ersteller und Datums Elemente anbietet.
  112. <classname>Zend_Feed_Reader</classname> fällt auf diese und ähnliche Operationen zurück
  113. wenn keine relativ nativen Elemente existieren. Wenn es absolut keine alternative
  114. finden kann wird es <constant>NULL</constant> zurückgeben, was anzeigt das die
  115. Informationen nicht im Feed gefunden werden können. Man sollte beachten das Klassen die
  116. <classname>Zend_Feed_Reader_FeedInterface</classname> implementieren auch
  117. die <acronym>SPL</acronym> Interfaces <classname>Iterator</classname> und
  118. <classname>Countable</classname> implementieren.
  119. </para>
  120. <para>
  121. Feeds können auch von Strings, Dateien und sogar Objekten des Typs
  122. <classname>Zend_Feed_Abstract</classname> importiert werden.
  123. </para>
  124. <programlisting language="php"><![CDATA[
  125. // von einer URI
  126. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  127. // von einem String
  128. $feed = Zend_Feed_Reader::importString($feedXmlString);
  129. // von einer Datei
  130. $feed = Zend_Feed_Reader::importFile('./feed.xml');
  131. // von einem abstrakten Zend_Feed_Abstract Objekt
  132. $zfeed = Zend_Feed::import('http://www.planet-php.net/atom/');
  133. $feed = Zend_Feed_Reader::importFeed($zfeed);
  134. ]]></programlisting>
  135. </sect2>
  136. <sect2 id="zend.feed.reader.sources">
  137. <title>Empfangen darunterliegender Quellen von Feeds und Einträgen</title>
  138. <para>
  139. <classname>Zend_Feed_Reader</classname> macht sein bestes um Ihnen die Details
  140. abzunehmen. Wenn man an einem Feed ausserhalb von
  141. <classname>Zend_Feed_Reader</classname> arbeiten muß, kann man das grundsätzliche
  142. <classname>DOMDocument</classname> oder <classname>DOMElement</classname> von jeder
  143. Klasse extrahieren, oder sogar einen <acronym>XML</acronym> String der sie enthält.
  144. Es werden auch Methoden angeboten um das aktuelle <classname>DOMXPath</classname>
  145. Objekt (mit allen registrierten Kern und Erweiterungs Namespaces) zu extrahieren, und
  146. den richtigen Präfix der in allen XPath Anfragen für den aktuellen Feed oder Eintrag
  147. verwendet wird. Die grundsätzlich zu verwenden Methoden (für jedes Objekt) sind
  148. <methodname>saveXml()</methodname>, <methodname>getDomDocument()</methodname>,
  149. <methodname>getElement()</methodname>, <methodname>getXpath()</methodname> und
  150. <methodname>getXpathPrefix()</methodname>. Diese erlauben es sich von
  151. <classname>Zend_Feed_Reader</classname> zu lösen und das zu tun was man selbst
  152. machen will.
  153. </para>
  154. <itemizedlist>
  155. <listitem>
  156. <para>
  157. <methodname>saveXml()</methodname> gibt einen <acronym>XML</acronym> String
  158. zurück der nur das Element enthält welches das aktuelle Objekt repräsentiert.
  159. </para>
  160. </listitem>
  161. <listitem>
  162. <para>
  163. <methodname>getDomDocument()</methodname> gibt das
  164. <classname>DOMDocument</classname> Objekt zurück das den kompletten Feed
  165. repräsentiert (sogar wenn es von einem Entry Objekt aus aufgerufen wird).
  166. </para>
  167. </listitem>
  168. <listitem>
  169. <para>
  170. <methodname>getElement()</methodname> gibt das <classname>DOMElement</classname>
  171. des aktuellen Objekts zurück (z.B. den Feed oder aktuellen Eintrag).
  172. </para>
  173. </listitem>
  174. <listitem>
  175. <para>
  176. <methodname>getXpath()</methodname> gibt das aktuelle
  177. <classname>DOMXPath</classname> Objekt für den aktuellen Feed zurück (sogar wenn
  178. es von einem Entry Objekt aus aufgerufen wird) mit den Namespaces des aktuellen
  179. Feed Typs und allen vor-registrierten geladenen Erweiterungen.
  180. </para>
  181. </listitem>
  182. <listitem>
  183. <para>
  184. <methodname>getXpathPrefix()</methodname> gibt den Präfix der Abfrage für das
  185. aktuelle Objekt zurück (z.B. den Feed oder den aktuellen Eintrag) welcher den
  186. richtigen XPath Query Pfad für den spezifizierten Feed oder Eintrag enthält.
  187. </para>
  188. </listitem>
  189. </itemizedlist>
  190. <para>
  191. Hier ist ein Beispiel bei dem ein Feed eine <acronym>RSS</acronym> Erweiterung enthalten
  192. können die von <classname>Zend_Feed_Reader</classname> nicht out of the Box unterstützt
  193. wird. Beachtenswert ist, das man eine Erweiterungen schreiben und registrieren könnte
  194. (wird später behandelt) um das zu bewerkstelligen, aber das ist nicht immer eine
  195. Garantie für einen schnellen Check. Man muß jeden neuen Namespace beim
  196. <classname>DOMXPath</classname> Objekt registrieren bevor es verwendet wird ausser Sie
  197. werden vorab von <classname>Zend_Feed_Reader</classname> oder einer Erweiterung
  198. registriert.
  199. </para>
  200. <programlisting language="php"><![CDATA[
  201. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  202. $xpathPrefix = $feed->getXpathPrefix();
  203. $xpath = $feed->getXpath();
  204. $xpath->registerNamespace('admin', 'http://webns.net/mvcb/');
  205. $reportErrorsTo = $xpath->evaluate('string('
  206. . $xpathPrefix
  207. . '/admin:errorReportsTo)');
  208. ]]></programlisting>
  209. <warning>
  210. <para>
  211. Wenn man einen bereits registrierten Namespace mit einem anderen Präfix Namen
  212. registriert als jenen der von <classname>Zend_Feed_Reader</classname> intern
  213. verwendet wird, zerstört das die Interne Arbeitsweise dieser Komponente.
  214. </para>
  215. </warning>
  216. </sect2>
  217. <sect2 id="zend.feed.reader.cache-request">
  218. <title>Unterstützung für Caches und intelligente Anfragen</title>
  219. <sect3 id="zend.feed.reader.cache-request.cache">
  220. <title>Unterstützung für Caches in Zend_Feed_Reader hinzufügen</title>
  221. <para>
  222. <classname>Zend_Feed_Reader</classname> unterstützt die Verwendung einer Instanz von
  223. <classname>Zend_Cache</classname> um Feeds zu cachen (als <acronym>XML</acronym>)
  224. um unnötige Anfragen im Netzwerk zu vermeiden. Das Hinzufügen eines Caches ist hier
  225. so einfach wie bei anderen Zend Framework Komponenten. Den Cache erstellen und
  226. konfigurieren und dann <classname>Zend_Feed_Reader</classname> mitteilen das er
  227. verwendet werden soll! Der verwendete Cache Schlüssel ist
  228. "<classname>Zend_Feed_Reader_</classname>" gefolgt von dem
  229. <acronym>MD5</acronym> Hash der <acronym>URI</acronym> des Feeds.
  230. </para>
  231. <programlisting language="php"><![CDATA[
  232. $frontendOptions = array(
  233. 'lifetime' => 7200,
  234. 'automatic_serialization' => true
  235. );
  236. $backendOptions = array('cache_dir' => './tmp/');
  237. $cache = Zend_Cache::factory(
  238. 'Core', 'File', $frontendOptions, $backendOptions
  239. );
  240. Zend_Feed_Reader::setCache($cache);
  241. ]]></programlisting>
  242. <note>
  243. <para>
  244. Auch wenn es etwas abseits ist, sollte man daran denken zu
  245. <classname>Zend_Loader_PluginLoader</classname> einen Cache hinzuzufügen der
  246. von <classname>Zend_Feed_Reader</classname> verwendet wird um Erweiterungen zu
  247. laden.
  248. </para>
  249. </note>
  250. </sect3>
  251. <sect3 id="zend.feed.reader.cache-request.http-conditional-get">
  252. <title>Unterstützung für HTTP Conditional GET</title>
  253. <para>
  254. Die große Frage wenn man ofters einen Feed importiert, ist ob er sich geändert hat.
  255. Wenn ein Cache aktiviert ist, kann man die Unterstützung für <acronym>HTTP</acronym>
  256. Conditional GET hinzufügen um diese Frage zu beantworten.
  257. </para>
  258. <para>
  259. Using this method, you can request feeds from <acronym>URI</acronym>s and include
  260. their last known ETag and Last-Modified response header values
  261. with the request (using the If-None-Match and If-Modified-Since
  262. headers). If the feed on the server remains unchanged, you
  263. should receive a 304 response which tells
  264. <classname>Zend_Feed_Reader</classname> to use the cached
  265. version. If a full feed is sent in a response with a status code
  266. of 200, this means the feed has changed and
  267. <classname>Zend_Feed_Reader</classname> will parse the new
  268. version and save it to the cache. It will also cache the new
  269. ETag and Last-Modified header values for future use.
  270. </para>
  271. <para>
  272. These "conditional" requests are not guaranteed to be supported
  273. by the server you request a <acronym>URI</acronym> of, but can be attempted
  274. regardless. Most common feed sources like blogs should however
  275. have this supported. To enable conditional requests, you will
  276. need to provide a cache to <classname>Zend_Feed_Reader</classname>.
  277. </para>
  278. <programlisting language="php"><![CDATA[
  279. $frontendOptions = array(
  280. 'lifetime' => 86400,
  281. 'automatic_serialization' => true
  282. );
  283. $backendOptions = array('cache_dir' => './tmp/');
  284. $cache = Zend_Cache::factory(
  285. 'Core', 'File', $frontendOptions, $backendOptions
  286. );
  287. Zend_Feed_Reader::setCache($cache);
  288. Zend_Feed_Reader::useHttpConditionalGet();
  289. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  290. ]]></programlisting>
  291. <para>
  292. In the example above, with <acronym>HTTP</acronym> Conditional GET requests enabled,
  293. the response header values for ETag and Last-Modified will be cached
  294. along with the feed. For the next 24hrs (the cache lifetime), feeds will
  295. only be updated on the cache if a non-304 response is received
  296. containing a valid <acronym>RSS</acronym> or Atom <acronym>XML</acronym> document.
  297. </para>
  298. <para>
  299. If you intend on managing request headers from outside
  300. <classname>Zend_Feed_Reader</classname>, you can set the
  301. relevant If-None-Matches and If-Modified-Since request headers
  302. via the <acronym>URI</acronym> import method.
  303. </para>
  304. <programlisting language="php"><![CDATA[
  305. $lastEtagReceived = '5e6cefe7df5a7e95c8b1ba1a2ccaff3d';
  306. $lastModifiedDateReceived = 'Wed, 08 Jul 2009 13:37:22 GMT';
  307. $feed = Zend_Feed_Reader::import(
  308. $uri, $lastEtagReceived, $lastModifiedDateReceived
  309. );
  310. ]]></programlisting>
  311. </sect3>
  312. </sect2>
  313. <sect2 id="zend.feed.reader.locate">
  314. <title>Locating Feed URIs from Websites</title>
  315. <para>
  316. These days, many websites are aware that the location of their <acronym>XML</acronym>
  317. feeds is not always obvious. A small <acronym>RDF</acronym>, <acronym>RSS</acronym> or
  318. Atom graphic helps when the user is reading the page, but what about when a machine
  319. visits trying to identify where your feeds are located? To assist in
  320. this, websites may point to their feeds using &lt;link&gt; tags in
  321. the &lt;head&gt; section of their <acronym>HTML</acronym>. To take advantage of this,
  322. you can use <classname>Zend_Feed_Reader</classname> to locate these
  323. feeds using the static <methodname>findFeedLinks()</methodname>
  324. method.
  325. </para>
  326. <para>
  327. This method calls any <acronym>URI</acronym> and searches for the location of
  328. <acronym>RSS</acronym>, <acronym>RDF</acronym>
  329. and Atom feeds assuming the wlebsite's <acronym>HTML</acronym> contains the relevant
  330. links. It then returns a value object where you can check for the existence of a
  331. <acronym>RSS</acronym>, <acronym>RDF</acronym> or Atom feed <acronym>URI</acronym>.
  332. </para>
  333. <programlisting language="php"><![CDATA[
  334. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  335. if(isset($links->rdf)) {
  336. echo $links->rdf, "\n"; // http://www.planet-php.org/rdf/
  337. }
  338. if(isset($links->rss)) {
  339. echo $links->rss, "\n"; // http://www.planet-php.org/rss/
  340. }
  341. if(isset($links->atom)) {
  342. echo $links->atom, "\n"; // http://www.planet-php.org/atom/
  343. }
  344. ]]></programlisting>
  345. <para>
  346. Based on these links, you can then import from whichever source you
  347. wish in the usual manner.
  348. </para>
  349. </sect2>
  350. <sect2 id="zend.feed.reader.retrieve-info">
  351. <title>Retrieving Feed Information</title>
  352. <para>
  353. Retrieving information from a feed (we'll cover entries/items in the
  354. next section though they follow identical principals) uses a clearly
  355. defined <acronym>API</acronym> which is exactly the same regardless of whether the feed
  356. in question is <acronym>RSS</acronym>/<acronym>RDF</acronym>/Atom. The same goes for
  357. sub-versions of these standards and we've tested every single
  358. <acronym>RSS</acronym> and Atom version. While
  359. the underlying feed <acronym>XML</acronym> can differ substantially in terms of the
  360. tags and elements they present, they nonetheless are all trying to
  361. convey similar information and to reflect this all the differences
  362. and wrangling over alternative tags are handled internally by
  363. <classname>Zend_Feed_Reader</classname> presenting you with an
  364. identical interface for each. Ideally, you should not have to care
  365. whether a feed is <acronym>RSS</acronym> or Atom so long as you can extract the
  366. information you want.
  367. </para>
  368. <para>
  369. Of course, we don't live in an ideal world so there may be times the
  370. <acronym>API</acronym> just does not cover what you're looking for. To assist you,
  371. <classname>Zend_Feed_Reader</classname> offers a plugin system which
  372. allows you to write Extensions to expand the core <acronym>API</acronym> and cover any
  373. additional data you are trying to extract from feeds. If writing
  374. another Extension is too much trouble, you can simply grab the
  375. underlying <acronym>DOM</acronym> or XPath objects and do it by hand in your
  376. application. Of course, we really do encourage writing an Extension
  377. simply to make it more portable and reusable.
  378. </para>
  379. <para>
  380. Here's a summary of the Core <acronym>API</acronym> for Feeds. You should note it
  381. comprises not only the basic <acronym>RSS</acronym> and Atom standards, but also
  382. accounts for a number of included Extensions bundled with
  383. <classname>Zend_Feed_Reader</classname>. The naming of these
  384. Extension sourced methods remain fairly generic - all Extension
  385. methods operate at the same level as the Core <acronym>API</acronym> though we do allow
  386. you to retrieve any specific Extension object separately if
  387. required.
  388. </para>
  389. <table>
  390. <title>Feed Level API Methods</title>
  391. <tgroup cols="2">
  392. <tbody>
  393. <row>
  394. <entry><methodname>getId()</methodname></entry>
  395. <entry>Returns a unique ID associated with this feed</entry>
  396. </row>
  397. <row>
  398. <entry><methodname>getTitle()</methodname></entry>
  399. <entry>Returns the title of the feed</entry>
  400. </row>
  401. <row>
  402. <entry><methodname>getDescription()</methodname></entry>
  403. <entry>Returns the text description of the feed</entry>
  404. </row>
  405. <row>
  406. <entry><methodname>getLink()</methodname></entry>
  407. <entry>
  408. Returns a <acronym>URI</acronym> to the <acronym>HTML</acronym> website
  409. containing the same or
  410. similar information as this feed (i.e. if the feed is from a blog,
  411. it should provide the blog's <acronym>URI</acronym> where the
  412. <acronym>HTML</acronym> version of the entries can be read).
  413. </entry>
  414. </row>
  415. <row>
  416. <entry><methodname>getFeedLink()</methodname></entry>
  417. <entry>
  418. Returns the <acronym>URI</acronym> of this feed, which should be the
  419. same as the <acronym>URI</acronym> used to import the feed
  420. </entry>
  421. </row>
  422. <row>
  423. <entry><methodname>getAuthors()</methodname></entry>
  424. <entry>
  425. Returns an array of all authors associated with this feed
  426. including email address in the author string if available
  427. </entry>
  428. </row>
  429. <row>
  430. <entry><methodname>getAuthor(integer $index = 0)</methodname></entry>
  431. <entry>
  432. Returns either the first author known, or with the
  433. optional <varname>$index</varname> parameter any specific
  434. index on the array of Authors (returning null if an
  435. invalid index).
  436. </entry>
  437. </row>
  438. <row>
  439. <entry><methodname>getDateCreated()</methodname></entry>
  440. <entry>
  441. Returns the date on which this feed was created. Generally
  442. only applicable to Atom where it represents the date the resource
  443. described by an Atom 1.0 document was created.
  444. </entry>
  445. </row>
  446. <row>
  447. <entry><methodname>getDateModified()</methodname></entry>
  448. <entry>
  449. Returns the date on which this feed was last modified
  450. </entry>
  451. </row>
  452. <row>
  453. <entry><methodname>getLanguage()</methodname></entry>
  454. <entry>
  455. Returns the language of the feed (if defined) or simply the
  456. language noted in the <acronym>XML</acronym> document
  457. </entry>
  458. </row>
  459. <row>
  460. <entry><methodname>getGenerator()</methodname></entry>
  461. <entry>
  462. Returns the generator of the feed, e.g. the software which
  463. generated it. This may differ between <acronym>RSS</acronym> and Atom
  464. since Atom defines a different notation.
  465. </entry>
  466. </row>
  467. <row>
  468. <entry><methodname>getCopyright()</methodname></entry>
  469. <entry>
  470. Returns any copyright notice associated with the feed
  471. </entry>
  472. </row>
  473. </tbody>
  474. </tgroup>
  475. </table>
  476. <para>
  477. Given the variety of feeds in the wild, some of these methods will
  478. undoubtedly return <constant>NULL</constant> indicating the relevant information
  479. couldn't be located. Where possible, <classname>Zend_Feed_Reader</classname>
  480. will fall back on alternative elements during its search. For
  481. example, searching an <acronym>RSS</acronym> feed for a modification date is more
  482. complicated than it looks. <acronym>RSS</acronym> 2.0 feeds should include a
  483. <code>&lt;lastBuildDate&gt;</code> tag and/or a
  484. <code>&lt;pubDate&gt;</code> element. But what if it doesn't, maybe
  485. this is an <acronym>RSS</acronym> 1.0 feed? Perhaps it instead has an
  486. <code>&lt;atom:updated&gt;</code> element with identical information
  487. (Atom may be used to supplement <acronym>RSS</acronym>'s syntax)? Failing that, we
  488. could simply look at the entries, pick the most recent, and use its
  489. <code>&lt;pubDate&gt;</code> element. Assuming it exists... Many
  490. feeds also use Dublin Core 1.0/1.1 <code>&lt;dc:date&gt;</code>
  491. elements for feeds/entries. Or we could find Atom lurking again.
  492. </para>
  493. <para>
  494. The point is, <classname>Zend_Feed_Reader</classname> was designed
  495. to know this. When you ask for the modification date (or anything
  496. else), it will run off and search for all these alternatives until
  497. it either gives up and returns <constant>NULL</constant>, or finds an
  498. alternative that should have the right answer.
  499. </para>
  500. <para>
  501. In addition to the above methods, all Feed objects implement methods
  502. for retrieving the <acronym>DOM</acronym> and XPath objects for the current feeds as
  503. described earlier. Feed objects also implement the <acronym>SPL</acronym> Iterator and
  504. Countable interfaces. The extended <acronym>API</acronym> is summarised below.
  505. </para>
  506. <table>
  507. <title>Extended Feed Level API Methods</title>
  508. <tgroup cols="2">
  509. <tbody>
  510. <row>
  511. <entry><methodname>getDomDocument()</methodname></entry>
  512. <entry>
  513. Returns the parent
  514. <classname>DOMDocument</classname> object for the
  515. entire source <acronym>XML</acronym> document
  516. </entry>
  517. </row>
  518. <row>
  519. <entry><methodname>getElement()</methodname></entry>
  520. <entry>
  521. Returns the current feed level
  522. <classname>DOMElement</classname> object
  523. </entry>
  524. </row>
  525. <row>
  526. <entry><methodname>saveXml()</methodname></entry>
  527. <entry>
  528. Returns a string containing an <acronym>XML</acronym> document of the
  529. entire feed element (this is not the original
  530. document but a rebuilt version)
  531. </entry>
  532. </row>
  533. <row>
  534. <entry><methodname>getXpath()</methodname></entry>
  535. <entry>
  536. Returns the <classname>DOMXPath</classname> object
  537. used internally to run queries on the
  538. <classname>DOMDocument</classname> object (this
  539. includes core and Extension namespaces
  540. pre-registered)
  541. </entry>
  542. </row>
  543. <row>
  544. <entry><methodname>getXpathPrefix()</methodname></entry>
  545. <entry>
  546. Returns the valid <acronym>DOM</acronym> path prefix prepended
  547. to all XPath queries matching the feed being queried
  548. </entry>
  549. </row>
  550. <row>
  551. <entry><methodname>getEncoding()</methodname></entry>
  552. <entry>
  553. Returns the encoding of the source <acronym>XML</acronym> document
  554. (note: this cannot account for errors such as the
  555. server sending documents in a different encoding)
  556. </entry>
  557. </row>
  558. <row>
  559. <entry><methodname>count()</methodname></entry>
  560. <entry>
  561. Returns a count of the entries or items this feed contains
  562. (implements <acronym>SPL</acronym> <classname>Countable</classname>
  563. interface)
  564. </entry>
  565. </row>
  566. <row>
  567. <entry><methodname>current()</methodname></entry>
  568. <entry>
  569. Returns either the current entry (using the current index
  570. from <methodname>key()</methodname>)
  571. </entry>
  572. </row>
  573. <row>
  574. <entry><methodname>key()</methodname></entry>
  575. <entry>Returns the current entry index</entry>
  576. </row>
  577. <row>
  578. <entry><methodname>next()</methodname></entry>
  579. <entry>Increments the entry index value by one</entry>
  580. </row>
  581. <row>
  582. <entry><methodname>rewind()</methodname></entry>
  583. <entry>Resets the entry index to 0</entry>
  584. </row>
  585. <row>
  586. <entry><methodname>valid()</methodname></entry>
  587. <entry>
  588. Checks that the current entry index is valid, i.e.
  589. it does fall below 0 and does not exceed the number
  590. of entries existing.
  591. </entry>
  592. </row>
  593. <row>
  594. <entry><methodname>getExtensions()</methodname></entry>
  595. <entry>
  596. Returns an array of all Extension objects loaded for
  597. the current feed (note: both feed-level and entry-level Extensions
  598. exist, and only feed-level Extensions are returned here).
  599. The array keys are of the form {ExtensionName}_Feed.
  600. </entry>
  601. </row>
  602. <row>
  603. <entry><methodname>getExtension(string $name)</methodname></entry>
  604. <entry>
  605. Returns an Extension object for the feed registered under the
  606. provided name. This allows more fine-grained access to
  607. Extensions which may otherwise be hidden within the implementation
  608. of the standard <acronym>API</acronym> methods.
  609. </entry>
  610. </row>
  611. <row>
  612. <entry><methodname>getType()</methodname></entry>
  613. <entry>
  614. Returns a static class constant (e.g.
  615. <constant>Zend_Feed_Reader::TYPE_ATOM_03</constant>,
  616. i.e. Atom 0.3) indicating exactly what kind of feed
  617. is being consumed.
  618. </entry>
  619. </row>
  620. </tbody>
  621. </tgroup>
  622. </table>
  623. </sect2>
  624. <sect2 id="zend.feed.reader.entry">
  625. <title>Retrieving Entry/Item Information</title>
  626. <para>
  627. Retrieving information for specific entries or items (depending on
  628. whether you speak Atom or <acronym>RSS</acronym>) is identical to feed level data.
  629. Accessing entries is simply a matter of iterating over a Feed object
  630. or using the <acronym>SPL</acronym> <classname>Iterator</classname> interface Feed
  631. objects implement and calling the appropriate method on each.
  632. </para>
  633. <table>
  634. <title>Entry Level API Methods</title>
  635. <tgroup cols="2">
  636. <tbody>
  637. <row>
  638. <entry><methodname>getId()</methodname></entry>
  639. <entry>Returns a unique ID for the current entry</entry>
  640. </row>
  641. <row>
  642. <entry><methodname>getTitle()</methodname></entry>
  643. <entry>Returns the title of the current entry</entry>
  644. </row>
  645. <row>
  646. <entry><methodname>getDescription()</methodname></entry>
  647. <entry>Returns a description of the current entry</entry>
  648. </row>
  649. <row>
  650. <entry><methodname>getLink()</methodname></entry>
  651. <entry>
  652. Returns a <acronym>URI</acronym> to the <acronym>HTML</acronym> version
  653. of the current entry
  654. </entry>
  655. </row>
  656. <row>
  657. <entry><methodname>getPermaLink()</methodname></entry>
  658. <entry>
  659. Returns the permanent link to the current entry
  660. </entry>
  661. </row>
  662. <row>
  663. <entry><methodname>getAuthors()</methodname></entry>
  664. <entry>
  665. Returns an array of all authors associated with this entry
  666. including email address in the author string if available
  667. </entry>
  668. </row>
  669. <row>
  670. <entry><methodname>getAuthor($index = 0)</methodname></entry>
  671. <entry>
  672. Returns either the first author known, or with the
  673. optional <varname>$index</varname> parameter any specific
  674. index on the array of Authors (returning null if an
  675. invalid index).
  676. </entry>
  677. </row>
  678. <row>
  679. <entry><methodname>getDateCreated()</methodname></entry>
  680. <entry>
  681. Returns the date on which the current entry was
  682. created. Generally only applicable to Atom where it
  683. represents the date the resource described by an
  684. Atom 1.0 document was created.
  685. </entry>
  686. </row>
  687. <row>
  688. <entry><methodname>getDateModified()</methodname></entry>
  689. <entry>
  690. Returns the date on which the current entry was last
  691. modified
  692. </entry>
  693. </row>
  694. <row>
  695. <entry><methodname>getContent()</methodname></entry>
  696. <entry>
  697. Returns the content of the current entry (this has any
  698. entities reversed if possible assuming the content type is
  699. <acronym>HTML</acronym>). The description is returned if a
  700. separate content element does not exist.
  701. </entry>
  702. </row>
  703. <row>
  704. <entry><methodname>getCommentCount()</methodname></entry>
  705. <entry>
  706. Returns the number of comments made on this entry at the
  707. time the feed was last generated
  708. </entry>
  709. </row>
  710. <row>
  711. <entry><methodname>getCommentLink()</methodname></entry>
  712. <entry>
  713. Returns a <acronym>URI</acronym> pointing to the <acronym>HTML</acronym>
  714. page where comments can be made on this entry
  715. </entry>
  716. </row>
  717. <row>
  718. <entry>
  719. <methodname>getCommentFeedLink(string $type =
  720. 'atom'|'rss')</methodname>
  721. </entry>
  722. <entry>
  723. Returns a <acronym>URI</acronym> pointing to a feed of the provided type
  724. containing all comments for this entry (type defaults to
  725. Atom/<acronym>RSS</acronym> depending on current feed type).
  726. </entry>
  727. </row>
  728. </tbody>
  729. </tgroup>
  730. </table>
  731. <para>
  732. The extended <acronym>API</acronym> for entries is identical to that for feeds with the
  733. exception of the Iterator methods which are not needed here.
  734. </para>
  735. <caution>
  736. <para>
  737. There is often confusion over the concepts of modified and
  738. created dates. In Atom, these are two clearly defined concepts
  739. (so knock yourself out) but in <acronym>RSS</acronym> they are vague.
  740. <acronym>RSS</acronym> 2.0
  741. defines a single <emphasis>&lt;pubDate&gt;</emphasis> element
  742. which typically refers to the date this entry was published,
  743. i.e. a creation date of sorts. This is not always the case, and
  744. it may change with updates or not. As a result, if you really
  745. want to check whether an entry has changed, don't rely on the
  746. results of <methodname>getDateModified()</methodname>. Instead,
  747. consider tracking the <acronym>MD5</acronym> hash of three other elements
  748. concatenated, e.g. using <methodname>getTitle()</methodname>,
  749. <methodname>getDescription()</methodname> and
  750. <methodname>getContent()</methodname>. If the entry was trully
  751. updated, this hash computation will give a different result than
  752. previously saved hashes for the same entry. Further muddying the
  753. waters, dates in feeds may follow different standards. Atom and
  754. Dublin Core dates should follow <acronym>ISO</acronym> 8601,
  755. and <acronym>RSS</acronym> dates should
  756. follow <acronym>RFC</acronym> 822 or <acronym>RFC</acronym> 2822
  757. which is also common. Date methods
  758. will throw an exception if <classname>Zend_Date</classname>
  759. cannot load the date string using one of the above standards.
  760. </para>
  761. </caution>
  762. <warning>
  763. <para>
  764. The values returned from these methods are not validated. This
  765. means users must perform validation on all retrieved data
  766. including the filtering of any <acronym>HTML</acronym> such as from
  767. <methodname>getContent()</methodname> before it is output from
  768. your application. Remember that most feeds come from external
  769. sources, and therefore the default assumption should be that
  770. they cannot be trusted.
  771. </para>
  772. </warning>
  773. <table>
  774. <title>Extended Entry Level API Methods</title>
  775. <tgroup cols="2">
  776. <tbody>
  777. <row>
  778. <entry><methodname>getDomDocument()</methodname></entry>
  779. <entry>
  780. Returns the parent
  781. <classname>DOMDocument</classname> object for the
  782. entire feed (not just the current entry)
  783. </entry>
  784. </row>
  785. <row>
  786. <entry><methodname>getElement()</methodname></entry>
  787. <entry>
  788. Returns the current entry level
  789. <classname>DOMElement</classname> object
  790. </entry>
  791. </row>
  792. <row>
  793. <entry><methodname>getXpath()</methodname></entry>
  794. <entry>
  795. Returns the <classname>DOMXPath</classname> object
  796. used internally to run queries on the
  797. <classname>DOMDocument</classname> object (this
  798. includes core and Extension namespaces
  799. pre-registered)
  800. </entry>
  801. </row>
  802. <row>
  803. <entry><methodname>getXpathPrefix()</methodname></entry>
  804. <entry>
  805. Returns the valid <acronym>DOM</acronym> path prefix prepended
  806. to all XPath queries matching the entry being queried
  807. </entry>
  808. </row>
  809. <row>
  810. <entry><methodname>getEncoding()</methodname></entry>
  811. <entry>
  812. Returns the encoding of the source <acronym>XML</acronym> document
  813. (note: this cannot account for errors such as the server sending
  814. documents in a different encoding)
  815. </entry>
  816. </row>
  817. <row>
  818. <entry><methodname>getExtensions()</methodname></entry>
  819. <entry>
  820. Returns an array of all Extension objects loaded for
  821. the current entry (note: both feed-level and entry-level
  822. Extensions exist, and only entry-level Extensions are returned
  823. here). The array keys are in the form {ExtensionName}_Entry.
  824. </entry>
  825. </row>
  826. <row>
  827. <entry><methodname>getExtension(string $name)</methodname></entry>
  828. <entry>
  829. Returns an Extension object for the entry registered under the
  830. provided name. This allows more fine-grained access to
  831. Extensions which may otherwise be hidden within the implementation
  832. of the standard <acronym>API</acronym> methods.
  833. </entry>
  834. </row>
  835. <row>
  836. <entry><methodname>getType()</methodname></entry>
  837. <entry>
  838. Returns a static class constant (e.g.
  839. <constant>Zend_Feed_Reader::TYPE_ATOM_03</constant>,
  840. i.e. Atom 0.3) indicating exactly what kind
  841. of feed is being consumed.
  842. </entry>
  843. </row>
  844. </tbody>
  845. </tgroup>
  846. </table>
  847. </sect2>
  848. <sect2 id="zend.feed.reader.extending">
  849. <title>Extending Feed and Entry APIs</title>
  850. <para>
  851. Extending <classname>Zend_Feed_Reader</classname> allows you to add
  852. methods at both the feed and entry level which cover the retrieval
  853. of information not already supported by
  854. <classname>Zend_Feed_Reader</classname>. Given the number of
  855. <acronym>RSS</acronym> and
  856. Atom extensions that exist, this is a good thing since
  857. <classname>Zend_Feed_Reader</classname> couldn't possibly add
  858. everything.
  859. </para>
  860. <para>
  861. There are two types of Extensions possible, those which retrieve
  862. information from elements which are immediate children of the root
  863. element (e.g. <code>&lt;channel&gt;</code> for <acronym>RSS</acronym> or
  864. <code>&lt;feed&gt;</code> for Atom) and those who retrieve
  865. information from child elements of an entry (e.g.
  866. <code>&lt;item&gt;</code> for <acronym>RSS</acronym> or <code>&lt;entry&gt;</code> for
  867. Atom). On the filesystem these are grouped as classes within
  868. a namespace based on the extension standard's name. For
  869. example, internally we have
  870. <classname>Zend_Feed_Reader_Extension_DublinCore_Feed</classname>
  871. and <classname>Zend_Feed_Reader_Extension_DublinCore_Entry</classname>
  872. classes which are two Extensions implementing Dublin Core
  873. 1.0/1.1 support.
  874. </para>
  875. <para>
  876. Extensions are loaded into <classname>Zend_Feed_Reader</classname>
  877. using <classname>Zend_Loader_PluginLoader</classname>, so their operation
  878. will be familiar from other Zend Framework components.
  879. <classname>Zend_Feed_Reader</classname> already bundles a number of
  880. these Extensions, however those which are not used internally and
  881. registered by default (so called Core Extensions) must be registered
  882. to <classname>Zend_Feed_Reader</classname> before they are used. The
  883. bundled Extensions include:
  884. </para>
  885. <table>
  886. <title>Core Extensions (pre-registered)</title>
  887. <tgroup cols="2">
  888. <tbody>
  889. <row>
  890. <entry>DublinCore (Feed and Entry)</entry>
  891. <entry>Implements support for Dublin Core Metadata Element Set 1.0
  892. and 1.1 </entry>
  893. </row>
  894. <row>
  895. <entry>Content (Entry only)</entry>
  896. <entry>Implements support for Content 1.0</entry>
  897. </row>
  898. <row>
  899. <entry>Atom (Feed and Entry)</entry>
  900. <entry>Implements support for Atom 0.3 and Atom 1.0</entry>
  901. </row>
  902. <row>
  903. <entry>Slash</entry>
  904. <entry>Implements support for the Slash <acronym>RSS</acronym> 1.0 module</entry>
  905. </row>
  906. <row>
  907. <entry>WellFormedWeb</entry>
  908. <entry>Implements support for the Well Formed Web CommentAPI 1.0</entry>
  909. </row>
  910. <row>
  911. <entry>Thread</entry>
  912. <entry>Implements support for Atom Threading Extensions as described
  913. in <acronym>RFC</acronym> 4685</entry>
  914. </row>
  915. <row>
  916. <entry>Podcast</entry>
  917. <entry>Implements support for the Podcast 1.0 <acronym>DTD</acronym> from Apple</entry>
  918. </row>
  919. </tbody>
  920. </tgroup>
  921. </table>
  922. <para>
  923. The Core Extensions are somewhat special since they are extremely
  924. common and multi-faceted. For example, we have a Core Extension for Atom.
  925. Atom is implemented as an Extension (not just a base class) because it
  926. doubles as a valid <acronym>RSS</acronym> module - you can insert
  927. Atom elements into <acronym>RSS</acronym> feeds. I've even seen
  928. <acronym>RDF</acronym> feeds which use a lot of Atom in place of more
  929. common Extensions like Dublin Core.
  930. </para>
  931. <table>
  932. <title>Non-Core Extensions (must register manually)</title>
  933. <tgroup cols="2">
  934. <tbody>
  935. <row>
  936. <entry>Syndication</entry>
  937. <entry>Implements Syndication 1.0 support for <acronym>RSS</acronym> feeds</entry>
  938. </row>
  939. <row>
  940. <entry>CreativeCommons</entry>
  941. <entry>A <acronym>RSS</acronym> module that adds an element at the &lt;channel&gt;
  942. or &lt;item&gt; level that specifies which Creative Commons license
  943. applies.</entry>
  944. </row>
  945. </tbody>
  946. </tgroup>
  947. </table>
  948. <para>
  949. The additional non-Core Extensions are offered but not registered to
  950. <classname>Zend_Feed_Reader</classname> by default. If you want to
  951. use them, you'll need to tell
  952. <classname>Zend_Feed_Reader</classname> to load them in advance of
  953. importing a feed. Additional non-Core Extensions will be included
  954. in future iterations of the component.
  955. </para>
  956. <para>
  957. Registering an Extension with
  958. <classname>Zend_Feed_Reader</classname>, so it is loaded and its <acronym>API</acronym>
  959. is available to Feed and Entry objects, is a simple affair using the
  960. <classname>Zend_Loader_PluginLoader</classname>. Here we register
  961. the optional Slash Extension, and discover that it can be directly
  962. called from the Entry level <acronym>API</acronym> without any effort. Note that
  963. Extension names are case sensitive and use camel casing for multiple
  964. terms.
  965. </para>
  966. <programlisting language="php"><![CDATA[
  967. Zend_Feed_Reader::registerExtension('Syndication');
  968. $feed = Zend_Feed_Reader::import('http://rss.slashdot.org/Slashdot/slashdot');
  969. $updatePeriod = $feed->current()->getUpdatePeriod();
  970. ]]></programlisting>
  971. <para>
  972. In the simple example above, we checked how frequently a feed is being updated
  973. using the <methodname>getUpdatePeriod()</methodname>
  974. method. Since it's not part of
  975. <classname>Zend_Feed_Reader</classname>'s core <acronym>API</acronym>, it could only be
  976. a method supported by the newly registered Syndication Extension.
  977. </para>
  978. <para>
  979. As you can also notice, the new methods from Extensions are accessible from the main
  980. <acronym>API</acronym> using <acronym>PHP</acronym>'s magic methods. As an alternative,
  981. you can also directly access any Extension object for a similar result as seen below.
  982. </para>
  983. <programlisting language="php"><![CDATA[
  984. Zend_Feed_Reader::registerExtension('Syndication');
  985. $feed = Zend_Feed_Reader::import('http://rss.slashdot.org/Slashdot/slashdot');
  986. $syndication = $feed->getExtension('Syndication');
  987. $updatePeriod = $syndication->getUpdatePeriod();
  988. ]]></programlisting>
  989. <sect3 id="zend.feed.reader.extending.feed">
  990. <title>Writing Zend_Feed_Reader Extensions</title>
  991. <para>
  992. Inevitably, there will be times when the
  993. <classname>Zend_Feed_Reader</classname> <acronym>API</acronym> is just not capable
  994. of getting something you need from a feed or entry. You can use
  995. the underlying source objects, like
  996. <classname>DOMDocument</classname>, to get these by hand however
  997. there is a more reusable method available by writing Extensions
  998. supporting these new queries.
  999. </para>
  1000. <para>
  1001. As an example, let's take the case of a purely fictitious
  1002. corporation named Jungle Books. Jungle Books have been
  1003. publishing a lot of reviews on books they sell (from external
  1004. sources and customers), which are distributed as an <acronym>RSS</acronym> 2.0
  1005. feed. Their marketing department realises that web applications
  1006. using this feed cannot currently figure out exactly what book is
  1007. being reviewed. To make life easier for everyone, they determine
  1008. that the geek department needs to extend <acronym>RSS</acronym> 2.0 to include a
  1009. new element per entry supplying the <acronym>ISBN</acronym>-10 or
  1010. <acronym>ISBN</acronym>-13 number of
  1011. the publication the entry concerns. They define the new
  1012. <code>&lt;isbn&gt;</code> element quite simply with a standard
  1013. name and namespace <acronym>URI</acronym>:
  1014. </para>
  1015. <programlisting language="php"><![CDATA[
  1016. JungleBooks 1.0:
  1017. http://example.com/junglebooks/rss/module/1.0/
  1018. ]]></programlisting>
  1019. <para>
  1020. A snippet of <acronym>RSS</acronym> containing this extension in practice could be
  1021. something similar to:
  1022. </para>
  1023. <programlisting language="php"><![CDATA[
  1024. <?xml version="1.0" encoding="utf-8" ?>
  1025. <rss version="2.0"
  1026. xmlns:content="http://purl.org/rss/1.0/modules/content/"
  1027. xmlns:jungle="http://example.com/junglebooks/rss/module/1.0/">
  1028. <channel>
  1029. <title>Jungle Books Customer Reviews</title>
  1030. <link>http://example.com/junglebooks</link>
  1031. <description>Many book reviews!</description>
  1032. <pubDate>Fri, 26 Jun 2009 19:15:10 GMT</pubDate>
  1033. <jungle:dayPopular>http://example.com/junglebooks/book/938</jungle:dayPopular>
  1034. <item>
  1035. <title>Review Of Flatland: A Romance of Many Dimensions</title>
  1036. <link>http://example.com/junglebooks/review/987</link>
  1037. <author>Confused Physics Student</author>
  1038. <content:encoded>
  1039. A romantic square?!
  1040. </content:encoded>
  1041. <pubDate>Thu, 25 Jun 2009 20:03:28 -0700</pubDate>
  1042. <jungle:isbn>048627263X</jungle:isbn>
  1043. </item>
  1044. </channel>
  1045. </rss>
  1046. ]]></programlisting>
  1047. <para>
  1048. Implementing this new <acronym>ISBN</acronym> element as a simple entry level
  1049. extension would require the following class (using your own class
  1050. namespace outside of Zend).
  1051. </para>
  1052. <programlisting language="php"><![CDATA[
  1053. class My_FeedReader_Extension_JungleBooks_Entry
  1054. extends Zend_Feed_Reader_Extension_EntryAbstract
  1055. {
  1056. public function getIsbn()
  1057. {
  1058. if (isset($this->_data['isbn'])) {
  1059. return $this->_data['isbn'];
  1060. }
  1061. $isbn = $this->_xpath->evaluate(
  1062. 'string(' . $this->getXpathPrefix() . '/jungle:isbn)'
  1063. );
  1064. if (!$isbn) {
  1065. $isbn = null;
  1066. }
  1067. $this->_data['isbn'] = $isbn;
  1068. return $this->_data['isbn'];
  1069. }
  1070. protected function _registerNamespaces()
  1071. {
  1072. $this->_xpath->registerNamespace(
  1073. 'jungle', 'http://example.com/junglebooks/rss/module/1.0/'
  1074. );
  1075. }
  1076. }
  1077. ]]></programlisting>
  1078. <para>
  1079. This extension is easy enough to follow. It creates a new method
  1080. <methodname>getIsbn()</methodname> which runs an XPath query on
  1081. the current entry to extract the <acronym>ISBN</acronym> number enclosed by the
  1082. <code>&lt;jungle:isbn&gt;</code> element. It can optionally
  1083. store this to the internal non-persistent cache (no need to keep
  1084. querying the <acronym>DOM</acronym> if it's called again on the same entry). The
  1085. value is returned to the caller. At the end we have a protected
  1086. method (it's abstract so it must exist) which registers the
  1087. Jungle Books namespace for their custom <acronym>RSS</acronym> module. While we
  1088. call this an <acronym>RSS</acronym> module, there's nothing to prevent the same
  1089. element being used in Atom feeds - and all Extensions which use
  1090. the prefix provided by <methodname>getXpathPrefix()</methodname>
  1091. are actually neutral and work on <acronym>RSS</acronym> or Atom feeds with no
  1092. extra code.
  1093. </para>
  1094. <para>
  1095. Since this Extension is stored outside of Zend Framework, you'll
  1096. need to register the path prefix for your Extensions so
  1097. <classname>Zend_Loader_PluginLoader</classname> can find them.
  1098. After that, it's merely a matter of registering the Extension,
  1099. if it's not already loaded, and using it in practice.
  1100. </para>
  1101. <programlisting language="php"><![CDATA[
  1102. if(!Zend_Feed_Reader::isRegistered('JungleBooks')) {
  1103. Zend_Feed_Reader::addPrefixPath(
  1104. '/path/to/My/FeedReader/Extension', 'My_FeedReader_Extension'
  1105. );
  1106. Zend_Feed_Reader::registerExtension('JungleBooks');
  1107. }
  1108. $feed = Zend_Feed_Reader::import('http://example.com/junglebooks/rss');
  1109. // ISBN for whatever book the first entry in the feed was concerned with
  1110. $firstIsbn = $feed->current()->getIsbn();
  1111. ]]></programlisting>
  1112. <para>
  1113. Writing a feed level Extension is not much different. The
  1114. example feed from earlier included an unmentioned
  1115. <code>&lt;jungle:dayPopular&gt;</code> element which Jungle
  1116. Books have added to their standard to include a link to the
  1117. day's most popular book (in terms of visitor traffic). Here's
  1118. an Extension which adds a
  1119. <methodname>getDaysPopularBookLink()</methodname> method to the
  1120. feel level <acronym>API</acronym>.
  1121. </para>
  1122. <programlisting language="php"><![CDATA[
  1123. class My_FeedReader_Extension_JungleBooks_Feed
  1124. extends Zend_Feed_Reader_Extension_FeedAbstract
  1125. {
  1126. public function getDaysPopularBookLink()
  1127. {
  1128. if (isset($this->_data['dayPopular'])) {
  1129. return $this->_data['dayPopular'];
  1130. }
  1131. $dayPopular = $this->_xpath->evaluate(
  1132. 'string(' . $this->getXpathPrefix() . '/jungle:dayPopular)'
  1133. );
  1134. if (!$dayPopular) {
  1135. $dayPopular = null;
  1136. }
  1137. $this->_data['dayPopular'] = $dayPopular;
  1138. return $this->_data['dayPopular'];
  1139. }
  1140. protected function _registerNamespaces()
  1141. {
  1142. $this->_xpath->registerNamespace(
  1143. 'jungle', 'http://example.com/junglebooks/rss/module/1.0/'
  1144. );
  1145. }
  1146. }
  1147. ]]></programlisting>
  1148. <para>
  1149. Let's repeat the last example using a custom Extension to show the
  1150. method being used.
  1151. </para>
  1152. <programlisting language="php"><![CDATA[
  1153. if(!Zend_Feed_Reader::isRegistered('JungleBooks')) {
  1154. Zend_Feed_Reader::addPrefixPath(
  1155. '/path/to/My/FeedReader/Extension', 'My_FeedReader_Extension'
  1156. );
  1157. Zend_Feed_Reader::registerExtension('JungleBooks');
  1158. }
  1159. $feed = Zend_Feed_Reader::import('http://example.com/junglebooks/rss');
  1160. // URI to the information page of the day's most popular book with visitors
  1161. $daysPopularBookLink = $feed->getDaysPopularBookLink();
  1162. // ISBN for whatever book the first entry in the feed was concerned with
  1163. $firstIsbn = $feed->current()->getIsbn();
  1164. ]]></programlisting>
  1165. <para>
  1166. Going through these examples, you'll note that we don't register
  1167. feed and entry Extensions separately. Extensions within the same
  1168. standard may or may not include both a feed and entry class, so
  1169. <classname>Zend_Feed_Reader</classname> only requires you to
  1170. register the overall parent name, e.g. JungleBooks, DublinCore,
  1171. Slash. Internally, it can check at what level Extensions exist
  1172. and load them up if found. In our case, we have a full set of
  1173. Extensions now: <classname>JungleBooks_Feed</classname> and
  1174. <classname>JungleBooks_Entry</classname>.
  1175. </para>
  1176. </sect3>
  1177. </sect2>
  1178. </sect1>