PageRenderTime 65ms CodeModel.GetById 20ms RepoModel.GetById 2ms app.codeStats 0ms

/plugins/metadata/nlm30/filter/Nlm30CitationSchemaCitationAdapter.inc.php

https://github.com/mbehiels/pkp-lib
PHP | 164 lines | 81 code | 20 blank | 63 comment | 11 complexity | b39f362647ab181febfb5fc633a63a1f MD5 | raw file
  1. <?php
  2. /**
  3. * @file plugins/metadata/nlm30/filter/Nlm30CitationSchemaCitationAdapter.inc.php
  4. *
  5. * Copyright (c) 2000-2011 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class Nlm30CitationSchemaCitationAdapter
  9. * @ingroup plugins_metadata_nlm30_filter
  10. * @see Citation
  11. * @see Nlm30CitationSchema
  12. *
  13. * @brief Class that injects/extracts NLM citation schema compliant
  14. * meta-data into/from a Citation object.
  15. */
  16. import('lib.pkp.classes.metadata.MetadataDataObjectAdapter');
  17. import('lib.pkp.plugins.metadata.nlm30.schema.Nlm30NameSchema');
  18. class Nlm30CitationSchemaCitationAdapter extends MetadataDataObjectAdapter {
  19. /**
  20. * Constructor
  21. * @param $filterGroup FilterGroup
  22. */
  23. function Nlm30CitationSchemaCitationAdapter(&$filterGroup) {
  24. parent::MetadataDataObjectAdapter($filterGroup);
  25. }
  26. //
  27. // Implement template methods from PersistableFilter
  28. //
  29. /**
  30. * @see PersistableFilter::getClassName()
  31. */
  32. function getClassName() {
  33. return 'lib.pkp.plugins.metadata.nlm30.filter.Nlm30CitationSchemaCitationAdapter';
  34. }
  35. //
  36. // Implement template methods from MetadataDataObjectAdapter
  37. //
  38. /**
  39. * @see MetadataDataObjectAdapter::injectMetadataIntoDataObject()
  40. * @param $metadataDescription MetadataDescription
  41. * @param $dataObject Citation
  42. * @return DataObject
  43. */
  44. function &injectMetadataIntoDataObject(&$metadataDescription, &$dataObject) {
  45. assert(is_a($dataObject, 'Citation'));
  46. // Add new meta-data statements to the citation. Add the schema
  47. // name space to each property name so that it becomes unique
  48. // across schemas.
  49. $metadataSchemaNamespace = $this->getMetadataNamespace();
  50. $nullVar = null;
  51. foreach($metadataDescription->getPropertyNames() as $propertyName) {
  52. $dataObjectKey = $metadataSchemaNamespace.':'.$propertyName;
  53. if ($metadataDescription->hasStatement($propertyName)) {
  54. // Directly retrieve the internal data so that we don't
  55. // have to care about cardinality and translation.
  56. $value =& $metadataDescription->getData($propertyName);
  57. if (in_array($propertyName, array('person-group[@person-group-type="author"]', 'person-group[@person-group-type="editor"]'))) {
  58. assert(is_array($value));
  59. // Dereference the value to make sure that we don't destroy
  60. // the original MetadataDescription.
  61. $tmpValue = $value;
  62. unset($value);
  63. $value =& $tmpValue;
  64. // Convert MetadataDescription objects to simple key/value arrays.
  65. foreach($value as $key => $name) {
  66. if(is_a($name, 'MetadataDescription')) {
  67. // A name can either be a full name description...
  68. $value[$key] =& $name->getAllData();
  69. } else {
  70. // ...or an 'et-al' string.
  71. assert($name == PERSON_STRING_FILTER_ETAL);
  72. // No need to change the value encoding.
  73. }
  74. }
  75. }
  76. $dataObject->setData($dataObjectKey, $value);
  77. unset($value);
  78. }
  79. }
  80. return $dataObject;
  81. }
  82. /**
  83. * @see MetadataDataObjectAdapter::extractMetadataFromDataObject()
  84. * @param $dataObject Citation
  85. * @return MetadataDescription
  86. */
  87. function &extractMetadataFromDataObject(&$dataObject) {
  88. $metadataDescription =& $this->instantiateMetadataDescription();
  89. // Establish the association between the meta-data description
  90. // and the citation object.
  91. $metadataDescription->setAssocId($dataObject->getId());
  92. // Identify the length of the name space prefix
  93. $namespacePrefixLength = strlen($this->getMetadataNamespace())+1;
  94. // Get all meta-data field names
  95. $fieldNames = array_merge($this->getDataObjectMetadataFieldNames(false),
  96. $this->getDataObjectMetadataFieldNames(true));
  97. // Retrieve the statements from the data object
  98. $statements = array();
  99. foreach($fieldNames as $fieldName) {
  100. if ($dataObject->hasData($fieldName)) {
  101. // Remove the name space prefix
  102. $propertyName = substr($fieldName, $namespacePrefixLength);
  103. if (in_array($propertyName, array('person-group[@person-group-type="author"]', 'person-group[@person-group-type="editor"]'))) {
  104. // Retrieve the names array (must not be by-ref
  105. // to protect the original citation object!)
  106. $names = $dataObject->getData($fieldName);
  107. // Convert key/value arrays to MetadataDescription objects.
  108. foreach($names as $key => $name) {
  109. if (is_array($name)) {
  110. // Construct a meta-data description from
  111. // this name array.
  112. switch($propertyName) {
  113. case 'person-group[@person-group-type="author"]':
  114. $assocType = ASSOC_TYPE_AUTHOR;
  115. break;
  116. case 'person-group[@person-group-type="editor"]':
  117. $assocType = ASSOC_TYPE_EDITOR;
  118. break;
  119. }
  120. $nameDescription = new MetadataDescription('lib.pkp.plugins.metadata.nlm30.schema.Nlm30NameSchema', $assocType);
  121. $nameDescription->setStatements($name);
  122. $names[$key] =& $nameDescription;
  123. unset($nameDescription);
  124. } else {
  125. // The only non-structured data allowed here
  126. // is the et-al string.
  127. import('lib.pkp.plugins.metadata.nlm30.filter.Nlm30PersonStringFilter');
  128. assert($name == PERSON_STRING_FILTER_ETAL);
  129. }
  130. }
  131. $statements[$propertyName] =& $names;
  132. unset($names);
  133. } else {
  134. $statements[$propertyName] =& $dataObject->getData($fieldName);
  135. }
  136. }
  137. }
  138. // Set the statements in the meta-data description
  139. $success = $metadataDescription->setStatements($statements);
  140. assert($success);
  141. return $metadataDescription;
  142. }
  143. }
  144. ?>