Docx.php 5.5 KB

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