-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPublicationValidatorPlugin.php
236 lines (213 loc) · 8.15 KB
/
PublicationValidatorPlugin.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
<?php
import('lib.pkp.classes.plugins.GenericPlugin');
import('plugins.generic.publicationValidator.classes.services.ServiceDOAJ');
import('plugins.generic.publicationValidator.classes.services.ServiceOpenAire');
class PublicationValidatorPlugin extends GenericPlugin
{
public function register($category, $path, $mainContextId = NULL)
{
// Register the plugin even when it is not enabled
$success = parent::register($category, $path);
if ($success && $this->getEnabled()) {
// Do something when the plugin is enabled
\HookRegistry::register('Publication::validatePublish', [$this, 'validate']);
}
return $success;
}
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDisplayName()
{
return __('plugins.generic.publicationValidator.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
public function getDescription()
{
return __('plugins.generic.publicationValidator.description');
}
/**
* Add a settings action to the plugin's entry in the
* plugins list.
*
* @param Request $request
* @param array $actionArgs
* @return array
*/
public function getActions($request, $actionArgs) {
// Get the existing actions
$actions = parent::getActions($request, $actionArgs);
// Only add the settings action when the plugin is enabled
if (!$this->getEnabled()) {
return $actions;
}
// Create a LinkAction that will make a request to the
// plugin's `manage` method with the `settings` verb.
$router = $request->getRouter();
import('lib.pkp.classes.linkAction.request.AjaxModal');
$linkAction = new LinkAction(
'settings',
new AjaxModal(
$router->url(
$request,
null,
null,
'manage',
null,
[
'verb' => 'settings',
'plugin' => $this->getName(),
'category' => 'generic'
]
),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
);
// Add the LinkAction to the existing actions.
// Make it the first action to be consistent with
// other plugins.
array_unshift($actions, $linkAction);
return $actions;
}
/**
* Show and save the settings form when the settings action
* is clicked.
*
* @param array $args
* @param Request $request
* @return JSONMessage
*/
public function manage($args, $request) {
switch ($request->getUserVar('verb')) {
case 'settings':
// Load the custom form
$this->import('PublicationValidatorPluginSettingsForm');
$form = new PublicationValidatorPluginSettingsForm($this);
// Fetch the form the first time it loads, before
// the user has tried to save it
if (!$request->getUserVar('save')) {
$form->initData();
return new JSONMessage(true, $form->fetch($request));
}
// Validate and save the form data
$form->readInputData();
if ($form->validate()) {
$form->execute();
return new JSONMessage(true);
}
}
return parent::manage($args, $request);
}
/**
* Load a setting for a specific journal or load it from the config.inc.php if it is specified there.
*
* @param $contextId int The id of the journal from which the plugin settings should be loaded.
* @param $name string Name of the setting.
* @return mixed The setting value, either from the database for this context
* or from the global configuration file.
*/
function getSetting($contextId, $name) {
switch ($name) {
case 'enableOpenAire':
$config_value = Config::getVar('publicationValidator', 'openair');
break;
case 'enableDoaj':
$config_value = Config::getVar('publicationValidator', 'doaj');
break;
case 'enableBase':
$config_value = Config::getVar('publicationValidator', 'base');
break;
case 'enableWebOfScience':
$config_value = Config::getVar('publicationValidator', 'web_of_science');
break;
case 'enableCrossref':
$config_value = Config::getVar('publicationValidator', 'crossref');
break;
case 'enableJGate':
$config_value = Config::getVar('publicationValidator', 'jgate');
break;
default:
return parent::getSetting($contextId, $name);
}
return $config_value ?: parent::getSetting($contextId, $name);
}
/**
* Check if there exist a valid publication validator configuration section in the global config.inc.php of OJS.
* @return boolean True, if the config file has openair, doaj, base, web_of_science, crossref, jgate set in an [PublicationValidator] section
*/
public function isGloballyConfigured($key)
{
$configValue = Config::getVar('publicationValidator', $key);
return isset($configValue) && $configValue === 1;
}
/**
* create message which fields will be
* validated before publish when enabled OpenAire
* @return string
*/
public function validateOpenAireFields() :string
{
return __('plugins.generic.publicationValidator.field.abstract').' ,'.
__('plugins.generic.publicationValidator.field.authors').' ,'.
__('plugins.generic.publicationValidator.field.authorAffiliation').' ,'.
__('plugins.generic.publicationValidator.field.articleTitle').' ,'.
__('plugins.generic.publicationValidator.field.locale').' ,'.
__('plugins.generic.publicationValidator.field.publisher').' ,'.
__('plugins.generic.publicationValidator.field.doi').' ,'.
__('plugins.generic.publicationValidator.field.issn').' ,'.
__('plugins.generic.publicationValidator.field.subjects').' ,'.
__('plugins.generic.publicationValidator.field.licenseUrl').' ,'.
__('plugins.generic.publicationValidator.field.rights').' ,'.
__('plugins.generic.publicationValidator.field.common');
}
/**
* create message which fields will be
* validated before publish when enabled DOAJ
* @return string
*/
public function validateDoajFields() :string
{
return __('plugins.generic.publicationValidator.field.abstract').' ,'.
__('plugins.generic.publicationValidator.field.authors').' ,'.
__('plugins.generic.publicationValidator.field.authorAffiliation').' ,'.
__('plugins.generic.publicationValidator.field.articleTitle').' ,'.
__('plugins.generic.publicationValidator.field.locale').' ,'.
__('plugins.generic.publicationValidator.field.publisher').' ,'.
__('plugins.generic.publicationValidator.field.doi').' ,'.
__('plugins.generic.publicationValidator.field.issn').' ,'.
__('plugins.generic.publicationValidator.field.common');
}
/**
* Make additional validation checks against publishing requirements
*
* @see PKPPublicationService::validatePublish()
* @param $hookName string
* @param $args array [
* @option array Validation errors already identified
* @option Publication The publication to validate
* @option Submission The submission of the publication being validated
* @option array The locales accepted for this object
* @option string The primary locale for this object
* ]
*/
public function validate($hookName, $args) {
$errors =& $args[0];
$publication = $args[1];
$submission = $args[2];
$request = PKPApplication::get()->getRequest();
$context = $request->getContext();
if(Config::getVar('publicationValidator', 'doaj') === 1 || $this->getSetting($context->getId(), 'enableDoaj') == 1){
$errors = $errors + (new ServiceDOAJ())->validate($publication,$submission,$context,'DOAJ')->getErrors();
}
if(Config::getVar('publicationValidator', 'openair') === 1 || $this->getSetting($context->getId(), 'enableOpenAire') == 1){
$errors = $errors + (new ServiceOpenAire())->validate($publication,$submission,$context,'OpenAire')->getErrors();
}
if(!empty($errors)){
$errors = array_unique($errors);
}
}
}