You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
4.1 KiB
162 lines
4.1 KiB
13 years ago
|
<?php
|
||
|
|
||
|
/**
|
||
|
* @file
|
||
12 years ago
|
*
|
||
13 years ago
|
* Implements a simple class for working with Dublin Core data and exporting it
|
||
|
* back to XML. Inspiration and design shamelessly stolen from the pyfedora
|
||
|
* project at http://pypi.python.org/pypi/pyfedora/0.1.0
|
||
|
*/
|
||
|
|
||
|
/**
|
||
|
* Dublin Core Class
|
||
|
*/
|
||
12 years ago
|
class DublinCore {
|
||
13 years ago
|
|
||
|
public $dc = array(
|
||
|
'dc:title' => array(),
|
||
|
'dc:creator' => array(),
|
||
|
'dc:subject' => array(),
|
||
|
'dc:description' => array(),
|
||
|
'dc:publisher' => array(),
|
||
|
'dc:contributor' => array(),
|
||
|
'dc:date' => array(),
|
||
|
'dc:type' => array(),
|
||
|
'dc:format' => array(),
|
||
|
'dc:identifier' => array(),
|
||
|
'dc:source' => array(),
|
||
|
'dc:language' => array(),
|
||
|
'dc:relation' => array(),
|
||
|
'dc:coverage' => array(),
|
||
|
'dc:rights' => array(),
|
||
|
);
|
||
|
public $owner;
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Constructs a DublinCore object from a Fedora_Item object and populates the
|
||
|
* $dc array.
|
||
12 years ago
|
*
|
||
|
* @param string $dc_xml
|
||
13 years ago
|
*/
|
||
12 years ago
|
function DublinCore($dc_xml = NULL) {
|
||
12 years ago
|
if (!empty($dc_xml)) {
|
||
13 years ago
|
$this->dc = self::import_from_xml_string($dc_xml);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Add Elements
|
||
|
* @param <type> $element_name
|
||
|
* @param <type> $value
|
||
|
*/
|
||
|
function add_element($element_name, $value) {
|
||
|
if (is_string($value) && is_array($this->dc[$element_name])) {
|
||
|
$this->dc[$element_name][] = $value;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Replace the given DC element with the values in $values
|
||
|
* @param string $elemnt_name
|
||
|
* @param array $values
|
||
|
*/
|
||
|
function set_element($element_name, $values) {
|
||
|
if (is_array($values)) {
|
||
|
$this->dc[$element_name] = $values;
|
||
|
}
|
||
|
elseif (is_string($values)) {
|
||
|
$this->dc[$element_name] = array($values);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Serialize this object to XML and return it.
|
||
|
* @param type $with_preamble
|
||
12 years ago
|
* @return type
|
||
13 years ago
|
*/
|
||
|
function as_xml($with_preamble = FALSE) {
|
||
|
$dc_xml = new DomDocument();
|
||
|
$oai_dc = $dc_xml->createElementNS('http://www.openarchives.org/OAI/2.0/oai_dc/', 'oai_dc:dc');
|
||
|
$oai_dc->setAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
|
||
|
foreach ($this->dc as $dc_element => $values) {
|
||
|
if (is_array($values) && !empty($values)) {
|
||
|
foreach ($values as $value) {
|
||
|
$new_item = $dc_xml->createElement($dc_element, $value);
|
||
|
$oai_dc->appendChild($new_item);
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
$new_item = $dc_xml->createElement($dc_element);
|
||
|
$oai_dc->appendChild($new_item);
|
||
|
}
|
||
|
}
|
||
|
$dc_xml->appendChild($oai_dc);
|
||
|
return $dc_xml->saveXML();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Create dc from dict ( does nothing )
|
||
|
*/
|
||
|
static function create_dc_from_dict() {
|
||
12 years ago
|
|
||
13 years ago
|
}
|
||
|
|
||
13 years ago
|
function as_formatted_array() {
|
||
|
$dc_array = array();
|
||
13 years ago
|
foreach ($this as $element) {
|
||
13 years ago
|
if (!empty($element)) {
|
||
13 years ago
|
foreach ($element as $field => $values) {
|
||
13 years ago
|
// split value if the result value is an array
|
||
13 years ago
|
if (is_array($values)) {
|
||
|
$value = '';
|
||
|
$i = 0;
|
||
12 years ago
|
foreach ($values as $piece) {
|
||
|
if (!empty($piece)) {
|
||
|
if ($i++) {
|
||
13 years ago
|
$value .= ", ";
|
||
|
}
|
||
|
$value .= $piece;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
$value = $values;
|
||
13 years ago
|
}
|
||
|
$dc_label = explode(':', $field);
|
||
12 years ago
|
$element_label = drupal_ucfirst($dc_label[1]);
|
||
13 years ago
|
$dc_array[$field]['label'] = $element_label;
|
||
13 years ago
|
$dc_array[$field]['value'] = $value;
|
||
12 years ago
|
$dc_array[$field]['class'] = drupal_strtolower(preg_replace('/[^A-Za-z0-9]/', '-', $field));
|
||
13 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
return $dc_array;
|
||
|
}
|
||
12 years ago
|
|
||
13 years ago
|
|
||
|
/**
|
||
|
* Creates a new instance of the class by parsing dc_xml
|
||
12 years ago
|
*
|
||
13 years ago
|
* @param string $dc_xml
|
||
12 years ago
|
* @return DublinCore
|
||
13 years ago
|
*/
|
||
|
static function import_from_xml_string($dc_xml) {
|
||
|
$dc_doc = new DomDocument();
|
||
|
if ($dc_doc->loadXML($dc_xml)) {
|
||
|
$oai_dc = $dc_doc->getElementsByTagNameNS('http://purl.org/dc/elements/1.1/', '*');
|
||
12 years ago
|
$new_dc = new DublinCore();
|
||
12 years ago
|
|
||
13 years ago
|
foreach ($oai_dc as $child) {
|
||
12 years ago
|
if (isset($new_dc->dc[$child->nodeName])) {
|
||
12 years ago
|
array_push($new_dc->dc[$child->nodeName], $child->nodeValue);
|
||
|
}
|
||
13 years ago
|
}
|
||
|
return $new_dc;
|
||
|
}
|
||
|
else {
|
||
|
return NULL;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|