Newer
Older
Alex Barth
committed
<?php
/**
* @file
* Definition of FeedsPlugin class.
*/
/**
* Base class for a fetcher, parser or processor result.
*/
class FeedsResult {}
Alex Barth
committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Implement source interface for all plugins.
*
* Note how this class does not attempt to store source information locally.
* Doing this would break the model where source information is represented by
* an object that is being passed into a Feed object and its plugins.
*/
abstract class FeedsPlugin extends FeedsConfigurable implements FeedsSourceInterface {
/**
* Constructor.
*
* Initialize class variables.
*/
protected function __construct($id) {
parent::__construct($id);
$this->source_config = $this->sourceDefaults();
}
/**
* Save changes to the configuration of this object.
* Delegate saving to parent (= Feed) which will collect
* information from this object by way of getConfig() and store it.
*/
public function save() {
feeds_importer($this->id)->save();
}
/**
* Returns TRUE if $this->sourceForm() returns a form.
*/
public function hasSourceConfig() {
$form = $this->sourceForm(array());
return !empty($form);
}
/**
* Implements FeedsSourceInterface::sourceDefaults().
Alex Barth
committed
*/
public function sourceDefaults() {
$values = array_flip(array_keys($this->sourceForm(array())));
foreach ($values as $k => $v) {
$values[$k] = '';
}
return $values;
}
/**
* Callback methods, exposes source form.
*/
public function sourceForm($source_config) {
return array();
}
/**
* Validation handler for sourceForm.
*/
Alex Barth
committed
public function sourceFormValidate(&$source_config) {}
/**
* A source is being saved.
*/
public function sourceSave(FeedsSource $source) {}
/**
* A source is being deleted.
*/
public function sourceDelete(FeedsSource $source) {}
/**
* Loads on-behalf implementations from mappers/ directory.
*
* FeedsProcessor::map() does not load from mappers/ as only node and user
* processor ship with on-behalf implementations.
*
* @see FeedsNodeProcessor::map()
* @see FeedsUserProcessor::map()
Alex Barth
committed
*
* @todo: Use CTools Plugin API.
*/
protected static function loadMappers() {
static $loaded = FALSE;
if (!$loaded) {
$path = drupal_get_path('module', 'feeds') . '/mappers';
$files = drupal_system_listing('/.*\.inc$/', $path, 'name', 0);
foreach ($files as $file) {
Alex Barth
committed
if (strstr($file->uri, '/mappers/')) {
require_once("./$file->uri");
}
}
}
$loaded = TRUE;
}
Alex Barth
committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* Get all available plugins.
*/
public static function all() {
ctools_include('plugins');
$plugins = ctools_get_plugins('feeds', 'plugins');
$result = array();
foreach ($plugins as $key => $info) {
if (!empty($info['hidden'])) {
continue;
}
$result[$key] = $info;
}
// Sort plugins by name and return.
uasort($result, 'feeds_plugin_compare');
return $result;
}
/**
* Determines whether given plugin is derived from given base plugin.
*
* @param $plugin_key
* String that identifies a Feeds plugin key.
* @param $parent_plugin
* String that identifies a Feeds plugin key to be tested against.
*
* @return
* TRUE if $parent_plugin is directly *or indirectly* a parent of $plugin,
* FALSE otherwise.
*/
public static function child($plugin_key, $parent_plugin) {
ctools_include('plugins');
$plugins = ctools_get_plugins('feeds', 'plugins');
$info = $plugins[$plugin_key];
if (empty($info['handler']['parent'])) {
return FALSE;
}
elseif ($info['handler']['parent'] == $parent_plugin) {
return TRUE;
}
else {
return self::child($info['handler']['parent'], $parent_plugin);
}
}
/**
* Determines the type of a plugin.
*
* @todo PHP5.3: Implement self::type() and query with $plugin_key::type().
*
* @param $plugin_key
* String that identifies a Feeds plugin key.
*
* @return
* One of the following values:
* 'fetcher' if the plugin is a fetcher
* 'parser' if the plugin is a parser
* 'processor' if the plugin is a processor
* FALSE otherwise.
*/
public static function typeOf($plugin_key) {
if (self::child($plugin_key, 'FeedsFetcher')) {
return 'fetcher';
}
elseif (self::child($plugin_key, 'FeedsParser')) {
return 'parser';
}
elseif (self::child($plugin_key, 'FeedsProcessor')) {
return 'processor';
}
return FALSE;
}
/**
* Gets all available plugins of a particular type.
*
* @param $type
* 'fetcher', 'parser' or 'processor'
*/
public static function byType($type) {
$plugins = self::all();
$result = array();
foreach ($plugins as $key => $info) {
if ($type == self::typeOf($key)) {
$result[$key] = $info;
}
}
return $result;
}
Alex Barth
committed
}
/**
* Used when a plugin is missing.
*/
class FeedsMissingPlugin extends FeedsPlugin {
public function menuItem() {
return array();
}
Alex Barth
committed
/**
* Sort callback for FeedsPlugin::all().
*/
function feeds_plugin_compare($a, $b) {
return strcasecmp($a['name'], $b['name']);
}