Skip to content

Commit

Permalink
Adapt files upload to 3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy-1 committed May 22, 2021
1 parent 89d1e20 commit 6cdc4fd
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 94 deletions.
115 changes: 68 additions & 47 deletions DOCXConverterHandler.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@

import('classes.handler.Handler');
import('plugins.generic.docxConverter.classes.DOCXConverterDocument');
import('lib.pkp.classes.file.PrivateFileManager');
require_once __DIR__ . "/docxToJats/vendor/autoload.php";
use docx2jats\DOCXArchive;

class ConverterHandler extends Handler {

/** @copydoc PKPHandler::_isBackendPage */
var $_isBackendPage = true;

/**
* Constructor
*/
Expand All @@ -32,65 +37,78 @@ function __construct() {
* @copydoc PKPHandler::authorize()
*/
function authorize($request, &$args, $roleAssignments) {
import('lib.pkp.classes.security.authorization.SubmissionFileAccessPolicy');
$this->addPolicy(new SubmissionFileAccessPolicy($request, $args, $roleAssignments, SUBMISSION_FILE_ACCESS_MODIFY));
import('lib.pkp.classes.security.authorization.WorkflowStageAccessPolicy');
$this->addPolicy(new WorkflowStageAccessPolicy($request, $args, $roleAssignments, 'submissionId', (int)$request->getUserVar('stageId')));
return parent::authorize($request, $args, $roleAssignments);
}

public function parse($args, $request) {
$fileId = (int) $request->getUserVar('fileId');
$submissionFiles = Services::get('submissionFile')->getMany([
'fileIds' => [$fileId],
]);
$submissionFile = $submissionFiles->current();

$user = $request->getUser();
$submissionFile = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION_FILE);
$filePath = $submissionFile->getFilePath();
$fileManager = new PrivateFileManager();
$filePath = $fileManager->getBasePath() . '/' . $submissionFile->getData('path');

$docxArchive = new DOCXArchive($filePath);
$jatsXML = new DOCXConverterDocument($docxArchive);

$submissionDao = Application::getSubmissionDAO();
$submissionId = $submissionFile->getSubmissionId();
$submission = $submissionDao->getById($submissionId);
$submissionId = $submissionFile->getData('submissionId');
$submission = Services::get('submission')->get($submissionId);
$jatsXML->setDocumentMeta($request, $submission);
$tmpfname = tempnam(sys_get_temp_dir(), 'docxConverter');
file_put_contents($tmpfname, $jatsXML->saveXML());
$genreId = $submissionFile->getGenreId();
$fileSize = filesize($tmpfname);
$genreId = $submissionFile->getData('genreId');

$originalFileInfo = pathinfo($submissionFile->getOriginalFileName());
// Add new JATS XML file
$submissionDir = Services::get('submissionFile')->getSubmissionDir($submission->getData('contextId'), $submissionId);
$newFileId = Services::get('file')->add(
$tmpfname,
$submissionDir . DIRECTORY_SEPARATOR . uniqid() . '.xml'
);

/* @var $newSubmissionFile SubmissionFile */
$submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO');
$newSubmissionFile = $submissionFileDao->newDataObjectByGenreId($genreId);
$newSubmissionFile->setSubmissionId($submission->getId());
$newSubmissionFile->setSubmissionLocale($submission->getLocale());
$newSubmissionFile->setGenreId($genreId);
$newSubmissionFile->setFileStage($submissionFile->getFileStage());
$newSubmissionFile->setDateUploaded(Core::getCurrentDate());
$newSubmissionFile->setDateModified(Core::getCurrentDate());
$newSubmissionFile->setOriginalFileName($originalFileInfo['filename'] . ".xml");
$newSubmissionFile->setUploaderUserId($user->getId());
$newSubmissionFile->setFileSize($fileSize);
$newSubmissionFile->setFileType("text/xml");
$newSubmissionFile->setSourceFileId($submissionFile->getFileId());
$newSubmissionFile->setSourceRevision($submissionFile->getRevision());
$newSubmissionFile->setRevision(1);
$insertedSubmissionFile = $submissionFileDao->insertObject($newSubmissionFile, $tmpfname);
$newSubmissionFile = $submissionFileDao->newDataObject();
$newName = [];
foreach ($submissionFile->getData('name') as $localeKey => $name) {
$newName[$localeKey] = pathinfo($name)['filename'] . '.xml';
}

$newSubmissionFile->setAllData(
[
'fileId' => $newFileId,
'assocType' => $submissionFile->getData('assocType'),
'assocId' => $submissionFile->getData('assocId'),
'fileStage' => $submissionFile->getData('fileStage'),
'mimetype' => 'application/xml',
'locale' => $submissionFile->getData('locale'),
'genreId' => $genreId,
'name' => $newName,
'submissionId' => $submissionId,
]
);

$newSubmissionFile = Services::get('submissionFile')->add($newSubmissionFile, $request);

unlink($tmpfname);

$mediaData = $docxArchive->getMediaFilesContent();
if (!empty($mediaData)) {
foreach ($mediaData as $originalName => $singleData) {
$this->_attachSupplementaryFile($request, $submission, $submissionFileDao, $newSubmissionFile, $originalName, $singleData);
$this->_attachSupplementaryFile($request, $submission, $submissionFileDao, $newSubmissionFile, $fileManager, $originalName, $singleData);
}
}

return new JSONMessage(true, array(
'submissionId' => $insertedSubmissionFile->getSubmissionId(),
'fileId' => $insertedSubmissionFile->getFileIdAndRevision(),
'fileStage' => $insertedSubmissionFile->getFileStage(),
'submissionId' => $submissionId,
'fileId' => $newSubmissionFile->getData('fileId'),
'fileStage' => $newSubmissionFile->getData('fileStage'),
));
}

private function _attachSupplementaryFile(Request $request, Submission $submission, SubmissionFileDAO $submissionFileDao, SubmissionFile $newSubmissionFile, string $originalName, string $singleData) {
private function _attachSupplementaryFile(Request $request, Submission $submission, SubmissionFileDAO $submissionFileDao, SubmissionFile $newSubmissionFile, PrivateFileManager $fileManager, string $originalName, string $singleData) {
$tmpfnameSuppl = tempnam(sys_get_temp_dir(), 'docxConverter');
file_put_contents($tmpfnameSuppl, $singleData);
$mimeType = mime_content_type($tmpfnameSuppl);
Expand All @@ -110,22 +128,25 @@ private function _attachSupplementaryFile(Request $request, Submission $submissi
return;
}

$submissionDir = Services::get('submissionFile')->getSubmissionDir($submission->getData('contextId'), $submission->getId());
$newFileId = Services::get('file')->add(
$tmpfnameSuppl,
$submissionDir . '/' . uniqid() . '.' . $fileManager->parseFileExtension($originalName)
);

// Set file
$supplementaryFile = $submissionFileDao->newDataObjectByGenreId($supplGenreId);
$supplementaryFile->setSubmissionId($submission->getId());
$supplementaryFile->setSubmissionLocale($submission->getLocale());
$supplementaryFile->setGenreId($supplGenreId);
$supplementaryFile->setFileStage(SUBMISSION_FILE_DEPENDENT);
$supplementaryFile->setDateUploaded(Core::getCurrentDate());
$supplementaryFile->setDateModified(Core::getCurrentDate());
$supplementaryFile->setUploaderUserId($request->getUser()->getId());
$supplementaryFile->setFileSize(filesize($tmpfnameSuppl));
$supplementaryFile->setFileType($mimeType);
$supplementaryFile->setAssocId($newSubmissionFile->getFileId());
$supplementaryFile->setAssocType(ASSOC_TYPE_SUBMISSION_FILE);
$supplementaryFile->setOriginalFileName(basename($originalName));

$submissionFileDao->insertObject($supplementaryFile, $tmpfnameSuppl);
$newSupplementaryFile = $submissionFileDao->newDataObject();
$newSupplementaryFile->setAllData([
'fileId' => $newFileId,
'assocId' => $newSubmissionFile->getId(),
'assocType' => ASSOC_TYPE_SUBMISSION_FILE,
'fileStage' => SUBMISSION_FILE_DEPENDENT,
'submissionId' => $submission->getId(),
'genreId' => $supplGenreId,
'name' => array_fill_keys(array_keys($newSubmissionFile->getData('name')), basename($originalName))
]);

Services::get('submissionFile')->add($newSupplementaryFile, $request);
unlink($tmpfnameSuppl);
}

Expand Down
13 changes: 6 additions & 7 deletions DocxToJatsPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ public function templateFetchCallback($hookName, $params) {
$resourceName = $params[1];
if ($resourceName == 'controllers/grid/gridRow.tpl') {
/* @var $row GridRow */
$row = $templateMgr->get_template_vars('row');
$row = $templateMgr->getTemplateVars('row');
$data = $row->getData();
if (is_array($data) && (isset($data['submissionFile']))) {
$submissionFile = $data['submissionFile'];
$fileExtension = strtolower($submissionFile->getExtension());
$fileExtension = strtolower($submissionFile->getData('mimetype'));

// Ensure that the conversion is run on the appropriate workflow stage
$stageId = (int) $request->getUserVar('stageId');
$submissionId = $submissionFile->getSubmissionId();
$submissionId = $submissionFile->getData('submissionId');
$submission = Services::get('submission')->get($submissionId); /** @var $submission Submission */
$submissionStageId = $submission->getData('stageId');
$roles = $request->getUser()->getRoles($request->getContext()->getId());
Expand All @@ -104,8 +104,7 @@ public function templateFetchCallback($hookName, $params) {
break;
}
}

if (strtolower($fileExtension) == 'docx' && // show only for files with docx extension
if (strtolower($fileExtension) == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' && // show only for files with docx extension
$accessAllowed && // only for those that have access according to the DOCXConverterHandler rules
in_array($stageId, $this->getAllowedWorkflowStages()) && // only for stage ids copyediting or higher
in_array($submissionStageId, $this->getAllowedWorkflowStages()) // only if submission has correspondent stage id
Expand All @@ -114,13 +113,13 @@ public function templateFetchCallback($hookName, $params) {
$path = $dispatcher->url($request, ROUTE_PAGE, null, 'docxParser', 'parse', null,
array(
'submissionId' => $submissionId,
'fileId' => $submissionFile->getFileId(),
'fileId' => $submissionFile->getData('fileId'),
'stageId' => $stageId
));
$pathRedirect = $dispatcher->url($request, ROUTE_PAGE, null, 'workflow', 'access',
array(
'submissionId' => $submissionId,
'fileId' => $submissionFile->getFileId(),
'fileId' => $submissionFile->getData('fileId'),
'stageId' => $stageId
));

Expand Down
24 changes: 24 additions & 0 deletions locale/en_US/locale.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2021-05-22T13:48:36+00:00\n"
"PO-Revision-Date: 2021-05-22T13:48:36+00:00\n"
"Language: \n"

msgid "plugins.generic.docxToJats.displayName"
msgstr "DOCX to JATS XML Converter Plugin"

msgid "plugins.generic.docxToJats.description"
msgstr "Is developed to transform scientific articles from DOCX (OOXML) to JATS XML format."

msgid "plugins.generic.docxToJats.button.parseDocx"
msgstr "Convert to JATS XML"

msgid "grid.action.parse"
msgstr "Convert this file to JATS XML"
20 changes: 0 additions & 20 deletions locale/en_US/locale.xml

This file was deleted.

24 changes: 24 additions & 0 deletions locale/uk_UA/locale.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2021-05-22T13:49:16+00:00\n"
"PO-Revision-Date: 2021-05-22T13:49:16+00:00\n"
"Language: \n"

msgid "plugins.generic.docxToJats.displayName"
msgstr "Плагін для конвертації DOCX в JATS XML"

msgid "plugins.generic.docxToJats.description"
msgstr "Розроблений для конвертації наукових статей із формату DOCX (OOXML) в формат JATS XML."

msgid "plugins.generic.docxToJats.button.parseDocx"
msgstr "Конвертувати в JATS XML"

msgid "grid.action.parse"
msgstr "Конвертувати цей файл в JATS XML"
20 changes: 0 additions & 20 deletions locale/uk_UA/locale.xml

This file was deleted.

0 comments on commit 6cdc4fd

Please sign in to comment.