Skip to content

Commit

Permalink
Merge branch 'ISLANDORA-227'
Browse files Browse the repository at this point in the history
  • Loading branch information
alxp committed Mar 8, 2011
2 parents 19750f1 + 7ee6cb7 commit ee4c5e2
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 18 deletions.
47 changes: 29 additions & 18 deletions ilives/book.inc
Expand Up @@ -58,8 +58,12 @@ class IslandoraBook {
'#type' => 'textarea',
'#title' => 'MODS Record to Import',
'#rows' => 20,
'#value' => (!empty($mods_save) ? $mods_save['mods']['mods_record'] : ''),

);

if (!empty($mods_save)) {
$form['mods']['mods_record']['#value'] = $mods_save['mods']['mods_record'];
}
return $form;
}

Expand Down Expand Up @@ -101,7 +105,7 @@ class IslandoraBook {
return TRUE;
}

public function handleIngestForm($form_values) {
public function handleIngestForm($form_values, &$form_state) {
/*
* process the metadata form
* Create fedora object
Expand All @@ -113,40 +117,47 @@ class IslandoraBook {
return;
}


$mods_simple = simplexml_load_string($form_values['mods']['mods_record']);
$mods_list_doc = new DomDocument();
$mods_list_doc->loadXML($form_values['mods']['mods_record']);
$mods_item_doc = new DomDocument();
$mods_item = $mods_list_doc->getElementsByTagNameNS('http://www.loc.gov/mods/v3', 'mods')->item(0);
$new_mods_item = $mods_item_doc->importNode($mods_item, TRUE);
$mods_item_doc->appendChild($new_mods_item);

$title_info = $mods_item_doc->getElementsByTagNameNS('http://www.loc.gov/mods/v3', 'titleInfo')->item(0);
$title = '';
foreach ($mods_simple->children('http://www.loc.gov/mods/v3')->mods[0]->titleInfo[0]->children() as $child) {
if ($child->getName() == 'subTitle') {
// We don't care about subtitles for creating the item label.
continue;
}
$title .= $child;
foreach(array('nonSort', 'title') as $title_field) {
$title .= $title_info->getElementsByTagNameNS('http://www.loc.gov/mods/v3', $title_field)->item(0)->nodeValue;
}


$mods_text = $mods_item_doc->saveXML();
global $user;
$mimetype = new MimeClass();

$new_item = Fedora_Item::ingest_new_item(!empty($form_values['custom_pid']) ? $form_values['custom_pid'] : $form_values['pid'], 'A', $title,
$user->name);

$new_item->add_datastream_from_string($form_values['mods']['mods_record'], 'MODS',
$new_item->add_datastream_from_string($mods_text, 'MODS',
'MODS Metadata', 'text/xml', 'X');

$dc = transform_mods_to_dc($form_values['mods']['mods_record']);
$dc = transform_mods_to_dc($mods_text);
if ($dc) {
// Add the PID to a dc:identifier field.
$dc_doc = simplexml_load_string($dc);
$dc_item = $dc_doc->xpath('/srw_dc:dcCollection/srw_dc:dc[1]');
$dc_doc = simplexml_load_string($dc);
$dc_doc->registerXPathNamespace('oai_dc', 'http://www.openarchives.org/OAI/2.0/oai_dc/');
$dc_item = $dc_doc->xpath('//oai_dc:dc');
foreach($dc_item as $node) {
$node->addChild('identifier', $new_item->pid, 'http://purl.org/dc/elements/1.1/');
$node->addChild('dc:identifier', $new_item->pid, 'http://purl.org/dc/elements/1.1/');
}
$new_item->modify_datastream_by_value($dc_doc->saveXML(), 'DC', 'Dublin Core XML Metadata', 'text/xml');
}
$new_item->add_relationship('hasModel', $form_values['content_model_pid'], FEDORA_MODEL_URI);
$new_item->add_relationship(!empty($form_values['relationship']) ? $form_values['relationship'] : 'isMemberOfCollection', $form_values['collection_pid']);
drupal_set_message(t("Item !pid created successfully.", array('!pid' => l($new_item->pid, 'fedora/repository/'. $new_item->pid))), "status");

}


public function buildAddPagesForm($form = array()) {

}
Expand Down Expand Up @@ -291,4 +302,4 @@ function transform_mods_to_dc($mods) {
else {
return FALSE;
}
}
}
132 changes: 132 additions & 0 deletions ilives/tests/fedora_ilives.test
@@ -0,0 +1,132 @@
<?php
// $Id$

/*
* Tests the functions of the Fedora Repository module.
*/

class FedoraBookTestCase extends DrupalWebTestCase {

public static function getInfo() {
return array(
'name' => 'Fedora Book',
'description' => t('The Fedora repository book content model.'),
'group' => t('fedora repository'),
);
}

function setUp() {
parent::setUp('fedora_repository', 'fedora_ilives', 'tabs');

module_load_include('inc', 'fedora_repository', 'api/fedora_item');

// Create and login user.
$repository_user = $this->drupalCreateFedoraUser(array('add fedora datastreams',
'edit fedora meta data',
'edit tags datastream',
'ingest new fedora objects',
'purge objects and datastreams',
'view fedora collection',
'view detailed list of content'));


$this->drupalLogin($repository_user);

}

public function testBookCModel() {
// First add a book collection

$pid_list = array();
// Create a collection for ingesting book content model objects.

$ingest_form = array();
$ingest_form['models'] = 'islandora:collectionCModel/ISLANDORACM';

$this->drupalPost('fedora/ingestObject/islandora:top/Islandora%20Top-Level%20Collection', $ingest_form, 'Next');

$ingest_title = $this->randomName(32);
$ingest_form_step_2['dc:title'] = $ingest_title;
$ingest_form_step_2['dc:description'] = $this->randomName(256);
$ingest_form_step_2['files[ingest-file-location]'] = realpath(drupal_get_path('module', 'fedora_ilives') . '/xml/book_collection_policy.xml');
$this->drupalPost(NULL, $ingest_form_step_2, 'Ingest');
$this->assertPattern('/Item .* created successfully./', "Verified item created.");

$pid = $this->getIngestedPid();
$this->drupalGet("fedora/repository/$pid");
$pid_list[] = $pid;

// Now add a book into the new collection
$this->pass("Create book collection $pid below top-level collection.", 'fedora book');
$ingest_book_form = array();
$ingest_book_form['models'] = 'ilives:bookCModel/ISLANDORACM';
$this->drupalPost("fedora/ingestObject/$pid/", $ingest_book_form, 'Next');
$ingest_book_form_step_2 = array();

$ingest_book_form_step_2['mods[mods_record]'] = file_get_contents(drupal_get_path('module', 'fedora_ilives') . '/tests/test_files/mods_record.xml');
$this->outputScreenContents();
$this->drupalPost(NULL, $ingest_book_form_step_2, 'Ingest');
$this->outputScreenContents();
$book_pid = $this->getIngestedPid();
$pid_list[] = $book_pid;
if (!empty($book_pid)) {
$this->pass("Successfully ingested book object $book_pid.");
}
$this->cleanUpRepository($pid_list);

}

private function cleanUpRepository($pid_list = array()) {
$this->pass("This is the PID list to purge: ". implode(", ", $pid_list) );
foreach ($pid_list as $pid) {
$this->drupalPost("fedora/repository/purgeObject/$pid", array(), 'Purge');
$this->drupalPost(NULL, array(), 'Delete');
}
}

private function getIngestedPid() {
$subject = $this->drupalGetContent();
$pattern = '/">(.*)<\/a> created successfully./';
$matches = array();
$res = preg_match($pattern, $subject, $matches);
return $matches[1];
}

private function outputScreenContents($description = '', $basename = '') {
// This is a hack to get a directory that won't be cleaned up by SimpleTest.
$file_dir = file_directory_path() . '../simpletest_output_pages';
if (!is_dir($file_dir)) {
mkdir($file_dir, 0777, TRUE);
}
$output_path = "$file_dir/$basename.". $this->randomName(10) . '.html';
$rv = file_put_contents($output_path, $this->drupalGetContent());
$this->pass("$description: Contents of result page are ". l('here', $output_path));
}

protected function drupalCreateFedoraUser($permissions = array('access comments', 'access content', 'post comments', 'post comments without approval')) {
// Create a role with the given permission set.
if (!($rid = $this->drupalCreateRole($permissions))) {
return FALSE;
}

// Create a user assigned to that role.
$edit = array();
$edit['name'] = 'simpletestuser';
$edit['mail'] = $edit['name'] . '@example.com';
$edit['roles'] = array($rid => $rid);
$edit['pass'] = 'simpletestpass';
$edit['status'] = 1;

$account = user_save('', $edit);

$this->assertTrue(!empty($account->uid), t('User created with name %name and pass %pass', array('%name' => $edit['name'], '%pass' => $edit['pass'])), t('User login'));
if (empty($account->uid)) {
return FALSE;
}

// Add the raw password so that we can log in as this user.
$account->pass_raw = $edit['pass'];
return $account;
}

}
83 changes: 83 additions & 0 deletions ilives/tests/test_files/mods_record.xml
@@ -0,0 +1,83 @@
<?xml version="1.0"?>
<mods:modsCollection xmlns:mods="http://www.loc.gov/mods/v3" version="3.0">
<mods:mods xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/mods/v3" version="3.0" xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-0.xsd">
<titleInfo>
<nonSort>The </nonSort>
<title>amazing Maurice and his educated rodents</title>
</titleInfo>
<name type="personal">
<namePart>Pratchett, Terry.</namePart>
<role>
<roleTerm authority="marcrelator" type="text">creator</roleTerm>
</role>
</name>
<typeOfResource>text</typeOfResource>
<genre authority="marc">novel</genre>
<originInfo>
<place>
<placeTerm type="code" authority="marccountry">enk</placeTerm>
</place>
<place>
<placeTerm type="text">London</placeTerm>
</place>
<publisher>Corgi Books</publisher>
<dateIssued>2002</dateIssued>
<dateIssued encoding="marc">2001</dateIssued>
<issuance>monographic</issuance>
</originInfo>
<language>
<languageTerm authority="iso639-2b" type="code">eng</languageTerm>
</language>
<physicalDescription>
<form authority="marcform">print</form>
<extent>269 p. ; 22 cm.</extent>
</physicalDescription>
<abstract>A talking cat, intelligent rats, and a strange boy cooperate in a Pied Piper scam until they try to con the wrong town and are confronted by a deadly evil rat king.</abstract>
<targetAudience authority="marctarget">juvenile</targetAudience>
<note type="statement of responsibility">Terry Pratchett.</note>
<note>Carnegie Medal winner.</note>
<subject authority="lcsh">
<topic>Discworld (Imaginary place)</topic>
<topic>Fiction</topic>
</subject>
<subject authority="lcsh">
<topic>Discworld (Imaginary place)</topic>
<topic>Fiction</topic>
</subject>
<subject authority="lcsh">
<topic>Rats</topic>
<topic>Fiction</topic>
</subject>
<subject authority="lcsh">
<topic>Cats</topic>
<topic>Fiction</topic>
</subject>
<subject authority="lcsh">
<topic>Fantasy fiction</topic>
</subject>
<subject authority="lcsh">
<topic>Humorous stories</topic>
</subject>
<classification authority="lcc">PZ7.P8865 Am 2002</classification>
<relatedItem>
<titleInfo>
<title>Discworld series</title>
</titleInfo>
<name type="personal">
<namePart>Pratchett, Terry.</namePart>
</name>
</relatedItem>
<identifier type="isbn">006001234X (library binding)</identifier>
<identifier type="isbn">0385601239</identifier>
<identifier type="isbn">0552546933 (pbk.)</identifier>
<recordInfo>
<recordContentSource authority="marcorg">CaNWHRN</recordContentSource>
<recordCreationDate encoding="marc">010730</recordCreationDate>
<recordChangeDate encoding="iso8601">20020314 .0</recordChangeDate>
<languageOfCataloging>
<languageTerm authority="iso639-2b" type="code">eng</languageTerm>
</languageOfCataloging>
</recordInfo>
</mods:mods>
</mods:modsCollection>

22 changes: 22 additions & 0 deletions ilives/xml/book_collection_policy.xml
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<collection_policy xmlns="http://www.islandora.ca" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="" xsi:schemaLocation="http://www.islandora.ca http://syn.lib.umanitoba.ca/collection_policy.xsd">
<content_models>
<content_model dsid="ISLANDORACM" name="Book" namespace="islandora" pid="ilives:bookCModel"></content_model>
</content_models>
<search_terms>
<term field="dc.title">dc.title</term>
<term field="dc.creator">dc.creator</term>
<term default="true" field="dc.description">dc.description</term>
<term field="dc.date">dc.date</term>
<term field="dc.identifier">dc.identifier</term>
<term field="dc.language">dc.language</term>
<term field="dc.publisher">dc.publisher</term>
<term field="dc.rights">dc.rights</term>
<term field="dc.subject">dc.subject</term>
<term field="dc.relation">dc.relation</term>
<term field="dcterms.temporal">dcterms.temporal</term>
<term field="dcterms.spatial">dcterms.spatial</term>
<term field="fgs.DS.first.text">Full Text</term>
</search_terms>
<relationship>isMemberOfCollection</relationship>
</collection_policy>

0 comments on commit ee4c5e2

Please sign in to comment.