public function __construct($po_request, $ps_table = "") { $this->opo_request = $po_request; $this->ops_table = $ps_table; $this->opo_dm = Datamodel::load(); $this->opa_errors = array(); $this->ops_method = $this->opo_request->getRequestMethod(); if (!in_array($this->ops_method, array("PUT", "DELETE", "GET", "POST", "OPTIONS"))) { $this->addError("Invalid HTTP request method"); } $this->opn_id = $this->opo_request->getParameter("id", pString); // we allow for a string to support fetching by idno; typically it's a numeric id if ($vs_locale = $this->opo_request->getParameter('lang', pString)) { global $g_ui_locale, $g_ui_locale_id, $_; $g_ui_locale = $vs_locale; $t_locale = new ca_locales(); if ($g_ui_locale_id = $t_locale->localeCodeToID($vs_locale)) { $g_ui_locale = $vs_locale; if (!initializeLocale($g_ui_locale)) { die("Error loading locale " . $g_ui_locale); } $this->opo_request->reloadAppConfig(); } } $vs_post_data = $this->opo_request->getRawPostData(); if (strlen(trim($vs_post_data)) > 0) { $this->opa_post = json_decode($vs_post_data, true); if (!is_array($this->opa_post)) { $this->addError(_t("Data sent via POST doesn't seem to be in JSON format")); } } else { if ($vs_post_data = $this->opo_request->getParameter('source', pString)) { $this->opa_post = json_decode($vs_post_data, true); if (!is_array($this->opa_post)) { $this->addError(_t("Data sent via 'source' parameter doesn't seem to be in JSON format")); } } else { $this->opa_post = array(); } } $this->opa_valid_tables = array("ca_objects", "ca_object_lots", "ca_entities", "ca_places", "ca_occurrences", "ca_collections", "ca_list_items", "ca_lists", "ca_object_representations", "ca_storage_locations", "ca_movements", "ca_loans", "ca_tours", "ca_tour_stops", "ca_sets"); if (strlen($ps_table) > 0) { if (!in_array($ps_table, $this->opa_valid_tables)) { $this->addError(_t("Table does not exist")); } } }
/** * Import oral histories from specified directory into CollectiveAccess database */ public function commandImportOralHistories() { $o_conf = $this->getToolConfig(); // Get locale from config and translate to numeric code $t_locale = new ca_locales(); $pn_locale_id = $t_locale->localeCodeToID($o_conf->get('locale')); $o_log = $this->getLogger(); $o_progress = $this->getProgressBar(0); $vs_transcript_dir = $this->getSetting("transcript_directory"); if (!is_readable($vs_transcript_dir)) { if ($o_log) { $o_log->logError($vs_err_msg = _t("Transcript directory %1 is not readable", $vs_transcript_dir)); } if ($o_progress) { $o_progress->setError($vs_err_msg); $o_progress->finish(); } return false; } $vs_audio_dir = $this->getSetting("audio_directory"); if (!is_readable($vs_audio_dir)) { if ($o_log) { $o_log->logError($vs_err_msg = _t("Audio directory %1 is not readable", $vs_audio_dir)); } if ($o_progress) { $o_progress->setError($vs_err_msg); $o_progress->finish(); } return false; } if ($o_progress) { $o_progress->start("Starting oral history import"); } // ---------------------------------------------------------------------- // process main data $r_dir = opendir($vs_transcript_dir); while (($vs_file = readdir($r_dir)) !== false) { if ($vs_file[0] == '.') { continue; } // Get markup and fix it up to be valid XML $vs_markup = file_get_contents($vs_transcript_dir . $vs_file); $vs_markup = preg_replace('!&!', '&', $vs_markup); $vs_xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><transcript>{$vs_markup}</transcript>"; try { $o_xml = new SimpleXMLElement($vs_xml); } catch (Exception $e) { $o_log->logError("Could not parse XML transcript for {$vs_transcript_dir}{$vs_file}: " . $e->getMessage()); continue; } $vs_idno = (string) $o_xml->identifier; if (!file_exists($vs_media_path = "{$vs_audio_dir}{$vs_idno}.mp3")) { $o_log->logError("No audio file found for {$vs_idno}. File path was {$vs_media_path}"); continue; } $vs_title = (string) $o_xml->title; $vs_date_created = (string) $o_xml->datecreated; $vs_format = (string) $o_xml->format; $vs_medium = (string) $o_xml->medium; $vs_place_recorded = (string) $o_xml->placeRecorded; $vs_rights = (string) $o_xml->rights; $vs_extent = (string) $o_xml->extent; $vs_country = (string) $o_xml->countryOfOrigin; $va_interviewers = array(); foreach ($o_xml->interviewer as $o_interviewer) { $va_interviewers[(string) $o_interviewer->attributes()->abbreviation] = (string) $o_interviewer; } $va_participants = array(); foreach ($o_xml->participant as $o_participant) { $va_participants[(string) $o_participant->attributes()->abbreviation] = (string) $o_participant; } $va_observers = array(); if ($o_xml->observer) { foreach ($o_xml->observer as $o_observer) { $va_observers[] = (string) $o_observer; } } // Create object $t_object = new ca_objects(); $t_object->setMode(ACCESS_WRITE); if (!$t_object->load(array('idno' => $vs_idno, 'deleted' => 0))) { $t_object->set('type_id', 'oral_history'); $t_object->set('idno', $vs_idno); $t_object->set('status', 0); $t_object->set('access', 1); } $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'dc_format' => $vs_format), 'dc_format'); $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'dates_value' => $vs_date_created, 'dc_dates_types' => 'created'), 'date'); $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'medium' => $vs_medium), 'medium'); $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'interview_location' => $vs_place_recorded), 'interview_location'); $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'rights' => $vs_rights), 'rights'); $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'extent' => $vs_extent), 'extent'); $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'countryOfOrigin' => $vs_country), 'countryOfOrigin'); if (!$t_object->getPrimaryKey()) { $t_object->insert(); DataMigrationUtils::postError($t_object, 'While inserting object'); if ($t_object->numErrors()) { $o_log->logError("While adding object for {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors())); } $t_object->addLabel(array('name' => $vs_title), $pn_locale_id, null, true); DataMigrationUtils::postError($t_object, 'While adding object label'); if ($t_object->numErrors()) { $o_log->logError("While adding object label for {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors())); } } else { $t_object->update(); DataMigrationUtils::postError($t_object, 'While updating object'); if ($t_object->numErrors()) { $o_log->logError("While updating object for {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors())); } } // add entities foreach ($va_interviewers as $vs_abbr => $vs_name) { $vn_entity_id = DataMigrationUtils::getEntityID(DataMigrationUtils::splitEntityName($vs_name), 'ind', $pn_locale_id); $t_object->addRelationship('ca_entities', $vn_entity_id, 'interviewer'); DataMigrationUtils::postError($t_object, "While adding interviewer {$vs_name} to object"); if ($t_object->numErrors()) { $o_log->logError("While adding interview {$vs_name} to {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors())); } } foreach ($va_participants as $vs_abbr => $vs_name) { $vn_entity_id = DataMigrationUtils::getEntityID(DataMigrationUtils::splitEntityName($vs_name), 'ind', $pn_locale_id); $t_object->addRelationship('ca_entities', $vn_entity_id, 'interviewee'); DataMigrationUtils::postError($t_object, "While adding interviewee {$vs_name} to object"); if ($t_object->numErrors()) { $o_log->logError("While adding interviee {$vs_name} to {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors())); } } foreach ($va_observers as $vn_i => $vs_name) { $vn_entity_id = DataMigrationUtils::getEntityID(DataMigrationUtils::splitEntityName($vs_name), 'ind', $pn_locale_id); $t_object->addRelationship('ca_entities', $vn_entity_id, 'observer'); DataMigrationUtils::postError($t_object, "While adding observer {$vs_name} to object"); if ($t_object->numErrors()) { $o_log->logError("While adding observer {$vs_name} to {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors())); } } // Add media $t_rep = $t_object->addRepresentation($vs_media_path, "front", $pn_locale_id, 0, 1, true, array(), array('returnRepresentation' => true)); DataMigrationUtils::postError($t_object, "While adding representation {$vs_media_path} to object"); if ($t_object->numErrors()) { $o_log->logError("While adding representation {$vs_media_path} to {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors())); } if ($t_object->numErrors()) { continue; } $va_clips = array(); foreach ($o_xml->clip as $o_clip) { $vs_content = nl2br(preg_replace('!^[\\n\\r\\t]+!', '', trim((string) $o_clip->asXML()))); $vs_start = (string) $o_clip->attributes()->start; $va_themes = $va_places = array(); foreach ($o_clip->children() as $o_node) { $vs_tag = (string) $o_node->getName(); switch ($vs_tag) { case 'place': $va_places[] = (string) $o_node; break; default: $va_themes[] = $vs_tag; break; } } $va_clips[] = array('start' => $vs_start, 'content' => $vs_content, 'themes' => $va_themes, 'places' => $va_places); } foreach ($va_clips as $vn_i => $va_clip) { $vs_start = $va_clip['start']; if (!($vs_end = $va_clips[$vn_i + 1]['start'])) { $va_info = $t_rep->getMediaInfo('media', 'original'); $vs_end = $va_info['PROPERTIES']['duration']; } //print "[$vs_start/$vs_end] (".join('/', $va_clip['themes'])."); (".join('/', $va_clip['places']).") ".substr($va_clip['content'], 0, 30)."\n\n\n"; $t_annotation = $t_rep->addAnnotation("{$vs_start} ... {$vs_end}", $pn_locale_id, 1, array('startTimecode' => $vs_start, 'endTimecode' => $vs_end), 0, 1, array('transcription' => $va_clip['content']), array('returnAnnotation' => true)); DataMigrationUtils::postError($t_rep, "While adding annotation to representation"); if ($t_rep->numErrors()) { $o_log->logError("While adding annotation {$vs_start}/{$vs_end} to {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_rep->getErrors())); } if ($t_annotation) { foreach ($va_clip['themes'] as $vs_theme) { $t_annotation->addRelationship('ca_list_items', $vs_theme, 'describes'); DataMigrationUtils::postError($t_annotation, "While adding theme {$vs_theme} to annotation"); if ($t_annotation->numErrors()) { $o_log->logError("While adding theme {$vs_theme} to annotation {$vs_start}/{$vs_end} for {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_annotation->getErrors())); } } foreach ($va_clip['places'] as $vs_place) { if ($vn_place_id = ca_places::find(array('preferred_labels' => array('name' => $vs_place)), array('returnAs' => 'firstId'))) { $t_annotation->addRelationship('ca_places', $vn_place_id, 'describes'); DataMigrationUtils::postError($t_annotation, "While adding place {$vs_place} to annotation"); if ($t_annotation->numErrors()) { $o_log->logError("While adding place {$vs_place} to annotation {$vs_start}/{$vs_end} for {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_annotation->getErrors())); } } } } } $o_log->logInfo("Imported {$vs_file}"); } $o_progress->finish("Completed processing"); if ($o_log) { $o_log->logDebug(_t("Ended oral history import")); } return true; }
private function editItem() { if (!($t_instance = $this->_getTableInstance($this->ops_table, $this->opn_id))) { return false; } $t_locales = new ca_locales(); $va_post = $this->getRequestBodyArray(); // intrinsic fields if (is_array($va_post["intrinsic_fields"]) && sizeof($va_post["intrinsic_fields"])) { foreach ($va_post["intrinsic_fields"] as $vs_field_name => $vs_value) { $t_instance->set($vs_field_name, $vs_value); } } // attributes if (is_array($va_post["remove_attributes"])) { foreach ($va_post["remove_attributes"] as $vs_code_to_delete) { $t_instance->removeAttributes($vs_code_to_delete); } } else { if ($va_post["remove_all_attributes"]) { $t_instance->removeAttributes(); } } if (is_array($va_post["attributes"]) && sizeof($va_post["attributes"])) { foreach ($va_post["attributes"] as $vs_attribute_name => $va_values) { foreach ($va_values as $va_value) { if ($va_value["locale"]) { $va_value["locale_id"] = $t_locales->localeCodeToID($va_value["locale"]); unset($va_value["locale"]); } $t_instance->addAttribute($va_value, $vs_attribute_name); } } } $t_instance->setMode(ACCESS_WRITE); $t_instance->update(); // AFTER UPDATE STUFF // yank all labels? if ($va_post["remove_all_labels"]) { $t_instance->removeAllLabels(); } // preferred labels if (is_array($va_post["preferred_labels"]) && sizeof($va_post["preferred_labels"])) { foreach ($va_post["preferred_labels"] as $va_label) { if ($va_label["locale"]) { $vn_locale_id = $t_locales->localeCodeToID($va_label["locale"]); unset($va_label["locale"]); } $t_instance->addLabel($va_label, $vn_locale_id, null, true); } } // nonpreferred labels if (is_array($va_post["nonpreferred_labels"]) && sizeof($va_post["nonpreferred_labels"])) { foreach ($va_post["nonpreferred_labels"] as $va_label) { if ($va_label["locale"]) { $vn_locale_id = $t_locales->localeCodeToID($va_label["locale"]); unset($va_label["locale"]); } if ($va_label["type_id"]) { $vn_type_id = $va_label["type_id"]; unset($va_label["type_id"]); } else { $vn_type_id = null; } $t_instance->addLabel($va_label, $vn_locale_id, $vn_type_id, false); } } // relationships if (is_array($va_post["remove_relationships"])) { foreach ($va_post["remove_relationships"] as $vs_table) { $t_instance->removeRelationships($vs_table); } } if ($va_post["remove_all_relationships"]) { foreach ($this->opa_valid_tables as $vs_table) { $t_instance->removeRelationships($vs_table); } } if (is_array($va_post["related"]) && sizeof($va_post["related"]) > 0) { foreach ($va_post["related"] as $vs_table => $va_relationships) { foreach ($va_relationships as $va_relationship) { $vs_source_info = isset($va_relationship["source_info"]) ? $va_relationship["source_info"] : null; $vs_effective_date = isset($va_relationship["effective_date"]) ? $va_relationship["effective_date"] : null; $vs_direction = isset($va_relationship["direction"]) ? $va_relationship["direction"] : null; $t_rel_instance = $this->_getTableInstance($vs_table); $vs_pk = isset($va_relationship[$t_rel_instance->primaryKey()]) ? $va_relationship[$t_rel_instance->primaryKey()] : null; $vs_type_id = isset($va_relationship["type_id"]) ? $va_relationship["type_id"] : null; $t_instance->addRelationship($vs_table, $vs_pk, $vs_type_id, $vs_effective_date, $vs_source_info, $vs_direction); } } } if ($t_instance->numErrors() > 0) { foreach ($t_instance->getErrors() as $vs_error) { $this->addError($vs_error); } return false; } else { return array($t_instance->primaryKey() => $t_instance->getPrimaryKey()); } }
/** * */ public function getPreferredUILocaleID() { if (!defined("__CA_DEFAULT_LOCALE__")) { define("__CA_DEFAULT_LOCALE__", "en_US"); // if all else fails... } $t_locale = new ca_locales(); if ($vs_locale = $this->getPreference('ui_locale')) { if ($vn_locale_id = $t_locale->localeCodeToID($vs_locale)) { return $vn_locale_id; } } $va_default_locales = $this->getAppConfig()->getList('locale_defaults'); if (sizeof($va_default_locales) && ($vn_locale_id = $t_locale->localeCodeToID($va_default_locales[0]))) { return $vn_locale_id; } return $t_locale->localeCodeToID(__CA_DEFAULT_LOCALE__); }
/** * Returns list of caption/subtitle files attached to a representation * The return value is an array key'ed on the caption_id; array values are arrays * with keys set to values for each file returned. They keys are: * path = The absolute file path to the file * url = The URL for the file * caption_id = a unique identifier for each attached caption file * * @param int $pn_representation_id The representation_id of the representation to return files for. If omitted the currently loaded representation is used. If no representation_id is specified and no row is loaded null will be returned. * @param array $pa_locale_ids * @param array $pa_options * @return array A list of caption files attached to the representations. If no files are associated an empty array is returned. */ public function getCaptionFileList($pn_representation_id = null, $pa_locale_ids = null, $pa_options = null) { if (!($vn_representation_id = $pn_representation_id)) { if (!($vn_representation_id = $this->getPrimaryKey())) { return null; } } $t_locale = new ca_locales(); $va_locale_ids = array(); if ($pa_locale_ids) { if (!is_array($pa_locale_ids)) { $pa_locale_ids = array($pa_locale_ids); } foreach ($pa_locale_ids as $vn_i => $vm_locale) { if (is_numeric($vm_locale) && (int) $vm_locale) { $va_locale_ids[] = (int) $vm_locale; } else { if ($vn_locale_id = $t_locale->localeCodeToID($vm_locale)) { $va_locale_ids[] = $vn_locale_id; } } } } $vs_locale_sql = ''; $va_params = array((int) $vn_representation_id); if (sizeof($va_locale_ids) > 0) { $vs_locale_sql = " AND locale_id IN (?)"; $va_params[] = $va_locale_ids; } $o_db = $this->getDb(); $qr_res = $o_db->query("\n \t\t\tSELECT *\n \t\t\tFROM ca_object_representation_captions\n \t\t\tWHERE\n \t\t\t\trepresentation_id = ?\n \t\t\t{$vs_locale_sql}\n \t\t", $va_params); $va_files = array(); while ($qr_res->nextRow()) { $vn_caption_id = $qr_res->get('caption_id'); $vn_locale_id = $qr_res->get('locale_id'); $va_files[$vn_caption_id] = $qr_res->getRow(); unset($va_files[$vn_caption_id]['caption_file']); $va_files[$vn_caption_id]['path'] = $qr_res->getFilePath('caption_file'); $va_files[$vn_caption_id]['url'] = $qr_res->getFileUrl('caption_file'); $va_files[$vn_caption_id]['filesize'] = caFormatFileSize(filesize($va_files[$vn_caption_id]['path'])); $va_files[$vn_caption_id]['caption_id'] = $vn_caption_id; $va_files[$vn_caption_id]['locale_id'] = $vn_locale_id; $va_files[$vn_caption_id]['locale'] = $t_locale->localeIDToName($vn_locale_id); $va_files[$vn_caption_id]['locale_code'] = $t_locale->localeIDToCode($vn_locale_id); } return $va_files; }
// TODO: move this into a library so $_, $g_ui_locale_id and $g_ui_locale gets set up automatically require_once __CA_APP_DIR__ . "/helpers/initializeLocale.php"; $va_ui_locales = $g_request->config->getList('ui_locales'); if ($vs_lang = $g_request->getParameter('lang', pString)) { if (in_array($vs_lang, $va_ui_locales)) { $g_request->session->setVar('lang', $vs_lang); } } if (!($g_ui_locale = $g_request->session->getVar('lang'))) { $g_ui_locale = $va_ui_locales[0]; } if (!in_array($g_ui_locale, $va_ui_locales)) { $g_ui_locale = $va_ui_locales[0]; } $t_locale = new ca_locales(); $g_ui_locale_id = $t_locale->localeCodeToID($g_ui_locale); // get current UI locale as locale_id (available as global) $_ = array(); if (file_exists($vs_theme_specific_locale_path = $g_request->getThemeDirectoryPath() . '/locale/' . $g_ui_locale . '/messages.mo')) { $_[] = new Zend_Translate('gettext', $vs_theme_specific_locale_path, $g_ui_locale); } $_[] = new Zend_Translate('gettext', __CA_APP_DIR__ . '/locale/' . $g_ui_locale . '/messages.mo', $g_ui_locale); if (!initializeLocale($g_ui_locale)) { die("Error loading locale " . $g_ui_locale); } $g_request->reloadAppConfig(); // need to reload app config to reflect current locale // // PageFormat plug-in generates header/footer shell around page content // if (!$g_request->isAjax() && !$g_request->isDownload()) {
public function insert($pa_options = null) { if (!$this->inTransaction()) { $this->setTransaction(new Transaction()); } if ($this->get('is_default')) { $this->getDb()->query("\n\t\t\t\tUPDATE ca_list_items \n\t\t\t\tSET is_default = 0 \n\t\t\t\tWHERE list_id = ?\n\t\t\t", (int) $this->get('list_id')); } $vn_rc = parent::insert($pa_options); if ($this->getPrimaryKey()) { $t_list = new ca_lists(); $o_trans = $this->getTransaction(); $t_list->setTransaction($o_trans); if ($t_list->load($this->get('list_id')) && $t_list->get('list_code') == 'place_hierarchies' && $this->get('parent_id')) { // insert root or place hierarchy when creating non-root items in 'place_hierarchies' list $t_locale = new ca_locales(); $va_locales = $this->getAppConfig()->getList('locale_defaults'); $vn_locale_id = $t_locale->localeCodeToID($va_locales[0]); // create root in ca_places $t_place = new ca_places(); $t_place->setTransaction($o_trans); $t_place->setMode(ACCESS_WRITE); $t_place->set('hierarchy_id', $this->getPrimaryKey()); $t_place->set('locale_id', $vn_locale_id); $t_place->set('type_id', null); $t_place->set('parent_id', null); $t_place->set('idno', 'Root node for ' . $this->get('idno')); $t_place->insert(); if ($t_place->numErrors()) { $this->delete(); $this->errors = array_merge($this->errors, $t_place->errors); return false; } $t_place->addLabel(array('name' => 'Root node for ' . $this->get('idno')), $vn_locale_id, null, true); } } if ($this->numErrors()) { $this->getTransaction()->rollback(); } else { $this->getTransaction()->commit(); $this->_setSettingsForList(); } return $vn_rc; }
private function fetchAndImport($pa_item_queue, $po_client, $pa_config, $pa_tables, $ps_code) { if (!is_array($pa_tables)) { $pa_tables = array(); } $t_rel_type = new ca_relationship_types(); $vs_base_url = $pa_config['baseUrl']; $o_dm = Datamodel::load(); $t_locale = new ca_locales(); $t_list = new ca_lists(); $vn_source_id = $t_list->getItemIDFromList('object_sources', $pa_config['code']); $pn_rep_type_id = $t_list->getItemIDFromList('object_representation_types', 'front'); foreach ($pa_item_queue as $vn_i => $va_item) { $vs_table = $va_item['table']; $va_import_relationships_from = $pa_config['importRelatedFor'][$va_item['table']]; print "oo"; print_r($va_import_relationships_from); $vn_id = $va_item['id']; if (!$vn_id) { print "[Notice] SKIP CAUSE NO ID ({$ps_code})\n"; continue; } if (isset($this->opa_processed_records[$vs_table . '/' . $vn_id])) { continue; } $vs_idno = trim((string) $va_item['idno']); try { $o_xml = $po_client->getItem($vs_table, $vn_id)->get(); } catch (exception $e) { print "[ERROR] While trying to get item information: " . $e->getMessage() . "\n"; continue; } $o_item = $o_xml->getItem; $t_instance = $o_dm->getInstanceByTableName($vs_table, false); $t_instance_label = $t_instance->getLabelTableInstance(); // Look for existing record $vb_skip = false; $vb_update = false; $vs_label_fld = $t_instance->getLabelDisplayField(); $vs_label = (string) $o_item->preferred_labels->en_US->{$vs_label_fld}; print "[Notice] Processing [{$vs_table}] {$vs_label} [{$vs_idno}] ({$ps_code})\n"; $t_instance_label->clear(); if ($vs_idno && ($vs_table == 'ca_objects' && $t_instance->load(array('idno' => $vs_idno)) || $vs_table != 'ca_objects' && $t_instance->load(array('idno' => $vs_idno)))) { if ($t_instance->hasField('deleted') && $t_instance->get('deleted') == 1) { $t_instance->set('deleted', 0); } //print "[Notice] Update [{$vs_idno}] for {$vs_table} 'cause it already exists ({$ps_code})\n"; if (!$t_instance->getPrimaryKey()) { $vb_skip = true; print "[ERROR] Could not load instance for [{$vs_idno}]\n"; } $vb_update = true; $t_instance->setMode(ACCESS_WRITE); // Clear labels $t_instance->removeAllLabels(); if ($t_instance->numErrors()) { print "[ERROR] Could not remove labels for updating: " . join("; ", $t_instance->getErrors()) . "\n"; } // Clear attributes $t_instance->removeAttributes(null, array('dontCheckMinMax' => true)); if ($t_instance->numErrors()) { print "[ERROR] Could not remove attributes for updating: " . join("; ", $t_instance->getErrors()) . "\n"; } // Clear relationships if (is_array($va_import_relationships_from)) { foreach ($va_import_relationships_from as $vs_rel_table => $va_table_info) { $t_instance->removeRelationships($vs_rel_table); if ($t_instance->numErrors()) { print "[ERROR] Could not remove {$vs_rel_table} relationships for updating: " . join("; ", $t_instance->getErrors()) . "\n"; } } } if ($t_instance->tableName() == 'ca_objects') { //$t_instance->set('source_id', $vn_source_id); } $t_instance->update(); if ($t_instance->numErrors()) { print "[ERROR] Could not clear record for updating: " . join("; ", $t_instance->getErrors()) . "\n"; } } // create new one if (!$vb_update) { $t_instance->clear(); if ($t_instance->tableName() == 'ca_objects') { //$t_instance->set('source_id', $vn_source_id); } } $t_instance->setMode(ACCESS_WRITE); // add intrinsics switch ($vs_table) { case 'ca_collections': $va_intrinsics = array('status', 'access', 'idno'); break; case 'ca_occurrences': $va_intrinsics = array('status', 'access', 'idno'); break; case 'ca_objects': $va_intrinsics = array('status', 'access', 'idno'); break; case 'ca_entities': $va_intrinsics = array('status', 'access', 'lifespan', 'source_id', 'idno'); break; case 'ca_object_lots': $va_intrinsics = array('status', 'access', 'idno_stub'); break; default: $va_intrinsics = array('status', 'access', 'idno'); break; } // TODO: Need to properly handle foreign-key intrinsics when the item they point to doesn't exist // eg. source_id fields, various ca_objects and ca_object_lots intrinsics, etc. if ($vs_table == 'ca_list_items') { // does list exist? $vs_list_code = (string) $o_item->{'list_code'}; $t_list = new ca_lists(); if (!$t_list->load(array('list_code' => $vs_list_code))) { // create list $t_list->setMode(ACCESS_WRITE); // TODO: should we bother to replicate the is_hierarchical, use_as_vocabulary and default_sort settings via a service? // For now just set reasonable values $t_list->set('list_code', $vs_list_code); $t_list->set('is_hierarchical', 1); $t_list->set('use_as_vocabulary', 1); $t_list->set('default_sort', 0); $t_list->insert(); if ($t_list->numErrors()) { print "[ERROR] Could not insert new list '{$vs_list_code}': " . join('; ', $t_list->getErrors()) . "\n"; } else { $t_list->addLabel(array('name' => $vs_list_code), $pn_locale_id, null, true); if ($t_list->numErrors()) { print "[ERROR] Could not add label to new list '{$vs_list_code}': " . join('; ', $t_list->getErrors()) . "\n"; } } } $t_instance->set('list_id', $t_list->getPrimaryKey()); } foreach ($va_intrinsics as $vs_f) { $t_instance->set($vs_f, $o_item->{$vs_f}); } if (!$vb_update) { $vn_type_id = $t_instance->getTypeIDForCode((string) $o_item->type_id); if (!$vn_type_id) { print "NO TYPE FOR {$vs_table}/" . $o_item->type_id . "\n"; } $t_instance->set('type_id', $vn_type_id); if ($t_instance->tableName() == 'ca_objects') { //$t_instance->set('source_id', $vn_source_id); } // TODO: add hook onBeforeInsert() $t_instance->insert(); // TODO: add hook onInsert() if ($t_instance->numErrors()) { print "[ERROR] Could not insert record: " . join('; ', $t_instance->getErrors()) . "\n"; } } // add attributes // TODO: make this configurable $va_codes = $t_instance->getApplicableElementCodes(); // $va_codes = array( // 'description', // 'georeference', 'geonames', 'internal_notes', // 'oclc_number', 'file_name', // 'digitized_by', 'digitized_date', 'call_number', // 'other_call_number', 'collection_title', 'collection_number', // 'box_number', 'folder_number', 'volume_number', 'page_number', 'shelf', // 'pulled_digitization', 'pulled_name', 'pulled_date', 'returned_digitization', // 'returned_name', 'returned_date', 'needs_redigitization', 'donor', 'copyright_holder', // 'reproduction_restrictions', 'administrative_notes', 'date_view', 'date_item', // 'view_format', 'item_format', 'dimensions', 'map_scale', 'image_description', 'address', // 'lcsh_terms', 'inscription' // // ); foreach ($va_codes as $vs_code) { $t_element = $t_instance->_getElementInstance($vs_code); switch ($t_element->get('datatype')) { case 0: // container $va_elements = $t_element->getElementsInSet(); $o_attr = $o_item->{'ca_attribute_' . $vs_code}; foreach ($o_attr as $va_tag => $o_tags) { foreach ($o_tags as $vs_locale => $o_values) { if (!($vn_locale_id = $t_locale->localeCodeToID($vs_locale))) { $vn_locale_id = null; } $va_container_data = array('locale_id' => $vn_locale_id); foreach ($o_values as $o_value) { foreach ($va_elements as $vn_i => $va_element_info) { if ($va_element_info['datatype'] == 0) { continue; } if ($vs_value = trim((string) $o_value->{$va_element_info['element_code']})) { switch ($va_element_info['datatype']) { case 3: //list $va_tmp = explode(":", $vs_value); //<item_id>:<item_idno> //print "CONTAINER LIST CODE=".$va_tmp[1]."/$vs_value/".$va_element_info['list_id']."\n"; $va_container_data[$va_element_info['element_code']] = $t_list->getItemIDFromList($va_element_info['list_id'], $va_tmp[1]); break; default: $va_container_data[$va_element_info['element_code']] = $vs_value; break; } } } $t_instance->replaceAttribute($va_container_data, $vs_code); } } } break; case 3: // list $o_attr = $o_item->{'ca_attribute_' . $vs_code}; foreach ($o_attr as $va_tag => $o_tags) { foreach ($o_tags as $vs_locale => $o_values) { if (!($vn_locale_id = $t_locale->localeCodeToID($vs_locale))) { $vn_locale_id = null; } foreach ($o_values as $o_value) { if ($vs_value = trim((string) $o_value->{$vs_code})) { $va_tmp = explode(":", $vs_value); //<item_id>:<item_idno> // TODO: create lists and list items if they don't already exist if ($vn_item_id = $t_list->getItemIDFromList($t_element->get('list_id'), $va_tmp[1])) { $t_instance->replaceAttribute(array($vs_code => $vn_item_id, 'locale_id' => $vn_locale_id), $vs_code); } } } } } break; case 15: // File // File case 16: // Media $t_instance->update(); if ($t_instance->numErrors()) { print "[ERROR] Could not update record before media: " . join('; ', $t_instance->getErrors()) . "\n"; } // TODO: detect if media has changes and only pull if it has $o_attr = $o_item->{'ca_attribute_' . $vs_code}; foreach ($o_attr as $va_tag => $o_tags) { foreach ($o_tags as $vs_locale => $o_values) { if (!($vn_locale_id = $t_locale->localeCodeToID($vs_locale))) { $vn_locale_id = null; } foreach ($o_values as $o_value) { if ($vs_value = trim((string) $o_value->{$vs_code})) { $t_instance->replaceAttribute(array($vs_code => $vs_value, 'locale_id' => $vn_locale_id), $vs_code); } } } } $t_instance->update(); if ($t_instance->numErrors()) { print "[ERROR] Could not update record after media: " . join('; ', $t_instance->getErrors()) . "\n"; } break; default: $o_attr = $o_item->{'ca_attribute_' . $vs_code}; foreach ($o_attr as $va_tag => $o_tags) { foreach ($o_tags as $vs_locale => $o_values) { if (!($vn_locale_id = $t_locale->localeCodeToID($vs_locale))) { $vn_locale_id = null; } foreach ($o_values as $o_value) { if ($vs_value = trim((string) $o_value->{$vs_code})) { $t_instance->replaceAttribute(array($vs_code => $vs_value, 'locale_id' => $vn_locale_id), $vs_code); } } } } break; } } $t_instance->update(); if ($t_instance->numErrors()) { print "[ERROR] Could not update [1] record: " . join('; ', $t_instance->getErrors()) . "\n"; } // TODO: add hook onBeforeUpdate() $t_instance->update(); // TODO: add hook onUpdate() if ($t_instance->numErrors()) { print "[ERROR] Could not update [2] record: " . join('; ', $t_instance->getErrors()) . "\n"; } // get label fields $va_label_data = array(); foreach ($t_instance->getLabelUIFields() as $vs_field) { if (!($va_label_data[$vs_field] = $o_item->preferred_labels->en_US->{$vs_field})) { $va_label_data[$vs_field] = $o_item->preferred_labels->en_US->{$vs_field}; } } // TODO: add hook onBeforeAddLabel() $t_instance->addLabel($va_label_data, 1, null, true); // TODO: add hook onAddLabel() if ($t_instance->numErrors()) { print "ERROR adding label: " . join('; ', $t_instance->getErrors()) . "\n"; } $this->opa_processed_records[$va_item['table'] . '/' . (int) $va_item['id']] = $t_instance->getPrimaryKey(); if ($vb_skip) { continue; } if (!is_array($va_import_relationships_from)) { continue; } $pa_tables[$va_item['table']] = true; // Are there relationships? $pb_imported_self_relations = false; print_r($va_import_relationships_from); foreach ($va_import_relationships_from as $vs_rel_table => $va_table_info) { $vb_is_self_relation = $vs_rel_table == $t_instance->tableName() && !$pb_imported_self_relations ? true : false; if (!$pa_tables[$vs_rel_table] || $vb_is_self_relation) { // load related records recursively if ($vs_rel_table == $t_instance->tableName()) { $pb_imported_self_relations = true; } if ($o_item->{'related_' . $vs_rel_table}) { $t_rel = $o_dm->getInstanceByTableName($vs_rel_table, false); // TODO: add hook onBeforeAddRelationships() foreach ($o_item->{'related_' . $vs_rel_table} as $vs_tag => $o_related_items) { foreach ($o_related_items as $vs_i => $o_related_item) { if (is_array($pa_config['importRelatedFor'][$va_item['table']][$vs_rel_table])) { $va_rel_types = array_keys($pa_config['importRelatedFor'][$va_item['table']][$vs_rel_table]); if (is_array($va_rel_types) && sizeof($va_rel_types) && !in_array((string) $o_related_item->relationship_type_code, $va_rel_types)) { print "[INFO] Skipped relationship for {$vs_display_name} because type='" . (string) $o_related_item->relationship_type_code . "' is excluded\n"; continue; } } $vs_pk = $t_rel->primaryKey(); $vn_id = (int) $o_related_item->{$vs_pk}; $va_queue = array($vs_rel_table . "/" . $vn_id => array('table' => $vs_rel_table, 'id' => $vn_id, 'idno' => (string) $o_related_item->idno)); // TODO: Add from/until support $this->fetchAndImport($va_queue, $po_client, $pa_config, $pa_tables, $ps_code); $vn_rel_record_id = $this->opa_processed_records[$vs_rel_table . '/' . (int) $vn_id]; $vb_skip = false; if ($vb_is_self_relation) { if ($this->opa_processed_self_relations[$vs_rel_table][$vn_rel_record_id][$t_instance->getPrimaryKey()][(string) $o_related_item->relationship_type_code] || $this->opa_processed_self_relations[$vs_rel_table][$t_instance->getPrimaryKey()][$vn_rel_record_id][(string) $o_related_item->relationship_type_code]) { $vb_skip = true; } else { $this->opa_processed_self_relations[$vs_rel_table][$t_instance->getPrimaryKey()][$vn_rel_record_id][(string) $o_related_item->relationship_type_code] = $this->opa_processed_self_relations[$vs_rel_table][$vn_rel_record_id][$t_instance->getPrimaryKey()][(string) $o_related_item->relationship_type_code] = true; } } if (!$vb_skip) { $t_instance->addRelationship($vs_rel_table, $vn_rel_record_id, (string) $o_related_item->relationship_type_code); if ($t_instance->numErrors()) { print "[ERROR] Could not add relationship to {$vs_rel_table} for row_id={$vn_rel_record_id}: " . join('; ', $t_instance->getErrors()) . "\n"; } } } } // TODO: add hook onAddRelationships() } } } // Is there media? if ($t_instance->tableName() == 'ca_objects') { try { $o_rep_xml = $po_client->getObjectRepresentations((int) $va_item['id'], array('large', 'original'))->get(); } catch (exception $e) { print "[ERROR] While getting object representations: " . $e->getMessage() . "\n"; } $va_existing_reps = $t_instance->getRepresentations(array('large', 'original')); $va_existing_md5s = array(); $va_rep_ids = array(); $va_dupe_reps = array(); foreach ($va_existing_reps as $va_rep) { if ($va_existing_md5s[$va_rep['info']['original']['MD5']] && $va_existing_md5s[$va_rep['info']['large']['MD5']]) { // dupe $va_dupe_reps[] = $va_rep['representation_id']; continue; } $va_existing_md5s[$va_rep['info']['original']['MD5']] = $va_rep['representation_id']; $va_existing_md5s[$va_rep['info']['large']['MD5']] = $va_rep['representation_id']; $va_rep_ids[] = $va_rep['representation_id']; } if ($o_rep_xml->getObjectRepresentations) { foreach ($o_rep_xml->getObjectRepresentations as $vs_x => $o_reps) { foreach ($o_reps as $vs_key => $o_rep) { if ($vs_url = trim((string) $o_rep->urls->large)) { $vs_remote_original_md5 = (string) $o_rep->info->original->MD5; $vs_remote_large_md5 = (string) $o_rep->info->large->MD5; if (isset($va_existing_md5s[$vs_remote_original_md5]) && $va_existing_md5s[$vs_remote_original_md5] || isset($va_existing_md5s[$vs_remote_large_md5]) && $va_existing_md5s[$vs_remote_large_md5]) { print "[NOTICE] Skipping representation at {$vs_url} because it already exists (MD5={$vs_remote_original_md5}/{$vs_remote_large_md5}) ({$ps_code})\n"; if (!($vn_kill_rep_id = $va_existing_md5s[$vs_remote_large_md5])) { $vn_kill_rep_id = $va_existing_md5s[$vs_remote_original_md5]; } foreach ($va_existing_md5s as $vs_md5 => $vn_rep_id) { if ($vn_kill_rep_id == $vn_rep_id) { $t_existing_rep_link = new ca_objects_x_object_representations(); if ($t_existing_rep_link->load(array('object_id' => $t_instance->getPrimaryKey(), 'representation_id' => $vn_rep_id))) { $t_existing_rep_link->setMode(ACCESS_WRITE); // print "update object_id ".$t_instance->getPrimaryKey()."/rep=$vn_rep_id to rank=".$o_rep->rank."/primary=".$o_rep->is_primary."\n"; $t_existing_rep_link->set('is_primary', (int) $o_rep->is_primary); $t_existing_rep_link->set('rank', (int) $o_rep->rank); $t_existing_rep_link->update(); if ($t_existing_rep_link->numErrors()) { print_r($t_existing_rep_link->getErrors()); } } unset($va_existing_md5s[$vs_md5]); } } continue; } print "[Notice] Importing for [{$vs_idno}] media from {$vs_url}: primary=" . (string) $o_rep->is_primary . " ({$ps_code})\n"; print "instance has id=" . $t_instance->getPrimaryKey() . "\n"; // TODO: add hook onBeforeAddMedia() $vn_link_id = $t_instance->addRepresentation($vs_url, $pn_rep_type_id, 1, (int) $o_rep->status, (int) $o_rep->access, (int) $o_rep->is_primary); // TODO: add hook onAddMedia() if ($t_instance->numErrors()) { print "[ERROR] Could not load object representation: " . join("; ", $t_instance->getErrors()) . " ({$ps_code})\n"; } else { $t_link = new ca_objects_x_object_representations($vn_link_id); $t_new_rep = new ca_object_representations($t_link->get('representation_id')); //unlink($x=$t_new_rep->getMediaPath('media', 'original')); } } } } } $va_rep_ids = array(); foreach ($va_existing_md5s as $vs_md5 => $vn_rep_id) { if ($va_rep_ids[$vn_rep_id]) { continue; } $t_obj_x_rep = new ca_objects_x_object_representations(); while ($t_obj_x_rep->load(array('object_id' => $t_instance->getPrimaryKey(), 'representation_id' => $vn_rep_id))) { $t_obj_x_rep->setMode(ACCESS_WRITE); $t_obj_x_rep->delete(true); if ($t_obj_x_rep->numErrors()) { print "[ERROR] Could not load remove object-to-representation link: " . join("; ", $t_obj_x_rep->getErrors()) . " ({$ps_code})\n"; break; } if (!$t_obj_x_rep->load(array('representation_id' => $vn_rep_id))) { $t_rep = new ca_object_representations(); if ($t_rep->load($vn_rep_id)) { $t_rep->setMode(ACCESS_WRITE); $t_rep->delete(true, array('hard' => true)); if ($t_rep->numErrors()) { print "[ERROR] Could not remove representation: " . join("; ", $t_rep->getErrors()) . "\n"; break; } } } } $va_rep_ids[$vn_rep_id] = true; } foreach ($va_dupe_reps as $vn_dupe_rep_id) { $t_rep = new ca_object_representations(); if ($t_rep->load($vn_dupe_rep_id)) { print "[Notice] DELETE DUPE {$vn_dupe_rep_id}\n"; $t_rep->setMode(ACCESS_WRITE); $t_rep->delete(true, array('hard' => true)); if ($t_rep->numErrors()) { print "[ERROR] Could not remove dupe representation: " . join("; ", $t_rep->getErrors()) . "\n"; break; } } } } unset($pa_tables[$va_item['table']]); } }
public function processLocales() { require_once __CA_MODELS_DIR__ . "/ca_locales.php"; $t_locale = new ca_locales(); $t_locale->setMode(ACCESS_WRITE); // Find any existing locales $va_locales = $t_locale->getLocaleList(array('index_by_code' => true)); foreach ($va_locales as $vs_code => $va_locale) { $this->opa_locales[$vs_code] = $va_locale['locale_id']; } if ($this->ops_base_name) { $va_locales = array(); foreach ($this->opo_profile->locales->children() as $vo_locale) { $va_locales[] = $vo_locale; } foreach ($this->opo_base->locales->children() as $vo_locale) { $va_locales[] = $vo_locale; } } else { $va_locales = $this->opo_profile->locales->children(); } foreach ($va_locales as $vo_locale) { $vs_language = self::getAttribute($vo_locale, "lang"); $vs_dialect = self::getAttribute($vo_locale, "dialect"); $vs_country = self::getAttribute($vo_locale, "country"); $vb_dont_use_for_cataloguing = self::getAttribute($vo_locale, "dontUseForCataloguing"); if (isset($this->opa_locales[$vs_language . "_" . $vs_country])) { // don't insert duplicate locales continue; } $t_locale->set('name', (string) $vo_locale); $t_locale->set('country', $vs_country); $t_locale->set('language', $vs_language); if ($vs_dialect) { $t_locale->set('dialect', $vs_dialect); } $t_locale->set('dont_use_for_cataloguing', (bool) $vb_dont_use_for_cataloguing); $t_locale->insert(); if ($t_locale->numErrors()) { $this->addError("There was an error while inserting locale {$vs_language}_{$vs_country}: " . join(" ", $t_locale->getErrors())); } $this->opa_locales[$vs_language . "_" . $vs_country] = $t_locale->getPrimaryKey(); } $va_locales = $t_locale->getAppConfig()->getList('locale_defaults'); $vn_locale_id = $t_locale->localeCodeToID($va_locales[0]); if (!$vn_locale_id) { throw new Exception("The locale default is set to a non-existing locale. Try adding '" . $va_locales[0] . "' to your profile."); } return true; }
/** * Import SIPs from specified directory into CollectiveAccess Database */ public function commandImportSIPs() { $o_conf = $this->getToolConfig(); $t = new Timer(); // Get locale from config and translate to numeric code $t_locale = new ca_locales(); $pn_locale_id = $t_locale->localeCodeToID($o_conf->get('locale')); $o_log = $this->getLogger(); $o_progress = $this->getProgressBar(0); if ($o_progress) { $o_progress->start("Starting import"); } $vs_import_directory = $this->getSetting("import_directory"); if (!is_readable($vs_import_directory)) { if ($o_log) { $o_log->logError($vs_err_msg = _t("Import directory %1 is not readable", $vs_import_directory)); } if ($o_progress) { $o_progress->setError($vs_err_msg); $o_progress->finish(); } return false; } if ($o_log) { $o_log->logDebug(_t("Started SIP import")); } //print "[1] ".$t->getTime(4)."\n"; // Look for ZIP files or directories $va_files = caGetDirectoryContentsAsList($vs_import_directory, false, false, false, true); //print "[2] ".$t->getTime(4)."\n"; $vn_count = 0; foreach ($va_files as $vs_file) { if (!preg_match("!\\.zip\$!i", $vs_file) && !is_dir($vs_file)) { continue; } $vn_count++; } $o_progress->setTotal($vn_count); //print "[3] ".$t->getTime(4)."\n"; foreach ($va_files as $vs_file) { // Top level zips or directories $vb_is_dir = is_dir($vs_file); if (!preg_match("!\\.zip\$!i", $vs_file) && !$vb_is_dir) { continue; } //print "[4] ".$t->getTime(4)."\n"; $o_progress->setMessage(_t('Processing %1', $vs_file)); // unpack ZIP $va_package_files = caGetDirectoryContentsAsList($vb_is_dir ? $vs_file : 'phar://' . $vs_file . '/', false, false, false, true); // files in top level of directory or zip //foreach($va_package_files as $vs_package_path) { //$va_tmp = explode("/", $vs_package_path); //$vs_package_dir = array_pop($va_tmp); $va_archive_files = caGetDirectoryContentsAsList($vb_is_dir ? $vs_file : 'phar://' . $vs_file, true); // Does it look like a SIP? $vb_is_sip = false; $vs_idno = $vs_idno_padded = $vs_zip_path = $vs_category = null; $va_sides = array(); foreach ($va_archive_files as $vs_archive_file) { if ($o_log) { $o_log->logDebug(_t("Testing file %1 for SIP-ness", $vs_archive_file)); } if (preg_match("!category.txt\$!", $vs_archive_file)) { $vb_is_sip = true; $va_tmp = explode("/", $vs_archive_file); array_pop($va_tmp); // pop category.txt $vs_idno = array_pop($va_tmp); $va_tmp_idno = explode("-", $vs_idno); $va_tmp_idno[2] = str_pad($va_tmp_idno[2], 6, "0", STR_PAD_LEFT); $vs_idno_padded = join("-", $va_tmp_idno); $vs_category = strtolower(preg_replace("![^A-Z \\-a-z0-9]!", "", file_get_contents($vs_archive_file))); // Translate categories switch ($vs_category) { case 'mixed': $vs_category = 'unclassified'; break; } $vs_zip_path = join("/", $va_tmp); if ($o_log) { $o_log->logInfo(_t("Found SIP %1 with category %2", $vs_archive_dir, $vs_category)); } continue; } } if (!$vb_is_sip) { if ($o_progress) { $o_progress->setError(_t('File %1 is not a valid SIP', $vs_file)); } if ($o_log) { $o_log->logWarn(_t('File %1 is not a valid SIP', $vs_file)); } continue; } //print "[5] ".$t->getTime(4)."\n"; $va_track_audio_by_side = $va_track_xml_by_side = $va_side_audio = $va_side_xml = $va_artifacts = array(); // Reset total # of SIPS with contents of ZIP $o_progress->setTotal((int) $o_progress->getTotal() - 1 + sizeof($va_archive_files)); foreach ($va_archive_files as $vs_archive_file) { $vs_file_in_zip = str_replace($vb_is_dir ? "{$vs_file}" : "phar://{$vs_file}/{$vs_idno}", "", $vs_archive_file); $va_tmp = explode("/", $vs_file_in_zip); switch ($va_tmp[1]) { case 'sides': if ($va_tmp[4]) { $vs_ext = pathinfo($va_tmp[4], PATHINFO_EXTENSION); switch ($vs_ext) { case 'mp3': $va_track_audio_by_side[$va_tmp[2]][] = ($vb_is_dir ? "{$vs_file}/sides/" : "phar://{$vs_file}/{$vs_idno}/sides/") . $va_tmp[2] . "/" . $va_tmp[3] . "/" . $va_tmp[4]; break; case 'xml': $va_track_xml_by_side[$va_tmp[2]][] = ($vb_is_dir ? "{$vs_file}/sides/" : "phar://{$vs_file}/{$vs_idno}/sides/") . $va_tmp[2] . "/" . $va_tmp[3] . "/" . $va_tmp[4]; break; } } else { if ($va_tmp[2]) { $vs_ext = pathinfo($va_tmp[3], PATHINFO_EXTENSION); switch ($vs_ext) { case 'mp3': $va_side_audio[$va_tmp[2]] = $va_tmp[3]; break; case 'xml': if (preg_match('!meta!', $va_tmp[3])) { $va_side_xml[$va_tmp[2]] = ($vb_is_dir ? "{$vs_file}/sides/" : "phar://{$vs_file}/{$vs_idno}/sides/") . $va_tmp[2] . "/" . $va_tmp[3]; } break; } } } break; case 'artifacts': if (sizeof($va_tmp) == 3) { $va_artifacts[] = ($vb_is_dir ? "{$vs_file}/artifacts/" : "phar://{$vs_file}/{$vs_idno}/artifacts/") . $va_tmp[2]; } break; } } //print "[6] ".$t->getTime(4)."\n"; //print_R($va_side_xml); die; // Process // Create parent record $vn_image_count = 0; $o_progress->next(_t('Processing %1', $vs_archive_file)); $o_progress->setMessage(_t("Creating reel for %1", $vs_idno)); $va_ids = ca_objects::find(array('idno' => $vs_idno, 'deleted' => 0, 'type_id' => 'reel'), array('returnAs' => 'ids')); if (!is_array($va_ids) || !sizeof($va_ids)) { $t_object = new ca_objects(); $t_object->setMode(ACCESS_WRITE); $t_object->set(array('status' => 5, 'type_id' => 'reel', 'idno' => $vs_idno)); $vn_object_id = $t_object->insert(); if ($t_object->numErrors()) { if ($o_log) { $o_log->logError(_t("Could not add reel record %1: %2", $vs_idno, join("; ", $t_object->getErrors()))); } } $t_object->addLabel(array('name' => $vs_idno_padded), $pn_locale_id, null, true); if ($t_object->numErrors()) { if ($o_log) { $o_log->logError(_t("Could not add label to reel record %1: %2", $vs_idno, join("; ", $t_object->getErrors()))); } } if ($t_object->numErrors()) { if ($o_log) { $o_log->logError(_t("Could not add artifct media %1 to reel %2: %3", pathinfo($vs_artifact, PATHINFO_BASENAME), $vs_idno, join("; ", $t_object->getErrors()))); } } } else { if ($o_log) { $o_log->logDebug(_t("Found existing reel record %1 for %2", $va_ids[0], $vs_idno)); } $t_object = new ca_objects($vn_object_id = $va_ids[0]); $t_object->setMode(ACCESS_WRITE); $t_object->set('status', 5); $t_object->update(); if (($vn_image_count = $t_object->numberOfRepresentationsOfClass("image")) > 0) { // skip reels that have images already //if ($o_log) { $o_log->logDebug(_t("Skipped existing reel record %1 because it already has %2 images", $vs_idno, $vn_image_count)); } //if ($o_progress) { $o_progress->setError(_t("Skipped existing reel record %1 because it already has %2 images", $vs_idno, $vn_image_count)); } //continue; } $t_object->setMode(ACCESS_WRITE); } //print "[7] ".$t->getTime(4)."\n"; if ($vn_image_count > 0) { $t_object->removeAllRepresentations(); if ($t_object->numErrors()) { if ($o_log) { $o_log->logError(_t("Could not add remove existing media from reel %1: %2", $vs_idno, join("; ", $t_object->getErrors()))); } } } $o_progress->setMessage(_t("Linking artifact images for %1", $vs_idno)); foreach ($va_artifacts as $vs_artifact) { $o_progress->next(_t('Processing artifact image %1', $vs_artifact)); copy($vs_artifact, $vs_tmp_filepath = "/tmp/pbc" . md5(time())); $t_object->addRepresentation($vs_tmp_filepath, 'front', $pn_locale_id, 0, 1, 1); $o_progress->setMessage(_t("Added artifact image %1 for %2", $vs_artifact, $vs_idno)); if ($t_object->numErrors()) { if ($o_log) { $o_log->logError(_t("Could not add artifact media %1 to reel %2: %3", pathinfo($vs_artifact, PATHINFO_BASENAME), $vs_idno, join("; ", $t_object->getErrors()))); } } // remove tmp file @unlink($vs_tmp_filepath); } // // Add XML // $o_progress->setMessage(_t("Adding XML for %1", $vs_track_idno)); $t_object->removeAttributes('sip_metadata'); foreach ($va_side_xml as $vs_side => $vs_xml_path) { copy($vs_xml_path, $vs_xml_tmppath = "/tmp/" . pathinfo($vs_xml_path, PATHINFO_BASENAME)); $o_progress->next(_t('Processing XML %1', $vs_xml_tmppath)); $t_object->addAttribute(array('sip_metadata' => $vs_xml_tmppath, 'locale_id' => $pn_locale_id), 'sip_metadata'); $t_object->update(); if ($t_object->numErrors()) { if ($o_log) { $o_log->logError(_t("Could not import XML file %1 into reel %2: %3", pathinfo($va_track_xml_by_side[$vs_side][$vn_i], PATHINFO_BASENAME), $vs_track_idno, join("; ", $t_object->getErrors()))); } } } //print "[8] ".$t->getTime(4)."\n"; // Create tracks $o_progress->setMessage(_t("Creating tracks for %1", $vs_idno)); foreach ($va_track_audio_by_side as $vs_side => $va_tracks) { foreach ($va_tracks as $vn_i => $vs_audio) { $o_progress->next(_t('Processing track %1', $vs_audio)); $vs_ext = pathinfo($vs_audio, PATHINFO_EXTENSION); copy($vs_audio, $vs_tmp_filepath = "/tmp/pbc" . md5(time()) . ".{$vs_ext}"); $vs_track_idno = pathinfo($vs_audio, PATHINFO_FILENAME); // Does track already exist? $va_track_ids = ca_objects::find(array('idno' => $vs_track_idno, 'deleted' => 0), array('returnAs' => 'ids')); if (!is_array($va_track_ids) || !sizeof($va_track_ids)) { $o_progress->setMessage(_t("Creating %2 track for %1", $vs_track_idno, $vs_category)); // Create track record $t_track = new ca_objects(); $t_track->setMode(ACCESS_WRITE); $va_tmp = explode("/", $vs_audio); $vs_category = strtolower(str_replace(' ', '_', $vs_category)); $t_track->set(array('type_id' => $vs_category, 'idno' => $vs_track_idno, 'parent_id' => $vn_object_id)); $vn_track_id = $t_track->insert(); if ($t_track->numErrors()) { if ($o_log) { $o_log->logError(_t("Could not add track %1: %2", $vs_track_idno, join("; ", $t_track->getErrors()))); } } $t_track->addLabel(array('name' => "{$vs_track_idno}"), $pn_locale_id, null, true); if ($t_track->numErrors()) { if ($o_log) { $o_log->logError(_t("Could not add label to track %1: %2", $vs_track_idno, join("; ", $t_track->getErrors()))); } } } else { if ($o_log) { $o_log->logDebug(_t("Found existing track record %1 for %2", $va_track_ids[0], $vs_track_idno)); } $t_track = new ca_objects($va_track_ids[0]); if (($vn_audio_count = $t_track->numberOfRepresentationsOfClass("audio")) > 0) { // skip tracks that have audio already //if ($o_log) { $o_log->logDebug(_t("Skip existing track record %1 because it already has %2 audio files", $vs_track_idno, $vn_audio_count)); } //continue; $t_track->removeAllRepresentations(); } } // // Add track audio // $o_progress->setMessage(_t("Adding track audio for %1", $vs_track_idno)); $t_track->addRepresentation($vs_tmp_filepath, 'front', $pn_locale_id, 0, 1, 1); if ($t_track->numErrors()) { if ($o_log) { $o_log->logError($vs_err_msg = _t("Could not import audio file %1 into track %2: %3", pathinfo($vs_audio, PATHINFO_BASENAME), $vs_track_idno, join("; ", $t_track->getErrors()))); } if ($o_progress) { $o_progress->setError($vs_err_msg); } } // Remove tmp files @unlink($vs_tmp_filepath); @unlink($vs_xml_tmppath); } } //print "[9] ".$t->getTime(4)."\n"; //} if ((bool) $this->getSetting('delete_after_import')) { $o_progress->setMessage(_t("Deleting SIP %1", $vs_file)); if ($vb_is_dir) { caRemoveDirectory($vs_file); } else { @unlink($vs_file); } } } //print "[10] ".$t->getTime(4)."\n"; $o_progress->finish("Completed processing"); if ($o_log) { $o_log->logDebug(_t("Ended SIP import")); } //print "DONE\n\n"; return true; }