PageRenderTime 81ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/modsxmlsenayan.inc.php

https://gitlab.com/mucill/sman7
PHP | 224 lines | 147 code | 24 blank | 53 comment | 38 complexity | c415cdd29214d77203cad636b661a896 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * MODS XML to SENAYAN converter
  5. *
  6. * Copyright (C) 2010 Hendro Wicaksono (hendrowicaksono@yahoo.com)
  7. * Copyright (C) 2011,2012 Arie Nugraha (dicarve@gmail.com)
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. */
  24. define('MODS_XML_PARSE_ERROR', 199);
  25. /**
  26. * MODS XML parser for SENAYAN 3
  27. * @param string $str_modsxml: can be string, file or uri
  28. * @return array
  29. **/
  30. function modsXMLsenayan($str_modsxml, $str_xml_type = 'string')
  31. {
  32. // initiate records array
  33. $_records = array();
  34. libxml_use_internal_errors(true);
  35. // load XML
  36. if ($str_xml_type == 'file') {
  37. // load from file
  38. if (file_exists($str_modsxml)) {
  39. $xml = @simplexml_load_file($str_modsxml);
  40. } else {
  41. return 'File '.$str_modsxml.' not found! Please supply full path to MODS XML file';
  42. }
  43. } else {
  44. // load from string
  45. try {
  46. // check if type is URI
  47. if ($str_xml_type == 'uri') {
  48. $xml = @new SimpleXMLElement($str_modsxml, LIBXML_NSCLEAN, true);
  49. } else {
  50. $xml = @new SimpleXMLElement($str_modsxml, LIBXML_NSCLEAN);
  51. }
  52. } catch (Exception $xmlerr) {
  53. return MODS_XML_PARSE_ERROR;
  54. // die($xmlerr->getMessage());
  55. }
  56. }
  57. // get result information from SLiMS Namespaced node
  58. $_slims = $xml->children('http://senayan.diknas.go.id');
  59. if (!$_slims) { $_slims = $xml->children('http://slims.web.id'); }
  60. if ($_slims) {
  61. if (isset($_slims->resultInfo)) {
  62. $_records['result_num'] = (integer)$_slims->resultInfo->modsResultNum;
  63. $_records['result_page'] = (integer)$_slims->resultInfo->modsResultPage;
  64. $_records['result_showed'] = (integer)$_slims->resultInfo->modsResultShowed;
  65. } else {
  66. $_records['result_num'] = (integer)$_slims->modsResultNum;
  67. $_records['result_page'] = (integer)$_slims->modsResultPage;
  68. $_records['result_showed'] = (integer)$_slims->modsResultShowed;
  69. }
  70. } else {
  71. $_records['result_num'] = isset($xml->modsResultNum)?$xml->modsResultNum:'';
  72. $_records['result_page'] = isset($xml->modsResultPage)?$xml->modsResultPage:'';
  73. $_records['result_showed'] = isset($xml->modsResultShowed)?$xml->modsResultShowed:'';
  74. }
  75. $record_num = 0;
  76. // start iterate records
  77. foreach($xml->mods as $record) {
  78. $data = array();
  79. $data['id'] = (string)$record['ID'];
  80. if (!$data['id']) {
  81. $data['id'] = (string)$record['id'];
  82. }
  83. # authors
  84. $data['authors'] = array();
  85. # title
  86. $data['title'] = (string)$record->titleInfo->title;
  87. if (isset($record->titleInfo->subTitle)) {
  88. $data['title'] .= (string)$record->titleInfo->subTitle;
  89. }
  90. # name/author (repeatable)
  91. if (isset($record->name) AND $record->name) {
  92. foreach ($record->name as $value) {
  93. $_author_type = $value['type'];
  94. if ($value->role->roleTerm == 'Primary Author') {
  95. $_level = 1;
  96. } else {
  97. $_level = 2;
  98. }
  99. $data['authors'][] = array('name' => (string)$value->namePart, 'authority_list' => (string)$value['authority'], 'level' => $_level, 'author_type' => (string)$_author_type);
  100. }
  101. }
  102. # mods->typeOfResource
  103. $data['manuscript'] = (boolean)$record->typeOfResource['manuscript'] == 'yes';
  104. $data['collection'] = (boolean)$record->typeOfResource['collection'] == 'yes';
  105. $data['resource_type'] = (string)$record->typeOfResource;
  106. # mods->genre
  107. $data['genre_authority'] = (string)$record->genre['authority'];
  108. $data['genre'] = (string)$record->genre;
  109. # mods->originInfo
  110. $data['publish_place'] = isset($record->originInfo->place->placeTerm)?(string)$record->originInfo->place->placeTerm:'';
  111. $data['publisher'] = (string)$record->originInfo->publisher;
  112. $data['publish_year'] = (string)$record->originInfo->dateIssued;
  113. $data['issuance'] = (string)$record->originInfo->issuance;
  114. $data['edition'] = (string)$record->originInfo->edition;
  115. # mods->language
  116. if (isset($record->language->languageTerm)) {
  117. foreach ($record->language->languageTerm as $_langterm) {
  118. if ($_langterm['type'] == 'code') {
  119. $data['language']['code'] = (string)$_langterm;
  120. } else {
  121. $data['language']['name'] = (string)$_langterm;
  122. }
  123. }
  124. }
  125. # mods->physicalDescription
  126. $data['gmd'] = (string)$record->physicalDescription->form;
  127. $data['collation'] = (string)$record->physicalDescription->extent;
  128. # mods->relatedItem
  129. if ($record->relatedItem['type'] == 'series') {
  130. $data['series_title'] = (string)$record->relatedItem->titleInfo->title;
  131. }
  132. # mods->note
  133. $data['notes'] = (string)$record->note;
  134. # mods->subject
  135. foreach ($record->subject as $_subj) {
  136. $_authority = (string)$_subj['authority'];
  137. if (isset($_subj->topic)) {
  138. $_term_type = 'topical';
  139. $_term = (string)$_subj->topic;
  140. }
  141. if (isset($_subj->geographic)) {
  142. $_term_type = 'geographic';
  143. $_term = (string)$_subj->geographic;
  144. }
  145. if (isset($_subj->name)) {
  146. $_term_type = 'name';
  147. $_term = (string)$_subj->name;
  148. }
  149. if (isset($_subj->temporal)) {
  150. $_term_type = 'temporal';
  151. $_term = (string)$_subj->temporal;
  152. }
  153. if (isset($_subj->genre)) {
  154. $_term_type = 'genre';
  155. $_term = (string)$_subj->genre;
  156. }
  157. if (isset($_subj->occupation)) {
  158. $_term_type = 'occupation';
  159. $_term = (string)$_subj->occupation;
  160. }
  161. $data['subjects'][] = array('term' => $_term, 'term_type' => $_term_type, 'authority' => $_authority);
  162. }
  163. # mods->classification
  164. $data['classification'] = (string)$record->classification;
  165. # mods->identifier
  166. $data['isbn_issn'] = '';
  167. if ($record->identifier['type'] == 'isbn') {
  168. $data['isbn_issn'] = (string)$record->identifier;
  169. }
  170. # mods->location
  171. $data['location'] = (string)$record->location->physicalLocation;
  172. $data['call_number'] = (string)$record->location->shelfLocator;
  173. # mods->recordInfo
  174. if (isset($record->recordInfo)) {
  175. $data['id'] = (string)$record->recordInfo->recordIdentifier;
  176. $data['create_date'] = (string)$record->recordInfo->recordCreationDate;
  177. $data['modified_date'] = (string)$record->recordInfo->recordChangeDate;
  178. $data['origin'] = (string)$record->recordInfo->recordOrigin;
  179. }
  180. # images
  181. if (isset($_slims->image)) {
  182. $data['image'] = (string)$_slims->image;
  183. }
  184. # digital files
  185. if (isset($_slims->digitals)) {
  186. foreach ($_slims->digitals->digital_item as $_dig_item) {
  187. // get attributes
  188. $_attr = (array)$_dig_item->attributes();
  189. $data['digitals'][] = array('id' => $_attr['@attributes']['id'],
  190. 'title' => (string)$_dig_item,
  191. 'path' => $_attr['@attributes']['path'],
  192. 'mimetype' => $_attr['@attributes']['mimetype'],
  193. 'url' => $_attr['@attributes']['url']);
  194. }
  195. }
  196. $_records['records'][] = $data;
  197. $record_num++;
  198. }
  199. return $_records;
  200. }