final move of files
[web.mtrack] / Zend / Search / Lucene / Document / Docx.php
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-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 $
21  */
22
23 /** Zend_Search_Lucene_Document_OpenXml */
24 require_once 'Zend/Search/Lucene/Document/OpenXml.php';
25
26 if (class_exists('ZipArchive', false)) {
27
28 /**
29  * Docx document.
30  *
31  * @category   Zend
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
36  */
37 class Zend_Search_Lucene_Document_Docx extends Zend_Search_Lucene_Document_OpenXml {
38     /**
39      * Xml Schema - WordprocessingML
40      *
41      * @var string
42      */
43     const SCHEMA_WORDPROCESSINGML = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
44
45     /**
46      * Object constructor
47      *
48      * @param string  $fileName
49      * @param boolean $storeContent
50      */
51     private function __construct($fileName, $storeContent) {
52         // Document data holders
53         $documentBody = array();
54         $coreProperties = array();
55
56         // Open OpenXML package
57         $package = new ZipArchive();
58         $package->open($fileName);
59
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'])
67                                                               . '/'
68                                                               . basename($rel['Target']))
69                                                                        ));
70
71                 $contents->registerXPathNamespace('w', Zend_Search_Lucene_Document_Docx::SCHEMA_WORDPROCESSINGML);
72                 $paragraphs = $contents->xpath('//w:body/w:p');
73
74                 foreach ($paragraphs as $paragraph) {
75                     $runs = $paragraph->xpath('.//w:r/*[name() = "w:t" or name() = "w:br"]');
76
77                     if ($runs === false) {
78                         // Paragraph doesn't contain any text or breaks
79                         continue;
80                     }
81
82                     foreach ($runs as $run) {
83                      if ($run->getName() == 'br') {
84                          // Break element
85                          $documentBody[] = ' ';
86                      } else {
87                         $documentBody[] = (string)$run;
88                      }
89                     }
90
91                     // Add space after each paragraph. So they are not bound together.
92                     $documentBody[] = ' ';
93                 }
94
95                 break;
96             }
97         }
98
99         // Read core properties
100         $coreProperties = $this->extractMetaData($package);
101
102         // Close file
103         $package->close();
104
105         // Store filename
106         $this->addField(Zend_Search_Lucene_Field::Text('filename', $fileName, 'UTF-8'));
107
108         // Store contents
109         if ($storeContent) {
110             $this->addField(Zend_Search_Lucene_Field::Text('body', implode('', $documentBody), 'UTF-8'));
111         } else {
112             $this->addField(Zend_Search_Lucene_Field::UnStored('body', implode('', $documentBody), 'UTF-8'));
113         }
114
115         // Store meta data properties
116         foreach ($coreProperties as $key => $value) {
117             $this->addField(Zend_Search_Lucene_Field::Text($key, $value, 'UTF-8'));
118         }
119
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'));
123         }
124     }
125
126     /**
127      * Load Docx document from a file
128      *
129      * @param string  $fileName
130      * @param boolean $storeContent
131      * @return Zend_Search_Lucene_Document_Docx
132      * @throws Zend_Search_Lucene_Document_Exception
133      */
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.');
138         }
139
140         return new Zend_Search_Lucene_Document_Docx($fileName, $storeContent);
141     }
142 }
143
144 } // end if (class_exists('ZipArchive'))