forked from pkp/crossref-ops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrossrefPlugin.php
397 lines (345 loc) · 12.2 KB
/
CrossrefPlugin.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/**
* @file plugins/generic/crossref/CrossrefPlugin.php
*
* Copyright (c) 2014-2022 Simon Fraser University
* Copyright (c) 2003-2022 John Willinsky
* Distributed under The MIT License. For full terms see the file LICENSE.
*
* @class CrossrefPlugin
*
* @brief Plugin to let managers deposit DOIs and metadata to Crossref
*
*/
namespace APP\plugins\generic\crossref;
use APP\core\Services;
use APP\facades\Repo;
use APP\plugins\generic\crossref\classes\CrossrefSettings;
use APP\plugins\IDoiRegistrationAgency;
use APP\services\ContextService;
use Exception;
use Illuminate\Support\Collection;
use PKP\config\Config;
use PKP\context\Context;
use PKP\doi\RegistrationAgencySettings;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
use PKP\plugins\PluginRegistry;
use PKP\services\PKPSchemaService;
class CrossrefPlugin extends GenericPlugin implements IDoiRegistrationAgency
{
private CrossrefSettings $_settingsObject;
private ?CrossrefExportPlugin $_exportPlugin = null;
public function getDisplayName(): string
{
return __('plugins.generic.crossref.displayName');
}
public function getDescription(): string
{
return __('plugins.generic.crossref.description');
}
/**
* @copydoc Plugin::register()
*
* @param null|mixed $mainContextId
*/
public function register($category, $path, $mainContextId = null)
{
$success = parent::register($category, $path, $mainContextId);
if ($success) {
// If the system isn't installed, or is performing an upgrade, don't
// register hooks. This will prevent DB access attempts before the
// schema is installed.
if (!Config::getVar('general', 'installed') || defined('RUNNING_UPGRADE')) {
return true;
}
if ($this->getEnabled($mainContextId)) {
$this->_pluginInitialization();
}
}
return $success;
}
/**
* Remove plugin as configured registration agency if set at the time plugin is disabled.
*
* @copydoc LazyLoadPlugin::setEnabled()
*/
public function setEnabled($enabled)
{
parent::setEnabled($enabled);
if (!$enabled) {
$contextId = $this->getCurrentContextId();
/** @var \PKP\context\ContextDAO $contextDao */
$contextDao = \APP\core\Application::getContextDAO();
$context = $contextDao->getById($contextId);
if ($context->getData(Context::SETTING_CONFIGURED_REGISTRATION_AGENCY) === $this->getName()) {
$context->setData(Context::SETTING_CONFIGURED_REGISTRATION_AGENCY, Context::SETTING_NO_REGISTRATION_AGENCY);
$contextDao->updateObject($context);
}
}
}
/**
* Helper to register hooks that are used in normal plugin setup and in CLI tool usage.
*/
private function _pluginInitialization()
{
PluginRegistry::register('importexport', new CrossrefExportPlugin($this), $this->getPluginPath());
Hook::add('DoiSettingsForm::setEnabledRegistrationAgencies', [$this, 'addAsRegistrationAgencyOption']);
Hook::add('DoiSetupSettingsForm::getObjectTypes', [$this, 'addAllowedObjectTypes']);
Hook::add('Context::validate', [$this, 'validateAllowedPubObjectTypes']);
Hook::add('Schema::get::doi', [$this, 'addToSchema']);
Hook::add('Doi::markRegistered', [$this, 'editMarkRegisteredParams']);
Hook::add('DoiListPanel::setConfig', [$this, 'addRegistrationAgencyName']);
}
/**
* Add properties for Crossref to the DOI entity for storage in the database.
*
* @param string $hookName `Schema::get::doi`
* @param array $args [
*
* @option stdClass $schema
* ]
*
*/
public function addToSchema(string $hookName, array $args): bool
{
$schema = &$args[0];
$settings = [
$this->_getDepositBatchIdSettingName(),
$this->_getFailedMsgSettingName(),
$this->_getSuccessMsgSettingName(),
];
foreach ($settings as $settingName) {
$schema->properties->{$settingName} = (object) [
'type' => 'string',
'apiSummary' => true,
'validation' => ['nullable'],
];
}
return false;
}
/**
* @inheritDoc
*/
public function exportSubmissions(array $submissions, Context $context): array
{
// Get filter and set objectsFileNamePart (see: PubObjectsExportPlugin::prepareAndExportPubObjects)
/** @var CrossrefExportPlugin */
$exportPlugin = $this->_getExportPlugin();
$filterName = $exportPlugin->getSubmissionFilter();
$xmlErrors = [];
$temporaryFileId = $exportPlugin->exportAsDownload($context, $submissions, $filterName, 'preprints', null, $xmlErrors);
return ['temporaryFileId' => $temporaryFileId, 'xmlErrors' => $xmlErrors];
}
/**
* @inheritDoc
*/
public function depositSubmissions(array $submissions, Context $context): array
{
$exportPlugin = $this->_getExportPlugin();
$filterName = $exportPlugin->getSubmissionFilter();
$responseMessage = '';
$status = $exportPlugin->exportAndDeposit($context, $submissions, $filterName, $responseMessage);
return [
'hasErrors' => !$status,
'responseMessage' => $responseMessage
];
}
/**
* Includes plugin in list of configurable registration agencies for DOI depositing functionality
*
* @param string $hookName DoiSettingsForm::setEnabledRegistrationAgencies
* @param array $args [
*
* @option $enabledRegistrationAgencies Collection<IDoiRegistrationAgency>
* ]
*/
public function addAsRegistrationAgencyOption(string $hookName, array $args): bool
{
/** @var Collection<IDoiRegistrationAgency> $enabledRegistrationAgencies */
$enabledRegistrationAgencies = &$args[0];
$enabledRegistrationAgencies->add($this);
return Hook::CONTINUE;
}
/**
* Adds self to "allowed" list of pub object types that can be assigned DOIs for this registration agency.
*
* @param string $hookName DoiSetupSettingsForm::getObjectTypes
* @param array $args [
*
* @option array &$objectTypeOptions
* ]
*/
public function addAllowedObjectTypes(string $hookName, array $args): bool
{
$objectTypeOptions = &$args[0];
$allowedTypes = $this->getAllowedDoiTypes();
$objectTypeOptions = array_map(function ($option) use ($allowedTypes) {
if (in_array($option['value'], $allowedTypes)) {
$option['allowedBy'][] = $this->getName();
}
return $option;
}, $objectTypeOptions);
return Hook::CONTINUE;
}
/**
* Add validation rule to Context for restriction of allowed pubObject types for DOI registration.
*
* @throws \Exception
*/
public function validateAllowedPubObjectTypes(string $hookName, array $args): bool
{
$errors = &$args[0];
$props = $args[2];
if (!isset($props['enabledDoiTypes'])) {
return Hook::CONTINUE;
}
$contextId = $props['id'];
if (empty($contextId)) {
throw new Exception('A context ID must be present to edit context settings');
}
/** @var ContextService $contextService */
$contextService = Services::get('context');
$context = $contextService->get($contextId);
$enabledRegistrationAgency = $context->getConfiguredDoiAgency();
if (!$enabledRegistrationAgency instanceof $this) {
return Hook::CONTINUE;
}
$allowedTypes = $enabledRegistrationAgency->getAllowedDoiTypes();
if (!empty(array_diff($props['enabledDoiTypes'], $allowedTypes))) {
$errors['enabledDoiTypes'] = [__('doi.manager.settings.enabledDoiTypes.error')];
}
return Hook::CONTINUE;
}
/**
* Includes human-readable name of registration agency for display in conjunction with how/with whom the
* DOI was registered.
*
* @param string $hookName DoiListPanel::setConfig
* @param array $args [
*
* @option $config array
* ]
*/
public function addRegistrationAgencyName(string $hookName, array $args): bool
{
$config = &$args[0];
$config['registrationAgencyNames'][$this->_getExportPlugin()->getName()] = $this->getRegistrationAgencyName();
return HOOK::CONTINUE;
}
/**
* Checks if plugin meets registration agency-specific requirements for being active and handling deposits
*
*/
public function isPluginConfigured(Context $context): bool
{
$settingsObject = $this->getSettingsObject();
/** @var PKPSchemaService $schemaService */
$schemaService = Services::get('schema');
$requiredProps = $schemaService->getRequiredProps($settingsObject::class);
foreach ($requiredProps as $requiredProp) {
$settingValue = $this->getSetting($context->getId(), $requiredProp);
if (empty($settingValue)) {
return false;
}
}
$doiPrefix = $context->getData(Context::SETTING_DOI_PREFIX);
if (empty($doiPrefix)) {
return false;
}
if (!in_array(Repo::doi()::TYPE_PUBLICATION, $context->getData(Context::SETTING_ENABLED_DOI_TYPES) ?? [])) {
return false;
}
return true;
}
/**
* Get configured registration agency display name for use in DOI management pages
*/
public function getRegistrationAgencyName(): string
{
return __('plugins.generic.crossref.registrationAgency.name');
}
/**
* @inheritDoc
*/
public function getErrorMessageKey(): ?string
{
return $this->_getFailedMsgSettingName();
}
/**
* @inheritDoc
*/
public function getRegisteredMessageKey(): ?string
{
return $this->_getSuccessMsgSettingName();
}
/**
* Adds Crossref specific info to Repo::doi()->markRegistered()
*
* @param string $hookName Doi::markRegistered
*
*/
public function editMarkRegisteredParams(string $hookName, array $args): bool
{
$editParams = &$args[0];
$editParams[$this->_getFailedMsgSettingName()] = null;
$editParams[$this->_getSuccessMsgSettingName()] = null;
return false;
}
/**
* @return CrossrefExportPlugin
*/
private function _getExportPlugin()
{
if (empty($this->_exportPlugin)) {
$pluginCategory = 'importexport';
$pluginPathName = 'CrossrefExportPlugin';
$this->_exportPlugin = PluginRegistry::getPlugin($pluginCategory, $pluginPathName);
// If being run from CLI, there is no context, so plugin initialization would not have been fired
if ($this->_exportPlugin === null && !isset($_SERVER['SERVER_NAME'])) {
$this->_pluginInitialization();
$this->_exportPlugin = PluginRegistry::getPlugin($pluginCategory, $pluginPathName);
}
}
return $this->_exportPlugin;
}
/**
* Get request failed message setting name.
* NB: Change from 3.3.x to camelCase (over crossref::failedMsg)
*
*/
private function _getFailedMsgSettingName(): string
{
return $this->getName() . '_failedMsg';
}
/**
* Get deposit batch ID setting name.
* NB: Change from 3.3.x to camelCase (over crossref::batchId)
*
*/
private function _getDepositBatchIdSettingName(): string
{
return $this->getName() . '_batchId';
}
private function _getSuccessMsgSettingName(): string
{
return $this->getName() . '_successMsg';
}
/**
* @inheritDoc
*/
public function getSettingsObject(): RegistrationAgencySettings
{
if (!isset($this->_settingsObject)) {
$this->_settingsObject = new CrossrefSettings($this);
}
return $this->_settingsObject;
}
/**
* @inheritDoc
*/
public function getAllowedDoiTypes(): array
{
return [Repo::doi()::TYPE_PUBLICATION];
}
}