View source
<?php
abstract class FeedsExBase extends FeedsParser {
protected $isMultibyte;
protected $messenger;
protected abstract function executeContext(FeedsSource $source, FeedsFetcherResult $fetcher_result);
protected abstract function executeSourceExpression($machine_name, $expression, $row);
protected abstract function validateExpression(&$expression);
protected abstract function getErrors();
protected function setUp(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
}
protected function cleanUp(FeedsSource $source, FeedsParserResult $parser_result) {
}
protected function startErrorHandling() {
}
protected function stopErrorHandling() {
}
protected function loadLibrary() {
}
protected function hasConfigurableContext() {
return TRUE;
}
protected function configFormTableHeader() {
return array();
}
protected function configFormTableColumn(array &$form_state, $column_name, array $source) {
return array();
}
protected function __construct($id) {
parent::__construct($id);
$this->isMultibyte = $GLOBALS['multibyte'] == UNICODE_MULTIBYTE;
}
public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
$this
->loadLibrary();
$this
->startErrorHandling();
try {
$result = $this
->delegateParsing($source, $fetcher_result);
} catch (FeedsExEmptyException $e) {
$this
->getMessenger()
->setMessage(t('The feed is empty.'), 'warning', FALSE);
$result = new FeedsParserResult();
} catch (Expcetion $exception) {
}
$errors = $this
->getErrors();
if ($this->config['display_errors']) {
$this
->printErrors($errors, WATCHDOG_DEBUG);
}
else {
$this
->printErrors($errors);
}
$this
->logErrors($source, $errors);
$this
->stopErrorHandling();
if (isset($exception)) {
throw $exception;
}
return $result;
}
protected function delegateParsing(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
$result = new FeedsParserResult();
$fetcher_config = $source
->getConfigFor($source->importer->fetcher);
$result->link = is_string($fetcher_config['source']) ? $fetcher_config['source'] : '';
$expressions = $this
->prepareExpressions();
$variable_map = $this
->prepareVariables($expressions);
$this
->setUp($source, $fetcher_result);
foreach ($this
->executeContext($source, $fetcher_result) as $row) {
if ($item = $this
->executeSources($row, $expressions, $variable_map)) {
$result->items[] = $item;
}
}
$this
->cleanUp($source, $result);
return $result;
}
protected function prepareExpressions() {
$expressions = array();
foreach ($this->config['sources'] as $machine_name => $source) {
if (strlen($source['value'])) {
$expressions[$machine_name] = $source['value'];
}
}
return $expressions;
}
protected function prepareVariables(array $expressions) {
$variable_map = array();
foreach ($expressions as $machine_name => $expression) {
$variable_map[$machine_name] = '$' . $machine_name;
}
return $variable_map;
}
protected function executeSources($row, array $expressions, array $variable_map) {
$item = array();
$variables = array();
foreach ($expressions as $machine_name => $expression) {
$expression = strtr($expression, $variables);
$result = $this
->executeSourceExpression($machine_name, $expression, $row);
if (!empty($this->config['sources'][$machine_name]['debug'])) {
$this
->debug($result, $machine_name);
}
if ($result === NULL) {
$variables[$variable_map[$machine_name]] = '';
continue;
}
$item[$machine_name] = $result;
$variables[$variable_map[$machine_name]] = is_array($result) ? reset($result) : $result;
}
return $item;
}
protected function printErrors(array $errors, $severity = WATCHDOG_ERROR) {
foreach ($errors as $error) {
if ($error['severity'] > $severity) {
continue;
}
$this
->getMessenger()
->setMessage(t($error['message'], $error['variables']), $error['severity'] <= WATCHDOG_ERROR ? 'error' : 'warning', FALSE);
}
}
protected function logErrors(FeedsSource $source, array $errors, $severity = WATCHDOG_ERROR) {
foreach ($errors as $error) {
if ($error['severity'] > $severity) {
continue;
}
$source
->log('feeds_ex', $error['message'], $error['variables'], $error['severity']);
}
}
protected function detectEncoding($data) {
if ($this->isMultibyte) {
return mb_detect_encoding($data, $this->config['source_encoding'], TRUE);
}
return FALSE;
}
protected function convertEncoding($data, $encoding) {
$php_supported = array(
'utf-8',
'us-ascii',
'ascii',
);
if (in_array(strtolower($encoding), $php_supported)) {
return $data;
}
$converted = drupal_convert_to_utf8($data, $encoding);
if ($converted === FALSE) {
return $data;
}
return $converted;
}
protected function debug($data, $machine_name) {
$name = $machine_name;
if ($this->config['sources'][$machine_name]['name']) {
$name = $this->config['sources'][$machine_name]['name'];
}
$output = '<strong>' . $name . ':</strong>';
$data = is_array($data) ? $data : array(
$data,
);
foreach ($data as $key => $value) {
$data[$key] = check_plain($value);
}
$output .= theme('item_list', array(
'items' => $data,
));
$this
->getMessenger()
->setMessage($output);
}
public function getMappingSources() {
return parent::getMappingSources() + $this->config['sources'];
}
public function configDefaults() {
return array(
'sources' => array(),
'context' => array(
'value' => '',
),
'display_errors' => FALSE,
'source_encoding' => array(
'auto',
),
'debug_mode' => FALSE,
);
}
public function configForm(&$form_state) {
$form = array(
'#tree' => TRUE,
'#theme' => 'feeds_ex_configuration_table',
'#prefix' => '<div id="feeds-ex-configuration-wrapper">',
'#suffix' => '</div>',
);
if ($this
->hasConfigurableContext()) {
$form['context']['name'] = array(
'#type' => 'markup',
'#markup' => t('Context'),
);
$form['context']['value'] = array(
'#type' => 'textfield',
'#title' => t('Context value'),
'#title_display' => 'invisible',
'#default_value' => $this->config['context']['value'],
'#size' => 50,
'#required' => TRUE,
'#description' => '<span class="form-required">*</span>',
'#attributes' => array(
'class' => array(
'feeds-ex-context-value',
),
),
);
}
$form['sources'] = array(
'#id' => 'feeds-ex-source-table',
);
$max_weight = 0;
foreach ($this->config['sources'] as $machine_name => $source) {
$form['sources'][$machine_name]['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#title_display' => 'invisible',
'#default_value' => $source['name'],
'#size' => 20,
);
$form['sources'][$machine_name]['machine_name'] = array(
'#title' => t('Machine name'),
'#title_display' => 'invisible',
'#markup' => $machine_name,
);
$form['sources'][$machine_name]['value'] = array(
'#type' => 'textfield',
'#title' => t('Value'),
'#title_display' => 'invisible',
'#default_value' => $source['value'],
'#size' => 50,
);
foreach ($this
->configFormTableHeader() as $column => $name) {
$form['sources'][$machine_name][$column] = $this
->configFormTableColumn($form_state, $column, $source);
}
$form['sources'][$machine_name]['debug'] = array(
'#type' => 'checkbox',
'#title' => t('Debug'),
'#title_display' => 'invisible',
'#default_value' => $source['debug'],
);
$form['sources'][$machine_name]['remove'] = array(
'#type' => 'checkbox',
'#title' => t('Remove'),
'#title_display' => 'invisible',
);
$form['sources'][$machine_name]['weight'] = array(
'#type' => 'textfield',
'#default_value' => $source['weight'],
'#size' => 3,
'#attributes' => array(
'class' => array(
'feeds-ex-source-weight',
),
),
);
$max_weight = $source['weight'];
}
$form['add']['name'] = array(
'#type' => 'textfield',
'#title' => t('Add new source'),
'#id' => 'edit-sources-add-name',
'#description' => t('Name'),
'#size' => 20,
);
$form['add']['machine_name'] = array(
'#title' => t('Machine name'),
'#title_display' => 'invisible',
'#type' => 'machine_name',
'#machine_name' => array(
'exists' => 'feeds_ex_source_exists',
'source' => array(
'add',
'name',
),
'standalone' => TRUE,
'label' => '',
),
'#field_prefix' => '<span dir="ltr">',
'#field_suffix' => '</span>‎',
'#feeds_importer' => $this->id,
'#required' => FALSE,
'#maxlength' => 32,
'#size' => 15,
'#description' => t('A unique machine-readable name containing letters, numbers, and underscores.'),
);
$form['add']['value'] = array(
'#type' => 'textfield',
'#description' => t('Value'),
'#title' => ' ',
'#size' => 50,
);
foreach ($this
->configFormTableHeader() as $column => $name) {
$form['add'][$column] = $this
->configFormTableColumn($form_state, $column, array());
}
$form['add']['debug'] = array(
'#type' => 'checkbox',
'#title' => t('Debug'),
'#title_display' => 'invisible',
);
$form['add']['weight'] = array(
'#type' => 'textfield',
'#default_value' => ++$max_weight,
'#size' => 3,
'#attributes' => array(
'class' => array(
'feeds-ex-source-weight',
),
),
);
$form['display_errors'] = array(
'#type' => 'checkbox',
'#title' => t('Display errors'),
'#description' => t('Display all error messages after parsing. Fatal errors will always be displayed.'),
'#default_value' => $this->config['display_errors'],
);
$form['debug_mode'] = array(
'#type' => 'checkbox',
'#title' => t('Enable debug mode'),
'#description' => t('Displays the configuration form on the feed source page to ease figuring out the expressions. Any values entered on that page will be saved here.'),
'#default_value' => $this->config['debug_mode'],
);
if ($this->isMultibyte) {
$args = array(
'%encodings' => implode(', ', mb_detect_order()),
);
$form['source_encoding'] = array(
'#type' => 'textfield',
'#title' => t('Source encoding'),
'#description' => t('The possible encodings of the source files. auto: %encodings', $args),
'#default_value' => implode(', ', $this->config['source_encoding']),
'#autocomplete_path' => '_feeds_ex/encoding_autocomplete',
);
}
$form['#attached']['drupal_add_tabledrag'][] = array(
'feeds-ex-source-table',
'order',
'sibling',
'feeds-ex-source-weight',
);
$form['#attached']['css'][] = drupal_get_path('module', 'feeds_ex') . '/feeds_ex.css';
$form['#header'] = $this
->getFormHeader();
return $form;
}
public function configFormValidate(&$values) {
try {
$this
->loadLibrary();
} catch (RuntimeException $e) {
$this
->getMessenger()
->setMessage($e
->getMessage(), 'error', FALSE);
return;
}
$values += $this
->configDefaults();
foreach ($values['sources'] as $machine_name => $source) {
if (!empty($source['remove'])) {
unset($values['sources'][$machine_name]);
}
}
if ($this
->hasConfigurableContext()) {
if ($message = $this
->validateExpression($values['context']['value'])) {
form_set_error('context', $message);
}
}
foreach (array_keys($values['sources']) as $machine_name) {
if ($message = $this
->validateExpression($values['sources'][$machine_name]['value'])) {
form_set_error('sources][' . $machine_name . '][value', $message);
}
}
if (strlen($values['add']['machine_name']) && strlen($values['add']['name'])) {
if ($message = $this
->validateExpression($values['add']['value'])) {
form_set_error('add][value', $message);
}
else {
$values['sources'][$values['add']['machine_name']] = $values['add'];
}
}
$columns = $this
->getFormHeader();
unset($columns['remove'], $columns['machine_name']);
$columns = array_keys($columns);
foreach ($values['sources'] as $machine_name => $source) {
$new_value = array();
foreach ($columns as $column) {
$new_value[$column] = $source[$column];
}
$values['sources'][$machine_name] = $new_value;
}
uasort($values['sources'], 'ctools_plugin_sort');
if (!$this->isMultibyte) {
return;
}
$encodings = array_map('drupal_strtolower', array_map('trim', explode(',', $values['source_encoding'])));
$values['source_encoding'] = array();
foreach (mb_list_encodings() as $encoding) {
$pos = array_search(drupal_strtolower($encoding), $encodings);
if ($pos !== FALSE) {
$values['source_encoding'][$pos] = $encoding;
}
}
ksort($values['source_encoding']);
if (!$values['source_encoding']) {
$values['source_encoding'][] = 'auto';
}
}
public function hasConfigForm() {
return TRUE;
}
public function sourceDefaults() {
return array();
}
public function sourceForm($source_config) {
if (!$this
->hasSourceConfig()) {
return array();
}
$form_state = array();
$form = $this
->configForm($form_state);
$form['add']['machine_name']['#machine_name']['source'] = array(
'feeds',
get_class($this),
'add',
'name',
);
return $form;
}
public function sourceFormValidate(&$source_config) {
$this
->configFormValidate($source_config);
}
public function sourceSave(FeedsSource $source) {
$config = $source
->getConfigFor($this);
$source
->setConfigFor($this, array());
if ($this
->hasSourceConfig() && $config) {
$this
->setConfig($config);
$this
->save();
}
}
public function hasSourceConfig() {
return !empty($this->config['debug_mode']);
}
protected function getFormHeader() {
$header = array(
'name' => t('Name'),
'machine_name' => t('Machine name'),
'value' => t('Value'),
);
$header += $this
->configFormTableHeader();
$header += array(
'debug' => t('Debug'),
'remove' => t('Remove'),
'weight' => t('Weight'),
);
return $header;
}
public function setMultibyte($is_multibyte) {
$this->isMultibyte = (bool) $is_multibyte;
}
public function setMessenger(FeedsExMessengerInterface $messenger) {
$this->messenger = $messenger;
}
public function getMessenger() {
if (!isset($this->messenger)) {
$this->messenger = new FeedsExMessenger();
}
return $this->messenger;
}
}
interface FeedsExMessengerInterface {
public function setMessage($message = NULL, $type = 'status', $repeat = TRUE);
}
class FeedsExMessenger implements FeedsExMessengerInterface {
public function setMessage($message = NULL, $type = 'status', $repeat = TRUE) {
drupal_set_message($message, $type, $repeat);
}
}
class FeedsExTestMessenger implements FeedsExMessengerInterface {
protected $messages = array();
public function setMessage($message = NULL, $type = 'status', $repeat = TRUE) {
$this->messages[] = array(
'message' => $message,
'type' => $type,
'repeat' => $repeat,
);
}
public function getMessages() {
return $this->messages;
}
}
class FeedsExEmptyException extends RuntimeException {
}