/[drupal]/contributions/modules/simplepie/simplepie.module
ViewVC logotype

Contents of /contributions/modules/simplepie/simplepie.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.14 - (show annotations) (download) (as text)
Sat Sep 19 20:57:50 2009 UTC (2 months ago) by mustafau
Branch: MAIN
CVS Tags: HEAD
Changes since 1.13: +3 -3 lines
File MIME type: text/x-php
Move settings page under "config/services"
1 <?php
2 // $Id: simplepie.module,v 1.13 2009/08/31 22:10:47 mustafau Exp $
3
4 /**
5 * @file
6 *
7 * @see http://simplepie.org/
8 */
9
10 /**
11 * Implement hook_help().
12 */
13 function simplepie_help($path, $arg) {
14 switch ($path) {
15 case 'admin/help#simplepie':
16 return '<p>'. t('Ensures that the SimplePie library is installed. Makes SimplePie available to other modules.') .'</p>';
17 case 'admin/config/services/simplepie':
18 return '<p>'. t('Specify a cache location.') .'</p>';
19 }
20 }
21
22 /**
23 * Implement hook_menu().
24 */
25 function simplepie_menu() {
26 $items['admin/config/services/simplepie'] = array(
27 'title' => 'SimplePie Core settings',
28 'description' => 'Control how SimplePie Core works.',
29 'page callback' => 'drupal_get_form',
30 'page arguments' => array('simplepie_admin_settings'),
31 'access arguments' => array('administer site configuration'),
32 'file' => 'simplepie.admin.inc',
33 );
34 return $items;
35 }
36
37 /**
38 * Returns the path of the SimplePie library.
39 */
40 function simplepie_get_library_path() {
41 return drupal_get_path('module', 'simplepie') .'/lib/simplepie.inc';
42 }
43
44 /**
45 * Enter description here...
46 *
47 * @param $location
48 */
49 function simplepie_set_cache_location($location) {
50 variable_set('simplepie_cache_location', $location);
51 }
52
53 function simplepie_get_cache_location() {
54 return file_directory_path() .'/'. variable_get('simplepie_cache_location', 'cache/simplepie');
55 }
56
57 function simplepie_require($once = TRUE) {
58 if (defined('SIMPLEPIE_VERSION')) {
59 return;
60 }
61
62 $library = simplepie_get_library_path();
63
64 if ($once) {
65 require_once './'. $library;
66 }
67 else {
68 require './'. $library;
69 }
70 }
71
72 /**
73 * Enter description here...
74 *
75 * @param $feed_url
76 * @param $cache_location
77 * Relative to files directory.
78 * @param $cache_duration
79 * @return
80 * Instance of SimplePie.
81 */
82 function simplepie_get($feed_url, $cache_location = 'cache/simplepie', $cache_duration = 1800) {
83 static $simplepie = NULL;
84
85 simplepie_require();
86
87 $simplepie = new SimplePie();
88
89 if ($cache_location) {
90 $simplepie->set_cache_location(file_directory_path() .'/'. $cache_location);
91 }
92 else {
93 $simplepie->set_cache_location(simplepie_get_cache_location());
94 }
95
96 if ($cache_duration) {
97 $simplepie->set_cache_duration($cache_duration);
98 }
99
100 $simplepie->set_stupidly_fast(TRUE);
101 $simplepie->set_feed_url($feed_url);
102 $simplepie->init();
103
104 return $simplepie;
105 }
106
107 /**
108 * Implement hook_aggregator_parse_info().
109 */
110 function simplepie_aggregator_parse_info() {
111 return array(
112 'title' => t('SimplePie parser'),
113 'description' => t('Parses RSS, Atom and RDF feeds.'),
114 );
115 }
116
117 /**
118 * Implement hook_aggregator_parse().
119 */
120 function simplepie_aggregator_parse($feed) {
121 simplepie_require();
122
123 $simplepie = new SimplePie();
124 $simplepie->set_stupidly_fast(TRUE);
125 $simplepie->set_raw_data($feed->source_string);
126
127 // Parse the feed data.
128 if ($simplepie->init()) {
129 $items = $simplepie->get_items();
130
131 foreach ($items as $simplepie_item) {
132 // Resolve the item's title.
133 $item['title'] = decode_entities($simplepie_item->get_title());
134
135 // Resolve the items link.
136 $item['link'] = $simplepie_item->get_permalink();
137
138 // Resolve the items GUID.
139 $item['guid'] = $simplepie_item->get_id();
140
141 // Resolve the items content.
142 $item['description'] = decode_entities($simplepie_item->get_content());
143
144 // Resolve the items publication date.
145 $item['timestamp'] = strtotime($simplepie_item->get_date());
146
147 // Resolve the items author.
148 $item['author'] = $simplepie_item->get_author();
149
150 // Store on $feed object. This is where processors will look for parsed items.
151 $feed->items[] = $item;
152 }
153
154 $modified = empty($feed->http_headers['Last-Modified']) ? 0 : strtotime($feed->http_headers['Last-Modified']);
155 $etag = empty($feed->http_headers['ETag']) ? '' : $feed->http_headers['ETag'];
156
157 // Update the feed data.
158 db_merge('aggregator_feed')
159 ->key(array('fid' => $feed->fid))
160 ->fields(array(
161 'url' => $feed->url,
162 'checked' => REQUEST_TIME,
163 'link' => ($simplepie->get_link() != NULL) ? $simplepie->get_link() : '',
164 'description' => ($simplepie->get_description() != NULL) ? $simplepie->get_description() : '',
165 'hash' => md5($feed->source_string),
166 'etag' => $etag,
167 'modified' => $modified,
168 ))
169 ->execute();
170
171 // Clear the cache.
172 cache_clear_all();
173
174 if (isset($feed->redirected)) {
175 watchdog('simplepie', 'Updated URL for feed %title to %url.', array('%title' => $feed->title, '%url' => $feed->url));
176 }
177
178 watchdog('simplepie', 'There is new syndicated content from %site.', array('%site' => $feed->title));
179 drupal_set_message(t('There is new syndicated content from %site.', array('%site' => $feed->title)));
180 }
181 }

  ViewVC Help
Powered by ViewVC 1.1.2