-
Notifications
You must be signed in to change notification settings - Fork 1
/
RSCIExportPlugin.inc.php
264 lines (239 loc) · 9.25 KB
/
RSCIExportPlugin.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
<?php
/**
* @file plugins/importexport/rsciexport/RSCIExportPlugin.inc.php
* @class RSCIExportPlugin
* @ingroup plugins_importexport_rsci
*
* @brief RSCI XML export plugin.
*/
import('lib.pkp.classes.plugins.ImportExportPlugin');
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_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();
$dispatcher->handle404();
}
}
/**
* @copydoc Plugin::manage()
*/
function manage($args, $request) {
$user = $request->getUser();
$this->import('classes.form.RSCIExportSettingsForm');
$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_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
* @return string XML contents representing the supplied issue IDs.
*/
function exportIssue($issueId)
{
$issueDao = DAORegistry::getDAO('IssueDAO');
$issue = $issueDao->getById($issueId);
//$publishedArticleDao = DAORegistry::getDAO('PublishedArticleDAO');
//$submissionIds = array();
//$publishedArticles = $publishedArticleDao->getPublishedArticles($issueId);
//foreach ($publishedArticles as $publishedArticle) {
// $submissionIds[] = $publishedArticle->getId();
//}
//$submissionDao = Application::getSubmissionDAO();
$xml = '';
$filterDao = DAORegistry::getDAO('FilterDAO');
$rsciExportFilters = $filterDao->getObjectsByGroup('issue=>rsci-xml');
assert(count($rsciExportFilters) == 1); // Assert only a single serialization filter
$exportFilter = array_shift($rsciExportFilters);
$context = Application::getRequest()->getContext();
$exportSettings = array ('isExportArtTypeFromSectionAbbrev' => $this->getSetting($context->getId(), 'exportArtTypeFromSectionAbbrev'),
'isExportSections' => $this->getSetting($context->getId(), 'exportSections'),
'journalRSCITitleId' => $this->getSetting($context->getId(), 'journalRSCITitleId'));
$exportFilter->SetExportSettings($exportSettings);
//$submissions = array();
//foreach ($submissionIds as $submissionId) {
// $submission = $submissionDao->getById($submissionId, $context->getId());
// if ($submission) $submissions[] = $submission;
//}
libxml_use_internal_errors(true);
$issueXml = $exportFilter->execute($issue, true);
$xml = $issueXml->saveXml();
// $errors = array_filter(libxml_get_errors(), function($a) {
// return $a->level == LIBXML_ERR_ERROR || $a->level == LIBXML_ERR_FATAL;
// });
// if (!empty($errors)) {
// $this->displayXMLValidationErrors($errors, $xml);
// }
return $xml;
}
/**
* @param $issueId int
* @param $xml string XML file content
*/
protected function _uploadZip($issueId, $xml)
{
import('lib.pkp.classes.file.FileManager');
$fileManager = new FileManager();
$xmlFileName = $this->getExportPath() . 'Markup_unicode.xml';
$fileManager->writeFile($xmlFileName, $xml);
$issueDao = DAORegistry::getDAO('IssueDAO');
$issue = $issueDao->getById($issueId);
$coverUrl = $issue->getLocalizedCoverImageUrl();
$coverUrlParts = explode('/', $coverUrl);
$coverName = end($coverUrlParts);
$fileManager->copyFile($coverUrl, $this->getExportPath() . $coverName);
$publishedArticleDao = DAORegistry::getDAO('PublishedArticleDAO');
$articles = $publishedArticleDao->getPublishedArticles($issue->getId());
$articleGalleyDAO = DAORegistry::getDAO('ArticleGalleyDAO');
foreach ($articles as $article)
{
/** @var ArticleGalley $galley */
$galley = $articleGalleyDAO->getBySubmissionId($article->getId())->next();
$articleFilePath = $galley->getFile()->getFilePath();
$fileName = $galley->getFile()->getName(AppLocale::getPrimaryLocale());
$fileManager->copyFile($articleFilePath, $this->getExportPath() . explode(' ', $fileName)[1]);
}
// 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)) {
$zip->addFile($this->getExportPath() . $file, basename($file));
}
}
$zip->close();
// UPLOAD:
$fileManager->downloadByPath($zipPath);
$fileManager->rmtree($this->getExportPath());
}
/**
* @var string
*/
protected $_generatedTempPath = '';
/**
* @copydoc ImportExportPlugin::getExportPath()
*/
function getExportPath()
{
if ($this->_generatedTempPath === '')
{
$exportPath = parent::getExportPath();
$journal = Application::getRequest()->getJournal();
$this->_generatedTempPath = $exportPath . $this->getPluginSettingsPrefix() . 'Temp-' . date('Ymd-His'). $journal->getId() . '/';
}
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)
{
// TODO: Implement executeCLI() method.
}
/**
* Display the command-line usage information
* @param $scriptName string
*/
function usage($scriptName)
{
// TODO: Implement usage() method.
}
/**
* 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');
}
}