Пример #1
0
 /** 
  * Returns place_id for the place with the specified name, regardless of specified type. If the place does not already 
  * exist then it will be created with the specified name, type and locale, as well as with any specified values in the $pa_values array.
  * $pa_values keys should be either valid place fields or attributes.
  *
  * @param string $ps_place_name Place label name
  * @param int $pn_parent_id The parent_id of the place; must be set to a non-null value
  * @param int $pn_type_id The type_id of the place type to use if the place needs to be created
  * @param int $pn_locale_id The locale_id to use if the place needs to be created (will be used for both the place locale as well as the label locale)
  * @param array $pa_values An optional array of additional values to populate newly created place records with. These values are *only* used for newly created places; they will not be applied if the place named already exists. The array keys should be names of ca_places fields or valid entity attributes. Values should be either a scalar (for single-value attributes) or an array of values for (multi-valued attributes)
  * @param array $pa_options An optional array of options, which include:
  *				outputErrors - if true, errors will be printed to console [default=false]
  *				matchOnIdno - try to match on idno if name match fails [default=false]
  *				dontCreate - if true then new entities will not be created [default=false]
  * 				transaction - if Transaction object is passed, use it for all Db-related tasks [default=null]
  *				returnInstance = return ca_places instance rather than place_id. Default is false. 
  *				generateIdnoWithTemplate = A template to use when setting the idno. The template is a value with automatically-set SERIAL values replaced with % characters. Eg. 2012.% will set the created row's idno value to 2012.121 (assuming that 121 is the next number in the serial sequence.) The template is NOT used if idno is passed explicitly as a value in $pa_values.
  *				importEvent = if ca_data_import_events instance is passed then the insert/update of the place will be logged as part of the import
  *				importEventSource = if importEvent is passed, then the value set for importEventSource is used in the import event log as the data source. If omitted a default value of "?" is used
  *				log = if KLogger instance is passed then actions will be logged
  */
 static function getPlaceID($ps_place_name, $pn_parent_id, $pn_type_id, $pn_locale_id, $pa_values = null, $pa_options = null)
 {
     if (!is_array($pa_options)) {
         $pa_options = array();
     }
     if (!isset($pa_options['outputErrors'])) {
         $pa_options['outputErrors'] = false;
     }
     $pb_match_on_idno = caGetOption('matchOnIdno', $pa_options, false);
     $t_place = new ca_places();
     if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
         $t_place->setTransaction($pa_options['transaction']);
     }
     $o_event = isset($pa_options['importEvent']) && $pa_options['importEvent'] instanceof ca_data_import_events ? $pa_options['importEvent'] : null;
     $vs_event_source = isset($pa_options['importEventSource']) && $pa_options['importEventSource'] ? $pa_options['importEventSource'] : "?";
     $o_log = isset($pa_options['log']) && $pa_options['log'] instanceof KLogger ? $pa_options['log'] : null;
     if (!($vs_idno = isset($pa_values['idno']) ? (string) $pa_values['idno'] : null)) {
         if (isset($pa_options['generateIdnoWithTemplate']) && $pa_options['generateIdnoWithTemplate']) {
             $vs_idno = $t_place->setIdnoTWithTemplate($pa_options['generateIdnoWithTemplate'], array('dontSetValue' => true));
         }
     }
     $va_find_arr = array();
     if ($pn_type_id) {
         $va_find_arr['type_id'] = $pn_type_id;
     }
     if ($pn_parent_id) {
         $va_find_arr['parent_id'] = $pn_parent_id;
     }
     if (!($vn_id = ca_places::find(array_merge(array('preferred_labels' => array('name' => $ps_place_name)), $va_find_arr), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction'])))) {
         if ($pb_match_on_idno && $vs_idno) {
             $va_find_arr['idno'] = $vs_idno;
             $vn_id = ca_places::find($va_find_arr, array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']));
         }
     }
     if (!$vn_id) {
         if (isset($pa_options['dontCreate']) && $pa_options['dontCreate']) {
             return false;
         }
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_places', 'I');
         }
         $t_place->setMode(ACCESS_WRITE);
         $t_place->set('locale_id', $pn_locale_id);
         $t_place->set('type_id', $pn_type_id);
         $t_place->set('parent_id', $pn_parent_id);
         $t_place->set('source_id', isset($pa_values['source_id']) ? $pa_values['source_id'] : null);
         $t_place->set('access', isset($pa_values['access']) ? $pa_values['access'] : 0);
         $t_place->set('status', isset($pa_values['status']) ? $pa_values['status'] : 0);
         $t_place->set('idno', $vs_idno);
         $t_place->set('lifespan', isset($pa_values['lifespan']) ? $pa_values['lifespan'] : null);
         $t_place->set('hierarchy_id', isset($pa_values['hierarchy_id']) ? $pa_values['hierarchy_id'] : null);
         $t_place->insert();
         if ($t_place->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not insert place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not insert place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())));
             }
             return null;
         }
         $vb_label_errors = false;
         $t_place->addLabel(array('name' => $ps_place_name), $pn_locale_id, null, true);
         if ($t_place->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not set preferred label for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not set preferred label for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())));
             }
             $vb_label_errors = true;
         }
         unset($pa_values['access']);
         unset($pa_values['status']);
         unset($pa_values['idno']);
         unset($pa_values['source_id']);
         unset($pa_values['lifespan']);
         unset($pa_values['hierarchy_id']);
         $vb_attr_errors = false;
         if (is_array($pa_values)) {
             foreach ($pa_values as $vs_element => $va_value) {
                 if (is_array($va_value)) {
                     // array of values (complex multi-valued attribute)
                     $t_place->addAttribute(array_merge($va_value, array('locale_id' => $pn_locale_id)), $vs_element);
                 } else {
                     // scalar value (simple single value attribute)
                     if ($va_value) {
                         $t_place->addAttribute(array('locale_id' => $pn_locale_id, $vs_element => $va_value), $vs_element);
                     }
                 }
             }
         }
         $t_place->insert();
         if ($t_place->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not set values for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not set values for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())));
             }
             $vb_attr_errors = true;
         }
         $vn_place_id = $t_place->getPrimaryKey();
         if ($o_event) {
             if ($vb_attr_errors || $vb_label_errors) {
                 $o_event->endItem($vn_place_id, __CA_DATA_IMPORT_ITEM_PARTIAL_SUCCESS__, _t("Errors setting field values: %1", join('; ', $t_place->getErrors())));
             } else {
                 $o_event->endItem($vn_place_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
             }
         }
         if ($o_log) {
             $o_log->logInfo(_t("Created new place %1", $ps_place_name));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             return $t_place;
         }
     } else {
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_places', 'U');
         }
         $vn_place_id = $vn_id;
         if ($o_event) {
             $o_event->endItem($vn_place_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
         }
         if ($o_log) {
             $o_log->logDebug(_t("Found existing place %1 in DataMigrationUtils::getPlaceID(); total of %2 places were found", $ps_place_name, sizeof($va_place_ids) + 1));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             return new ca_places($vn_place_id);
         }
     }
     return $vn_place_id;
 }
