Query.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Dom
  17. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Dom_Query_Css2Xpath
  23. */
  24. require_once 'Zend/Dom/Query/Css2Xpath.php';
  25. /**
  26. * @see Zend_Dom_Query_Result
  27. */
  28. require_once 'Zend/Dom/Query/Result.php';
  29. /** @see Zend_Xml_Security */
  30. require_once 'Zend/Xml/Security.php';
  31. /** @see Zend_Xml_Exception */
  32. require_once 'Zend/Xml/Exception.php';
  33. /**
  34. * Query DOM structures based on CSS selectors and/or XPath
  35. *
  36. * @package Zend_Dom
  37. * @subpackage Query
  38. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_Dom_Query
  42. {
  43. /**#@+
  44. * Document types
  45. */
  46. const DOC_XML = 'docXml';
  47. const DOC_HTML = 'docHtml';
  48. const DOC_XHTML = 'docXhtml';
  49. /**#@-*/
  50. /**
  51. * @var string
  52. */
  53. protected $_document;
  54. /**
  55. * DOMDocument errors, if any
  56. * @var false|array
  57. */
  58. protected $_documentErrors = false;
  59. /**
  60. * Document type
  61. * @var string
  62. */
  63. protected $_docType;
  64. /**
  65. * Document encoding
  66. * @var null|string
  67. */
  68. protected $_encoding;
  69. /**
  70. * XPath namespaces
  71. * @var array
  72. */
  73. protected $_xpathNamespaces = array();
  74. /**
  75. * Constructor
  76. *
  77. * @param null|string $document
  78. * @return void
  79. */
  80. public function __construct($document = null, $encoding = null)
  81. {
  82. $this->setEncoding($encoding);
  83. $this->setDocument($document);
  84. }
  85. /**
  86. * Set document encoding
  87. *
  88. * @param string $encoding
  89. * @return Zend_Dom_Query
  90. */
  91. public function setEncoding($encoding)
  92. {
  93. $this->_encoding = (null === $encoding) ? null : (string) $encoding;
  94. return $this;
  95. }
  96. /**
  97. * Get document encoding
  98. *
  99. * @return null|string
  100. */
  101. public function getEncoding()
  102. {
  103. return $this->_encoding;
  104. }
  105. /**
  106. * Set document to query
  107. *
  108. * @param string $document
  109. * @param null|string $encoding Document encoding
  110. * @return Zend_Dom_Query
  111. */
  112. public function setDocument($document, $encoding = null)
  113. {
  114. if (0 === strlen($document)) {
  115. return $this;
  116. }
  117. // breaking XML declaration to make syntax highlighting work
  118. if ('<' . '?xml' == substr(trim($document), 0, 5)) {
  119. if (preg_match('/<html[^>]*xmlns="([^"]+)"[^>]*>/i', $document, $matches)) {
  120. $this->_xpathNamespaces[] = $matches[1];
  121. return $this->setDocumentXhtml($document, $encoding);
  122. }
  123. return $this->setDocumentXml($document, $encoding);
  124. }
  125. if (strstr($document, 'DTD XHTML')) {
  126. return $this->setDocumentXhtml($document, $encoding);
  127. }
  128. return $this->setDocumentHtml($document, $encoding);
  129. }
  130. /**
  131. * Register HTML document
  132. *
  133. * @param string $document
  134. * @param null|string $encoding Document encoding
  135. * @return Zend_Dom_Query
  136. */
  137. public function setDocumentHtml($document, $encoding = null)
  138. {
  139. $this->_document = (string) $document;
  140. $this->_docType = self::DOC_HTML;
  141. if (null !== $encoding) {
  142. $this->setEncoding($encoding);
  143. }
  144. return $this;
  145. }
  146. /**
  147. * Register XHTML document
  148. *
  149. * @param string $document
  150. * @param null|string $encoding Document encoding
  151. * @return Zend_Dom_Query
  152. */
  153. public function setDocumentXhtml($document, $encoding = null)
  154. {
  155. $this->_document = (string) $document;
  156. $this->_docType = self::DOC_XHTML;
  157. if (null !== $encoding) {
  158. $this->setEncoding($encoding);
  159. }
  160. return $this;
  161. }
  162. /**
  163. * Register XML document
  164. *
  165. * @param string $document
  166. * @param null|string $encoding Document encoding
  167. * @return Zend_Dom_Query
  168. */
  169. public function setDocumentXml($document, $encoding = null)
  170. {
  171. $this->_document = (string) $document;
  172. $this->_docType = self::DOC_XML;
  173. if (null !== $encoding) {
  174. $this->setEncoding($encoding);
  175. }
  176. return $this;
  177. }
  178. /**
  179. * Retrieve current document
  180. *
  181. * @return string
  182. */
  183. public function getDocument()
  184. {
  185. return $this->_document;
  186. }
  187. /**
  188. * Get document type
  189. *
  190. * @return string
  191. */
  192. public function getDocumentType()
  193. {
  194. return $this->_docType;
  195. }
  196. /**
  197. * Get any DOMDocument errors found
  198. *
  199. * @return false|array
  200. */
  201. public function getDocumentErrors()
  202. {
  203. return $this->_documentErrors;
  204. }
  205. /**
  206. * Perform a CSS selector query
  207. *
  208. * @param string $query
  209. * @return Zend_Dom_Query_Result
  210. */
  211. public function query($query)
  212. {
  213. $xpathQuery = Zend_Dom_Query_Css2Xpath::transform($query);
  214. return $this->queryXpath($xpathQuery, $query);
  215. }
  216. /**
  217. * Perform an XPath query
  218. *
  219. * @param string|array $xpathQuery
  220. * @param string $query CSS selector query
  221. * @return Zend_Dom_Query_Result
  222. */
  223. public function queryXpath($xpathQuery, $query = null)
  224. {
  225. if (null === ($document = $this->getDocument())) {
  226. require_once 'Zend/Dom/Exception.php';
  227. throw new Zend_Dom_Exception('Cannot query; no document registered');
  228. }
  229. $encoding = $this->getEncoding();
  230. libxml_use_internal_errors(true);
  231. if (null === $encoding) {
  232. $domDoc = new DOMDocument('1.0');
  233. } else {
  234. $domDoc = new DOMDocument('1.0', $encoding);
  235. }
  236. $type = $this->getDocumentType();
  237. switch ($type) {
  238. case self::DOC_XML:
  239. try {
  240. $domDoc = Zend_Xml_Security::scan($document, $domDoc);
  241. $success = ($domDoc !== false);
  242. } catch (Zend_Xml_Exception $e) {
  243. require_once 'Zend/Dom/Exception.php';
  244. throw new Zend_Dom_Exception(
  245. $e->getMessage()
  246. );
  247. }
  248. break;
  249. case self::DOC_HTML:
  250. case self::DOC_XHTML:
  251. default:
  252. $success = $domDoc->loadHTML($document);
  253. break;
  254. }
  255. $errors = libxml_get_errors();
  256. if (!empty($errors)) {
  257. $this->_documentErrors = $errors;
  258. libxml_clear_errors();
  259. }
  260. libxml_use_internal_errors(false);
  261. if (!$success) {
  262. require_once 'Zend/Dom/Exception.php';
  263. throw new Zend_Dom_Exception(sprintf('Error parsing document (type == %s)', $type));
  264. }
  265. $nodeList = $this->_getNodeList($domDoc, $xpathQuery);
  266. return new Zend_Dom_Query_Result($query, $xpathQuery, $domDoc, $nodeList);
  267. }
  268. /**
  269. * Register XPath namespaces
  270. *
  271. * @param array $xpathNamespaces
  272. * @return void
  273. */
  274. public function registerXpathNamespaces($xpathNamespaces)
  275. {
  276. $this->_xpathNamespaces = $xpathNamespaces;
  277. }
  278. /**
  279. * Prepare node list
  280. *
  281. * @param DOMDocument $document
  282. * @param string|array $xpathQuery
  283. * @return array
  284. */
  285. protected function _getNodeList($document, $xpathQuery)
  286. {
  287. $xpath = new DOMXPath($document);
  288. foreach ($this->_xpathNamespaces as $prefix => $namespaceUri) {
  289. $xpath->registerNamespace($prefix, $namespaceUri);
  290. }
  291. $xpathQuery = (string) $xpathQuery;
  292. if (preg_match_all('|\[contains\((@[a-z0-9_-]+),\s?\' |i', $xpathQuery, $matches)) {
  293. foreach ($matches[1] as $attribute) {
  294. $queryString = '//*[' . $attribute . ']';
  295. $attributeName = substr($attribute, 1);
  296. $nodes = $xpath->query($queryString);
  297. foreach ($nodes as $node) {
  298. $attr = $node->attributes->getNamedItem($attributeName);
  299. $attr->value = ' ' . $attr->value . ' ';
  300. }
  301. }
  302. }
  303. return $xpath->query($xpathQuery);
  304. }
  305. }