forked from jonasraoni/article-importer-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArticleImporterPlugin.inc.php
198 lines (176 loc) · 5.79 KB
/
ArticleImporterPlugin.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
<?php
/**
* @file plugins/importexport/articleImporter/ArticleImporterPlugin.inc.php
*
* Copyright (c) 2014-2020 Simon Fraser University
* Copyright (c) 2000-2020 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class ArticleImporterPlugin
* @ingroup plugins_importexport_articleImporter
*
* @brief ArticleImporter XML import plugin
*/
namespace PKP\Plugins\ImportExport\ArticleImporter;
import('lib.pkp.classes.plugins.ImportExportPlugin');
import('lib.pkp.classes.submission.SubmissionFile');
import('lib.pkp.classes.file.SubmissionFileManager');
import('lib.pkp.classes.xml.XMLParser');
use \PKP\Plugins\ImportExport\ArticleImporter\Exceptions\ArticleSkippedException;
class ArticleImporterPlugin extends \ImportExportPlugin {
/**
* Registers a custom autoloader to handle the plugin namespace
*/
private function useAutoLoader() {
spl_autoload_register(function ($className) {
// Removes the base namespace from the class name
$path = explode(__NAMESPACE__ . '\\', $className, 2);
if (!reset($path)) {
// Breaks the remaining class name by \ to retrieve the folder and class name
$path = explode('\\', end($path));
$class = array_pop($path);
$path = array_map(function ($name) {
return strtolower($name[0]) . substr($name, 1);
}, $path);
$path[] = $class;
// Uses the internal loader
$this->import(implode('.' , $path));
}
});
}
/**
* @copydoc ImportExportPlugin::getDescription()
*/
public function executeCLI($scriptName, &$args): void
{
// Disable the time limit
set_time_limit(0);
// Expects 5 non-empty arguments
if (count(array_filter($args, 'strlen')) != 5) {
$this->usage($scriptName);
return;
}
// Map arguments to variables
[$contextPath, $username, $editorUsername, $email, $importPath] = $args;
$count = $imported = $failed = $skipped = 0;
try {
$configuration = new Configuration(
[Parsers\APlusPlus\Parser::class, Parsers\Jats\Parser::class],
$contextPath, $username, $editorUsername, $email, $importPath
);
$this->_writeLine(__('plugins.importexport.articleImporter.importStart'));
$sectionDao = \Application::getSectionDAO();
$lastIssueId = null;
// Iterates through all the found article entries, already sorted by ascending volume > issue > article
$iterator = $configuration->getArticleIterator();
$count = count($iterator);
foreach ($iterator as $entry) {
$article = implode('-', [$entry->getVolume(), $entry->getIssue(), $entry->getArticle()]);
try {
// Process the article
$parser = $entry->process($configuration);
++$imported;
$this->_writeLine(__('plugins.importexport.articleImporter.articleImported', ['article' => $article]));
}
catch (ArticleSkippedException $e) {
$this->_writeLine(__('plugins.importexport.articleImporter.articleSkipped', ['article' => $article, 'message' => $e->getMessage()]));
++$skipped;
}
catch (\Exception $e) {
$this->_writeLine(__('plugins.importexport.articleImporter.articleSkipped', ['article' => $article, 'message' => $e->getMessage()]));
++$failed;
}
}
// Resequences issue orders
if ($imported) {
$this->resequenceIssues($configuration);
}
$this->_writeLine(__('plugins.importexport.articleImporter.importEnd'));
}
catch (\Exception $e) {
$this->_writeLine(__('plugins.importexport.articleImporter.importError', ['message' => $e->getMessage()]));
}
$this->_writeLine(__('plugins.importexport.articleImporter.importStatus', ['count' => $count, 'imported' => $imported, 'failed' => $failed, 'skipped' => $skipped]));
}
/**
* Resequences the issues
* @param Configuration $configuration
*/
public function resequenceIssues(Configuration $configuration): void
{
$contextId = $configuration->getContext()->getId();
$issueDao = \DAORegistry::getDAO('IssueDAO');
// Clears previous ordering
$issueDao->deleteCustomIssueOrdering($contextId);
// Retrieves issue IDs sorted by volume and number
$rsIssues = \Services::get('issue')->getQueryBuilder([
'contextId' => $contextId,
'isPublished' => true,
'orderBy' => 'seq',
'orderDirection' => 'ASC'
])
->getQuery()
->orderBy('volume', 'DESC')
->orderBy('number', 'DESC')
->select('i.issue_id')
->pluck('i.issue_id');
$sequence = 0;
$latestIssue = null;
foreach ($rsIssues as $id) {
$latestIssue || ($latestIssue = $id);
$issueDao->insertCustomIssueOrder($contextId, $id, ++$sequence);
}
// Sets latest issue as the current one
$latestIssue = \Services::get('issue')->get($latestIssue);
$latestIssue->setData('current', true);
$issueDao->updateCurrent($configuration->getContext()->getId(), $latestIssue);
}
/**
* Outputs a message with a line break
* @param string $message
*/
private function _writeLine($message): void
{
echo $message, \PHP_EOL;
flush();
}
/**
* @copydoc Plugin::register()
*/
public function register(string $category, string $path, ?int $mainContextId = null): bool
{
$success = parent::register($category, $path);
$this->addLocaleData();
$this->useAutoLoader();
return $success;
}
/**
* @copydoc Plugin::getName()
*/
public function getName(): string
{
$class = explode('\\', __CLASS__);
return end($class);
}
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDisplayName(): string
{
return __('plugins.importexport.articleImporter.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
public function getDescription(): string
{
return __('plugins.importexport.articleImporter.description');
}
/**
* @copydoc ImportExportPlugin::usage()
*/
public function usage($scriptName): void
{
$this->_writeLine(__('plugins.importexport.articleImporter.cliUsage', ['scriptName' => $scriptName, 'pluginName' => $this->getName()]));
}
}