Query.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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-2010 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. /**
  30. * Query DOM structures based on CSS selectors and/or XPath
  31. *
  32. * @package Zend_Dom
  33. * @subpackage Query
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Dom_Query
  38. {
  39. /**#@+
  40. * Document types
  41. */
  42. const DOC_XML = 'docXml';
  43. const DOC_HTML = 'docHtml';
  44. const DOC_XHTML = 'docXhtml';
  45. /**#@-*/
  46. /**
  47. * @var string
  48. */
  49. protected $_document;
  50. /**
  51. * DOMDocument errors, if any
  52. * @var false|array
  53. */
  54. protected $_documentErrors = false;
  55. /**
  56. * Document type
  57. * @var string
  58. */
  59. protected $_docType;
  60. /**
  61. * XPath namespaces
  62. * @var array
  63. */
  64. protected $_xpathNamespaces = array();
  65. /**
  66. * Constructor
  67. *
  68. * @param null|string $document
  69. * @return void
  70. */
  71. public function __construct($document = null)
  72. {
  73. $this->setDocument($document);
  74. }
  75. /**
  76. * Set document to query
  77. *
  78. * @param string $document
  79. * @return Zend_Dom_Query
  80. */
  81. public function setDocument($document)
  82. {
  83. if (0 === strlen($document)) {
  84. return $this;
  85. }
  86. // breaking XML declaration to make syntax highlighting work
  87. if ('<' . '?xml' == substr(trim($document), 0, 5)) {
  88. return $this->setDocumentXml($document);
  89. }
  90. if (strstr($document, 'DTD XHTML')) {
  91. return $this->setDocumentXhtml($document);
  92. }
  93. return $this->setDocumentHtml($document);
  94. }
  95. /**
  96. * Register HTML document
  97. *
  98. * @param string $document
  99. * @return Zend_Dom_Query
  100. */
  101. public function setDocumentHtml($document)
  102. {
  103. $this->_document = (string) $document;
  104. $this->_docType = self::DOC_HTML;
  105. return $this;
  106. }
  107. /**
  108. * Register XHTML document
  109. *
  110. * @param string $document
  111. * @return Zend_Dom_Query
  112. */
  113. public function setDocumentXhtml($document)
  114. {
  115. $this->_document = (string) $document;
  116. $this->_docType = self::DOC_XHTML;
  117. return $this;
  118. }
  119. /**
  120. * Register XML document
  121. *
  122. * @param string $document
  123. * @return Zend_Dom_Query
  124. */
  125. public function setDocumentXml($document)
  126. {
  127. $this->_document = (string) $document;
  128. $this->_docType = self::DOC_XML;
  129. return $this;
  130. }
  131. /**
  132. * Retrieve current document
  133. *
  134. * @return string
  135. */
  136. public function getDocument()
  137. {
  138. return $this->_document;
  139. }
  140. /**
  141. * Get document type
  142. *
  143. * @return string
  144. */
  145. public function getDocumentType()
  146. {
  147. return $this->_docType;
  148. }
  149. /**
  150. * Get any DOMDocument errors found
  151. *
  152. * @return false|array
  153. */
  154. public function getDocumentErrors()
  155. {
  156. return $this->_documentErrors;
  157. }
  158. /**
  159. * Perform a CSS selector query
  160. *
  161. * @param string $query
  162. * @return Zend_Dom_Query_Result
  163. */
  164. public function query($query)
  165. {
  166. $xpathQuery = Zend_Dom_Query_Css2Xpath::transform($query);
  167. return $this->queryXpath($xpathQuery, $query);
  168. }
  169. /**
  170. * Perform an XPath query
  171. *
  172. * @param string|array $xpathQuery
  173. * @param string $query CSS selector query
  174. * @return Zend_Dom_Query_Result
  175. */
  176. public function queryXpath($xpathQuery, $query = null)
  177. {
  178. if (null === ($document = $this->getDocument())) {
  179. require_once 'Zend/Dom/Exception.php';
  180. throw new Zend_Dom_Exception('Cannot query; no document registered');
  181. }
  182. libxml_use_internal_errors(true);
  183. $domDoc = new DOMDocument;
  184. $type = $this->getDocumentType();
  185. switch ($type) {
  186. case self::DOC_XML:
  187. $success = $domDoc->loadXML($document);
  188. break;
  189. case self::DOC_HTML:
  190. case self::DOC_XHTML:
  191. default:
  192. $success = $domDoc->loadHTML($document);
  193. break;
  194. }
  195. $errors = libxml_get_errors();
  196. if (!empty($errors)) {
  197. $this->_documentErrors = $errors;
  198. libxml_clear_errors();
  199. }
  200. libxml_use_internal_errors(false);
  201. if (!$success) {
  202. require_once 'Zend/Dom/Exception.php';
  203. throw new Zend_Dom_Exception(sprintf('Error parsing document (type == %s)', $type));
  204. }
  205. $nodeList = $this->_getNodeList($domDoc, $xpathQuery);
  206. return new Zend_Dom_Query_Result($query, $xpathQuery, $domDoc, $nodeList);
  207. }
  208. /**
  209. * Register XPath namespaces
  210. *
  211. * @param array $xpathNamespaces
  212. * @return void
  213. */
  214. public function registerXpathNamespaces($xpathNamespaces)
  215. {
  216. $this->_xpathNamespaces = $xpathNamespaces;
  217. }
  218. /**
  219. * Prepare node list
  220. *
  221. * @param DOMDocument $document
  222. * @param string|array $xpathQuery
  223. * @return array
  224. */
  225. protected function _getNodeList($document, $xpathQuery)
  226. {
  227. $xpath = new DOMXPath($document);
  228. foreach ($this->_xpathNamespaces as $prefix => $namespaceUri) {
  229. $xpath->registerNamespace($prefix, $namespaceUri);
  230. }
  231. $xpathQuery = (string) $xpathQuery;
  232. if (preg_match_all('|\[contains\((@[a-z0-9_-]+),\s?\' |i', $xpathQuery, $matches)) {
  233. foreach ($matches[1] as $attribute) {
  234. $queryString = '//*[' . $attribute . ']';
  235. $attributeName = substr($attribute, 1);
  236. $nodes = $xpath->query($queryString);
  237. foreach ($nodes as $node) {
  238. $attr = $node->attributes->getNamedItem($attributeName);
  239. $attr->value = ' ' . $attr->value . ' ';
  240. }
  241. }
  242. }
  243. return $xpath->query($xpathQuery);
  244. }
  245. }