Open Journal Systems  3.3.0
PKPPublicationNativeXmlFilter.inc.php
1 <?php
2 
16 import('lib.pkp.plugins.importexport.native.filter.NativeExportFilter');
17 
23  function __construct($filterGroup) {
24  $this->setDisplayName('Native XML Publication export');
25  parent::__construct($filterGroup);
26  }
27 
28  //
29  // Implement template methods from PersistableFilter
30  //
34  function getClassName() {
35  return 'lib.pkp.plugins.importexport.native.filter.PKPPublicationNativeXmlFilter';
36  }
37 
38  //
39  // Implement template methods from Filter
40  //
46  function &process(&$entity) {
47  // Create the XML document
48  $doc = new DOMDocument('1.0');
49  $doc->preserveWhiteSpace = false;
50  $doc->formatOutput = true;
51  $deployment = $this->getDeployment();
52  $rootNode = $this->createEntityNode($doc, $entity);
53  $doc->appendChild($rootNode);
54  $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
55  $rootNode->setAttribute('xsi:schemaLocation', $deployment->getNamespace() . ' ' . $deployment->getSchemaFilename());
56 
57  return $doc;
58  }
59 
60  //
61  // Representation conversion functions
62  //
69  function createEntityNode($doc, $entity) {
70  $deployment = $this->getDeployment();
71  $context = $deployment->getContext();
72 
73  // Create the entity node
74  $entityNode = $doc->createElementNS($deployment->getNamespace(), 'publication');
75 
76  $this->addIdentifiers($doc, $entityNode, $entity);
77 
78  $entityNode->setAttribute('locale', $entity->getData('locale'));
79  $entityNode->setAttribute('version', $entity->getData('version') ?: 1);
80  $entityNode->setAttribute('status', $entity->getData('status'));
81  if ($primaryContactId = $entity->getData('primaryContactId')) $entityNode->setAttribute('primary_contact_id', $primaryContactId);
82  $entityNode->setAttribute('url_path', $entity->getData('urlPath'));
83 
84  $isPublished = $entity->getData('status') === STATUS_PUBLISHED;
85  $isPublished ? $entityNode->setAttribute('seq', (int) $entity->getData('seq')) : $entityNode->setAttribute('seq', '0');
86 
87  $entityLanguages = $entity->getData('languages');
88  if ($entityLanguages) {
89  $entityNode->setAttribute('language', $entityLanguages);
90  }
91 
92  if ($datePublished = $entity->getData('datePublished')) {
93  $entityNode->setAttribute('date_published', strftime('%Y-%m-%d', strtotime($datePublished)));
94  }
95 
96  $this->addMetadata($doc, $entityNode, $entity);
97  $this->addAuthors($doc, $entityNode, $entity);
98  $this->addRepresentations($doc, $entityNode, $entity);
99 
100  return $entityNode;
101  }
102 
109  function addIdentifiers($doc, $entityNode, $entity) {
110  $deployment = $this->getDeployment();
111 
112  // Add internal ID
113  $entityNode->appendChild($node = $doc->createElementNS($deployment->getNamespace(), 'id', $entity->getId()));
114  $node->setAttribute('type', 'internal');
115  $node->setAttribute('advice', 'ignore');
116 
117  // Add public ID
118  if ($pubId = $entity->getStoredPubId('publisher-id')) {
119  $entityNode->appendChild($node = $doc->createElementNS($deployment->getNamespace(), 'id', htmlspecialchars($pubId, ENT_COMPAT, 'UTF-8')));
120  $node->setAttribute('type', 'public');
121  $node->setAttribute('advice', 'update');
122  }
123 
124  // Add pub IDs by plugin
125  $pubIdPlugins = PluginRegistry::loadCategory('pubIds', true, $deployment->getContext()->getId());
126  foreach ($pubIdPlugins as $pubIdPlugin) {
127  $this->addPubIdentifier($doc, $entityNode, $entity, $pubIdPlugin);
128  }
129  }
130 
139  function addPubIdentifier($doc, $entityNode, $entity, $pubIdPlugin) {
140  $pubId = $entity->getStoredPubId($pubIdPlugin->getPubIdType());
141  if ($pubId) {
142  $deployment = $this->getDeployment();
143  $entityNode->appendChild($node = $doc->createElementNS($deployment->getNamespace(), 'id', htmlspecialchars($pubId, ENT_COMPAT, 'UTF-8')));
144  $node->setAttribute('type', $pubIdPlugin->getPubIdType());
145  $node->setAttribute('advice', 'update');
146  return $node;
147  }
148  return null;
149  }
150 
157  function addMetadata($doc, $entityNode, $entity) {
158  $deployment = $this->getDeployment();
159  $this->createLocalizedNodes($doc, $entityNode, 'title', $entity->getData('title'));
160  $this->createLocalizedNodes($doc, $entityNode, 'prefix', $entity->getData('prefix'));
161  $this->createLocalizedNodes($doc, $entityNode, 'subtitle', $entity->getData('subtitle'));
162  $this->createLocalizedNodes($doc, $entityNode, 'abstract', $entity->getData('abstract'));
163  $this->createLocalizedNodes($doc, $entityNode, 'coverage', $entity->getData('coverage'));
164  $this->createLocalizedNodes($doc, $entityNode, 'type', $entity->getData('type'));
165  $this->createLocalizedNodes($doc, $entityNode, 'source', $entity->getData('source'));
166  $this->createLocalizedNodes($doc, $entityNode, 'rights', $entity->getData('rights'));
167 
168  if ($entity->getData('licenseUrl')) {
169  $entityNode->appendChild($node = $doc->createElementNS($deployment->getNamespace(), 'licenseUrl', htmlspecialchars($entity->getData('licenseUrl'))));
170  }
171 
172  $this->createLocalizedNodes($doc, $entityNode, 'copyrightHolder', $entity->getData('copyrightHolder'));
173 
174  if ($entity->getData('copyrightYear')) {
175  $entityNode->appendChild($node = $doc->createElementNS($deployment->getNamespace(), 'copyrightYear', intval($entity->getData('copyrightYear'))));
176  }
177 
178  // add controlled vocabularies
179  // get the supported locale keys
180  $supportedLocales = array_keys(AppLocale::getSupportedFormLocales());
181  $controlledVocabulariesMapping = $this->_getControlledVocabulariesMappings();
182  foreach ($controlledVocabulariesMapping as $controlledVocabulariesNodeName => $mappings) {
183  $dao = DAORegistry::getDAO($mappings[0]);
184  $getFunction = $mappings[1];
185  $controlledVocabularyNodeName = $mappings[2];
186  $controlledVocabulary = $dao->$getFunction($entity->getId(), $supportedLocales);
187  $this->addControlledVocabulary($doc, $entityNode, $controlledVocabulariesNodeName, $controlledVocabularyNodeName, $controlledVocabulary);
188  }
189  }
190 
199  function addControlledVocabulary($doc, $entityNode, $controlledVocabulariesNodeName, $controlledVocabularyNodeName, $controlledVocabulary) {
200  $deployment = $this->getDeployment();
201  $locales = array_keys($controlledVocabulary);
202  foreach ($locales as $locale) {
203  if (!empty($controlledVocabulary[$locale])) {
204  $controlledVocabulariesNode = $doc->createElementNS($deployment->getNamespace(), $controlledVocabulariesNodeName);
205  $controlledVocabulariesNode->setAttribute('locale', $locale);
206  foreach ($controlledVocabulary[$locale] as $controlledVocabularyItem) {
207  $controlledVocabulariesNode->appendChild($node = $doc->createElementNS($deployment->getNamespace(), $controlledVocabularyNodeName, htmlspecialchars($controlledVocabularyItem, ENT_COMPAT, 'UTF-8')));
208  }
209 
210  $entityNode->appendChild($controlledVocabulariesNode);
211  }
212  }
213  }
214 
221  function addAuthors($doc, $entityNode, $entity) {
222  $filterDao = DAORegistry::getDAO('FilterDAO');
223  $nativeExportFilters = $filterDao->getObjectsByGroup('author=>native-xml');
224  assert(count($nativeExportFilters)==1); // Assert only a single serialization filter
225  $exportFilter = array_shift($nativeExportFilters);
226  $exportFilter->setDeployment($this->getDeployment());
227 
228  $authors = $entity->getData('authors');
229  $authorsDoc = $exportFilter->execute($authors);
230  if ($authorsDoc->documentElement instanceof DOMElement) {
231  $clone = $doc->importNode($authorsDoc->documentElement, true);
232  $entityNode->appendChild($clone);
233  }
234  }
235 
242  function addRepresentations($doc, $entityNode, $entity) {
243  $filterDao = DAORegistry::getDAO('FilterDAO');
244  $nativeExportFilters = $filterDao->getObjectsByGroup($this->getRepresentationExportFilterGroupName());
245  assert(count($nativeExportFilters)==1); // Assert only a single serialization filter
246  $exportFilter = array_shift($nativeExportFilters);
247  $exportFilter->setDeployment($this->getDeployment());
248 
249  $representationDao = Application::getRepresentationDAO();
250  $representations = $representationDao->getByPublicationId($entity->getId());
251  while ($representation = $representations->next()) {
252  $representationDoc = $exportFilter->execute($representation);
253  $clone = $doc->importNode($representationDoc->documentElement, true);
254  $entityNode->appendChild($clone);
255  }
256  }
257 
263  return array(
264  'keywords' => array('SubmissionKeywordDAO', 'getKeywords', 'keyword'),
265  'agencies' => array('SubmissionAgencyDAO', 'getAgencies', 'agency'),
266  'disciplines' => array('SubmissionDisciplineDAO', 'getDisciplines', 'discipline'),
267  'subjects' => array('SubmissionSubjectDAO', 'getSubjects', 'subject'),
268  );
269  }
270 
271  //
272  // Abstract methods to be implemented by subclasses
273  //
279  function getFiles($representation) {
280  assert(false); // To be overridden by subclasses
281  }
282 }
283 
284 
PKPPublicationNativeXmlFilter\addRepresentations
addRepresentations($doc, $entityNode, $entity)
Definition: PKPPublicationNativeXmlFilter.inc.php:242
Application\getRepresentationDAO
static getRepresentationDAO()
Definition: Application.inc.php:162
PKPPublicationNativeXmlFilter\addAuthors
addAuthors($doc, $entityNode, $entity)
Definition: PKPPublicationNativeXmlFilter.inc.php:221
PKPPublicationNativeXmlFilter\addMetadata
addMetadata($doc, $entityNode, $entity)
Definition: PKPPublicationNativeXmlFilter.inc.php:157
PKPPublicationNativeXmlFilter\addPubIdentifier
addPubIdentifier($doc, $entityNode, $entity, $pubIdPlugin)
Definition: PKPPublicationNativeXmlFilter.inc.php:139
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
PKPPublicationNativeXmlFilter\process
& process(&$entity)
Definition: PKPPublicationNativeXmlFilter.inc.php:46
PKPPublicationNativeXmlFilter\createEntityNode
createEntityNode($doc, $entity)
Definition: PKPPublicationNativeXmlFilter.inc.php:69
PKPPublicationNativeXmlFilter\getFiles
getFiles($representation)
Definition: PKPPublicationNativeXmlFilter.inc.php:279
NativeImportExportFilter\getDeployment
getDeployment()
Definition: NativeImportExportFilter.inc.php:49
NativeExportFilter\createLocalizedNodes
createLocalizedNodes($doc, $parentNode, $name, $values)
Definition: NativeExportFilter.inc.php:87
PluginRegistry\loadCategory
static loadCategory($category, $enabledOnly=false, $mainContextId=null)
Definition: PluginRegistry.inc.php:103
PKPPublicationNativeXmlFilter
Base class that converts a PKPPublication to a Native XML document.
Definition: PKPPublicationNativeXmlFilter.inc.php:18
NativeExportFilter
Base class that converts a DataObject to a Native XML document.
Definition: NativeExportFilter.inc.php:18
PKPPublicationNativeXmlFilter\getClassName
getClassName()
Definition: PKPPublicationNativeXmlFilter.inc.php:34
PKPPublicationNativeXmlFilter\addIdentifiers
addIdentifiers($doc, $entityNode, $entity)
Definition: PKPPublicationNativeXmlFilter.inc.php:109
PKPPublicationNativeXmlFilter\__construct
__construct($filterGroup)
Definition: PKPPublicationNativeXmlFilter.inc.php:23
PKPPublicationNativeXmlFilter\addControlledVocabulary
addControlledVocabulary($doc, $entityNode, $controlledVocabulariesNodeName, $controlledVocabularyNodeName, $controlledVocabulary)
Definition: PKPPublicationNativeXmlFilter.inc.php:199
PKPPublicationNativeXmlFilter\_getControlledVocabulariesMappings
_getControlledVocabulariesMappings()
Definition: PKPPublicationNativeXmlFilter.inc.php:262
Filter\setDisplayName
setDisplayName($displayName)
Definition: Filter.inc.php:140
AppLocale\getSupportedFormLocales
static getSupportedFormLocales()
Definition: env1/MockAppLocale.inc.php:124