Projet

Général

Profil

Révision 0b4524f6

Ajouté par Assos Assos il y a presque 9 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/feeds_xpathparser/FeedsXPathParserBase.inc
10 10
 */
11 11
abstract class FeedsXPathParserBase extends FeedsParser {
12 12

  
13
  /**
14
   * The DOMDocument used for parsing.
15
   *
16
   * @var DOMDocument
17
   */
18
  protected $doc;
19

  
20
  /**
21
   * The return value of libxml_disable_entity_loader().
22
   *
23
   * @var bool
24
   */
25
  protected $loader;
26

  
27
  /**
28
   * The elements that should be displayed in raw XML.
29
   *
30
   * @var array
31
   */
13 32
  protected $rawXML = array();
14
  protected $doc = NULL;
15
  protected $xpath = NULL;
33

  
34
  /**
35
   * The DOMXPath objet used for parsing.
36
   *
37
   * @var DOMXPath
38
   */
39
  protected $xpath;
16 40

  
17 41
  /**
18 42
   * Classes that use FeedsXPathParserBase must implement this.
......
82 106

  
83 107
    $all_nodes = $this->xpath->namespacedQuery($context_query, NULL, 'context');
84 108

  
109
    // The source config could have old values that don't exist in the importer.
110
    $sources = array_intersect_key($source_config['sources'], $mappings);
111

  
85 112
    foreach ($all_nodes as $node) {
86 113
      // Invoke a hook to check whether the domnode should be skipped.
87 114
      if (in_array(TRUE, module_invoke_all('feeds_xpathparser_filter_domnode', $node, $this->doc, $source), TRUE)) {
......
89 116
      }
90 117

  
91 118
      $parsed_item = $variables = array();
92
      foreach ($source_config['sources'] as $element_key => $query) {
119
      foreach ($sources as $element_key => $query) {
93 120
        // Variable substitution.
94 121
        $query = strtr($query, $variables);
95 122
        // Parse the item.
96 123
        $result = $this->parseSourceElement($query, $node, $element_key);
97 124
        if (isset($result)) {
98
          if (!is_array($result)) {
99
            $variables['$' . $mappings[$element_key]] = $result;
100
          }
101
          else {
102
            $variables['$' . $mappings[$element_key]] = '';
103
          }
125
          $variables['$' . $mappings[$element_key]] = is_array($result) ? reset($result) : $result;
104 126
          $parsed_item[$element_key] = $result;
105 127
        }
106 128
      }
......
532 554
   */
533 555
  protected function errorStart() {
534 556
    libxml_clear_errors();
557
    if (function_exists('libxml_disable_entity_loader')) {
558
      $this->loader = libxml_disable_entity_loader(TRUE);
559
    }
560

  
535 561
    return libxml_use_internal_errors(TRUE);
536 562
  }
537 563

  
......
567 593
    }
568 594
    libxml_clear_errors();
569 595
    libxml_use_internal_errors($use);
596

  
597
    if (function_exists('libxml_disable_entity_loader') && isset($this->loader)) {
598
      libxml_disable_entity_loader($this->loader);
599
      unset($this->loader);
600
    }
570 601
  }
571 602

  
572 603
  /**
drupal7/sites/all/modules/feeds_xpathparser/FeedsXPathParserDOMXPath.inc
36 36
   *
37 37
   * @var arrray
38 38
   */
39
  protected $namepsaces = array();
39
  protected $namespaces = array();
40 40

  
41 41
  /**
42 42
   * The most recent error from parsing.
drupal7/sites/all/modules/feeds_xpathparser/FeedsXPathParserHTML.inc
10 10
 */
