15 import(
'lib.pkp.classes.scheduledTask.ScheduledTask');
17 define(
'FILE_LOADER_RETURN_TO_STAGING', 0x01);
18 define(
'FILE_LOADER_ERROR_MESSAGE_TYPE',
'common.error');
19 define(
'FILE_LOADER_WARNING_MESSAGE_TYPE',
'common.warning');
21 define(
'FILE_LOADER_PATH_STAGING',
'stage');
22 define(
'FILE_LOADER_PATH_PROCESSING',
'processing');
23 define(
'FILE_LOADER_PATH_REJECT',
'reject');
24 define(
'FILE_LOADER_PATH_ARCHIVE',
'archive');
29 private $_claimedFilename;
38 private $_processingPath;
41 private $_archivePath;
53 private $_stagedBackFiles = array();
56 private $_compressArchives =
false;
63 parent::__construct($args);
70 $basePath = rtrim($args[0], DIRECTORY_SEPARATOR);
71 $basePathFolder = basename($basePath);
74 $basePathParent = realpath(dirname($basePath));
75 if ($basePathParent ===
false) {
78 $basePath = $basePathParent . DIRECTORY_SEPARATOR . $basePathFolder;
80 $this->_basePath = $basePath;
83 if (!is_null($basePath)) {
84 $this->_stagePath = $basePath . DIRECTORY_SEPARATOR . FILE_LOADER_PATH_STAGING;
85 $this->_archivePath = $basePath . DIRECTORY_SEPARATOR . FILE_LOADER_PATH_ARCHIVE;
86 $this->_rejectPath = $basePath . DIRECTORY_SEPARATOR . FILE_LOADER_PATH_REJECT;
87 $this->_processingPath = $basePath . DIRECTORY_SEPARATOR . FILE_LOADER_PATH_PROCESSING;
92 $site = $siteDao->getSite();
93 $this->_adminEmail = $site->getLocalizedContactEmail();
94 $this->_adminName = $site->getLocalizedContactName();
106 return $this->_stagePath;
114 return $this->_processingPath;
122 return $this->_rejectPath;
130 return $this->_archivePath;
138 return $this->_compressArchives;
146 $this->_compressArchives = $compressArchives;
159 $foundErrors =
false;
160 while(!is_null($filePath = $this->_claimNextFile())) {
161 if ($filePath ===
false) {
168 }
catch(Exception $e) {
170 $this->_rejectFile();
175 if ($result === FILE_LOADER_RETURN_TO_STAGING) {
180 $this->_stagedBackFiles[] = $this->_claimedFilename;
182 $this->_archiveFile();
187 array(
'filename' => $filePath)), SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
190 return !$foundErrors;
208 if (is_null($this->_basePath) || strpos($this->_basePath, $filesDir) !== 0) {
209 $this->
addExecutionLogEntry(__(
'admin.fileLoader.wrongBasePathLocation', array(
'path' => $this->_basePath)),
210 SCHEDULED_TASK_MESSAGE_TYPE_ERROR);
215 $pathsToCheck = array(
219 $this->_processingPath
222 foreach($pathsToCheck as $path) {
223 if (!(is_dir($path) && is_readable($path))) {
226 if (is_null($fileManager)) {
227 import(
'lib.pkp.classes.file.FileManager');
230 $fileManager->mkdirtree($path);
234 if (!(is_dir($path) && is_readable($path))) {
237 SCHEDULED_TASK_MESSAGE_TYPE_ERROR);
256 abstract protected function processFile($filePath);
265 protected function moveFile($sourceDir, $destDir, $filename) {
266 $currentFilePath = $sourceDir . DIRECTORY_SEPARATOR . $filename;
267 $destinationPath = $destDir . DIRECTORY_SEPARATOR . $filename;
269 if (!rename($currentFilePath, $destinationPath)) {
270 $message = __(
'admin.fileLoader.moveFileFailed', array(
'filename' => $filename,
271 'currentFilePath' => $currentFilePath,
'destinationPath' => $destinationPath));
279 return $destinationPath;
290 private function _claimNextFile() {
291 $stageDir = opendir($this->_stagePath);
292 $processingFilePath =
false;
294 while($filename = readdir($stageDir)) {
295 if ($filename ==
'..' || $filename ==
'.' ||
296 in_array($filename, $this->_stagedBackFiles))
continue;
298 $processingFilePath = $this->
moveFile($this->_stagePath, $this->_processingPath, $filename);
302 if (pathinfo($processingFilePath, PATHINFO_EXTENSION) ==
'gz') {
305 $processingFilePath = $fileMgr->decompressFile($processingFilePath);
306 $filename = pathinfo($processingFilePath, PATHINFO_BASENAME);
307 }
catch (Exception $e) {
308 $this->
moveFile($this->_processingPath, $this->_stagePath, $filename);
314 if ($processingFilePath) {
315 $this->_claimedFilename = $filename;
316 return $processingFilePath;
325 private function _rejectFile() {
326 $this->
moveFile($this->_processingPath, $this->_rejectPath, $this->_claimedFilename);
332 private function _archiveFile() {
333 $this->
moveFile($this->_processingPath, $this->_archivePath, $this->_claimedFilename);
337 $filePath = $this->_archivePath . DIRECTORY_SEPARATOR . $this->_claimedFilename;
338 $fileMgr->compressFile($filePath);
339 }
catch (Exception $e) {
348 private function _stageFile() {
349 $this->
moveFile($this->_processingPath, $this->_stagePath, $this->_claimedFilename);
356 private function _notify($message, $messageType) {
358 import(
'lib.pkp.classes.mail.Mail');
362 $mail->addRecipient($this->_adminEmail, $this->_adminName);
365 $mail->setSubject(__(
'admin.fileLoader.emailSubject', array(
'processId' => $this->
getProcessId())) .
366 ' - ' . __($messageType));
367 $mail->setBody($message);