import v1.0.0-RC4 | 2009-05-20
This commit is contained in:
119
libs/Zend/Search/Lucene/Document/Docx.php
Normal file
119
libs/Zend/Search/Lucene/Document/Docx.php
Normal file
@ -0,0 +1,119 @@
|
||||
<?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
|
||||
* @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
|
||||
*/
|
||||
|
||||
/** Zend_Search_Lucene_Document_OpenXml */
|
||||
require_once 'Zend/Search/Lucene/Document/OpenXml.php';
|
||||
|
||||
if (class_exists ( 'ZipArchive' )) {
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 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 ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 );
|
||||
}
|
||||
}
|
||||
|
||||
} // end if (class_exists('ZipArchive'))
|
343
libs/Zend/Search/Lucene/Document/Html.php
Normal file
343
libs/Zend/Search/Lucene/Document/Html.php
Normal file
@ -0,0 +1,343 @@
|
||||
<?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
|
||||
* @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
|
||||
*/
|
||||
|
||||
|
||||
/** Zend_Search_Lucene_Document */
|
||||
require_once 'Zend/Search/Lucene/Document.php';
|
||||
|
||||
|
||||
/**
|
||||
* HTML 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_Html extends Zend_Search_Lucene_Document
|
||||
{
|
||||
/**
|
||||
* List of document links
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $_links = array();
|
||||
|
||||
/**
|
||||
* List of document header links
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $_headerLinks = array();
|
||||
|
||||
/**
|
||||
* Stored DOM representation
|
||||
*
|
||||
* @var DOMDocument
|
||||
*/
|
||||
private $_doc;
|
||||
|
||||
/**
|
||||
* Exclud nofollow links flag
|
||||
*
|
||||
* If true then links with rel='nofollow' attribute are not included into
|
||||
* document links.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
private static $_excludeNoFollowLinks = false;
|
||||
|
||||
/**
|
||||
* Object constructor
|
||||
*
|
||||
* @param string $data
|
||||
* @param boolean $isFile
|
||||
* @param boolean $storeContent
|
||||
*/
|
||||
private function __construct($data, $isFile, $storeContent)
|
||||
{
|
||||
$this->_doc = new DOMDocument();
|
||||
$this->_doc->substituteEntities = true;
|
||||
|
||||
if ($isFile) {
|
||||
$htmlData = file_get_contents($data);
|
||||
} else {
|
||||
$htmlData = $data;
|
||||
}
|
||||
@$this->_doc->loadHTML($htmlData);
|
||||
|
||||
$xpath = new DOMXPath($this->_doc);
|
||||
|
||||
$docTitle = '';
|
||||
$titleNodes = $xpath->query('/html/head/title');
|
||||
foreach ($titleNodes as $titleNode) {
|
||||
// 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));
|
||||
|
||||
$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));
|
||||
}
|
||||
|
||||
$docBody = '';
|
||||
$bodyNodes = $xpath->query('/html/body');
|
||||
foreach ($bodyNodes as $bodyNode) {
|
||||
// body should always have only one entry, but we process all nodeset entries
|
||||
$this->_retrieveNodeText($bodyNode, $docBody);
|
||||
}
|
||||
if ($storeContent) {
|
||||
$this->addField(Zend_Search_Lucene_Field::Text('body', $docBody, $this->_doc->actualEncoding));
|
||||
} else {
|
||||
$this->addField(Zend_Search_Lucene_Field::UnStored('body', $docBody, $this->_doc->actualEncoding));
|
||||
}
|
||||
|
||||
$linkNodes = $this->_doc->getElementsByTagName('a');
|
||||
foreach ($linkNodes as $linkNode) {
|
||||
if (($href = $linkNode->getAttribute('href')) != '' &&
|
||||
(!self::$_excludeNoFollowLinks || strtolower($linkNode->getAttribute('rel')) != 'nofollow' )
|
||||
) {
|
||||
$this->_links[] = $href;
|
||||
}
|
||||
}
|
||||
$this->_links = array_unique($this->_links);
|
||||
|
||||
$linkNodes = $xpath->query('/html/head/link');
|
||||
foreach ($linkNodes as $linkNode) {
|
||||
if (($href = $linkNode->getAttribute('href')) != '') {
|
||||
$this->_headerLinks[] = $href;
|
||||
}
|
||||
}
|
||||
$this->_headerLinks = array_unique($this->_headerLinks);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set exclude nofollow links flag
|
||||
*
|
||||
* @param boolean $newValue
|
||||
*/
|
||||
public static function setExcludeNoFollowLinks($newValue)
|
||||
{
|
||||
self::$_excludeNoFollowLinks = $newValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get exclude nofollow links flag
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function getExcludeNoFollowLinks()
|
||||
{
|
||||
return self::$_excludeNoFollowLinks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get node text
|
||||
*
|
||||
* We should exclude scripts, which may be not included into comment tags, CDATA sections,
|
||||
*
|
||||
* @param DOMNode $node
|
||||
* @param string &$text
|
||||
*/
|
||||
private function _retrieveNodeText(DOMNode $node, &$text)
|
||||
{
|
||||
if ($node->nodeType == XML_TEXT_NODE) {
|
||||
$text .= $node->nodeValue ;
|
||||
$text .= ' ';
|
||||
} else if ($node->nodeType == XML_ELEMENT_NODE && $node->nodeName != 'script') {
|
||||
foreach ($node->childNodes as $childNode) {
|
||||
$this->_retrieveNodeText($childNode, $text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get document HREF links
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getLinks()
|
||||
{
|
||||
return $this->_links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get document header links
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getHeaderLinks()
|
||||
{
|
||||
return $this->_headerLinks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load HTML document from a string
|
||||
*
|
||||
* @param string $data
|
||||
* @param boolean $storeContent
|
||||
* @return Zend_Search_Lucene_Document_Html
|
||||
*/
|
||||
public static function loadHTML($data, $storeContent = false)
|
||||
{
|
||||
return new Zend_Search_Lucene_Document_Html($data, false, $storeContent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load HTML document from a file
|
||||
*
|
||||
* @param string $file
|
||||
* @param boolean $storeContent
|
||||
* @return Zend_Search_Lucene_Document_Html
|
||||
*/
|
||||
public static function loadHTMLFile($file, $storeContent = false)
|
||||
{
|
||||
return new Zend_Search_Lucene_Document_Html($file, true, $storeContent);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Highlight text in text node
|
||||
*
|
||||
* @param DOMText $node
|
||||
* @param array $wordsToHighlight
|
||||
* @param string $color
|
||||
*/
|
||||
public function _highlightTextNode(DOMText $node, $wordsToHighlight, $color)
|
||||
{
|
||||
$analyzer = Zend_Search_Lucene_Analysis_Analyzer::getDefault();
|
||||
$analyzer->setInput($node->nodeValue, $this->_doc->encoding);
|
||||
|
||||
$matchedTokens = array();
|
||||
|
||||
while (($token = $analyzer->nextToken()) !== null) {
|
||||
if (isset($wordsToHighlight[$token->getTermText()])) {
|
||||
$matchedTokens[] = $token;
|
||||
}
|
||||
}
|
||||
|
||||
if (count($matchedTokens) == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$matchedTokens = array_reverse($matchedTokens);
|
||||
|
||||
foreach ($matchedTokens as $token) {
|
||||
// Cut text after matched token
|
||||
$node->splitText($token->getEndOffset());
|
||||
|
||||
// Cut matched node
|
||||
$matchedWordNode = $node->splitText($token->getStartOffset());
|
||||
|
||||
$highlightedNode = $this->_doc->createElement('b', $matchedWordNode->nodeValue);
|
||||
$highlightedNode->setAttribute('style', 'color:black;background-color:' . $color);
|
||||
|
||||
$node->parentNode->replaceChild($highlightedNode, $matchedWordNode);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* highlight words in content of the specified node
|
||||
*
|
||||
* @param DOMNode $contextNode
|
||||
* @param array $wordsToHighlight
|
||||
* @param string $color
|
||||
*/
|
||||
public function _highlightNode(DOMNode $contextNode, $wordsToHighlight, $color)
|
||||
{
|
||||
$textNodes = array();
|
||||
|
||||
if (!$contextNode->hasChildNodes()) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($contextNode->childNodes as $childNode) {
|
||||
if ($childNode->nodeType == XML_TEXT_NODE) {
|
||||
// process node later to leave childNodes structure untouched
|
||||
$textNodes[] = $childNode;
|
||||
} else {
|
||||
// Skip script nodes
|
||||
if ($childNode->nodeName != 'script') {
|
||||
$this->_highlightNode($childNode, $wordsToHighlight, $color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($textNodes as $textNode) {
|
||||
$this->_highlightTextNode($textNode, $wordsToHighlight, $color);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Highlight text with specified color
|
||||
*
|
||||
* @param string|array $words
|
||||
* @param string $color
|
||||
* @return string
|
||||
*/
|
||||
public function highlight($words, $color = '#66ffff')
|
||||
{
|
||||
if (!is_array($words)) {
|
||||
$words = array($words);
|
||||
}
|
||||
$wordsToHighlight = array();
|
||||
|
||||
$analyzer = Zend_Search_Lucene_Analysis_Analyzer::getDefault();
|
||||
foreach ($words as $wordString) {
|
||||
$wordsToHighlight = array_merge($wordsToHighlight, $analyzer->tokenize($wordString));
|
||||
}
|
||||
|
||||
if (count($wordsToHighlight) == 0) {
|
||||
return $this->_doc->saveHTML();
|
||||
}
|
||||
|
||||
$wordsToHighlightFlipped = array();
|
||||
foreach ($wordsToHighlight as $id => $token) {
|
||||
$wordsToHighlightFlipped[$token->getTermText()] = $id;
|
||||
}
|
||||
|
||||
$xpath = new DOMXPath($this->_doc);
|
||||
|
||||
$matchedNodes = $xpath->query("/html/body");
|
||||
foreach ($matchedNodes as $matchedNode) {
|
||||
$this->_highlightNode($matchedNode, $wordsToHighlightFlipped, $color);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get HTML
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getHTML()
|
||||
{
|
||||
return $this->_doc->saveHTML();
|
||||
}
|
||||
}
|
||||
|
131
libs/Zend/Search/Lucene/Document/OpenXml.php
Normal file
131
libs/Zend/Search/Lucene/Document/OpenXml.php
Normal file
@ -0,0 +1,131 @@
|
||||
<?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
|
||||
* @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
|
||||
*/
|
||||
|
||||
|
||||
/** Zend_Search_Lucene_Document */
|
||||
require_once 'Zend/Search/Lucene/Document.php';
|
||||
|
||||
if (class_exists('ZipArchive')) {
|
||||
|
||||
/**
|
||||
* OpenXML 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
|
||||
*/
|
||||
abstract class Zend_Search_Lucene_Document_OpenXml extends Zend_Search_Lucene_Document
|
||||
{
|
||||
/**
|
||||
* Xml Schema - Relationships
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_RELATIONSHIP = 'http://schemas.openxmlformats.org/package/2006/relationships';
|
||||
|
||||
/**
|
||||
* Xml Schema - Office document
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_OFFICEDOCUMENT = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument';
|
||||
|
||||
/**
|
||||
* Xml Schema - Core properties
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_COREPROPERTIES = 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties';
|
||||
|
||||
/**
|
||||
* Xml Schema - Dublin Core
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_DUBLINCORE = 'http://purl.org/dc/elements/1.1/';
|
||||
|
||||
/**
|
||||
* Xml Schema - Dublin Core Terms
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_DUBLINCORETERMS = 'http://purl.org/dc/terms/';
|
||||
|
||||
/**
|
||||
* Extract metadata from document
|
||||
*
|
||||
* @param ZipArchive $package ZipArchive OpenXML package
|
||||
* @return array Key-value pairs containing document meta data
|
||||
*/
|
||||
protected function extractMetaData(ZipArchive $package)
|
||||
{
|
||||
// Data holders
|
||||
$coreProperties = array();
|
||||
|
||||
// Read relations and search for core properties
|
||||
$relations = simplexml_load_string($package->getFromName("_rels/.rels"));
|
||||
foreach ($relations->Relationship as $rel) {
|
||||
if ($rel["Type"] == Zend_Search_Lucene_Document_OpenXml::SCHEMA_COREPROPERTIES) {
|
||||
// Found core properties! Read in contents...
|
||||
$contents = simplexml_load_string(
|
||||
$package->getFromName(dirname($rel["Target"]) . "/" . basename($rel["Target"]))
|
||||
);
|
||||
|
||||
foreach ($contents->children(Zend_Search_Lucene_Document_OpenXml::SCHEMA_DUBLINCORE) as $child) {
|
||||
$coreProperties[$child->getName()] = (string)$child;
|
||||
}
|
||||
foreach ($contents->children(Zend_Search_Lucene_Document_OpenXml::SCHEMA_COREPROPERTIES) as $child) {
|
||||
$coreProperties[$child->getName()] = (string)$child;
|
||||
}
|
||||
foreach ($contents->children(Zend_Search_Lucene_Document_OpenXml::SCHEMA_DUBLINCORETERMS) as $child) {
|
||||
$coreProperties[$child->getName()] = (string)$child;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $coreProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine absolute zip path
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
protected function absoluteZipPath($path) {
|
||||
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
|
||||
$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
|
||||
$absolutes = array();
|
||||
foreach ($parts as $part) {
|
||||
if ('.' == $part) continue;
|
||||
if ('..' == $part) {
|
||||
array_pop($absolutes);
|
||||
} else {
|
||||
$absolutes[] = $part;
|
||||
}
|
||||
}
|
||||
return implode('/', $absolutes);
|
||||
}
|
||||
}
|
||||
|
||||
} // end if (class_exists('ZipArchive'))
|
192
libs/Zend/Search/Lucene/Document/Pptx.php
Normal file
192
libs/Zend/Search/Lucene/Document/Pptx.php
Normal file
@ -0,0 +1,192 @@
|
||||
<?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
|
||||
* @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
|
||||
*/
|
||||
|
||||
|
||||
/** Zend_Search_Lucene_Document_OpenXml */
|
||||
require_once 'Zend/Search/Lucene/Document/OpenXml.php';
|
||||
|
||||
if (class_exists('ZipArchive')) {
|
||||
|
||||
/**
|
||||
* Pptx 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_Pptx extends Zend_Search_Lucene_Document_OpenXml
|
||||
{
|
||||
/**
|
||||
* Xml Schema - PresentationML
|
||||
*
|
||||
* @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';
|
||||
|
||||
/**
|
||||
* Xml Schema - Slide relation
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_SLIDERELATION = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide';
|
||||
|
||||
/**
|
||||
* Xml Schema - Slide notes relation
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_SLIDENOTESRELATION = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide';
|
||||
|
||||
/**
|
||||
* Object constructor
|
||||
*
|
||||
* @param string $fileName
|
||||
* @param boolean $storeContent
|
||||
*/
|
||||
private function __construct($fileName, $storeContent)
|
||||
{
|
||||
// Document data holders
|
||||
$slides = array();
|
||||
$slideNotes = array();
|
||||
$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! Search for slides...
|
||||
$slideRelations = simplexml_load_string($package->getFromName( $this->absoluteZipPath(dirname($rel["Target"]) . "/_rels/" . basename($rel["Target"]) . ".rels")) );
|
||||
foreach ($slideRelations->Relationship as $slideRel) {
|
||||
if ($slideRel["Type"] == Zend_Search_Lucene_Document_Pptx::SCHEMA_SLIDERELATION) {
|
||||
// Found slide!
|
||||
$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) {
|
||||
if ($slideNoteRel["Type"] == Zend_Search_Lucene_Document_Pptx::SCHEMA_SLIDENOTESRELATION) {
|
||||
// Found slide notes!
|
||||
$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) {
|
||||
$documentBody[] = (string)$textElement;
|
||||
}
|
||||
|
||||
// Extract contents from slide notes
|
||||
if (isset($slideNotes[$slideKey])) {
|
||||
// Fetch slide note
|
||||
$slideNote = $slideNotes[$slideKey];
|
||||
|
||||
// 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);
|
||||
|
||||
// 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));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load Pptx document from a file
|
||||
*
|
||||
* @param string $fileName
|
||||
* @param boolean $storeContent
|
||||
* @return Zend_Search_Lucene_Document_Pptx
|
||||
*/
|
||||
public static function loadPptxFile($fileName, $storeContent = false)
|
||||
{
|
||||
return new Zend_Search_Lucene_Document_Pptx($fileName, $storeContent);
|
||||
}
|
||||
}
|
||||
|
||||
} // end if (class_exists('ZipArchive'))
|
255
libs/Zend/Search/Lucene/Document/Xlsx.php
Normal file
255
libs/Zend/Search/Lucene/Document/Xlsx.php
Normal file
@ -0,0 +1,255 @@
|
||||
<?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
|
||||
* @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
|
||||
*/
|
||||
|
||||
|
||||
/** Zend_Search_Lucene_Document_OpenXml */
|
||||
require_once 'Zend/Search/Lucene/Document/OpenXml.php';
|
||||
|
||||
if (class_exists('ZipArchive')) {
|
||||
|
||||
/**
|
||||
* Xlsx 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_Xlsx extends Zend_Search_Lucene_Document_OpenXml
|
||||
{
|
||||
/**
|
||||
* Xml Schema - SpreadsheetML
|
||||
*
|
||||
* @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';
|
||||
|
||||
/**
|
||||
* Xml Schema - Shared Strings
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_SHAREDSTRINGS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings';
|
||||
|
||||
/**
|
||||
* Xml Schema - Worksheet relation
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_WORKSHEETRELATION = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet';
|
||||
|
||||
/**
|
||||
* Xml Schema - Slide notes relation
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_SLIDENOTESRELATION = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide';
|
||||
|
||||
/**
|
||||
* Object constructor
|
||||
*
|
||||
* @param string $fileName
|
||||
* @param boolean $storeContent
|
||||
*/
|
||||
private function __construct($fileName, $storeContent)
|
||||
{
|
||||
// Document data holders
|
||||
$sharedStrings = array();
|
||||
$worksheets = array();
|
||||
$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 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)) );
|
||||
if (isset($xmlStrings) && isset($xmlStrings->si)) {
|
||||
foreach ($xmlStrings->si as $val) {
|
||||
if (isset($val->t)) {
|
||||
$sharedStrings[] = (string)$val->t;
|
||||
} elseif (isset($val->r)) {
|
||||
$sharedStrings[] = $this->_parseRichText($val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Loop relations for workbook and extract worksheets...
|
||||
foreach ($workbookRelations->Relationship as $workbookRelation) {
|
||||
if ($workbookRelation["Type"] == Zend_Search_Lucene_Document_Xlsx::SCHEMA_WORKSHEETRELATION) {
|
||||
$worksheets[ str_replace( 'rId', '', (string)$workbookRelation["Id"]) ] = simplexml_load_string(
|
||||
$package->getFromName( $this->absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($workbookRelation["Target"]) . "/" . basename($workbookRelation["Target"])) )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Sort worksheets
|
||||
ksort($worksheets);
|
||||
|
||||
// Extract contents from worksheets
|
||||
foreach ($worksheets as $sheetKey => $worksheet) {
|
||||
foreach ($worksheet->sheetData->row as $row) {
|
||||
foreach ($row->c as $c) {
|
||||
// Determine data type
|
||||
$dataType = (string)$c["t"];
|
||||
switch ($dataType) {
|
||||
case "s":
|
||||
// Value is a shared string
|
||||
if ((string)$c->v != '') {
|
||||
$value = $sharedStrings[intval($c->v)];
|
||||
} else {
|
||||
$value = '';
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "b":
|
||||
// Value is boolean
|
||||
$value = (string)$c->v;
|
||||
if ($value == '0') {
|
||||
$value = false;
|
||||
} else if ($value == '1') {
|
||||
$value = true;
|
||||
} else {
|
||||
$value = (bool)$c->v;
|
||||
}
|
||||
|
||||
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 != '') {
|
||||
$value = (string)$c->v;
|
||||
} else {
|
||||
$value = '';
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
// Value is a string
|
||||
$value = (string)$c->v;
|
||||
|
||||
// Check for numeric values
|
||||
if (is_numeric($value) && $dataType != 's') {
|
||||
if ($value == (int)$value) $value = (int)$value;
|
||||
elseif ($value == (float)$value) $value = (float)$value;
|
||||
elseif ($value == (double)$value) $value = (double)$value;
|
||||
}
|
||||
}
|
||||
|
||||
$documentBody[] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse rich text XML
|
||||
*
|
||||
* @param SimpleXMLElement $is
|
||||
* @return string
|
||||
*/
|
||||
private function _parseRichText($is = null) {
|
||||
$value = array();
|
||||
|
||||
if (isset($is->t)) {
|
||||
$value[] = (string)$is->t;
|
||||
} else {
|
||||
foreach ($is->r as $run) {
|
||||
$value[] = (string)$run->t;
|
||||
}
|
||||
}
|
||||
|
||||
return implode('', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load Xlsx document from a file
|
||||
*
|
||||
* @param string $fileName
|
||||
* @param boolean $storeContent
|
||||
* @return Zend_Search_Lucene_Document_Xlsx
|
||||
*/
|
||||
public static function loadXlsxFile($fileName, $storeContent = false)
|
||||
{
|
||||
return new Zend_Search_Lucene_Document_Xlsx($fileName, $storeContent);
|
||||
}
|
||||
}
|
||||
|
||||
} // end if (class_exists('ZipArchive'))
|
Reference in New Issue
Block a user