Révision f066bdb5
Ajouté par Assos Assos il y a plus de 9 ans
drupal7/sites/all/modules/feeds_xpathparser/FeedsXPathParserDOMXPath.inc | ||
---|---|---|
9 | 9 |
* Wraps DOMXPath providing enhanced debugging and special namespace handling. |
10 | 10 |
*/ |
11 | 11 |
class FeedsXPathParserDOMXPath extends DOMXPath { |
12 |
|
|
13 |
/** |
|
14 |
* The DOMDocument to parse. |
|
15 |
* |
|
16 |
* @var DOMDocument |
|
17 |
*/ |
|
18 |
protected $doc; |
|
19 |
|
|
20 |
/** |
|
21 |
* Configuration array. |
|
22 |
* |
|
23 |
* @var array |
|
24 |
*/ |
|
12 | 25 |
protected $config = array(); |
26 |
|
|
27 |
/** |
|
28 |
* Modified query cache. |
|
29 |
* |
|
30 |
* @var arrray |
|
31 |
*/ |
|
13 | 32 |
protected $modifiedQueries = array(); |
14 | 33 |
|
15 | 34 |
/** |
16 |
* Constructs a FeedsXPathParserDOMXPath. |
|
35 |
* The namespaces in the document. |
|
36 |
* |
|
37 |
* @var arrray |
|
38 |
*/ |
|
39 |
protected $namepsaces = array(); |
|
40 |
|
|
41 |
/** |
|
42 |
* The most recent error from parsing. |
|
43 |
* |
|
44 |
* @var stdClass |
|
45 |
*/ |
|
46 |
protected $error; |
|
47 |
|
|
48 |
/** |
|
49 |
* Constructs a FeedsXPathParserDOMXPath object. |
|
17 | 50 |
* |
18 | 51 |
* @param DOMDocument $doc |
19 | 52 |
* The DOMDocument that we're operating on. |
20 | 53 |
*/ |
21 | 54 |
public function __construct(DOMDocument $doc) { |
22 |
$this->namespaces = array(); |
|
55 |
|
|
23 | 56 |
$simple = simplexml_import_dom($doc); |
57 |
|
|
24 | 58 |
// An empty DOMDocument will make $simple NULL. |
25 | 59 |
if ($simple !== NULL) { |
26 | 60 |
$this->namespaces = $simple->getNamespaces(TRUE); |
27 | 61 |
} |
28 | 62 |
$this->doc = $doc; |
63 |
|
|
29 | 64 |
parent::__construct($doc); |
30 | 65 |
} |
31 | 66 |
|
... | ... | |
46 | 81 |
* The result of an XPath query. Either a scalar or a DOMNodeList. |
47 | 82 |
* @param string $source |
48 | 83 |
* The source key that produced this query. |
84 |
* |
|
85 |
* @todo Use theme_item_list(). |
|
49 | 86 |
*/ |
50 | 87 |
protected function debug($data, $source) { |
51 | 88 |
$output = "$source : <ul>"; |
89 |
|
|
52 | 90 |
if ($data instanceof DOMNodeList) { |
53 | 91 |
foreach ($data as $node) { |
54 | 92 |
$output .= '<li>' . check_plain($this->doc->saveXML($node)) . '</li>'; |
... | ... | |
58 | 96 |
$output .= '<li>' . check_plain($data) . '</li>'; |
59 | 97 |
} |
60 | 98 |
$output .= '</ul>'; |
99 |
|
|
61 | 100 |
drupal_set_message($output); |
62 | 101 |
} |
63 | 102 |
|
... | ... | |
76 | 115 |
*/ |
77 | 116 |
public function namespacedQuery($query, $context, $source) { |
78 | 117 |
$this->addDefaultNamespace($query); |
118 |
|
|
79 | 119 |
$results = $this->executeQuery($query, $context); |
120 |
|
|
80 | 121 |
if (in_array($source, $this->config['debug'])) { |
81 | 122 |
$this->debug($results, $source); |
82 | 123 |
} |
... | ... | |
106 | 147 |
} |
107 | 148 |
|
108 | 149 |
// DOMXPath::evaluate() and DOMXPath::query() will return FALSE on error or |
109 |
// if the value is false. We check error result and return NULL in case
|
|
150 |
// if the value is FALSE. We check error result and return NULL in case
|
|
110 | 151 |
// of error. |
111 | 152 |
if (is_object($this->error) && $this->error->level == LIBXML_ERR_ERROR) { |
112 | 153 |
return NULL; |
... | ... | |
146 | 187 |
/** |
147 | 188 |
* Performs a XPath query. |
148 | 189 |
* |
149 |
* Here we set libxml_use_internal_errors to TRUE because depending on the |
|
190 |
* Here we set libxml_use_internal_errors() to TRUE because depending on the
|
|
150 | 191 |
* libxml version, $xml->xpath() might return FALSE or an empty array() when |
151 | 192 |
* a query doesn't match. |
152 | 193 |
* |
153 | 194 |
* @param string $query |
154 | 195 |
* The XPath query string. |
155 | 196 |
* @param DOMNode $context |
156 |
* (Optional) A context object. Defaults to NULL.
|
|
197 |
* (optional) A context object. Defaults to NULL.
|
|
157 | 198 |
* |
158 | 199 |
* @return mixed |
159 | 200 |
* The result of the XPath query. |
160 | 201 |
*/ |
161 |
protected function executeQuery($query, $context = NULL) { |
|
202 |
protected function executeQuery($query, DOMNode $context = NULL) {
|
|
162 | 203 |
$use_errors = libxml_use_internal_errors(TRUE); |
163 | 204 |
|
164 | 205 |
// Perfom XPath query. |
Formats disponibles : Unified diff
Weekly update of contrib modules