Zend_Feed_Reader.xml 74 KB

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