Pptx.php 7.4 KB

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