Zend_Feed_Reader.xml 62 KB

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