PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/app/sitesearch/lib/Zend/Search/Lucene/Document/Html.php

https://github.com/lux/sitellite
PHP | 310 lines | 146 code | 43 blank | 121 comment | 27 complexity | f182dbfd9e355011cba92e6d6d4b4d2b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, GPL-3.0
  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_Search_Lucene
  17. * @subpackage Document
  18. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Search_Lucene_Document */
  22. require_once 'Zend/Search/Lucene/Document.php';
  23. /**
  24. * HTML document.
  25. *
  26. * @category Zend
  27. * @package Zend_Search_Lucene
  28. * @subpackage Document
  29. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Search_Lucene_Document_Html extends Zend_Search_Lucene_Document
  33. {
  34. /**
  35. * List of document links
  36. *
  37. * @var array
  38. */
  39. private $_links = array();
  40. /**
  41. * List of document header links
  42. *
  43. * @var array
  44. */
  45. private $_headerLinks = array();
  46. /**
  47. * Stored DOM representation
  48. *
  49. * @var DOMDocument
  50. */
  51. private $_doc;
  52. /**
  53. * Object constructor
  54. *
  55. * @param string $data
  56. * @param boolean $isFile
  57. * @param boolean $storeContent
  58. */
  59. private function __construct($data, $isFile, $storeContent)
  60. {
  61. $this->_doc = new DOMDocument();
  62. $this->_doc->substituteEntities = true;
  63. if ($isFile) {
  64. @$this->_doc->loadHTMLFile($data);
  65. } else{
  66. @$this->_doc->loadHTML($data);
  67. }
  68. $xpath = new DOMXPath($this->_doc);
  69. $docTitle = '';
  70. $titleNodes = $xpath->query('/html/head/title');
  71. foreach ($titleNodes as $titleNode) {
  72. // title should always have only one entry, but we process all nodeset entries
  73. $docTitle .= $titleNode->nodeValue . ' ';
  74. }
  75. $this->addField(Zend_Search_Lucene_Field::Text('title', $docTitle, $this->_doc->actualEncoding));
  76. $metaNodes = $xpath->query('/html/head/meta[@name]');
  77. foreach ($metaNodes as $metaNode) {
  78. $this->addField(Zend_Search_Lucene_Field::Text($metaNode->getAttribute('name'),
  79. $metaNode->getAttribute('content'),
  80. $this->_doc->actualEncoding));
  81. }
  82. $docBody = '';
  83. $bodyNodes = $xpath->query('/html/body');
  84. foreach ($bodyNodes as $bodyNode) {
  85. // body should always have only one entry, but we process all nodeset entries
  86. $this->_retrieveNodeText($bodyNode, $docBody);
  87. }
  88. if ($storeContent) {
  89. $this->addField(Zend_Search_Lucene_Field::Text('body', $docBody, $this->_doc->actualEncoding));
  90. } else {
  91. $this->addField(Zend_Search_Lucene_Field::UnStored('body', $docBody, $this->_doc->actualEncoding));
  92. }
  93. $linkNodes = $this->_doc->getElementsByTagName('a');
  94. foreach ($linkNodes as $linkNode) {
  95. if (($href = $linkNode->getAttribute('href')) != '') {
  96. $this->_links[] = $href;
  97. }
  98. }
  99. $this->_links = array_unique($this->_links);
  100. $linkNodes = $xpath->query('/html/head/link');
  101. foreach ($linkNodes as $linkNode) {
  102. if (($href = $linkNode->getAttribute('href')) != '') {
  103. $this->_headerLinks[] = $href;
  104. }
  105. }
  106. $this->_headerLinks = array_unique($this->_headerLinks);
  107. }
  108. /**
  109. * Get node text
  110. *
  111. * We should exclude scripts, which may be not included into comment tags, CDATA sections,
  112. *
  113. * @param DOMNode $node
  114. * @param string &$text
  115. */
  116. private function _retrieveNodeText(DOMNode $node, &$text)
  117. {
  118. if ($node->nodeType == XML_TEXT_NODE) {
  119. $text .= $node->nodeValue ;
  120. $text .= ' ';
  121. } else if ($node->nodeType == XML_ELEMENT_NODE && $node->nodeName != 'script') {
  122. foreach ($node->childNodes as $childNode) {
  123. $this->_retrieveNodeText($childNode, $text);
  124. }
  125. }
  126. }
  127. /**
  128. * Get document HREF links
  129. *
  130. * @return array
  131. */
  132. public function getLinks()
  133. {
  134. return $this->_links;
  135. }
  136. /**
  137. * Get document header links
  138. *
  139. * @return array
  140. */
  141. public function getHeaderLinks()
  142. {
  143. return $this->_headerLinks;
  144. }
  145. /**
  146. * Load HTML document from a string
  147. *
  148. * @param string $data
  149. * @param boolean $storeContent
  150. * @return Zend_Search_Lucene_Document_Html
  151. */
  152. public static function loadHTML($data, $storeContent = false)
  153. {
  154. return new Zend_Search_Lucene_Document_Html($data, false, $storeContent);
  155. }
  156. /**
  157. * Load HTML document from a file
  158. *
  159. * @param string $file
  160. * @param boolean $storeContent
  161. * @return Zend_Search_Lucene_Document_Html
  162. */
  163. public static function loadHTMLFile($file, $storeContent = false)
  164. {
  165. return new Zend_Search_Lucene_Document_Html($file, true, $storeContent);
  166. }
  167. /**
  168. * Highlight text in text node
  169. *
  170. * @param DOMText $node
  171. * @param array $wordsToHighlight
  172. * @param string $color
  173. */
  174. public function _highlightTextNode(DOMText $node, $wordsToHighlight, $color)
  175. {
  176. $analyzer = Zend_Search_Lucene_Analysis_Analyzer::getDefault();
  177. $analyzer->setInput($node->nodeValue, $this->_doc->encoding);
  178. $matchedTokens = array();
  179. while (($token = $analyzer->nextToken()) !== null) {
  180. if (isset($wordsToHighlight[$token->getTermText()])) {
  181. $matchedTokens[] = $token;
  182. }
  183. }
  184. if (count($matchedTokens) == 0) {
  185. return;
  186. }
  187. $matchedTokens = array_reverse($matchedTokens);
  188. foreach ($matchedTokens as $token) {
  189. // Cut text after matched token
  190. $node->splitText($token->getEndOffset());
  191. // Cut matched node
  192. $matchedWordNode = $node->splitText($token->getStartOffset());
  193. $highlightedNode = $this->_doc->createElement('b', $matchedWordNode->nodeValue);
  194. $highlightedNode->setAttribute('style', 'color:black;background-color:' . $color);
  195. $node->parentNode->replaceChild($highlightedNode, $matchedWordNode);
  196. }
  197. }
  198. /**
  199. * highlight words in content of the specified node
  200. *
  201. * @param DOMNode $contextNode
  202. * @param array $wordsToHighlight
  203. * @param string $color
  204. */
  205. public function _highlightNode(DOMNode $contextNode, $wordsToHighlight, $color)
  206. {
  207. $textNodes = array();
  208. if (!$contextNode->hasChildNodes()) {
  209. return;
  210. }
  211. foreach ($contextNode->childNodes as $childNode) {
  212. if ($childNode->nodeType == XML_TEXT_NODE) {
  213. // process node later to leave childNodes structure untouched
  214. $textNodes[] = $childNode;
  215. } else {
  216. // Skip script nodes
  217. if ($childNode->nodeName != 'script') {
  218. $this->_highlightNode($childNode, $wordsToHighlight, $color);
  219. }
  220. }
  221. }
  222. foreach ($textNodes as $textNode) {
  223. $this->_highlightTextNode($textNode, $wordsToHighlight, $color);
  224. }
  225. }
  226. /**
  227. * Highlight text with specified color
  228. *
  229. * @param string|array $words
  230. * @param string $color
  231. * @return string
  232. */
  233. public function highlight($words, $color = '#66ffff')
  234. {
  235. if (!is_array($words)) {
  236. $words = array($words);
  237. }
  238. $wordsToHighlight = array();
  239. $analyzer = Zend_Search_Lucene_Analysis_Analyzer::getDefault();
  240. foreach ($words as $wordString) {
  241. $wordsToHighlight = array_merge($wordsToHighlight, $analyzer->tokenize($wordString));
  242. }
  243. if (count($wordsToHighlight) == 0) {
  244. return $this->_doc->saveHTML();
  245. }
  246. $wordsToHighlightFlipped = array();
  247. foreach ($wordsToHighlight as $id => $token) {
  248. $wordsToHighlightFlipped[$token->getTermText()] = $id;
  249. }
  250. $xpath = new DOMXPath($this->_doc);
  251. $matchedNodes = $xpath->query("/html/body/*");
  252. foreach ($matchedNodes as $matchedNode) {
  253. $this->_highlightNode($matchedNode, $wordsToHighlightFlipped, $color);
  254. }
  255. }
  256. /**
  257. * Get HTML
  258. *
  259. * @return string
  260. */
  261. public function getHTML()
  262. {
  263. return $this->_doc->saveHTML();
  264. }
  265. }