Commit d83344dcceb4f06e9910d591c966c0f57885369c
1 parent
555d3398
Add OpenDocument indexing support (requires unzip)
git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@4989 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
2 changed files
with
81 additions
and
0 deletions
plugins/ktstandard/KTIndexer.php
| ... | ... | @@ -16,6 +16,8 @@ class KTIndexerPlugin extends KTPlugin { |
| 16 | 16 | 'ktstandard.indexer.triggers.pdf', 'contents/PdfIndexer.php'); |
| 17 | 17 | $this->registerTrigger('content', 'transform', 'KTPostscriptIndexerTrigger', |
| 18 | 18 | 'ktstandard.indexer.triggers.ps', 'contents/PsIndexer.php'); |
| 19 | + $this->registerTrigger('content', 'transform', 'KTOpenDocumentIndexerTrigger', | |
| 20 | + 'ktstandard.indexer.triggers.opendocument', 'contents/OpenDocumentIndexer.php'); | |
| 19 | 21 | } |
| 20 | 22 | } |
| 21 | 23 | ... | ... |
plugins/ktstandard/contents/OpenDocumentIndexer.php
0 → 100644
| 1 | +<?php | |
| 2 | + | |
| 3 | +require_once(KT_DIR . '/plugins/ktstandard/contents/BaseIndexer.php'); | |
| 4 | + | |
| 5 | +class KTOpenDocumentIndexerTrigger extends KTBaseIndexerTrigger { | |
| 6 | + var $mimetypes = array( | |
| 7 | + 'application/octet-stream' => true, | |
| 8 | + 'application/zip' => true, | |
| 9 | + 'application/x-zip' => true, | |
| 10 | + 'application/vnd.oasis.opendocument.text' => true, | |
| 11 | + 'application/vnd.oasis.opendocument.text-template' => true, | |
| 12 | + 'application/vnd.oasis.opendocument.presentation' => true, | |
| 13 | + 'application/vnd.oasis.opendocument.presentation-template' => true, | |
| 14 | + 'application/vnd.oasis.opendocument.spreadsheet' => true, | |
| 15 | + 'application/vnd.oasis.opendocument.spreadsheet-template' => true, | |
| 16 | + ); | |
| 17 | + | |
| 18 | + function transform() { | |
| 19 | + $iMimeTypeId = $this->oDocument->getMimeTypeId(); | |
| 20 | + $sMimeType = KTMime::getMimeTypeName($iMimeTypeId); | |
| 21 | + $sFileName = $this->oDocument->getFileName(); | |
| 22 | + if (in_array($sMimeType, array('application/octet-stream', 'application/zip', 'application/x-zip'))) { | |
| 23 | + $sExtension = KTMime::stripAllButExtension($sFileName); | |
| 24 | + $sTable = KTUtil::getTableName('mimetypes'); | |
| 25 | + $sQuery = "SELECT id FROM $sTable WHERE LOWER(filetypes) = ?"; | |
| 26 | + $aParams = array($sExtension); | |
| 27 | + $id = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id'); | |
| 28 | + if ($id) { | |
| 29 | + $this->oDocument->setMimeTypeId($id); | |
| 30 | + $this->oDocument->update(); | |
| 31 | + } | |
| 32 | + } | |
| 33 | + parent::transform(); | |
| 34 | + } | |
| 35 | + | |
| 36 | + function extract_contents($sFilename, $sTmpFilename) { | |
| 37 | + $sUnzipCommand = KTUtil::findCommand("import/unzip", "unzip"); | |
| 38 | + if (empty($sUnzipCommand)) { | |
| 39 | + return; | |
| 40 | + } | |
| 41 | + $this->sTmpPath = tempnam('/tmp', 'opendocumentextract'); | |
| 42 | + if ($this->sTmpPath === false) { | |
| 43 | + return; | |
| 44 | + } | |
| 45 | + unlink($this->sTmpPath); | |
| 46 | + mkdir($this->sTmpPath, 0700); | |
| 47 | + | |
| 48 | + $sCmd = array( | |
| 49 | + $sUnzipCommand, | |
| 50 | + "-q", "-n", | |
| 51 | + "-d", $this->sTmpPath, | |
| 52 | + $sFilename, | |
| 53 | + ); | |
| 54 | + KTUtil::pexec($sCmd); | |
| 55 | + | |
| 56 | + $sManifest = sprintf("%s/%s", $this->sTmpPath, "META-INF/manifest.xml"); | |
| 57 | + if (!file_exists($sManifest)) { | |
| 58 | + $this->cleanup(); | |
| 59 | + return; | |
| 60 | + } | |
| 61 | + $sContentFile = sprintf("%s/%s", $this->sTmpPath, "content.xml"); | |
| 62 | + if (!file_exists($sContentFile)) { | |
| 63 | + $this->cleanup(); | |
| 64 | + return; | |
| 65 | + } | |
| 66 | + | |
| 67 | + $sContent = file_get_contents($sContentFile); | |
| 68 | + $sContent = preg_replace ("@(</?[^>]*>)+@", " ", $sContent); | |
| 69 | + | |
| 70 | + $this->cleanup(); | |
| 71 | + return $sContent; | |
| 72 | + } | |
| 73 | + | |
| 74 | + function cleanup() { | |
| 75 | + KTUtil::deleteDirectory($this->sTmpPath); | |
| 76 | + } | |
| 77 | +} | |
| 78 | + | |
| 79 | +?> | ... | ... |