Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 140 lines (124 sloc) 3.622 kb
e436b56 Alexander O'Neill Migration to GitHub
alxp authored
1 <?php
64fac18 Ben Woodhead Added doc tags to the api
bwoodhead authored
2
d705d89 Ben Woodhead Removed CVS id tags
bwoodhead authored
3
e436b56 Alexander O'Neill Migration to GitHub
alxp authored
4
64fac18 Ben Woodhead Added doc tags to the api
bwoodhead authored
5 /**
6 * @file
e436b56 Alexander O'Neill Migration to GitHub
alxp authored
7 * Implements a simple class for working with Dublin Core data and exporting it
8 * back to XML. Inspiration and design shamelessly stolen from the pyfedora
9 * project at http://pypi.python.org/pypi/pyfedora/0.1.0
10 */
11
64fac18 Ben Woodhead Added doc tags to the api
bwoodhead authored
12 /**
13 * Dublin Core Class
14 */
e436b56 Alexander O'Neill Migration to GitHub
alxp authored
15 class Dublin_Core {
64fac18 Ben Woodhead Added doc tags to the api
bwoodhead authored
16
e436b56 Alexander O'Neill Migration to GitHub
alxp authored
17 public $dc = array(
18 'dc:title' => array(),
19 'dc:creator' => array(),
20 'dc:subject' => array(),
21 'dc:description' => array(),
22 'dc:publisher' => array(),
23 'dc:contributor' => array(),
24 'dc:date' => array(),
25 'dc:type' => array(),
26 'dc:format' => array(),
27 'dc:identifier' => array(),
28 'dc:source' => array(),
29 'dc:language' => array(),
30 'dc:relation' => array(),
31 'dc:coverage' => array(),
32 'dc:rights' => array(),
33 );
34 public $owner;
35
36 /**
37 * Constructs a Dublin_Core object from a Fedora_Item object and populates
38 * the $dc array.
39 * @param <type> $item
40 */
41 function Dublin_Core($item = NULL) {
42 if (!empty($item)) {
43 $this->owner = $item;
44 $dc_xml = $item->get_datastream_dissemination('DC');
45 $this->dc = self::import_from_xml_string($dc_xml)->dc;
46 }
47 }
48
49 /**
64fac18 Ben Woodhead Added doc tags to the api
bwoodhead authored
50 * Add Elements
e436b56 Alexander O'Neill Migration to GitHub
alxp authored
51 * @param <type> $element_name
52 * @param <type> $value
53 */
64fac18 Ben Woodhead Added doc tags to the api
bwoodhead authored
54 function add_element($element_name, $value) {
e436b56 Alexander O'Neill Migration to GitHub
alxp authored
55 if (is_string($value) && is_array($this->dc[$element_name])) {
56 $this->dc[$element_name][] = $value;
57 }
58 }
59
64fac18 Ben Woodhead Added doc tags to the api
bwoodhead authored
60 /**
61 * Replace the given DC element with the values in $values
62 * @param string $elemnt_name
abfc844 Jonathan Green ISLANDORA-278 Initial pass at making the islandora code conform to the d...
jonathangreen authored
63 * @param array $values
64fac18 Ben Woodhead Added doc tags to the api
bwoodhead authored
64 */
e436b56 Alexander O'Neill Migration to GitHub
alxp authored
65 function set_element($element_name, $values) {
66 if (is_array($values)) {
67 $this->dc[$element_name] = $values;
64fac18 Ben Woodhead Added doc tags to the api
bwoodhead authored
68 }
e436b56 Alexander O'Neill Migration to GitHub
alxp authored
69 elseif (is_string($values)) {
70 $this->dc[$element_name] = array($values);
71 }
72 }
73
74 /**
75 * Serialize this object to XML and return it.
64fac18 Ben Woodhead Added doc tags to the api
bwoodhead authored
76 * @param type $with_preamble
77 * @return type
e436b56 Alexander O'Neill Migration to GitHub
alxp authored
78 */
64fac18 Ben Woodhead Added doc tags to the api
bwoodhead authored
79 function as_xml($with_preamble = FALSE) {
e436b56 Alexander O'Neill Migration to GitHub
alxp authored
80 $dc_xml = new DomDocument();
81 $oai_dc = $dc_xml->createElementNS('http://www.openarchives.org/OAI/2.0/oai_dc/', 'oai_dc:dc');
82 $oai_dc->setAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
83 foreach ($this->dc as $dc_element => $values) {
84 if (is_array($values) && !empty($values)) {
85 foreach ($values as $value) {
86 $new_item = $dc_xml->createElement($dc_element, $value);
87 $oai_dc->appendChild($new_item);
88 }
64fac18 Ben Woodhead Added doc tags to the api
bwoodhead authored
89 }
e436b56 Alexander O'Neill Migration to GitHub
alxp authored
90 else {
91 $new_item = $dc_xml->createElement($dc_element);
92 $oai_dc->appendChild($new_item);
93 }
94 }
95 $dc_xml->appendChild($oai_dc);
96 return $dc_xml->saveXML();
97 }
98
64fac18 Ben Woodhead Added doc tags to the api
bwoodhead authored
99 /**
100 * Create dc from dict ( does nothing )
101 */
e436b56 Alexander O'Neill Migration to GitHub
alxp authored
102 static function create_dc_from_dict() {
64fac18 Ben Woodhead Added doc tags to the api
bwoodhead authored
103
e436b56 Alexander O'Neill Migration to GitHub
alxp authored
104 }
105
64fac18 Ben Woodhead Added doc tags to the api
bwoodhead authored
106 /**
107 * Save ??
108 * @param type $alt_owner
109 */
e436b56 Alexander O'Neill Migration to GitHub
alxp authored
110 function save($alt_owner = NULL) {
111 $item_to_update = (!empty($alt_owner) ? $alt_owner : $this->owner);
112 // My Java roots showing, trying to do polymorphism in PHP.
113 if (!empty($item_to_update)) {
114 $item_to_update->modify_datastream_by_value($this->as_xml(), 'DC', 'Default Dublin Core Metadata', 'text/xml');
115 }
116 }
117
118 /**
119 * Creates a new instance of the class by parsing dc_xml
120 * @param string $dc_xml
121 * @return Dublin_Core
122 */
123 static function import_from_xml_string($dc_xml) {
124 $dc_doc = new DomDocument();
125 if ($dc_doc->loadXML($dc_xml)) {
126 $oai_dc = $dc_doc->getElementsByTagNameNS('http://purl.org/dc/elements/1.1/', '*');
127 $new_dc = new Dublin_Core();
128 foreach ($oai_dc as $child) {
129 array_push($new_dc->dc[$child->nodeName], $child->nodeValue);
130 }
131 return $new_dc;
64fac18 Ben Woodhead Added doc tags to the api
bwoodhead authored
132 }
e436b56 Alexander O'Neill Migration to GitHub
alxp authored
133 else {
134 return NULL;
135 }
136 }
137
138 }
139
Something went wrong with that request. Please try again.