Zend_Feed_Reader.xml 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.feed.reader">
  4. <title>Zend_Feed_Reader</title>
  5. <sect2 id="zend.feed.reader.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. <classname>Zend_Feed_Reader</classname> is a component used to
  9. consume <acronym>RSS</acronym> and Atom feeds of any version, including
  10. <acronym>RDF</acronym>/<acronym>RSS</acronym> 1.0,
  11. <acronym>RSS</acronym> 2.0 and Atom 0.3/1.0. The <acronym>API</acronym> for
  12. retrieving feed data is
  13. deliberately simple since <classname>Zend_Feed_Reader</classname> is
  14. capable of searching any feed of any type for the information
  15. requested through the <acronym>API</acronym>. If the typical elements containing this
  16. information are not present, it will adapt and fall back on a
  17. variety of alternative elements instead. This ability to choose from
  18. alternatives removes the need for users to create their own
  19. abstraction layer on top of the component to make it useful or have
  20. any in-depth knowledge of the underlying standards, current
  21. alternatives, and namespaced extensions.
  22. </para>
  23. <para>
  24. Internally, <classname>Zend_Feed_Reader</classname> works almost
  25. entirely on the basis of making XPath queries against the feed <acronym>XML</acronym>'s
  26. Document Object Model. The <acronym>DOM</acronym> is not exposed though a chained
  27. property <acronym>API</acronym> like <classname>Zend_Feed</classname> though the
  28. underlying <classname>DOMDocument</classname>,
  29. <classname>DOMElement</classname> and
  30. <classname>DOMXPath</classname> objects are exposed for external
  31. manipulation. This singular approach to parsing is consistent and
  32. the component offers a plugin system to add to the Feed and Entry
  33. level <acronym>API</acronym> by writing Extensions on a similar basis.
  34. </para>
  35. <para>
  36. Performance is assisted in three ways. First of all,
  37. <classname>Zend_Feed_Reader</classname> supports caching using
  38. <classname>Zend_Cache</classname> to maintain a copy of the original
  39. feed <acronym>XML</acronym>. This allows you to skip network requests for a feed
  40. <acronym>URI</acronym> if
  41. the cache is valid. Second, the Feed and Entry level <acronym>API</acronym> is backed
  42. by an internal cache (non-persistant) so repeat <acronym>API</acronym> calls for the
  43. same feed will avoid additional <acronym>DOM</acronym>/XPath use. Thirdly, importing
  44. feeds from a <acronym>URI</acronym> can take advantage of
  45. <acronym>HTTP</acronym> Conditional GET requests
  46. which allow servers to issue an empty 304 response when the
  47. requested feed has not changed since the last time you requested it.
  48. In the final case, an instance of <classname>Zend_Cache</classname>
  49. will hold the last received feed along with the ETag and
  50. Last-Modified header values sent in the <acronym>HTTP</acronym> response.
  51. </para>
  52. <para>
  53. In relation to <classname>Zend_Feed</classname>,
  54. <classname>Zend_Feed_Reader</classname> was formulated as a free
  55. standing replacement for <classname>Zend_Feed</classname> but it is
  56. not backwards compatible with <classname>Zend_Feed</classname>.
  57. Rather it is an alternative following a different ideology focused
  58. on being simple to use, flexible, consistent and extendable through
  59. the plugin system. <classname>Zend_Feed_Reader</classname> is also
  60. not capable of constructing feeds through this will be addressed at
  61. a future date.
  62. </para>
  63. </sect2>
  64. <sect2 id="zend.feed.reader.import">
  65. <title>Importing Feeds</title>
  66. <para>
  67. Importing a feed with <classname>Zend_Feed_Reader</classname> is not
  68. that much different to <classname>Zend_Feed</classname>. Feeds can
  69. be imported from a string, file, <acronym>URI</acronym> or an instance of type
  70. <classname>Zend_Feed_Abstract</classname>. Importing from a <acronym>URI</acronym> can
  71. additionally utilise a <acronym>HTTP</acronym> Conditional GET request. If importing
  72. fails, an exception will be raised. The end result will be an object
  73. of type <classname>Zend_Feed_Reader_FeedInterface</classname>, the
  74. core implementations of which are
  75. <classname>Zend_Feed_Reader_Feed_Rss</classname> and
  76. <classname>Zend_Feed_Reader_Feed_Atom</classname>
  77. (<classname>Zend_Feed</classname> took all the short names!). Both
  78. objects support multiple (all existing) versions of these broad feed
  79. types.
  80. </para>
  81. <para>
  82. In the following example, we import an <acronym>RDF</acronym>/<acronym>RSS</acronym> 1.0
  83. feed and extract some basic information that can be saved to a database or
  84. elsewhere.
  85. </para>
  86. <programlisting language="php"><![CDATA[
  87. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  88. $data = array(
  89. 'title' => $feed->getTitle(),
  90. 'link' => $feed->getLink(),
  91. 'dateModified' => $feed->getDateModified(),
  92. 'description' => $feed->getDescription(),
  93. 'language' => $feed->getLanguage(),
  94. 'entries' => array(),
  95. );
  96. foreach ($feed as $entry) {
  97. $edata = array(
  98. 'title' => $entry->getTitle(),
  99. 'description' => $entry->getDescription(),
  100. 'dateModified' => $entry->getDateModified(),
  101. 'author' => $entry->getAuthor(),
  102. 'link' => $entry->getLink(),
  103. 'content' => $entry->getContent()
  104. );
  105. $data['entries'][] = $edata;
  106. }
  107. ]]></programlisting>
  108. <para>
  109. The example above demonstrates
  110. <classname>Zend_Feed_Reader</classname>'s <acronym>API</acronym>, and it also
  111. demonstrates some of it's internal operation. In reality, the <acronym>RDF</acronym>
  112. feed selected does not have any native date or author elements,
  113. however it does utilise the Dublin Core 1.1 module which offers
  114. namespaced creator and date elements.
  115. <classname>Zend_Feed_Reader</classname> falls back on these and
  116. similar options if no relevant native elements exist. If it
  117. absolutely cannot find an alternative it will return <constant>NULL</constant>,
  118. indicating the information could not be found in the feed. You
  119. should note that classes implementing
  120. <classname>Zend_Feed_Reader_FeedInterface</classname> also implement
  121. the <acronym>SPL</acronym> <classname>Iterator</classname> and
  122. <classname>Countable</classname> interfaces.
  123. </para>
  124. <para>
  125. Feeds can also be imported from strings, files, and even objects of
  126. type <classname>Zend_Feed_Abstract</classname>.
  127. </para>
  128. <programlisting language="php"><![CDATA[
  129. // from a URI
  130. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  131. // from a String
  132. $feed = Zend_Feed_Reader::importString($feedXmlString);
  133. // from a file
  134. $feed = Zend_Feed_Reader::importFile('./feed.xml');
  135. // from a Zend_Feed_Abstract object
  136. $zfeed = Zend_Feed::import('http://www.planet-php.net/atom/');
  137. $feed = Zend_Feed_Reader::importFeed($zfeed);
  138. ]]></programlisting>
  139. </sect2>
  140. <sect2 id="zend.feed.reader.sources">
  141. <title>Retrieving Underlying Feed and Entry Sources</title>
  142. <para>
  143. <classname>Zend_Feed_Reader</classname> does it's best not to stick
  144. you in a narrow confine. If you need to work on a feed outside of
  145. <classname>Zend_Feed_Reader</classname>, you can extract the base
  146. <classname>DOMDocument</classname> or
  147. <classname>DOMElement</classname> objects from any class, or even an
  148. <acronym>XML</acronym> string containing these. Also provided are methods to extract
  149. the current <classname>DOMXPath</classname> object (with all core
  150. and Extension namespaces registered) and the correct prefix used in
  151. all XPath queries for the current Feed or Entry. The basic methods
  152. to use (on any object) are <methodname>saveXml()</methodname>,
  153. <methodname>getDomDocument()</methodname>,
  154. <methodname>getElement()</methodname>,
  155. <methodname>getXpath()</methodname> and
  156. <methodname>getXpathPrefix()</methodname>. These will let you break
  157. free of <classname>Zend_Feed_Reader</classname> and do whatever else
  158. you want.
  159. </para>
  160. <itemizedlist>
  161. <listitem>
  162. <para>
  163. <methodname>saveXml()</methodname> returns an <acronym>XML</acronym> string
  164. containing only the element representing the current object.
  165. </para>
  166. </listitem>
  167. <listitem>
  168. <para>
  169. <methodname>getDomDocument()</methodname> returns the
  170. <classname>DOMDocument</classname> object representing the
  171. entire feed (even if called from an Entry object).
  172. </para>
  173. </listitem>
  174. <listitem>
  175. <para>
  176. <methodname>getElement()</methodname> returns the
  177. <classname>DOMElement</classname> of the current object
  178. (i.e. the Feed or current Entry).
  179. </para>
  180. </listitem>
  181. <listitem>
  182. <para>
  183. <methodname>getXpath()</methodname> returns the
  184. <classname>DOMXPath</classname> object for the current feed
  185. (even if called from an Entry object) with the namespaces of
  186. the current feed type and all loaded Extensions
  187. pre-registered.
  188. </para>
  189. </listitem>
  190. <listitem>
  191. <para>
  192. <methodname>getXpathPrefix()</methodname> returns the query
  193. prefix for the current object (i.e. the Feed or current
  194. Entry) which includes the correct XPath query path for that
  195. specific Feed or Entry.
  196. </para>
  197. </listitem>
  198. </itemizedlist>
  199. <para>
  200. Here's an example where a feed might include an <acronym>RSS</acronym> Extension not
  201. supported by <classname>Zend_Feed_Reader</classname> out of the box.
  202. Notably, you could write and register an Extension (covered later)
  203. to do this, but that's not always warranted for a quick check. You
  204. must register any new namespaces on the
  205. <classname>DOMXPath</classname> object before use unless they are
  206. registered by <classname>Zend_Feed_Reader</classname> or an
  207. Extension beforehand.
  208. </para>
  209. <programlisting language="php"><![CDATA[
  210. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  211. $xpathPrefix = $feed->getXpathPrefix();
  212. $xpath = $feed->getXpath();
  213. $xpath->registerNamespace('admin', 'http://webns.net/mvcb/');
  214. $reportErrorsTo = $xpath->evaluate('string('
  215. . $xpathPrefix
  216. . '/admin:errorReportsTo)');
  217. ]]></programlisting>
  218. <warning>
  219. <para>
  220. If you register an already registered namespace with a different
  221. prefix name to that used internally by
  222. <classname>Zend_Feed_Reader</classname>, it will break the
  223. internal operation of this component.
  224. </para>
  225. </warning>
  226. </sect2>
  227. <sect2 id="zend.feed.reader.cache-request">
  228. <title>Cache Support and Intelligent Requests</title>
  229. <sect3 id="zend.feed.reader.cache-request.cache">
  230. <title>Adding Cache Support to Zend_Feed_Reader</title>
  231. <para>
  232. <classname>Zend_Feed_Reader</classname> supports using an
  233. instance of <classname>Zend_Cache</classname> to cache feeds (as
  234. <acronym>XML</acronym>) to avoid unnecessary network requests. Adding a cache is as
  235. simple here as it is for other Zend Framework components, create
  236. and configure your cache and then tell
  237. <classname>Zend_Feed_Reader</classname> to use it! The cache key
  238. used is "<classname>Zend_Feed_Reader_</classname>" followed by the
  239. <acronym>MD5</acronym> hash of the feed's <acronym>URI</acronym>.
  240. </para>
  241. <programlisting language="php"><![CDATA[
  242. $frontendOptions = array(
  243. 'lifetime' => 7200,
  244. 'automatic_serialization' => true
  245. );
  246. $backendOptions = array('cache_dir' => './tmp/');
  247. $cache = Zend_Cache::factory(
  248. 'Core', 'File', $frontendOptions, $backendOptions
  249. );
  250. Zend_Feed_Reader::setCache($cache);
  251. ]]></programlisting>
  252. <note>
  253. <para>
  254. While it's a little off track, you should also consider
  255. adding a cache to
  256. <classname>Zend_Loader_PluginLoader</classname> which is
  257. used by <classname>Zend_Feed_Reader</classname> to load
  258. Extensions.
  259. </para>
  260. </note>
  261. </sect3>
  262. <sect3 id="zend.feed.reader.cache-request.http-conditional-get">
  263. <title>HTTP Conditional GET Support</title>
  264. <para>
  265. The big question often asked when importing a feed frequently, is
  266. if it has even changed. With a cache enabled, you can add <acronym>HTTP</acronym>
  267. Conditional GET support to your arsenal to answer that question.
  268. </para>
  269. <para>
  270. Using this method, you can request feeds from <acronym>URI</acronym>s and include
  271. their last known ETag and Last-Modified response header values
  272. with the request (using the If-None-Match and If-Modified-Since
  273. headers). If the feed on the server remains unchanged, you
  274. should receive a 304 response which tells
  275. <classname>Zend_Feed_Reader</classname> to use the cached
  276. version. If a full feed is sent in a response with a status code
  277. of 200, this means the feed has changed and
  278. <classname>Zend_Feed_Reader</classname> will parse the new
  279. version and save it to the cache. It will also cache the new
  280. ETag and Last-Modified header values for future use.
  281. </para>
  282. <para>
  283. These "conditional" requests are not guaranteed to be supported
  284. by the server you request a <acronym>URI</acronym> of, but can be attempted
  285. regardless. Most common feed sources like blogs should however
  286. have this supported. To enable conditional requests, you will
  287. need to provide a cache to <classname>Zend_Feed_Reader</classname>.
  288. </para>
  289. <programlisting language="php"><![CDATA[
  290. $frontendOptions = array(
  291. 'lifetime' => 86400,
  292. 'automatic_serialization' => true
  293. );
  294. $backendOptions = array('cache_dir' => './tmp/');
  295. $cache = Zend_Cache::factory(
  296. 'Core', 'File', $frontendOptions, $backendOptions
  297. );
  298. Zend_Feed_Reader::setCache($cache);
  299. Zend_Feed_Reader::useHttpConditionalGet();
  300. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  301. ]]></programlisting>
  302. <para>
  303. In the example above, with <acronym>HTTP</acronym> Conditional GET requests enabled,
  304. the response header values for ETag and Last-Modified will be cached
  305. along with the feed. For the next 24hrs (the cache lifetime), feeds will
  306. only be updated on the cache if a non-304 response is received
  307. containing a valid <acronym>RSS</acronym> or Atom <acronym>XML</acronym> document.
  308. </para>
  309. <para>
  310. If you intend on managing request headers from outside
  311. <classname>Zend_Feed_Reader</classname>, you can set the
  312. relevant If-None-Matches and If-Modified-Since request headers
  313. via the <acronym>URI</acronym> import method.
  314. </para>
  315. <programlisting language="php"><![CDATA[
  316. $lastEtagReceived = '5e6cefe7df5a7e95c8b1ba1a2ccaff3d';
  317. $lastModifiedDateReceived = 'Wed, 08 Jul 2009 13:37:22 GMT';
  318. $feed = Zend_Feed_Reader::import(
  319. $uri, $lastEtagReceived, $lastModifiedDateReceived
  320. );
  321. ]]></programlisting>
  322. </sect3>
  323. </sect2>
  324. <sect2 id="zend.feed.reader.locate">
  325. <title>Locating Feed URIs from Websites</title>
  326. <para>
  327. These days, many websites are aware that the location of their <acronym>XML</acronym>
  328. feeds is not always obvious. A small <acronym>RDF</acronym>, <acronym>RSS</acronym> or
  329. Atom graphic helps when the user is reading the page, but what about when a machine
  330. visits trying to identify where your feeds are located? To assist in
  331. this, websites may point to their feeds using &lt;link&gt; tags in
  332. the &lt;head&gt; section of their <acronym>HTML</acronym>. To take advantage of this,
  333. you can use <classname>Zend_Feed_Reader</classname> to locate these
  334. feeds using the static <methodname>findFeedLinks()</methodname>
  335. method.
  336. </para>
  337. <para>
  338. This method calls any <acronym>URI</acronym> and searches for the location of
  339. <acronym>RSS</acronym>, <acronym>RDF</acronym>
  340. and Atom feeds assuming the wlebsite's <acronym>HTML</acronym> contains the relevant
  341. links. It then returns a value object where you can check for the existence of a
  342. <acronym>RSS</acronym>, <acronym>RDF</acronym> or Atom feed <acronym>URI</acronym>.
  343. </para>
  344. <programlisting language="php"><![CDATA[
  345. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  346. if(isset($links->rdf)) {
  347. echo $links->rdf, "\n"; // http://www.planet-php.org/rdf/
  348. }
  349. if(isset($links->rss)) {
  350. echo $links->rss, "\n"; // http://www.planet-php.org/rss/
  351. }
  352. if(isset($links->atom)) {
  353. echo $links->atom, "\n"; // http://www.planet-php.org/atom/
  354. }
  355. ]]></programlisting>
  356. <para>
  357. Based on these links, you can then import from whichever source you
  358. wish in the usual manner.
  359. </para>
  360. </sect2>
  361. <sect2 id="zend.feed.reader.retrieve-info">
  362. <title>Retrieving Feed Information</title>
  363. <para>
  364. Retrieving information from a feed (we'll cover entries/items in the
  365. next section though they follow identical principals) uses a clearly
  366. defined <acronym>API</acronym> which is exactly the same regardless of whether the feed
  367. in question is <acronym>RSS</acronym>/<acronym>RDF</acronym>/Atom. The same goes for
  368. sub-versions of these standards and we've tested every single
  369. <acronym>RSS</acronym> and Atom version. While
  370. the underlying feed <acronym>XML</acronym> can differ substantially in terms of the
  371. tags and elements they present, they nonetheless are all trying to
  372. convey similar information and to reflect this all the differences
  373. and wrangling over alternative tags are handled internally by
  374. <classname>Zend_Feed_Reader</classname> presenting you with an
  375. identical interface for each. Ideally, you should not have to care
  376. whether a feed is <acronym>RSS</acronym> or Atom so long as you can extract the
  377. information you want.
  378. </para>
  379. <para>
  380. Of course, we don't live in an ideal world so there may be times the
  381. <acronym>API</acronym> just does not cover what you're looking for. To assist you,
  382. <classname>Zend_Feed_Reader</classname> offers a plugin system which
  383. allows you to write Extensions to expand the core <acronym>API</acronym> and cover any
  384. additional data you are trying to extract from feeds. If writing
  385. another Extension is too much trouble, you can simply grab the
  386. underlying <acronym>DOM</acronym> or XPath objects and do it by hand in your
  387. application. Of course, we really do encourage writing an Extension
  388. simply to make it more portable and reusable.
  389. </para>
  390. <para>
  391. Here's a summary of the Core <acronym>API</acronym> for Feeds. You should note it
  392. comprises not only the basic <acronym>RSS</acronym> and Atom standards, but also
  393. accounts for a number of included Extensions bundled with
  394. <classname>Zend_Feed_Reader</classname>. The naming of these
  395. Extension sourced methods remain fairly generic - all Extension
  396. methods operate at the same level as the Core <acronym>API</acronym> though we do allow
  397. you to retrieve any specific Extension object separately if
  398. required.
  399. </para>
  400. <table>
  401. <title>Feed Level API Methods</title>
  402. <tgroup cols="2">
  403. <tbody>
  404. <row>
  405. <entry><methodname>getId()</methodname></entry>
  406. <entry>Returns a unique ID associated with this feed</entry>
  407. </row>
  408. <row>
  409. <entry><methodname>getTitle()</methodname></entry>
  410. <entry>Returns the title of the feed</entry>
  411. </row>
  412. <row>
  413. <entry><methodname>getDescription()</methodname></entry>
  414. <entry>Returns the text description of the feed</entry>
  415. </row>
  416. <row>
  417. <entry><methodname>getLink()</methodname></entry>
  418. <entry>
  419. Returns a <acronym>URI</acronym> to the <acronym>HTML</acronym> website
  420. containing the same or
  421. similar information as this feed (i.e. if the feed is from a blog,
  422. it should provide the blog's <acronym>URI</acronym> where the
  423. <acronym>HTML</acronym> version of the entries can be read).
  424. </entry>
  425. </row>
  426. <row>
  427. <entry><methodname>getFeedLink()</methodname></entry>
  428. <entry>
  429. Returns the <acronym>URI</acronym> of this feed, which should be the
  430. same as the <acronym>URI</acronym> used to import the feed
  431. </entry>
  432. </row>
  433. <row>
  434. <entry><methodname>getAuthors()</methodname></entry>
  435. <entry>
  436. Returns an array of all authors associated with this feed
  437. including email address in the author string if available
  438. </entry>
  439. </row>
  440. <row>
  441. <entry><methodname>getAuthor(integer $index = 0)</methodname></entry>
  442. <entry>
  443. Returns either the first author known, or with the
  444. optional <varname>$index</varname> parameter any specific
  445. index on the array of Authors (returning null if an
  446. invalid index).
  447. </entry>
  448. </row>
  449. <row>
  450. <entry><methodname>getDateCreated()</methodname></entry>
  451. <entry>
  452. Returns the date on which this feed was created. Generally
  453. only applicable to Atom where it represents the date the resource
  454. described by an Atom 1.0 document was created.
  455. </entry>
  456. </row>
  457. <row>
  458. <entry><methodname>getDateModified()</methodname></entry>
  459. <entry>
  460. Returns the date on which this feed was last modified
  461. </entry>
  462. </row>
  463. <row>
  464. <entry><methodname>getLanguage()</methodname></entry>
  465. <entry>
  466. Returns the language of the feed (if defined) or simply the
  467. language noted in the <acronym>XML</acronym> document
  468. </entry>
  469. </row>
  470. <row>
  471. <entry><methodname>getGenerator()</methodname></entry>
  472. <entry>
  473. Returns the generator of the feed, e.g. the software which
  474. generated it. This may differ between <acronym>RSS</acronym> and Atom
  475. since Atom defines a different notation.
  476. </entry>
  477. </row>
  478. <row>
  479. <entry><methodname>getCopyright()</methodname></entry>
  480. <entry>
  481. Returns any copyright notice associated with the feed
  482. </entry>
  483. </row>
  484. </tbody>
  485. </tgroup>
  486. </table>
  487. <para>
  488. Given the variety of feeds in the wild, some of these methods will
  489. undoubtedly return <constant>NULL</constant> indicating the relevant information
  490. couldn't be located. Where possible, <classname>Zend_Feed_Reader</classname>
  491. will fall back on alternative elements during its search. For
  492. example, searching an <acronym>RSS</acronym> feed for a modification date is more
  493. complicated than it looks. <acronym>RSS</acronym> 2.0 feeds should include a
  494. <code>&lt;lastBuildDate&gt;</code> tag and/or a
  495. <code>&lt;pubDate&gt;</code> element. But what if it doesn't, maybe
  496. this is an <acronym>RSS</acronym> 1.0 feed? Perhaps it instead has an
  497. <code>&lt;atom:updated&gt;</code> element with identical information
  498. (Atom may be used to supplement <acronym>RSS</acronym>'s syntax)? Failing that, we
  499. could simply look at the entries, pick the most recent, and use its
  500. <code>&lt;pubDate&gt;</code> element. Assuming it exists... Many
  501. feeds also use Dublin Core 1.0/1.1 <code>&lt;dc:date&gt;</code>
  502. elements for feeds/entries. Or we could find Atom lurking again.
  503. </para>
  504. <para>
  505. The point is, <classname>Zend_Feed_Reader</classname> was designed
  506. to know this. When you ask for the modification date (or anything
  507. else), it will run off and search for all these alternatives until
  508. it either gives up and returns <constant>NULL</constant>, or finds an
  509. alternative that should have the right answer.
  510. </para>
  511. <para>
  512. In addition to the above methods, all Feed objects implement methods
  513. for retrieving the <acronym>DOM</acronym> and XPath objects for the current feeds as
  514. described earlier. Feed objects also implement the <acronym>SPL</acronym> Iterator and
  515. Countable interfaces. The extended <acronym>API</acronym> is summarised below.
  516. </para>
  517. <table>
  518. <title>Extended Feed Level API Methods</title>
  519. <tgroup cols="2">
  520. <tbody>
  521. <row>
  522. <entry><methodname>getDomDocument()</methodname></entry>
  523. <entry>
  524. Returns the parent
  525. <classname>DOMDocument</classname> object for the
  526. entire source <acronym>XML</acronym> document
  527. </entry>
  528. </row>
  529. <row>
  530. <entry><methodname>getElement()</methodname></entry>
  531. <entry>
  532. Returns the current feed level
  533. <classname>DOMElement</classname> object
  534. </entry>
  535. </row>
  536. <row>
  537. <entry><methodname>saveXml()</methodname></entry>
  538. <entry>
  539. Returns a string containing an <acronym>XML</acronym> document of the
  540. entire feed element (this is not the original
  541. document but a rebuilt version)
  542. </entry>
  543. </row>
  544. <row>
  545. <entry><methodname>getXpath()</methodname></entry>
  546. <entry>
  547. Returns the <classname>DOMXPath</classname> object
  548. used internally to run queries on the
  549. <classname>DOMDocument</classname> object (this
  550. includes core and Extension namespaces
  551. pre-registered)
  552. </entry>
  553. </row>
  554. <row>
  555. <entry><methodname>getXpathPrefix()</methodname></entry>
  556. <entry>
  557. Returns the valid <acronym>DOM</acronym> path prefix prepended
  558. to all XPath queries matching the feed being queried
  559. </entry>
  560. </row>
  561. <row>
  562. <entry><methodname>getEncoding()</methodname></entry>
  563. <entry>
  564. Returns the encoding of the source <acronym>XML</acronym> document
  565. (note: this cannot account for errors such as the
  566. server sending documents in a different encoding)
  567. </entry>
  568. </row>
  569. <row>
  570. <entry><methodname>count()</methodname></entry>
  571. <entry>
  572. Returns a count of the entries or items this feed contains
  573. (implements <acronym>SPL</acronym> <classname>Countable</classname>
  574. interface)
  575. </entry>
  576. </row>
  577. <row>
  578. <entry><methodname>current()</methodname></entry>
  579. <entry>
  580. Returns either the current entry (using the current index
  581. from <methodname>key()</methodname>)
  582. </entry>
  583. </row>
  584. <row>
  585. <entry><methodname>key()</methodname></entry>
  586. <entry>Returns the current entry index</entry>
  587. </row>
  588. <row>
  589. <entry><methodname>next()</methodname></entry>
  590. <entry>Increments the entry index value by one</entry>
  591. </row>
  592. <row>
  593. <entry><methodname>rewind()</methodname></entry>
  594. <entry>Resets the entry index to 0</entry>
  595. </row>
  596. <row>
  597. <entry><methodname>valid()</methodname></entry>
  598. <entry>
  599. Checks that the current entry index is valid, i.e.
  600. it does fall below 0 and does not exceed the number
  601. of entries existing.
  602. </entry>
  603. </row>
  604. <row>
  605. <entry><methodname>getExtensions()</methodname></entry>
  606. <entry>
  607. Returns an array of non-Core Extension objects loaded for
  608. the current feed (note: both feed-level and entry-level Extensions
  609. exist, and only feed-level Extensions are returned here).
  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 non-Core 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).
  825. </entry>
  826. </row>
  827. <row>
  828. <entry><methodname>getType()</methodname></entry>
  829. <entry>
  830. Returns a static class constant (e.g.
  831. <constant>Zend_Feed_Reader::TYPE_ATOM_03</constant>,
  832. i.e. Atom 0.3) indicating exactly what kind
  833. of feed is being consumed.
  834. </entry>
  835. </row>
  836. </tbody>
  837. </tgroup>
  838. </table>
  839. </sect2>
  840. <sect2 id="zend.feed.reader.extending">
  841. <title>Extending Feed and Entry APIs</title>
  842. <para>
  843. Extending <classname>Zend_Feed_Reader</classname> allows you to add
  844. methods at both the feed and entry level which cover the retrieval
  845. of information not already supported by
  846. <classname>Zend_Feed_Reader</classname>. Given the number of
  847. <acronym>RSS</acronym> and
  848. Atom extensions that exist, this is a good thing since
  849. <classname>Zend_Feed_Reader</classname> couldn't possibly add
  850. everything.
  851. </para>
  852. <para>
  853. There are two types of Extensions possible, those which retrieve
  854. information from elements which are immediate children of the root
  855. element (e.g. <code>&lt;channel&gt;</code> for <acronym>RSS</acronym> or
  856. <code>&lt;feed&gt;</code> for Atom) and those who retrieve
  857. information from child elements of an entry (e.g.
  858. <code>&lt;item&gt;</code> for <acronym>RSS</acronym> or <code>&lt;entry&gt;</code> for
  859. Atom). On the filesystem these are grouped as classes within
  860. a namespace based on the extension standard's name. For
  861. example, internally we have
  862. <classname>Zend_Feed_Reader_Extension_DublinCore_Feed</classname>
  863. and <classname>Zend_Feed_Reader_Extension_DublinCore_Entry</classname>
  864. classes which are two Extensions implementing Dublin Core
  865. 1.0/1.1 support.
  866. </para>
  867. <para>
  868. Extensions are loaded into <classname>Zend_Feed_Reader</classname>
  869. using <classname>Zend_Loader_PluginLoader</classname>, so their operation
  870. will be familiar from other Zend Framework components.
  871. <classname>Zend_Feed_Reader</classname> already bundles a number of
  872. these Extensions, however those which are not used internally and
  873. registered by default (so called Core Extensions) must be registered
  874. to <classname>Zend_Feed_Reader</classname> before they are used. The
  875. bundled Extensions include:
  876. </para>
  877. <table>
  878. <title>Core Extensions (pre-registered)</title>
  879. <tgroup cols="2">
  880. <tbody>
  881. <row>
  882. <entry>DublinCore (Feed and Entry)</entry>
  883. <entry>Implements support for both Dublin Core 1.0 and 1.1</entry>
  884. </row>
  885. <row>
  886. <entry>Content (Entry only)</entry>
  887. <entry>Implements support for Content 1.0</entry>
  888. </row>
  889. <row>
  890. <entry>Atom (Feed and Entry)</entry>
  891. <entry>Implements support for Atom 0.3 and Atom 1.0</entry>
  892. </row>
  893. <row>
  894. <entry>Slash</entry>
  895. <entry>todo</entry>
  896. </row>
  897. <row>
  898. <entry>WellFormedWeb</entry>
  899. <entry>todo</entry>
  900. </row>
  901. </tbody>
  902. </tgroup>
  903. </table>
  904. <para>
  905. The Core Extensions are somewhat special since they are extremely
  906. common and multi-faceted. For example, we have a Core Extension for Atom.
  907. Atom is implemented as an Extension (not just a base class) because it
  908. doubles as a valid <acronym>RSS</acronym> Extension - you can insert
  909. Atom elements into <acronym>RSS</acronym> feeds. I've even seen
  910. <acronym>RDF</acronym> feeds which use a lot of Atom in place of more
  911. common Extensions like Dublin Core.
  912. </para>
  913. <table>
  914. <title>Non-Core Extensions (must register manually)</title>
  915. <tgroup cols="1">
  916. <tbody>
  917. <row>
  918. <entry>At present, no non-Core Extensions are distributed.</entry>
  919. </row>
  920. </tbody>
  921. </tgroup>
  922. </table>
  923. <para>
  924. The additional non-Core Extensions are offered but not registered to
  925. <classname>Zend_Feed_Reader</classname> by default. If you want to
  926. use them, you'll need to tell
  927. <classname>Zend_Feed_Reader</classname> to load them in advance of
  928. importing a feed.
  929. </para>
  930. <para>
  931. Registering an Extension with
  932. <classname>Zend_Feed_Reader</classname>, so it is loaded and its <acronym>API</acronym>
  933. is available to Feed and Entry objects, is a simple affair using the
  934. <classname>Zend_Loader_PluginLoader</classname>. Here we register
  935. the optional Slash Extension, and discover that it can be directly
  936. called from the Entry level <acronym>API</acronym> without any effort. Note that
  937. Extension names are case sensitive and use camel casing for multiple
  938. terms.
  939. </para>
  940. <programlisting language="php"><![CDATA[
  941. Zend_Feed_Reader::registerExtension('Slash');
  942. $feed = Zend_Feed_Reader::import('http://rss.slashdot.org/Slashdot/slashdot');
  943. $commentsForLastEntry = $feed->current()->getCommentCount();
  944. ]]></programlisting>
  945. <para>
  946. In the simple example above, we checked how many comments were made
  947. on an entry using the <methodname>getCommentCount()</methodname>
  948. method. Since it's not part of
  949. <classname>Zend_Feed_Reader</classname>'s core <acronym>API</acronym>, it could only be
  950. a method supported by the newly registered Slash Extension (indeed
  951. Slash was invented at Slashdot as an <acronym>RDF</acronym> Site Summary module and is
  952. now fairly popular).
  953. </para>
  954. <sect3 id="zend.feed.reader.extending.feed">
  955. <title>Writing Zend_Feed_Reader Extensions</title>
  956. <para>
  957. Inevitably, there will be times when the
  958. <classname>Zend_Feed_Reader</classname> <acronym>API</acronym> is just not capable
  959. of getting something you need from a feed or entry. You can use
  960. the underlying source objects, like
  961. <classname>DOMDocument</classname>, to get these by hand however
  962. there is a more reusable method available by writing Extensions
  963. supporting these new queries.
  964. </para>
  965. <para>
  966. As an example, let's take the case of a purely fictitious
  967. corporation named Jungle Books. Jungle Books have been
  968. publishing a lot of reviews on books they sell (from external
  969. sources and customers), which are distributed as an <acronym>RSS</acronym> 2.0
  970. feed. Their marketing department realises that web applications
  971. using this feed cannot currently figure out exactly what book is
  972. being reviewed. To make life easier for everyone, they determine
  973. that the geek department needs to extend <acronym>RSS</acronym> 2.0 to include a
  974. new element per entry supplying the <acronym>ISBN</acronym>-10 or
  975. <acronym>ISBN</acronym>-13 number of
  976. the publication the entry concerns. They define the new
  977. <code>&lt;isbn&gt;</code> element quite simply with a standard
  978. name and namespace <acronym>URI</acronym>:
  979. </para>
  980. <programlisting language="php"><![CDATA[
  981. JungleBooks 1.0:
  982. http://example.com/junglebooks/rss/module/1.0/
  983. ]]></programlisting>
  984. <para>
  985. A snippet of <acronym>RSS</acronym> containing this extension in practice could be
  986. something similar to:
  987. </para>
  988. <programlisting language="php"><![CDATA[
  989. <?xml version="1.0" encoding="utf-8" ?>
  990. <rss version="2.0"
  991. xmlns:content="http://purl.org/rss/1.0/modules/content/"
  992. xmlns:jungle="http://example.com/junglebooks/rss/module/1.0/">
  993. <channel>
  994. <title>Jungle Books Customer Reviews</title>
  995. <link>http://example.com/junglebooks</link>
  996. <description>Many book reviews!</description>
  997. <pubDate>Fri, 26 Jun 2009 19:15:10 GMT</pubDate>
  998. <jungle:dayPopular>http://example.com/junglebooks/book/938</jungle:dayPopular>
  999. <item>
  1000. <title>Review Of Flatland: A Romance of Many Dimensions</title>
  1001. <link>http://example.com/junglebooks/review/987</link>
  1002. <author>Confused Physics Student</author>
  1003. <content:encoded>
  1004. A romantic square?!
  1005. </content:encoded>
  1006. <pubDate>Thu, 25 Jun 2009 20:03:28 -0700</pubDate>
  1007. <jungle:isbn>048627263X</jungle:isbn>
  1008. </item>
  1009. </channel>
  1010. </rss>
  1011. ]]></programlisting>
  1012. <para>
  1013. Implementing this new <acronym>ISBN</acronym> element as a simple entry level
  1014. extension would require the following class (using your own class
  1015. namespace outside of Zend).
  1016. </para>
  1017. <programlisting language="php"><![CDATA[
  1018. class My_FeedReader_Extension_JungleBooks_Entry
  1019. extends Zend_Feed_Reader_Extension_EntryAbstract
  1020. {
  1021. public function getIsbn()
  1022. {
  1023. if (isset($this->_data['isbn'])) {
  1024. return $this->_data['isbn'];
  1025. }
  1026. $isbn = $this->_xpath->evaluate(
  1027. 'string(' . $this->getXpathPrefix() . '/jungle:isbn)'
  1028. );
  1029. if (!$isbn) {
  1030. $isbn = null;
  1031. }
  1032. $this->_data['isbn'] = $title;
  1033. return $this->_data['isbn'];
  1034. }
  1035. protected function _registerNamespaces()
  1036. {
  1037. $this->_xpath->registerNamespace(
  1038. 'jungle', 'http://example.com/junglebooks/rss/module/1.0/'
  1039. );
  1040. }
  1041. }
  1042. ]]></programlisting>
  1043. <para>
  1044. This extension is easy enough to follow. It creates a new method
  1045. <methodname>getIsbn()</methodname> which runs an XPath query on
  1046. the current entry to extract the <acronym>ISBN</acronym> number enclosed by the
  1047. <code>&lt;jungle:isbn&gt;</code> element. It can optionally
  1048. store this to the internal non-persistent cache (no need to keep
  1049. querying the <acronym>DOM</acronym> if it's called again on the same entry). The
  1050. value is returned to the caller. At the end we have a protected
  1051. method (it's abstract so it must exist) which registers the
  1052. Jungle Books namespace for their custom <acronym>RSS</acronym> module. While we
  1053. call this an <acronym>RSS</acronym> module, there's nothing to prevent the same
  1054. element being used in Atom feeds - and all Extensions which use
  1055. the prefix provided by <methodname>getXpathPrefix()</methodname>
  1056. are actually neutral and work on <acronym>RSS</acronym> or Atom feeds with no
  1057. extra code.
  1058. </para>
  1059. <para>
  1060. Since this Extension is stored outside of Zend Framework, you'll
  1061. need to register the path prefix for your Extensions so
  1062. <classname>Zend_Loader_PluginLoader</classname> can find them.
  1063. After that, it's merely a matter of registering the Extension,
  1064. if it's not already loaded, and using it in practice.
  1065. </para>
  1066. <programlisting language="php"><![CDATA[
  1067. if(!Zend_Feed_Reader::isRegistered('JungleBooks')) {
  1068. Zend_Feed_Reader::addPrefixPath(
  1069. '/path/to/My/FeedReader/Extension', 'My_FeedReader_Extension'
  1070. );
  1071. Zend_Feed_Reader::registerExtension('JungleBooks');
  1072. }
  1073. $feed = Zend_Feed_Reader::import('http://example.com/junglebooks/rss');
  1074. // ISBN for whatever book the first entry in the feed was concerned with
  1075. $firstIsbn = $feed->current()->getIsbn();
  1076. ]]></programlisting>
  1077. <para>
  1078. Writing a feed level Extension is not much different. The
  1079. example feed from earlier included an unmentioned
  1080. <code>&lt;jungle:dayPopular&gt;</code> element which Jungle
  1081. Books have added to their standard to include a link to the
  1082. day's most popular book (in terms of visitor traffic). Here's
  1083. an Extension which adds a
  1084. <methodname>getDaysPopularBookLink()</methodname> method to the
  1085. feel level <acronym>API</acronym>.
  1086. </para>
  1087. <programlisting language="php"><![CDATA[
  1088. class My_FeedReader_Extension_JungleBooks_Feed
  1089. extends Zend_Feed_Reader_Extension_FeedAbstract
  1090. {
  1091. public function getDaysPopularBookLink()
  1092. {
  1093. if (isset($this->_data['dayPopular'])) {
  1094. return $this->_data['dayPopular'];
  1095. }
  1096. $dayPopular = $this->_xpath->evaluate(
  1097. 'string(' . $this->getXpathPrefix() . '/jungle:dayPopular)'
  1098. );
  1099. if (!$dayPopular) {
  1100. $dayPopular = null;
  1101. }
  1102. $this->_data['dayPopular'] = $dayPopular;
  1103. return $this->_data['dayPopular'];
  1104. }
  1105. protected function _registerNamespaces()
  1106. {
  1107. $this->_xpath->registerNamespace(
  1108. 'jungle', 'http://example.com/junglebooks/rss/module/1.0/'
  1109. );
  1110. }
  1111. }]]></programlisting>
  1112. <para>
  1113. Let's repeat the last example using a custom Extension to show the
  1114. method being used.
  1115. </para>
  1116. <programlisting language="php"><![CDATA[
  1117. if(!Zend_Feed_Reader::isRegistered('JungleBooks')) {
  1118. Zend_Feed_Reader::addPrefixPath(
  1119. '/path/to/My/FeedReader/Extension', 'My_FeedReader_Extension'
  1120. );
  1121. Zend_Feed_Reader::registerExtension('JungleBooks');
  1122. }
  1123. $feed = Zend_Feed_Reader::import('http://example.com/junglebooks/rss');
  1124. // URI to the information page of the day's most popular book with visitors
  1125. $daysPopularBookLink = $feed->getDaysPopularBookLink();
  1126. // ISBN for whatever book the first entry in the feed was concerned with
  1127. $firstIsbn = $feed->current()->getIsbn();
  1128. ]]></programlisting>
  1129. <para>
  1130. Going through these examples, you'll note that we don't register
  1131. feed and entry Extensions separately. Extensions within the same
  1132. standard may or may not include both a feed and entry class, so
  1133. <classname>Zend_Feed_Reader</classname> only requires you to
  1134. register the overall parent name, e.g. JungleBooks, DublinCore,
  1135. Slash. Internally, it can check at what level Extensions exist
  1136. and load them up if found. In our case, we have a full set of
  1137. Extensions now: <classname>JungleBooks_Feed</classname> and
  1138. <classname>JungleBooks_Entry</classname>.
  1139. </para>
  1140. </sect3>
  1141. </sect2>
  1142. </sect1>