forked from jonasraoni/article-importer-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseParser.inc.php
More file actions
335 lines (302 loc) · 9.79 KB
/
BaseParser.inc.php
File metadata and controls
335 lines (302 loc) · 9.79 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
<?php
/**
* @file plugins/importexport/articleImporter/BaseParser.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 BaseParser
* @ingroup plugins_importexport_articleImporter
*
* @brief Base class that parsers should extend
*/
namespace PKP\Plugins\ImportExport\ArticleImporter;
use \PKP\Plugins\ImportExport\ArticleImporter\Exceptions\{InvalidDocTypeException, AlreadyExistsException};
abstract class BaseParser {
/** @var Configuration Configuration */
private $_configuration;
/** @var ArticleEntry Article entry */
private $_entry;
/** @var \DOMDocument The DOMDocument instance for the XML metadata */
private $_document;
/** @var \DOMXPath The DOMXPath instance for the XML metadata */
private $_xpath;
/** @var int Context ID */
private $_contextId;
/** @var string Default locale, grabbed from the context */
private $_locale;
/** @var int[] cache of genres by context id and extension */
private $_cachedGenres;
/**
* Constructor
* @param Configuration $configuration
* @param ArticleEntry $entry
*/
public function __construct(Configuration $configuration, ArticleEntry $entry)
{
$this->_configuration = $configuration;
$this->_entry = $entry;
$context = $this->_configuration->getContext();
$this->_contextId = $context->getId();
$this->_locale = $context->getPrimaryLocale();
}
/**
* Parses the publication
* @return \Publication
*/
public abstract function getPublication(): \Publication;
/**
* Parses the issue
* @return \Issue
*/
public abstract function getIssue(): \Issue;
/**
* Parses the submission
* @return \Submission
*/
public abstract function getSubmission(): \Submission;
/**
* Parses the section
* @return \Section
*/
public abstract function getSection(): \Section;
/**
* Retrieves the public IDs
* @return array Returns array, where the key is the type and value the ID
*/
public abstract function getPublicIds(): array;
/**
* Rollbacks the process
*/
public abstract function rollback(): void;
/**
* Retrieves the DOCTYPE
* @return array \DOMDocumentType[]
*/
public abstract function getDocType(): array;
/**
* Executes the parser
* @throws \Exception Throws when something goes wrong, and an attempt to revert the actions will be performed
*/
public function execute(): void
{
try {
$this
->_ensureMetadataIsValidAndParse()
->_ensureSubmissionDoesNotExist()
->getPublication();
}
catch (\Exception $e) {
$this->rollback();
throw $e;
}
}
/**
* Validates the metadata file and try to parse the XML
* @throws \Exception Throws when there's an error to parse the XML
* @return Parser
*/
private function _ensureMetadataIsValidAndParse(): self
{
// Tries to parse the XML
$this->_document = new \DOMDocument();
if (!$this->_document->load($this->getArticleEntry()->getMetadataFile()->getPathname())) {
throw new \Exception(__('plugins.importexport.jats.failedToParseXMLDocument'));
}
// Checks whether the loaded document is supported by the parser (the doctype should match)
$docType = $this->_document->doctype;
$supportedDocTypes = $this->getDocType();
$found = false;
foreach ($supportedDocTypes as $supportedDocType) {
if ([$docType->systemId, $docType->publicId, $docType->name] == [$supportedDocType->systemId, $supportedDocType->publicId, $supportedDocType->name]) {
$found = true;
break;
}
}
if (!$found) {
throw new InvalidDocTypeException(__('plugins.importexport.articleImporter.invalidDoctype'));
}
$this->_xpath = new \DOMXPath($this->_document);
return $this;
}
/**
* Evaluates and retrieves the given XPath expression
* @param string $path XPath expression
* @param \DOMNode $context Optional context node
* @return mixed
*/
public function evaluate(string $path, ?\DOMNode $context = null)
{
return $this->_xpath->evaluate($path, $context);
}
/**
* Evaluates and retrieves the given XPath expression as a trimmed and tag stripped string
* The path is expected to target a single node
* @param string $path XPath expression
* @param \DOMNode $context Optional context node
* @return string
*/
public function selectText(string $path, ?\DOMNode $context = null): string
{
return \strip_tags(\trim($this->evaluate("string($path)", $context)));
}
/**
* Retrieves the nodes that match the given XPath expression
* @param string $path XPath expression
* @param \DOMNode $context Optional context node
* @return \DOMNodeList
*/
public function select(string $path, ?\DOMNode $context = null): \DOMNodeList
{
return $this->_xpath->query($path, $context);
}
/**
* Query the given XPath expression and retrieves the first item
* @param string $path XPath expression
* @param \DOMNode $context Optional context node
* @return object|null
*/
public function selectFirst(string $path, ?\DOMNode $context = null): ?object
{
return $this->select($path, $context)->item(0);
}
/**
* Checks if the submission isn't already registered using the public IDs
* @throws \Exception Throws when a submission with the same public ID is found
* @return Parser
*/
public function _ensureSubmissionDoesNotExist(): self
{
foreach ($this->getPublicIds() as $type => $id) {
if (\Application::getSubmissionDAO()->getByPubId($type, $id, $this->getContextId())) {
throw new AlreadyExistsException(__('plugins.importexport.articleImporter.alreadyExists', ['type' => $type, 'id' => $id]));
}
}
return $this;
}
/**
* Retrieves the context ID
* @return int
*/
public function getContextId(): int
{
return $this->_contextId;
}
/**
* Tries to map the given locale to the PKP standard, returns the default locale if it fails or if the parameter is null
* @param string $locale
* @return string
*/
public function getLocale(?string $locale = null): string
{
if ($locale && !\PKPLocale::isLocaleValid($locale)) {
$locale = strtolower($locale);
// Tries to convert from recognized formats
$iso3 = \PKPLocale::getIso3FromIso1($locale) ?: \PKPLocale::getIso3FromLocale($locale);
// If the language part of the locale is the same (ex. fr_FR and fr_CA), then gives preference to context's locale
$locale = $iso3 == \PKPLocale::getIso3FromLocale($this->_locale) ? $this->_locale : \PKPLocale::getLocaleFromIso3($iso3);
}
return $locale ?: $this->_locale;
}
/**
* Retrieves the configuration instance
* @return Configuration
*/
public function getConfiguration(): Configuration
{
return $this->_configuration;
}
/**
* Retrieves the article entry instance
* @return ArticleEntry
*/
public function getArticleEntry(): ArticleEntry
{
return $this->_entry;
}
/**
* Includes a section into the issue custom order
* @param \Section $section
*/
public function includeSection(\Section $section): void
{
static $cache = [];
// If the section wasn't included into the issue yet
if (!isset($cache[$this->getIssue()->getId()][$section->getId()])) {
// Adds it to the list
$cache[$this->getIssue()->getId()][$section->getId()] = true;
$sectionDao = \Application::getSectionDAO();
// Checks whether the section is already present in the issue
if (!$sectionDao->getCustomSectionOrder($this->getIssue()->getId(), $section->getId())) {
$sectionDao->insertCustomSectionOrder($this->getIssue()->getId(), $section->getId(), count($cache[$this->getIssue()->getId()]));
}
}
}
/**
* Manually evaluates the textContent of the node with the help of a transformation callback
* @param \DOMNode|null $node
* @param callable $callback The callback will receive two arguments, the current node being parsed and the already transformed textContent of it
*/
public function getTextContent(?\DOMNode $node, callable $callback){
if (!$node) {
return null;
}
if ($node instanceof \DOMText) {
return htmlspecialchars($node->textContent, ENT_HTML5 | ENT_NOQUOTES);
}
$data = '';
foreach($node->childNodes ?? [] as $child) {
$data .= $this->getTextContent($child, $callback);
}
return $callback($node, $data);
}
/**
* Looks in $issueFolder for a cover image, and applies it to $issue if found
* @param string $issueFolder
* @param \Issue $issue
* @return void
*/
public function setIssueCover(string $issueFolder, \Issue &$issue) {
$issueDao = \DAORegistry::getDAO('IssueDAO');
$issueCover = null;
foreach ($this->getConfiguration()->getImageExtensions() as $ext) {
$checkFile = $issueFolder.DIRECTORY_SEPARATOR.$this->getConfiguration()->getIssueCoverFilename().'.'.$ext;
if (file_exists($checkFile)) {
$issueCover = $checkFile;
break;
}
}
if ($issueCover) {
import('classes.file.PublicFileManager');
$publicFileManager = new \PublicFileManager();
$fileparts = explode('.', $issueCover);
$ext = array_pop($fileparts);
$newFileName = 'cover_issue_' . $issue->getId() . '_' . $this->getLocale() . '.' . $ext;
$publicFileManager->copyContextFile($this->getContextId(), $issueCover, $newFileName);
$issue->setCoverImage($newFileName, $this->getLocale());
$issueDao->updateObject($issue);
}
}
/**
* Find a Genre ID for a context and extension
* @param $contextId
* @param $extension
* @return int
*/
protected function _getGenreId($contextId, $extension) {
if (isset($this->_cachedGenres[$contextId][$extension])) {
return $this->_cachedGenres[$contextId][$extension];
}
$genreDao = \DAORegistry::getDAO('GenreDAO');
if (in_array($extension, $this->getConfiguration()->getImageExtensions())) {
$genre = $genreDao->getByKey('IMAGE', $contextId);
$genreId = $genre->getId();
} else {
$genre = $genreDao->getByKey('MULTIMEDIA', $contextId);
$genreId = $genre->getId();
}
$this->_cachedGenres[$contextId][$extension] = $genreId;
return $genreId;
}
}