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-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: Docx.php 16971 2009-07-22 18:05:45Z mikaelkael $
23 /** Zend_Search_Lucene_Document_OpenXml */
24 require_once 'Zend/Search/Lucene/Document/OpenXml.php';
26 if (class_exists('ZipArchive', false)) {
32 * @package Zend_Search_Lucene
33 * @subpackage Document
34 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
35 * @license http://framework.zend.com/license/new-bsd New BSD License
37 class Zend_Search_Lucene_Document_Docx extends Zend_Search_Lucene_Document_OpenXml {
39 * Xml Schema - WordprocessingML
43 const SCHEMA_WORDPROCESSINGML = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
48 * @param string $fileName
49 * @param boolean $storeContent
51 private function __construct($fileName, $storeContent) {
52 // Document data holders
53 $documentBody = array();
54 $coreProperties = array();
56 // Open OpenXML package
57 $package = new ZipArchive();
58 $package->open($fileName);
60 // Read relations and search for officeDocument
61 $relations = simplexml_load_string($package->getFromName('_rels/.rels'));
62 foreach($relations->Relationship as $rel) {
63 if ($rel ["Type"] == Zend_Search_Lucene_Document_OpenXml::SCHEMA_OFFICEDOCUMENT) {
64 // Found office document! Read in contents...
65 $contents = simplexml_load_string($package->getFromName(
66 $this->absoluteZipPath(dirname($rel['Target'])
68 . basename($rel['Target']))
71 $contents->registerXPathNamespace('w', Zend_Search_Lucene_Document_Docx::SCHEMA_WORDPROCESSINGML);
72 $paragraphs = $contents->xpath('//w:body/w:p');
74 foreach ($paragraphs as $paragraph) {
75 $runs = $paragraph->xpath('.//w:r/*[name() = "w:t" or name() = "w:br"]');
77 if ($runs === false) {
78 // Paragraph doesn't contain any text or breaks
82 foreach ($runs as $run) {
83 if ($run->getName() == 'br') {
85 $documentBody[] = ' ';
87 $documentBody[] = (string)$run;
91 // Add space after each paragraph. So they are not bound together.
92 $documentBody[] = ' ';
99 // Read core properties
100 $coreProperties = $this->extractMetaData($package);
106 $this->addField(Zend_Search_Lucene_Field::Text('filename', $fileName, 'UTF-8'));
110 $this->addField(Zend_Search_Lucene_Field::Text('body', implode('', $documentBody), 'UTF-8'));
112 $this->addField(Zend_Search_Lucene_Field::UnStored('body', implode('', $documentBody), 'UTF-8'));
115 // Store meta data properties
116 foreach ($coreProperties as $key => $value) {
117 $this->addField(Zend_Search_Lucene_Field::Text($key, $value, 'UTF-8'));
120 // Store title (if not present in meta data)
121 if (! isset($coreProperties['title'])) {
122 $this->addField(Zend_Search_Lucene_Field::Text('title', $fileName, 'UTF-8'));
127 * Load Docx document from a file
129 * @param string $fileName
130 * @param boolean $storeContent
131 * @return Zend_Search_Lucene_Document_Docx
132 * @throws Zend_Search_Lucene_Document_Exception
134 public static function loadDocxFile($fileName, $storeContent = false) {
135 if (!is_readable($fileName)) {
136 require_once 'Zend/Search/Lucene/Document/Exception.php';
137 throw new Zend_Search_Lucene_Document_Exception('Provided file \'' . $fileName . '\' is not readable.');
140 return new Zend_Search_Lucene_Document_Docx($fileName, $storeContent);
144 } // end if (class_exists('ZipArchive'))