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