PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/rss_parser/pi.rss_parser.php

https://github.com/cswebgrl/RSS-Parser
PHP | 298 lines | 182 code | 59 blank | 57 comment | 8 complexity | 99a9e7fe236b0a5b5ae33c13a1a0b2df MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /*
  3. Copyright (C) 2012 EllisLab, Inc.
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. ELLISLAB, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. Except as contained in this notice, the name of EllisLab, Inc. shall not be
  19. used in advertising or otherwise to promote the sale, use or other dealings
  20. in this Software without prior written authorization from EllisLab, Inc.
  21. */
  22. $plugin_info = array(
  23. 'pi_name' => 'RSS Parser',
  24. 'pi_version' => '1.0',
  25. 'pi_author' => 'EllisLab Development Team',
  26. 'pi_author_url' => 'http://ellislab.com/',
  27. 'pi_description' => 'Retrieves and Parses RSS/Atom Feeds',
  28. 'pi_usage' => Rss_parser::usage()
  29. );
  30. Class Rss_parser {
  31. private $cache_name = 'rss_cache';
  32. public function __construct()
  33. {
  34. $this->EE =& get_instance();
  35. // Fetch Parameters and set defaults
  36. $url = $this->EE->TMPL->fetch_param('url');
  37. $limit = (int) $this->EE->TMPL->fetch_param('limit', 10);
  38. $offset = (int) $this->EE->TMPL->fetch_param('offset', 0);
  39. $refresh = (int) $this->EE->TMPL->fetch_param('refresh', 180);
  40. // Bring in SimplePie
  41. require_once(PATH_THIRD.'rss_parser/libraries/SimplePieAutoloader.php');
  42. $feed = new SimplePie();
  43. $feed->set_feed_url($url);
  44. // Establish the cache
  45. $this->_check_cache();
  46. $feed->set_cache_location(APPPATH.'cache/'.$this->cache_name.'/');
  47. $feed->set_cache_duration($refresh * 60); // Get parameter to seconds
  48. // Check to see if the feed was initialized, if so, deal with the type
  49. $success = $feed->init();
  50. $feed->handle_content_type();
  51. // Parse the variables
  52. if ($success)
  53. {
  54. // Make sure there's at least one item
  55. if ($feed->get_item_quantity() <= 0)
  56. {
  57. $this->return_data = $this->EE->TMPL->no_results();
  58. }
  59. $content = array(
  60. 'feed_items' => $this->_map_feed_items($feed, $limit, $offset),
  61. // Feed Information
  62. 'feed_title' => $feed->get_title(),
  63. 'feed_link' => $feed->get_link(),
  64. 'feed_copyright' => $feed->get_copyright(),
  65. 'feed_description' => $feed->get_description(),
  66. 'feed_language' => $feed->get_language(),
  67. // Feed Logo Information
  68. 'logo_url' => $feed->get_image_url(),
  69. 'logo_title' => $feed->get_image_title(),
  70. 'logo_link' => $feed->get_image_link(),
  71. 'logo_width' => $feed->get_image_width(),
  72. 'logo_height' => $feed->get_image_height()
  73. );
  74. $this->return_data = $this->EE->TMPL->parse_variables(
  75. $this->EE->TMPL->tagdata,
  76. array($content)
  77. );
  78. }
  79. // Feed couldn't be fetched, put the error into the Template log
  80. else
  81. {
  82. $this->EE->TMPL->log_item("RSS Parser Error: ".$feed->error());
  83. $this->return_data = $this->EE->TMPL->no_results();
  84. }
  85. }
  86. // -------------------------------------------------------------------------
  87. /**
  88. * Map Feed Items to array
  89. * @param SimplePie $feed SimplePie feed object
  90. * @param int $limit Number of items to return
  91. * @param int $offset Number of items to skip
  92. * @return mapped associative array containing all feed items
  93. */
  94. private function _map_feed_items($feed, $limit, $offset)
  95. {
  96. $items = array();
  97. foreach ($feed->get_items($offset, $limit) as $index => $item)
  98. {
  99. // Get Categories
  100. $categories = array();
  101. $item_categories = $item->get_categories();
  102. if ( ! empty($item_categories))
  103. {
  104. foreach ($item_categories as $category)
  105. {
  106. $categories[] = array(
  107. 'category_name' => $category->get_term()
  108. );
  109. }
  110. }
  111. // Get Authors
  112. $authors = array();
  113. $item_authors = $item->get_authors();
  114. if ( ! empty($item_authors))
  115. {
  116. foreach ($item_authors as $author)
  117. {
  118. $authors[] = array(
  119. 'author_email' => $author->get_email(),
  120. 'author_link' => $author->get_link(),
  121. 'author_name' => $author->get_name()
  122. );
  123. }
  124. }
  125. $items[] = array(
  126. 'item_title' => $item->get_title(),
  127. 'item_link' => $item->get_permalink(),
  128. 'item_date' => $item->get_date('U'),
  129. 'item_content' => $item->get_content(),
  130. 'item_description' => $item->get_description(),
  131. 'item_categories' => $categories,
  132. 'item_authors' => $authors
  133. );
  134. }
  135. return $items;
  136. }
  137. // -------------------------------------------------------------------------
  138. /**
  139. * Check to make sure the cache exists and create it otherwise
  140. * @return void
  141. */
  142. private function _check_cache()
  143. {
  144. // Make sure the cache directory exists and is writeable
  145. if ( ! @is_dir(APPPATH.'cache/'.$this->cache_name))
  146. {
  147. if ( ! @mkdir(APPPATH.'cache/'.$this->cache_name, DIR_WRITE_MODE))
  148. {
  149. $this->EE->TMPL->log_item("RSS Parser Error: Cache directory unwritable.");
  150. return $this->EE->TMPL->no_results();
  151. }
  152. }
  153. }
  154. // -------------------------------------------------------------------------
  155. /**
  156. * Plugin Usage
  157. *
  158. * @return void
  159. */
  160. public function usage()
  161. {
  162. ob_start();
  163. ?>
  164. RSS Parser
  165. ===========================
  166. There is only one tag for the RSS Parser:
  167. {exp:rss_parser url="http://expressionengine.com/feeds/rss/full/" offset="5" limit="10" refresh="720"}
  168. Parameters
  169. ===========================
  170. The tag has three parameters:
  171. - url - The URL of the RSS or Atom feed.
  172. - limit - Number of items to display from feed.
  173. - offset - Skip a certain number of items in the display of the feed.
  174. - refresh - How often to refresh the cache file in minutes. The plugin default is to refresh the cached file every three hours.
  175. Single Variables
  176. ===========================
  177. - feed_title
  178. - feed_link
  179. - feed_copyright
  180. - feed_description
  181. - feed_language
  182. Both RSS 2.0 and Atom 1.0 feeds can have a "feed logo". The following variables
  183. can be used to display the logo:
  184. - logo_title
  185. - logo_url
  186. - logo_link
  187. - logo_width
  188. - logo_height
  189. Pair Variables
  190. ===========================
  191. There are three pair variables available: {feed_items}, {item_categories}, and
  192. {item_authors}. Both {item_categories} and {item_authors}, are only available
  193. within {feed_items}.
  194. {feed_items}
  195. ---------------------------
  196. The {feed_items} variable contains all of the items found within the feed:
  197. - item_title
  198. - item_link
  199. - item_date: uses standard ExpressionEngine date formatting (e.g. {date format="%F %d %Y"})
  200. - item_description
  201. - item_content
  202. {item_authors}
  203. ---------------------------
  204. The {item_authors} variable contains information about all of the authors of a
  205. particular item. Each author has three single variables associated with it:
  206. - author_email
  207. - author_link
  208. - author_name
  209. {item_categories}
  210. ---------------------------
  211. The {item_categories} variable contains all of the categories that a feed item has
  212. been assigned. Each category has one useful variable:
  213. - category_name
  214. Example
  215. ===========================
  216. {exp:rss_parser url="http://expressionengine.com/feeds/rss/full/" limit="10" refresh="720"}
  217. <ul>
  218. {feed_items}
  219. <li><a href="{item_link}">{item_title}</a></li>
  220. {/feed_items}
  221. </ul>
  222. {/exp:rss_parser}
  223. Changelog
  224. ===========================
  225. Version 1.0
  226. ---------------------------
  227. - Initial release and (mostly) feature parity with MagPie plugin
  228. <?php
  229. $buffer = ob_get_contents();
  230. ob_end_clean();
  231. return $buffer;
  232. }
  233. } // END RSS Parser class
  234. /* End of file pi.rss_parser.php */
  235. /* Location: ./system/expressionengine/third_party/rss_parser/pi.rss_parser.php */