-
Notifications
You must be signed in to change notification settings - Fork 1
/
URNDNBPubIdPlugin.inc.php
301 lines (261 loc) · 8.91 KB
/
URNDNBPubIdPlugin.inc.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
<?php
/**
* @file URNDNBPubIdPlugin.inc.php
*
* Author: Božana Bokan, Center for Digital Systems (CeDiS), Freie Universität Berlin
* Last update: September 25, 2015
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @package plugins.pubIds.urnDNB
* @class URNDNBPubIdPlugin
*
* @brief DNB URN plugin class
*/
import('classes.plugins.PubIdPlugin');
class URNDNBPubIdPlugin extends PubIdPlugin {
//
// Implement template methods from PKPPlugin.
//
/**
* @see PubIdPlugin::register()
*/
function register($category, $path) {
$success = parent::register($category, $path);
$this->addLocaleData();
return $success;
}
/**
* @see PKPPlugin::getName()
*/
function getName() {
return 'URNDNBPubIdPlugin';
}
/**
* @see PKPPlugin::getDisplayName()
*/
function getDisplayName() {
return __('plugins.pubIds.urnDNB.displayName');
}
/**
* @see PKPPlugin::getDescription()
*/
function getDescription() {
return __('plugins.pubIds.urnDNB.description');
}
/**
* @see PKPPlugin::getTemplatePath()
*/
function getTemplatePath() {
return parent::getTemplatePath() . 'templates/';
}
//
// Implement template methods from PubIdPlugin.
//
/**
* @see PubIdPlugin::getPubId()
*/
function getPubId(&$pubObject, $preview = false) {
$urn = $pubObject->getStoredPubId($this->getPubIdType());
if (!$urn && !$this->isExcluded($pubObject)) {
// Determine the type of the publishing object
$pubObjectType = $this->getPubObjectType($pubObject);
// concerning DNB policy URNs can only be assigned to galleys
// in the article main language
if ($pubObjectType != 'Galley') return null;
$galley = $pubObject;
if (!$galley->isPdfGalley()) return null;
// Get the journal id of the object
// Retrieve the published article
assert(is_a($pubObject, 'ArticleFile'));
$articleDao = DAORegistry::getDAO('ArticleDAO');
$article = $articleDao->getArticle($pubObject->getArticleId(), null, true);
if (!$article) return null;
//$articleLanguages = $article->getLanguage();
$articleLanguages = array_map('trim', explode(';', $article->getLanguage()));
$galleyLocale = $galley->getLocale();
//if ($galley->getLocale() != $article->getLocale()) return null;
if (AppLocale::getIso1FromLocale($galleyLocale) != $articleLanguages[0]) return null;
// Now we can identify the journal
$journalId = $article->getJournalId();
// get the journal
$journal = $this->getJournal($journalId);
if (!$journal) return null;
$journalId = $journal->getId();
// Retrieve the issue
assert(!is_null($article));
$issueDao = DAORegistry::getDAO('IssueDAO');
$issue = $issueDao->getIssueByArticleId($article->getId(), $journal->getId(), true);
// Retrieve the URN prefix
$urnPrefix = $this->getSetting($journal->getId(), 'urnDNBPrefix');
if (empty($urnPrefix)) return null;
// Generate the URN suffix
$urnSuffixSetting = $this->getSetting($journal->getId(), 'urnDNBSuffix');
switch ($urnSuffixSetting) {
case 'customIdentifier':
$urnSuffix = $pubObject->getData('urnDNBSuffix');
if (!empty($urnSuffix)) {
$urn = $urnPrefix . $urnSuffix;
}
break;
case 'pattern':
$suffixPattern = $this->getSetting($journal->getId(), "urnDNBGalleySuffixPattern");
$urn = $urnPrefix . $suffixPattern;
// %j - journal initials
if ($journal->getLocalizedSetting('initials', $journal->getPrimaryLocale())) {
$suffixPattern = String::regexp_replace('/%j/', String::strtolower($journal->getLocalizedSetting('initials', $journal->getPrimaryLocale())), $suffixPattern);
}
// %x - custom identifier
if ($pubObject->getStoredPubId('publisher-id')) {
$urnSuffix = String::regexp_replace('/%x/', $pubObject->getStoredPubId('publisher-id'), $suffixPattern);
}
if ($issue) {
// %v - volume number
$suffixPattern = String::regexp_replace('/%v/', $issue->getVolume(), $suffixPattern);
// %i - issue number
$suffixPattern = String::regexp_replace('/%i/', $issue->getNumber(), $suffixPattern);
// %Y - year
$suffixPattern = String::regexp_replace('/%Y/', $issue->getYear(), $suffixPattern);
}
// %a - article id
$suffixPattern = String::regexp_replace('/%a/', $article->getId(), $suffixPattern);
// %p - page number
$suffixPattern = String::regexp_replace('/%p/', $article->getPages(), $suffixPattern);
// %g - galley id
$suffixPattern = String::regexp_replace('/%g/', $galley->getId(), $suffixPattern);
$urn = $urnPrefix . $suffixPattern;
$urn .= $this->_calculateCheckNo($urn);
break;
default:
if ($journal->getLocalizedSetting('initials', $journal->getPrimaryLocale())) {
$suffixPattern = String::strtolower($journal->getLocalizedSetting('initials', $journal->getPrimaryLocale()));
} else {
$suffixPattern = '%j';
}
if ($issue) {
$suffixPattern .= '.v' . $issue->getVolume() . 'i' . $issue->getNumber();
} else {
$suffixPattern = '.v%vi%i';
}
$suffixPattern .= '.' . $article->getId();
$suffixPattern .= '.g' . $galley->getId();
$urn = $urnPrefix . $suffixPattern;
$urn .= $this->_calculateCheckNo($urn);
}
if ($urn && !$preview) {
$this->setStoredPubId($pubObject, $pubObjectType, $urn);
}
}
return $urn;
}
/**
* @see PubIdPlugin::getPubIdType()
*/
function getPubIdType() {
return 'other::urnDNB';
}
/**
* @see PubIdPlugin::getPubIdDisplayType()
*/
function getPubIdDisplayType() {
return 'URN';
}
/**
* @see PubIdPlugin::getPubIdFullName()
*/
function getPubIdFullName() {
return 'Uniform Resource Name';
}
/**
* @see PubIdPlugin::getResolvingURL()
*/
function getResolvingURL($journalId, $pubId) {
$resolverURL = $this->getSetting($journalId, 'urnDNBResolver');
return $resolverURL . $pubId;
}
/**
* @see PubIdPlugin::getFormFieldNames()
*/
function getFormFieldNames() {
return array('urnDNBSuffix', 'excludeURNDNB');
}
/**
* @see PubIdPlugin::getExcludeFormFieldName()
*/
function getExcludeFormFieldName() {
return 'excludeURNDNB';
}
/**
* @see PubIdPlugin::getDAOFieldNames()
*/
function getDAOFieldNames() {
return array('pub-id::other::urnDNB');
}
/**
* @see PubIdPlugin::getPubIdMetadataFile()
*/
function getPubIdMetadataFile() {
return $this->getTemplatePath().'urnSuffixEdit.tpl';
}
/**
* @see PubIdPlugin::getSettingsFormName()
*/
function getSettingsFormName() {
return 'classes.form.URNDNBSettingsForm';
}
/**
* @see PubIdPlugin::verifyData()
*/
function verifyData($fieldName, $fieldValue, &$pubObject, $journalId, &$errorMsg) {
if ($fieldName == 'urnDNBSuffix') {
if (empty($fieldValue)) return true;
// Construct the potential new URN with the posted suffix.
$urnPrefix = $this->getSetting($journalId, 'urnDNBPrefix');
if (empty($urnPrefix)) return true;
$newURN = $urnPrefix . $fieldValue;
$newURNWithoutCheckNo = substr($newURN, 0, -1);
$newURNWithCheckNo = $newURNWithoutCheckNo . $this->_calculateCheckNo($newURNWithoutCheckNo);
if ($newURN != $newURNWithCheckNo) {
$errorMsg = __('plugins.pubIds.urnDNB.form.checkNoRequired');
return false;
}
if(!$this->checkDuplicate($newURN, $pubObject, $journalId)) {
$errorMsg = __('plugins.pubIds.urnDNB.form.customIdentifierNotUnique');
return false;
}
}
return true;
}
//
// Private helper methods
//
/**
* Get the last, check number.
* Algorithm (s. http://www.persistent-identifier.de/?link=316):
* every URN character is replaced with a number according to the conversion table,
* every number is multiplied by it's position/index (beginning with 1),
* the numbers' sum is calculated,
* the sum is devided by the last number,
* the last number of the quotient before the decimal point is the check number.
* @param $urn string
* @return string
*/
function _calculateCheckNo($urn) {
$urnLower = strtolower_codesafe($urn);
$conversionTable = array('9' => '41', '8' => '9', '7' => '8', '6' => '7', '5' => '6', '4' => '5', '3' => '4', '2' => '3', '1' => '2', '0' => '1', 'a' => '18', 'b' => '14', 'c' => '19', 'd' => '15', 'e' => '16', 'f' => '21', 'g' => '22', 'h' => '23', 'i' => '24', 'j' => '25', 'k' => '42', 'l' => '26', 'm' => '27', 'n' => '13', 'o' => '28', 'p' => '29', 'q' => '31', 'r' => '12', 's' => '32', 't' => '33', 'u' => '11', 'v' => '34', 'w' => '35', 'x' => '36', 'y' => '37', 'z' => '38', '-' => '39', ':' => '17', '_' => '43', '/' => '45', '.' => '47', '+' => '49');
$newURN = '';
for ($i = 0; $i < strlen($urnLower); $i++) {
$char = $urnLower[$i];
$newURN .= $conversionTable[$char];
}
$sum = 0;
for ($j = 1; $j <= strlen($newURN); $j++) {
$sum = $sum + ($newURN[$j-1] * $j);
}
$lastNumber = $newURN[strlen($newURN)-1];
$quot = $sum / $lastNumber;
$quotRound = floor($quot);
$quotString = (string)$quotRound;
return $quotString[strlen($quotString)-1];
}
}
?>