Docx.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. */
  47. private function __construct($fileName, $storeContent) {
  48. // Document data holders
  49. $documentBody = array();
  50. $coreProperties = array();
  51. // Open OpenXML package
  52. $package = new ZipArchive();
  53. $package->open($fileName);
  54. // Read relations and search for officeDocument
  55. $relations = simplexml_load_string($package->getFromName('_rels/.rels'));
  56. foreach($relations->Relationship as $rel) {
  57. if ($rel ["Type"] == Zend_Search_Lucene_Document_OpenXml::SCHEMA_OFFICEDOCUMENT) {
  58. // Found office document! Read in contents...
  59. $contents = simplexml_load_string($package->getFromName(
  60. $this->absoluteZipPath(dirname($rel['Target'])
  61. . '/'
  62. . basename($rel['Target']))
  63. ));
  64. $contents->registerXPathNamespace('w', Zend_Search_Lucene_Document_Docx::SCHEMA_WORDPROCESSINGML);
  65. $paragraphs = $contents->xpath('//w:body/w:p');
  66. foreach ($paragraphs as $paragraph) {
  67. $runs = $paragraph->xpath('.//w:r/*[name() = "w:t" or name() = "w:br"]');
  68. if ($runs === false) {
  69. // Paragraph doesn't contain any text or breaks
  70. continue;
  71. }
  72. foreach ($runs as $run) {
  73. if ($run->getName() == 'br') {
  74. // Break element
  75. $documentBody[] = ' ';
  76. } else {
  77. $documentBody[] = (string)$run;
  78. }
  79. }
  80. // Add space after each paragraph. So they are not bound together.
  81. $documentBody[] = ' ';
  82. }
  83. break;
  84. }
  85. }
  86. // Read core properties
  87. $coreProperties = $this->extractMetaData($package);
  88. // Close file
  89. $package->close();
  90. // Store filename
  91. $this->addField(Zend_Search_Lucene_Field::Text('filename', $fileName, 'UTF-8'));
  92. // Store contents
  93. if ($storeContent) {
  94. $this->addField(Zend_Search_Lucene_Field::Text('body', implode('', $documentBody), 'UTF-8'));
  95. } else {
  96. $this->addField(Zend_Search_Lucene_Field::UnStored('body', implode('', $documentBody), 'UTF-8'));
  97. }
  98. // Store meta data properties
  99. foreach ($coreProperties as $key => $value) {
  100. $this->addField(Zend_Search_Lucene_Field::Text($key, $value, 'UTF-8'));
  101. }
  102. // Store title (if not present in meta data)
  103. if (! isset($coreProperties['title'])) {
  104. $this->addField(Zend_Search_Lucene_Field::Text('title', $fileName, 'UTF-8'));
  105. }
  106. }
  107. /**
  108. * Load Docx document from a file
  109. *
  110. * @param string $fileName
  111. * @param boolean $storeContent
  112. * @return Zend_Search_Lucene_Document_Docx
  113. * @throws Zend_Search_Lucene_Document_Exception
  114. */
  115. public static function loadDocxFile($fileName, $storeContent = false) {
  116. if (!is_readable($fileName)) {
  117. require_once 'Zend/Search/Lucene/Document/Exception.php';
  118. throw new Zend_Search_Lucene_Document_Exception('Provided file \'' . $fileName . '\' is not readable.');
  119. }
  120. return new Zend_Search_Lucene_Document_Docx($fileName, $storeContent);
  121. }
  122. }
  123. } // end if (class_exists('ZipArchive'))