-
Notifications
You must be signed in to change notification settings - Fork 7
/
MedraDoiDataMigration.php
325 lines (290 loc) · 12.2 KB
/
MedraDoiDataMigration.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
<?php
/**
* @file plugins/generic/medra/MedraDoiDataMigration.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.
*
* @class MedraDoiDataMigration
*
* @brief Migrations for the mEDRA DOI settings
*/
namespace APP\plugins\generic\medra;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
use PKP\core\Core;
use PKP\doi\Doi;
use PKP\file\FileManager;
use PKP\install\DowngradeNotSupportedException;
class MedraDoiDataMigration extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
$this->migrateMedraSettings();
}
public function migrateMedraSettings(): void
{
// ===== Filters ===== //
// Old filters should not be there in 3.4 but
// for a case there are, remove them.
// The new filters will be installed with this plugin.
$galleyFilterGroupId = DB::table('filters')
->where('class_name', '=', 'plugins.importexport.medra.filter.GalleyMedraXmlFilter')
->orWhere('class_name', '=', 'plugins.importexport.medra.filter.ArticleMedraXmlFilter')
->orWhere('class_name', '=', 'plugins.importexport.medra.filter.IssueMedraXmlFilter')
->pluck('filter_group_id');
DB::table('filter_groups')->whereIn('filter_group_id', $galleyFilterGroupId)->delete();
// ===== Issues Statuses & Settings ===== //
// 1. Get issues with mEDRA-related info
$issueData = DB::table('issues', 'i')
->leftJoin('issue_settings as iss', 'i.issue_id', '=', 'iss.issue_id')
->whereIn('iss.setting_name', ['medra::registeredDoi', 'medra::status'])
->select(['i.issue_id', 'i.doi_id', 'iss.setting_name', 'iss.setting_value'])
->get()
->reduce(function ($carry, $item) {
if (!isset($carry[$item->issue_id])) {
$carry[$item->issue_id] = [
'doi_id' => $item->doi_id
];
}
$carry[$item->issue_id][$item->setting_name] = $item->setting_value;
return $carry;
}, []);
// 2. Map statuses insert statements
$statuses = [];
$registrationAgencies = [];
foreach ($issueData as $item) {
// Status
if (isset($item['medra::status'])) {
$status = Doi::STATUS_ERROR;
$registrationAgency = null;
if (in_array($item['medra::status'], ['found', 'registered', 'markedRegistered'])) {
if ($item['medra::status'] === 'registered') {
$registrationAgency = 'MedraExportPlugin';
}
$status = Doi::STATUS_REGISTERED;
} elseif (isset($item['medra::registeredDoi'])) {
$status = Doi::STATUS_REGISTERED;
}
$statuses[$item['doi_id']] = ['status' => $status];
$registrationAgencies[$item['doi_id']] = $registrationAgency;
}
}
// 3. Insert updated statuses
foreach ($statuses as $doiId => $insert) {
DB::table('dois')
->where('doi_id', '=', $doiId)
->update($insert);
}
foreach ($registrationAgencies as $doiId => $agency) {
if ($agency === null) {
continue;
}
DB::table('doi_settings')
->insert([
'doi_id' => $doiId,
'setting_name' => 'registrationAgency',
'setting_value' => $agency
]);
}
// 4. Clean up old settings
DB::table('issue_settings')
->whereIn('setting_name', ['medra::registeredDoi', 'medra::status'])
->delete();
// ===== Publications Statuses & Settings ===== //
// 1. Get publications with mEDRA-related info
$publicationData = DB::table('submissions', 's')
->leftJoin('submission_settings as ss', 's.submission_id', '=', 'ss.submission_id')
->leftJoin('publications as p', 's.current_publication_id', '=', 'p.publication_id')
->whereIn('ss.setting_name', ['medra::registeredDoi', 'medra::status'])
->select(['p.publication_id', 'p.doi_id', 'ss.setting_name', 'ss.setting_value'])
->get()
->reduce(function ($carry, $item) {
if (!isset($carry[$item->publication_id])) {
$carry[$item->publication_id] = [
'doi_id' => $item->doi_id
];
}
$carry[$item->publication_id][$item->setting_name] = $item->setting_value;
return $carry;
}, []);
// 2. Map statuses insert statements
$statuses = [];
$registrationAgencies = [];
foreach ($publicationData as $item) {
// Status
if (isset($item['medra::status'])) {
$status = Doi::STATUS_ERROR;
$registrationAgency = null;
if (in_array($item['medra::status'], ['found', 'registered', 'markedRegistered'])) {
if ($item['medra::status'] === 'registered') {
$registrationAgency = 'MedraExportPlugin';
}
$status = Doi::STATUS_REGISTERED;
} elseif (isset($item['medra::registeredDoi'])) {
$status = Doi::STATUS_REGISTERED;
}
$statuses[$item['doi_id']] = ['status' => $status];
$registrationAgencies[$item['doi_id']] = $registrationAgency;
}
}
// 3. Insert updated statuses
foreach ($statuses as $doiId => $insert) {
DB::table('dois')
->where('doi_id', '=', $doiId)
->update($insert);
}
foreach ($registrationAgencies as $doiId => $agency) {
if ($agency === null) {
continue;
}
// s. https://github.com/pkp/medra/issues/21 why we use upsert here
DB::table('doi_settings')
->upsert(
[
'doi_id' => $doiId,
'setting_name' => 'registrationAgency',
'setting_value' => $agency
],
['doi_id', 'locale', 'setting_name'],
['setting_value']
);
}
// 4. Clean up old settings
DB::table('submission_settings')
->whereIn('setting_name', ['medra::registeredDoi', 'medra::status'])
->delete();
// ===== Galleys Statuses & Settings ===== //
// 1. Get galleys with mEDRA-related info
$galleyData = DB::table('publication_galleys', 'pg')
->leftJoin('publication_galley_settings as pgs', 'pg.galley_id', '=', 'pgs.galley_id')
->whereIn('pgs.setting_name', ['medra::registeredDoi', 'medra::status'])
->select(['pg.galley_id', 'pg.doi_id', 'pgs.setting_name', 'pgs.setting_value'])
->get()
->reduce(function ($carry, $item) {
if (!isset($carry[$item->galley_id])) {
$carry[$item->galley_id] = [
'doi_id' => $item->doi_id
];
}
$carry[$item->galley_id][$item->setting_name] = $item->setting_value;
return $carry;
}, []);
// 2. Map statuses insert statements
$statuses = [];
$registrationAgencies = [];
foreach ($galleyData as $item) {
// Status
if (isset($item['medra::status'])) {
$status = Doi::STATUS_ERROR;
$registrationAgency = null;
if (in_array($item['medra::status'], ['found', 'registered', 'markedRegistered'])) {
if ($item['medra::status'] === 'registered') {
$registrationAgency = 'MedraExportPlugin';
}
$status = Doi::STATUS_REGISTERED;
} elseif (isset($item['medra::registeredDoi'])) {
$status = Doi::STATUS_REGISTERED;
}
$statuses[$item['doi_id']] = ['status' => $status];
$registrationAgencies[$item['doi_id']] = $registrationAgency;
}
}
// 3. Insert updated statuses
foreach ($statuses as $doiId => $insert) {
DB::table('dois')
->where('doi_id', '=', $doiId)
->update($insert);
}
foreach ($registrationAgencies as $doiId => $agency) {
if ($agency === null) {
continue;
}
DB::table('doi_settings')
->insert([
'doi_id' => $doiId,
'setting_name' => 'registrationAgency',
'setting_value' => $agency
]);
}
// 4. Clean up old settings
DB::table('publication_galley_settings')
->whereIn('setting_name', ['medra::registeredDoi', 'medra::status'])
->delete();
// ===== General cleanup ===== //
// If any mEDRA settings are configured, assume plugin is in use and enable
$contextsWithPluginEnabled = DB::table('journals')
->whereIn('journal_id', function (Builder $q) {
$q->select('context_id')
->from('plugin_settings')
->where('plugin_name', '=', 'medraexportplugin');
})
->select(['journal_id'])
->get();
$contextsWithPluginEnabled->each(function ($item) {
DB::table('plugin_settings')
->insert(
[
'plugin_name' => 'medraplugin',
'context_id' => $item->journal_id,
'setting_name' => 'enabled',
'setting_value' => 1,
'setting_type' => 'bool'
]
);
});
// Enable automatic DOI deposit if configured
$contextsWithAutomaticDeposit = DB::table('journals')
->whereIn('journal_id', function (Builder $q) {
$q->select(['context_id'])
->from('plugin_settings')
->where('plugin_name', '=', 'medraexportplugin')
->where('setting_name', '=', 'automaticRegistration')
->where('setting_value', '=', 1) ;
})
->select(['journal_id'])
->get();
$contextsWithAutomaticDeposit->each(function ($item) {
DB::table('journal_settings')
->upsert(
[
'journal_id' => $item->journal_id,
'setting_name' => 'automaticDoiDeposit',
'setting_value' => 1
],
['journal_id', 'locale', 'setting_name'],
['setting_value']
);
});
DB::table('plugin_settings')
->where('plugin_name', '=', 'medraexportplugin')
->where('setting_name', '=', 'automaticRegistration')
->delete();
DB::table('plugin_settings')
->where('plugin_name', '=', 'medraexportplugin')
->update(['plugin_name' => 'medraplugin']);
// Delete no-longer-in-use version for importExport plugin
DB::table('versions')
->where('product_type', '=', 'plugins.importexport')
->where('product', '=', 'medra')
->delete();
// Delete no-longer-in-use files for importExport plugin, in case there are still there.
$fileManager = new FileManager();
$oldMedraImportExportPlugin = Core::getBaseDir() . '/plugins/importexport/medra';
$fileManager->rmtree($oldMedraImportExportPlugin);
}
/**
* Reverse the migrations
*
* @throws DowngradeNotSupportedException
*/
public function down(): void
{
throw new DowngradeNotSupportedException();
}
}