Zend_Feed_Reader.xml 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 17230 -->
  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. While it's a little off track, you should also consider
  245. adding a cache to
  246. <classname>Zend_Loader_PluginLoader</classname> which is
  247. used by <classname>Zend_Feed_Reader</classname> to load
  248. Extensions.
  249. </para>
  250. </note>
  251. </sect3>
  252. <sect3 id="zend.feed.reader.cache-request.http-conditional-get">
  253. <title>HTTP Conditional GET Support</title>
  254. <para>
  255. The big question often asked when importing a feed frequently, is
  256. if it has even changed. With a cache enabled, you can add <acronym>HTTP</acronym>
  257. Conditional GET support to your arsenal to answer that question.
  258. </para>
  259. <para>
  260. Using this method, you can request feeds from <acronym>URI</acronym>s and include
  261. their last known ETag and Last-Modified response header values
  262. with the request (using the If-None-Match and If-Modified-Since
  263. headers). If the feed on the server remains unchanged, you
  264. should receive a 304 response which tells
  265. <classname>Zend_Feed_Reader</classname> to use the cached
  266. version. If a full feed is sent in a response with a status code
  267. of 200, this means the feed has changed and
  268. <classname>Zend_Feed_Reader</classname> will parse the new
  269. version and save it to the cache. It will also cache the new
  270. ETag and Last-Modified header values for future use.
  271. </para>
  272. <para>
  273. These "conditional" requests are not guaranteed to be supported
  274. by the server you request a <acronym>URI</acronym> of, but can be attempted
  275. regardless. Most common feed sources like blogs should however
  276. have this supported. To enable conditional requests, you will
  277. need to provide a cache to <classname>Zend_Feed_Reader</classname>.
  278. </para>
  279. <programlisting language="php"><![CDATA[
  280. $frontendOptions = array(
  281. 'lifetime' => 86400,
  282. 'automatic_serialization' => true
  283. );
  284. $backendOptions = array('cache_dir' => './tmp/');
  285. $cache = Zend_Cache::factory(
  286. 'Core', 'File', $frontendOptions, $backendOptions
  287. );
  288. Zend_Feed_Reader::setCache($cache);
  289. Zend_Feed_Reader::useHttpConditionalGet();
  290. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  291. ]]></programlisting>
  292. <para>
  293. In the example above, with <acronym>HTTP</acronym> Conditional GET requests enabled,
  294. the response header values for ETag and Last-Modified will be cached
  295. along with the feed. For the next 24hrs (the cache lifetime), feeds will
  296. only be updated on the cache if a non-304 response is received
  297. containing a valid <acronym>RSS</acronym> or Atom <acronym>XML</acronym> document.
  298. </para>
  299. <para>
  300. If you intend on managing request headers from outside
  301. <classname>Zend_Feed_Reader</classname>, you can set the
  302. relevant If-None-Matches and If-Modified-Since request headers
  303. via the <acronym>URI</acronym> import method.
  304. </para>
  305. <programlisting language="php"><![CDATA[
  306. $lastEtagReceived = '5e6cefe7df5a7e95c8b1ba1a2ccaff3d';
  307. $lastModifiedDateReceived = 'Wed, 08 Jul 2009 13:37:22 GMT';
  308. $feed = Zend_Feed_Reader::import(
  309. $uri, $lastEtagReceived, $lastModifiedDateReceived
  310. );
  311. ]]></programlisting>
  312. </sect3>
  313. </sect2>
  314. <sect2 id="zend.feed.reader.locate">
  315. <title>Locating Feed URIs from Websites</title>
  316. <para>
  317. These days, many websites are aware that the location of their <acronym>XML</acronym>
  318. feeds is not always obvious. A small <acronym>RDF</acronym>, <acronym>RSS</acronym> or
  319. Atom graphic helps when the user is reading the page, but what about when a machine
  320. visits trying to identify where your feeds are located? To assist in
  321. this, websites may point to their feeds using &lt;link&gt; tags in
  322. the &lt;head&gt; section of their <acronym>HTML</acronym>. To take advantage of this,
  323. you can use <classname>Zend_Feed_Reader</classname> to locate these
  324. feeds using the static <methodname>findFeedLinks()</methodname>
  325. method.
  326. </para>
  327. <para>
  328. This method calls any <acronym>URI</acronym> and searches for the location of
  329. <acronym>RSS</acronym>, <acronym>RDF</acronym>
  330. and Atom feeds assuming the wlebsite's <acronym>HTML</acronym> contains the relevant
  331. links. It then returns a value object where you can check for the existence of a
  332. <acronym>RSS</acronym>, <acronym>RDF</acronym> or Atom feed <acronym>URI</acronym>.
  333. </para>
  334. <programlisting language="php"><![CDATA[
  335. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  336. if(isset($links->rdf)) {
  337. echo $links->rdf, "\n"; // http://www.planet-php.org/rdf/
  338. }
  339. if(isset($links->rss)) {
  340. echo $links->rss, "\n"; // http://www.planet-php.org/rss/
  341. }
  342. if(isset($links->atom)) {
  343. echo $links->atom, "\n"; // http://www.planet-php.org/atom/
  344. }
  345. ]]></programlisting>
  346. <para>
  347. Based on these links, you can then import from whichever source you
  348. wish in the usual manner.
  349. </para>
  350. </sect2>
  351. <sect2 id="zend.feed.reader.retrieve-info">
  352. <title>Retrieving Feed Information</title>
  353. <para>
  354. Retrieving information from a feed (we'll cover entries/items in the
  355. next section though they follow identical principals) uses a clearly
  356. defined <acronym>API</acronym> which is exactly the same regardless of whether the feed
  357. in question is <acronym>RSS</acronym>/<acronym>RDF</acronym>/Atom. The same goes for
  358. sub-versions of these standards and we've tested every single
  359. <acronym>RSS</acronym> and Atom version. While
  360. the underlying feed <acronym>XML</acronym> can differ substantially in terms of the
  361. tags and elements they present, they nonetheless are all trying to
  362. convey similar information and to reflect this all the differences
  363. and wrangling over alternative tags are handled internally by
  364. <classname>Zend_Feed_Reader</classname> presenting you with an
  365. identical interface for each. Ideally, you should not have to care
  366. whether a feed is <acronym>RSS</acronym> or Atom so long as you can extract the
  367. information you want.
  368. </para>
  369. <para>
  370. Of course, we don't live in an ideal world so there may be times the
  371. <acronym>API</acronym> just does not cover what you're looking for. To assist you,
  372. <classname>Zend_Feed_Reader</classname> offers a plugin system which
  373. allows you to write Extensions to expand the core <acronym>API</acronym> and cover any
  374. additional data you are trying to extract from feeds. If writing
  375. another Extension is too much trouble, you can simply grab the
  376. underlying <acronym>DOM</acronym> or XPath objects and do it by hand in your
  377. application. Of course, we really do encourage writing an Extension
  378. simply to make it more portable and reusable.
  379. </para>
  380. <para>
  381. Here's a summary of the Core <acronym>API</acronym> for Feeds. You should note it
  382. comprises not only the basic <acronym>RSS</acronym> and Atom standards, but also
  383. accounts for a number of included Extensions bundled with
  384. <classname>Zend_Feed_Reader</classname>. The naming of these
  385. Extension sourced methods remain fairly generic - all Extension
  386. methods operate at the same level as the Core <acronym>API</acronym> though we do allow
  387. you to retrieve any specific Extension object separately if
  388. required.
  389. </para>
  390. <table>
  391. <title>Feed Level API Methods</title>
  392. <tgroup cols="2">
  393. <tbody>
  394. <row>
  395. <entry><methodname>getId()</methodname></entry>
  396. <entry>Returns a unique ID associated with this feed</entry>
  397. </row>
  398. <row>
  399. <entry><methodname>getTitle()</methodname></entry>
  400. <entry>Returns the title of the feed</entry>
  401. </row>
  402. <row>
  403. <entry><methodname>getDescription()</methodname></entry>
  404. <entry>Returns the text description of the feed</entry>
  405. </row>
  406. <row>
  407. <entry><methodname>getLink()</methodname></entry>
  408. <entry>
  409. Returns a <acronym>URI</acronym> to the <acronym>HTML</acronym> website
  410. containing the same or
  411. similar information as this feed (i.e. if the feed is from a blog,
  412. it should provide the blog's <acronym>URI</acronym> where the
  413. <acronym>HTML</acronym> version of the entries can be read).
  414. </entry>
  415. </row>
  416. <row>
  417. <entry><methodname>getFeedLink()</methodname></entry>
  418. <entry>
  419. Returns the <acronym>URI</acronym> of this feed, which should be the
  420. same as the <acronym>URI</acronym> used to import the feed
  421. </entry>
  422. </row>
  423. <row>
  424. <entry><methodname>getAuthors()</methodname></entry>
  425. <entry>
  426. Returns an array of all authors associated with this feed
  427. including email address in the author string if available
  428. </entry>
  429. </row>
  430. <row>
  431. <entry><methodname>getAuthor(integer $index = 0)</methodname></entry>
  432. <entry>
  433. Returns either the first author known, or with the
  434. optional <varname>$index</varname> parameter any specific
  435. index on the array of Authors (returning null if an
  436. invalid index).
  437. </entry>
  438. </row>
  439. <row>
  440. <entry><methodname>getDateCreated()</methodname></entry>
  441. <entry>
  442. Returns the date on which this feed was created. Generally
  443. only applicable to Atom where it represents the date the resource
  444. described by an Atom 1.0 document was created.
  445. </entry>
  446. </row>
  447. <row>
  448. <entry><methodname>getDateModified()</methodname></entry>
  449. <entry>
  450. Returns the date on which this feed was last modified
  451. </entry>
  452. </row>
  453. <row>
  454. <entry><methodname>getLanguage()</methodname></entry>
  455. <entry>
  456. Returns the language of the feed (if defined) or simply the
  457. language noted in the <acronym>XML</acronym> document
  458. </entry>
  459. </row>
  460. <row>
  461. <entry><methodname>getGenerator()</methodname></entry>
  462. <entry>
  463. Returns the generator of the feed, e.g. the software which
  464. generated it. This may differ between <acronym>RSS</acronym> and Atom
  465. since Atom defines a different notation.
  466. </entry>
  467. </row>
  468. <row>
  469. <entry><methodname>getCopyright()</methodname></entry>
  470. <entry>
  471. Returns any copyright notice associated with the feed
  472. </entry>
  473. </row>
  474. </tbody>
  475. </tgroup>
  476. </table>
  477. <para>
  478. Given the variety of feeds in the wild, some of these methods will
  479. undoubtedly return <constant>NULL</constant> indicating the relevant information
  480. couldn't be located. Where possible, <classname>Zend_Feed_Reader</classname>
  481. will fall back on alternative elements during its search. For
  482. example, searching an <acronym>RSS</acronym> feed for a modification date is more
  483. complicated than it looks. <acronym>RSS</acronym> 2.0 feeds should include a
  484. <code>&lt;lastBuildDate&gt;</code> tag and/or a
  485. <code>&lt;pubDate&gt;</code> element. But what if it doesn't, maybe
  486. this is an <acronym>RSS</acronym> 1.0 feed? Perhaps it instead has an
  487. <code>&lt;atom:updated&gt;</code> element with identical information
  488. (Atom may be used to supplement <acronym>RSS</acronym>'s syntax)? Failing that, we
  489. could simply look at the entries, pick the most recent, and use its
  490. <code>&lt;pubDate&gt;</code> element. Assuming it exists... Many
  491. feeds also use Dublin Core 1.0/1.1 <code>&lt;dc:date&gt;</code>
  492. elements for feeds/entries. Or we could find Atom lurking again.
  493. </para>
  494. <para>
  495. The point is, <classname>Zend_Feed_Reader</classname> was designed
  496. to know this. When you ask for the modification date (or anything
  497. else), it will run off and search for all these alternatives until
  498. it either gives up and returns <constant>NULL</constant>, or finds an
  499. alternative that should have the right answer.
  500. </para>
  501. <para>
  502. In addition to the above methods, all Feed objects implement methods
  503. for retrieving the <acronym>DOM</acronym> and XPath objects for the current feeds as
  504. described earlier. Feed objects also implement the <acronym>SPL</acronym> Iterator and
  505. Countable interfaces. The extended <acronym>API</acronym> is summarised below.
  506. </para>
  507. <table>
  508. <title>Extended Feed Level API Methods</title>
  509. <tgroup cols="2">
  510. <tbody>
  511. <row>
  512. <entry><methodname>getDomDocument()</methodname></entry>
  513. <entry>
  514. Returns the parent
  515. <classname>DOMDocument</classname> object for the
  516. entire source <acronym>XML</acronym> document
  517. </entry>
  518. </row>
  519. <row>
  520. <entry><methodname>getElement()</methodname></entry>
  521. <entry>
  522. Returns the current feed level
  523. <classname>DOMElement</classname> object
  524. </entry>
  525. </row>
  526. <row>
  527. <entry><methodname>saveXml()</methodname></entry>
  528. <entry>
  529. Returns a string containing an <acronym>XML</acronym> document of the
  530. entire feed element (this is not the original
  531. document but a rebuilt version)
  532. </entry>
  533. </row>
  534. <row>
  535. <entry><methodname>getXpath()</methodname></entry>
  536. <entry>
  537. Returns the <classname>DOMXPath</classname> object
  538. used internally to run queries on the
  539. <classname>DOMDocument</classname> object (this
  540. includes core and Extension namespaces
  541. pre-registered)
  542. </entry>
  543. </row>
  544. <row>
  545. <entry><methodname>getXpathPrefix()</methodname></entry>
  546. <entry>
  547. Returns the valid <acronym>DOM</acronym> path prefix prepended
  548. to all XPath queries matching the feed being queried
  549. </entry>
  550. </row>
  551. <row>
  552. <entry><methodname>getEncoding()</methodname></entry>
  553. <entry>
  554. Returns the encoding of the source <acronym>XML</acronym> document
  555. (note: this cannot account for errors such as the
  556. server sending documents in a different encoding)
  557. </entry>
  558. </row>
  559. <row>
  560. <entry><methodname>count()</methodname></entry>
  561. <entry>
  562. Returns a count of the entries or items this feed contains
  563. (implements <acronym>SPL</acronym> <classname>Countable</classname>
  564. interface)
  565. </entry>
  566. </row>
  567. <row>
  568. <entry><methodname>current()</methodname></entry>
  569. <entry>
  570. Returns either the current entry (using the current index
  571. from <methodname>key()</methodname>)
  572. </entry>
  573. </row>
  574. <row>
  575. <entry><methodname>key()</methodname></entry>
  576. <entry>Returns the current entry index</entry>
  577. </row>
  578. <row>
  579. <entry><methodname>next()</methodname></entry>
  580. <entry>Increments the entry index value by one</entry>
  581. </row>
  582. <row>
  583. <entry><methodname>rewind()</methodname></entry>
  584. <entry>Resets the entry index to 0</entry>
  585. </row>
  586. <row>
  587. <entry><methodname>valid()</methodname></entry>
  588. <entry>
  589. Checks that the current entry index is valid, i.e.
  590. it does fall below 0 and does not exceed the number
  591. of entries existing.
  592. </entry>
  593. </row>
  594. <row>
  595. <entry><methodname>getExtensions()</methodname></entry>
  596. <entry>
  597. Returns an array of all Extension objects loaded for
  598. the current feed (note: both feed-level and entry-level Extensions
  599. exist, and only feed-level Extensions are returned here).
  600. The array keys are of the form {ExtensionName}_Feed.
  601. </entry>
  602. </row>
  603. <row>
  604. <entry><methodname>getExtension(string $name)</methodname></entry>
  605. <entry>
  606. Returns an Extension object for the feed registered under the
  607. provided name. This allows more fine-grained access to
  608. Extensions which may otherwise be hidden within the implementation
  609. of the standard <acronym>API</acronym> methods.
  610. </entry>
  611. </row>
  612. <row>
  613. <entry><methodname>getType()</methodname></entry>
  614. <entry>
  615. Returns a static class constant (e.g.
  616. <constant>Zend_Feed_Reader::TYPE_ATOM_03</constant>,
  617. i.e. Atom 0.3) indicating exactly what kind of feed
  618. is being consumed.
  619. </entry>
  620. </row>
  621. </tbody>
  622. </tgroup>
  623. </table>
  624. </sect2>
  625. <sect2 id="zend.feed.reader.entry">
  626. <title>Retrieving Entry/Item Information</title>
  627. <para>
  628. Retrieving information for specific entries or items (depending on
  629. whether you speak Atom or <acronym>RSS</acronym>) is identical to feed level data.
  630. Accessing entries is simply a matter of iterating over a Feed object
  631. or using the <acronym>SPL</acronym> <classname>Iterator</classname> interface Feed
  632. objects implement and calling the appropriate method on each.
  633. </para>
  634. <table>
  635. <title>Entry Level API Methods</title>
  636. <tgroup cols="2">
  637. <tbody>
  638. <row>
  639. <entry><methodname>getId()</methodname></entry>
  640. <entry>Returns a unique ID for the current entry</entry>
  641. </row>
  642. <row>
  643. <entry><methodname>getTitle()</methodname></entry>
  644. <entry>Returns the title of the current entry</entry>
  645. </row>
  646. <row>
  647. <entry><methodname>getDescription()</methodname></entry>
  648. <entry>Returns a description of the current entry</entry>
  649. </row>
  650. <row>
  651. <entry><methodname>getLink()</methodname></entry>
  652. <entry>
  653. Returns a <acronym>URI</acronym> to the <acronym>HTML</acronym> version
  654. of the current entry
  655. </entry>
  656. </row>
  657. <row>
  658. <entry><methodname>getPermaLink()</methodname></entry>
  659. <entry>
  660. Returns the permanent link to the current entry
  661. </entry>
  662. </row>
  663. <row>
  664. <entry><methodname>getAuthors()</methodname></entry>
  665. <entry>
  666. Returns an array of all authors associated with this entry
  667. including email address in the author string if available
  668. </entry>
  669. </row>
  670. <row>
  671. <entry><methodname>getAuthor($index = 0)</methodname></entry>
  672. <entry>
  673. Returns either the first author known, or with the
  674. optional <varname>$index</varname> parameter any specific
  675. index on the array of Authors (returning null if an
  676. invalid index).
  677. </entry>
  678. </row>
  679. <row>
  680. <entry><methodname>getDateCreated()</methodname></entry>
  681. <entry>
  682. Returns the date on which the current entry was
  683. created. Generally only applicable to Atom where it
  684. represents the date the resource described by an
  685. Atom 1.0 document was created.
  686. </entry>
  687. </row>
  688. <row>
  689. <entry><methodname>getDateModified()</methodname></entry>
  690. <entry>
  691. Returns the date on which the current entry was last
  692. modified
  693. </entry>
  694. </row>
  695. <row>
  696. <entry><methodname>getContent()</methodname></entry>
  697. <entry>
  698. Returns the content of the current entry (this has any
  699. entities reversed if possible assuming the content type is
  700. <acronym>HTML</acronym>). The description is returned if a
  701. separate content element does not exist.
  702. </entry>
  703. </row>
  704. <row>
  705. <entry><methodname>getCommentCount()</methodname></entry>
  706. <entry>
  707. Returns the number of comments made on this entry at the
  708. time the feed was last generated
  709. </entry>
  710. </row>
  711. <row>
  712. <entry><methodname>getCommentLink()</methodname></entry>
  713. <entry>
  714. Returns a <acronym>URI</acronym> pointing to the <acronym>HTML</acronym>
  715. page where comments can be made on this entry
  716. </entry>
  717. </row>
  718. <row>
  719. <entry>
  720. <methodname>getCommentFeedLink(string $type =
  721. 'atom'|'rss')</methodname>
  722. </entry>
  723. <entry>
  724. Returns a <acronym>URI</acronym> pointing to a feed of the provided type
  725. containing all comments for this entry (type defaults to
  726. Atom/<acronym>RSS</acronym> depending on current feed type).
  727. </entry>
  728. </row>
  729. </tbody>
  730. </tgroup>
  731. </table>
  732. <para>
  733. The extended <acronym>API</acronym> for entries is identical to that for feeds with the
  734. exception of the Iterator methods which are not needed here.
  735. </para>
  736. <caution>
  737. <para>
  738. There is often confusion over the concepts of modified and
  739. created dates. In Atom, these are two clearly defined concepts
  740. (so knock yourself out) but in <acronym>RSS</acronym> they are vague.
  741. <acronym>RSS</acronym> 2.0
  742. defines a single <emphasis>&lt;pubDate&gt;</emphasis> element
  743. which typically refers to the date this entry was published,
  744. i.e. a creation date of sorts. This is not always the case, and
  745. it may change with updates or not. As a result, if you really
  746. want to check whether an entry has changed, don't rely on the
  747. results of <methodname>getDateModified()</methodname>. Instead,
  748. consider tracking the <acronym>MD5</acronym> hash of three other elements
  749. concatenated, e.g. using <methodname>getTitle()</methodname>,
  750. <methodname>getDescription()</methodname> and
  751. <methodname>getContent()</methodname>. If the entry was trully
  752. updated, this hash computation will give a different result than
  753. previously saved hashes for the same entry. Further muddying the
  754. waters, dates in feeds may follow different standards. Atom and
  755. Dublin Core dates should follow <acronym>ISO</acronym> 8601,
  756. and <acronym>RSS</acronym> dates should
  757. follow <acronym>RFC</acronym> 822 or <acronym>RFC</acronym> 2822
  758. which is also common. Date methods
  759. will throw an exception if <classname>Zend_Date</classname>
  760. cannot load the date string using one of the above standards.
  761. </para>
  762. </caution>
  763. <warning>
  764. <para>
  765. The values returned from these methods are not validated. This
  766. means users must perform validation on all retrieved data
  767. including the filtering of any <acronym>HTML</acronym> such as from
  768. <methodname>getContent()</methodname> before it is output from
  769. your application. Remember that most feeds come from external
  770. sources, and therefore the default assumption should be that
  771. they cannot be trusted.
  772. </para>
  773. </warning>
  774. <table>
  775. <title>Extended Entry Level API Methods</title>
  776. <tgroup cols="2">
  777. <tbody>
  778. <row>
  779. <entry><methodname>getDomDocument()</methodname></entry>
  780. <entry>
  781. Returns the parent
  782. <classname>DOMDocument</classname> object for the
  783. entire feed (not just the current entry)
  784. </entry>
  785. </row>
  786. <row>
  787. <entry><methodname>getElement()</methodname></entry>
  788. <entry>
  789. Returns the current entry level
  790. <classname>DOMElement</classname> object
  791. </entry>
  792. </row>
  793. <row>
  794. <entry><methodname>getXpath()</methodname></entry>
  795. <entry>
  796. Returns the <classname>DOMXPath</classname> object
  797. used internally to run queries on the
  798. <classname>DOMDocument</classname> object (this
  799. includes core and Extension namespaces
  800. pre-registered)
  801. </entry>
  802. </row>
  803. <row>
  804. <entry><methodname>getXpathPrefix()</methodname></entry>
  805. <entry>
  806. Returns the valid <acronym>DOM</acronym> path prefix prepended
  807. to all XPath queries matching the entry being queried
  808. </entry>
  809. </row>
  810. <row>
  811. <entry><methodname>getEncoding()</methodname></entry>
  812. <entry>
  813. Returns the encoding of the source <acronym>XML</acronym> document
  814. (note: this cannot account for errors such as the server sending
  815. documents in a different encoding)
  816. </entry>
  817. </row>
  818. <row>
  819. <entry><methodname>getExtensions()</methodname></entry>
  820. <entry>
  821. Returns an array of all Extension objects loaded for
  822. the current entry (note: both feed-level and entry-level
  823. Extensions exist, and only entry-level Extensions are returned
  824. here). The array keys are in the form {ExtensionName}_Entry.
  825. </entry>
  826. </row>
  827. <row>
  828. <entry><methodname>getExtension(string $name)</methodname></entry>
  829. <entry>
  830. Returns an Extension object for the entry registered under the
  831. provided name. This allows more fine-grained access to
  832. Extensions which may otherwise be hidden within the implementation
  833. of the standard <acronym>API</acronym> methods.
  834. </entry>
  835. </row>
  836. <row>
  837. <entry><methodname>getType()</methodname></entry>
  838. <entry>
  839. Returns a static class constant (e.g.
  840. <constant>Zend_Feed_Reader::TYPE_ATOM_03</constant>,
  841. i.e. Atom 0.3) indicating exactly what kind
  842. of feed is being consumed.
  843. </entry>
  844. </row>
  845. </tbody>
  846. </tgroup>
  847. </table>
  848. </sect2>
  849. <sect2 id="zend.feed.reader.extending">
  850. <title>Extending Feed and Entry APIs</title>
  851. <para>
  852. Extending <classname>Zend_Feed_Reader</classname> allows you to add
  853. methods at both the feed and entry level which cover the retrieval
  854. of information not already supported by
  855. <classname>Zend_Feed_Reader</classname>. Given the number of
  856. <acronym>RSS</acronym> and
  857. Atom extensions that exist, this is a good thing since
  858. <classname>Zend_Feed_Reader</classname> couldn't possibly add
  859. everything.
  860. </para>
  861. <para>
  862. There are two types of Extensions possible, those which retrieve
  863. information from elements which are immediate children of the root
  864. element (e.g. <code>&lt;channel&gt;</code> for <acronym>RSS</acronym> or
  865. <code>&lt;feed&gt;</code> for Atom) and those who retrieve
  866. information from child elements of an entry (e.g.
  867. <code>&lt;item&gt;</code> for <acronym>RSS</acronym> or <code>&lt;entry&gt;</code> for
  868. Atom). On the filesystem these are grouped as classes within
  869. a namespace based on the extension standard's name. For
  870. example, internally we have
  871. <classname>Zend_Feed_Reader_Extension_DublinCore_Feed</classname>
  872. and <classname>Zend_Feed_Reader_Extension_DublinCore_Entry</classname>
  873. classes which are two Extensions implementing Dublin Core
  874. 1.0/1.1 support.
  875. </para>
  876. <para>
  877. Extensions are loaded into <classname>Zend_Feed_Reader</classname>
  878. using <classname>Zend_Loader_PluginLoader</classname>, so their operation
  879. will be familiar from other Zend Framework components.
  880. <classname>Zend_Feed_Reader</classname> already bundles a number of
  881. these Extensions, however those which are not used internally and
  882. registered by default (so called Core Extensions) must be registered
  883. to <classname>Zend_Feed_Reader</classname> before they are used. The
  884. bundled Extensions include:
  885. </para>
  886. <table>
  887. <title>Core Extensions (pre-registered)</title>
  888. <tgroup cols="2">
  889. <tbody>
  890. <row>
  891. <entry>DublinCore (Feed and Entry)</entry>
  892. <entry>Implements support for Dublin Core Metadata Element Set 1.0
  893. and 1.1 </entry>
  894. </row>
  895. <row>
  896. <entry>Content (Entry only)</entry>
  897. <entry>Implements support for Content 1.0</entry>
  898. </row>
  899. <row>
  900. <entry>Atom (Feed and Entry)</entry>
  901. <entry>Implements support for Atom 0.3 and Atom 1.0</entry>
  902. </row>
  903. <row>
  904. <entry>Slash</entry>
  905. <entry>Implements support for the Slash <acronym>RSS</acronym> 1.0 module</entry>
  906. </row>
  907. <row>
  908. <entry>WellFormedWeb</entry>
  909. <entry>Implements support for the Well Formed Web CommentAPI 1.0</entry>
  910. </row>
  911. <row>
  912. <entry>Thread</entry>
  913. <entry>Implements support for Atom Threading Extensions as described
  914. in <acronym>RFC</acronym> 4685</entry>
  915. </row>
  916. <row>
  917. <entry>Podcast</entry>
  918. <entry>Implements support for the Podcast 1.0 <acronym>DTD</acronym> from Apple</entry>
  919. </row>
  920. </tbody>
  921. </tgroup>
  922. </table>
  923. <para>
  924. The Core Extensions are somewhat special since they are extremely
  925. common and multi-faceted. For example, we have a Core Extension for Atom.
  926. Atom is implemented as an Extension (not just a base class) because it
  927. doubles as a valid <acronym>RSS</acronym> module - you can insert
  928. Atom elements into <acronym>RSS</acronym> feeds. I've even seen
  929. <acronym>RDF</acronym> feeds which use a lot of Atom in place of more
  930. common Extensions like Dublin Core.
  931. </para>
  932. <table>
  933. <title>Non-Core Extensions (must register manually)</title>
  934. <tgroup cols="2">
  935. <tbody>
  936. <row>
  937. <entry>Syndication</entry>
  938. <entry>Implements Syndication 1.0 support for <acronym>RSS</acronym> feeds</entry>
  939. </row>
  940. <row>
  941. <entry>CreativeCommons</entry>
  942. <entry>A <acronym>RSS</acronym> module that adds an element at the &lt;channel&gt;
  943. or &lt;item&gt; level that specifies which Creative Commons license
  944. applies.</entry>
  945. </row>
  946. </tbody>
  947. </tgroup>
  948. </table>
  949. <para>
  950. The additional non-Core Extensions are offered but not registered to
  951. <classname>Zend_Feed_Reader</classname> by default. If you want to
  952. use them, you'll need to tell
  953. <classname>Zend_Feed_Reader</classname> to load them in advance of
  954. importing a feed. Additional non-Core Extensions will be included
  955. in future iterations of the component.
  956. </para>
  957. <para>
  958. Registering an Extension with
  959. <classname>Zend_Feed_Reader</classname>, so it is loaded and its <acronym>API</acronym>
  960. is available to Feed and Entry objects, is a simple affair using the
  961. <classname>Zend_Loader_PluginLoader</classname>. Here we register
  962. the optional Slash Extension, and discover that it can be directly
  963. called from the Entry level <acronym>API</acronym> without any effort. Note that
  964. Extension names are case sensitive and use camel casing for multiple
  965. terms.
  966. </para>
  967. <programlisting language="php"><![CDATA[
  968. Zend_Feed_Reader::registerExtension('Syndication');
  969. $feed = Zend_Feed_Reader::import('http://rss.slashdot.org/Slashdot/slashdot');
  970. $updatePeriod = $feed->current()->getUpdatePeriod();
  971. ]]></programlisting>
  972. <para>
  973. In the simple example above, we checked how frequently a feed is being updated
  974. using the <methodname>getUpdatePeriod()</methodname>
  975. method. Since it's not part of
  976. <classname>Zend_Feed_Reader</classname>'s core <acronym>API</acronym>, it could only be
  977. a method supported by the newly registered Syndication Extension.
  978. </para>
  979. <para>
  980. As you can also notice, the new methods from Extensions are accessible from the main
  981. <acronym>API</acronym> using <acronym>PHP</acronym>'s magic methods. As an alternative,
  982. you can also directly access any Extension object for a similar result as seen below.
  983. </para>
  984. <programlisting language="php"><![CDATA[
  985. Zend_Feed_Reader::registerExtension('Syndication');
  986. $feed = Zend_Feed_Reader::import('http://rss.slashdot.org/Slashdot/slashdot');
  987. $syndication = $feed->getExtension('Syndication');
  988. $updatePeriod = $syndication->getUpdatePeriod();
  989. ]]></programlisting>
  990. <sect3 id="zend.feed.reader.extending.feed">
  991. <title>Writing Zend_Feed_Reader Extensions</title>
  992. <para>
  993. Inevitably, there will be times when the
  994. <classname>Zend_Feed_Reader</classname> <acronym>API</acronym> is just not capable
  995. of getting something you need from a feed or entry. You can use
  996. the underlying source objects, like
  997. <classname>DOMDocument</classname>, to get these by hand however
  998. there is a more reusable method available by writing Extensions
  999. supporting these new queries.
  1000. </para>
  1001. <para>
  1002. As an example, let's take the case of a purely fictitious
  1003. corporation named Jungle Books. Jungle Books have been
  1004. publishing a lot of reviews on books they sell (from external
  1005. sources and customers), which are distributed as an <acronym>RSS</acronym> 2.0
  1006. feed. Their marketing department realises that web applications
  1007. using this feed cannot currently figure out exactly what book is
  1008. being reviewed. To make life easier for everyone, they determine
  1009. that the geek department needs to extend <acronym>RSS</acronym> 2.0 to include a
  1010. new element per entry supplying the <acronym>ISBN</acronym>-10 or
  1011. <acronym>ISBN</acronym>-13 number of
  1012. the publication the entry concerns. They define the new
  1013. <code>&lt;isbn&gt;</code> element quite simply with a standard
  1014. name and namespace <acronym>URI</acronym>:
  1015. </para>
  1016. <programlisting language="php"><![CDATA[
  1017. JungleBooks 1.0:
  1018. http://example.com/junglebooks/rss/module/1.0/
  1019. ]]></programlisting>
  1020. <para>
  1021. A snippet of <acronym>RSS</acronym> containing this extension in practice could be
  1022. something similar to:
  1023. </para>
  1024. <programlisting language="php"><![CDATA[
  1025. <?xml version="1.0" encoding="utf-8" ?>
  1026. <rss version="2.0"
  1027. xmlns:content="http://purl.org/rss/1.0/modules/content/"
  1028. xmlns:jungle="http://example.com/junglebooks/rss/module/1.0/">
  1029. <channel>
  1030. <title>Jungle Books Customer Reviews</title>
  1031. <link>http://example.com/junglebooks</link>
  1032. <description>Many book reviews!</description>
  1033. <pubDate>Fri, 26 Jun 2009 19:15:10 GMT</pubDate>
  1034. <jungle:dayPopular>http://example.com/junglebooks/book/938</jungle:dayPopular>
  1035. <item>
  1036. <title>Review Of Flatland: A Romance of Many Dimensions</title>
  1037. <link>http://example.com/junglebooks/review/987</link>
  1038. <author>Confused Physics Student</author>
  1039. <content:encoded>
  1040. A romantic square?!
  1041. </content:encoded>
  1042. <pubDate>Thu, 25 Jun 2009 20:03:28 -0700</pubDate>
  1043. <jungle:isbn>048627263X</jungle:isbn>
  1044. </item>
  1045. </channel>
  1046. </rss>
  1047. ]]></programlisting>
  1048. <para>
  1049. Implementing this new <acronym>ISBN</acronym> element as a simple entry level
  1050. extension would require the following class (using your own class
  1051. namespace outside of Zend).
  1052. </para>
  1053. <programlisting language="php"><![CDATA[
  1054. class My_FeedReader_Extension_JungleBooks_Entry
  1055. extends Zend_Feed_Reader_Extension_EntryAbstract
  1056. {
  1057. public function getIsbn()
  1058. {
  1059. if (isset($this->_data['isbn'])) {
  1060. return $this->_data['isbn'];
  1061. }
  1062. $isbn = $this->_xpath->evaluate(
  1063. 'string(' . $this->getXpathPrefix() . '/jungle:isbn)'
  1064. );
  1065. if (!$isbn) {
  1066. $isbn = null;
  1067. }
  1068. $this->_data['isbn'] = $isbn;
  1069. return $this->_data['isbn'];
  1070. }
  1071. protected function _registerNamespaces()
  1072. {
  1073. $this->_xpath->registerNamespace(
  1074. 'jungle', 'http://example.com/junglebooks/rss/module/1.0/'
  1075. );
  1076. }
  1077. }
  1078. ]]></programlisting>
  1079. <para>
  1080. This extension is easy enough to follow. It creates a new method
  1081. <methodname>getIsbn()</methodname> which runs an XPath query on
  1082. the current entry to extract the <acronym>ISBN</acronym> number enclosed by the
  1083. <code>&lt;jungle:isbn&gt;</code> element. It can optionally
  1084. store this to the internal non-persistent cache (no need to keep
  1085. querying the <acronym>DOM</acronym> if it's called again on the same entry). The
  1086. value is returned to the caller. At the end we have a protected
  1087. method (it's abstract so it must exist) which registers the
  1088. Jungle Books namespace for their custom <acronym>RSS</acronym> module. While we
  1089. call this an <acronym>RSS</acronym> module, there's nothing to prevent the same
  1090. element being used in Atom feeds - and all Extensions which use
  1091. the prefix provided by <methodname>getXpathPrefix()</methodname>
  1092. are actually neutral and work on <acronym>RSS</acronym> or Atom feeds with no
  1093. extra code.
  1094. </para>
  1095. <para>
  1096. Since this Extension is stored outside of Zend Framework, you'll
  1097. need to register the path prefix for your Extensions so
  1098. <classname>Zend_Loader_PluginLoader</classname> can find them.
  1099. After that, it's merely a matter of registering the Extension,
  1100. if it's not already loaded, and using it in practice.
  1101. </para>
  1102. <programlisting language="php"><![CDATA[
  1103. if(!Zend_Feed_Reader::isRegistered('JungleBooks')) {
  1104. Zend_Feed_Reader::addPrefixPath(
  1105. '/path/to/My/FeedReader/Extension', 'My_FeedReader_Extension'
  1106. );
  1107. Zend_Feed_Reader::registerExtension('JungleBooks');
  1108. }
  1109. $feed = Zend_Feed_Reader::import('http://example.com/junglebooks/rss');
  1110. // ISBN for whatever book the first entry in the feed was concerned with
  1111. $firstIsbn = $feed->current()->getIsbn();
  1112. ]]></programlisting>
  1113. <para>
  1114. Writing a feed level Extension is not much different. The
  1115. example feed from earlier included an unmentioned
  1116. <code>&lt;jungle:dayPopular&gt;</code> element which Jungle
  1117. Books have added to their standard to include a link to the
  1118. day's most popular book (in terms of visitor traffic). Here's
  1119. an Extension which adds a
  1120. <methodname>getDaysPopularBookLink()</methodname> method to the
  1121. feel level <acronym>API</acronym>.
  1122. </para>
  1123. <programlisting language="php"><![CDATA[
  1124. class My_FeedReader_Extension_JungleBooks_Feed
  1125. extends Zend_Feed_Reader_Extension_FeedAbstract
  1126. {
  1127. public function getDaysPopularBookLink()
  1128. {
  1129. if (isset($this->_data['dayPopular'])) {
  1130. return $this->_data['dayPopular'];
  1131. }
  1132. $dayPopular = $this->_xpath->evaluate(
  1133. 'string(' . $this->getXpathPrefix() . '/jungle:dayPopular)'
  1134. );
  1135. if (!$dayPopular) {
  1136. $dayPopular = null;
  1137. }
  1138. $this->_data['dayPopular'] = $dayPopular;
  1139. return $this->_data['dayPopular'];
  1140. }
  1141. protected function _registerNamespaces()
  1142. {
  1143. $this->_xpath->registerNamespace(
  1144. 'jungle', 'http://example.com/junglebooks/rss/module/1.0/'
  1145. );
  1146. }
  1147. }
  1148. ]]></programlisting>
  1149. <para>
  1150. Let's repeat the last example using a custom Extension to show the
  1151. method being used.
  1152. </para>
  1153. <programlisting language="php"><![CDATA[
  1154. if(!Zend_Feed_Reader::isRegistered('JungleBooks')) {
  1155. Zend_Feed_Reader::addPrefixPath(
  1156. '/path/to/My/FeedReader/Extension', 'My_FeedReader_Extension'
  1157. );
  1158. Zend_Feed_Reader::registerExtension('JungleBooks');
  1159. }
  1160. $feed = Zend_Feed_Reader::import('http://example.com/junglebooks/rss');
  1161. // URI to the information page of the day's most popular book with visitors
  1162. $daysPopularBookLink = $feed->getDaysPopularBookLink();
  1163. // ISBN for whatever book the first entry in the feed was concerned with
  1164. $firstIsbn = $feed->current()->getIsbn();
  1165. ]]></programlisting>
  1166. <para>
  1167. Going through these examples, you'll note that we don't register
  1168. feed and entry Extensions separately. Extensions within the same
  1169. standard may or may not include both a feed and entry class, so
  1170. <classname>Zend_Feed_Reader</classname> only requires you to
  1171. register the overall parent name, e.g. JungleBooks, DublinCore,
  1172. Slash. Internally, it can check at what level Extensions exist
  1173. and load them up if found. In our case, we have a full set of
  1174. Extensions now: <classname>JungleBooks_Feed</classname> and
  1175. <classname>JungleBooks_Entry</classname>.
  1176. </para>
  1177. </sect3>
  1178. </sect2>
  1179. </sect1>