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.
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
23 /** Zend_Search_Lucene_Document */
24 require_once $CFG->dirroot
.'/search/Zend/Search/Lucene/Document.php';
31 * @package Zend_Search_Lucene
32 * @subpackage Document
33 * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
34 * @license http://framework.zend.com/license/new-bsd New BSD License
36 class Zend_Search_Lucene_Document_Html
extends Zend_Search_Lucene_Document
39 * List of document links
43 private $_links = array();
46 * List of document header links
50 private $_headerLinks = array();
53 * Stored DOM representation
63 * @param boolean $isFile
64 * @param boolean $storeContent
66 private function __construct($data, $isFile, $storeContent)
68 $this->_doc
= new DOMDocument();
69 $this->_doc
->substituteEntities
= true;
72 @$this->_doc
->loadHTMLFile($data);
74 @$this->_doc
->loadHTML($data);
77 $xpath = new DOMXPath($this->_doc
);
80 $titleNodes = $xpath->query('/html/head/title');
81 foreach ($titleNodes as $titleNode) {
82 // title should always have only one entry, but we process all nodeset entries
83 $docTitle .= $titleNode->nodeValue
. ' ';
85 $this->addField(Zend_Search_Lucene_Field
::Text('title', $docTitle, $this->_doc
->actualEncoding
));
87 $metaNodes = $xpath->query('/html/head/meta[@name]');
88 foreach ($metaNodes as $metaNode) {
89 $this->addField(Zend_Search_Lucene_Field
::Text($metaNode->getAttribute('name'),
90 $metaNode->getAttribute('content'),
91 $this->_doc
->actualEncoding
));
95 $bodyNodes = $xpath->query('/html/body');
96 foreach ($bodyNodes as $bodyNode) {
97 // body should always have only one entry, but we process all nodeset entries
98 $this->_retrieveNodeText($bodyNode, $docBody);
101 $this->addField(Zend_Search_Lucene_Field
::Text('body', $docBody, $this->_doc
->actualEncoding
));
103 $this->addField(Zend_Search_Lucene_Field
::UnStored('body', $docBody, $this->_doc
->actualEncoding
));
106 $linkNodes = $this->_doc
->getElementsByTagName('a');
107 foreach ($linkNodes as $linkNode) {
108 if (($href = $linkNode->getAttribute('href')) != '') {
109 $this->_links
[] = $href;
112 $this->_links
= array_unique($this->_links
);
114 $linkNodes = $xpath->query('/html/head/link');
115 foreach ($linkNodes as $linkNode) {
116 if (($href = $linkNode->getAttribute('href')) != '') {
117 $this->_headerLinks
[] = $href;
120 $this->_headerLinks
= array_unique($this->_headerLinks
);
126 * We should exclude scripts, which may be not included into comment tags, CDATA sections,
128 * @param DOMNode $node
129 * @param string &$text
131 private function _retrieveNodeText(DOMNode
$node, &$text)
133 if ($node->nodeType
== XML_TEXT_NODE
) {
134 $text .= $node->nodeValue
;
136 } else if ($node->nodeType
== XML_ELEMENT_NODE
&& $node->nodeName
!= 'script') {
137 foreach ($node->childNodes
as $childNode) {
138 $this->_retrieveNodeText($childNode, $text);
144 * Get document HREF links
148 public function getLinks()
150 return $this->_links
;
154 * Get document header links
158 public function getHeaderLinks()
160 return $this->_headerLinks
;
164 * Load HTML document from a string
166 * @param string $data
167 * @param boolean $storeContent
168 * @return Zend_Search_Lucene_Document_Html
170 public static function loadHTML($data, $storeContent = false)
172 return new Zend_Search_Lucene_Document_Html($data, false, $storeContent);
176 * Load HTML document from a file
178 * @param string $file
179 * @param boolean $storeContent
180 * @return Zend_Search_Lucene_Document_Html
182 public static function loadHTMLFile($file, $storeContent = false)
184 return new Zend_Search_Lucene_Document_Html($file, true, $storeContent);
189 * Highlight text in text node
191 * @param DOMText $node
192 * @param array $wordsToHighlight
193 * @param string $color
195 public function _highlightTextNode(DOMText
$node, $wordsToHighlight, $color)
197 $analyzer = Zend_Search_Lucene_Analysis_Analyzer
::getDefault();
198 $analyzer->setInput($node->nodeValue
, $this->_doc
->encoding
);
200 $matchedTokens = array();
202 while (($token = $analyzer->nextToken()) !== null) {
203 if (isset($wordsToHighlight[$token->getTermText()])) {
204 $matchedTokens[] = $token;
208 if (count($matchedTokens) == 0) {
212 $matchedTokens = array_reverse($matchedTokens);
214 foreach ($matchedTokens as $token) {
215 // Cut text after matched token
216 $node->splitText($token->getEndOffset());
219 $matchedWordNode = $node->splitText($token->getStartOffset());
221 $highlightedNode = $this->_doc
->createElement('b', $matchedWordNode->nodeValue
);
222 $highlightedNode->setAttribute('style', 'color:black;background-color:' . $color);
224 $node->parentNode
->replaceChild($highlightedNode, $matchedWordNode);
230 * highlight words in content of the specified node
232 * @param DOMNode $contextNode
233 * @param array $wordsToHighlight
234 * @param string $color
236 public function _highlightNode(DOMNode
$contextNode, $wordsToHighlight, $color)
238 $textNodes = array();
240 if (!$contextNode->hasChildNodes()) {
244 foreach ($contextNode->childNodes
as $childNode) {
245 if ($childNode->nodeType
== XML_TEXT_NODE
) {
246 // process node later to leave childNodes structure untouched
247 $textNodes[] = $childNode;
250 if ($childNode->nodeName
!= 'script') {
251 $this->_highlightNode($childNode, $wordsToHighlight, $color);
256 foreach ($textNodes as $textNode) {
257 $this->_highlightTextNode($textNode, $wordsToHighlight, $color);
264 * Highlight text with specified color
266 * @param string|array $words
267 * @param string $color
270 public function highlight($words, $color = '#66ffff')
272 if (!is_array($words)) {
273 $words = array($words);
275 $wordsToHighlight = array();
277 $analyzer = Zend_Search_Lucene_Analysis_Analyzer
::getDefault();
278 foreach ($words as $wordString) {
279 $wordsToHighlight = array_merge($wordsToHighlight, $analyzer->tokenize($wordString));
282 if (count($wordsToHighlight) == 0) {
283 return $this->_doc
->saveHTML();
286 $wordsToHighlightFlipped = array();
287 foreach ($wordsToHighlight as $id => $token) {
288 $wordsToHighlightFlipped[$token->getTermText()] = $id;
291 $xpath = new DOMXPath($this->_doc
);
293 $matchedNodes = $xpath->query("/html/body/*");
294 foreach ($matchedNodes as $matchedNode) {
295 $this->_highlightNode($matchedNode, $wordsToHighlightFlipped, $color);
305 public function getHTML()
307 return $this->_doc
->saveHTML();