-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGrobidMetadataExtractorPlugin.php
More file actions
273 lines (231 loc) · 10.6 KB
/
Copy pathGrobidMetadataExtractorPlugin.php
File metadata and controls
273 lines (231 loc) · 10.6 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
<?php
/**
* @file GrobidMetadataExtractorPlugin.php
*
* Copyright (c) 2024 Simon Fraser University
* Copyright (c) 2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @brief Plugin to invoke an external Grobid service to extract submission metadata.
*/
namespace APP\plugins\generic\grobidMetadataExtractor;
use APP\template\TemplateManager;
use APP\core\Application;
use PKP\affiliation\Affiliation;
use PKP\config\Config;
use PKP\core\Registry;
use PKP\facades\Locale;
use PKP\plugins\GenericPlugin;
use PKP\db\DAORegistry;
use PKP\plugins\Hook;
use APP\facades\Repo;
use PKP\userGroup\UserGroup;
use PKP\security\Role;
class GrobidMetadataExtractorPlugin extends GenericPlugin
{
var $supportedMimeTypes = [
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/msword',
'application/vnd.oasis.opendocument.text',
'application/pdf',
];
var $convertMimeTypes = [
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/msword',
'application/vnd.oasis.opendocument.text',
];
/**
* @copydoc Plugin::register()
*
* @param null|mixed $mainContextId
*/
public function register($category, $path, $mainContextId = null): bool
{
if (parent::register($category, $path, $mainContextId)) {
if ($this->getEnabled($mainContextId)) {
Hook::add('Schema::get::submission', $this->augmentSubmissionSchema(...));
Hook::add('Schema::get::submissionFile', $this->augmentSubmissionFileSchema(...));
Hook::add('SubmissionFile::edit', $this->editSubmissionFile(...));
Hook::add('TemplateManager::display', $this->handleReloadInSubmissionWizard(...));
}
return true;
}
return false;
}
/**
* Detect when to reload publication in submission wizard
*/
function handleReloadInSubmissionWizard($hookName, $params) {
$request = Application::get()->getRequest();
if ($request->getRequestedPage() !== 'submission') {
return;
}
if ($request->getRequestedOp() === 'saved') {
return;
}
$templateMgr = $params[0];
$templateMgr->addJavaScript(
'plugin-funder-submission-wizard',
$request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/GrobidSubmissionWizard.js',
[
'contexts' => 'backend',
'priority' => TemplateManager::STYLE_SEQUENCE_LATE,
]
);
return false;
}
/**
* Add properties to the submission object.
*/
public function augmentSubmissionSchema(string $hookName, array $params): bool
{
$schema =& $params[0];
$schema->properties->grobidded = (object) [
'type' => 'boolean',
'description' => 'True if Grobid has already been invoked to pre-fill submission metadata',
'validation' => ['nullable'],
];
return Hook::CONTINUE;
}
/**
* Add properties to the submission file object.
*/
public function augmentSubmissionFileSchema(string $hookName, array $params): bool
{
$schema =& $params[0];
$schema->properties->grobidded = (object) [
'type' => 'boolean',
'description' => 'True if Grobid sourced metadata from this submission file',
'validation' => ['nullable'],
];
return Hook::CONTINUE;
}
function editSubmissionFile(string $hookName, array $args) : bool
{
$submissionFile =& $args[0];
$submission = Repo::submission()->get($submissionFile->getData('submissionId'));
$genreDao = DAORegistry::getDAO('GenreDAO');
$genre = $genreDao->getById((int) $submissionFile->getGenreId());
$pkpFileService = app()->get('file');
$file = $pkpFileService->get((int) $submissionFile->getData('fileId'));
// Do not submit to Grobid if the submission file does not look like the sort we want
if (
$submission->getData('submissionProgress') != 'start' ||
($submission->getData('grobidded') && !Config::getVar('grobidMetadataExtractor', 'repeat')) ||
$submissionFile->getFileStage() != $submissionFile::SUBMISSION_FILE_SUBMISSION ||
$genre->getKey() != 'SUBMISSION' ||
!$file ||
!in_array($file->mimetype, $this->supportedMimeTypes)
) return Hook::CONTINUE;
// Some files must be converted to PDF before Grobid can be executed on them.
if (in_array($file->mimetype, $this->convertMimeTypes)) {
// Copy the uploaded file into the temp directory (in case flysystem is using e.g. a remote filesystem)
$inputFilePath = tempnam(sys_get_temp_dir(), 'unoconv');
file_put_contents($inputFilePath, $pkpFileService->fs->read($file->path));
// Create an output filename for unoconv to use
$convertedFilePath = tempnam(sys_get_temp_dir(), 'unoconv');
unlink($convertedFilePath);
$convertedFilePath .= '.pdf';
$output = $result_code = null;
exec(
Config::getVar('grobidMetadataExtractor', 'unoconv', '/usr/bin/unoconv') . ' -o ' . escapeshellarg($convertedFilePath) . ' ' .
escapeshellarg($inputFilePath),
$output, $result_code
);
if ($result_code != 0) {
error_log('Grobid metadata extraction: unoconv failed to convert ' . $file->path . ' to PDF.');
return Hook::CONTINUE;
}
$inputFileContents = file_get_contents($convertedFilePath);
unlink($inputFilePath);
unlink($convertedFilePath);
} else {
$inputFileContents = $pkpFileService->fs->read($file->path);
}
// Invoke the Grobid client.
$client = Application::get()->getHttpClient();
$response = $client->request(
'POST',
Config::getVar('grobidMetadataExtractor', 'grobid_api_url', 'http://localhost:8070/api/processHeaderDocument'),
[
'headers' => [
'Accept' => 'application/xml',
],
'multipart' => [
[
'name' => 'input',
'contents' => $inputFileContents,
],
],
],
);
$doc = new \DOMDocument();
$doc->loadXML((string) $response->getBody());
$xpath = new \DOMXPath($doc);
$xpath->registerNamespace('tei', 'http://www.tei-c.org/ns/1.0');
$xpath->registerNamespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$xpath->registerNamespace('xlink', 'http://www.w3.org/1999/xlink');
$primaryLocale = $xpath->query('//tei:TEI/tei:teiHeader')->item(0)->getAttribute('xml:lang');
$currentPublication = $submission->getCurrentPublication();
// Extract the title & abstract
foreach ($xpath->query('//tei:TEI/tei:teiHeader/tei:fileDesc/tei:titleStmt/tei:title') as $titleNode) {
$currentPublication->setData('title', [$primaryLocale => htmlspecialchars($titleNode->nodeValue)]);
}
foreach ($xpath->query('//tei:TEI/tei:teiHeader/tei:profileDesc/tei:abstract') as $abstractNode) {
$currentPublication->setData('abstract', [$primaryLocale => htmlspecialchars($abstractNode->nodeValue)]);
}
// Extract author data
foreach ($xpath->query('//tei:TEI/tei:teiHeader/tei:fileDesc/tei:sourceDesc/tei:biblStruct/tei:analytic/tei:author') as $authorNode) {
$author = app(\APP\author\Author::class);
$submitAsUserGroup = UserGroup::withContextIds($submission->getData('contextId'))->withRoleIds(Role::ROLE_ID_AUTHOR)->first();
if (!$submitAsUserGroup) {
error_log('Grobid metadata extraction: aborting; no available author submitter user group.');
return Hook::CONTINUE;
}
$author->setData('userGroupId', $submitAsUserGroup->id);
$author->setData('email', ''); // This should be removed when author emails are made optional
$author->setData('givenName', $xpath->query('tei:persName/tei:forename[@type="first"]', $authorNode)->item(0)->nodeValue, $primaryLocale);
$author->setData('familyName', $xpath->query('tei:persName/tei:surname', $authorNode)->item(0)->nodeValue, $primaryLocale);
$author->setData('publicationId', $currentPublication->getId());
// We don't yet have a way to direct the submitting author to complete a partial record, so if an author record
// is missing important fields, do not create it automatically.
if (count(Repo::author()->validate($author, [], $submission, Application::get()->getRequest()->getContext()))) {
continue;
}
Repo::author()->add($author);
foreach ($xpath->query('tei:affiliation/tei:orgName[@type="institution"]', $authorNode) as $institutionNode) {
$institutionName = $institutionNode->nodeValue;
$affiliation = new Affiliation();
$rorMatches = Repo::ror()->getCollector()->filterByName($institutionName)->getMany();
if ($rorMatches->count() == 1) {
$ror = $rorMatches->first();
$affiliation->setRor($ror->getRor());
$affiliation->setName(null);
} else {
$affiliation->setName($institutionName, $primaryLocale);
}
$affiliation->setAuthorId($author->getId());
Repo::affiliation()->add($affiliation);
}
}
Repo::publication()->edit($currentPublication, ['title', 'abstract']);
// Stamp that the submission and submission file have been "grobidded"
Repo::submission()->edit($submission, ['grobidded' => true]);
$submissionFile->setData('grobidded', true);
return Hook::CONTINUE;
}
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDisplayName()
{
return __('plugins.generic.grobidMetadataExtractor.name');
}
/**
* @copydoc Plugin::getDescription()
*/
public function getDescription()
{
return __('plugins.generic.grobidMetadataExtractor.description');
}
}