Docx.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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$
  21. */
  22. /** Zend_Search_Lucene_Document_OpenXml */
  23. require_once 'Zend/Search/Lucene/Document/OpenXml.php';
  24. if (class_exists('ZipArchive', false)) {
  25. /**
  26. * Docx document.
  27. *
  28. * @category Zend
  29. * @package Zend_Search_Lucene
  30. * @subpackage Document
  31. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Search_Lucene_Document_Docx extends Zend_Search_Lucene_Document_OpenXml {
  35. /**
  36. * Xml Schema - WordprocessingML
  37. *
  38. * @var string
  39. */
  40. const SCHEMA_WORDPROCESSINGML = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
  41. /**
  42. * Object constructor
  43. *
  44. * @param string $fileName
  45. * @param boolean $storeContent
  46. * @throws Zend_Search_Lucene_Exception
  47. */
  48. private function __construct($fileName, $storeContent) {
  49. // Document data holders
  50. $documentBody = array();
  51. $coreProperties = array();
  52. // Open OpenXML package
  53. $package = new ZipArchive();
  54. $package->open($fileName);
  55. // Read relations and search for officeDocument
  56. $relationsXml = $package->getFromName('_rels/.rels');
  57. if ($relationsXml === false) {
  58. require_once 'Zend/Search/Lucene/Exception.php';
  59. throw new Zend_Search_Lucene_Exception('Invalid archive or corrupted .docx file.');
  60. }
  61. $relations = simplexml_load_string($relationsXml);
  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. $contents->registerXPathNamespace('w', Zend_Search_Lucene_Document_Docx::SCHEMA_WORDPROCESSINGML);
  71. $paragraphs = $contents->xpath('//w:body/w:p');
  72. foreach ($paragraphs as $paragraph) {
  73. $runs = $paragraph->xpath('.//w:r/*[name() = "w:t" or name() = "w:br"]');
  74. if ($runs === false) {
  75. // Paragraph doesn't contain any text or breaks
  76. continue;
  77. }
  78. foreach ($runs as $run) {
  79. if ($run->getName() == 'br') {
  80. // Break element
  81. $documentBody[] = ' ';
  82. } else {
  83. $documentBody[] = (string)$run;
  84. }
  85. }
  86. // Add space after each paragraph. So they are not bound together.
  87. $documentBody[] = ' ';
  88. }
  89. break;
  90. }
  91. }
  92. // Read core properties
  93. $coreProperties = $this->extractMetaData($package);
  94. // Close file
  95. $package->close();
  96. // Store filename
  97. $this->addField(Zend_Search_Lucene_Field::Text('filename', $fileName, 'UTF-8'));
  98. // Store contents
  99. if ($storeContent) {
  100. $this->addField(Zend_Search_Lucene_Field::Text('body', implode('', $documentBody), 'UTF-8'));
  101. } else {
  102. $this->addField(Zend_Search_Lucene_Field::UnStored('body', implode('', $documentBody), 'UTF-8'));
  103. }
  104. // Store meta data properties
  105. foreach ($coreProperties as $key => $value) {
  106. $this->addField(Zend_Search_Lucene_Field::Text($key, $value, 'UTF-8'));
  107. }
  108. // Store title (if not present in meta data)
  109. if (! isset($coreProperties['title'])) {
  110. $this->addField(Zend_Search_Lucene_Field::Text('title', $fileName, 'UTF-8'));
  111. }
  112. }
  113. /**
  114. * Load Docx document from a file
  115. *
  116. * @param string $fileName
  117. * @param boolean $storeContent
  118. * @return Zend_Search_Lucene_Document_Docx
  119. * @throws Zend_Search_Lucene_Document_Exception
  120. */
  121. public static function loadDocxFile($fileName, $storeContent = false) {
  122. if (!is_readable($fileName)) {
  123. require_once 'Zend/Search/Lucene/Document/Exception.php';
  124. throw new Zend_Search_Lucene_Document_Exception('Provided file \'' . $fileName . '\' is not readable.');
  125. }
  126. return new Zend_Search_Lucene_Document_Docx($fileName, $storeContent);
  127. }
  128. }
  129. } // end if (class_exists('ZipArchive'))