-
Notifications
You must be signed in to change notification settings - Fork 6
/
MedraPlugin.php
381 lines (338 loc) · 11.7 KB
/
MedraPlugin.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
<?php
/**
* @file plugins/generic/medra/MedraPlugin.php
*
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2003-2024 John Willinsky
* Distributed under The MIT License. For full terms see the file LICENSE.
*
* @class MedraPlugin
*
* @brief Plugin to let managers export, and deposit DOIs and metadata to mEDRA
*
*/
namespace APP\plugins\generic\medra;
use APP\core\Application;
use APP\core\Services;
use APP\facades\Repo;
use APP\issue\Issue;
use APP\plugins\generic\medra\classes\MedraSettings;
use APP\plugins\IDoiRegistrationAgency;
use APP\submission\Submission;
use Illuminate\Support\Collection;
use PKP\context\Context;
use PKP\db\DAORegistry;
use PKP\doi\RegistrationAgencySettings;
use PKP\install\Installer;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
use PKP\plugins\PluginRegistry;
use PKP\services\PKPSchemaService;
class MedraPlugin extends GenericPlugin implements IDoiRegistrationAgency
{
private ?MedraExportPlugin $_exportPlugin = null;
private MedraSettings $settingsObject;
/**
* @see Plugin::getDisplayName()
*/
public function getDisplayName()
{
return __('plugins.generic.medra.displayName');
}
/**
* @see Plugin::getDescription()
*/
public function getDescription()
{
return __('plugins.generic.medra.description');
}
/**
* @copydoc Plugin::register()
*
* @param null|mixed $mainContextId
*/
public function register($category, $path, $mainContextId = null)
{
$success = parent::register($category, $path, $mainContextId);
if ($success) {
Hook::add('Installer::preInstall', [$this, 'preInstall']);
// 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 (Application::isUnderMaintenance()) {
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 = 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);
}
}
}
/**
* Provides submissions metadata in ONIX4DOI format for download
* @param Submission[] $submissions
*/
public function exportSubmissions(array $submissions, Context $context): array
{
// Get filter and set objectsFileNamePart (see: PubObjectsExportPlugin::prepareAndExportPubObjects)
$filterName = $this->_exportPlugin->getSubmissionFilter();
$xmlErrors = [];
$temporaryFileId = $this->_exportPlugin->exportAsDownload($context, $submissions, $filterName, 'articles', null, $xmlErrors);
return ['temporaryFileId' => $temporaryFileId, 'xmlErrors' => $xmlErrors];
}
/**
* Registers submissions DOIs
* @param Submission[] $submissions
*/
public function depositSubmissions(array $submissions, Context $context): array
{
$filterName = $this->_exportPlugin->getSubmissionFilter();
$responseMessage = '';
$status = $this->_exportPlugin->exportAndDeposit($context, $submissions, $filterName, $responseMessage);
return [
'hasErrors' => !$status,
'responseMessage' => $responseMessage
];
}
/**
* Provides issues metadata in ONIX4DOI format for download
* @param Issue[] $issues
*/
public function exportIssues(array $issues, Context $context): array
{
// Get filter and set objectsFileNamePart (see: PubObjectsExportPlugin::prepareAndExportPubObjects)
$filterName = $this->_exportPlugin->getIssueFilter();
$xmlErrors = [];
$temporaryFileId = $this->_exportPlugin->exportAsDownload($context, $issues, $filterName, 'issues', null, $xmlErrors);
return ['temporaryFileId' => $temporaryFileId, 'xmlErrors' => $xmlErrors];
}
/**
* Registers issues DOIs
* @param Issue[] $issues
*/
public function depositIssues(array $issues, Context $context): array
{
$filterName = $this->_exportPlugin->getIssueFilter();
$responseMessage = '';
$status = $this->_exportPlugin->exportAndDeposit($context, $issues, $filterName, $responseMessage);
return [
'hasErrors' => !$status,
'responseMessage' => $responseMessage
];
}
/**
* Add properties for mEDRA 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->getFailedMsgSettingName(),
];
foreach ($settings as $settingName) {
$schema->properties->{$settingName} = (object) [
'type' => 'string',
'apiSummary' => true,
'validation' => ['nullable'],
];
}
return HOOK::CONTINUE;
}
/**
* Includes plugin in list of configurable registration agencies for DOI depositing functionality
*
* @param string $hookName DoiSettingsForm::setEnabledRegistrationAgencies
* @param array $args [
*
* @option $enabledRegistrationAgencies array
* ]
*/
public function addAsRegistrationAgencyOption(string $hookName, array $args)
{
/** @var Collection<int,IDoiRegistrationAgency> $enabledRegistrationAgencies */
$enabledRegistrationAgencies = &$args[0];
$enabledRegistrationAgencies->add($this);
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->_exportPlugin->getName()] = $this->getRegistrationAgencyName();
return HOOK::CONTINUE;
}
/**
* Get configured registration agency display name for use in DOI management pages
*
*/
public function getRegistrationAgencyName(): string
{
return __('plugins.importexport.medra.registrationAgency.name');
}
/**
* 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;
}
return true;
}
/**
* @inheritDoc
*/
public function getSettingsObject(): RegistrationAgencySettings
{
if (!isset($this->settingsObject)) {
$this->settingsObject = new MedraSettings($this);
}
return $this->settingsObject;
}
/**
* 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;
}
/**
* @inheritDoc
*/
public function getAllowedDoiTypes(): array
{
return [
Repo::doi()::TYPE_PUBLICATION,
Repo::doi()::TYPE_ISSUE,
];
}
/**
* Adds mEDRA 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;
return HOOK::CONTINUE;
}
/**
* Get key for retrieving error message if one exists on DOI object
*/
public function getErrorMessageKey(): ?string
{
return $this->getFailedMsgSettingName();
}
/**
* Get key for retrieving registered message if one exists on DOI object
*
*/
public function getRegisteredMessageKey(): ?string
{
return null;
}
/**
* Get request failed message setting name.
*/
public function getFailedMsgSettingName(): string
{
return $this->getName() . '_failedMsg';
}
/**
* Helper to register hooks that are used in normal plugin setup.
*/
private function _pluginInitialization()
{
PluginRegistry::register('importexport', new MedraExportPlugin($this), $this->getPluginPath());
$this->_exportPlugin = PluginRegistry::getPlugin('importexport', 'MedraExportPlugin');
Hook::add('DoiSettingsForm::setEnabledRegistrationAgencies', [$this, 'addAsRegistrationAgencyOption']);
Hook::add('DoiSetupSettingsForm::getObjectTypes', [$this, 'addAllowedObjectTypes']);
Hook::add('DoiListPanel::setConfig', [$this, 'addRegistrationAgencyName']);
Hook::add('Schema::get::doi', [$this, 'addToSchema']);
Hook::add('Doi::markRegistered', [$this, 'editMarkRegisteredParams']);
}
/**
* Call the migration script before the plugin installation
*
* @param string $hookName Installer::preInstall
*/
public function preInstall($hookName, $args)
{
/** @var Installer $installer */
$installer = $args[0];
$version = $installer->getCurrentVersion();
if ($version->getProduct() == 'medra' && $version->getProductType() == 'plugins.generic') {
/** @var VersionDAO $versionDao */
$versionDao = DAORegistry::getDAO('VersionDAO');
$installedPluginVersion = $versionDao->getCurrentVersion($version->getProductType(), $version->getProduct());
if (!$installedPluginVersion) {
$migration = new MedraDoiDataMigration($installer, $this);
$migration->up();
}
}
return HOOK::CONTINUE;
}
}