Zend_Feed_Reader.xml 61 KB

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