-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRSCIExportPlugin.php
More file actions
361 lines (321 loc) · 12.5 KB
/
Copy pathRSCIExportPlugin.php
File metadata and controls
361 lines (321 loc) · 12.5 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
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
357
358
359
360
361
<?php
/**
* @file plugins/importexport/rsciexport/RSCIExportPlugin.php
* @class RSCIExportPlugin
* @ingroup plugins_importexport_rsci
*
* @brief RSCI XML export plugin.
*/
namespace APP\plugins\importexport\rsciexport;
use APP\facades\Repo;
use APP\journal\JournalDAO;
use APP\publication\Publication;
use APP\submission\Submission;
use APP\template\TemplateManager;
use PKP\config\Config;
use PKP\context\Context;
use PKP\core\JSONMessage;
use PKP\core\PKPRequest;
use PKP\core\Registry;
use PKP\db\DAORegistry;
use PKP\file\FileManager;
use PKP\galley\Galley;
use APP\notification\Notification;
use PKP\plugins\ImportExportPlugin;
use APP\notification\NotificationManager;
use APP\plugins\importexport\rsciexport\classes\form\RSCIExportSettingsForm;
use PKP\submission\PKPSubmission;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use ZipArchive;
class RSCIExportPlugin extends ImportExportPlugin
{
/**
* @copyDoc Plugin::register()
*/
public function register($category, $path, $mainContextId = null)
{
$success = parent::register($category, $path, $mainContextId);
$this->addLocaleData();
return $success;
}
/**
* Display the plugin.
* @param $args array
* @param $request PKPRequest
*/
function display($args, $request)
{
parent::display($args, $request);
$templateMgr = TemplateManager::getManager($request);
$journal = $request->getJournal();
switch (array_shift($args)) {
case 'index':
case '':
$templateMgr->display($this->getTemplateResource('index.tpl'));
break;
case 'exportIssue':
$issueIdsArr = (array) $request->getUserVar('selectedIssues');
if (count($issueIdsArr) > 1 || count($issueIdsArr) < 1)
{
$user = $request->getUser();
$notificationManager = new NotificationManager();
$notificationManager->createTrivialNotification($user->getId(), Notification::NOTIFICATION_TYPE_ERROR, array('pluginName' => $this->getDisplayName(), 'contents' => "Choose one issue."));
$request->redirectUrl(str_replace("exportIssue", "", $request->getRequestPath()));
break;
}
else {
$issueId = $issueIdsArr[0];
$exportXml = $this->exportIssue(
$issueId,
$request->getContext()
);
$this->_uploadZip($issueId, $exportXml);
break;
}
default:
$dispatcher = $request->getDispatcher();
throw new NotFoundHttpException();
}
}
/**
* @copydoc Plugin::manage()
*/
function manage($args, $request) {
$user = $request->getUser();
$settingsForm = new RSCIExportSettingsForm($this, $request->getContext()->getId());
$notificationManager = new NotificationManager();
switch ($request->getUserVar('verb')) {
case 'save':
$settingsForm->readInputData();
if ($settingsForm->validate()) {
$settingsForm->execute([]);
$notificationManager->createTrivialNotification($user->getId(), Notification::NOTIFICATION_TYPE_SUCCESS);
return new JSONMessage();
} else {
return new JSONMessage(true, $settingsForm->fetch($request));
}
case 'index':
$settingsForm->initData();
return new JSONMessage(true, $settingsForm->fetch($request));
}
return parent::manage($args, $request);
}
/**
* Get the zip with XML for an issue.
* @param $issueId int
* @param $context Context
* @return string XML contents representing the supplied issue IDs.
*/
function exportIssue($issueId, $context)
{
$issue = Repo::issue()->get($issueId);
$filterDao = DAORegistry::getDAO('FilterDAO');
$rsciExportFilters = $filterDao->getObjectsByGroup('issue=>rsci-xml');
assert(count($rsciExportFilters) == 1); // Assert only a single serialization filter
$exportFilter = array_shift($rsciExportFilters);
$exportSettings = array ('isExportArtTypeFromSectionAbbrev' => $this->getSetting($context->getId(), 'exportArtTypeFromSectionAbbrev'),
'isExportSections' => $this->getSetting($context->getId(), 'exportSections'),
'journalRSCITitleId' => $this->getSetting($context->getId(), 'journalRSCITitleId'));
$exportFilter->SetExportSettings($exportSettings);
libxml_use_internal_errors(true);
$issueXml = $exportFilter->execute($issue, true);
$xml = $issueXml->saveXml();
return $xml;
}
/**
* @param $issueId int
* @param $xml string XML file content
*/
protected function _uploadZip($issueId, $xml)
{
$fileManager = new FileManager();
$zipPath = $this->createIssueZip($issueId, $xml);
// UPLOAD:
$fileManager->downloadByPath($zipPath);
$fileManager->rmtree($this->getExportPath());
}
/**
* Create the zip with XML, cover image, and article files for an issue.
* @param $issueId int
* @param $xml string XML file content
* @return string Path to the generated zip file.
*/
protected function createIssueZip($issueId, $xml)
{
$fileManager = new FileManager();
$xmlFileName = $this->getExportPath() . 'Markup_unicode.xml';
$fileManager->writeFile($xmlFileName, $xml);
$issue = Repo::issue()->get($issueId);
$coverUrl = $issue->getLocalizedCoverImageUrl();
$coverUrlParts = explode('/', $coverUrl);
$coverName = end($coverUrlParts);
if ($coverUrl != "")
$fileManager->copyFile($coverUrl, $this->getExportPath() . $coverName);
$submissionsIterator = Repo::submission()->getCollector()
->filterByContextIds([$issue->getData('journalId')])
->filterByIssueIds([$issue->getId()])
->filterByStatus([PKPSubmission::STATUS_PUBLISHED, PKPSubmission::STATUS_SCHEDULED])
->GetMany();
/** @var Submission[] $publiations */
$submissions = iterator_to_array($submissionsIterator);
// $publishedArticleDao = DAORegistry::getDAO('PublishedArticleDAO');
// $articles = $publishedArticleDao->getPublishedArticles($issue->getId());
foreach ($submissions as $submission)
{
/** @var Publication $publication */
$publication = $submission->getCurrentPublication();
/** @var Galley $galley */
$galleys = Repo::galley()->dao->getByPublicationId($publication->getId());
$galley = array_shift($galleys);
if (isset($galley))
{
$fileName = array_shift($galley->getFile()->getData('name'));
$articleFilePath = $galley->getFile()->getData('path');
if ($articleFilePath != "")
copy(Config::getVar('files', 'files_dir') . '/' . $articleFilePath, $this->getExportPath() . $fileName);
}
}
// ZIP:
$zip = new ZipArchive();
$zipPath = $this->getExportPath().'issue-'.$issue->getNumber().'-'.$issue->getYear() . '.zip';
if ($zip->open($zipPath, ZipArchive::CREATE)!==TRUE) {
exit('Невозможно создать архив ZIP (' . $zipPath . '\n');
}
$filesToArchive = scandir($this->getExportPath());
foreach($filesToArchive as $file) {
if (is_file($this->getExportPath(). $file) && $this->getExportPath() . $file != $zipPath) {
$zip->addFile($this->getExportPath() . $file, basename($file));
}
}
$zip->close();
return $zipPath;
}
/**
* @var string
*/
protected $_generatedTempPath = '';
/**
* @var int
*/
protected $_exportContextId = 0;
/**
* @copydoc ImportExportPlugin::getExportPath()
*/
function getExportPath()
{
if ($this->_generatedTempPath === '')
{
$exportPath = parent::getExportPath();
$request = Registry::get('request', false);
$journal = $request ? $request->getJournal() : null;
$contextId = $journal ? $journal->getId() : $this->_exportContextId;
$this->_generatedTempPath = $exportPath . $this->getPluginSettingsPrefix() . 'Temp-' . date('Ymd-His'). $contextId . '/';
}
return $this->_generatedTempPath;
}
/**
* @copydoc ImportExportPlugin::getPluginSettingsPrefix()
*/
function getPluginSettingsPrefix() {
return 'rsciexport';
}
/**
* Execute import/export tasks using the command-line interface.
* @param $scriptName The name of the command-line script (displayed as usage info)
* @param $args Parameters to the plugin
*/
function executeCLI($scriptName, &$args)
{
$command = array_shift($args);
if ($command == 'export') {
$zipFile = array_shift($args);
} else {
$zipFile = $command;
}
$journalPath = array_shift($args);
$issueArg = array_shift($args);
$issueId = $issueArg == 'issue' ? array_shift($args) : $issueArg;
$journalDao = DAORegistry::getDAO('JournalDAO'); /** @var JournalDAO $journalDao */
$journal = $journalDao->getByPath($journalPath);
if (!$journal) {
if ($journalPath != '') {
echo __('plugins.importexport.common.cliError') . "\n";
echo __('plugins.importexport.common.error.unknownContext', ['contextPath' => $journalPath]) . "\n\n";
}
$this->usage($scriptName);
return;
}
if (!$zipFile || !$issueId) {
$this->usage($scriptName);
return;
}
if ($this->isRelativePath($zipFile) && !preg_match('/^[A-Za-z]:[\/\\\\]/', $zipFile) && substr($zipFile, 0, 2) !== '\\\\') {
$zipFile = PWD . '/' . $zipFile;
}
$outputDir = dirname($zipFile);
if (!is_writable($outputDir) || (file_exists($zipFile) && !is_writable($zipFile))) {
echo __('plugins.importexport.common.cliError') . "\n";
echo __('plugins.importexport.common.export.error.outputFileNotWritable', ['param' => $zipFile]) . "\n\n";
$this->usage($scriptName);
return;
}
$issue = Repo::issue()->getByBestId($issueId, $journal->getId());
if (!$issue) {
echo __('plugins.importexport.common.cliError') . "\n";
echo __('plugins.importexport.rsciexport.export.error.issueNotFound', ['issueId' => $issueId]) . "\n\n";
return;
}
$this->_exportContextId = $journal->getId();
$exportXml = $this->exportIssue($issue->getId(), $journal);
$zipPath = $this->createIssueZip($issue->getId(), $exportXml);
$fileManager = new FileManager();
if (!$fileManager->copyFile($zipPath, $zipFile)) {
echo __('plugins.importexport.common.cliError') . "\n";
echo __('plugins.importexport.common.export.error.outputFileNotWritable', ['param' => $zipFile]) . "\n\n";
$fileManager->rmtree($this->getExportPath());
return;
}
$fileManager->rmtree($this->getExportPath());
return true;
}
/**
* Display the command-line usage information
* @param $scriptName string
*/
function usage($scriptName)
{
echo __('plugins.importexport.rsciexport.cliUsage', [
'scriptName' => $scriptName,
'pluginName' => $this->getName()
]) . "\n";
}
/**
* Get the name of this plugin. The name must be unique within
* its category, and should be suitable for part of a filename
* (ie short, no spaces, and no dependencies on cases being unique).
*
* @return string name of plugin
*/
function getName(): string
{
return "RSCIExportPlugin";
}
/**
* Get the display name for this plugin.
*
* @return string
*/
function getDisplayName(): string
{
return __('plugins.importexport.rsciexport.displayName');
}
/**
* Get a description of this plugin.
*
* @return string
*/
function getDescription(): string
{
return __('plugins.importexport.rsciexport.description');
}
}