11 11
class FeedsXPathParserHTML extends FeedsXPathParserBase {
12 12

  
13
  /**
14
   * Whether this version of PHP has a useable saveHTML() method.
15
   *
16
   * @var bool
17
   */
13 18
  protected $hasSaveHTML = FALSE;
14 19

  
15 20
  /**
16
   * Overrides parent::__construct().
21
   * {@inheritdoc}
17 22
   */
18 23
  public function __construct($id) {
19 24
    parent::__construct($id);
......
25 30
  }
26 31

  
27 32
  /**
28
   * Implements FeedsXPathParserBase::setup().
33
   * {@inheritdoc}
29 34
   */
30 35
  protected function setup($source_config, FeedsFetcherResult $fetcher_result) {
31

  
32 36
    if (!empty($source_config['exp']['tidy']) && extension_loaded('tidy')) {
33 37
      $config = array(
34 38
        'merge-divs'       => FALSE,
......
42 46
      );
43 47
      // Default tidy encoding is UTF8.
44 48
      $encoding = $source_config['exp']['tidy_encoding'];
45
      $raw = tidy_repair_string(trim($fetcher_result->getRaw()), $config, $encoding);
49
      $raw = tidy_repair_string($fetcher_result->getRaw(), $config, $encoding);
46 50
    }
47 51
    else {
48 52
      $raw = $fetcher_result->getRaw();
49 53
    }
50
    $doc = new DOMDocument();
54

  
55
    $document = new DOMDocument();
56
    $document->strictErrorChecking = FALSE;
57
    $document->recover = TRUE;
58

  
51 59
    // Use our own error handling.
52 60
    $use = $this->errorStart();
53
    $success = $doc->loadHTML($raw);
54
    unset($raw);
61

  
62
    if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
63
      $options = LIBXML_NONET;
64
      $options |= defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0;
65
      $options |= defined('LIBXML_PARSEHUGE') ? LIBXML_PARSEHUGE : 0;
66

  
67
      $success = $document->loadHTML($raw, $options);
68
    }
69
    else {
70
      $success = $document->loadHTML($raw);
71
    }
72

  
55 73
    $this->errorStop($use, $source_config['exp']['errors']);
74

  
56 75
    if (!$success) {
57 76
      throw new Exception(t('There was an error parsing the HTML document.'));
58 77
    }
59
    return $doc;
78

  
79
    return $document;
60 80
  }
61 81

  
82
  /**
83
   * {@inheritdoc}
84
   */
62 85
  protected function getRaw(DOMNode $node) {
63 86
    if ($this->hasSaveHTML) {
64 87
      return $this->doc->saveHTML($node);
......
66 89

  
67 90
    return $this->doc->saveXML($node, LIBXML_NOEMPTYTAG);
68 91
  }
92

  
69 93
}
drupal7/sites/all/modules/feeds_xpathparser/FeedsXPathParserXML.inc
11 11
class FeedsXPathParserXML extends FeedsXPathParserBase {
12 12

  
13 13
  /**
14
   * Implements FeedsXPathParserBase::setup().
14
   * {@inheritdoc}
15 15
   */
16 16
  protected function setup($source_config, FeedsFetcherResult $fetcher_result) {
17

  
18 17
    if (!empty($source_config['exp']['tidy']) && extension_loaded('tidy')) {
19 18
      $config = array(
20 19
        'input-xml' => TRUE,
......
23 22
      );
24 23
      // Default tidy encoding is UTF8.
25 24
      $encoding = $source_config['exp']['tidy_encoding'];
26
      $raw = tidy_repair_string(trim($fetcher_result->getRaw()), $config, $encoding);
25
      $raw = tidy_repair_string($fetcher_result->getRaw(), $config, $encoding);
27 26
    }
28 27
    else {
29 28
      $raw = $fetcher_result->getRaw();
30 29
    }
31
    $doc = new DOMDocument();
30

  
31
    $options = LIBXML_NONET;
32
    $options |= defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0;
33
    $options |= defined('LIBXML_PARSEHUGE') ? LIBXML_PARSEHUGE : 0;
34

  
35
    $document = new DOMDocument();
36
    $document->strictErrorChecking = FALSE;
37
    $document->recover = TRUE;
38

  
32 39
    $use = $this->errorStart();
33
    $success = $doc->loadXML($raw);
34
    unset($raw);
40

  
41
    $success = $document->loadXML($raw, $options);
42

  
35 43
    $this->errorStop($use, $source_config['exp']['errors']);
44

  
36 45
    if (!$success) {
37 46
      throw new Exception(t('There was an error parsing the XML document.'));
38 47
    }
39
    return $doc;
48

  
49
    return $document;
40 50
  }
41 51

  
52
  /**
53
   * {@inheritdoc}
54
   */
42 55
  protected function getRaw(DOMNode $node) {
43 56
    return $this->doc->saveXML($node);
44 57
  }
58

  
45 59
}
drupal7/sites/all/modules/feeds_xpathparser/feeds_xpathparser.info
14 14
files[] = tests/feeds_xpathparser_query_parser.test
15 15
core = 7.x
16 16

  
17
; Information added by Drupal.org packaging script on 2014-12-11
18
version = "7.x-1.0"
17
; Information added by Drupal.org packaging script on 2015-07-03
18
version = "7.x-1.1"
19 19
core = "7.x"
20 20
project = "feeds_xpathparser"
21
datestamp = "1418270885"
21
datestamp = "1435895044"
22 22

  

Formats disponibles : Unified diff