This repository has been archived by the owner on Feb 18, 2021. It is now read-only.
forked from ddooley/ojs-markup
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MarkupPluginUtilities.inc.php
356 lines (304 loc) · 8.82 KB
/
MarkupPluginUtilities.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
/**
* @file plugins/generic/markup/MarkupPluginUtilities.inc.php
*
* Copyright (c) 2003-2013 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class MarkupPluginUtilities
* @ingroup plugins_generic_markup
*
* @brief helper functions
*
*/
// Plugin gateway path folder.
define('MARKUP_GATEWAY_FOLDER', 'markupplugin');
// Title of suplementary files on markup server
define('MARKUP_SUPPLEMENTARY_FILE_TITLE', 'Document Markup Files');
class MarkupPluginUtilities {
/**
* Show a notification to the user
*
* @param $message string Translated text to display
* @param $typeFlag bool Success/Error message
* @param $userId int UserId of user to notify
*/
function showNotification($message, $typeFlag = true, $userId = null) {
import('classes.notification.NotificationManager');
$notificationManager = new NotificationManager();
$notificationType = NOTIFICATION_TYPE_SUCCESS;
if ($typeFlag == false) {
$notificationType = NOTIFICATION_TYPE_ERROR;
}
// If user not specified explicitly, then include current user.
if (!isset($userId)) {
$user =& Request::getUser();
$userId = $user->getId();
}
if (isset($userId)) {
$notificationManager->createTrivialNotification(
$userId,
$notificationType,
array('contents' => $message)
);
}
}
/**
* Return article's supplementary files directory.
*
* @param $articleId int ArticleId
*
* @return string Supplementary file folder path
*/
function getSuppFolder($articleId) {
import('classes.file.ArticleFileManager');
$articleFileManager = new ArticleFileManager((int) $articleId);
return $articleFileManager->filesDir . $articleFileManager->fileStageToPath(ARTICLE_FILE_SUPP);
}
/**
* Return requested markup file to user's browser.
*
* @param $folder string Server file path
* @param $fileName string Name of file to download
*
* @return void
*/
function downloadFile($folder, $fileName) {
$filePath = $folder . $fileName;
$fileManager = new FileManager();
if (!$fileManager->fileExists($filePath)) {
return self::showNotification(
__(
'plugins.generic.markup.archive.no_file',
array('file' => $fileName)
)
);
}
$mimeType = self::getMimeType($filePath);
$fileManager->downloadFile($filePath, $mimeType, true);
exit;
}
/**
* Clean markup plugin media files related to an article if no XML or HTML
* galley links are left.
*
* @param $articleId int ArticleID
* @param $type string What document type to discard if not provided all galley media will be discarded
*
* @return void
*/
function cleanGalleyMedia($articleId, $type = '') {
$galleyDao =& DAORegistry::getDAO('ArticleGalleyDAO');
$galleys =& $galleyDao->getGalleysByArticle($articleId);
$keep = array();
if (!empty($type)) {
foreach ($galleys as $galley) {
$label = $galley->getLabel();
if ($label == 'XML' && $type != 'XML') $keep['xml'] = true;
if ($label == 'HTML' && $type != 'HTML') $keep['html'] = true;
if ($label == 'PDF' && $type != 'PDF') $keep['pdf'] = true;
};
}
$suppFolder = self::getSuppFolder($articleId) . '/markup/';
$deletes = array();
if ($keep) {
if (!isset($keep['xml'])) {
$deletes[] = $suppFolder . 'document.xml';
}
if (!isset($keep['html'])) {
$deletes[] = $suppFolder . 'html';
}
if (!isset($keep['pdf'])) {
$deletes[] = $suppFolder . 'document.pdf';
}
} else {
$deletes[] = $suppFolder;
}
foreach ($deletes as $delete) {
if (!file_exists($delete)) continue;
if (is_file($delete)) {
unlink($delete);
} else {
$rdi = new RecursiveDirectoryIterator($delete, FilesystemIterator::SKIP_DOTS);
$rii = new RecursiveIteratorIterator($rdi, RecursiveIteratorIterator::CHILD_FIRST);
foreach($rii as $path) {
$path->isFile() ? unlink($path->getPathname()) : rmdir($path->getPathname());
}
rmdir($delete);
}
}
}
/**
* Return mime type of a file
*
* @param $file string File to get mimetype for
*
* @return string Mime type of the file
*/
function getMimeType($file) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
return finfo_file($finfo, $file);
}
/**
*
* Build the URL to query the markup server
*
* @param $plugin mixed Plugin to process the Request for
* @param $action string API action
* @param $params array Query parameters
*
* @return string Markup server query URL
*/
function apiUrl($plugin, $action, $params = array()) {
$journal = Request::getJournal();
$journalId = $journal->getId();
$apiUrl = $plugin->getSetting($journalId, 'markupHostURL');
$apiUrl = rtrim($apiUrl, '/');
$apiUrl = $apiUrl . '/api/job/' . $action;
if ($params) {
$apiUrl .= '?' . http_build_query($params);
}
return $apiUrl;
}
/**
* Call the markup server API
*
* @param $plugin mixed Plugin to process the Request for
* @param $action string API action
* @param $params array Query/POST parameters
* @param $method Whether to use a GET/POST request
* @param $method Whether to execute the curl request or to just return the cannel and url
*
* @return mixed API response
*/
function apiRequest($plugin, $action, $params = array(), $isPost = false, $execute = true) {
$journal = Request::getJournal();
$journalId = $journal->getId();
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$params['email'] = $plugin->getSetting($journalId, 'markupHostUser');
$params['password'] = $plugin->getSetting($journalId, 'markupHostPass');
if ($isPost) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$params = array();
}
$apiUrl = self::apiUrl($plugin, $action, $params);
if (!$execute) {
return array('channel' => $ch, 'apiUrl' => $apiUrl);
}
curl_setopt($ch, CURLOPT_URL, $apiUrl);
$response = curl_exec($ch);
$response = json_decode($response, true);
if (!$response) {
$error = curl_error($ch);
if (empty($error)) {
$error = 'HTTP status: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE);
}
$response = array(
'status' => 'error',
'error' => $error,
);
}
curl_close($ch);
return $response;
}
/**
* Submit a file to the markup server for conversion
*
* @param $plugin mixed Plugin to submit the file for
* @param $fileName string File name
* @param $filePath string File path
*
* @return mixed API response
*/
function submitFile($plugin, $fileName, $filePath) {
$journal = Request::getJournal();
$journalId = $journal->getId();
$params = array(
'fileName' => $fileName,
'fileContent' => file_get_contents($filePath),
'citationStyleHash' => $plugin->getSetting($journalId, 'cslStyle'),
);
return self::apiRequest($plugin, 'submit', $params, true);
}
/**
* Get the converted file URL
*
* @param $plugin mixed Plugin to retrieve the file for
* @param $jobId Job Id
* @param $conversionStage What conversion stage to retrieve
*
* @return mixed API response
*/
function getFileUrl($plugin, $jobId, $conversionStage) {
$params = array(
'id' => $jobId,
'conversionStage' => $conversionStage,
);
$data = self::apiRequest($plugin, 'retrieve', $params, false, false);
return $data['apiUrl'];
}
/**
* Get the converted ZIP file URL
*
* @param $plugin mixed Plugin to retrieve the file for
* @param $jobId Job Id
*
* @return mixed API response
*/
function getZipFileUrl($plugin, $jobId) {
return self::getFileUrl($plugin, $jobId, 10);
}
/**
* Retrieve a job status from markup server
*
* @param $plugin mixed Plugin to retrieve the job status for
* @param $jobId Job Id
*
* @return mixed API response
*/
function getJobStatus($plugin, $jobId) {
$params = array(
'id' => $jobId,
);
return self::apiRequest($plugin, 'status', $params);
}
/**
* Extract zip a archive
*
* @param $zipFile string File to extract
* @param $validFiles mixed Array with file names to extract
* @param $destination string Destination folder
* @param $message string Reference to status message from ZipArchive
*
* @return bool Whether or not the extraction was successful
*/
function zipArchiveExtract($zipFile, $destination, &$message, $validFiles = array()) {
$zip = new ZipArchive;
if (!$zip->open($zipFile, ZIPARCHIVE::CHECKCONS)) {
$message = $zip->getStatusString();
return false;
}
if (!empty($validFiles)) {
// Restrict which files to extract
$extractFiles = array();
foreach ($validFiles as $validFile) {
if ($zip->locateName($validFile) !== false) {
$extractFiles[] = $validFile;
}
}
$status = $zip->extractTo($destination, $extractFiles);
} else {
// Extract the entire archive
$status = $zip->extractTo($destination);
}
if ($status === false && $zip->getStatusString() != 'No error') {
$zip->close();
$message = $zip->getStatusString();
return false;
}
$zip->close();
return true;
}
}