1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Definition of FeedsPlugin class.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Base class for a fetcher, parser or processor result.
|
10
|
*/
|
11
|
class FeedsResult {}
|
12
|
|
13
|
/**
|
14
|
* Implement source interface for all plugins.
|
15
|
*
|
16
|
* Note how this class does not attempt to store source information locally.
|
17
|
* Doing this would break the model where source information is represented by
|
18
|
* an object that is being passed into a Feed object and its plugins.
|
19
|
*/
|
20
|
abstract class FeedsPlugin extends FeedsConfigurable implements FeedsSourceInterface {
|
21
|
|
22
|
/**
|
23
|
* The plugin definition.
|
24
|
*
|
25
|
* @var array
|
26
|
*/
|
27
|
protected $pluginDefinition;
|
28
|
|
29
|
/**
|
30
|
* Constructs a FeedsPlugin object.
|
31
|
*
|
32
|
* A copy of FeedsConfigurable::__construct() that doesn't call
|
33
|
* configDefaults() so that we avoid circular dependencies.
|
34
|
*
|
35
|
* @param string $id
|
36
|
* The importer id.
|
37
|
*/
|
38
|
protected function __construct($id) {
|
39
|
$this->id = $id;
|
40
|
$this->export_type = FEEDS_EXPORT_NONE;
|
41
|
$this->disabled = FALSE;
|
42
|
}
|
43
|
|
44
|
/**
|
45
|
* Instantiates a FeedsPlugin object.
|
46
|
*
|
47
|
* Don't use directly, use feeds_plugin() instead.
|
48
|
*
|
49
|
* @see feeds_plugin()
|
50
|
*/
|
51
|
public static function instance($class, $id, array $plugin_definition = array()) {
|
52
|
if (!strlen($id)) {
|
53
|
throw new InvalidArgumentException(t('Empty configuration identifier.'));
|
54
|
}
|
55
|
|
56
|
static $instances = array();
|
57
|
|
58
|
if (!isset($instances[$class][$id])) {
|
59
|
$instance = new $class($id);
|
60
|
|
61
|
// The ordering here is important. The plugin definition should be usable
|
62
|
// in getConfig().
|
63
|
$instance->setPluginDefinition($plugin_definition);
|
64
|
$instance->setConfig($instance->configDefaults());
|
65
|
$instances[$class][$id] = $instance;
|
66
|
}
|
67
|
|
68
|
return $instances[$class][$id];
|
69
|
}
|
70
|
|
71
|
/**
|
72
|
* Returns the type of plugin.
|
73
|
*
|
74
|
* @return string
|
75
|
* One of either 'fetcher', 'parser', or 'processor'.
|
76
|
*/
|
77
|
abstract public function pluginType();
|
78
|
|
79
|
/**
|
80
|
* Returns the plugin definition.
|
81
|
*
|
82
|
* @return array
|
83
|
* The plugin definition array.
|
84
|
*
|
85
|
* @see ctools_get_plugins()
|
86
|
*/
|
87
|
public function pluginDefinition() {
|
88
|
return $this->pluginDefinition;
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
* Sets the plugin definition.
|
93
|
*
|
94
|
* This is protected since we're only using it in FeedsPlugin::instance().
|
95
|
*
|
96
|
* @param array $plugin_definition
|
97
|
* The plugin definition.
|
98
|
*/
|
99
|
protected function setPluginDefinition(array $plugin_definition) {
|
100
|
$this->pluginDefinition = $plugin_definition;
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Save changes to the configuration of this object.
|
105
|
* Delegate saving to parent (= Feed) which will collect
|
106
|
* information from this object by way of getConfig() and store it.
|
107
|
*/
|
108
|
public function save() {
|
109
|
feeds_importer($this->id)->save();
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* Returns TRUE if $this->sourceForm() returns a form.
|
114
|
*/
|
115
|
public function hasSourceConfig() {
|
116
|
$form = $this->sourceForm(array());
|
117
|
return !empty($form);
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* Implements FeedsSourceInterface::sourceDefaults().
|
122
|
*/
|
123
|
public function sourceDefaults() {
|
124
|
$values = array_flip(array_keys($this->sourceForm(array())));
|
125
|
foreach ($values as $k => $v) {
|
126
|
$values[$k] = '';
|
127
|
}
|
128
|
return $values;
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* Callback methods, exposes source form.
|
133
|
*/
|
134
|
public function sourceForm($source_config) {
|
135
|
return array();
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* Validation handler for sourceForm.
|
140
|
*/
|
141
|
public function sourceFormValidate(&$source_config) {}
|
142
|
|
143
|
/**
|
144
|
* A source is being saved.
|
145
|
*/
|
146
|
public function sourceSave(FeedsSource $source) {}
|
147
|
|
148
|
/**
|
149
|
* A source is being deleted.
|
150
|
*/
|
151
|
public function sourceDelete(FeedsSource $source) {}
|
152
|
|
153
|
/**
|
154
|
* Loads on-behalf implementations from mappers/ directory.
|
155
|
*
|
156
|
* FeedsProcessor::map() does not load from mappers/ as only node and user
|
157
|
* processor ship with on-behalf implementations.
|
158
|
*
|
159
|
* @see FeedsNodeProcessor::map()
|
160
|
* @see FeedsUserProcessor::map()
|
161
|
*
|
162
|
* @todo: Use CTools Plugin API.
|
163
|
*/
|
164
|
public static function loadMappers() {
|
165
|
static $loaded = FALSE;
|
166
|
if (!$loaded) {
|
167
|
$path = drupal_get_path('module', 'feeds') . '/mappers';
|
168
|
$files = drupal_system_listing('/.*\.inc$/', $path, 'name', 0);
|
169
|
foreach ($files as $file) {
|
170
|
if (strstr($file->uri, '/mappers/')) {
|
171
|
require_once(DRUPAL_ROOT . '/' . $file->uri);
|
172
|
}
|
173
|
}
|
174
|
}
|
175
|
$loaded = TRUE;
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* Get all available plugins.
|
180
|
*/
|
181
|
public static function all() {
|
182
|
ctools_include('plugins');
|
183
|
$plugins = ctools_get_plugins('feeds', 'plugins');
|
184
|
|
185
|
$result = array();
|
186
|
foreach ($plugins as $key => $info) {
|
187
|
if (!empty($info['hidden'])) {
|
188
|
continue;
|
189
|
}
|
190
|
$result[$key] = $info;
|
191
|
}
|
192
|
|
193
|
// Sort plugins by name and return.
|
194
|
uasort($result, 'feeds_plugin_compare');
|
195
|
return $result;
|
196
|
}
|
197
|
|
198
|
/**
|
199
|
* Determines whether given plugin is derived from given base plugin.
|
200
|
*
|
201
|
* @param $plugin_key
|
202
|
* String that identifies a Feeds plugin key.
|
203
|
* @param $parent_plugin
|
204
|
* String that identifies a Feeds plugin key to be tested against.
|
205
|
*
|
206
|
* @return
|
207
|
* TRUE if $parent_plugin is directly *or indirectly* a parent of $plugin,
|
208
|
* FALSE otherwise.
|
209
|
*/
|
210
|
public static function child($plugin_key, $parent_plugin) {
|
211
|
ctools_include('plugins');
|
212
|
$plugins = ctools_get_plugins('feeds', 'plugins');
|
213
|
$info = $plugins[$plugin_key];
|
214
|
|
215
|
if (empty($info['handler']['parent'])) {
|
216
|
return FALSE;
|
217
|
}
|
218
|
elseif ($info['handler']['parent'] == $parent_plugin) {
|
219
|
return TRUE;
|
220
|
}
|
221
|
else {
|
222
|
return self::child($info['handler']['parent'], $parent_plugin);
|
223
|
}
|
224
|
}
|
225
|
|
226
|
/**
|
227
|
* Determines the type of a plugin.
|
228
|
*
|
229
|
* @todo PHP5.3: Implement self::type() and query with $plugin_key::type().
|
230
|
*
|
231
|
* @param $plugin_key
|
232
|
* String that identifies a Feeds plugin key.
|
233
|
*
|
234
|
* @return
|
235
|
* One of the following values:
|
236
|
* 'fetcher' if the plugin is a fetcher
|
237
|
* 'parser' if the plugin is a parser
|
238
|
* 'processor' if the plugin is a processor
|
239
|
* FALSE otherwise.
|
240
|
*/
|
241
|
public static function typeOf($plugin_key) {
|
242
|
if (self::child($plugin_key, 'FeedsFetcher')) {
|
243
|
return 'fetcher';
|
244
|
}
|
245
|
elseif (self::child($plugin_key, 'FeedsParser')) {
|
246
|
return 'parser';
|
247
|
}
|
248
|
elseif (self::child($plugin_key, 'FeedsProcessor')) {
|
249
|
return 'processor';
|
250
|
}
|
251
|
return FALSE;
|
252
|
}
|
253
|
|
254
|
/**
|
255
|
* Gets all available plugins of a particular type.
|
256
|
*
|
257
|
* @param $type
|
258
|
* 'fetcher', 'parser' or 'processor'
|
259
|
*/
|
260
|
public static function byType($type) {
|
261
|
$plugins = self::all();
|
262
|
|
263
|
$result = array();
|
264
|
foreach ($plugins as $key => $info) {
|
265
|
if ($type == self::typeOf($key)) {
|
266
|
$result[$key] = $info;
|
267
|
}
|
268
|
}
|
269
|
return $result;
|
270
|
}
|
271
|
|
272
|
/**
|
273
|
* Implements FeedsConfigurable::dependencies().
|
274
|
*/
|
275
|
public function dependencies() {
|
276
|
$dependencies = parent::dependencies();
|
277
|
|
278
|
// Find out which module provides this plugin.
|
279
|
$plugin_info = $this->pluginDefinition();
|
280
|
if (isset($plugin_info['module'])) {
|
281
|
$dependencies[$plugin_info['module']] = $plugin_info['module'];
|
282
|
}
|
283
|
|
284
|
return $dependencies;
|
285
|
}
|
286
|
}
|
287
|
|
288
|
/**
|
289
|
* Used when a plugin is missing.
|
290
|
*/
|
291
|
class FeedsMissingPlugin extends FeedsPlugin {
|
292
|
public function pluginType() {
|
293
|
return 'missing';
|
294
|
}
|
295
|
|
296
|
public function save() {}
|
297
|
|
298
|
/**
|
299
|
* Fetcher methods.
|
300
|
*/
|
301
|
public function fetch(FeedsSource $source) {
|
302
|
return new FeedsFetcherResult('');
|
303
|
}
|
304
|
|
305
|
public function clear(FeedsSource $source) {}
|
306
|
|
307
|
public function request($feed_nid = 0) {
|
308
|
drupal_access_denied();
|
309
|
}
|
310
|
|
311
|
public function menuItem() {
|
312
|
return array();
|
313
|
}
|
314
|
|
315
|
public function subscribe(FeedsSource $source) {}
|
316
|
|
317
|
public function unsubscribe(FeedsSource $source) {}
|
318
|
|
319
|
public function importPeriod(FeedsSource $source) {}
|
320
|
|
321
|
/**
|
322
|
* Parser methods.
|
323
|
*/
|
324
|
public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
|
325
|
return new FeedsParserResult();
|
326
|
}
|
327
|
|
328
|
public function getMappingSources() {
|
329
|
return array();
|
330
|
}
|
331
|
|
332
|
/**
|
333
|
* Processor methods.
|
334
|
*/
|
335
|
public function process(FeedsSource $source, FeedsParserResult $parser_result) {}
|
336
|
|
337
|
public function entityType() {}
|
338
|
|
339
|
public function bundle() {}
|
340
|
|
341
|
public function bundleOptions() {
|
342
|
return array();
|
343
|
}
|
344
|
|
345
|
public function getLimit() {
|
346
|
return 0;
|
347
|
}
|
348
|
|
349
|
public function getMappings() {
|
350
|
return array();
|
351
|
}
|
352
|
|
353
|
public function getMappingTargets() {
|
354
|
return array();
|
355
|
}
|
356
|
|
357
|
public function expire(FeedsSource $source, $time = NULL) {}
|
358
|
|
359
|
public function itemCount(FeedsSource $source) {
|
360
|
return 0;
|
361
|
}
|
362
|
|
363
|
public function expiryTime() {
|
364
|
return FEEDS_EXPIRE_NEVER;
|
365
|
}
|
366
|
|
367
|
}
|
368
|
|
369
|
/**
|
370
|
* Sort callback for FeedsPlugin::all().
|
371
|
*/
|
372
|
function feeds_plugin_compare($a, $b) {
|
373
|
return strcasecmp($a['name'], $b['name']);
|
374
|
}
|