class FeedsExXml in Feeds extensible parsers 7
Same name and namespace in other branches
- 7.2 src/FeedsExXml.inc \FeedsExXml
Parses XML documents with XPath.
Hierarchy
- class \FeedsExBase extends \FeedsParser
- class \FeedsExXml
Expanded class hierarchy of FeedsExXml
2 string references to 'FeedsExXml'
- FeedsExXml.test in src/
Tests/ FeedsExXml.test - feeds_ex_feeds_plugins in ./
feeds_ex.feeds.inc - Implements hook_feeds_plugins().
File
- src/
FeedsExXml.inc, line 11 - Contains FeedsExXml.
View source
class FeedsExXml extends FeedsExBase {
/**
* The FeedsExXpathDomXpath object used for parsing.
*
* @var FeedsExXpathDomXpath
*/
protected $xpath;
/**
* The previous value for XML error handling.
*
* @var bool
*/
protected $handleXmlErrors;
/**
* The previous value for the entity loader.
*
* @var bool
*/
protected $entityLoader;
/**
* {@inheritdoc}
*/
protected $encoderClass = 'FeedsExXmlEncoder';
/**
* {@inheritdoc}
*/
protected function setUp(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
$document = $this
->prepareDocument($source, $fetcher_result);
$this->xpath = new FeedsExXpathDomXpath($document);
}
/**
* {@inheritdoc}
*/
protected function cleanUp(FeedsSource $source, FeedsParserResult $result) {
// Try to free up some memory. There shouldn't be any other references to
// $this->xpath or the DOMDocument.
unset($this->xpath);
// Calculate progress.
$state = $source
->state(FEEDS_PARSE);
$state
->progress($state->total, $state->pointer);
}
/**
* {@inheritdoc}
*/
protected function executeContext(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
$state = $source
->state(FEEDS_PARSE);
if (!$state->total) {
$state->total = $this->xpath
->evaluate('count(' . $this->config['context']['value'] . ')');
}
$start = (int) $state->pointer;
$state->pointer = $start + $source->importer
->getLimit();
// A batched XPath expression.
$context_query = '(' . $this->config['context']['value'] . ")[position() > {$start} and position() <= {$state->pointer}]";
return $this->xpath
->query($context_query);
}
/**
* {@inheritdoc}
*/
protected function executeSourceExpression($machine_name, $expression, $row) {
$result = $this->xpath
->evaluate($expression, $row);
if (!$result instanceof DOMNodeList) {
return $result;
}
if ($result->length == 0) {
return;
}
$return = array();
if (!empty($this->config['sources'][$machine_name]['inner'])) {
foreach ($result as $node) {
$return[] = $this
->getInnerXml($node);
}
}
elseif (!empty($this->config['sources'][$machine_name]['raw'])) {
foreach ($result as $node) {
$return[] = $this
->getRaw($node);
}
}
else {
foreach ($result as $node) {
$return[] = $node->nodeValue;
}
}
// Return a single value if there's only one value.
return count($return) === 1 ? reset($return) : $return;
}
/**
* {@inheritdoc}
*/
public function configDefaults() {
return array(
'use_tidy' => FALSE,
) + parent::configDefaults();
}
/**
* {@inheritdoc}
*/
public function configForm(&$form_state) {
$form = parent::configForm($form_state);
if (extension_loaded('tidy')) {
$form['use_tidy'] = array(
'#type' => 'checkbox',
'#title' => t('Use tidy'),
'#description' => t('The <a href="http://php.net/manual/en/book.tidy.php">Tidy PHP</a> extension has been detected. Select this to clean the markup before parsing.'),
'#default_value' => $this->config['use_tidy'],
);
}
return $form;
}
/**
* {@inheritdoc}
*/
protected function configFormTableHeader() {
return array(
'raw' => t('Raw'),
'inner' => t('Inner XML'),
);
}
/**
* {@inheritdoc}
*/
protected function configFormTableColumn(array &$form_state, array $values, $column, $machine_name) {
$id = 'feeds-ex-xml-raw-' . check_plain($machine_name);
switch ($column) {
case 'raw':
return array(
'#type' => 'checkbox',
'#title' => t('Raw value'),
'#title_display' => 'invisible',
'#default_value' => (int) (!empty($values['raw'])),
'#id' => $id,
);
case 'inner':
return array(
'#type' => 'checkbox',
'#title' => t('Inner XML'),
'#title_display' => 'invisible',
'#default_value' => (int) (!empty($values['inner'])),
'#states' => array(
'visible' => array(
'#' . $id => array(
'checked' => TRUE,
),
),
),
);
}
}
/**
* {@inheritdoc}
*/
protected function validateExpression(&$expression) {
$expression = trim($expression);
$message = NULL;
if (!$expression) {
return $message;
}
$this
->startErrorHandling();
$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<items></items>");
$xml
->xpath($expression);
if ($error = libxml_get_last_error()) {
// Our variable substitution options can cause syntax errors, check if
// we're doing that.
if ($error->code == 1207 && strpos($expression, '$') !== FALSE) {
// Do nothing.
}
elseif ($error->code != 1219) {
$message = check_plain(trim($error->message));
}
}
$this
->stopErrorHandling();
return $message;
}
/**
* {@inheritdoc}
*/
public function configFormValidate(&$values) {
parent::configFormValidate($values);
// Remove values that are inner but not raw.
foreach ($values['sources'] as $machine_name => $source) {
if (!empty($source['inner']) && empty($source['raw'])) {
unset($values['sources'][$machine_name]['inner']);
}
}
}
/**
* Prepares the DOM document.
*
* @param FeedsSource $source
* The feed source.
* @param FeedsFetcherResult $fetcher_result
* The fetcher result.
*
* @return DOMDocument
* The DOM document.
*/
protected function prepareDocument(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
$raw = $this
->prepareRaw($fetcher_result);
// Remove default namespaces. This has to run after the encoding conversion
// because a limited set of encodings are supported in regular expressions.
$raw = FeedsExXmlUtility::removeDefaultNamespaces($raw);
if ($this->config['use_tidy'] && extension_loaded('tidy')) {
$raw = tidy_repair_string($raw, $this
->getTidyConfig(), 'utf8');
}
$raw = FeedsExXmlUtility::decodeNamedHtmlEntities($raw);
return FeedsExXmlUtility::createXmlDocument($raw);
}
/**
* Returns the raw XML of a DOM node.
*
* @param DOMNode $node
* The node to convert to raw XML.
*
* @return string
* The raw XML.
*/
protected function getRaw(DOMNode $node) {
return $node->ownerDocument
->saveXML($node);
}
/**
* Returns the inner XML of a DOM node.
*
* @param DOMNode $node
* The node to convert to raw XML.
*
* @return string
* The inner XML.
*/
protected function getInnerXml(DOMNode $node) {
$buffer = '';
foreach ($node->childNodes as $child) {
$buffer .= $this
->getRaw($child);
}
return $buffer;
}
/**
* {@inheritdoc}
*/
protected function startErrorHandling() {
parent::startErrorHandling();
libxml_clear_errors();
$this->handleXmlErrors = libxml_use_internal_errors(TRUE);
// Only available in PHP >= 5.2.11.
// See http://symfony.com/blog/security-release-symfony-2-0-17-released for
// details.
if (function_exists('libxml_disable_entity_loader')) {
$this->entityLoader = libxml_disable_entity_loader(TRUE);
}
}
/**
* {@inheritdoc}
*/
protected function stopErrorHandling() {
parent::stopErrorHandling();
libxml_clear_errors();
libxml_use_internal_errors($this->handleXmlErrors);
if (function_exists('libxml_disable_entity_loader')) {
libxml_disable_entity_loader($this->entityLoader);
}
}
/**
* {@inheritdoc}
*/
protected function getErrors() {
$return = array();
foreach (libxml_get_errors() as $error) {
// Translate error values.
switch ($error->level) {
case LIBXML_ERR_FATAL:
$severity = WATCHDOG_ERROR;
break;
case LIBXML_ERR_ERROR:
$severity = WATCHDOG_WARNING;
break;
default:
$severity = WATCHDOG_NOTICE;
break;
}
$return[] = array(
'message' => '%error on line %num. Error code: %code',
'variables' => array(
'%error' => trim($error->message),
'%num' => $error->line,
'%code' => $error->code,
),
'severity' => $severity,
);
}
return $return;
}
/**
* Returns the options for phptidy.
*
* @see http://php.net/manual/en/book.tidy.php
* @see tidy_repair_string()
*
* @return array
* The configuration array.
*/
protected function getTidyConfig() {
return array(
'input-xml' => TRUE,
'output-xml' => TRUE,
'add-xml-decl' => TRUE,
'wrap' => 0,
'tidy-mark' => FALSE,
);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FeedsExBase:: |
protected | property | The encoder used to convert encodings. | |
FeedsExBase:: |
protected | property | The object used to display messages to the user. | |
FeedsExBase:: |
protected | function | Renders our debug messages into a list. | |
FeedsExBase:: |
protected | function | Executes the source expressions. | |
FeedsExBase:: |
public | function | Returns the encoder. | |
FeedsExBase:: |
protected | function | Returns the configuration form table header. | |
FeedsExBase:: |
public | function | ||
FeedsExBase:: |
public | function | Returns the messenger. | |
FeedsExBase:: |
public | function | ||
FeedsExBase:: |
protected | function | Returns whether or not this parser uses a context query. | 2 |
FeedsExBase:: |
public | function | ||
FeedsExBase:: |
protected | function | Loads the necessary library. | 2 |
FeedsExBase:: |
protected | function | Logs errors. | |
FeedsExBase:: |
public | function | ||
FeedsExBase:: |
protected | function | Performs the actual parsing. | 2 |
FeedsExBase:: |
protected | function | Prepares the expressions for parsing. | |
FeedsExBase:: |
protected | function | Prepares the raw string for parsing. | |
FeedsExBase:: |
protected | function | Prepares the variable map used to substitution. | |
FeedsExBase:: |
protected | function | Prints errors to the screen. | |
FeedsExBase:: |
public | function | Sets the encoder. | |
FeedsExBase:: |
public | function | Sets the messenger to be used to display messages. | |
FeedsExBase:: |
public | function | ||
FeedsExBase:: |
public | function | ||
FeedsExBase:: |
public | function | ||
FeedsExBase:: |
public | function | ||
FeedsExXml:: |
protected | property |
The class used as the text encoder. Overrides FeedsExBase:: |
2 |
FeedsExXml:: |
protected | property | The previous value for the entity loader. | |
FeedsExXml:: |
protected | property | The previous value for XML error handling. | |
FeedsExXml:: |
protected | property | The FeedsExXpathDomXpath object used for parsing. | |
FeedsExXml:: |
protected | function |
Allows subclasses to cleanup after parsing. Overrides FeedsExBase:: |
|
FeedsExXml:: |
public | function |
Overrides FeedsExBase:: |
|
FeedsExXml:: |
public | function |
Overrides FeedsExBase:: |
|
FeedsExXml:: |
protected | function |
Returns a form element for a specific column. Overrides FeedsExBase:: |
1 |
FeedsExXml:: |
protected | function |
Reuturns the list of table headers. Overrides FeedsExBase:: |
1 |
FeedsExXml:: |
public | function |
Overrides FeedsExBase:: |
|
FeedsExXml:: |
protected | function |
Returns rows to be parsed. Overrides FeedsExBase:: |
1 |
FeedsExXml:: |
protected | function |
Executes a single source expression. Overrides FeedsExBase:: |
1 |
FeedsExXml:: |
protected | function |
Returns the errors after parsing. Overrides FeedsExBase:: |
|
FeedsExXml:: |
protected | function | Returns the inner XML of a DOM node. | |
FeedsExXml:: |
protected | function | Returns the raw XML of a DOM node. | 1 |
FeedsExXml:: |
protected | function | Returns the options for phptidy. | 2 |
FeedsExXml:: |
protected | function | Prepares the DOM document. | 2 |
FeedsExXml:: |
protected | function |
Allows subclasses to prepare for parsing. Overrides FeedsExBase:: |
1 |
FeedsExXml:: |
protected | function |
Starts internal error handling. Overrides FeedsExBase:: |
|
FeedsExXml:: |
protected | function |
Stops internal error handling. Overrides FeedsExBase:: |
|
FeedsExXml:: |
protected | function |
Validates an expression. Overrides FeedsExBase:: |
1 |