Query.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. * Constructor
  62. *
  63. * @param null|string $document
  64. * @return void
  65. */
  66. public function __construct($document = null)
  67. {
  68. $this->setDocument($document);
  69. }
  70. /**
  71. * Set document to query
  72. *
  73. * @param string $document
  74. * @return Zend_Dom_Query
  75. */
  76. public function setDocument($document)
  77. {
  78. if (0 === strlen($document)) {
  79. return $this;
  80. }
  81. // breaking XML declaration to make syntax highlighting work
  82. if ('<' . '?xml' == substr(trim($document), 0, 5)) {
  83. return $this->setDocumentXml($document);
  84. }
  85. if (strstr($document, 'DTD XHTML')) {
  86. return $this->setDocumentXhtml($document);
  87. }
  88. return $this->setDocumentHtml($document);
  89. }
  90. /**
  91. * Register HTML document
  92. *
  93. * @param string $document
  94. * @return Zend_Dom_Query
  95. */
  96. public function setDocumentHtml($document)
  97. {
  98. $this->_document = (string) $document;
  99. $this->_docType = self::DOC_HTML;
  100. return $this;
  101. }
  102. /**
  103. * Register XHTML document
  104. *
  105. * @param string $document
  106. * @return Zend_Dom_Query
  107. */
  108. public function setDocumentXhtml($document)
  109. {
  110. $this->_document = (string) $document;
  111. $this->_docType = self::DOC_XHTML;
  112. return $this;
  113. }
  114. /**
  115. * Register XML document
  116. *
  117. * @param string $document
  118. * @return Zend_Dom_Query
  119. */
  120. public function setDocumentXml($document)
  121. {
  122. $this->_document = (string) $document;
  123. $this->_docType = self::DOC_XML;
  124. return $this;
  125. }
  126. /**
  127. * Retrieve current document
  128. *
  129. * @return string
  130. */
  131. public function getDocument()
  132. {
  133. return $this->_document;
  134. }
  135. /**
  136. * Get document type
  137. *
  138. * @return string
  139. */
  140. public function getDocumentType()
  141. {
  142. return $this->_docType;
  143. }
  144. /**
  145. * Get any DOMDocument errors found
  146. *
  147. * @return false|array
  148. */
  149. public function getDocumentErrors()
  150. {
  151. return $this->_documentErrors;
  152. }
  153. /**
  154. * Perform a CSS selector query
  155. *
  156. * @param string $query
  157. * @return Zend_Dom_Query_Result
  158. */
  159. public function query($query)
  160. {
  161. $xpathQuery = Zend_Dom_Query_Css2Xpath::transform($query);
  162. return $this->queryXpath($xpathQuery, $query);
  163. }
  164. /**
  165. * Perform an XPath query
  166. *
  167. * @param string|array $xpathQuery
  168. * @param string $query CSS selector query
  169. * @return Zend_Dom_Query_Result
  170. */
  171. public function queryXpath($xpathQuery, $query = null)
  172. {
  173. if (null === ($document = $this->getDocument())) {
  174. require_once 'Zend/Dom/Exception.php';
  175. throw new Zend_Dom_Exception('Cannot query; no document registered');
  176. }
  177. libxml_use_internal_errors(true);
  178. $domDoc = new DOMDocument;
  179. $type = $this->getDocumentType();
  180. switch ($type) {
  181. case self::DOC_XML:
  182. $success = $domDoc->loadXML($document);
  183. break;
  184. case self::DOC_HTML:
  185. case self::DOC_XHTML:
  186. default:
  187. $success = $domDoc->loadHTML($document);
  188. break;
  189. }
  190. $errors = libxml_get_errors();
  191. if (!empty($errors)) {
  192. $this->_documentErrors = $errors;
  193. libxml_clear_errors();
  194. }
  195. libxml_use_internal_errors(false);
  196. if (!$success) {
  197. require_once 'Zend/Dom/Exception.php';
  198. throw new Zend_Dom_Exception(sprintf('Error parsing document (type == %s)', $type));
  199. }
  200. $nodeList = $this->_getNodeList($domDoc, $xpathQuery);
  201. return new Zend_Dom_Query_Result($query, $xpathQuery, $domDoc, $nodeList);
  202. }
  203. /**
  204. * Prepare node list
  205. *
  206. * @param DOMDocument $document
  207. * @param string|array $xpathQuery
  208. * @return array
  209. */
  210. protected function _getNodeList($document, $xpathQuery)
  211. {
  212. $xpath = new DOMXPath($document);
  213. $xpathQuery = (string) $xpathQuery;
  214. if (preg_match_all('|\[contains\((@[a-z0-9_-]+),\s?\' |i', $xpathQuery, $matches)) {
  215. foreach ($matches[1] as $attribute) {
  216. $queryString = '//*[' . $attribute . ']';
  217. $attributeName = substr($attribute, 1);
  218. $nodes = $xpath->query($queryString);
  219. foreach ($nodes as $node) {
  220. $attr = $node->attributes->getNamedItem($attributeName);
  221. $attr->value = ' ' . $attr->value . ' ';
  222. }
  223. }
  224. }
  225. return $xpath->query($xpathQuery);
  226. }
  227. }