View source
<?php
require_once dirname(__FILE__) . '/FeedsConfigurable.inc';
require_once dirname(__FILE__) . '/FeedsSource.inc';
require_once dirname(__FILE__) . '/FeedsBatch.inc';
class FeedsImporter extends FeedsConfigurable {
protected $fetcher, $parser, $processor;
protected $plugin_types = array(
'fetcher',
'parser',
'processor',
);
protected function __construct($id) {
parent::__construct($id);
$this
->load();
foreach ($this->plugin_types as $type) {
$plugin = feeds_plugin_instance($this->config[$type]['plugin_key'], $this->id);
if (isset($this->config[$type]['config'])) {
$plugin
->setConfig($this->config[$type]['config']);
}
$this->{$type} = $plugin;
}
}
public function expire($time = NULL) {
return $this->processor
->expire($time);
}
public function schedule() {
$job = array(
'callback' => 'feeds_importer_expire',
'type' => $this->id,
'period' => 0,
'periodic' => TRUE,
);
if (FEEDS_EXPIRE_NEVER != $this->processor
->expiryTime()) {
$job['period'] = 3600;
job_scheduler()
->set($job);
}
else {
job_scheduler()
->remove($job);
}
}
public function save() {
$save = new stdClass();
$save->id = $this->id;
$save->config = $this
->getConfig();
if ($config = db_result(db_query("SELECT config FROM {feeds_importer} WHERE id = '%s'", $this->id))) {
drupal_write_record('feeds_importer', $save, 'id');
$config = unserialize($config);
if ($config['content_type'] != $save->config['content_type']) {
variable_set('menu_rebuild_needed', TRUE);
}
}
else {
drupal_write_record('feeds_importer', $save);
}
}
public function load() {
ctools_include('export');
if ($config = ctools_export_load_object('feeds_importer', 'conditions', array(
'id' => $this->id,
))) {
$config = array_shift($config);
$this->export_type = $config->export_type;
$this->disabled = isset($config->disabled) ? $config->disabled : FALSE;
$this->config = $config->config;
return TRUE;
}
return FALSE;
}
public function delete() {
db_query("DELETE FROM {feeds_importer} WHERE id = '%s'", $this->id);
$job = array(
'callback' => 'feeds_importer_expire',
'type' => $this->id,
'id' => 0,
);
if ($this->export_type & EXPORT_IN_CODE) {
feeds_reschedule($this->id);
}
else {
job_scheduler()
->remove($job);
}
}
public function setPlugin($plugin_key) {
if ($plugin_type = feeds_plugin_type($plugin_key)) {
if ($plugin = feeds_plugin_instance($plugin_key, $this->id)) {
unset($this->{$plugin_type});
$this->{$plugin_type} = $plugin;
$this->config[$plugin_type] = array(
'plugin_key' => $plugin_key,
);
}
}
}
public function copy(FeedsConfigurable $importer) {
$this
->setConfig($importer->config);
foreach ($this->plugin_types as $plugin_type) {
$this
->setPlugin($importer->config[$plugin_type]['plugin_key']);
$this->{$plugin_type}
->setConfig($importer->config[$plugin_type]['config']);
}
}
public function getConfig() {
foreach (array(
'fetcher',
'parser',
'processor',
) as $type) {
$this->config[$type]['config'] = $this->{$type}
->getConfig();
}
return $this->config;
}
public function configDefaults() {
return array(
'name' => '',
'description' => '',
'fetcher' => array(
'plugin_key' => 'FeedsHTTPFetcher',
),
'parser' => array(
'plugin_key' => 'FeedsSyndicationParser',
),
'processor' => array(
'plugin_key' => 'FeedsNodeProcessor',
),
'content_type' => '',
'update' => 0,
'import_period' => 1800,
'expire_period' => 3600,
'import_on_create' => TRUE,
);
}
public function configForm(&$form_state) {
$form = array();
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#description' => t('The name of this configuration.'),
'#default_value' => $this->config['name'],
'#required' => TRUE,
);
$form['description'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#description' => t('A description of this configuration.'),
'#default_value' => $this->config['description'],
);
$form['content_type'] = array(
'#type' => 'select',
'#title' => t('Attach to content type'),
'#description' => t('If an importer is attached to a content type, content is imported by creating a node. If the standalone form is selected, content is imported by using the standalone form under http://example.com/import.'),
'#options' => array(
'' => t('Use standalone form'),
) + node_get_types('names'),
'#default_value' => $this->config['content_type'],
);
$period = drupal_map_assoc(array(
0,
900,
1800,
3600,
10800,
21600,
43200,
86400,
259200,
604800,
2419200,
), 'format_interval');
$period[FEEDS_SCHEDULE_NEVER] = t('Never');
$period[0] = t('As often as possible');
$form['import_period'] = array(
'#type' => 'select',
'#title' => t('Minimum refresh period'),
'#options' => $period,
'#description' => t('This is the minimum time that must elapse before a feed may be refreshed automatically.'),
'#default_value' => $this->config['import_period'],
);
$form['import_on_create'] = array(
'#type' => 'checkbox',
'#title' => t('Import on submission'),
'#description' => t('Check if content should be imported at the moment of feed submission.'),
'#default_value' => $this->config['import_on_create'],
);
return $form;
}
public function configFormSubmit(&$values) {
if ($this->config['import_period'] != $values['import_period']) {
feeds_reschedule($this->id);
}
parent::configFormSubmit($values);
}
}
function feeds_format_expire($timestamp) {
if ($timestamp == FEEDS_EXPIRE_NEVER) {
return t('Never');
}
return t('after !time', array(
'!time' => format_interval($timestamp),
));
}