Révision ed9a13f1
Ajouté par Assos Assos il y a plus de 3 ans
drupal7/sites/all/modules/feeds/includes/FeedsImporter.inc | ||
---|---|---|
6 | 6 |
*/ |
7 | 7 |
|
8 | 8 |
/** |
9 |
* Class for a Feeds importer. |
|
10 |
* |
|
9 | 11 |
* A FeedsImporter object describes how an external source should be fetched, |
10 | 12 |
* parsed and processed. Feeds can manage an arbitrary amount of importers. |
11 | 13 |
* |
... | ... | |
24 | 26 |
*/ |
25 | 27 |
class FeedsImporter extends FeedsConfigurable { |
26 | 28 |
|
27 |
// Every feed has a fetcher, a parser and a processor. |
|
28 |
// These variable names match the possible return values of |
|
29 |
// FeedsPlugin::typeOf(). |
|
30 |
protected $fetcher, $parser, $processor; |
|
29 |
/** |
|
30 |
* Every feed has a fetcher, a parser and a processor. |
|
31 |
* |
|
32 |
* These variable names match the possible return values of |
|
33 |
* FeedsPlugin::typeOf(). |
|
34 |
*/ |
|
35 |
|
|
36 |
/** |
|
37 |
* The selected fetcher for this importer. |
|
38 |
* |
|
39 |
* @var FeedsFetcher|null |
|
40 |
*/ |
|
41 |
protected $fetcher; |
|
42 |
|
|
43 |
/** |
|
44 |
* The selected parser for this importer. |
|
45 |
* |
|
46 |
* @var FeedsParser|null |
|
47 |
*/ |
|
48 |
protected $parser; |
|
49 |
|
|
50 |
/** |
|
51 |
* The selected processor for this importer. |
|
52 |
* |
|
53 |
* @var FeedsProcessor|null |
|
54 |
*/ |
|
55 |
protected $processor; |
|
31 | 56 |
|
32 |
// This array defines the variable names of the plugins above. |
|
57 |
/** |
|
58 |
* This array defines the variable names of the plugins above. |
|
59 |
* |
|
60 |
* @var string[] |
|
61 |
*/ |
|
33 | 62 |
protected $plugin_types = array('fetcher', 'parser', 'processor'); |
34 | 63 |
|
35 | 64 |
/** |
36 |
* Instantiate class variables, initialize and configure |
|
37 |
* plugins. |
|
65 |
* Instantiate class variables, initialize and configure plugins. |
|
66 |
* |
|
67 |
* @param string $id |
|
68 |
* The importer ID. |
|
38 | 69 |
*/ |
39 | 70 |
protected function __construct($id) { |
40 | 71 |
parent::__construct($id); |
... | ... | |
55 | 86 |
} |
56 | 87 |
|
57 | 88 |
/** |
58 |
* Report how many items *should* be created on one page load by this |
|
59 |
* importer. |
|
89 |
* Reports how many items *should* be created on one request by this importer. |
|
60 | 90 |
* |
61 | 91 |
* Note: |
62 | 92 |
* |
... | ... | |
65 | 95 |
* number of items that can be created on one page load is actually without |
66 | 96 |
* limit. |
67 | 97 |
* |
68 |
* @return |
|
98 |
* @return int
|
|
69 | 99 |
* A positive number defining the number of items that can be created on |
70 | 100 |
* one page load. 0 if this number is unlimited. |
71 | 101 |
*/ |
... | ... | |
128 | 158 |
/** |
129 | 159 |
* Set plugin. |
130 | 160 |
* |
131 |
* @param $plugin_key |
|
132 |
* A fetcher, parser or processor plugin.
|
|
161 |
* @param string $plugin_key
|
|
162 |
* The name of a fetcher, parser or processor plugin.
|
|
133 | 163 |
* |
134 | 164 |
* @todo Error handling, handle setting to the same plugin. |
135 | 165 |
*/ |
136 | 166 |
public function setPlugin($plugin_key) { |
137 |
// $plugin_type can be either 'fetcher', 'parser' or 'processor' |
|
167 |
// $plugin_type can be either 'fetcher', 'parser' or 'processor'.
|
|
138 | 168 |
if ($plugin_type = FeedsPlugin::typeOf($plugin_key)) { |
139 | 169 |
if ($plugin = feeds_plugin($plugin_key, $this->id)) { |
140 | 170 |
// Unset existing plugin, switch to new plugin. |
... | ... | |
153 | 183 |
* @param FeedsConfigurable $configurable |
154 | 184 |
* The feeds importer object to copy from. |
155 | 185 |
*/ |
156 |
public function copy(FeedsConfigurable $configurable) {
|
|
157 |
parent::copy($configurable);
|
|
158 |
|
|
159 |
if ($configurable instanceof FeedsImporter) {
|
|
160 |
// Instantiate new fetcher, parser and processor and initialize their
|
|
161 |
// configurations.
|
|
162 |
foreach ($this->plugin_types as $plugin_type) {
|
|
163 |
$this->setPlugin($configurable->config[$plugin_type]['plugin_key']);
|
|
164 |
$this->$plugin_type->setConfig($configurable->config[$plugin_type]['config']);
|
|
165 |
}
|
|
166 |
}
|
|
167 |
}
|
|
186 |
public function copy(FeedsConfigurable $configurable) { |
|
187 |
parent::copy($configurable); |
|
188 |
|
|
189 |
if ($configurable instanceof FeedsImporter) { |
|
190 |
// Instantiate new fetcher, parser and processor and initialize their |
|
191 |
// configurations. |
|
192 |
foreach ($this->plugin_types as $plugin_type) { |
|
193 |
$this->setPlugin($configurable->config[$plugin_type]['plugin_key']); |
|
194 |
$this->$plugin_type->setConfig($configurable->config[$plugin_type]['config']); |
|
195 |
} |
|
196 |
} |
|
197 |
} |
|
168 | 198 |
|
169 | 199 |
/** |
170 | 200 |
* Get configuration of this feed. |
... | ... | |
265 | 295 |
), |
266 | 296 |
'content_type' => '', |
267 | 297 |
'update' => 0, |
268 |
'import_period' => 1800, // Refresh every 30 minutes by default. |
|
269 |
'expire_period' => 3600, // Expire every hour by default, this is a hidden setting. |
|
270 |
'import_on_create' => TRUE, // Import on submission. |
|
298 |
// Refresh every 30 minutes by default. |
|
299 |
'import_period' => 1800, |
|
300 |
// Expire every hour by default, this is a hidden setting. |
|
301 |
'expire_period' => 3600, |
|
302 |
// Import on submission. |
|
303 |
'import_on_create' => TRUE, |
|
271 | 304 |
'process_in_background' => FALSE, |
272 | 305 |
) + parent::configDefaults(); |
273 | 306 |
} |
274 | 307 |
|
275 | 308 |
/** |
276 |
* Override parent::configForm(). |
|
309 |
* Overrides parent::configForm(). |
|
310 |
* |
|
311 |
* @param array $form_state |
|
312 |
* The current state of the form. |
|
313 |
* |
|
314 |
* @return array |
|
315 |
* The form definition. |
|
277 | 316 |
*/ |
278 | 317 |
public function configForm(&$form_state) { |
279 | 318 |
$config = $this->getConfig(); |
... | ... | |
296 | 335 |
$form['content_type'] = array( |
297 | 336 |
'#type' => 'select', |
298 | 337 |
'#title' => t('Attach to content type'), |
299 |
'#description' => t('If "Use standalone form" is selected a source is imported by using a form under !import_form.
|
|
300 |
If a content type is selected a source is imported by creating a node of that content type.',
|
|
301 |
array('!import_form' => l(url('import', array('absolute' => TRUE)), 'import', array('attributes' => array('target' => '_new'))))),
|
|
338 |
'#description' => '<p>' . t('If "Use standalone form" is selected, a source is imported by using a form under !import_form. If a content type is selected, a source is imported by creating a node of that content type.', array(
|
|
339 |
'!import_form' => l(url('import', array('absolute' => TRUE)), 'import', array('attributes' => array('target' => '_new'))),
|
|
340 |
)) . '</p>',
|
|
302 | 341 |
'#options' => array('' => t('Use standalone form')) + $node_types, |
303 | 342 |
'#default_value' => $config['content_type'], |
304 | 343 |
); |
305 |
$cron_required = ' ' . l(t('Requires cron to be configured.'), 'http://drupal.org/cron', array('attributes' => array('target' => '_new'))); |
|
306 |
$period = drupal_map_assoc(array(900, 1800, 3600, 10800, 21600, 43200, 86400, 259200, 604800, 2419200), 'format_interval'); |
|
344 |
$cron_required = ' ' . l(t('Requires cron to be configured.'), 'http://drupal.org/cron', array('attributes' => array('target' => '_new'))); |
|
345 |
$period = drupal_map_assoc(array( |
|
346 |
900, |
|
347 |
1800, |
|
348 |
3600, |
|
349 |
10800, |
|
350 |
21600, |
|
351 |
43200, |
|
352 |
86400, |
|
353 |
259200, |
|
354 |
604800, |
|
355 |
2419200, |
|
356 |
), 'format_interval'); |
|
307 | 357 |
foreach ($period as &$p) { |
308 | 358 |
$p = t('Every !p', array('!p' => $p)); |
309 | 359 |
} |
... | ... | |
334 | 384 |
} |
335 | 385 |
|
336 | 386 |
/** |
387 |
* Overrides parent::configFormSubmit(). |
|
388 |
* |
|
337 | 389 |
* Reschedule if import period changes. |
390 |
* |
|
391 |
* @param array $values |
|
392 |
* An array that contains the values entered by the user through configForm. |
|
338 | 393 |
*/ |
339 | 394 |
public function configFormSubmit(&$values) { |
340 | 395 |
if ($this->config['import_period'] != $values['import_period']) { |
... | ... | |
353 | 408 |
} |
354 | 409 |
return $dependencies; |
355 | 410 |
} |
411 |
|
|
356 | 412 |
} |
357 | 413 |
|
358 | 414 |
/** |
359 |
* Helper, see FeedsDataProcessor class. |
|
415 |
* Formats a time interval. |
|
416 |
* |
|
417 |
* @param int $timestamp |
|
418 |
* The length of the interval in seconds. |
|
419 |
* |
|
420 |
* @return string |
|
421 |
* A translated string representation of the interval. |
|
360 | 422 |
*/ |
361 | 423 |
function feeds_format_expire($timestamp) { |
362 | 424 |
if ($timestamp == FEEDS_EXPIRE_NEVER) { |
Formats disponibles : Unified diff
Weekly update of contrib modules