import v1.1.0_beta1 | 2009-08-21
This commit is contained in:
@ -22,98 +22,122 @@
|
||||
/** Zend_Search_Lucene_Document_OpenXml */
|
||||
require_once 'Zend/Search/Lucene/Document/OpenXml.php';
|
||||
|
||||
if (class_exists ( 'ZipArchive' )) {
|
||||
|
||||
if (class_exists('ZipArchive', false)) {
|
||||
|
||||
/**
|
||||
* Docx document.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Search_Lucene
|
||||
* @subpackage Document
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Search_Lucene_Document_Docx extends Zend_Search_Lucene_Document_OpenXml {
|
||||
/**
|
||||
* Docx document.
|
||||
* Xml Schema - WordprocessingML
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Search_Lucene
|
||||
* @subpackage Document
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @var string
|
||||
*/
|
||||
class Zend_Search_Lucene_Document_Docx extends Zend_Search_Lucene_Document_OpenXml {
|
||||
/**
|
||||
* Xml Schema - WordprocessingML
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_WORDPROCESSINGML = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
|
||||
|
||||
/**
|
||||
* Object constructor
|
||||
*
|
||||
* @param string $fileName
|
||||
* @param boolean $storeContent
|
||||
*/
|
||||
private function __construct($fileName, $storeContent) {
|
||||
// Document data holders
|
||||
$documentBody = array ( );
|
||||
$coreProperties = array ( );
|
||||
|
||||
// Open OpenXML package
|
||||
$package = new ZipArchive ( );
|
||||
$package->open ( $fileName );
|
||||
|
||||
// Read relations and search for officeDocument
|
||||
$relations = simplexml_load_string ( $package->getFromName ( "_rels/.rels" ) );
|
||||
foreach ( $relations->Relationship as $rel ) {
|
||||
if ($rel ["Type"] == Zend_Search_Lucene_Document_OpenXml::SCHEMA_OFFICEDOCUMENT) {
|
||||
// Found office document! Read in contents...
|
||||
$contents = simplexml_load_string ( $package->getFromName ( $this->absoluteZipPath ( dirname ( $rel ["Target"] ) . "/" . basename ( $rel ["Target"] ) ) ) );
|
||||
|
||||
$contents->registerXPathNamespace ( "w", Zend_Search_Lucene_Document_Docx::SCHEMA_WORDPROCESSINGML );
|
||||
$paragraphs = $contents->xpath ( '//w:body/w:p' );
|
||||
|
||||
foreach ( $paragraphs as $paragraph ) {
|
||||
$runs = $paragraph->xpath ( '//w:r/w:t' );
|
||||
foreach ( $runs as $run ) {
|
||||
$documentBody [] = ( string ) $run;
|
||||
}
|
||||
const SCHEMA_WORDPROCESSINGML = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
|
||||
|
||||
/**
|
||||
* Object constructor
|
||||
*
|
||||
* @param string $fileName
|
||||
* @param boolean $storeContent
|
||||
*/
|
||||
private function __construct($fileName, $storeContent) {
|
||||
// Document data holders
|
||||
$documentBody = array();
|
||||
$coreProperties = array();
|
||||
|
||||
// Open OpenXML package
|
||||
$package = new ZipArchive();
|
||||
$package->open($fileName);
|
||||
|
||||
// Read relations and search for officeDocument
|
||||
$relations = simplexml_load_string($package->getFromName('_rels/.rels'));
|
||||
foreach($relations->Relationship as $rel) {
|
||||
if ($rel ["Type"] == Zend_Search_Lucene_Document_OpenXml::SCHEMA_OFFICEDOCUMENT) {
|
||||
// Found office document! Read in contents...
|
||||
$contents = simplexml_load_string($package->getFromName(
|
||||
$this->absoluteZipPath(dirname($rel['Target'])
|
||||
. '/'
|
||||
. basename($rel['Target']))
|
||||
));
|
||||
|
||||
$contents->registerXPathNamespace('w', Zend_Search_Lucene_Document_Docx::SCHEMA_WORDPROCESSINGML);
|
||||
$paragraphs = $contents->xpath('//w:body/w:p');
|
||||
|
||||
foreach ($paragraphs as $paragraph) {
|
||||
$runs = $paragraph->xpath('.//w:r/*[name() = "w:t" or name() = "w:br"]');
|
||||
|
||||
if ($runs === false) {
|
||||
// Paragraph doesn't contain any text or breaks
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
foreach ($runs as $run) {
|
||||
if ($run->getName() == 'br') {
|
||||
// Break element
|
||||
$documentBody[] = ' ';
|
||||
} else {
|
||||
$documentBody[] = (string)$run;
|
||||
}
|
||||
}
|
||||
|
||||
// Add space after each paragraph. So they are not bound together.
|
||||
$documentBody[] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
// Read core properties
|
||||
$coreProperties = $this->extractMetaData ( $package );
|
||||
|
||||
// Close file
|
||||
$package->close ();
|
||||
|
||||
// Store filename
|
||||
$this->addField ( Zend_Search_Lucene_Field::Text ( 'filename', $fileName ) );
|
||||
|
||||
// Store contents
|
||||
if ($storeContent) {
|
||||
$this->addField ( Zend_Search_Lucene_Field::Text ( 'body', implode ( ' ', $documentBody ) ) );
|
||||
} else {
|
||||
$this->addField ( Zend_Search_Lucene_Field::UnStored ( 'body', implode ( ' ', $documentBody ) ) );
|
||||
}
|
||||
|
||||
// Store meta data properties
|
||||
foreach ( $coreProperties as $key => $value ) {
|
||||
$this->addField ( Zend_Search_Lucene_Field::Text ( $key, $value ) );
|
||||
}
|
||||
|
||||
// Store title (if not present in meta data)
|
||||
if (! isset ( $coreProperties ['title'] )) {
|
||||
$this->addField ( Zend_Search_Lucene_Field::Text ( 'title', $fileName ) );
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load Docx document from a file
|
||||
*
|
||||
* @param string $fileName
|
||||
* @param boolean $storeContent
|
||||
* @return Zend_Search_Lucene_Document_Docx
|
||||
*/
|
||||
public static function loadDocxFile($fileName, $storeContent = false) {
|
||||
return new Zend_Search_Lucene_Document_Docx ( $fileName, $storeContent );
|
||||
|
||||
// Read core properties
|
||||
$coreProperties = $this->extractMetaData($package);
|
||||
|
||||
// Close file
|
||||
$package->close();
|
||||
|
||||
// Store filename
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('filename', $fileName, 'UTF-8'));
|
||||
|
||||
// Store contents
|
||||
if ($storeContent) {
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('body', implode('', $documentBody), 'UTF-8'));
|
||||
} else {
|
||||
$this->addField(Zend_Search_Lucene_Field::UnStored('body', implode('', $documentBody), 'UTF-8'));
|
||||
}
|
||||
|
||||
// Store meta data properties
|
||||
foreach ($coreProperties as $key => $value) {
|
||||
$this->addField(Zend_Search_Lucene_Field::Text($key, $value, 'UTF-8'));
|
||||
}
|
||||
|
||||
// Store title (if not present in meta data)
|
||||
if (! isset($coreProperties['title'])) {
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('title', $fileName, 'UTF-8'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load Docx document from a file
|
||||
*
|
||||
* @param string $fileName
|
||||
* @param boolean $storeContent
|
||||
* @return Zend_Search_Lucene_Document_Docx
|
||||
* @throws Zend_Search_Lucene_Document_Exception
|
||||
*/
|
||||
public static function loadDocxFile($fileName, $storeContent = false) {
|
||||
if (!is_readable($fileName)) {
|
||||
require_once 'Zend/Search/Lucene/Document/Exception.php';
|
||||
throw new Zend_Search_Lucene_Document_Exception('Provided file \'' . $fileName . '\' is not readable.');
|
||||
}
|
||||
|
||||
return new Zend_Search_Lucene_Document_Docx($fileName, $storeContent);
|
||||
}
|
||||
}
|
||||
|
||||
} // end if (class_exists('ZipArchive'))
|
||||
|
36
libs/Zend/Search/Lucene/Document/Exception.php
Normal file
36
libs/Zend/Search/Lucene/Document/Exception.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Search_Lucene
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Framework base exception
|
||||
*/
|
||||
require_once 'Zend/Search/Lucene/Exception.php';
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Search_Lucene
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Search_Lucene_Document_Exception extends Zend_Search_Lucene_Exception
|
||||
{}
|
||||
|
@ -69,11 +69,12 @@ class Zend_Search_Lucene_Document_Html extends Zend_Search_Lucene_Document
|
||||
/**
|
||||
* Object constructor
|
||||
*
|
||||
* @param string $data
|
||||
* @param string $data HTML string (may be HTML fragment, )
|
||||
* @param boolean $isFile
|
||||
* @param boolean $storeContent
|
||||
* @param string $defaultEncoding HTML encoding, is used if it's not specified using Content-type HTTP-EQUIV meta tag.
|
||||
*/
|
||||
private function __construct($data, $isFile, $storeContent)
|
||||
private function __construct($data, $isFile, $storeContent, $defaultEncoding = '')
|
||||
{
|
||||
$this->_doc = new DOMDocument();
|
||||
$this->_doc->substituteEntities = true;
|
||||
@ -85,6 +86,37 @@ class Zend_Search_Lucene_Document_Html extends Zend_Search_Lucene_Document
|
||||
}
|
||||
@$this->_doc->loadHTML($htmlData);
|
||||
|
||||
if ($this->_doc->encoding === null) {
|
||||
// Document encoding is not recognized
|
||||
|
||||
/** @todo improve HTML vs HTML fragment recognition */
|
||||
if (preg_match('/<html>/i', $htmlData, $matches, PREG_OFFSET_CAPTURE)) {
|
||||
// It's an HTML document
|
||||
// Add additional HEAD section and recognize document
|
||||
$htmlTagOffset = $matches[0][1] + strlen($matches[0][1]);
|
||||
|
||||
@$this->_doc->loadHTML(iconv($defaultEncoding, 'UTF-8//IGNORE', substr($htmlData, 0, $htmlTagOffset))
|
||||
. '<head><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"/></head>'
|
||||
. iconv($defaultEncoding, 'UTF-8//IGNORE', substr($htmlData, $htmlTagOffset)));
|
||||
|
||||
// Remove additional HEAD section
|
||||
$xpath = new DOMXPath($this->_doc);
|
||||
$head = $xpath->query('/html/head')->item(0);
|
||||
$head->parentNode->removeChild($head);
|
||||
} else {
|
||||
// It's an HTML fragment
|
||||
@$this->_doc->loadHTML('<html><head><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"/></head><body>'
|
||||
. iconv($defaultEncoding, 'UTF-8//IGNORE', $htmlData)
|
||||
. '</body></html>');
|
||||
}
|
||||
|
||||
}
|
||||
/** @todo Add correction of wrong HTML encoding recognition processing
|
||||
* The case is:
|
||||
* Content-type HTTP-EQUIV meta tag is presented, but ISO-8859-5 encoding is actually used,
|
||||
* even $this->_doc->encoding demonstrates another recognized encoding
|
||||
*/
|
||||
|
||||
$xpath = new DOMXPath($this->_doc);
|
||||
|
||||
$docTitle = '';
|
||||
@ -93,13 +125,13 @@ class Zend_Search_Lucene_Document_Html extends Zend_Search_Lucene_Document
|
||||
// title should always have only one entry, but we process all nodeset entries
|
||||
$docTitle .= $titleNode->nodeValue . ' ';
|
||||
}
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('title', $docTitle, $this->_doc->actualEncoding));
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('title', $docTitle, 'UTF-8'));
|
||||
|
||||
$metaNodes = $xpath->query('/html/head/meta[@name]');
|
||||
foreach ($metaNodes as $metaNode) {
|
||||
$this->addField(Zend_Search_Lucene_Field::Text($metaNode->getAttribute('name'),
|
||||
$metaNode->getAttribute('content'),
|
||||
$this->_doc->actualEncoding));
|
||||
'UTF-8'));
|
||||
}
|
||||
|
||||
$docBody = '';
|
||||
@ -109,9 +141,9 @@ class Zend_Search_Lucene_Document_Html extends Zend_Search_Lucene_Document
|
||||
$this->_retrieveNodeText($bodyNode, $docBody);
|
||||
}
|
||||
if ($storeContent) {
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('body', $docBody, $this->_doc->actualEncoding));
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('body', $docBody, 'UTF-8'));
|
||||
} else {
|
||||
$this->addField(Zend_Search_Lucene_Field::UnStored('body', $docBody, $this->_doc->actualEncoding));
|
||||
$this->addField(Zend_Search_Lucene_Field::UnStored('body', $docBody, 'UTF-8'));
|
||||
}
|
||||
|
||||
$linkNodes = $this->_doc->getElementsByTagName('a');
|
||||
@ -196,25 +228,27 @@ class Zend_Search_Lucene_Document_Html extends Zend_Search_Lucene_Document
|
||||
/**
|
||||
* Load HTML document from a string
|
||||
*
|
||||
* @param string $data
|
||||
* @param string $data
|
||||
* @param boolean $storeContent
|
||||
* @param string $defaultEncoding HTML encoding, is used if it's not specified using Content-type HTTP-EQUIV meta tag.
|
||||
* @return Zend_Search_Lucene_Document_Html
|
||||
*/
|
||||
public static function loadHTML($data, $storeContent = false)
|
||||
public static function loadHTML($data, $storeContent = false, $defaultEncoding = '')
|
||||
{
|
||||
return new Zend_Search_Lucene_Document_Html($data, false, $storeContent);
|
||||
return new Zend_Search_Lucene_Document_Html($data, false, $storeContent, $defaultEncoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load HTML document from a file
|
||||
*
|
||||
* @param string $file
|
||||
* @param string $file
|
||||
* @param boolean $storeContent
|
||||
* @param string $defaultEncoding HTML encoding, is used if it's not specified using Content-type HTTP-EQUIV meta tag.
|
||||
* @return Zend_Search_Lucene_Document_Html
|
||||
*/
|
||||
public static function loadHTMLFile($file, $storeContent = false)
|
||||
public static function loadHTMLFile($file, $storeContent = false, $defaultEncoding = '')
|
||||
{
|
||||
return new Zend_Search_Lucene_Document_Html($file, true, $storeContent);
|
||||
return new Zend_Search_Lucene_Document_Html($file, true, $storeContent, $defaultEncoding);
|
||||
}
|
||||
|
||||
|
||||
@ -223,12 +257,14 @@ class Zend_Search_Lucene_Document_Html extends Zend_Search_Lucene_Document
|
||||
*
|
||||
* @param DOMText $node
|
||||
* @param array $wordsToHighlight
|
||||
* @param string $color
|
||||
* @param callback $callback Callback method, used to transform (highlighting) text.
|
||||
* @param array $params Array of additionall callback parameters (first non-optional parameter is a text to transform)
|
||||
* @throws Zend_Search_Lucene_Exception
|
||||
*/
|
||||
public function _highlightTextNode(DOMText $node, $wordsToHighlight, $color)
|
||||
protected function _highlightTextNode(DOMText $node, $wordsToHighlight, $callback, $params)
|
||||
{
|
||||
$analyzer = Zend_Search_Lucene_Analysis_Analyzer::getDefault();
|
||||
$analyzer->setInput($node->nodeValue, $this->_doc->encoding);
|
||||
$analyzer->setInput($node->nodeValue, 'UTF-8');
|
||||
|
||||
$matchedTokens = array();
|
||||
|
||||
@ -251,10 +287,32 @@ class Zend_Search_Lucene_Document_Html extends Zend_Search_Lucene_Document
|
||||
// Cut matched node
|
||||
$matchedWordNode = $node->splitText($token->getStartOffset());
|
||||
|
||||
$highlightedNode = $this->_doc->createElement('b', $matchedWordNode->nodeValue);
|
||||
$highlightedNode->setAttribute('style', 'color:black;background-color:' . $color);
|
||||
// Retrieve HTML string representation for highlihted word
|
||||
$fullCallbackparamsList = $params;
|
||||
array_unshift($fullCallbackparamsList, $matchedWordNode->nodeValue);
|
||||
$highlightedWordNodeSetHtml = call_user_func_array($callback, $fullCallbackparamsList);
|
||||
|
||||
$node->parentNode->replaceChild($highlightedNode, $matchedWordNode);
|
||||
// Transform HTML string to a DOM representation and automatically transform retrieved string
|
||||
// into valid XHTML (It's automatically done by loadHTML() method)
|
||||
$highlightedWordNodeSetDomDocument = new DOMDocument('1.0', 'UTF-8');
|
||||
$success = @$highlightedWordNodeSetDomDocument->
|
||||
loadHTML('<html><head><meta http-equiv="Content-type" content="text/html; charset=UTF-8"/></head><body>'
|
||||
. $highlightedWordNodeSetHtml
|
||||
. '</body></html>');
|
||||
if (!$success) {
|
||||
require_once 'Zend/Search/Lucene/Exception.php';
|
||||
throw new Zend_Search_Lucene_Exception("Error occured while loading highlighted text fragment: '$highlightedNodeHtml'.");
|
||||
}
|
||||
$highlightedWordNodeSetXpath = new DOMXPath($highlightedWordNodeSetDomDocument);
|
||||
$highlightedWordNodeSet = $highlightedWordNodeSetXpath->query('/html/body')->item(0)->childNodes;
|
||||
|
||||
for ($count = 0; $count < $highlightedWordNodeSet->length; $count++) {
|
||||
$nodeToImport = $highlightedWordNodeSet->item($count);
|
||||
$node->parentNode->insertBefore($this->_doc->importNode($nodeToImport, true /* deep copy */),
|
||||
$matchedWordNode);
|
||||
}
|
||||
|
||||
$node->parentNode->removeChild($matchedWordNode);
|
||||
}
|
||||
}
|
||||
|
||||
@ -264,9 +322,10 @@ class Zend_Search_Lucene_Document_Html extends Zend_Search_Lucene_Document
|
||||
*
|
||||
* @param DOMNode $contextNode
|
||||
* @param array $wordsToHighlight
|
||||
* @param string $color
|
||||
* @param callback $callback Callback method, used to transform (highlighting) text.
|
||||
* @param array $params Array of additionall callback parameters (first non-optional parameter is a text to transform)
|
||||
*/
|
||||
public function _highlightNode(DOMNode $contextNode, $wordsToHighlight, $color)
|
||||
protected function _highlightNodeRecursive(DOMNode $contextNode, $wordsToHighlight, $callback, $params)
|
||||
{
|
||||
$textNodes = array();
|
||||
|
||||
@ -279,38 +338,66 @@ class Zend_Search_Lucene_Document_Html extends Zend_Search_Lucene_Document
|
||||
// process node later to leave childNodes structure untouched
|
||||
$textNodes[] = $childNode;
|
||||
} else {
|
||||
// Skip script nodes
|
||||
// Process node if it's not a script node
|
||||
if ($childNode->nodeName != 'script') {
|
||||
$this->_highlightNode($childNode, $wordsToHighlight, $color);
|
||||
$this->_highlightNodeRecursive($childNode, $wordsToHighlight, $callback, $params);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($textNodes as $textNode) {
|
||||
$this->_highlightTextNode($textNode, $wordsToHighlight, $color);
|
||||
$this->_highlightTextNode($textNode, $wordsToHighlight, $callback, $params);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Standard callback method used to highlight words.
|
||||
*
|
||||
* @param string $stringToHighlight
|
||||
* @return string
|
||||
* @internal
|
||||
*/
|
||||
public function applyColour($stringToHighlight, $colour)
|
||||
{
|
||||
return '<b style="color:black;background-color:' . $colour . '">' . $stringToHighlight . '</b>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Highlight text with specified color
|
||||
*
|
||||
* @param string|array $words
|
||||
* @param string $color
|
||||
* @param string $colour
|
||||
* @return string
|
||||
*/
|
||||
public function highlight($words, $color = '#66ffff')
|
||||
public function highlight($words, $colour = '#66ffff')
|
||||
{
|
||||
return $this->highlightExtended($words, array($this, 'applyColour'), array($colour));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Highlight text using specified View helper or callback function.
|
||||
*
|
||||
* @param string|array $words Words to highlight. Words could be organized using the array or string.
|
||||
* @param callback $callback Callback method, used to transform (highlighting) text.
|
||||
* @param array $params Array of additionall callback parameters passed through into it
|
||||
* (first non-optional parameter is an HTML fragment for highlighting)
|
||||
* @return string
|
||||
* @throws Zend_Search_Lucene_Exception
|
||||
*/
|
||||
public function highlightExtended($words, $callback, $params = array())
|
||||
{
|
||||
if (!is_array($words)) {
|
||||
$words = array($words);
|
||||
}
|
||||
$wordsToHighlight = array();
|
||||
|
||||
$wordsToHighlightList = array();
|
||||
$analyzer = Zend_Search_Lucene_Analysis_Analyzer::getDefault();
|
||||
foreach ($words as $wordString) {
|
||||
$wordsToHighlight = array_merge($wordsToHighlight, $analyzer->tokenize($wordString));
|
||||
$wordsToHighlightList[] = $analyzer->tokenize($wordString);
|
||||
}
|
||||
$wordsToHighlight = call_user_func_array('array_merge', $wordsToHighlightList);
|
||||
|
||||
if (count($wordsToHighlight) == 0) {
|
||||
return $this->_doc->saveHTML();
|
||||
@ -321,15 +408,20 @@ class Zend_Search_Lucene_Document_Html extends Zend_Search_Lucene_Document
|
||||
$wordsToHighlightFlipped[$token->getTermText()] = $id;
|
||||
}
|
||||
|
||||
if (!is_callable($callback)) {
|
||||
require_once 'Zend/Search/Lucene/Exception.php';
|
||||
throw new Zend_Search_Lucene_Exception('$viewHelper parameter mast be a View Helper name, View Helper object or callback.');
|
||||
}
|
||||
|
||||
$xpath = new DOMXPath($this->_doc);
|
||||
|
||||
$matchedNodes = $xpath->query("/html/body");
|
||||
foreach ($matchedNodes as $matchedNode) {
|
||||
$this->_highlightNode($matchedNode, $wordsToHighlightFlipped, $color);
|
||||
$this->_highlightNodeRecursive($matchedNode, $wordsToHighlightFlipped, $callback, $params);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get HTML
|
||||
*
|
||||
@ -339,5 +431,23 @@ class Zend_Search_Lucene_Document_Html extends Zend_Search_Lucene_Document
|
||||
{
|
||||
return $this->_doc->saveHTML();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get HTML body
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getHtmlBody()
|
||||
{
|
||||
$xpath = new DOMXPath($this->_doc);
|
||||
$bodyNodes = $xpath->query('/html/body')->item(0)->childNodes;
|
||||
|
||||
$outputFragments = array();
|
||||
for ($count = 0; $count < $bodyNodes->length; $count++) {
|
||||
$outputFragments[] = $this->_doc->saveXML($bodyNodes->item($count));
|
||||
}
|
||||
|
||||
return implode($outputFragments);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,7 @@
|
||||
/** Zend_Search_Lucene_Document */
|
||||
require_once 'Zend/Search/Lucene/Document.php';
|
||||
|
||||
if (class_exists('ZipArchive')) {
|
||||
if (class_exists('ZipArchive', false)) {
|
||||
|
||||
/**
|
||||
* OpenXML document.
|
||||
|
@ -23,7 +23,7 @@
|
||||
/** Zend_Search_Lucene_Document_OpenXml */
|
||||
require_once 'Zend/Search/Lucene/Document/OpenXml.php';
|
||||
|
||||
if (class_exists('ZipArchive')) {
|
||||
if (class_exists('ZipArchive', false)) {
|
||||
|
||||
/**
|
||||
* Pptx document.
|
||||
@ -42,14 +42,14 @@ class Zend_Search_Lucene_Document_Pptx extends Zend_Search_Lucene_Document_OpenX
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_PRESENTATIONML = 'http://schemas.openxmlformats.org/presentationml/2006/main';
|
||||
|
||||
|
||||
/**
|
||||
* Xml Schema - DrawingML
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_DRAWINGML = 'http://schemas.openxmlformats.org/drawingml/2006/main';
|
||||
|
||||
const SCHEMA_DRAWINGML = 'http://schemas.openxmlformats.org/drawingml/2006/main';
|
||||
|
||||
/**
|
||||
* Xml Schema - Slide relation
|
||||
*
|
||||
@ -63,7 +63,7 @@ class Zend_Search_Lucene_Document_Pptx extends Zend_Search_Lucene_Document_OpenX
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_SLIDENOTESRELATION = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide';
|
||||
|
||||
|
||||
/**
|
||||
* Object constructor
|
||||
*
|
||||
@ -94,7 +94,7 @@ class Zend_Search_Lucene_Document_Pptx extends Zend_Search_Lucene_Document_OpenX
|
||||
$slides[ str_replace( 'rId', '', (string)$slideRel["Id"] ) ] = simplexml_load_string(
|
||||
$package->getFromName( $this->absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($slideRel["Target"]) . "/" . basename($slideRel["Target"])) )
|
||||
);
|
||||
|
||||
|
||||
// Search for slide notes
|
||||
$slideNotesRelations = simplexml_load_string($package->getFromName( $this->absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($slideRel["Target"]) . "/_rels/" . basename($slideRel["Target"]) . ".rels")) );
|
||||
foreach ($slideNotesRelations->Relationship as $slideNoteRel) {
|
||||
@ -103,27 +103,27 @@ class Zend_Search_Lucene_Document_Pptx extends Zend_Search_Lucene_Document_OpenX
|
||||
$slideNotes[ str_replace( 'rId', '', (string)$slideRel["Id"] ) ] = simplexml_load_string(
|
||||
$package->getFromName( $this->absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($slideRel["Target"]) . "/" . dirname($slideNoteRel["Target"]) . "/" . basename($slideNoteRel["Target"])) )
|
||||
);
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Sort slides
|
||||
ksort($slides);
|
||||
ksort($slideNotes);
|
||||
|
||||
|
||||
// Extract contents from slides
|
||||
foreach ($slides as $slideKey => $slide) {
|
||||
// Register namespaces
|
||||
$slide->registerXPathNamespace("p", Zend_Search_Lucene_Document_Pptx::SCHEMA_PRESENTATIONML);
|
||||
$slide->registerXPathNamespace("a", Zend_Search_Lucene_Document_Pptx::SCHEMA_DRAWINGML);
|
||||
|
||||
|
||||
// Fetch all text
|
||||
$textElements = $slide->xpath('//a:t');
|
||||
foreach ($textElements as $textElement) {
|
||||
@ -138,15 +138,15 @@ class Zend_Search_Lucene_Document_Pptx extends Zend_Search_Lucene_Document_OpenX
|
||||
// Register namespaces
|
||||
$slideNote->registerXPathNamespace("p", Zend_Search_Lucene_Document_Pptx::SCHEMA_PRESENTATIONML);
|
||||
$slideNote->registerXPathNamespace("a", Zend_Search_Lucene_Document_Pptx::SCHEMA_DRAWINGML);
|
||||
|
||||
|
||||
// Fetch all text
|
||||
$textElements = $slideNote->xpath('//a:t');
|
||||
foreach ($textElements as $textElement) {
|
||||
$documentBody[] = (string)$textElement;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Read core properties
|
||||
$coreProperties = $this->extractMetaData($package);
|
||||
|
||||
@ -154,25 +154,25 @@ class Zend_Search_Lucene_Document_Pptx extends Zend_Search_Lucene_Document_OpenX
|
||||
$package->close();
|
||||
|
||||
// Store filename
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('filename', $fileName));
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('filename', $fileName, 'UTF-8'));
|
||||
|
||||
// Store contents
|
||||
if ($storeContent) {
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('body', implode(' ', $documentBody)));
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('body', implode(' ', $documentBody), 'UTF-8'));
|
||||
} else {
|
||||
$this->addField(Zend_Search_Lucene_Field::UnStored('body', implode(' ', $documentBody)));
|
||||
$this->addField(Zend_Search_Lucene_Field::UnStored('body', implode(' ', $documentBody), 'UTF-8'));
|
||||
}
|
||||
|
||||
// Store meta data properties
|
||||
foreach ($coreProperties as $key => $value)
|
||||
{
|
||||
$this->addField(Zend_Search_Lucene_Field::Text($key, $value));
|
||||
$this->addField(Zend_Search_Lucene_Field::Text($key, $value, 'UTF-8'));
|
||||
}
|
||||
|
||||
// Store title (if not present in meta data)
|
||||
if (!isset($coreProperties['title']))
|
||||
{
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('title', $fileName));
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('title', $fileName, 'UTF-8'));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,7 @@
|
||||
/** Zend_Search_Lucene_Document_OpenXml */
|
||||
require_once 'Zend/Search/Lucene/Document/OpenXml.php';
|
||||
|
||||
if (class_exists('ZipArchive')) {
|
||||
if (class_exists('ZipArchive', false)) {
|
||||
|
||||
/**
|
||||
* Xlsx document.
|
||||
@ -42,21 +42,21 @@ class Zend_Search_Lucene_Document_Xlsx extends Zend_Search_Lucene_Document_OpenX
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_SPREADSHEETML = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main';
|
||||
|
||||
|
||||
/**
|
||||
* Xml Schema - DrawingML
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_DRAWINGML = 'http://schemas.openxmlformats.org/drawingml/2006/main';
|
||||
|
||||
const SCHEMA_DRAWINGML = 'http://schemas.openxmlformats.org/drawingml/2006/main';
|
||||
|
||||
/**
|
||||
* Xml Schema - Shared Strings
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_SHAREDSTRINGS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings';
|
||||
|
||||
|
||||
/**
|
||||
* Xml Schema - Worksheet relation
|
||||
*
|
||||
@ -70,7 +70,7 @@ class Zend_Search_Lucene_Document_Xlsx extends Zend_Search_Lucene_Document_OpenX
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_SLIDENOTESRELATION = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide';
|
||||
|
||||
|
||||
/**
|
||||
* Object constructor
|
||||
*
|
||||
@ -84,7 +84,7 @@ class Zend_Search_Lucene_Document_Xlsx extends Zend_Search_Lucene_Document_OpenX
|
||||
$worksheets = array();
|
||||
$documentBody = array();
|
||||
$coreProperties = array();
|
||||
|
||||
|
||||
// Open OpenXML package
|
||||
$package = new ZipArchive();
|
||||
$package->open($fileName);
|
||||
@ -96,11 +96,11 @@ class Zend_Search_Lucene_Document_Xlsx extends Zend_Search_Lucene_Document_OpenX
|
||||
// Found office document! Read relations for workbook...
|
||||
$workbookRelations = simplexml_load_string($package->getFromName( $this->absoluteZipPath(dirname($rel["Target"]) . "/_rels/" . basename($rel["Target"]) . ".rels")) );
|
||||
$workbookRelations->registerXPathNamespace("rel", Zend_Search_Lucene_Document_OpenXml::SCHEMA_RELATIONSHIP);
|
||||
|
||||
|
||||
// Read shared strings
|
||||
$sharedStringsPath = $workbookRelations->xpath("rel:Relationship[@Type='" . Zend_Search_Lucene_Document_Xlsx::SCHEMA_SHAREDSTRINGS . "']");
|
||||
$sharedStringsPath = (string)$sharedStringsPath[0]['Target'];
|
||||
$xmlStrings = simplexml_load_string($package->getFromName( $this->absoluteZipPath(dirname($rel["Target"]) . "/" . $sharedStringsPath)) );
|
||||
$sharedStringsPath = (string)$sharedStringsPath[0]['Target'];
|
||||
$xmlStrings = simplexml_load_string($package->getFromName( $this->absoluteZipPath(dirname($rel["Target"]) . "/" . $sharedStringsPath)) );
|
||||
if (isset($xmlStrings) && isset($xmlStrings->si)) {
|
||||
foreach ($xmlStrings->si as $val) {
|
||||
if (isset($val->t)) {
|
||||
@ -119,14 +119,14 @@ class Zend_Search_Lucene_Document_Xlsx extends Zend_Search_Lucene_Document_OpenX
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Sort worksheets
|
||||
ksort($worksheets);
|
||||
|
||||
|
||||
// Extract contents from worksheets
|
||||
foreach ($worksheets as $sheetKey => $worksheet) {
|
||||
foreach ($worksheet->sheetData->row as $row) {
|
||||
@ -143,7 +143,7 @@ class Zend_Search_Lucene_Document_Xlsx extends Zend_Search_Lucene_Document_OpenX
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
|
||||
case "b":
|
||||
// Value is boolean
|
||||
$value = (string)$c->v;
|
||||
@ -156,13 +156,13 @@ class Zend_Search_Lucene_Document_Xlsx extends Zend_Search_Lucene_Document_OpenX
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
|
||||
case "inlineStr":
|
||||
// Value is rich text inline
|
||||
$value = $this->_parseRichText($c->is);
|
||||
|
||||
|
||||
break;
|
||||
|
||||
|
||||
case "e":
|
||||
// Value is an error message
|
||||
if ((string)$c->v != '') {
|
||||
@ -184,11 +184,11 @@ class Zend_Search_Lucene_Document_Xlsx extends Zend_Search_Lucene_Document_OpenX
|
||||
elseif ($value == (double)$value) $value = (double)$value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$documentBody[] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Read core properties
|
||||
$coreProperties = $this->extractMetaData($package);
|
||||
@ -197,28 +197,28 @@ class Zend_Search_Lucene_Document_Xlsx extends Zend_Search_Lucene_Document_OpenX
|
||||
$package->close();
|
||||
|
||||
// Store filename
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('filename', $fileName));
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('filename', $fileName, 'UTF-8'));
|
||||
|
||||
// Store contents
|
||||
if ($storeContent) {
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('body', implode(' ', $documentBody)));
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('body', implode(' ', $documentBody), 'UTF-8'));
|
||||
} else {
|
||||
$this->addField(Zend_Search_Lucene_Field::UnStored('body', implode(' ', $documentBody)));
|
||||
$this->addField(Zend_Search_Lucene_Field::UnStored('body', implode(' ', $documentBody), 'UTF-8'));
|
||||
}
|
||||
|
||||
// Store meta data properties
|
||||
foreach ($coreProperties as $key => $value)
|
||||
{
|
||||
$this->addField(Zend_Search_Lucene_Field::Text($key, $value));
|
||||
$this->addField(Zend_Search_Lucene_Field::Text($key, $value, 'UTF-8'));
|
||||
}
|
||||
|
||||
// Store title (if not present in meta data)
|
||||
if (!isset($coreProperties['title']))
|
||||
{
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('title', $fileName));
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('title', $fileName, 'UTF-8'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parse rich text XML
|
||||
*
|
||||
|
Reference in New Issue
Block a user