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
|
* Constructor.
|
24
|
*
|
25
|
* Initialize class variables.
|
26
|
*/
|
27
|
protected function __construct($id) {
|
28
|
parent::__construct($id);
|
29
|
$this->source_config = $this->sourceDefaults();
|
30
|
}
|
31
|
|
32
|
/**
|
33
|
* Returns the type of plugin.
|
34
|
*
|
35
|
* @return string
|
36
|
* One of either 'fetcher', 'parser', or 'processor'.
|
37
|
*/
|
38
|
abstract public function pluginType();
|
39
|
|
40
|
/**
|
41
|
* Save changes to the configuration of this object.
|
42
|
* Delegate saving to parent (= Feed) which will collect
|
43
|
* information from this object by way of getConfig() and store it.
|
44
|
*/
|
45
|
public function save() {
|
46
|
feeds_importer($this->id)->save();
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* Returns TRUE if $this->sourceForm() returns a form.
|
51
|
*/
|
52
|
public function hasSourceConfig() {
|
53
|
$form = $this->sourceForm(array());
|
54
|
return !empty($form);
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* Implements FeedsSourceInterface::sourceDefaults().
|
59
|
*/
|
60
|
public function sourceDefaults() {
|
61
|
$values = array_flip(array_keys($this->sourceForm(array())));
|
62
|
foreach ($values as $k => $v) {
|
63
|
$values[$k] = '';
|
64
|
}
|
65
|
return $values;
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Callback methods, exposes source form.
|
70
|
*/
|
71
|
public function sourceForm($source_config) {
|
72
|
return array();
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
* Validation handler for sourceForm.
|
77
|
*/
|
78
|
public function sourceFormValidate(&$source_config) {}
|
79
|
|
80
|
/**
|
81
|
* A source is being saved.
|
82
|
*/
|
83
|
public function sourceSave(FeedsSource $source) {}
|
84
|
|
85
|
/**
|
86
|
* A source is being deleted.
|
87
|
*/
|
88
|
public function sourceDelete(FeedsSource $source) {}
|
89
|
|
90
|
/**
|
91
|
* Loads on-behalf implementations from mappers/ directory.
|
92
|
*
|
93
|
* FeedsProcessor::map() does not load from mappers/ as only node and user
|
94
|
* processor ship with on-behalf implementations.
|
95
|
*
|
96
|
* @see FeedsNodeProcessor::map()
|
97
|
* @see FeedsUserProcessor::map()
|
98
|
*
|
99
|
* @todo: Use CTools Plugin API.
|
100
|
*/
|
101
|
public static function loadMappers() {
|
102
|
static $loaded = FALSE;
|
103
|
if (!$loaded) {
|
104
|
$path = drupal_get_path('module', 'feeds') . '/mappers';
|
105
|
$files = drupal_system_listing('/.*\.inc$/', $path, 'name', 0);
|
106
|
foreach ($files as $file) {
|
107
|
if (strstr($file->uri, '/mappers/')) {
|
108
|
require_once(DRUPAL_ROOT . '/' . $file->uri);
|
109
|
}
|
110
|
}
|
111
|
}
|
112
|
$loaded = TRUE;
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* Get all available plugins.
|
117
|
*/
|
118
|
public static function all() {
|
119
|
ctools_include('plugins');
|
120
|
$plugins = ctools_get_plugins('feeds', 'plugins');
|
121
|
|
122
|
$result = array();
|
123
|
foreach ($plugins as $key => $info) {
|
124
|
if (!empty($info['hidden'])) {
|
125
|
continue;
|
126
|
}
|
127
|
$result[$key] = $info;
|
128
|
}
|
129
|
|
130
|
// Sort plugins by name and return.
|
131
|
uasort($result, 'feeds_plugin_compare');
|
132
|
return $result;
|
133
|
}
|
134
|
|
135
|
/**
|
136
|
* Determines whether given plugin is derived from given base plugin.
|
137
|
*
|
138
|
* @param $plugin_key
|
139
|
* String that identifies a Feeds plugin key.
|
140
|
* @param $parent_plugin
|
141
|
* String that identifies a Feeds plugin key to be tested against.
|
142
|
*
|
143
|
* @return
|
144
|
* TRUE if $parent_plugin is directly *or indirectly* a parent of $plugin,
|
145
|
* FALSE otherwise.
|
146
|
*/
|
147
|
public static function child($plugin_key, $parent_plugin) {
|
148
|
ctools_include('plugins');
|
149
|
$plugins = ctools_get_plugins('feeds', 'plugins');
|
150
|
$info = $plugins[$plugin_key];
|
151
|
|
152
|
if (empty($info['handler']['parent'])) {
|
153
|
return FALSE;
|
154
|
}
|
155
|
elseif ($info['handler']['parent'] == $parent_plugin) {
|
156
|
return TRUE;
|
157
|
}
|
158
|
else {
|
159
|
return self::child($info['handler']['parent'], $parent_plugin);
|
160
|
}
|
161
|
}
|
162
|
|
163
|
/**
|
164
|
* Determines the type of a plugin.
|
165
|
*
|
166
|
* @todo PHP5.3: Implement self::type() and query with $plugin_key::type().
|
167
|
*
|
168
|
* @param $plugin_key
|
169
|
* String that identifies a Feeds plugin key.
|
170
|
*
|
171
|
* @return
|
172
|
* One of the following values:
|
173
|
* 'fetcher' if the plugin is a fetcher
|
174
|
* 'parser' if the plugin is a parser
|
175
|
* 'processor' if the plugin is a processor
|
176
|
* FALSE otherwise.
|
177
|
*/
|
178
|
public static function typeOf($plugin_key) {
|
179
|
if (self::child($plugin_key, 'FeedsFetcher')) {
|
180
|
return 'fetcher';
|
181
|
}
|
182
|
elseif (self::child($plugin_key, 'FeedsParser')) {
|
183
|
return 'parser';
|
184
|
}
|
185
|
elseif (self::child($plugin_key, 'FeedsProcessor')) {
|
186
|
return 'processor';
|
187
|
}
|
188
|
return FALSE;
|
189
|
}
|
190
|
|
191
|
/**
|
192
|
* Gets all available plugins of a particular type.
|
193
|
*
|
194
|
* @param $type
|
195
|
* 'fetcher', 'parser' or 'processor'
|
196
|
*/
|
197
|
public static function byType($type) {
|
198
|
$plugins = self::all();
|
199
|
|
200
|
$result = array();
|
201
|
foreach ($plugins as $key => $info) {
|
202
|
if ($type == self::typeOf($key)) {
|
203
|
$result[$key] = $info;
|
204
|
}
|
205
|
}
|
206
|
return $result;
|
207
|
}
|
208
|
}
|
209
|
|
210
|
/**
|
211
|
* Used when a plugin is missing.
|
212
|
*/
|
213
|
class FeedsMissingPlugin extends FeedsPlugin {
|
214
|
public function pluginType() {
|
215
|
return 'missing';
|
216
|
}
|
217
|
public function menuItem() {
|
218
|
return array();
|
219
|
}
|
220
|
}
|
221
|
|
222
|
/**
|
223
|
* Sort callback for FeedsPlugin::all().
|
224
|
*/
|
225
|
function feeds_plugin_compare($a, $b) {
|
226
|
return strcasecmp($a['name'], $b['name']);
|
227
|
}
|