Пример #2
0
 print str_repeat(chr(8), $vn_last_message_length);
 $vs_message = "[Notice] IMPORTING #" . ($vn_term_count + 1) . " [" . $va_subject['subject_id'] . "] " . $vs_preferred_term;
 if (($vn_l = 100 - strlen($vs_message)) < 1) {
     $vn_l = 1;
 }
 $vs_message .= str_repeat(' ', $vn_l);
 $vn_last_message_length = strlen($vs_message);
 print $vs_message;
 $t_place->clear();
 $t_place->set('parent_id', null);
 $t_place->set('type_id', $vn_type_id);
 $t_place->set('idno', $va_subject['subject_id']);
 $t_place->set('hierarchy_id', $vn_tgn_id);
 // Add description
 if ($vs_description_element_code && $va_subject['description']) {
     $t_place->addAttribute(array($vs_description_element_code => $va_subject['description'], 'locale_id' => $pn_en_locale_id), $vs_description_element_code);
 }
 // Add georeference
 if ($vs_georef_element_code && ($va_coords['latitude']['decimal'] && $va_coords['longitude']['decimal'])) {
     $t_place->addAttribute(array($vs_georef_element_code => "[" . $va_coords['latitude']['decimal'] . $va_coords['latitude']['direction'] . "," . $va_coords['longitude']['decimal'] . $va_coords['longitude']['direction'] . "]", 'locale_id' => $pn_en_locale_id), $vs_georef_element_code);
     DataMigrationUtils::postError($t_place, "[Error] While adding georeference to place");
 }
 if ($vn_place_id = $t_place->insert(array('dontSetHierarchicalIndexing' => true))) {
     if (!$t_place->addLabel(array('name' => $vs_preferred_term, 'description' => ''), $pn_en_locale_id, null, true)) {
         print "[Error] Could not add preferred label to TGN term [" . $va_subject['subject_id'] . "] " . $vs_preferred_term . ": " . join("; ", $t_place->getErrors()) . "\n";
     }
     // add alternate labels
     if (is_array($va_subject['non_preferred_terms'])) {
         for ($vn_i = 0; $vn_i < sizeof($va_subject['non_preferred_terms']); $vn_i++) {
             $vs_np_label = $va_subject['non_preferred_terms'][$vn_i];
             $vs_np_term_type = $va_subject['non_preferred_term_types'][$vn_i];
Пример #3
0
 /**
  * Returns place_id for the place with the specified name (and type) or idno (regardless of specified type.) If the place does not already
  * exist then it will be created with the specified name, type and locale, as well as with any specified values in the $pa_values array.
  * $pa_values keys should be either valid place fields or attributes.
  *
  * @param string $ps_place_name Place label name
  * @param int $pn_parent_id The parent_id of the place; must be set to a non-null value
  * @param int $pn_type_id The type_id of the place type to use if the place needs to be created
  * @param int $pn_locale_id The locale_id to use if the place needs to be created (will be used for both the place locale as well as the label locale)
  * @param array $pa_values An optional array of additional values to populate newly created place records with. These values are *only* used for newly created places; they will not be applied if the place named already exists. The array keys should be names of ca_places fields or valid entity attributes. Values should be either a scalar (for single-value attributes) or an array of values for (multi-valued attributes)
  * @param array $pa_options An optional array of options, which include:
  *                outputErrors - if true, errors will be printed to console [default=false]
  *                matchOn = optional list indicating sequence of checks for an existing record; values of array can be "label" and "idno". Ex. array("idno", "label") will first try to match on idno and then label if the first match fails.
  *                dontCreate - if true then new places will not be created [default=false]
  *                transaction - if Transaction object is passed, use it for all Db-related tasks [default=null]
  *                returnInstance = return ca_places instance rather than place_id. Default is false.
  *                generateIdnoWithTemplate = A template to use when setting the idno. The template is a value with automatically-set SERIAL values replaced with % characters. Eg. 2012.% will set the created row's idno value to 2012.121 (assuming that 121 is the next number in the serial sequence.) The template is NOT used if idno is passed explicitly as a value in $pa_values.
  *                importEvent = if ca_data_import_events instance is passed then the insert/update of the place will be logged as part of the import
  *                importEventSource = if importEvent is passed, then the value set for importEventSource is used in the import event log as the data source. If omitted a default value of "?" is used
  *                nonPreferredLabels = an optional array of nonpreferred labels to add to any newly created places. Each label in the array is an array with required place label values.
  *                log = if KLogger instance is passed then actions will be logged
  * @return bool|\ca_places|mixed|null
  */
 static function getPlaceID($ps_place_name, $pn_parent_id, $pn_type_id, $pn_locale_id, $pa_values = null, $pa_options = null)
 {
     if (!is_array($pa_options)) {
         $pa_options = array();
     }
     if (!isset($pa_options['outputErrors'])) {
         $pa_options['outputErrors'] = false;
     }
     $pa_match_on = caGetOption('matchOn', $pa_options, array('label', 'idno'), array('castTo' => "array"));
     /** @var ca_data_import_events $o_event */
     $o_event = isset($pa_options['importEvent']) && $pa_options['importEvent'] instanceof ca_data_import_events ? $pa_options['importEvent'] : null;
     $t_place = new ca_places();
     if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
         $t_place->setTransaction($pa_options['transaction']);
         if ($o_event) {
             $o_event->setTransaction($pa_options['transaction']);
         }
     }
     $vs_event_source = isset($pa_options['importEventSource']) && $pa_options['importEventSource'] ? $pa_options['importEventSource'] : "?";
     /** @var KLogger $o_log */
     $o_log = isset($pa_options['log']) && $pa_options['log'] instanceof KLogger ? $pa_options['log'] : null;
     $vs_idno = isset($pa_values['idno']) ? (string) $pa_values['idno'] : null;
     if (preg_match('!\\%!', $vs_idno)) {
         $pa_options['generateIdnoWithTemplate'] = $vs_idno;
         $vs_idno = null;
     }
     if (!$vs_idno) {
         if (isset($pa_options['generateIdnoWithTemplate']) && $pa_options['generateIdnoWithTemplate']) {
             $vs_idno = $t_place->setIdnoWithTemplate($pa_options['generateIdnoWithTemplate'], array('dontSetValue' => true));
         }
     }
     $vn_id = null;
     foreach ($pa_match_on as $vs_match_on) {
         switch (strtolower($vs_match_on)) {
             case 'label':
             case 'labels':
                 if (trim($ps_place_name)) {
                     if ($vn_id = ca_places::find(array('preferred_labels' => array('name' => $ps_place_name), 'type_id' => $pn_type_id, 'parent_id' => $pn_parent_id), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']))) {
                         break 2;
                     }
                     break;
                 }
             case 'idno':
                 if ($vs_idno == '%') {
                     break;
                 }
                 // don't try to match on an unreplaced idno placeholder
                 if ($vn_id = ca_places::find(array('idno' => $vs_idno ? $vs_idno : $ps_place_name), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']))) {
                     break 2;
                 }
                 break;
         }
     }
     if (!$vn_id) {
         if (isset($pa_options['dontCreate']) && $pa_options['dontCreate']) {
             return false;
         }
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_places', 'I');
         }
         $t_place->setMode(ACCESS_WRITE);
         $t_place->set('locale_id', $pn_locale_id);
         $t_place->set('type_id', $pn_type_id);
         $t_place->set('parent_id', $pn_parent_id);
         $t_place->set('source_id', isset($pa_values['source_id']) ? $pa_values['source_id'] : null);
         $t_place->set('access', isset($pa_values['access']) ? $pa_values['access'] : 0);
         $t_place->set('status', isset($pa_values['status']) ? $pa_values['status'] : 0);
         $t_place->set('idno', $vs_idno);
         $t_place->set('lifespan', isset($pa_values['lifespan']) ? $pa_values['lifespan'] : null);
         $t_place->set('hierarchy_id', isset($pa_values['hierarchy_id']) ? $pa_values['hierarchy_id'] : null);
         $t_place->insert();
         if ($t_place->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not insert place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not insert place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())));
             }
             return null;
         }
         $vb_label_errors = false;
         $t_place->addLabel(array('name' => $ps_place_name), $pn_locale_id, null, true);
         if ($t_place->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not set preferred label for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not set preferred label for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())));
             }
             $vb_label_errors = true;
         }
         unset($pa_values['access']);
         unset($pa_values['status']);
         unset($pa_values['idno']);
         unset($pa_values['source_id']);
         unset($pa_values['lifespan']);
         unset($pa_values['hierarchy_id']);
         $vb_attr_errors = false;
         if (is_array($pa_values)) {
             foreach ($pa_values as $vs_element => $va_values) {
                 if (!caIsIndexedArray($va_values)) {
                     $va_values = array($va_values);
                 }
                 foreach ($va_values as $va_value) {
                     if (is_array($va_value)) {
                         // array of values (complex multi-valued attribute)
                         $t_place->addAttribute(array_merge($va_value, array('locale_id' => $pn_locale_id)), $vs_element);
                     } else {
                         // scalar value (simple single value attribute)
                         if ($va_value) {
                             $t_place->addAttribute(array('locale_id' => $pn_locale_id, $vs_element => $va_value), $vs_element);
                         }
                     }
                 }
             }
             $t_place->update();
             if ($t_place->numErrors()) {
                 if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                     print "[Error] " . _t("Could not set values for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())) . "\n";
                 }
                 if ($o_log) {
                     $o_log->logError(_t("Could not set values for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())));
                 }
                 $vb_attr_errors = true;
             }
         }
         /** @var IIDNumbering $o_idno */
         if ($o_idno = $t_place->getIDNoPlugInInstance()) {
             $va_values = $o_idno->htmlFormValuesAsArray('idno', $vs_idno);
             if (!is_array($va_values)) {
                 $va_values = array($va_values);
             }
             if (!($vs_sep = $o_idno->getSeparator())) {
                 $vs_sep = '';
             }
             if (($vs_proc_idno = join($vs_sep, $va_values)) && $vs_proc_idno != $vs_idno) {
                 $t_place->set('idno', $vs_proc_idno);
                 $t_place->update();
                 if ($t_place->numErrors()) {
                     if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                         print "[Error] " . _t("Could not update idno for %1: %2", $ps_place_name, join('; ', $t_place->getErrors())) . "\n";
                     }
                     if ($o_log) {
                         $o_log->logError(_t("Could not idno for %1: %2", $ps_place_name, join('; ', $t_place->getErrors())));
                     }
                     return null;
                 }
             }
         }
         if (is_array($va_nonpreferred_labels = caGetOption("nonPreferredLabels", $pa_options, null))) {
             if (caIsAssociativeArray($va_nonpreferred_labels)) {
                 // single non-preferred label
                 $va_labels = array($va_nonpreferred_labels);
             } else {
                 // list of non-preferred labels
                 $va_labels = $va_nonpreferred_labels;
             }
             foreach ($va_labels as $va_label) {
                 $t_place->addLabel($va_label, $pn_locale_id, null, false);
                 if ($t_place->numErrors()) {
                     if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                         print "[Error] " . _t("Could not set non-preferred label for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())) . "\n";
                     }
                     if ($o_log) {
                         $o_log->logError(_t("Could not set non-preferred label for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())));
                     }
                 }
             }
         }
         $vn_place_id = $t_place->getPrimaryKey();
         if ($o_event) {
             if ($vb_attr_errors || $vb_label_errors) {
                 $o_event->endItem($vn_place_id, __CA_DATA_IMPORT_ITEM_PARTIAL_SUCCESS__, _t("Errors setting field values: %1", join('; ', $t_place->getErrors())));
             } else {
                 $o_event->endItem($vn_place_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
             }
         }
         if ($o_log) {
             $o_log->logInfo(_t("Created new place %1", $ps_place_name));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             return $t_place;
         }
     } else {
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_places', 'U');
         }
         $vn_place_id = $vn_id;
         if ($o_event) {
             $o_event->endItem($vn_place_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
         }
         if ($o_log) {
             $o_log->logDebug(_t("Found existing place %1 in DataMigrationUtils::getPlaceID()", $ps_place_name));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             $t_place = new ca_places($vn_place_id);
             if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
                 $t_place->setTransaction($pa_options['transaction']);
             }
             return $t_place;
         }
     }
     return $vn_place_id;
 }