2
0

Docx.php 5.0 KB

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