Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
// $Id$
/**
* @file
*/
/**
* Importer class.
*
* After fetch() has been invoked, it can be cached and revoked in a subsequent
* page callback until it is reset().
*/
class Feed extends FeedsConfigurable {
public $fetcher, $parser, $processors;
/**
* Instantiate class variables.
*/
public function __construct($id, $config = NULL) {
parent::__construct($id, $config);
feeds_require_plugin($this->config['fetcher']);
$this->fetcher = new $this->config['fetcher']($id);
feeds_require_plugin($this->config['parser']);
$this->parser = new $this->config['parser']($id);
foreach ($this->config['processors'] as $processor) {
feeds_require_plugin($processor);
$this->processors[$processor] = new $processor($id);
}
}
/**
* Import feed by using configured fetchers, parsers, processors.
*/
public function import() {
$raw = $this->fetcher->fetch($this->source);
$parsed = $this->parser->parse($raw);
foreach ($this->processors as $processor) {
$processor->process($parsed);
}
}
/**
* Set active fetcher. Does not save fetcher to configuration.
*
* @param $fetcher
* String that is the class name of the fetcher.
*/
public function setFetcher($fetcher) {
unset($this->fetcher);
feeds_require_plugin($fetcher);
$this->config['fetcher'] = $fetcher;
$this->fetcher = new $fetcher($this->id);
}
/**
* Set active parser. Does not save parser to configuration.
*
* @param $parser
* String that is the class name of the parser.
*/
public function setParser($parser) {
unset($this->parser);
feeds_require_plugin($parser);
$this->config['parser'] = $parser;
$this->save();
$this->parser = new $parser($this->id);
}
/**
* Set active processors.
*
* @param $processors
* Array of strings that are the processors of this feed.
*/
public function setProcessors($processors) {
unset($this->processors);
$this->processors = array();
$this->config['processors'] = $processors;
$this->save();
foreach ($processors as $processor) {
feeds_require_plugin($processor);
$this->processors[] = new $processor($this->id);
}
}
}
/**
* Base class for configurable, peristent objects.
*/
class FeedsConfigurable {
protected $config;
protected $id;
/**
* Constructor.
*
* @param $id
* String identifier of this object.
* @param $config
* Configuration of this object. If not available, will attempt to load from database.
*/
public function __construct($id, $config = NULL) {
$this->id = $id;
if (empty($config)) {
$this->load();
}
else {
$this->config = $config;
}
}
/**
* Save configuration.
*/
public function save() {
$save = new stdClass();
$save->id = $this->id;
$save->class = get_class($this);
$save->config = $this->config;
db_query('DELETE FROM {feeds_configuration} WHERE id = "%s"', $save->id);
drupal_write_record('feeds_configuration', $save);
}
/**
* Load configuration and unpack.
*/
public function load() {
ctools_include('export');
if ($config = ctools_export_load_object('feeds_configuration', 'conditions', array('id' => $this->id, 'class' => get_class($this)))) {
$config = array_shift($config);
$this->config = $config->config;
return TRUE;
}
return FALSE;
}
public function getConfig($fallback = TRUE) {
if ($fallback) {
return empty($this->config) ? $this->getDefaultConfig() : $this->config;
}
return $this->config;
}
public function setConfig($config) {
$this->config = $config;
}
public function getDefaultConfig() {
return array();
}
public function getId() {
return $this->id;
}
public function form() {
return array();
}
}
/**
* Abstract class, defines interface for fetchers.
*
* Not using interfaces because we need a simple inheritence tree for determining the
* plugin type. See hook_feeds_plugin().
*/
class FeedsFetcher extends FeedsConfigurable {
public function fetch($source) {
return NULL;
}
}
/**
* Abstract class, defines interface for parsers.
*/
class FeedsParser extends FeedsConfigurable {
public function parse($raw) {
return NULL;
}
public function getMappingSources() {
return NULL;
}
}
/**
* Abstract class, defines interface for processors.
*/
class FeedsProcessor extends FeedsConfigurable {
public function process($feed) {
return NULL;
}
public function getMappingTargets() {
return NULL;
}
/**
* Build the mapping form.
*/
public function mappingForm() {
return array();
}
/**
* Return TRUE if processor requires that the feed configuration be attached to a feed node.
*/
public function requiresFeedAsNode() {
return TRUE;
}
}