forked from withanage/ojsLeibnizOpen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOAIMetadataFormat_DCWGL.inc.php
197 lines (160 loc) · 5.23 KB
/
OAIMetadataFormat_DCWGL.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
<?php
/**
* @file plugins/oaiMetadataFormats/dcwgl/OAIMetadataFormat_DCWGL.inc.php
*
* Distributed under the GNU GPL v3
*
* @class OAIMetadataFormat_WGL
* @ingroup oai_format
* @see OAI
*
* @brief OAI metadata format class -- WGL.
*/
import('lib.pkp.plugins.oaiMetadataFormats.dc.PKPOAIMetadataFormat_DC');
/**
* Class OAIMetadataFormat_WGL Creates a Leibniz-Open XML File
*/
class OAIMetadataFormat_DCWGL extends PKPOAIMetadataFormat_DC
{
/**
* @see lib/pkp/plugins/oaiMetadataFormats/dc/PKPOAIMetadataFormat_DC::toXml()
*
* @param $record
* @param $format
* @return string
*/
public function toXml($record, $format = null) {
$submission = $record->getData('article');
$submission = (!empty($submission)) ? $submission : $record->getData('monograph');
$publicationFormat = $record->getData('publicationFormat');
$doc = (!empty($publicationFormat)) ?$publicationFormat: $submission ;
$dom = DOMDocument::loadXML( parent::toXml($doc));
$dom->formatOutput = true;
$dom->encoding ='UTF-8';
$siteAgencies = $this->_getSiteAgencies($submission);
$submissionAgencies = $this->_getSubmissionAgencies();
$isLeibnizAgency = false;
$leibnizAgency = array();
if (isset($submissionAgencies) & !empty($submissionAgencies)) {
$leibnizAgencies = explode('|', $submissionAgencies);
foreach ($leibnizAgencies as $agency) {
$agency = explode(':', $agency);
foreach ($siteAgencies as $agenciesInSubmission) {
foreach ($agenciesInSubmission as $agencyInSubmission) {
if (trim($agency[0]) == trim($agencyInSubmission)) {
$isLeibnizAgency = true;
$leibnizAgency = $agency;
}
}
}
}
}
if ($isLeibnizAgency) {
$xpath = new DOMXPath($dom);
$wgl = $this->_createWGLElement($dom);
$wgl = $this->_renameDCElements($xpath, $dom, $wgl);
$wgl = $this->_setWGLType($submission, $dom, $wgl);
$contributorElement = $dom->createElement('wgl:wglcontributor', trim($leibnizAgency[0]));
$wgl->appendChild($contributorElement);
$subjectElement = $dom->createElement('wgl:wglsubject', trim($leibnizAgency[1]));
$wgl->appendChild($subjectElement);
$this->_deleteDCElements($xpath);
$dom->appendChild($wgl);
}
return $dom->saveXML($dom->documentElement);
}
/**
* @param $node DOMElement
* @param $doc DOMDocument
* @param $prefix string
* @return DOMElement
*/
public function renameNamespace($node, $doc, $prefix) {
$prefixedName = preg_replace('/.*:/', $prefix . ':', $node->nodeName);
$newElement = $doc->createElement($prefixedName);
foreach ($node->attributes as $value)
$newElement->setAttribute($value->nodeName, $value->value);
if (!$node->childNodes)
return $newElement;
foreach ($node->childNodes as $child) {
if ($child->nodeName == "#text")
$newElement->appendChild($doc->createTextNode($child->nodeValue));
else
$newElement->appendChild($this->renameNamespace($child, $doc, $prefix));
}
return $newElement;
}
/**
* @param $submission
* @return mixed
*/
protected function _getSiteAgencies($submission) {
$site = Application::getRequest()->getSite();
$submissionAgencyDao = DAORegistry::getDAO('SubmissionAgencyDAO');
$siteSupportedLocales = $site->getSupportedLocales();
$agencies = $submissionAgencyDao->getAgencies($submission->getId(), $siteSupportedLocales);
return $agencies;
}
/**
* @return mixed
*/
protected function _getSubmissionAgencies() {
$context = Request::getContext();
$contextId = $context ? $context->getId() : CONTEXT_ID_NONE;
$plugin = PluginRegistry::getPlugin('oaiMetadataFormats', 'OAIMetadataFormatPlugin_DCWGL');
$pluginSettings = $plugin->getSetting($contextId, 'wglSettings');
return $pluginSettings;
}
/**
* @param DOMXPath $xpath
* @param $dom
* @param $ne
* @return mixed
*/
protected function _renameDCElements(DOMXPath $xpath, $dom, $ne)
{
$dcElements = $xpath->query("//oai_dc:dc/*");
foreach ($dcElements as $node) {
$clone = $this->renameNamespace($node, $dom, 'wgl');
$ne->appendChild($clone);
}
return $ne;
}
/**
* @param $submission
* @param $dom
* @param $ne
*/
protected function _setWGLType($submission, $dom, $ne)
{
if (is_a($submission, 'PublishedArticle')) {
$extraElement = $dom->createElement('wgl:wgltype', 'Zeitschriftenartikel');
$ne->appendChild($extraElement);
} elseif (is_a($submission, 'publishedMonograph')) {
$extraElement = $dom->createElement('wgl:wgltype', 'Monographie');
$ne->appendChild($extraElement);
}
return $ne;
}
/**
* @param DOMXPath $xpath
*/
protected function _deleteDCElements(DOMXPath $xpath)
{
$oaiDCElement = $xpath->query("//oai_dc:dc")->item(0);
$getParent = $oaiDCElement->parentNode;
$getParent->removeChild($oaiDCElement);
}
/**
* @param $dom
* @return mixed
*/
protected function _createWGLElement($dom)
{
$ne = $dom->createElementNS('http://www.leibnizopen.de/fileadmin/default/documents/oai_wgl', 'oai_wgl:wgl');
$ne->setAttribute('xmlns:wgl', 'http://www.leibnizopen.de/fileadmin/default/documents/wgl_dc');
$ne->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$ne->setAttribute('xsi:schemaLocation', 'http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd');
return $ne;
}
}