Commit 6c19b2e96ab5787c3a60717dbdfdbbb8ec856eed
1 parent
3df04ed1
Add Bulk Export Plugin, which creates a ZIP file of the folder and all
its contents. git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@4915 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
160 additions
and
0 deletions
plugins/ktstandard/KTBulkExportPlugin.php
0 → 100644
| 1 | +<?php | |
| 2 | + | |
| 3 | +require_once(KT_LIB_DIR . '/plugins/plugin.inc.php'); | |
| 4 | +require_once(KT_LIB_DIR . '/plugins/pluginregistry.inc.php'); | |
| 5 | + | |
| 6 | +class KTBulkExportPlugin extends KTPlugin { | |
| 7 | + var $sNamespace = "ktstandard.bulkexport.plugin"; | |
| 8 | + | |
| 9 | + function setup() { | |
| 10 | + $this->registerAction('folderaction', 'KTBulkExportAction', 'ktstandard.bulkexport.action'); | |
| 11 | + } | |
| 12 | +} | |
| 13 | + | |
| 14 | +require_once(KT_LIB_DIR . '/actions/folderaction.inc.php'); | |
| 15 | + | |
| 16 | +class KTBulkExportAction extends KTFolderAction { | |
| 17 | + var $sDisplayName = 'Bulk Export'; | |
| 18 | + var $sName = 'ktstandard.bulkexport.action'; | |
| 19 | + var $sPermissionName = "ktcore.permissions.read"; | |
| 20 | + | |
| 21 | + function do_main() { | |
| 22 | + $this->oPage->template = "kt3/minimal_page"; | |
| 23 | + $this->handleOutput(""); | |
| 24 | + | |
| 25 | + $oStorage =& KTStorageManagerUtil::getSingleton(); | |
| 26 | + $aQuery = $this->buildQuery(); | |
| 27 | + $this->oValidator->notError($aQuery); | |
| 28 | + $aDocumentIds = DBUtil::getResultArrayKey($aQuery, 'id'); | |
| 29 | + $sTmpPath = tempnam('/tmp', 'kt_export'); | |
| 30 | + unlink($sTmpPath); | |
| 31 | + mkdir($sTmpPath, 0700); | |
| 32 | + $this->sTmpPath = $sTmpPath; | |
| 33 | + $aPaths = array(); | |
| 34 | + foreach ($aDocumentIds as $iId) { | |
| 35 | + $oDocument = Document::get($iId); | |
| 36 | + $sParentFolder = sprintf('%s/%s', $sTmpPath, $oDocument->getFullPath()); | |
| 37 | + $newDir = $this->sTmpPath; | |
| 38 | + foreach (split('/', $oDocument->getFullPath()) as $dirPart) { | |
| 39 | + $newDir = sprintf("%s/%s", $newDir, $dirPart); | |
| 40 | + if (!file_exists($newDir)) { | |
| 41 | + mkdir($newDir, 0700); | |
| 42 | + } | |
| 43 | + } | |
| 44 | + $sOrigFile = $oStorage->temporaryFile($oDocument); | |
| 45 | + $sFilename = sprintf("%s/%s", $sParentFolder, $oDocument->getFileName()); | |
| 46 | + copy($sOrigFile, $sFilename); | |
| 47 | + $aPaths[] = sprintf("%s/%s", $oDocument->getFullPath(), $oDocument->getFileName()); | |
| 48 | + } | |
| 49 | + $sManifest = sprintf("%s/%s", $this->sTmpPath, "MANIFEST"); | |
| 50 | + file_put_contents($sManifest, join("\n", $aPaths)); | |
| 51 | + $sZipFile = sprintf("%s/%s.zip", $this->sTmpPath, $this->oFolder->getName()); | |
| 52 | + $_SESSION['bulkexport'] = KTUtil::arrayGet($_SESSION, 'bulkexport', array()); | |
| 53 | + $sExportCode = KTUtil::randomString(); | |
| 54 | + $_SESSION['bulkexport'][$sExportCode] = array( | |
| 55 | + 'file' => $sZipFile, | |
| 56 | + 'dir' => $this->sTmpPath, | |
| 57 | + ); | |
| 58 | + $sZipCommand = KTUtil::findCommand("import/zip", "zip"); | |
| 59 | + $aCmd = array( | |
| 60 | + $sZipCommand, | |
| 61 | + "-r", | |
| 62 | + $sZipFile, | |
| 63 | + ".", | |
| 64 | + "-i@MANIFEST", | |
| 65 | + ); | |
| 66 | + $sOldPath = getcwd(); | |
| 67 | + chdir($this->sTmpPath); | |
| 68 | + $aOptions = array('popen' => 'r'); | |
| 69 | + $fh = KTUtil::pexec($aCmd, $aOptions); | |
| 70 | + $last_beat = time(); | |
| 71 | + while(!feof($fh)) { | |
| 72 | + if ($i % 1000 == 0) { | |
| 73 | + $this_beat = time(); | |
| 74 | + if ($last_beat + 1 < $this_beat) { | |
| 75 | + $last_beat = $this_beat; | |
| 76 | + print " "; | |
| 77 | + } | |
| 78 | + } | |
| 79 | + $contents = fread($fh, 4096); | |
| 80 | + if ($contents) { | |
| 81 | + print nl2br($contents); | |
| 82 | + } | |
| 83 | + $i++; | |
| 84 | + } | |
| 85 | + pclose($fh); | |
| 86 | + | |
| 87 | + $url = KTUtil::addQueryStringSelf(sprintf('action=downloadZipFile&fFolderId=%d&exportcode=%s', $this->oFolder->getId(), $sExportCode)); | |
| 88 | + printf('Go <a href="%s">here</a> to download the zip file if you are not automatically redirected there', $url); | |
| 89 | + printf('<script language="JavaScript">document.location.href = "%s";</script>', $url); | |
| 90 | + printf("</div></div>\n"); | |
| 91 | + exit(0); | |
| 92 | + } | |
| 93 | + | |
| 94 | + function buildQuery() { | |
| 95 | + $oUser = User::get($_SESSION['userID']); | |
| 96 | + $res = KTSearchUtil::permissionToSQL($oUser, $this->sPermissionName); | |
| 97 | + if (PEAR::isError($res)) { | |
| 98 | + return $res; | |
| 99 | + } | |
| 100 | + list($sPermissionString, $aPermissionParams, $sPermissionJoin) = $res; | |
| 101 | + $aPotentialWhere = array($sPermissionString, 'D.parent_folder_ids = ? OR D.parent_folder_ids LIKE ?', 'D.status_id = 1'); | |
| 102 | + $aWhere = array(); | |
| 103 | + foreach ($aPotentialWhere as $sWhere) { | |
| 104 | + if (empty($sWhere)) { | |
| 105 | + continue; | |
| 106 | + } | |
| 107 | + if ($sWhere == "()") { | |
| 108 | + continue; | |
| 109 | + } | |
| 110 | + $aWhere[] = $sWhere; | |
| 111 | + } | |
| 112 | + $sWhere = ""; | |
| 113 | + if ($aWhere) { | |
| 114 | + $sWhere = "\tWHERE " . join(" AND ", $aWhere); | |
| 115 | + } | |
| 116 | + | |
| 117 | + $sSelect = KTUtil::arrayGet($aOptions, 'select', 'D.id'); | |
| 118 | + | |
| 119 | + $sQuery = sprintf("SELECT %s FROM %s AS D | |
| 120 | + LEFT JOIN %s AS DM ON D.metadata_version_id = DM.id | |
| 121 | + LEFT JOIN %s AS DC ON DM.content_version_id = DC.id | |
| 122 | + %s %s", | |
| 123 | + $sSelect, KTUtil::getTableName("documents"), | |
| 124 | + KTUtil::getTableName("document_metadata_version"), | |
| 125 | + KTUtil::getTableName("document_content_version"), | |
| 126 | + $sPermissionJoin, $sWhere); | |
| 127 | + $aParams = array(); | |
| 128 | + $aParams = array_merge($aParams, $aPermissionParams); | |
| 129 | + $aParentFolderIds = split(',', $this->oFolder->getParentFolderIds()); | |
| 130 | + $aParentFolderIds[] = $this->oFolder->getId(); | |
| 131 | + if ($aParentFolderIds[0] == 0) { | |
| 132 | + array_shift($aParentFolderIds); | |
| 133 | + } | |
| 134 | + $sParentFolderIds = join(',', $aParentFolderIds); | |
| 135 | + $aParams[] = $sParentFolderIds; | |
| 136 | + $aParams[] = $sParentFolderIds . ",%"; | |
| 137 | + return array($sQuery, $aParams); | |
| 138 | + } | |
| 139 | + | |
| 140 | + function do_downloadZipFile() { | |
| 141 | + $sCode = $this->oValidator->validateString($_REQUEST['exportcode']); | |
| 142 | + $aData = KTUtil::arrayGet($_SESSION['bulkexport'], $sCode); | |
| 143 | + $this->oValidator->notEmpty($aData); | |
| 144 | + $sZipFile = $aData['file']; | |
| 145 | + header("Content-Type: application/zip"); | |
| 146 | + header("Content-Length: ". filesize($sZipFile)); | |
| 147 | + header("Content-Disposition: attachment; filename=\"" . $this->oFolder->getName() . ".zip" . "\""); | |
| 148 | + header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); | |
| 149 | + header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); | |
| 150 | + header("Cache-Control: must-revalidate"); | |
| 151 | + readfile($sZipFile); | |
| 152 | + $sTmpDir = $aData['dir']; | |
| 153 | + KTUtil::deleteDirectory($sTmpDir); | |
| 154 | + exit(0); | |
| 155 | + } | |
| 156 | +} | |
| 157 | +$oPluginRegistry =& KTPluginRegistry::getSingleton(); | |
| 158 | +$oPluginRegistry->registerPlugin('KTBulkExportPlugin', 'ktstandard.bulkexport.plugin', __FILE__); | |
| 159 | + | |
| 160 | +?> | ... | ... |