/modules/AOD_Index/Lib/Zend/Search/Lucene/Document/Pptx.php

https://gitlab.com/tjaafar/SuiteCRM · PHP · 200 lines · 89 code · 26 blank · 85 comment · 12 complexity · f03effd1de88db7030283667dbd55902 MD5 · raw file

  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: Pptx.php 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. /** Zend_Search_Lucene_Document_OpenXml */
  23. require_once 'Zend/Search/Lucene/Document/OpenXml.php';
  24. /**
  25. * Pptx 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_Pptx extends Zend_Search_Lucene_Document_OpenXml
  34. {
  35. /**
  36. * Xml Schema - PresentationML
  37. *
  38. * @var string
  39. */
  40. const SCHEMA_PRESENTATIONML = 'http://schemas.openxmlformats.org/presentationml/2006/main';
  41. /**
  42. * Xml Schema - DrawingML
  43. *
  44. * @var string
  45. */
  46. const SCHEMA_DRAWINGML = 'http://schemas.openxmlformats.org/drawingml/2006/main';
  47. /**
  48. * Xml Schema - Slide relation
  49. *
  50. * @var string
  51. */
  52. const SCHEMA_SLIDERELATION = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide';
  53. /**
  54. * Xml Schema - Slide notes relation
  55. *
  56. * @var string
  57. */
  58. const SCHEMA_SLIDENOTESRELATION = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide';
  59. /**
  60. * Object constructor
  61. *
  62. * @param string $fileName
  63. * @param boolean $storeContent
  64. * @throws Zend_Search_Lucene_Exception
  65. */
  66. private function __construct($fileName, $storeContent)
  67. {
  68. if (!class_exists('ZipArchive', false)) {
  69. require_once 'Zend/Search/Lucene/Exception.php';
  70. throw new Zend_Search_Lucene_Exception('MS Office documents processing functionality requires Zip extension to be loaded');
  71. }
  72. // Document data holders
  73. $slides = array();
  74. $slideNotes = array();
  75. $documentBody = array();
  76. $coreProperties = array();
  77. // Open OpenXML package
  78. $package = new ZipArchive();
  79. $package->open($fileName);
  80. // Read relations and search for officeDocument
  81. $relationsXml = $package->getFromName('_rels/.rels');
  82. if ($relationsXml === false) {
  83. require_once 'Zend/Search/Lucene/Exception.php';
  84. throw new Zend_Search_Lucene_Exception('Invalid archive or corrupted .pptx file.');
  85. }
  86. $relations = simplexml_load_string($relationsXml);
  87. foreach ($relations->Relationship as $rel) {
  88. if ($rel["Type"] == Zend_Search_Lucene_Document_OpenXml::SCHEMA_OFFICEDOCUMENT) {
  89. // Found office document! Search for slides...
  90. $slideRelations = simplexml_load_string($package->getFromName( $this->absoluteZipPath(dirname($rel["Target"]) . "/_rels/" . basename($rel["Target"]) . ".rels")) );
  91. foreach ($slideRelations->Relationship as $slideRel) {
  92. if ($slideRel["Type"] == Zend_Search_Lucene_Document_Pptx::SCHEMA_SLIDERELATION) {
  93. // Found slide!
  94. $slides[ str_replace( 'rId', '', (string)$slideRel["Id"] ) ] = simplexml_load_string(
  95. $package->getFromName( $this->absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($slideRel["Target"]) . "/" . basename($slideRel["Target"])) )
  96. );
  97. // Search for slide notes
  98. $slideNotesRelations = simplexml_load_string($package->getFromName( $this->absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($slideRel["Target"]) . "/_rels/" . basename($slideRel["Target"]) . ".rels")) );
  99. foreach ($slideNotesRelations->Relationship as $slideNoteRel) {
  100. if ($slideNoteRel["Type"] == Zend_Search_Lucene_Document_Pptx::SCHEMA_SLIDENOTESRELATION) {
  101. // Found slide notes!
  102. $slideNotes[ str_replace( 'rId', '', (string)$slideRel["Id"] ) ] = simplexml_load_string(
  103. $package->getFromName( $this->absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($slideRel["Target"]) . "/" . dirname($slideNoteRel["Target"]) . "/" . basename($slideNoteRel["Target"])) )
  104. );
  105. break;
  106. }
  107. }
  108. }
  109. }
  110. break;
  111. }
  112. }
  113. // Sort slides
  114. ksort($slides);
  115. ksort($slideNotes);
  116. // Extract contents from slides
  117. foreach ($slides as $slideKey => $slide) {
  118. // Register namespaces
  119. $slide->registerXPathNamespace("p", Zend_Search_Lucene_Document_Pptx::SCHEMA_PRESENTATIONML);
  120. $slide->registerXPathNamespace("a", Zend_Search_Lucene_Document_Pptx::SCHEMA_DRAWINGML);
  121. // Fetch all text
  122. $textElements = $slide->xpath('//a:t');
  123. foreach ($textElements as $textElement) {
  124. $documentBody[] = (string)$textElement;
  125. }
  126. // Extract contents from slide notes
  127. if (isset($slideNotes[$slideKey])) {
  128. // Fetch slide note
  129. $slideNote = $slideNotes[$slideKey];
  130. // Register namespaces
  131. $slideNote->registerXPathNamespace("p", Zend_Search_Lucene_Document_Pptx::SCHEMA_PRESENTATIONML);
  132. $slideNote->registerXPathNamespace("a", Zend_Search_Lucene_Document_Pptx::SCHEMA_DRAWINGML);
  133. // Fetch all text
  134. $textElements = $slideNote->xpath('//a:t');
  135. foreach ($textElements as $textElement) {
  136. $documentBody[] = (string)$textElement;
  137. }
  138. }
  139. }
  140. // Read core properties
  141. $coreProperties = $this->extractMetaData($package);
  142. // Close file
  143. $package->close();
  144. // Store filename
  145. $this->addField(Zend_Search_Lucene_Field::Text('filename', $fileName, 'UTF-8'));
  146. // Store contents
  147. if ($storeContent) {
  148. $this->addField(Zend_Search_Lucene_Field::Text('contents', implode(' ', $documentBody), 'UTF-8'));
  149. } else {
  150. $this->addField(Zend_Search_Lucene_Field::UnStored('contents', implode(' ', $documentBody), 'UTF-8'));
  151. }
  152. // Store meta data properties
  153. foreach ($coreProperties as $key => $value)
  154. {
  155. $this->addField(Zend_Search_Lucene_Field::Text($key, $value, 'UTF-8'));
  156. }
  157. // Store title (if not present in meta data)
  158. if (!isset($coreProperties['title']))
  159. {
  160. $this->addField(Zend_Search_Lucene_Field::Text('title', $fileName, 'UTF-8'));
  161. }
  162. }
  163. /**
  164. * Load Pptx document from a file
  165. *
  166. * @param string $fileName
  167. * @param boolean $storeContent
  168. * @return Zend_Search_Lucene_Document_Pptx
  169. */
  170. public static function loadPptxFile($fileName, $storeContent = false)
  171. {
  172. return new Zend_Search_Lucene_Document_Pptx($fileName, $storeContent);
  173. }
  174. }