-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypesetHandler.inc.php
294 lines (238 loc) · 9.64 KB
/
TypesetHandler.inc.php
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
import('classes.handler.Handler');
//TODO Doc
class TypesetHandler extends Handler {
/**
* TypesetHandler constructor.
*/
function __construct() {
parent::__construct();
$this->_plugin = PluginRegistry::getPlugin('generic', TYPESET_PLUGIN_NAME);
$this->addRoleAssignment(
array(ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR, ROLE_ID_ASSISTANT, ROLE_ID_AUTHOR),
array('convert','createGalley','createGalleyForm')
);
}
/**
* @param Request $request
* @param array $args
* @param array $roleAssignments
* @return bool True on successful authorization false otherwise
*/
function authorize($request, &$args, $roleAssignments) {
import('lib.pkp.classes.security.authorization.SubmissionFileAccessPolicy');
$this->addPolicy(new SubmissionFileAccessPolicy($request, $args, $roleAssignments, SUBMISSION_FILE_ACCESS_READ));
return parent::authorize($request, $args, $roleAssignments);
}
/**
* Create galley form
* @param $args array
* @param $request PKPRequest
* @return JSONMessage JSON object
*/
public function createGalleyForm($args, $request) {
import('plugins.generic.typeset.controllers.grid.form.TypesetArticleGalleyForm');
$galleyForm = new TypesetArticleGalleyForm(
$request,
$this->getPlugin(),
$this->getSubmission()
);
$galleyForm->initData();
return new JSONMessage(true, $galleyForm->fetch($request));
}
/**
* @param $args
* @param $request PKPRequest
* @return JSONMessage
*/
public function createGalley($args, $request) {
import('plugins.generic.typeset.controllers.grid.form.TypesetArticleGalleyForm');
$galleyForm = new TypesetArticleGalleyForm($request, $this->getPlugin(), $this->getSubmission());
$galleyForm->readInputData();
if ($galleyForm->validate()) {
$galleyForm->execute();
return $request->redirectUrlJson($request->getDispatcher()->url($request, ROUTE_PAGE, null, 'workflow', 'access', null,
array(
'submissionId' => $request->getUserVar('submissionId'),
'stageId' => $request->getUserVar('stageId')
)
));
}
return new JSONMessage(false);
}
/**
* Delete folder and its contents
* @note Adapted from https://www.php.net/manual/de/function.rmdir.php#117354
*/
private function rrmdir($src) {
$dir = opendir($src);
while (false !== ($file = readdir($dir))) {
if (($file != '.') && ($file != '..')) {
$full = $src . '/' . $file;
if (is_dir($full)) {
$this->rrmdir($full);
} else {
unlink($full);
}
}
}
closedir($dir);
rmdir($src);
}
/**
* @param Request $request
* @param array $args
* @return JSONMessage
*/
public function convert($args, $request) {
$user = $request->getUser();
$submissionFile = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION_FILE);
$filePath = $submissionFile->getFilePath();
$userVars = $request->getUserVars();
$notificationMgr = new NotificationManager();
list($typesetterOutputPath, $convertedFile, $typesetterCommand, $outputXMLType) = $this->meTypeset($filePath, $userVars['fileExtension']);
$output = '';
$returnCode = -1;
//run typesetter
exec(escapeshellcmd($typesetterCommand), $output, $returnCode);
if ($returnCode == 0) {
// create output file
$submissionDao = Application::getSubmissionDAO();
$submissionId = $submissionFile->getSubmissionId();
$submission = $submissionDao->getById($submissionId);
$tmpfname = tempnam(sys_get_temp_dir(), basename($this->getPluginPath()));
$fileContent = file_get_contents($convertedFile);
import('plugins.generic.typeset.classes.JATSDocument');
$JATSDocument = new JATSDocument($fileContent);
$JATSDocument->setMeta($submission);
$JATSDocument->setImages($submission);
file_put_contents($tmpfname, $JATSDocument->saveXML());
$genreId = $submissionFile->getGenreId();
$fileSize = filesize($tmpfname);
$originalFileInfo = pathinfo($submissionFile->getOriginalFileName());
$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'] .'-'.$outputXMLType. ".xml");
$newSubmissionFile->setUploaderUserId($user->getId());
$newSubmissionFile->setFileSize($fileSize);
$newSubmissionFile->setFileType("text/xml");
$newSubmissionFile->setSourceFileId($submissionFile->getFileId());
$newSubmissionFile->setSourceRevision($submissionFile->getRevision());
$newSubmissionFile->setRevision(1);
$newSubmissionFile = $submissionFileDao->insertObject($newSubmissionFile, $tmpfname);
unlink($tmpfname);
// create dependent files
$mediaFilesPath = glob($typesetterOutputPath . DIRECTORY_SEPARATOR . 'media/*');
if (!empty($mediaFilesPath)) {
foreach ($mediaFilesPath as $filePath) {
$this->_attachSupplementaryFile($request, $submission, $submissionFileDao, $newSubmissionFile, $filePath);
}
}
$this->rrmdir($typesetterOutputPath);
$successMsg = __('plugins.generic.typeset.tool.ConversionSuccess');
$notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => $successMsg));
} else {
$errorMsg = __('plugins.generic.typeset.tool.ConversionError ');
$notificationMgr->createTrivialNotification($request->getUser()->getId(), NOTIFICATION_TYPE_ERROR, array('contents' => $errorMsg));
}
return DAO::getDataChangedEvent();
}
/**
* Override the builtin to get the correct plugin path.
* @return string
*/
public function getPluginPath() {
return $this->_plugin->getPluginPath();
}
/**
* typeset using meTypeset
* @param $filePath source file path
* @param $fileType source file type
* @return array
*/
private function meTypeset($filePath, $fileType) {
$request = Application::getRequest();
$context = $request->getContext();
$toolPath = $this->_plugin->getToolPath();
$virtualPath = $this->_plugin->getPythonVM();
$aggression = $this->_plugin->_getPluginSetting($context, 'typesetToolAggression') ?: 5;
$clean = $this->_plugin->_getPluginSetting($context, 'typesetToolClean') ? ' --clean ' : '';
$noImage = $this->_plugin->_getPluginSetting($context, 'typesetToolImage') ? ' --noimageprocessing ' : '';
$noReference = $this->_plugin->_getPluginSetting($context, 'typesetToolReference') ? ' --nolink ' : '';
$teiOutput = $this->_plugin->_getPluginSetting($context, 'typesetToolOutputTEI') ? ' --prettytei ' : '';
$outputXMLType = $teiOutput ? 'tei' : 'nlm';
if (!file_exists($toolPath)) {
return;
} else {
$typesetterOutputPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid(basename($this->getPluginPath()));
$convertedFile = $typesetterOutputPath . DIRECTORY_SEPARATOR . $outputXMLType . DIRECTORY_SEPARATOR . 'out.xml';
$typesetterCommand = $virtualPath . 'python3 ' . $toolPath . ' --aggression ' . $aggression . ' --nogit ' . $clean . $noImage . $noReference . $teiOutput . $fileType . ' ' . $filePath . ' ' . $typesetterOutputPath;
if($outputXMLType == 'nlm') $outputXMLType = 'jats';
return array($typesetterOutputPath, $convertedFile, $typesetterCommand, $outputXMLType);
}
}
/**
* Adds attached images etc.
* @note Adapted from docxConverterPlugin
*
* @param Request $request
* @param Submission $submission
* @param SubmissionFileDAO $submissionFileDao
* @param SubmissionFile $newSubmissionFile
* @param string $originalName
* @param string $fileData
*/
private function _attachSupplementaryFile(Request $request, Submission $submission, SubmissionFileDAO $submissionFileDao, SubmissionFile $newSubmissionFile, $filePath) {
$tmpfnameSuppl = tempnam(sys_get_temp_dir(), $this->getPluginPath());
file_put_contents($tmpfnameSuppl, file_get_contents($filePath));
$mimeType = mime_content_type($filePath);
$genreDao = DAORegistry::getDAO('GenreDAO');
$genres = $genreDao->getByDependenceAndContextId(true, $request->getContext()->getId());
$supplGenreId = null;
while ($genre = $genres->next()) {
if (($mimeType == "image/png" || $mimeType == "image/jpeg") && $genre->getKey() == "IMAGE") {
$supplGenreId = $genre->getId();
}
}
if (!$supplGenreId) {
unlink($tmpfnameSuppl);
return;
}
// 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($filePath));
$submissionFileDao->insertObject($supplementaryFile, $tmpfnameSuppl);
unlink($tmpfnameSuppl);
}
/**
* Get the plugin.
* @return TypesetPlugin
*/
function getPlugin() {
return $this->_plugin;
}
/**
* Get the submission associated with this grid.
* @return Submission
*/
function getSubmission() {
return $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION);
}
}