public function dispatchLoopShutdown() { // // Force output to be sent - we need the client to have the page before // we start flushing progress bar updates // $app = AppController::getInstance(); $req = $app->getRequest(); $resp = $app->getResponse(); $resp->sendResponse(); $resp->clearContent(); // // Do reindexing // if ($req->isLoggedIn() && $req->user->canDoAction('can_do_search_reindex')) { set_time_limit(3600 * 8); $o_db = new Db(); $t_timer = new Timer(); $o_dm = Datamodel::load(); $va_table_names = $o_dm->getTableNames(); $vn_tc = 0; foreach ($va_table_names as $vs_table) { if ($o_instance = $o_dm->getInstanceByTableName($vs_table)) { if ($o_instance->isHierarchical()) { if (!$o_instance->rebuildAllHierarchicalIndexes()) { $o_instance->rebuildHierarchicalIndex(); } } caIncrementHierachicalReindexProgress(_t('Rebuilding hierarchical index for %1', $o_instance->getProperty('NAME_PLURAL')), $t_timer->getTime(2), memory_get_usage(true), $va_table_names, $o_instance->tableNum(), $o_instance->getProperty('NAME_PLURAL'), $vn_tc + 1); } $vn_tc++; } caIncrementHierachicalReindexProgress(_t('Index rebuild complete!'), $t_timer->getTime(2), memory_get_usage(true), $va_table_names, null, null, sizeof($va_table_names)); } }
function parse() { if ($this->doParseRSS) { require_once "include/domit_rss/timer.php"; $timer = new Timer(); $success = false; $timer->start(); if (!defined('DOMIT_RSS_INCLUDE_PATH')) { define('DOMIT_RSS_INCLUDE_PATH', dirname(__FILE__) . "/"); } switch ($this->rssparser) { case "domit_rss_lite": require_once DOMIT_RSS_INCLUDE_PATH . 'xml_domit_rss_lite.php'; $this->rssdoc = new xml_domit_rss_document_lite($this->rssurl); break; case "domit_rss": require_once DOMIT_RSS_INCLUDE_PATH . 'xml_domit_rss.php'; $this->rssdoc = new xml_domit_rss_document($this->rssurl); break; } // switch $timer->stop(); $this->displayNew(); echo "<br /><br />Time elapsed: " . $timer->getTime() . "seconds<br /><br />\n"; } }
function parse() { if ($this->xmlaction != null) { require_once "timer.php"; $timer = new Timer(); $success = false; $this->saxparser == "saxy" ? $parseSAXY = true : ($parseSAXY = false); $timer->start(); switch ($this->domparser) { case "domit": //change this to the domit path require_once 'xml_domit_parser.php'; $this->xmldoc =& new DOMIT_Document(); $this->xmldoc->expandEmptyElementTags(true); $this->xmldoc->setNamespaceAwareness(true); break; case "domitlite": //change this to the domit lite path require_once 'xml_domit_lite_parser.php'; $this->xmldoc =& new DOMIT_Lite_Document(); break; } // switch switch ($this->xmlaction) { case "parsefile": $success = $this->xmldoc->loadXML($this->xmlfile, $parseSAXY); break; case "parseurl": $success = $this->xmldoc->loadXML("http://" . $this->xmlurl, $parseSAXY); break; case "parsetext": $success = $this->xmldoc->parseXML($this->xmltext, $parseSAXY); break; } $timer->stop(); if ($success) { echo "<br /><br />Time elapsed: " . $timer->getTime() . "seconds<br /><br />\n"; if ($this->xmloutput == "tostring") { echo $this->xmldoc->toString(true); } else { if ($this->xmloutput == "tonormalizedstring") { echo $this->xmldoc->toNormalizedString(true); } else { if ($this->xmloutput == "toarray") { echo "<pre>\n"; print_r($this->xmldoc->toArray()); echo "</pre>\n"; } } } } else { echo "<br /><br />Parsing error: xml document may be invalid or malformed.\n"; } } }
the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Ocsinventoryng plugin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ocsinventoryng. If not, see <http://www.gnu.org/licenses/>. ---------------------------------------------------------------------------------------------------------------------------------------------------- */ // Ensure current directory when run from crontab chdir(dirname($_SERVER["SCRIPT_FILENAME"])); include '../../../inc/includes.php'; ini_set('display_errors', 1); restore_error_handler(); if (!isset($_SERVER['argv'][1])) { die("usage testsync.php <computerid>\n"); } $link = new PluginOcsinventoryngOcslink(); if (!$link->getFromDBforComputer($_SERVER['argv'][1])) { die("unknow computer\n"); } printf("Device: %s\n", $link->getField('ocs_deviceid')); $timer = new Timer(); $timer->start(); $prof = new XHProf("OCS sync"); PluginOcsinventoryngOcsServer::updateComputer($link->getID(), $link->getField('plugin_ocsinventoryng_ocsservers_id'), 1, 1); unset($prof); printf("Done in %s\n", $timer->getTime());
/** * Forces a full reindex of all rows in the database or, optionally, a single table * * @param array $pa_table_names * @param array $pa_options Reindexing options: * showProgress * interactiveProgressDisplay * log * callback * @return null|false */ public function reindex($pa_table_names = null, $pa_options = null) { define('__CollectiveAccess_IS_REINDEXING__', 1); $t_timer = new Timer(); $pb_display_progress = isset($pa_options['showProgress']) ? (bool) $pa_options['showProgress'] : true; $pb_interactive_display = isset($pa_options['interactiveProgressDisplay']) ? (bool) $pa_options['interactiveProgressDisplay'] : false; $ps_callback = isset($pa_options['callback']) ? (string) $pa_options['callback'] : false; if ($pa_table_names) { if (!is_array($pa_table_names)) { $pa_table_names = array($pa_table_names); } $va_table_names = array(); foreach ($pa_table_names as $vs_table) { if ($this->opo_datamodel->tableExists($vs_table)) { $vn_num = $this->opo_datamodel->getTableNum($vs_table); if ($pb_display_progress) { print "\nTRUNCATING {$vs_table}\n\n"; } $this->opo_engine->truncateIndex($vn_num); $t_instance = $this->opo_datamodel->getInstanceByTableName($vs_table, true); $va_table_names[$vn_num] = array('name' => $vs_table, 'num' => $vn_num, 'displayName' => $t_instance->getProperty('NAME_PLURAL')); } } if (!sizeof($va_table_names)) { return false; } } else { // full reindex $this->opo_engine->truncateIndex(); $va_table_names = $this->getIndexedTables(); } $o_db = $this->opo_db; if ($pb_display_progress || $ps_callback) { $va_names = array(); foreach ($va_table_names as $vn_table_num => $va_table_info) { $va_names[] = $va_table_info['displayName']; } if ($pb_display_progress) { print "\nWILL INDEX [" . join(", ", $va_names) . "]\n\n"; } } $vn_tc = 0; foreach ($va_table_names as $vn_table_num => $va_table_info) { $vs_table = $va_table_info['name']; $t_table_timer = new Timer(); $t_instance = $this->opo_datamodel->getInstanceByTableName($vs_table, true); $vs_table_pk = $t_instance->primaryKey(); $vn_table_num = $t_instance->tableNum(); $va_fields_to_index = $this->getFieldsToIndex($vn_table_num); if (!is_array($va_fields_to_index) || sizeof($va_fields_to_index) == 0) { continue; } $qr_all = $o_db->query("SELECT " . $t_instance->primaryKey() . " FROM {$vs_table}"); $vn_num_rows = $qr_all->numRows(); if ($pb_display_progress) { print CLIProgressBar::start($vn_num_rows, _t('Indexing %1', $t_instance->getProperty('NAME_PLURAL'))); } $vn_c = 0; $va_ids = $qr_all->getAllFieldValues($t_instance->primaryKey()); $va_element_ids = null; if (method_exists($t_instance, "getApplicableElementCodes")) { $va_element_ids = array_keys($t_instance->getApplicableElementCodes(null, false, false)); } $vn_table_num = $t_instance->tableNum(); $vs_table_pk = $t_instance->primaryKey(); $va_field_data = array(); $va_intrinsic_list = $this->getFieldsToIndex($vs_table, $vs_table, array('intrinsicOnly' => true)); $va_intrinsic_list[$vs_table_pk] = array(); foreach ($va_ids as $vn_i => $vn_id) { if (!($vn_i % 200)) { // Pre-load attribute values for next 200 items to index; improves index performance $va_id_slice = array_slice($va_ids, $vn_i, 200); if ($va_element_ids) { ca_attributes::prefetchAttributes($o_db, $vn_table_num, $va_id_slice, $va_element_ids); } $qr_field_data = $o_db->query("\n\t\t\t\t\t\tSELECT " . join(", ", array_keys($va_intrinsic_list)) . " \n\t\t\t\t\t\tFROM {$vs_table}\n\t\t\t\t\t\tWHERE {$vs_table_pk} IN (?)\t\n\t\t\t\t\t", array($va_id_slice)); $va_field_data = array(); while ($qr_field_data->nextRow()) { $va_field_data[(int) $qr_field_data->get($vs_table_pk)] = $qr_field_data->getRow(); } } $this->indexRow($vn_table_num, $vn_id, $va_field_data[$vn_id], true, null, array(), array()); if ($pb_display_progress && $pb_interactive_display) { CLIProgressBar::setMessage("Memory: " . caGetMemoryUsage()); print CLIProgressBar::next(); } if ($ps_callback && !($vn_c % 100)) { $ps_callback($vn_c, $vn_num_rows, null, null, (double) $t_timer->getTime(2), memory_get_usage(true), $va_table_names, $vn_table_num, $t_instance->getProperty('NAME_PLURAL'), $vn_tc + 1); } $vn_c++; } $qr_all->free(); unset($t_instance); if ($pb_display_progress && $pb_interactive_display) { print CLIProgressBar::finish(); } $this->opo_engine->optimizeIndex($vn_table_num); $vn_tc++; } if ($pb_display_progress) { print "\n\n\nDone! [Indexing for " . join(", ", $va_names) . " took " . caFormatInterval((double) $t_timer->getTime(4)) . "]\n"; print "Note that if you're using an external search service like Apache Solr, the data may only now be sent to the actual service because it was buffered until now. So you still might have to wait a while for the script to finish.\n"; } if ($ps_callback) { $ps_callback(1, 1, _t('Elapsed time: %1', caFormatInterval((double) $t_timer->getTime(2))), _t('Index rebuild complete!'), (double) $t_timer->getTime(2), memory_get_usage(true), $va_table_names, null, null, sizeof($va_table_names)); } }
/** * * * @return string */ function caLoadULAN($ps_path_to_ulan_data = null, $ps_path_to_ulan_config = null, $pa_options = null) { require_once __CA_LIB_DIR__ . '/core/Db.php'; require_once __CA_LIB_DIR__ . '/core/Configuration.php'; require_once __CA_LIB_DIR__ . '/ca/Utils/DataMigrationUtils.php'; require_once __CA_MODELS_DIR__ . '/ca_locales.php'; require_once __CA_MODELS_DIR__ . '/ca_entities.php'; require_once __CA_MODELS_DIR__ . '/ca_entities_x_entities.php'; require_once __CA_MODELS_DIR__ . '/ca_lists.php'; require_once __CA_MODELS_DIR__ . '/ca_list_items.php'; require_once __CA_MODELS_DIR__ . '/ca_list_items_x_list_items.php'; require_once __CA_MODELS_DIR__ . '/ca_relationship_types.php'; $t = new Timer(); $o_log = new KLogger(__CA_APP_DIR__ . '/log', KLogger::INFO); $va_parent_child_links = array(); $va_item_item_links = array(); $va_ulan_id_to_item_id = array(); $o_log->logInfo("Starting import of Getty ULAN"); define('__CA_DONT_DO_SEARCH_INDEXING__', true); $_ = new Zend_Translate('gettext', __CA_APP_DIR__ . '/locale/en_US/messages.mo', 'en_US'); $t_locale = new ca_locales(); $pn_en_locale_id = $t_locale->loadLocaleByCode('en_US'); if (!($o_config = Configuration::load($ps_path_to_ulan_config))) { $o_log->logError("Could not load ULAN import configuration file"); die("ERROR: Could not load ULAN import configuration\n"); } $vs_ulan_import_mode = $o_config->get('ulan_import_target'); $t_list = null; if ($vs_ulan_import_mode == 'ca_entities') { $va_ulan_types = $o_config->getAssoc('ulan_entity_types'); $va_mapping = $o_config->getAssoc('ulan_entity_mapping'); } elseif ($vs_ulan_import_mode == 'ca_list_items') { $va_ulan_types = $o_config->getAssoc('ulan_list_item_types'); if (!($vs_ulan_list_code = $o_config->get('ulan_import_list'))) { $vs_ulan_list_code = 'ULAN'; } // create vocabulary list record (if it doesn't exist already) $t_list = new ca_lists(); if (!$t_list->load(array('list_code' => $vs_ulan_list_code))) { $t_list->setMode(ACCESS_WRITE); $t_list->set('list_code', $vs_ulan_list_code); $t_list->set('is_system_list', 0); $t_list->set('is_hierarchical', 1); $t_list->set('use_as_vocabulary', 1); $t_list->insert(); if ($t_list->numErrors()) { $o_log->logError("Could not create list record for ULAN: " . join('; ', $t_list->getErrors())); die("ERROR: couldn't create ca_list row for ULAN: " . join('; ', $t_list->getErrors()) . "\n"); } $t_list->addLabel(array('name' => 'Union List of Artist Names'), $pn_en_locale_id, null, true); } $vn_list_id = $t_list->getPrimaryKey(); $va_mapping = $o_config->getAssoc('ulan_list_item_mapping'); } else { $o_log->logError("Invalid ULAN import mode {$vs_ulan_import_mode}"); die("ERROR: invalid ULAN import mode {$vs_ulan_import_mode}\n"); } $vn_last_message_length = 0; $vn_term_count = 0; $va_subject = array(); foreach (array('ULAN1.xml', 'ULAN2.xml', 'ULAN3.xml') as $vs_file) { if (!$ps_path_to_ulan_data) { $ps_path_to_ulan_data = "."; } if (!file_exists($ps_path_to_ulan_data . "/{$vs_file}")) { $o_log->logError("Could not find ULAN data file {$vs_file}"); print "[ERROR] cannot find ULAN data.\n"; continue; } $o_log->logInfo("Processing ULAN file {$vs_file}"); print "[Notice] Processing ULAN file {$vs_file}\n"; // load $o_xml = new XMLReader(); $o_xml->open($ps_path_to_ulan_data . '/' . $vs_file); while ($o_xml->read()) { switch ($o_xml->name) { # --------------------------- case 'Subject': if ($o_xml->nodeType == XMLReader::END_ELEMENT) { if (in_array($va_subject['subject_id'], array('500000000', '500000001'))) { break; } // skip top-level root $vs_preferred_term = $va_subject['preferred_term']; $pb_is_enabled = false; switch ($va_subject['record_type']) { case 'Person': default: $vn_type_id = $va_ulan_types['Person']; $pb_is_enabled = true; break; case 'Corporate Body': $vn_type_id = $va_ulan_types['Corporate Body']; $pb_is_enabled = true; break; } print str_repeat(chr(8), $vn_last_message_length); $vs_message = "\tIMPORTING #" . ($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; if ($vs_ulan_import_mode == 'ca_entities') { $va_np_labels = array(); if (is_array($va_subject['non_preferred_terms'])) { for ($vn_i = 0; $vn_i < sizeof($va_subject['non_preferred_terms']); $vn_i++) { $va_np_labels[] = DataMigrationUtils::splitEntityName(trim(htmlentities($va_subject['non_preferred_terms'][$vn_i]))); } } $t_item = DataMigrationUtils::getEntityID(DataMigrationUtils::splitEntityName(trim(htmlentities($vs_preferred_term, ENT_NOQUOTES))), $vn_type_id, $pn_en_locale_id, array('idno' => $va_subject['subject_id']), array('nonPreferredLabels' => $va_np_labels, 'returnInstance' => true)); if (!$t_item) { $o_log->logError("Failed to create entity for ULAN artist {$vs_preferred_term}"); break; } $t_item->setMode(ACCESS_WRITE); $va_ulan_id_to_item_id[$va_subject['subject_id']] = $t_item->getPrimaryKey(); } else { if ($t_item = $t_list->addItem($va_subject['subject_id'], $pb_is_enabled, false, null, $vn_type_id, $va_subject['subject_id'], '', 4, 1)) { $va_ulan_id_to_item_id[$va_subject['subject_id']] = $t_item->getPrimaryKey(); if ($va_subject['preferred_parent_subject_id'] != 500000000) { $va_parent_child_links[$va_subject['subject_id']] = $va_subject['preferred_parent_subject_id']; } // add preferred labels if (!$t_item->addLabel(array('name_singular' => trim(htmlentities($vs_preferred_term, ENT_NOQUOTES)), 'name_plural' => trim(htmlentities($vs_preferred_term, ENT_NOQUOTES)), 'description' => $va_subject['description']), $pn_en_locale_id, null, true)) { $o_log->logError("Could not add preferred label to ULAN term [" . $va_subject['subject_id'] . "] " . $vs_preferred_term . ": " . join("; ", $t_item->getErrors())); } // 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]; switch ($vs_np_term_type) { case 'Used For Term': $vn_np_term_type_id = $vn_list_item_label_type_uf; break; case 'Alternate Descriptor': $vn_np_term_type_id = $vn_list_item_label_type_alt; break; default: $vn_np_term_type_id = null; break; } if (!$t_item->addLabel(array('name_singular' => trim(htmlentities($vs_np_label, ENT_NOQUOTES)), 'name_plural' => trim(htmlentities($vs_np_label, ENT_NOQUOTES)), 'description' => ''), $pn_en_locale_id, $vn_np_term_type_id, false)) { $o_log->logError("Could not add non-preferred label to ULAN term [" . $va_subject['subject_id'] . "] " . $vs_np_label); } } } } else { $o_log->logError("Could not import ULAN term [" . $va_subject['subject_id'] . "] " . $vs_preferred_term . ": " . join("; ", $t_list->getErrors())); break; } } // Map content fields foreach ($va_mapping as $vs_dest => $vs_source) { $va_values = array(); switch ($vs_source) { case 'biography': if (!is_array($va_subject['biographies'])) { break; } foreach ($va_subject['biographies'] as $va_bio) { $va_values[] = $va_bio['text']; } break; case 'biography_dates': if (!is_array($va_subject['biographies'])) { break; } foreach ($va_subject['biographies'] as $va_bio) { if ($va_bio['birth_date'] == 1000 || $va_bio['birth_date'] < -5000) { if ($va_bio['death_date'] >= 2050) { break 2; } else { $va_values[] = "before " . $va_bio['death_date']; } } elseif ($va_bio['death_date'] >= 2050) { $va_values[] = "after " . $va_bio['birth_date']; } else { $va_values[] = $va_bio['birth_date'] . " - " . $va_bio['death_date']; } } break; case 'sex': if (!is_array($va_subject['biographies'])) { break; } foreach ($va_subject['biographies'] as $va_bio) { $va_values[] = $va_bio['sex']; } break; case 'nationality_name': if (!is_array($va_subject['nationalities'])) { break; } foreach ($va_subject['nationalities'] as $va_nationality) { $va_values[] = $va_nationality['name']; } break; case 'nationality_code': if (!is_array($va_subject['nationalities'])) { break; } foreach ($va_subject['nationalities'] as $va_nationality) { $va_values[] = $va_nationality['code']; } break; case 'role_name': if (!is_array($va_subject['roles'])) { break; } foreach ($va_subject['roles'] as $va_role) { $va_values[] = $va_role['name']; } break; case 'role_code': if (!is_array($va_subject['roles'])) { break; } foreach ($va_subject['roles'] as $va_role) { $va_values[] = $va_role['code']; } break; } if (sizeof($va_values)) { $va_dest = explode('.', $vs_dest); $vs_fld = array_pop($va_dest); if ($t_item->hasField($vs_fld)) { $t_item->set($vs_fld, join("\n", $va_values)); } else { foreach ($va_values as $vs_value) { $t_item->addAttribute(array('locale_id' => $pn_en_locale_id, $vs_fld => $vs_value), $vs_fld); } } $t_item->update(array('dontCheckCircularReferences' => true, 'dontSetHierarchicalIndexing' => true)); if ($t_item->numErrors()) { $o_log->logError("Could not update ULAN list item with content values: " . join("; ", $t_item->getErrors())); } } } // record item-item relations if (is_array($va_subject['related_subjects'])) { foreach ($va_subject['related_subjects'] as $vs_rel_subject_id) { $va_item_item_links[$va_subject['subject_id']] = $vs_rel_subject_id; } } $vn_term_count++; } else { $va_subject = array('subject_id' => $o_xml->getAttribute('Subject_ID')); } break; # --------------------------- # --------------------------- case 'Biographies': while ($o_xml->read()) { switch ($o_xml->name) { case 'Preferred_Biography': $va_bio = array(); while ($o_xml->read()) { switch ($o_xml->name) { case 'Biography_Text': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_bio['text'] = $o_xml->value; break; } break; case 'Birth_Date': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_bio['birth_date'] = $o_xml->value; if ($va_bio['birth_date'] < 0) { $va_bio['birth_date'] = abs($va_bio['birth_date']) . " BCE"; } break; } break; case 'Death_Date': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_bio['death_date'] = $o_xml->value; if ($va_bio['death_date'] < 0) { $va_bio['death_date'] = abs($va_bio['death_date']) . " BCE"; } break; } break; case 'Sex': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_bio['sex'] = $o_xml->value; break; } break; case 'Preferred_Biography': break 2; } } $va_subject['biographies'][] = $va_bio; break; case 'Biographies': break 2; } } break; # --------------------------- # --------------------------- case 'Nationalities': while ($o_xml->read()) { switch ($o_xml->name) { case 'Preferred_Nationality': $va_nationality = array(); while ($o_xml->read()) { switch ($o_xml->name) { case 'Nationality_Code': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_nationality['code'] = $o_xml->value; $va_nationality['name'] = array_pop(explode('/', $o_xml->value)); break; } break; case 'Preferred_Nationality': break 2; } } $va_subject['nationalities'][] = $va_nationality; break; case 'Nationalities': break 2; } } break; # --------------------------- # --------------------------- case 'Roles': while ($o_xml->read()) { switch ($o_xml->name) { case 'Preferred_Role': $va_role = array(); while ($o_xml->read()) { switch ($o_xml->name) { case 'Role_ID': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_role['code'] = $o_xml->value; $va_role['name'] = array_pop(explode('/', $o_xml->value)); break; } break; case 'Preferred_Role': break 2; } } $va_subject['roles'][] = $va_role; break; case 'Roles': break 2; } } break; # --------------------------- # --------------------------- case 'Record_Type': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['record_type'] = $o_xml->value; break; } break; # --------------------------- # --------------------------- case 'Hierarchy': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['hierarchy'] = $o_xml->value; break; } break; # --------------------------- # --------------------------- case 'Parent_Relationships': $vn_parent_id = $vs_historic_flag = null; while ($o_xml->read()) { switch ($o_xml->name) { case 'Preferred_Parent': while ($o_xml->read()) { switch ($o_xml->name) { case 'Parent_Subject_ID': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $vn_parent_id = $o_xml->value; break; } break; case 'Historic_Flag': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $vs_historic_flag = $o_xml->value; break; } break; case 'Preferred_Parent': $va_subject['preferred_parent_subject_id'] = $vn_parent_id; break 2; } } break; case 'Parent_Relationships': break 2; } } break; # --------------------------- # --------------------------- case 'Preferred_Term': while ($o_xml->read()) { switch ($o_xml->name) { case 'Term_Type': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['preferred_term_type'] = $o_xml->value; break; } break; case 'Term_Text': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['preferred_term'] = $o_xml->value; break; } break; case 'Term_ID': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['preferred_term_id'] = $o_xml->value; break; } break; break; case 'Preferred_Term': break 2; } } break; # --------------------------- # --------------------------- case 'Non-Preferred_Term': while ($o_xml->read()) { switch ($o_xml->name) { case 'Term_Type': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['non_preferred_term_types'][] = $o_xml->value; break; } break; case 'Term_Text': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['non_preferred_terms'][] = $o_xml->value; break; } break; case 'Term_ID': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['non_preferred_term_ids'][] = $o_xml->value; break; } break; case 'Non-Preferred_Term': break 2; } } break; # --------------------------- # --------------------------- case 'VP_Subject_ID': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['related_subjects'][] = $o_xml->value; break; } break; # --------------------------- } } $o_xml->close(); } $o_log->logInfo("Begin linking ULAN terms in hierarchy"); print "\n\nLINKING TERMS IN HIERARCHY...\n"; $vn_last_message_length = 0; $t_list = new ca_lists(); $t_item = new ca_list_items(); $t_item->setMode(ACCESS_WRITE); $vn_list_root_id = $t_list->getRootListItemID($vn_list_id); foreach ($va_parent_child_links as $vs_child_id => $vs_parent_id) { print str_repeat(chr(8), $vn_last_message_length); $vs_message = "\tLINKING {$vs_child_id} to parent {$vs_parent_id}"; 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; if (in_array($vs_parent_id, array('500000000', '500000001'))) { if (!$t_item->load($vn_child_item_id)) { $o_log->logError("Could not load item for {$vs_child_id} (was translated to item_id={$vn_child_item_id})"); continue; } $t_item->set('parent_id', $vn_list_root_id); $t_item->update(array('dontCheckCircularReferences' => true, 'dontSetHierarchicalIndexing' => true)); if ($t_item->numErrors()) { $o_log->logError("Could not set parent_id for {$vs_child_id} to root): " . join('; ', $t_item->getErrors())); continue; } $va_ulan_id_to_item_id[$vs_parent_id] = $vn_list_root_id; } if (!($vn_child_item_id = $va_ulan_id_to_item_id[$vs_child_id])) { $o_log->logError("No list item id for child_id {$vs_child_id} (were there previous errors?)"); continue; } if (!($vn_parent_item_id = $va_ulan_id_to_item_id[$vs_parent_id])) { $o_log->logError("No list item id for parent_id {$vs_parent_id} (were there previous errors?)"); continue; } if (!$t_item->load($vn_child_item_id)) { $o_log->logError("Could not load item for {$vs_child_id} (was translated to item_id={$vn_child_item_id})"); continue; } $t_item->set('parent_id', $vn_parent_item_id); $t_item->update(array('dontCheckCircularReferences' => true, 'dontSetHierarchicalIndexing' => true)); if ($t_item->numErrors()) { $o_log->logError("Could not set parent_id for {$vs_child_id} (was translated to item_id={$vn_child_item_id}): " . join('; ', $t_item->getErrors())); } } if ($vn_list_item_relation_type_id_related > 0) { $o_log->logInfo("Begin adding ULAN related term links"); $vn_last_message_length = 0; $t_item = new ca_list_items(); $t_link = new ca_list_items_x_list_items(); $t_link->setMode(ACCESS_WRITE); foreach ($va_item_item_links as $vs_left_id => $vs_right_id) { print str_repeat(chr(8), $vn_last_message_length); $vs_message = "\tLINKING {$vs_left_id} to {$vs_right_id}"; 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; if (!($vn_left_item_id = $va_ulan_id_to_item_id[$vs_left_id])) { $o_log->logError("No list item id for left_id {$vs_left_id} (were there previous errors?)"); continue; } if (!($vn_right_item_id = $va_ulan_id_to_item_id[$vs_right_id])) { $o_log->logError("No list item id for right_id {$vs_right_id} (were there previous errors?)"); continue; } $t_link->set('term_left_id', $vn_left_item_id); $t_link->set('term_right_id', $vn_right_item_id); $t_link->set('type_id', $vn_list_item_relation_type_id_related); $t_link->insert(); if ($t_link->numErrors()) { $o_log->logError("Could not set link between {$vs_left_id} (was translated to item_id={$vn_left_item_id}) and {$vs_right_id} (was translated to item_id={$vn_right_item_id}): " . join('; ', $t_link->getErrors())); } } } else { $o_log->logWarn("Skipped import of term-term relationships because the ca_list_items_x_list_items 'related' relationship type is not defined for your installation"); } $vn_duration = $t->getTime(1); $vs_time = caFormatInterval($vn_duration); $o_log->logInfo("Rebuilding hierarchical indices..."); $t_item->rebuildAllHierarchicalIndexes(); $o_log->logInfo("ULAN import complete. Took {$vs_time} ({$vn_duration})"); print "\n\nIMPORT COMPLETE. Took {$vs_time} ({$vn_duration})\n"; }
caIncrementProgress($vn_progress, "Creating logins"); $va_login_info = $vo_installer->processLogins(); $vn_progress += 7; caIncrementProgress($vn_progress, "Processing user interfaces"); $vo_installer->processUserInterfaces(); $vn_progress += 7; caIncrementProgress($vn_progress, "Processing displays"); $vo_installer->processDisplays(); $vn_progress += 7; caIncrementProgress($vn_progress, "Processing search forms"); $vo_installer->processSearchForms(); $vn_progress += 7; caIncrementProgress($vn_progress, "Setting up hierarchies"); $vo_installer->processMiscHierarchicalSetup(); caIncrementProgress(100, "Installation complete"); $vs_time = "(Installation took " . $t_total->getTime(0) . " seconds)"; if ($vo_installer->numErrors()) { $va_errors = array(); foreach ($vo_installer->getErrors() as $vs_err) { $va_errors[] = "<li>{$vs_err}</li>"; } caSetMessage("<div class='contentFailure'><div class='contentFailureHead'>There were errors during installation:</div><ul>" . join("", $va_errors) . "</ul><br/>{$vs_time}</div>"); } else { $vs_message = "<div class='contentSuccess'><span style='font-size:18px;'><b>Installation was successful!</b></span><br/>You can now <a href='../index.php?action=login'>login</a> with "; if (sizeof($va_login_info) == 1) { foreach ($va_login_info as $vs_user_name => $vs_password) { $vs_message .= "username <span class='contentHighlight'>{$vs_user_name}</span> and password <span class='contentHighlight'>{$vs_password}</span>.<br/><b>Make a note of this password!</b><br/>"; } } else { $vs_message .= " the following logins:<br/>"; foreach ($va_login_info as $vs_user_name => $vs_password) {
/** * Execute a MySQL query * * @param $query Query to execute * * @return Query result handler **/ function query($query) { global $CFG_GLPI, $DEBUG_SQL, $SQL_TOTAL_REQUEST; if ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE && $CFG_GLPI["debug_sql"]) { $SQL_TOTAL_REQUEST++; $DEBUG_SQL["queries"][$SQL_TOTAL_REQUEST] = $query; $TIMER = new Timer(); $TIMER->start(); } $res = @$this->dbh->query($query); if (!$res) { // no translation for error logs $error = " *** MySQL query error:\n SQL: " . addslashes($query) . "\n Error: " . $this->dbh->error . "\n"; $error .= toolbox::backtrace(false, 'DBmysql->query()', array('Toolbox::backtrace()')); Toolbox::logInFile("sql-errors", $error); if ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE && $CFG_GLPI["debug_sql"]) { $DEBUG_SQL["errors"][$SQL_TOTAL_REQUEST] = $this->error(); } } if ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE && $CFG_GLPI["debug_sql"]) { $TIME = $TIMER->getTime(); $DEBUG_SQL["times"][$SQL_TOTAL_REQUEST] = $TIME; } return $res; }
/** * Create a fresh installation of CollectiveAccess based on contents of setup.php. This is essentially a CLI * command wrapper for the installation process, as /install/inc/page2.php is a web wrapper. * @param Zend_Console_Getopt $po_opts * @param bool $pb_installing * @return bool */ public static function install($po_opts = null, $pb_installing = true) { require_once __CA_BASE_DIR__ . '/install/inc/Installer.php'; require_once __CA_BASE_DIR__ . '/install/inc/Updater.php'; if ($pb_installing && !$po_opts->getOption('profile-name')) { CLIUtils::addError(_t("Missing required parameter: profile-name")); return false; } if ($pb_installing && !$po_opts->getOption('admin-email')) { CLIUtils::addError(_t("Missing required parameter: admin-email")); return false; } $vs_profile_directory = $po_opts->getOption('profile-directory'); $vs_profile_directory = $vs_profile_directory ? $vs_profile_directory : __CA_BASE_DIR__ . '/install/profiles/xml'; $t_total = new Timer(); // If we are installing, then use Installer, otherwise use Updater $vo_installer = null; if ($pb_installing) { $vo_installer = new Installer($vs_profile_directory, $po_opts->getOption('profile-name'), $po_opts->getOption('admin-email'), $po_opts->getOption('overwrite'), $po_opts->getOption('debug')); } else { $vo_installer = new Updater($vs_profile_directory, $po_opts->getOption('profile-name'), null, false, $po_opts->getOption('debug')); } $vb_quiet = $po_opts->getOption('quiet'); // if profile validation against XSD failed, we already have an error here if ($vo_installer->numErrors()) { CLIUtils::addError(_t("There were errors parsing the profile(s): %1", "\n * " . join("\n * ", $vo_installer->getErrors()))); return false; } if ($pb_installing) { if (!$vb_quiet) { CLIUtils::addMessage(_t("Performing preinstall tasks")); } $vo_installer->performPreInstallTasks(); if (!$vb_quiet) { CLIUtils::addMessage(_t("Loading schema")); } $vo_installer->loadSchema(); if ($vo_installer->numErrors()) { CLIUtils::addError(_t("There were errors loading the database schema: %1", "\n * " . join("\n * ", $vo_installer->getErrors()))); return false; } } if (!$vb_quiet) { CLIUtils::addMessage(_t("Processing locales")); } $vo_installer->processLocales(); if (!$vb_quiet) { CLIUtils::addMessage(_t("Processing lists")); } $vo_installer->processLists(); if (!$vb_quiet) { CLIUtils::addMessage(_t("Processing relationship types")); } $vo_installer->processRelationshipTypes(); if (!$vb_quiet) { CLIUtils::addMessage(_t("Processing metadata elements")); } $vo_installer->processMetadataElements(); if (!$vb_quiet) { CLIUtils::addMessage(_t("Processing metadata dictionary")); } $vo_installer->processMetadataDictionary(); if (!$po_opts->getOption('skip-roles')) { if (!$vb_quiet) { CLIUtils::addMessage(_t("Processing access roles")); } $vo_installer->processRoles(); } if (!$vb_quiet) { CLIUtils::addMessage(_t("Processing user groups")); } $vo_installer->processGroups(); if (!$vb_quiet) { CLIUtils::addMessage(_t("Processing user logins")); } $va_login_info = $vo_installer->processLogins(); if (!$vb_quiet) { CLIUtils::addMessage(_t("Processing user interfaces")); } $vo_installer->processUserInterfaces(); if (!$vb_quiet) { CLIUtils::addMessage(_t("Processing displays")); } $vo_installer->processDisplays(); if (!$vb_quiet) { CLIUtils::addMessage(_t("Processing search forms")); } $vo_installer->processSearchForms(); if (!$vb_quiet) { CLIUtils::addMessage(_t("Setting up hierarchies")); } $vo_installer->processMiscHierarchicalSetup(); if (!$vb_quiet) { CLIUtils::addMessage(_t("Installation complete")); } $vs_time = _t("Installation took %1 seconds", $t_total->getTime(0)); if ($vo_installer->numErrors()) { CLIUtils::addError(_t("There were errors during installation: %1\n(%2)", "\n * " . join("\n * ", $vo_installer->getErrors()), $vs_time)); return false; } if ($pb_installing) { CLIUtils::addMessage(_t("Installation was successful!\n\nYou can now login with the following logins: %1\nMake a note of these passwords!", "\n * " . join("\n * ", array_map(function ($username, $password) { return _t("username %1 and password %2", $username, $password); }, array_keys($va_login_info), array_values($va_login_info))))); } else { CLIUtils::addMessage(_t("Update of installation profile successful")); } CLIUtils::addMessage($vs_time); return true; }
/** * Executes a SQL statement * * @param mixed $po_caller object representation of the calling class, usually Db() * @param DbStatement $opo_statement * @param string $ps_sql SQL statement * @param array $pa_values array of placeholder replacements */ public function execute($po_caller, $opo_statement, $ps_sql, $pa_values) { if (!$ps_sql) { $opo_statement->postError(240, _t("Query is empty"), "Db->pdo_mysql->execute()"); return false; } $va_placeholder_map = $po_caller->getOption('placeholder_map'); $vs_sql = $ps_sql; $va_proc_values = array(); $va_values_rev = array_reverse(array_keys($pa_values)); foreach ($va_values_rev as $vn_i) { if (is_array($pa_values[$vn_i])) { foreach ($pa_values[$vn_i] as $vn_x => $vs_vx) { $pa_values[$vn_i][$vn_x] = $this->autoQuote($vs_vx); } $vs_str = join(",", $pa_values[$vn_i]); $vs_sql = substr_replace($vs_sql, $vs_str, $va_placeholder_map[$vn_i], 1); continue; } $va_proc_values[] = $pa_values[$vn_i]; } if (sizeof($va_proc_values) != sizeof($pa_values)) { $pa_values = array_reverse($va_proc_values); $vo_dbstatement = $this->prepare($po_caller, $vs_sql); $opo_statement = $vo_dbstatement->getOption('native_statement'); } if (Db::$monitor) { $t = new Timer(); } if (!$opo_statement->execute(is_array($pa_values) && sizeof($pa_values) ? array_values($pa_values) : null)) { $va_err = $this->opr_db->errorCode(); $po_caller->postError($this->nativeToDbError($this->opr_db->errorCode()), $va_err[2] . (__CA_ENABLE_DEBUG_OUTPUT__ ? "\n<pre>" . caPrintStacktrace() . "</pre>" : ""), "Db->pdo_mysql->execute()"); return false; } if (Db::$monitor) { Db::$monitor->logQuery($vs_sql, $pa_values, $t->getTime(4), $opo_statement->rowCount()); } return new DbResult($this, $opo_statement); }
/** * Executes a SQL statement * * @param mixed $po_caller object representation of the calling class, usually Db() * @param PDOStatement $opo_statement * @param string $ps_sql SQL statement * @param array $pa_values array of placeholder replacements * @return bool|DbResult */ public function execute($po_caller, $opo_statement, $ps_sql, $pa_values) { if (!$ps_sql) { $po_caller->postError(240, _t("Query is empty"), "Db->pdo_mysql->execute()"); return false; } if (!$opo_statement instanceof PDOStatement) { $po_caller->postError(250, _t("Invalid prepared statement"), "Db->pdo_mysql->execute()"); return false; } if (!is_array($va_tmp = self::rewriteQueryAndParams($ps_sql, $pa_values))) { $po_caller->postError(250, _t("Query rewrite failed"), "Db->pdo_mysql->execute()"); return false; } if ($va_tmp['prepare']) { $opo_statement = $this->opr_db->prepare($va_tmp['sql']); } if (Db::$monitor) { $t = new Timer(); } try { $opo_statement->closeCursor(); $opo_statement->execute(is_array($va_tmp['values']) && sizeof($va_tmp['values']) ? array_values($va_tmp['values']) : null); } catch (PDOException $e) { $po_caller->postError($po_caller->nativeToDbError($this->opr_db->errorCode()), $e->getMessage() . (__CA_ENABLE_DEBUG_OUTPUT__ ? "\n<pre>" . caPrintStacktrace() . "\n{$ps_sql}</pre>" : ""), "Db->pdo_mysql->execute()"); return false; } if (Db::$monitor) { Db::$monitor->logQuery($ps_sql, $pa_values, $t->getTime(4), $opo_statement->rowCount()); } return new DbResult($this, $opo_statement); }
/** * Create each course in the moodle database * * @param type $courses */ function up_insert_courses($courses) { !empty($courses) or die('No courses to import!'); // counters for created and failed courses $courses_created = 0; $courses_failed = 0; // Start timer for script $ts_timer = new Timer(); $ts_timer->start(); $j = 0; // Start Counter // // cycle through each of the courses and get their information foreach ($courses as $data) { if (UP_DEBUG) { print "<br /><br />ROW: {$j}<br />"; print "COURSEID : " . $data->idnumber . '<br />'; print "SHORTNAME : " . $data->shortname . '<br />'; print "FULLNAME : " . $data->fullname . '<br />'; print "STARTDATE : " . $data->startdate . '<br />'; print "ENDDATE : " . $data->enddate . '<br />'; print "TERMCODE : " . $data->termcode . '<br />'; } $createstatus = up_insert_course($data); switch ($createstatus) { case COURSE_ALREADY_EXISTS: if (UP_DEBUG) { print "COURSE ALREADY EXISTS<br />"; } break; case COURSE_CREATION_FAILED: if (UP_DEBUG) { print "COURSE CREATION FAILED<br />"; $courses_failed++; } break; case COURSE_CREATION_SUCCEEDED: if (UP_DEBUG) { print "COURSE CREATION SUCCEDED<br />"; $courses_created++; } break; case true: print "Testing!"; break; } $j++; } $ts_timer->finish(); // end foreach( $results as $data ) // echo '<br />action took: ' . $ts_timer->getTime() . " seconds.<br />" . "<br /> Courses created: " . $courses_created . " | Courses failed: " . $courses_failed; }
$class = Logger::$_classes[$module]; // loop through module logging arguments //multiple args mean multiple charts like mysql or network modules foreach ($moduleSettings['logging']['args'] as $args) { $args = json_decode($args); // decode arguments $class->logfile = $logdir . $args->logfile; // the modules logfile si read from args //check for logdir if (isset($moduleSettings['module']['hasownlogdir']) && $moduleSettings['module']['hasownlogdir'] == "true") { $class->logdir = $args->logdir; // the modules logdir as read from args } //see if we are timing, if so set start time if ($timemode) { $st = $timer->getTime(); } // //run modules logger $responseData = $class->logData($logMode); // if API then collect data for API server if ($api) { //TODO: nead a way to deal with modules that return more than one dataset for api //this is for networking module if (is_array($responseData)) { $timestamp = ""; $dataInterface = ""; foreach ($responseData as $interface => $value) { //echo 'INT: ' . $interface . ' VAL: ' . $value . "\n"; $data = explode("|", $value); // parsing response data
/** * Executes a SQL statement * * @param mixed $po_caller object representation of the calling class, usually Db() * @param DbStatement $opo_statement * @param string $ps_sql SQL statement * @param array $pa_values array of placeholder replacements */ public function execute($po_caller, $opo_statement, $ps_sql, $pa_values) { if (!$ps_sql) { $opo_statement->postError(240, _t("Query is empty"), "Db->mysqli->execute()"); return false; } $vs_sql = $ps_sql; $va_placeholder_map = $opo_statement->getOption('placeholder_map'); $vn_needed_values = sizeof($va_placeholder_map); if ($vn_needed_values != sizeof($pa_values)) { $opo_statement->postError(285, _t("Number of values passed (%1) does not equal number of values required (%2)", sizeof($pa_values), $vn_needed_values), "Db->mysqli->execute()"); return false; } for ($vn_i = sizeof($pa_values) - 1; $vn_i >= 0; $vn_i--) { if (is_array($pa_values[$vn_i])) { foreach ($pa_values[$vn_i] as $vn_x => $vs_vx) { $pa_values[$vn_i][$vn_x] = $this->autoQuote($vs_vx); } $vs_sql = substr_replace($vs_sql, join(',', $pa_values[$vn_i]), $va_placeholder_map[$vn_i], 1); } else { $vs_sql = substr_replace($vs_sql, $this->autoQuote($pa_values[$vn_i]), $va_placeholder_map[$vn_i], 1); } } $va_limit_info = $opo_statement->getLimit(); if ($va_limit_info["limit"] > 0 || $va_limit_info["offset"] > 0) { if (!preg_match("/LIMIT[ ]+[\\d]+[,]{0,1}[\\d]*\$/i", $vs_sql)) { // check for LIMIT clause is raw SQL $vn_limit = $va_limit_info["limit"]; if ($vn_limit == 0) { $vn_limit = 4000000000; } $vs_sql .= " LIMIT " . intval($va_limit_info["offset"]) . "," . intval($vn_limit); } } if (Db::$monitor) { $t = new Timer(); } if (!($r_res = mysqli_query($this->opr_db, $vs_sql))) { //print "<pre>".caPrintStacktrace()."</pre>\n"; $opo_statement->postError($this->nativeToDbError(mysqli_errno($this->opr_db)), mysqli_error($this->opr_db), "Db->mysqli->execute()"); return false; } if (Db::$monitor) { Db::$monitor->logQuery($ps_sql, $pa_values, $t->getTime(4), is_bool($r_res) ? null : mysqli_num_rows($r_res)); } return new DbResult($this, $r_res); }
public function dispatchLoopShutdown() { // // Force output to be sent - we need the client to have the page before // we start flushing progress bar updates // $app = AppController::getInstance(); $req = $app->getRequest(); $resp = $app->getResponse(); $resp->sendResponse(); $resp->clearContent(); // // Do reindexing // if ($req->isLoggedIn() && $req->user->canDoAction('can_do_search_reindex')) { set_time_limit(3600 * 8); $o_db = new Db(); $t_timer = new Timer(); $va_table_names = array('ca_objects', 'ca_object_lots', 'ca_places', 'ca_entities', 'ca_occurrences', 'ca_collections', 'ca_storage_locations', 'ca_object_representations', 'ca_representation_annotations', 'ca_lists', 'ca_list_items', 'ca_loans', 'ca_movements', 'ca_tours', 'ca_tour_stops'); $vn_tc = 0; foreach ($va_table_names as $vs_table) { require_once __CA_MODELS_DIR__ . "/{$vs_table}.php"; $t_table = new $vs_table(); $vs_pk = $t_table->primaryKey(); $qr_res = $o_db->query("SELECT {$vs_pk} FROM {$vs_table}"); if ($vs_label_table_name = $t_table->getLabelTableName()) { require_once __CA_MODELS_DIR__ . "/{$vs_label_table_name}.php"; $va_table_names[] = $vs_label_table_name; $t_label = new $vs_label_table_name(); $vs_label_pk = $t_label->primaryKey(); $qr_labels = $o_db->query("SELECT {$vs_label_pk} FROM {$vs_label_table_name}"); $vn_c = 0; $vn_num_rows = $qr_labels->numRows(); $vn_table_num = $t_label->tableNum(); while ($qr_labels->nextRow()) { $vn_label_pk_val = $qr_labels->get($vs_label_pk); if (!($vn_c % 100)) { caIncrementSortValueReloadProgress($vn_c, $vn_num_rows, null, null, $t_timer->getTime(2), memory_get_usage(true), $va_table_names, $vn_table_num, $t_label->getProperty('NAME_PLURAL'), $vn_tc + 1); } if ($t_label->load($vn_label_pk_val)) { $t_label->setMode(ACCESS_WRITE); $t_label->update(); } $vn_c++; } $vn_tc++; } $vn_table_num = $t_table->tableNum(); $vn_num_rows = $qr_res->numRows(); $vn_c = 0; while ($qr_res->nextRow()) { $vn_pk_val = $qr_res->get($vs_pk); if (!($vn_c % 100)) { caIncrementSortValueReloadProgress($vn_c, $vn_num_rows, null, null, $t_timer->getTime(2), memory_get_usage(true), $va_table_names, $vn_table_num, $t_table->getProperty('NAME_PLURAL'), $vn_tc + 1); } if ($t_table->load($vn_pk_val)) { $t_table->setMode(ACCESS_WRITE); if ($vs_table == 'ca_object_representations') { $t_table->set('md5', $t_table->getMediaInfo('ca_object_representations.media', 'original', 'MD5')); $t_table->set('mimetype', $t_table->getMediaInfo('ca_object_representations.media', 'original', 'MIMETYPE')); $va_media_info = $t_table->getMediaInfo('ca_object_representations.media'); $t_table->set('original_filename', $va_media_info['ORIGINAL_FILENAME']); } $t_table->update(); if ($vs_table == 'ca_object_representations') { if (!$t_table->getPreferredLabelCount()) { $t_table->addLabel(array('name' => trim($va_media_info['ORIGINAL_FILENAME']) ? $va_media_info['ORIGINAL_FILENAME'] : _t('Representation')), $pn_locale_id, null, true); } } } $vn_c++; } $vn_tc++; } caIncrementSortValueReloadProgress(1, 1, _t('Elapsed time: %1', caFormatInterval($t_timer->getTime(2))), _t('Index rebuild complete!'), $t_timer->getTime(2), memory_get_usage(true), $va_table_names, null, null, sizeof($va_table_names)); } }
/** * Performs a search by calling the search() method on the underlying search engine plugin * Information about all searches is logged to ca_search_log * * @param string $ps_search The search to perform; engine takes Lucene syntax query * @param SearchResult $po_result A newly instantiated sub-class of SearchResult to place search results into and return. If this is not set, then a generic SearchResults object will be returned. * @param array $pa_options Optional array of options for the search. Options include: * * sort = field or attribute to sort on in <table name>.<field or attribute name> format (eg. ca_objects.idno); default is to sort on relevance (aka. sort='_natural') * sortDirection = direction to sort results by, either 'asc' for ascending order or 'desc' for descending order; default is 'asc' * no_cache = if true, search is performed regardless of whether results for the search are already cached; default is false * limit = if set then search results will be limited to the quantity specified. If not set then all results are returned. * form_id = optional form identifier string to record in log for search * log_details = optional form description to record in log for search * search_source = optional source indicator text to record in log for search * checkAccess = optional array of access values to filter results on * showDeleted = if set to true, related items that have been deleted are returned. Default is false. * deletedOnly = if set to true, only deleted items are returned. Default is false. * limitToModifiedOn = if set returned results will be limited to rows modified within the specified date range. The value should be a date/time expression parse-able by TimeExpressionParser * sets = if value is a list of set_ids, only rows that are members of those sets will be returned * user_id = If set item level access control is performed relative to specified user_id, otherwise defaults to logged in user * dontFilterByACL = if true ACL checking is not performed on results * appendToSearch = * restrictSearchToFields = * * @return SearchResult Results packages in a SearchResult object, or sub-class of SearchResult if an instance was passed in $po_result * @uses TimeExpressionParser::parse */ public function doSearch($ps_search, $po_result = null, $pa_options = null) { $t = new Timer(); global $AUTH_CURRENT_USER_ID; if ($vs_append_to_search = isset($pa_options['appendToSearch']) ? ' ' . $pa_options['appendToSearch'] : '') { $ps_search .= $vs_append_to_search; } $ps_search = str_replace("[BLANK]", '"[BLANK]"', $ps_search); // the special [BLANK] search term, which returns records that have *no* content in a specific fields, has to be quoted in order to protect the square brackets from the parser. if (!is_array($pa_options)) { $pa_options = array(); } if (($vn_limit = caGetOption('limit', $pa_options, null, array('castTo' => 'int'))) < 0) { $vn_limit = null; } $vs_sort = caGetOption('sort', $pa_options, null); $vs_sort_direction = strtolower(caGetOption('sortDirection', $pa_options, caGetOption('sort_direction', $pa_options, null))); //print "QUERY=$ps_search<br>"; // // Note that this is *not* misplaced code that should be in the Lucene plugin! // // We are using the Lucene syntax as our query syntax regardless the of back-end search engine. // The Lucene calls below just parse the query and then rewrite access points as-needed; the result // is a Lucene-compliant query ready-to-roll that is passed to the engine plugin. Of course, the Lucene // plugin just uses the string as-is... other plugins my choose to parse it however they wish to. // // // Process suffixes list... if search conforms to regex then we append a suffix. // This is useful, for example, to allow auto-wildcarding of accession numbers: if the search looks like an accession regex-wise we can append a "*" // $va_suffixes = $this->opo_search_config->getAssoc('search_suffixes'); if (is_array($va_suffixes) && sizeof($va_suffixes) && !preg_match('!"!', $ps_search)) { // don't add suffix wildcards when quoting foreach ($va_suffixes as $vs_preg => $vs_suffix) { if (preg_match("!{$vs_preg}!", $ps_search)) { $ps_search = preg_replace("!({$vs_preg})[\\*]*!", "\$1{$vs_suffix}", $ps_search); } } } $vb_no_cache = isset($pa_options['no_cache']) ? $pa_options['no_cache'] : false; unset($pa_options['no_cache']); $vn_cache_timeout = (int) $this->opo_search_config->get('cache_timeout'); if ($vn_cache_timeout == 0) { $vb_no_cache = true; } // don't try to cache if cache timeout is 0 (0 means disabled) $t_table = $this->opo_datamodel->getInstanceByTableName($this->ops_tablename, true); $vs_cache_key = md5($ps_search . "/" . print_R($this->getTypeRestrictionList(), true)); $o_cache = new SearchCache(); $vb_from_cache = false; if (!$vb_no_cache && $o_cache->load($vs_cache_key, $this->opn_tablenum, $pa_options)) { $vn_created_on = $o_cache->getParameter('created_on'); if (time() - $vn_created_on < $vn_cache_timeout) { Debug::msg('SEARCH cache hit for ' . $vs_cache_key); $va_hits = $o_cache->getResults(); if ($vs_sort != '_natural') { $va_hits = $this->sortHits($va_hits, $this->ops_tablename, $vs_sort, $vs_sort_direction); } elseif ($vs_sort == '_natural' && $vs_sort_direction == 'desc') { $va_hits = array_reverse($va_hits); } $o_res = new WLPlugSearchEngineCachedResult($va_hits, $this->opn_tablenum); $vb_from_cache = true; } else { Debug::msg('cache expire for ' . $vs_cache_key); $o_cache->remove(); } } if (!$vb_from_cache) { Debug::msg('SEARCH cache miss for ' . $vs_cache_key); $vs_char_set = $this->opo_app_config->get('character_set'); $o_query_parser = new LuceneSyntaxParser(); $o_query_parser->setEncoding($vs_char_set); $o_query_parser->setDefaultOperator(LuceneSyntaxParser::B_AND); $ps_search = preg_replace('![\']+!', '', $ps_search); try { $o_parsed_query = $o_query_parser->parse($ps_search, $vs_char_set); } catch (Exception $e) { // Retry search with all non-alphanumeric characters removed try { $o_parsed_query = $o_query_parser->parse(preg_replace("![^A-Za-z0-9 ]+!", " ", $ps_search), $vs_char_set); } catch (Exception $e) { $o_parsed_query = $o_query_parser->parse("", $vs_char_set); } } $va_rewrite_results = $this->_rewriteQuery($o_parsed_query); $o_rewritten_query = new Zend_Search_Lucene_Search_Query_Boolean($va_rewrite_results['terms'], $va_rewrite_results['signs']); $vs_search = $this->_queryToString($o_rewritten_query); //print "<div style='background:#FFFFFF; padding: 5px; border: 1px dotted #666666;'><strong>DEBUG: </strong>".$ps_search.'/'.$vs_search."</div>"; // Filter deleted records out of final result if (isset($pa_options['deletedOnly']) && $pa_options['deletedOnly'] && $t_table->hasField('deleted')) { $this->addResultFilter($this->ops_tablename . '.deleted', '=', '1'); } else { if ((!isset($pa_options['showDeleted']) || !$pa_options['showDeleted']) && $t_table->hasField('deleted')) { $this->addResultFilter($this->ops_tablename . '.deleted', '=', '0'); } } if (isset($pa_options['checkAccess']) && (is_array($pa_options['checkAccess']) && sizeof($pa_options['checkAccess']))) { $va_access_values = $pa_options['checkAccess']; $this->addResultFilter($this->ops_tablename . '.access', 'IN', join(",", $va_access_values)); } $vb_no_types = false; if (is_array($va_type_ids = $this->getTypeRestrictionList()) && sizeof($va_type_ids) > 0) { if ($t_table->getFieldInfo('type_id', 'IS_NULL')) { $va_type_ids[] = 'NULL'; } $this->addResultFilter($this->ops_tablename . '.type_id', 'IN', join(",", $va_type_ids)); } elseif (is_array($va_type_ids) && sizeof($va_type_ids) == 0) { $vb_no_types = true; } if (!$vb_no_types) { // Filter on source if (is_array($va_source_ids = $this->getSourceRestrictionList())) { $this->addResultFilter($this->ops_tablename . '.source_id', 'IN', join(",", $va_source_ids)); } if (in_array($t_table->getHierarchyType(), array(__CA_HIER_TYPE_SIMPLE_MONO__, __CA_HIER_TYPE_MULTI_MONO__))) { $this->addResultFilter($this->ops_tablename . '.parent_id', 'IS NOT', NULL); } if (is_array($va_restrict_to_fields = caGetOption('restrictSearchToFields', $pa_options, null)) && $this->opo_engine->can('restrict_to_fields')) { $this->opo_engine->setOption('restrictSearchToFields', $va_restrict_to_fields); } $o_res = $this->opo_engine->search($this->opn_tablenum, $vs_search, $this->opa_result_filters, $o_rewritten_query); // cache the results $va_hits = $o_res->getPrimaryKeyValues($vn_limit); $o_res->seek(0); } else { $va_hits = array(); } if (isset($pa_options['sets']) && $pa_options['sets']) { $va_hits = $this->filterHitsBySets($va_hits, $pa_options['sets'], array('search' => $vs_search)); } $vn_user_id = isset($pa_options['user_id']) && (int) $pa_options['user_id'] ? (int) $pa_options['user_id'] : (int) $AUTH_CURRENT_USER_ID; if ((!isset($pa_options['dontFilterByACL']) || !$pa_options['dontFilterByACL']) && $this->opo_app_config->get('perform_item_level_access_checking') && method_exists($t_table, "supportsACL") && $t_table->supportsACL()) { $va_hits = $this->filterHitsByACL($va_hits, $this->opn_tablenum, $vn_user_id, __CA_ACL_READONLY_ACCESS__); } if ($vs_sort != '_natural') { $va_hits = $this->sortHits($va_hits, $t_table->tableName(), $pa_options['sort'], isset($pa_options['sort_direction']) ? $pa_options['sort_direction'] : null); } elseif ($vs_sort == '_natural' && $vs_sort_direction == 'desc') { $va_hits = array_reverse($va_hits); } $o_res = new WLPlugSearchEngineCachedResult($va_hits, $this->opn_tablenum); // cache for later use if (!$vb_no_cache) { $o_cache->save($vs_cache_key, $this->opn_tablenum, $va_hits, array('created_on' => time()), null, $pa_options); } // log search $o_log = new Searchlog(); $vn_search_form_id = isset($pa_options['form_id']) ? $pa_options['form_id'] : null; $vs_log_details = isset($pa_options['log_details']) ? $pa_options['log_details'] : ''; $vs_search_source = isset($pa_options['search_source']) ? $pa_options['search_source'] : ''; $vn_execution_time = $t->getTime(4); $o_log->log(array('user_id' => $vn_user_id, 'table_num' => $this->opn_tablenum, 'search_expression' => $ps_search, 'num_hits' => sizeof($va_hits), 'form_id' => $vn_search_form_id, 'ip_addr' => $_SERVER['REMOTE_ADDR'] ? $_SERVER['REMOTE_ADDR'] : null, 'details' => $vs_log_details, 'search_source' => $vs_search_source, 'execution_time' => $vn_execution_time)); } if ($po_result) { $po_result->init($o_res, $this->opa_tables, $pa_options); return $po_result; } else { return new SearchResult($o_res, $this->opa_tables); } }
/** * * * @param string $ps_source * @param string $ps_mapping * @param array $pa_options * user_id = user to execute import for * description = Text describing purpose of import to be logged. * showCLIProgressBar = Show command-line progress bar. Default is false. * format = Format of data being imported. MANDATORY * useNcurses = Use ncurses library to format output * logDirectory = path to directory where logs should be written * logLevel = KLogger constant for minimum log level to record. Default is KLogger::INFO. Constants are, in descending order of shrillness: * KLogger::EMERG = Emergency messages (system is unusable) * KLogger::ALERT = Alert messages (action must be taken immediately) * KLogger::CRIT = Critical conditions * KLogger::ERR = Error conditions * KLogger::WARN = Warnings * KLogger::NOTICE = Notices (normal but significant conditions) * KLogger::INFO = Informational messages * KLogger::DEBUG = Debugging messages * dryRun = do import but don't actually save data * environment = an array of environment values to provide to the import process. The keys manifest themselves as mappable tags. * forceImportForPrimaryKeys = list of primary key ids to force mapped source data into. The number of keys passed should equal or exceed the number of rows in the source data. [Default is empty] * transaction = transaction to perform import within. Will not be used if noTransaction option is set. [Default is to create a new transaction] * noTransaction = don't wrap the import in a transaction. [Default is false] */ public static function importDataFromSource($ps_source, $ps_mapping, $pa_options = null) { ca_data_importers::$s_num_import_errors = 0; ca_data_importers::$s_num_records_processed = 0; ca_data_importers::$s_num_records_skipped = 0; ca_data_importers::$s_import_error_list = array(); $opa_app_plugin_manager = new ApplicationPluginManager(); $va_notices = $va_errors = array(); $pb_no_transaction = caGetOption('noTransaction', $pa_options, false, array('castTo' => 'bool')); $pa_force_import_for_primary_keys = caGetOption('forceImportForPrimaryKeys', $pa_options, null); if (!($t_mapping = ca_data_importers::mappingExists($ps_mapping))) { return null; } $o_event = ca_data_import_events::newEvent(isset($pa_options['user_id']) ? $pa_options['user_id'] : null, $pa_options['format'], $ps_source, isset($pa_options['description']) ? $pa_options['description'] : ''); $o_trans = null; if (!$pb_no_transaction) { if (!($o_trans = caGetOption('transaction', $pa_options, null))) { $o_trans = new Transaction(); } $t_mapping->setTransaction($o_trans); } $po_request = caGetOption('request', $pa_options, null); $pb_dry_run = caGetOption('dryRun', $pa_options, false); $pn_file_number = caGetOption('fileNumber', $pa_options, 0); $pn_number_of_files = caGetOption('numberOfFiles', $pa_options, 1); $o_config = Configuration::load(); if (!is_array($pa_options) || !isset($pa_options['logLevel']) || !$pa_options['logLevel']) { $pa_options['logLevel'] = KLogger::INFO; } if (!is_array($pa_options) || !isset($pa_options['logDirectory']) || !$pa_options['logDirectory'] || !file_exists($pa_options['logDirectory'])) { if (!($pa_options['logDirectory'] = $o_config->get('batch_metadata_import_log_directory'))) { $pa_options['logDirectory'] = "."; } } if (!is_writeable($pa_options['logDirectory'])) { $pa_options['logDirectory'] = caGetTempDirPath(); } $o_log = new KLogger($pa_options['logDirectory'], $pa_options['logLevel']); $vb_show_cli_progress_bar = isset($pa_options['showCLIProgressBar']) && $pa_options['showCLIProgressBar'] ? true : false; $o_progress = caGetOption('progressBar', $pa_options, new ProgressBar('WebUI')); if ($vb_show_cli_progress_bar) { $o_progress->setMode('CLI'); $o_progress->set('outputToTerminal', true); } if ($vb_use_ncurses = isset($pa_options['useNcurses']) && $pa_options['useNcurses'] ? true : false) { $vb_use_ncurses = caCLIUseNcurses(); } $vn_error_window_height = null; $vn_max_x = $vn_max_y = null; if ($vb_use_ncurses) { ncurses_init(); $r_screen = ncurses_newwin(0, 0, 0, 0); ncurses_border(0, 0, 0, 0, 0, 0, 0, 0); ncurses_getmaxyx($r_screen, $vn_max_y, $vn_max_x); $vn_error_window_height = $vn_max_y - 8; $r_errors = ncurses_newwin($vn_error_window_height, $vn_max_x - 4, 4, 2); ncurses_wborder($r_errors, 0, 0, 0, 0, 0, 0, 0, 0); ncurses_wattron($r_errors, NCURSES_A_REVERSE); ncurses_mvwaddstr($r_errors, 0, 1, _t(" Recent errors ")); ncurses_wattroff($r_errors, NCURSES_A_REVERSE); $r_progress = ncurses_newwin(3, $vn_max_x - 4, $vn_max_y - 4, 2); ncurses_wborder($r_progress, 0, 0, 0, 0, 0, 0, 0, 0); ncurses_wattron($r_progress, NCURSES_A_REVERSE); ncurses_mvwaddstr($r_progress, 0, 1, _t(" Progress ")); ncurses_wattroff($r_progress, NCURSES_A_REVERSE); $r_status = ncurses_newwin(3, $vn_max_x - 4, 1, 2); ncurses_wborder($r_status, 0, 0, 0, 0, 0, 0, 0, 0); ncurses_wattron($r_status, NCURSES_A_REVERSE); ncurses_mvwaddstr($r_status, 0, 1, _t(" Import status ")); ncurses_wattroff($r_status, NCURSES_A_REVERSE); ncurses_refresh(0); ncurses_wrefresh($r_progress); ncurses_wrefresh($r_errors); ncurses_wrefresh($r_status); } $o_log->logInfo(_t('Started import of %1 using mapping %2', $ps_source, $t_mapping->get("importer_code"))); $t = new Timer(); $vn_start_time = time(); $va_log_import_error_opts = array('startTime' => $vn_start_time, 'window' => $r_errors, 'log' => $o_log, 'request' => $po_request, 'progressCallback' => isset($pa_options['progressCallback']) && ($ps_callback = $pa_options['progressCallback']) ? $ps_callback : null, 'reportCallback' => isset($pa_options['reportCallback']) && ($ps_callback = $pa_options['reportCallback']) ? $ps_callback : null); global $g_ui_locale_id; // constant locale set by index.php for web requests $vn_locale_id = isset($pa_options['locale_id']) && (int) $pa_options['locale_id'] ? (int) $pa_options['locale_id'] : $g_ui_locale_id; $o_dm = $t_mapping->getAppDatamodel(); $o_progress->start(_t('Reading %1', $ps_source), array('window' => $r_progress)); if ($po_request && isset($pa_options['progressCallback']) && ($ps_callback = $pa_options['progressCallback'])) { $ps_callback($po_request, $pn_file_number, $pn_number_of_files, $ps_source, 0, 100, _t('Reading %1', $ps_source), time() - $vn_start_time, memory_get_usage(true), 0, ca_data_importers::$s_num_import_errors); } // Open file $ps_format = isset($pa_options['format']) && $pa_options['format'] ? $pa_options['format'] : null; if (!($o_reader = $t_mapping->getDataReader($ps_source, $ps_format))) { ca_data_importers::logImportError(_t("Could not open source %1 (format=%2)", $ps_source, $ps_format), $va_log_import_error_opts); if ($vb_use_ncurses) { ncurses_end(); } if ($o_trans) { $o_trans->rollback(); } return false; } if (!$o_reader->read($ps_source, array('basePath' => $t_mapping->getSetting('basePath')))) { ca_data_importers::logImportError(_t("Could not read source %1 (format=%2)", $ps_source, $ps_format), $va_log_import_error_opts); if ($vb_use_ncurses) { ncurses_end(); } if ($o_trans) { $o_trans->rollback(); } return false; } $o_log->logDebug(_t('Finished reading input source at %1 seconds', $t->getTime(4))); $vn_num_items = $o_reader->numRows(); $o_progress->setTotal($vn_num_items); $o_progress->start(_t('Importing from %1', $ps_source), array('window' => $r_progress)); if ($po_request && isset($pa_options['progressCallback']) && ($ps_callback = $pa_options['progressCallback'])) { $ps_callback($po_request, $pn_file_number, $pn_number_of_files, $ps_source, 0, $vn_num_items, _t('Importing from %1', $ps_source), time() - $vn_start_time, memory_get_usage(true), 0, ca_data_importers::$s_num_import_errors); } // What are we importing? $vn_table_num = $t_mapping->get('table_num'); if (!($t_subject = $o_dm->getInstanceByTableNum($vn_table_num))) { // invalid table if ($vb_use_ncurses) { ncurses_end(); } if ($o_trans) { $o_trans->rollback(); } return false; } $t_subject->setTransaction($o_trans); $vs_subject_table_name = $t_subject->tableName(); $t_label = $t_subject->getLabelTableInstance(); $t_label->setTransaction($o_trans); $vs_label_display_fld = $t_subject->getLabelDisplayField(); $vs_subject_table = $t_subject->tableName(); $vs_type_id_fld = $t_subject->getTypeFieldName(); $vs_idno_fld = $t_subject->getProperty('ID_NUMBERING_ID_FIELD'); // get mapping rules $va_mapping_rules = $t_mapping->getRules(); // get mapping groups $va_mapping_groups = $t_mapping->getGroups(); $va_mapping_items = $t_mapping->getItems(); // // Mapping-level settings // $vs_type_mapping_setting = $t_mapping->getSetting('type'); $vn_num_initial_rows_to_skip = $t_mapping->getSetting('numInitialRowsToSkip'); if (!in_array($vs_import_error_policy = $t_mapping->getSetting('errorPolicy'), array('ignore', 'stop'))) { $vs_import_error_policy = 'ignore'; } if (!in_array($vs_existing_record_policy = $t_mapping->getSetting('existingRecordPolicy'), array('none', 'skip_on_idno', 'skip_on_preferred_labels', 'merge_on_idno', 'merge_on_preferred_labels', 'merge_on_idno_and_preferred_labels', 'merge_on_idno_with_replace', 'merge_on_preferred_labels_with_replace', 'merge_on_idno_and_preferred_labels_with_replace', 'overwrite_on_idno', 'overwrite_on_preferred_labels', 'overwrite_on_idno_and_preferred_labels'))) { $vs_existing_record_policy = 'none'; } // Analyze mapping for figure out where type, idno, preferred label and other mandatory fields are coming from $vn_type_id_mapping_item_id = $vn_idno_mapping_item_id = null; $va_preferred_label_mapping_ids = array(); $va_mandatory_field_mapping_ids = array(); $va_mandatory_fields = $t_subject->getMandatoryFields(); foreach ($va_mapping_items as $vn_item_id => $va_item) { $vs_destination = $va_item['destination']; if (sizeof($va_dest_tmp = explode(".", $vs_destination)) >= 2) { if ($va_dest_tmp[0] == $vs_subject_table && $va_dest_tmp[1] == 'preferred_labels') { if (isset($va_dest_tmp[2])) { $va_preferred_label_mapping_ids[$vn_item_id] = $va_dest_tmp[2]; } else { $va_preferred_label_mapping_ids[$vn_item_id] = $vs_label_display_fld; } continue; } } switch ($vs_destination) { case 'representation_id': if ($vs_subject_table == 'ca_representation_annotations') { $vn_type_id_mapping_item_id = $vn_item_id; } break; case "{$vs_subject_table}.{$vs_type_id_fld}": $vn_type_id_mapping_item_id = $vn_item_id; break; case "{$vs_subject_table}.{$vs_idno_fld}": $vn_idno_mapping_item_id = $vn_item_id; break; } foreach ($va_mandatory_fields as $vs_mandatory_field) { if ($vs_mandatory_field == $vs_type_id_fld) { continue; } // type is handled separately if ($vs_destination == "{$vs_subject_table}.{$vs_mandatory_field}") { $va_mandatory_field_mapping_ids[$vs_mandatory_field] = $vn_item_id; } } } $va_items_by_group = array(); foreach ($va_mapping_items as $vn_item_id => $va_item) { $va_items_by_group[$va_item['group_id']][$va_item['item_id']] = $va_item; } $o_log->logDebug(_t('Finished analyzing mapping at %1 seconds', $t->getTime(4))); // // Set up environment // $va_environment = caGetOption('environment', $pa_options, array(), array('castTo' => 'array')); if (is_array($va_environment_config = $t_mapping->getEnvironment())) { foreach ($va_environment_config as $vn_i => $va_environment_item) { $va_env_tmp = explode("|", $va_environment_item['value']); if (!($o_env_reader = $t_mapping->getDataReader($ps_source, $ps_format))) { break; } if (!$o_env_reader->read($ps_source, array('basePath' => ''))) { break; } $o_env_reader->nextRow(); switch (sizeof($va_env_tmp)) { case 1: $vs_env_value = $o_env_reader->get($va_environment_item['value'], array('returnAsArray' => false)); break; case 2: $vn_seek = (int) $va_env_tmp[0]; $o_reader->seek($vn_seek > 0 ? $vn_seek - 1 : $vn_seek); $o_env_reader->nextRow(); $vs_env_value = $o_env_reader->get($va_env_tmp[1], array('returnAsArray' => false)); $o_reader->seek(0); break; default: $vs_env_value = $va_environment_item['value']; break; } $va_environment[$va_environment_item['name']] = $vs_env_value; } } // // Run through rows // $vn_row = 0; ca_data_importers::$s_num_records_processed = 0; while ($o_reader->nextRow()) { $va_mandatory_field_values = array(); $vs_preferred_label_for_log = null; if ($vn_row < $vn_num_initial_rows_to_skip) { // skip over initial header rows $vn_row++; continue; } $vn_row++; $t->startTimer(); $o_log->logDebug(_t('Started reading row %1 at %2 seconds', $vn_row, $t->getTime(4))); $t_subject = $o_dm->getInstanceByTableNum($vn_table_num); if ($o_trans) { $t_subject->setTransaction($o_trans); } $t_subject->setMode(ACCESS_WRITE); // Update status display if ($vb_use_ncurses && ca_data_importers::$s_num_records_processed) { ncurses_mvwaddstr($r_status, 1, 2, _t("Items processed/skipped: %1/%2", ca_data_importers::$s_num_records_processed, ca_data_importers::$s_num_records_skipped) . str_repeat(" ", 5) . _t("Errors: %1 (%2)", ca_data_importers::$s_num_import_errors, sprintf("%3.1f", ca_data_importers::$s_num_import_errors / ca_data_importers::$s_num_records_processed * 100) . "%") . str_repeat(" ", 6) . _t("Mapping: %1", $ps_mapping) . str_repeat(" ", 5) . _t("Source: %1", $ps_source) . str_repeat(" ", 5) . date("Y-m-d H:i:s") . str_repeat(" ", 5)); ncurses_refresh(0); ncurses_wrefresh($r_status); } // // Get data for current row // $va_row = array_merge($o_reader->getRow(), $va_environment); // // Apply rules // foreach ($va_mapping_rules as $va_rule) { if (!isset($va_rule['trigger']) || !$va_rule['trigger']) { continue; } if (!isset($va_rule['actions']) || !is_array($va_rule['actions']) || !sizeof($va_rule['actions'])) { continue; } $vm_ret = ExpressionParser::evaluate($va_rule['trigger'], $va_row); if (!ExpressionParser::hadError() && (bool) $vm_ret) { foreach ($va_rule['actions'] as $va_action) { if (!is_array($va_action) && strtolower($va_action) == 'skip') { $va_action = array('action' => 'skip'); } switch ($vs_action_code = strtolower($va_action['action'])) { case 'set': $va_row[$va_action['target']] = $va_action['value']; // TODO: transform value using mapping rules? break; case 'skip': default: if ($vs_action_code != 'skip') { $o_log->logInfo(_t('Row was skipped using rule "%1" with default action because an invalid action ("%2") was specified', $va_rule['trigger'], $vs_action_code)); } else { $o_log->logDebug(_t('Row was skipped using rule "%1" with action "%2"', $va_rule['trigger'], $vs_action_code)); } continue 4; break; } } } } // // Perform mapping and insert // // Get minimal info for imported row (type_id, idno, label) // Get type if ($vn_type_id_mapping_item_id) { // Type is specified in row $vs_type = ca_data_importers::getValueFromSource($va_mapping_items[$vn_type_id_mapping_item_id], $o_reader, array('environment' => $va_environment)); } else { // Type is constant for all rows $vs_type = $vs_type_mapping_setting; } // Get idno $vs_idno = null; if ($vn_idno_mapping_item_id) { // idno is specified in row $vs_idno = ca_data_importers::getValueFromSource($va_mapping_items[$vn_idno_mapping_item_id], $o_reader, array('environment' => $va_environment)); if (isset($va_mapping_items[$vn_idno_mapping_item_id]['settings']['default']) && strlen($va_mapping_items[$vn_idno_mapping_item_id]['settings']['default']) && !strlen($vs_idno)) { $vs_idno = $va_mapping_items[$vn_idno_mapping_item_id]['settings']['default']; } if (!is_array($vs_idno) && $vs_idno[0] == '^' && preg_match("!^\\^[^ ]+\$!", $vs_idno)) { // Parse placeholder when it's at the beginning of the value if (!is_null($vm_parsed_val = BaseRefinery::parsePlaceholder($vs_idno, $va_row, $va_item, null, array('reader' => $o_reader, 'returnAsString' => true)))) { $vs_idno = $vm_parsed_val; } } // Apply prefix/suffix *AFTER* setting default if ($vs_idno && isset($va_mapping_items[$vn_idno_mapping_item_id]['settings']['prefix']) && strlen($va_mapping_items[$vn_idno_mapping_item_id]['settings']['prefix'])) { $vs_idno = $va_mapping_items[$vn_idno_mapping_item_id]['settings']['prefix'] . $vs_idno; } if ($vs_idno && isset($va_mapping_items[$vn_idno_mapping_item_id]['settings']['suffix']) && strlen($va_mapping_items[$vn_idno_mapping_item_id]['settings']['suffix'])) { $vs_idno .= $va_mapping_items[$vn_idno_mapping_item_id]['settings']['suffix']; } if (isset($va_mapping_items[$vn_idno_mapping_item_id]['settings']['formatWithTemplate']) && strlen($va_mapping_items[$vn_idno_mapping_item_id]['settings']['formatWithTemplate'])) { $vs_idno = caProcessTemplate($va_mapping_items[$vn_idno_mapping_item_id]['settings']['formatWithTemplate'], $va_row); } } else { $vs_idno = "%"; } $vb_idno_is_template = (bool) preg_match('![%]+!', $vs_idno); // get preferred labels $va_pref_label_values = array(); foreach ($va_preferred_label_mapping_ids as $vn_preferred_label_mapping_id => $vs_preferred_label_mapping_fld) { $vs_label_val = ca_data_importers::getValueFromSource($va_mapping_items[$vn_preferred_label_mapping_id], $o_reader, array('environment' => $va_environment)); // If a template is specified format the label value with it so merge-on-preferred_label doesn't fail if (isset($va_mapping_items[$vn_preferred_label_mapping_id]['settings']['formatWithTemplate']) && strlen($va_mapping_items[$vn_preferred_label_mapping_id]['settings']['formatWithTemplate'])) { $vs_label_val = caProcessTemplate($va_mapping_items[$vn_preferred_label_mapping_id]['settings']['formatWithTemplate'], $va_row); } $va_pref_label_values[$vs_preferred_label_mapping_fld] = $vs_label_val; } $vs_display_label = isset($va_pref_label_values[$vs_label_display_fld]) ? $va_pref_label_values[$vs_label_display_fld] : $vs_idno; // // Look for existing record? // if (is_array($pa_force_import_for_primary_keys) && sizeof($pa_force_import_for_primary_keys) > 0) { $vn_id = array_shift($pa_force_import_for_primary_keys); if (!$t_subject->load($vn_id)) { $o_log->logInfo(_t('[%1] Skipped import because of forced primary key \'%1\' does not exist', $vn_id)); ca_data_importers::$s_num_records_skipped++; continue; // skip because primary key does not exist } } elseif ($vs_existing_record_policy != 'none') { switch ($vs_existing_record_policy) { case 'skip_on_idno': if (!$vb_idno_is_template) { $va_ids = call_user_func_array($t_subject->tableName() . "::find", array(array('type_id' => $vs_type, $t_subject->getProperty('ID_NUMBERING_ID_FIELD') => $vs_idno, 'deleted' => 0), array('returnAs' => 'ids'))); if (is_array($va_ids) && sizeof($va_ids) > 0) { $o_log->logInfo(_t('[%1] Skipped import because of existing record matched on identifier by policy %2', $vs_idno, $vs_existing_record_policy)); ca_data_importers::$s_num_records_skipped++; continue 2; // skip because idno matched } } break; case 'skip_on_preferred_labels': $va_ids = call_user_func_array($t_subject->tableName() . "::find", array(array('type_id' => $vs_type, 'preferred_labels' => $va_pref_label_values, 'deleted' => 0), array('returnAs' => 'ids'))); if (is_array($va_ids) && sizeof($va_ids) > 0) { $o_log->logInfo(_t('[%1] Skipped import because of existing record matched on label by policy %2', $vs_idno, $vs_existing_record_policy)); ca_data_importers::$s_num_records_skipped++; continue 2; // skip because label matched } break; case 'merge_on_idno_and_preferred_labels': case 'merge_on_idno': case 'merge_on_idno_and_preferred_labels_with_replace': case 'merge_on_idno_with_replace': if (!$vb_idno_is_template) { $va_ids = call_user_func_array($t_subject->tableName() . "::find", array(array('type_id' => $vs_type, $t_subject->getProperty('ID_NUMBERING_ID_FIELD') => $vs_idno, 'deleted' => 0), array('returnAs' => 'ids'))); if (is_array($va_ids) && sizeof($va_ids) > 0) { $t_subject->load($va_ids[0]); $o_log->logInfo(_t('[%1] Merged with existing record matched on identifer by policy %2', $vs_idno, $vs_existing_record_policy)); break; } } if (in_array($vs_existing_record_policy, array('merge_on_idno', 'merge_on_idno_with_replace'))) { break; } // fall through if merge_on_idno_and_preferred_labels // fall through if merge_on_idno_and_preferred_labels case 'merge_on_preferred_labels': case 'merge_on_preferred_labels_with_replace': $va_ids = call_user_func_array($t_subject->tableName() . "::find", array(array('type_id' => $vs_type, 'preferred_labels' => $va_pref_label_values, 'deleted' => 0), array('returnAs' => 'ids'))); if (is_array($va_ids) && sizeof($va_ids) > 0) { $t_subject->load($va_ids[0]); $o_log->logInfo(_t('[%1] Merged with existing record matched on label by policy %2', $vs_idno, $vs_existing_record_policy)); } break; case 'overwrite_on_idno_and_preferred_labels': case 'overwrite_on_idno': if (!$vb_idno_is_template && $vs_idno) { $va_ids = call_user_func_array($t_subject->tableName() . "::find", array(array('type_id' => $vs_type, $t_subject->getProperty('ID_NUMBERING_ID_FIELD') => $vs_idno, 'deleted' => 0), array('returnAs' => 'ids'))); if (is_array($va_ids) && sizeof($va_ids) > 0) { $t_subject->load($va_ids[0]); $t_subject->setMode(ACCESS_WRITE); $t_subject->delete(true, array('hard' => true)); if ($t_subject->numErrors()) { ca_data_importers::logImportError(_t('[%1] Could not delete existing record matched on identifier by policy %2', $vs_idno, $vs_existing_record_policy)); // Don't stop? } else { $o_log->logInfo(_t('[%1] Overwrote existing record matched on identifier by policy %2', $vs_idno, $vs_existing_record_policy)); $t_subject->clear(); break; } } } if ($vs_existing_record_policy == 'overwrite_on_idno') { break; } // fall through if overwrite_on_idno_and_preferred_labels // fall through if overwrite_on_idno_and_preferred_labels case 'overwrite_on_preferred_labels': $va_ids = call_user_func_array($t_subject->tableName() . "::find", array(array('type_id' => $vs_type, 'preferred_labels' => $va_pref_label_values, 'deleted' => 0), array('returnAs' => 'ids'))); if (is_array($va_ids) && sizeof($va_ids) > 0) { $t_subject->load($va_ids[0]); $t_subject->setMode(ACCESS_WRITE); $t_subject->delete(true, array('hard' => true)); if ($t_subject->numErrors()) { ca_data_importers::logImportError(_t('[%1] Could not delete existing record matched on label by policy %2', $vs_idno, $vs_existing_record_policy)); // Don't stop? } else { $o_log->logInfo(_t('[%1] Overwrote existing record matched on label by policy %2', $vs_idno, $vs_existing_record_policy)); break; } $t_subject->clear(); } break; } } $o_progress->next(_t("Importing %1", $vs_idno), array('window' => $r_progress)); if ($po_request && isset($pa_options['progressCallback']) && ($ps_callback = $pa_options['progressCallback'])) { $ps_callback($po_request, $pn_file_number, $pn_number_of_files, $ps_source, ca_data_importers::$s_num_records_processed, $vn_num_items, _t("[%3/%4] Processing %1 (%2)", caTruncateStringWithEllipsis($vs_display_label, 50), $vs_idno, ca_data_importers::$s_num_records_processed, $vn_num_items), time() - $vn_start_time, memory_get_usage(true), ca_data_importers::$s_num_records_processed, ca_data_importers::$s_num_import_errors); } $vb_output_subject_preferred_label = false; $va_content_tree = array(); foreach ($va_items_by_group as $vn_group_id => $va_items) { $va_group = $va_mapping_groups[$vn_group_id]; $vs_group_destination = $va_group['destination']; $va_group_tmp = explode(".", $vs_group_destination); if (sizeof($va_items) < 2 && sizeof($va_group_tmp) > 2) { array_pop($va_group_tmp); } $vs_target_table = $va_group_tmp[0]; if (!($t_target = $o_dm->getInstanceByTableName($vs_target_table, true))) { // Invalid target table $o_log->logWarn(_t('[%1] Skipped group %2 because target %3 is invalid', $vs_idno, $vn_group_id, $vs_target_table)); continue; } if ($o_trans) { $t_target->setTransaction($o_trans); } $va_group_buf = array(); foreach ($va_items as $vn_item_id => $va_item) { if ($vb_use_as_single_value = caGetOption('useAsSingleValue', $va_item['settings'], false)) { // Force repeating values to be imported as a single value $va_vals = array(ca_data_importers::getValueFromSource($va_item, $o_reader, array('delimiter' => caGetOption('delimiter', $va_item['settings'], ''), 'returnAsArray' => false))); } else { $va_vals = ca_data_importers::getValueFromSource($va_item, $o_reader, array('returnAsArray' => true, 'environment' => $va_environment)); } if (!sizeof($va_vals)) { $va_vals = array(0 => null); } // consider missing values equivalent to blanks // Do value conversions foreach ($va_vals as $vn_i => $vm_val) { if (isset($va_item['settings']['default']) && strlen($va_item['settings']['default']) && !strlen($vm_val)) { $vm_val = $va_item['settings']['default']; } // Apply prefix/suffix *AFTER* setting default if ($vm_val && isset($va_item['settings']['prefix']) && strlen($va_item['settings']['prefix'])) { $vm_val = $va_item['settings']['prefix'] . $vm_val; } if ($vm_val && isset($va_item['settings']['suffix']) && strlen($va_item['settings']['suffix'])) { $vm_val .= $va_item['settings']['suffix']; } if (!is_array($vm_val) && $vm_val[0] == '^' && preg_match("!^\\^[^ ]+\$!", $vm_val)) { // Parse placeholder if (!is_null($vm_parsed_val = BaseRefinery::parsePlaceholder($vm_val, $va_row, $va_item, $vn_i, array('reader' => $o_reader, 'returnAsString' => true)))) { $vm_val = $vm_parsed_val; } } if (isset($va_item['settings']['formatWithTemplate']) && strlen($va_item['settings']['formatWithTemplate'])) { $vm_val = caProcessTemplate($va_item['settings']['formatWithTemplate'], array_replace($va_row, array((string) $va_item['source'] => ca_data_importers::replaceValue($vm_val, $va_item))), array('getFrom' => $o_reader)); } if (isset($va_item['settings']['applyRegularExpressions']) && is_array($va_item['settings']['applyRegularExpressions'])) { if (is_array($va_item['settings']['applyRegularExpressions'])) { foreach ($va_item['settings']['applyRegularExpressions'] as $vn_regex_index => $va_regex) { if (!strlen($va_regex['match'])) { continue; } $vm_val = preg_replace("!" . str_replace("!", "\\!", $va_regex['match']) . "!" . (isset($va_regex['caseSensitive']) && (bool) $va_regex['caseSensitive'] ? '' : 'i'), $va_regex['replaceWith'], $vm_val); } } } $va_vals[$vn_i] = $vm_val; if ($o_reader->valuesCanRepeat()) { $va_row[$va_item['source']][$vn_i] = $va_row[mb_strtolower($va_item['source'])][$vn_i] = $vm_val; } else { $va_row[$va_item['source']] = $va_row[mb_strtolower($va_item['source'])] = $vm_val; } } // Process each value $vn_c = -1; foreach ($va_vals as $vn_i => $vm_val) { $vn_c++; if (isset($va_item['settings']['convertNewlinesToHTML']) && (bool) $va_item['settings']['convertNewlinesToHTML'] && is_string($vm_val)) { $vm_val = nl2br($vm_val); } // Get location in content tree for addition of new content $va_item_dest = explode(".", $va_item['destination']); $vs_item_terminal = $va_item_dest[sizeof($va_item_dest) - 1]; if (isset($va_item['settings']['restrictToTypes']) && is_array($va_item['settings']['restrictToTypes']) && !in_array($vs_type, $va_item['settings']['restrictToTypes'])) { $o_log->logInfo(_t('[%1] Skipped row %2 because of type restriction', $vs_idno, $vn_row)); continue 4; } if (isset($va_item['settings']['skipRowIfEmpty']) && (bool) $va_item['settings']['skipRowIfEmpty'] && !strlen($vm_val)) { $o_log->logInfo(_t('[%1] Skipped row %2 because value for %3 in group %4 is empty', $vs_idno, $vn_row, $vs_item_terminal, $vn_group_id)); continue 4; } if (isset($va_item['settings']['skipRowIfValue']) && is_array($va_item['settings']['skipRowIfValue']) && strlen($vm_val) && in_array($vm_val, $va_item['settings']['skipRowIfValue'])) { $o_log->logInfo(_t('[%1] Skipped row %2 because value for %3 in group %4 matches value %5', $vs_idno, $vn_row, $vs_item_terminal, $vn_group_id)); continue 4; } if (isset($va_item['settings']['skipRowIfNotValue']) && is_array($va_item['settings']['skipRowIfNotValue']) && strlen($vm_val) && !in_array($vm_val, $va_item['settings']['skipRowIfNotValue'])) { $o_log->logInfo(_t('[%1] Skipped row %2 because value for %3 in group %4 is not in list of values', $vs_idno, $vn_row, $vs_item_terminal, $vn_group_id, $vm_val)); continue 4; } if (isset($va_item['settings']['skipGroupIfEmpty']) && (bool) $va_item['settings']['skipGroupIfEmpty'] && !strlen($vm_val)) { $o_log->logInfo(_t('[%1] Skipped group %2 because value for %3 is empty', $vs_idno, $vn_group_id, $vs_item_terminal)); continue 3; } if (isset($va_item['settings']['skipGroupIfExpression']) && strlen(trim($va_item['settings']['skipGroupIfExpression']))) { if ($vm_ret = ExpressionParser::evaluate($va_item['settings']['skipGroupIfExpression'], $va_row)) { $o_log->logInfo(_t('[%1] Skipped group %2 because expression %3 is true', $vs_idno, $vn_group_id, $va_item['settings']['skipGroupIfExpression'])); continue 3; } } if (isset($va_item['settings']['skipGroupIfValue']) && is_array($va_item['settings']['skipGroupIfValue']) && strlen($vm_val) && in_array($vm_val, $va_item['settings']['skipGroupIfValue'])) { $o_log->logInfo(_t('[%1] Skipped group %2 because value for %3 matches value %4', $vs_idno, $vn_group_id, $vs_item_terminal, $vm_val)); continue 3; } if (isset($va_item['settings']['skipGroupIfNotValue']) && is_array($va_item['settings']['skipGroupIfNotValue']) && strlen($vm_val) && !in_array($vm_val, $va_item['settings']['skipGroupIfNotValue'])) { $o_log->logInfo(_t('[%1] Skipped group %2 because value for %3 matches is not in list of values', $vs_idno, $vn_group_id, $vs_item_terminal)); continue 3; } if (isset($va_item['settings']['skipIfExpression']) && strlen(trim($va_item['settings']['skipIfExpression']))) { if ($vm_ret = ExpressionParser::evaluate($va_item['settings']['skipIfExpression'], $va_row)) { $o_log->logInfo(_t('[%1] Skipped mapping because expression %2 is true', $vs_idno, $va_item['settings']['skipIfExpression'])); continue 2; } } if (isset($va_item['settings']['skipIfEmpty']) && (bool) $va_item['settings']['skipIfEmpty'] && !strlen($vm_val)) { $o_log->logInfo(_t('[%1] Skipped mapping because value for %2 is empty', $vs_idno, $vs_item_terminal)); continue 2; } if ($vn_type_id_mapping_item_id && $vn_item_id == $vn_type_id_mapping_item_id) { continue; } if ($vn_idno_mapping_item_id && $vn_item_id == $vn_idno_mapping_item_id) { continue; } if (is_null($vm_val)) { continue; } // Get mapping error policy $vb_item_error_policy_is_default = false; if (!isset($va_item['settings']['errorPolicy']) || !in_array($vs_item_error_policy = $va_item['settings']['errorPolicy'], array('ignore', 'stop'))) { $vs_item_error_policy = 'ignore'; $vb_item_error_policy_is_default = true; } // if (isset($va_item['settings']['relationshipType']) && strlen($vs_rel_type = $va_item['settings']['relationshipType']) && $vs_target_table != $vs_subject_table) { $va_group_buf[$vn_c]['_relationship_type'] = $vs_rel_type; } // Is it a constant value? if (preg_match("!^_CONSTANT_:[\\d]+:(.*)!", $va_item['source'], $va_matches)) { $va_group_buf[$vn_c][$vs_item_terminal] = $va_matches[1]; // Set it and go onto the next item if ($vs_target_table == $vs_subject_table_name && ($vs_k = array_search($vn_item_id, $va_mandatory_field_mapping_ids)) !== false) { $va_mandatory_field_values[$vs_k] = $vm_val; } continue; } // Perform refinery call (if required) per value if (isset($va_item['settings']['refineries']) && is_array($va_item['settings']['refineries'])) { foreach ($va_item['settings']['refineries'] as $vs_refinery) { if (!$vs_refinery) { continue; } if ($o_refinery = RefineryManager::getRefineryInstance($vs_refinery)) { $va_refined_values = $o_refinery->refine($va_content_tree, $va_group, $va_item, $va_row, array('mapping' => $t_mapping, 'source' => $ps_source, 'subject' => $t_subject, 'locale_id' => $vn_locale_id, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'reader' => $o_reader, 'valueIndex' => $vn_i)); if (!$va_refined_values || is_array($va_refined_values) && !sizeof($va_refined_values)) { continue 2; } if ($o_refinery->returnsMultipleValues()) { foreach ($va_refined_values as $va_refined_value) { $va_refined_value['_errorPolicy'] = $vs_item_error_policy; if (!is_array($va_group_buf[$vn_c])) { $va_group_buf[$vn_c] = array(); } $va_group_buf[$vn_c] = array_merge($va_group_buf[$vn_c], $va_refined_value); $vn_c++; } } else { $va_group_buf[$vn_c]['_errorPolicy'] = $vs_item_error_policy; $va_group_buf[$vn_c][$vs_item_terminal] = $va_refined_values; $vn_c++; } if ($vs_target_table == $vs_subject_table_name && ($vs_k = array_search($vn_item_id, $va_mandatory_field_mapping_ids)) !== false) { $va_mandatory_field_values[$vs_k] = $vm_val; } continue 2; } else { ca_data_importers::logImportError(_t('[%1] Invalid refinery %2 specified', $vs_idno, $vs_refinery)); } } } if ($vs_target_table == $vs_subject_table_name && ($vs_k = array_search($vn_item_id, $va_mandatory_field_mapping_ids)) !== false) { $va_mandatory_field_values[$vs_k] = $vm_val; } $vn_max_length = !is_array($vm_val) && isset($va_item['settings']['maxLength']) && (int) $va_item['settings']['maxLength'] ? (int) $va_item['settings']['maxLength'] : null; if (isset($va_item['settings']['delimiter']) && $va_item['settings']['delimiter']) { if (!is_array($va_item['settings']['delimiter'])) { $va_item['settings']['delimiter'] = array($va_item['settings']['delimiter']); } if (sizeof($va_item['settings']['delimiter'])) { foreach ($va_item['settings']['delimiter'] as $vn_index => $vs_delim) { $va_item['settings']['delimiter'][$vn_index] = preg_quote($vs_delim, "!"); } $va_val_list = preg_split("!(" . join("|", $va_item['settings']['delimiter']) . ")!", $vm_val); // Add delimited values foreach ($va_val_list as $vs_list_val) { $vs_list_val = trim(ca_data_importers::replaceValue($vs_list_val, $va_item)); if ($vn_max_length && mb_strlen($vs_list_val) > $vn_max_length) { $vs_list_val = mb_substr($vs_list_val, 0, $vn_max_length); } $va_group_buf[$vn_c] = array($vs_item_terminal => $vs_list_val, '_errorPolicy' => $vs_item_error_policy); $vn_c++; } $vn_row++; continue; // Don't add "regular" value below } } if ($vn_max_length && mb_strlen($vm_val) > $vn_max_length) { $vm_val = mb_substr($vm_val, 0, $vn_max_length); } switch ($vs_item_terminal) { case 'preferred_labels': case 'nonpreferred_labels': if ($t_instance = $o_dm->getInstanceByTableName($vs_target_table, true)) { $va_group_buf[$vn_c][$t_instance->getLabelDisplayField()] = $vm_val; } if ($o_trans) { $t_instance->setTransaction($o_trans); } if (!$vb_item_error_policy_is_default || !isset($va_group_buf[$vn_c]['_errorPolicy'])) { if (is_array($va_group_buf[$vn_c])) { $va_group_buf[$vn_c]['_errorPolicy'] = $vs_item_error_policy; } } if ($vs_item_terminal == 'preferred_labels') { $vs_preferred_label_for_log = $vm_val; } break; default: $va_group_buf[$vn_c][$vs_item_terminal] = $vm_val; if (!$vb_item_error_policy_is_default || !isset($va_group_buf[$vn_c]['_errorPolicy'])) { if (is_array($va_group_buf[$vn_c])) { $va_group_buf[$vn_c]['_errorPolicy'] = $vs_item_error_policy; } } break; } } // end foreach($va_vals as $vm_val) } foreach ($va_group_buf as $vn_group_index => $va_group_data) { $va_ptr =& $va_content_tree; foreach ($va_group_tmp as $vs_tmp) { if (!is_array($va_ptr[$vs_tmp])) { $va_ptr[$vs_tmp] = array(); } $va_ptr =& $va_ptr[$vs_tmp]; if ($vs_tmp == $vs_target_table) { // add numeric index after table to ensure repeat values don't overwrite each other $va_parent =& $va_ptr; $va_ptr[] = array(); $va_ptr =& $va_ptr[sizeof($va_ptr) - 1]; } } $va_ptr = $va_group_data; } } // // Process out self-relationships // if (is_array($va_content_tree[$vs_subject_table])) { $va_self_related_content = array(); foreach ($va_content_tree[$vs_subject_table] as $vn_i => $va_element_data) { if (isset($va_element_data['_relationship_type'])) { $va_self_related_content[] = $va_element_data; unset($va_content_tree[$vs_subject_table][$vn_i]); } } if (sizeof($va_self_related_content) > 0) { $va_content_tree["related.{$vs_subject_table}"] = $va_self_related_content; } } $vn_row++; $o_log->logDebug(_t('Finished building content tree for %1 at %2 seconds', $vs_idno, $t->getTime(4))); $o_log->logDebug(_t("Content tree is\n%1", print_R($va_content_tree, true))); // // Process data in subject record // //print_r($va_content_tree); //die("END\n\n"); //continue; $opa_app_plugin_manager->hookDataImportContentTree(array('mapping' => $t_mapping, 'content_tree' => &$va_content_tree, 'idno' => &$vs_idno, 'transaction' => &$o_trans, 'log' => &$o_log, 'reader' => $o_reader, 'environment' => $va_environment, 'importEvent' => $o_event, 'importEventSource' => $vn_row)); //print_r($va_content_tree); //die("done\n"); if (!sizeof($va_content_tree) && !str_replace("%", "", $vs_idno)) { continue; } if (!$t_subject->getPrimaryKey()) { $o_event->beginItem($vn_row, $t_subject->tableNum(), 'I'); $t_subject->setMode(ACCESS_WRITE); $t_subject->set($vs_type_id_fld, $vs_type); if ($vb_idno_is_template) { $t_subject->setIdnoWithTemplate($vs_idno); } else { $t_subject->set($vs_idno_fld, $vs_idno, array('assumeIdnoForRepresentationID' => true, 'assumeIdnoStubForLotID' => true)); // assumeIdnoStubForLotID forces ca_objects.lot_id values to always be considered as a potential idno_stub first, before use as a ca_objects.lot_id } // Look for parent_id in the content tree $vs_parent_id_fld = $t_subject->getProperty('HIERARCHY_PARENT_ID_FLD'); foreach ($va_content_tree as $vs_table_name => $va_content) { if ($vs_table_name == $vs_subject_table) { foreach ($va_content as $va_element_data) { foreach ($va_element_data as $vs_element => $va_element_content) { switch ($vs_element) { case $vs_parent_id_fld: if ($va_element_content[$vs_parent_id_fld]) { $t_subject->set($vs_parent_id_fld, $va_element_content[$vs_parent_id_fld], array('treatParentIDAsIdno' => true)); } break; } } } } } foreach ($va_mandatory_field_mapping_ids as $vs_mandatory_field => $vn_mandatory_mapping_item_id) { $t_subject->set($vs_mandatory_field, $va_mandatory_field_values[$vs_mandatory_field], array('assumeIdnoStubForLotID' => true)); } $t_subject->insert(); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("Could not insert new record"), array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_import_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to import error policy')); if ($vb_use_ncurses) { ncurses_end(); } $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno)); if ($o_trans) { $o_trans->rollback(); } return false; } continue; } $o_log->logDebug(_t('Created idno %1 at %2 seconds', $vs_idno, $t->getTime(4))); } else { $o_event->beginItem($vn_row, $t_subject->tableNum(), 'U'); // update $t_subject->setMode(ACCESS_WRITE); if ($vb_idno_is_template) { $t_subject->setIdnoWithTemplate($vs_idno); } else { $t_subject->set($vs_idno_fld, $vs_idno, array('assumeIdnoStubForLotID' => true)); // assumeIdnoStubForLotID forces ca_objects.lot_id values to always be considered as a potential idno_stub first, before use as a ca_objects.lot_di } $t_subject->update(); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("Could not update matched record"), array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_import_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to import error policy')); if ($vb_use_ncurses) { ncurses_end(); } $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno)); if ($o_trans) { $o_trans->rollback(); } return false; } continue; } $t_subject->clearErrors(); if (sizeof($va_preferred_label_mapping_ids) && $t_subject->getPreferredLabelCount() > 0) { $t_subject->removeAllLabels(__CA_LABEL_TYPE_PREFERRED__); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("Could not update remove preferred labels from matched record"), array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_import_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to import error policy')); if ($vb_use_ncurses) { ncurses_end(); } if ($o_trans) { $o_trans->rollback(); } $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno)); return false; } } } $o_log->logDebug(_t('Updated idno %1 at %2 seconds', $vs_idno, $t->getTime(4))); } if ($vs_idno_fld && ($o_idno = $t_subject->getIDNoPlugInInstance())) { $va_values = $o_idno->htmlFormValuesAsArray($vs_idno_fld, $t_subject->get($vs_idno_fld)); if (!is_array($va_values)) { $va_values = array($va_values); } if (($vs_proc_idno = join($o_idno->getSeparator(), $va_values)) && $vs_proc_idno != $vs_idno) { $t_subject->set($vs_idno_fld, $vs_proc_idno); $t_subject->update(); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("Could update idno"), array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_import_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to import error policy')); if ($vb_use_ncurses) { ncurses_end(); } $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno)); if ($o_trans) { $o_trans->rollback(); } return false; } continue; } } } $va_elements_set_for_this_record = array(); foreach ($va_content_tree as $vs_table_name => $va_content) { if ($vs_table_name == $vs_subject_table) { foreach ($va_content as $vn_i => $va_element_data) { foreach ($va_element_data as $vs_element => $va_element_content) { if (is_array($va_element_content)) { $vs_item_error_policy = $va_element_content['_errorPolicy']; unset($va_element_content['_errorPolicy']); } else { $vs_item_error_policy = null; } $t_subject->clearErrors(); $t_subject->setMode(ACCESS_WRITE); switch ($vs_element) { case 'preferred_labels': $t_subject->addLabel($va_element_content, $vn_locale_id, isset($va_element_content['type_id']) ? $va_element_content['type_id'] : null, true); if ($t_subject->numErrors() == 0) { $vb_output_subject_preferred_label = true; } if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add preferred label to %2. Record was deleted because no preferred label could be applied: ", $vs_idno, $t_subject->tableName()), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); $t_subject->delete(true, array('hard' => false)); if ($vs_import_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to import error policy %1', $vs_import_error_policy)); if ($vb_use_ncurses) { ncurses_end(); } $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno)); if ($o_trans) { $o_trans->rollback(); } return false; } if ($vs_item_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to mapping error policy')); if ($vb_use_ncurses) { ncurses_end(); } if ($o_trans) { $o_trans->rollback(); } return false; } continue 5; } break; case 'nonpreferred_labels': $t_subject->addLabel($va_element_content, $vn_locale_id, isset($va_element_content['type_id']) ? $va_element_content['type_id'] : null, false); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add non-preferred label to %2:", $vs_idno, $t_subject->tableName()), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_item_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to mapping error policy')); if ($vb_use_ncurses) { ncurses_end(); } $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno)); if ($o_trans) { $o_trans->rollback(); } return false; } } break; default: if ($t_subject->hasField($vs_element)) { $t_subject->set($vs_element, $va_element_content[$vs_element], array('assumeIdnoStubForLotID' => true)); $t_subject->update(); break; } if ($vs_subject_table == 'ca_representation_annotations' && $vs_element == 'properties') { foreach ($va_element_content as $vs_prop => $vs_prop_val) { $t_subject->setPropertyValue($vs_prop, $vs_prop_val); } break; } if (is_array($va_element_content)) { $va_element_content['locale_id'] = $vn_locale_id; } if (!isset($va_elements_set_for_this_record[$vs_element]) && !$va_elements_set_for_this_record[$vs_element] && in_array($vs_existing_record_policy, array('merge_on_idno_with_replace', 'merge_on_preferred_labels_with_replace', 'merge_on_idno_and_preferred_labels_with_replace'))) { $t_subject->removeAttributes($vs_element, array('force' => true)); } $va_elements_set_for_this_record[$vs_element] = true; $t_subject->addAttribute($va_element_content, $vs_element, null, array('showRepeatCountErrors' => true, 'alwaysTreatValueAsIdno' => true)); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Failed to add value for %2; values were %3: ", $vs_idno, $vs_element, ca_data_importers::formatValuesForLog($va_element_content)), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_item_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to mapping error policy')); if ($vb_use_ncurses) { ncurses_end(); } $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno)); if ($o_trans) { $o_trans->rollback(); } return false; } } $t_subject->update(); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Invalid %2; values were %3: ", $vs_idno, $vs_element, ca_data_importers::formatValuesForLog($va_element_content)), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_item_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to mapping error policy')); if ($vb_use_ncurses) { ncurses_end(); } $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno)); if ($o_trans) { $o_trans->rollback(); } return false; } } break; } } } } else { // related $vs_table_name = preg_replace('!^related\\.!', '', $vs_table_name); foreach ($va_content as $vn_i => $va_element_data) { $va_match_on = caGetOption('_matchOn', $va_element_data, null); $vb_dont_create = caGetOption('_dontCreate', $va_element_data, null); $va_data_for_rel_table = $va_element_data; $va_nonpreferred_labels = isset($va_data_for_rel_table['nonpreferred_labels']) ? $va_data_for_rel_table['nonpreferred_labels'] : null; unset($va_data_for_rel_table['preferred_labels']); unset($va_data_for_rel_table['_relationship_type']); unset($va_data_for_rel_table['_type']); unset($va_data_for_rel_table['_parent_id']); unset($va_data_for_rel_table['_errorPolicy']); unset($va_data_for_rel_table['_matchOn']); $va_data_for_rel_table = array_merge($va_data_for_rel_table, ca_data_importers::_extractIntrinsicValues($va_data_for_rel_table, $vs_table_name)); $t_subject->clearErrors(); switch ($vs_table_name) { case 'ca_objects': if ($vn_rel_id = DataMigrationUtils::getObjectID($va_element_data['preferred_labels']['name'], $va_element_data['_parent_id'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) { if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) { break; } $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial'])); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related object with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_item_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to mapping error policy')); if ($vb_use_ncurses) { ncurses_end(); } if ($o_trans) { $o_trans->rollback(); } return false; } } } break; case 'ca_object_lots': $vs_idno_stub = null; if (is_array($va_element_data['idno_stub'])) { $vs_idno_stub = isset($va_element_data['idno_stub']['idno_stub']) ? $va_element_data['idno_stub']['idno_stub'] : ''; } else { $vs_idno_stub = isset($va_element_data['idno_stub']) ? $va_element_data['idno_stub'] : ''; } if ($vn_rel_id = DataMigrationUtils::getObjectLotID($vs_idno_stub, $va_element_data['preferred_labels']['name'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) { if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno_stub']['_relationship_type'])) { break; } $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial'])); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related object lot with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_item_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to mapping error policy')); if ($vb_use_ncurses) { ncurses_end(); } if ($o_trans) { $o_trans->rollback(); } return false; } } } break; case 'ca_entities': if ($vn_rel_id = DataMigrationUtils::getEntityID($va_element_data['preferred_labels'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) { if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) { break; } $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial'])); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related entity with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_item_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to mapping error policy')); if ($vb_use_ncurses) { ncurses_end(); } if ($o_trans) { $o_trans->rollback(); } return false; } } } break; case 'ca_places': if ($vn_rel_id = DataMigrationUtils::getPlaceID($va_element_data['preferred_labels']['name'], $va_element_data['_parent_id'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) { if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) { break; } $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial'])); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related place with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_item_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to mapping error policy')); if ($vb_use_ncurses) { ncurses_end(); } if ($o_trans) { $o_trans->rollback(); } return false; } } } break; case 'ca_collections': if ($vn_rel_id = DataMigrationUtils::getCollectionID($va_element_data['preferred_labels']['name'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) { if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) { break; } $t_subject->addRelationship($vs_table_name, $vn_rel_id, $vs_rel_type, null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial'])); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related collection with relationship %2:", $vs_idno, $vs_rel_type), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_item_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to mapping error policy')); if ($vb_use_ncurses) { ncurses_end(); } if ($o_trans) { $o_trans->rollback(); } return false; } } } break; case 'ca_occurrences': if ($vn_rel_id = DataMigrationUtils::getOccurrenceID($va_element_data['preferred_labels']['name'], $va_element_data['_parent_id'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) { if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) { break; } $t_subject->addRelationship($vs_table_name, $vn_rel_id, $vs_rel_type, null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial'])); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related occurrence with relationship %2:", $vs_idno, $vs_rel_type), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_item_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to mapping error policy')); if ($vb_use_ncurses) { ncurses_end(); } if ($o_trans) { $o_trans->rollback(); } return false; } } } break; case 'ca_storage_locations': if ($vn_rel_id = DataMigrationUtils::getStorageLocationID($va_element_data['preferred_labels']['name'], $va_element_data['_parent_id'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) { if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) { break; } $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial'])); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related storage location with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_item_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to mapping error policy')); if ($vb_use_ncurses) { ncurses_end(); } if ($o_trans) { $o_trans->rollback(); } return false; } } } break; case 'ca_list_items': $va_data_for_rel_table['is_enabled'] = 1; $va_data_for_rel_table['preferred_labels'] = $va_element_data['preferred_labels']; if ($vn_rel_id = DataMigrationUtils::getListItemID($va_element_data['_list'], $va_element_data['idno'] ? $va_element_data['idno'] : null, $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) { if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) { break; } $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial'])); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related list item with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_item_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to mapping error policy')); if ($vb_use_ncurses) { ncurses_end(); } if ($o_trans) { $o_trans->rollback(); } return false; } } } break; case 'ca_object_representations': if ($vn_rel_id = DataMigrationUtils::getObjectRepresentationID($va_element_data['preferred_labels']['name'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels, 'matchMediaFilesWithoutExtension' => true))) { $t_subject->linkRepresentation($vn_rel_id, null, null, null, null, array('type_id' => trim($va_element_data['_relationship_type']), 'is_primary' => true)); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related object representation with:", $vs_idno), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_item_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to mapping error policy')); if ($vb_use_ncurses) { ncurses_end(); } if ($o_trans) { $o_trans->rollback(); } return false; } } } // // if (($vs_subject_table_name == 'ca_objects') && $va_element_data['media']['media']) { // unset($va_data_for_rel_table['media']); // // foreach($va_data_for_rel_table as $vs_key => $vm_val) { // // Attributes, including intrinsics are in two-level format, eg. idno is $va_attributes['idno']['idno'] // // but addRepresentations() expects intrinsics to be single level (eg. $va_attributes['idno']) so // // we do some rewriting here // if (is_array($vm_val) && isset($vm_val[$vs_key])) { // $va_data_for_rel_table[$vs_key] = $vm_val[$vs_key]; // } // } // // if (!($t_subject->addRepresentation($va_element_data['media']['media'], isset($va_element_data['_type']) ? $va_element_data['_type'] : caGetDefaultItemID('object_representation_types'), $vn_locale_id, 0, 0, true, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on)))) { // $vs_error = join("; ", $t_subject->getErrors()); // ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); // if ($vs_item_error_policy == 'stop') { // $o_log->logAlert(_t('Import stopped due to mapping error policy')); // if($vb_use_ncurses) { ncurses_end(); } // if ($o_trans) { $o_trans->rollback(); } // return false; // } // } // } break; case 'ca_loans': if ($vn_rel_id = DataMigrationUtils::getLoanID($va_element_data['preferred_labels']['name'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) { if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) { break; } $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial'])); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related loan with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_item_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to mapping error policy')); if ($vb_use_ncurses) { ncurses_end(); } if ($o_trans) { $o_trans->rollback(); } return false; } } } break; case 'ca_movements': if ($vn_rel_id = DataMigrationUtils::getMovementID($va_element_data['preferred_labels']['name'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) { if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) { break; } $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial'])); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related movement with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_item_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to mapping error policy')); if ($vb_use_ncurses) { ncurses_end(); } if ($o_trans) { $o_trans->rollback(); } return false; } } } break; } if (is_array($va_element_data['_related_related']) && sizeof($va_element_data['_related_related'])) { foreach ($va_element_data['_related_related'] as $vs_rel_rel_table => $va_rel_rels) { foreach ($va_rel_rels as $vn_i => $va_rel_rel) { if (!($t_rel_instance = $o_dm->getInstanceByTableName($vs_table_name))) { $o_log->logWarn(_t("[%1] Could not instantiate related table %2", $vs_idno, $vs_table_name)); continue; } if ($o_trans) { $t_rel_instance->setTransaction($o_trans); } if ($t_rel_instance->load($vn_rel_id)) { if ($t_rel_rel = $t_rel_instance->addRelationship($vs_rel_rel_table, $va_rel_rel['id'], $va_rel_rel['_relationship_type'])) { $o_log->logInfo(_t('[%1] Related %2 (%3) to related %4 with relationship %5', $vs_idno, $o_dm->getTableProperty($vs_rel_rel_table, 'NAME_SINGULAR'), $va_rel_rel['id'], $t_rel_instance->getProperty('NAME_SINGULAR'), trim($va_rel_rel['_relationship_type']))); } else { if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related %2 (%3) to related %4 with relationship %5:", $vs_idno, $o_dm->getTableProperty($vs_rel_rel_table, 'NAME_SINGULAR'), $va_rel_rel['id'], $t_rel_instance->getProperty('NAME_SINGULAR'), trim($va_rel_rel['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); } } } } } } } } } // $t_subject->update(); // // if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Invalid %2; values were %3: ", $vs_idno, 'attributes', ca_data_importers::formatValuesForLog($va_element_content)), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) { // ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); // if ($vs_item_error_policy == 'stop') { // $o_log->logAlert(_t('Import stopped due to mapping error policy')); // if($vb_use_ncurses) { ncurses_end(); } // // $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno)); // // if ($o_trans) { $o_trans->rollback(); } // return false; // } // } // $o_log->logDebug(_t('Finished inserting content tree for %1 at %2 seconds into database', $vs_idno, $t->getTime(4))); if (!$vb_output_subject_preferred_label && $t_subject->getPreferredLabelCount() == 0) { $t_subject->addLabel(array($vs_label_display_fld => '???'), $vn_locale_id, null, true); if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add default label", $vs_idno), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) { ca_data_importers::logImportError($vs_error, $va_log_import_error_opts); if ($vs_import_error_policy == 'stop') { $o_log->logAlert(_t('Import stopped due to import error policy')); if ($vb_use_ncurses) { ncurses_end(); } $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno)); if ($o_trans) { $o_trans->rollback(); } return false; } } } $o_log->logInfo(_t('[%1] Imported %2 as %3 ', $vs_idno, $vs_preferred_label_for_log, $vs_subject_table_name)); $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_SUCCESS__, _t('Imported %1', $vs_idno)); ca_data_importers::$s_num_records_processed++; } $o_log->logInfo(_t('Import of %1 completed using mapping %2: %3 imported/%4 skipped/%5 errors', $ps_source, $t_mapping->get('importer_code'), ca_data_importers::$s_num_records_processed, ca_data_importers::$s_num_records_skipped, ca_data_importers::$s_num_import_errors)); //if ($vb_show_cli_progress_bar) { $o_progress->finish(); //} if ($po_request && isset($pa_options['progressCallback']) && ($ps_callback = $pa_options['progressCallback'])) { $ps_callback($po_request, $pn_file_number, $pn_number_of_files, $ps_source, $vn_num_items, $vn_num_items, _t('Import completed'), time() - $vn_start_time, memory_get_usage(true), ca_data_importers::$s_num_records_processed, ca_data_importers::$s_num_import_errors); } if (isset($pa_options['reportCallback']) && ($ps_callback = $pa_options['reportCallback'])) { $va_general = array('elapsedTime' => time() - $vn_start_time, 'numErrors' => ca_data_importers::$s_num_import_errors, 'numProcessed' => ca_data_importers::$s_num_records_processed); $ps_callback($po_request, $va_general, ca_data_importers::$s_import_error_list, true); } if ($vb_use_ncurses) { ncurses_end(); } if ($pb_dry_run) { if ($o_trans) { $o_trans->rollback(); } $o_log->logInfo(_t('Rollback successful import run in "dry run" mode')); } else { if ($o_trans) { $o_trans->commit(); } } return true; }
/** * Forces a full reindex of all rows in the database or, optionally, a single table * * @param array $pa_table_name * @param array $pa_options Reindexing options: * showProgress * interactiveProgressDisplay * log * callback */ public function reindex($pa_table_names = null, $pa_options = null) { define('__CollectiveAccess_IS_REINDEXING__', 1); $t_timer = new Timer(); $pb_display_progress = isset($pa_options['showProgress']) ? (bool) $pa_options['showProgress'] : true; $pb_interactive_display = isset($pa_options['interactiveProgressDisplay']) ? (bool) $pa_options['interactiveProgressDisplay'] : false; $ps_callback = isset($pa_options['callback']) ? (string) $pa_options['callback'] : false; if ($pa_table_names) { if (!is_array($pa_table_names)) { $pa_table_names = array($pa_table_names); } $va_table_names = array(); foreach ($pa_table_names as $vs_table) { if ($this->opo_datamodel->tableExists($vs_table)) { $vn_num = $this->opo_datamodel->getTableNum($vs_table); print "\nTRUNCATING {$vs_table}\n\n"; $this->opo_engine->truncateIndex($vn_num); $t_instance = $this->opo_datamodel->getInstanceByTableName($vs_table, true); $va_table_names[$vn_num] = array('name' => $vs_table, 'num' => $vn_num, 'displayName' => $t_instance->getProperty('NAME_PLURAL')); } } if (!sizeof($va_table_names)) { return false; } } else { // full reindex $this->opo_engine->truncateIndex(); $va_table_names = $this->getIndexedTables(); } $o_db = $this->opo_db; if ($pb_display_progress || $ps_callback) { $va_names = array(); foreach ($va_table_names as $vn_table_num => $va_table_info) { $va_names[] = $va_table_info['displayName']; } if ($pb_display_progress) { print "\nWILL INDEX [" . join(", ", $va_names) . "]\n\n"; } } $vn_tc = 0; foreach ($va_table_names as $vn_table_num => $va_table_info) { $vs_table = $va_table_info['name']; $t_table_timer = new Timer(); $t_instance = $this->opo_datamodel->getInstanceByTableName($vs_table, true); $vs_table_pk = $t_instance->primaryKey(); $va_fields_to_index = $this->getFieldsToIndex($vn_table_num); if (!is_array($va_fields_to_index) || sizeof($va_fields_to_index) == 0) { continue; } $qr_all = $o_db->query("SELECT " . $t_instance->primaryKey() . " FROM {$vs_table}"); $vn_num_rows = $qr_all->numRows(); if ($pb_display_progress) { print CLIProgressBar::start($vn_num_rows, _t('Indexing %1', $t_instance->getProperty('NAME_PLURAL'))); } $vn_c = 0; while ($qr_all->nextRow()) { $t_instance->load($qr_all->get($t_instance->primaryKey())); $t_instance->doSearchIndexing(array(), true, $this->opo_engine->engineName()); if ($pb_display_progress && $pb_interactive_display) { print CLIProgressBar::next(); } if ($ps_callback && !($vn_c % 100)) { $ps_callback($vn_c, $vn_num_rows, null, null, (double) $t_timer->getTime(2), memory_get_usage(true), $va_table_names, $vn_table_num, $t_instance->getProperty('NAME_PLURAL'), $vn_tc + 1); } $vn_c++; } $qr_all->free(); unset($t_instance); if ($pb_display_progress && $pb_interactive_display) { print CLIProgressBar::finish(); } $this->opo_engine->optimizeIndex($vn_table_num); $vn_tc++; } if ($pb_display_progress) { print "\n\n\nDone! [Indexing for " . join(", ", $va_names) . " took " . caFormatInterval((double) $t_timer->getTime(4)) . "]\n"; } if ($ps_callback) { $ps_callback(1, 1, _t('Elapsed time: %1', caFormatInterval((double) $t_timer->getTime(2))), _t('Index rebuild complete!'), (double) $t_timer->getTime(2), memory_get_usage(true), $va_table_names, null, null, sizeof($va_table_names)); } }
/** * Execute a MySQL query * * @param $query Query to execute * * @return Query result handler **/ function query($query) { global $CFG_GLPI, $DEBUG_SQL, $SQL_TOTAL_REQUEST; if ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE && $CFG_GLPI["debug_sql"]) { $SQL_TOTAL_REQUEST++; $DEBUG_SQL["queries"][$SQL_TOTAL_REQUEST] = $query; $TIMER = new Timer(); $TIMER->start(); } $res = @$this->dbh->query($query); if (!$res) { // no translation for error logs $error = "*** MySQL query error: \n***\nSQL: " . addslashes($query) . "\nError: " . $this->dbh->error . "\n"; if (function_exists("debug_backtrace")) { $error .= "Backtrace :\n"; $traces = debug_backtrace(); foreach ($traces as $trace) { $error .= (isset($trace["file"]) ? $trace["file"] : "") . " :" . (isset($trace["line"]) ? $trace["line"] : "") . "\t\t" . (isset($trace["class"]) ? $trace["class"] : "") . (isset($trace["type"]) ? $trace["type"] : "") . (isset($trace["function"]) ? $trace["function"] . "()" : "") . "\n"; } } else { $error .= "Script : "; } $error .= $_SERVER["SCRIPT_FILENAME"] . "\n"; Toolbox::logInFile("sql-errors", $error . "\n"); if ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE && $CFG_GLPI["debug_sql"]) { $DEBUG_SQL["errors"][$SQL_TOTAL_REQUEST] = $this->error(); } } if ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE && $CFG_GLPI["debug_sql"]) { $TIME = $TIMER->getTime(); $DEBUG_SQL["times"][$SQL_TOTAL_REQUEST] = $TIME; } return $res; }
/** * Executes a SQL statement * * @param mixed $po_caller object representation of the calling class, usually Db() * @param DbStatement $opo_statement * @param string $ps_sql SQL statement * @param array $pa_values array of placeholder replacements */ public function execute($po_caller, $opo_statement, $ps_sql, $pa_values) { if (!$ps_sql) { $opo_statement->postError(240, _t("Query is empty"), "Db->mysql->execute()"); return false; } $vs_sql = $ps_sql; $va_placeholder_map = $opo_statement->getOption('placeholder_map'); $vn_needed_values = sizeof($va_placeholder_map); if ($vn_needed_values != sizeof($pa_values)) { $opo_statement->postError(285, _t("Number of values passed (%1) does not equal number of values required (%2)", sizeof($pa_values), $vn_needed_values), "Db->mysql->execute()"); return false; } for ($vn_i = sizeof($pa_values) - 1; $vn_i >= 0; $vn_i--) { if (is_array($pa_values[$vn_i])) { foreach ($pa_values[$vn_i] as $vn_x => $vs_vx) { $pa_values[$vn_i][$vn_x] = $this->autoQuote($vs_vx); } $vs_sql = substr_replace($vs_sql, join(',', $pa_values[$vn_i]), $va_placeholder_map[$vn_i], 1); } else { $vs_sql = substr_replace($vs_sql, $this->autoQuote($pa_values[$vn_i]), $va_placeholder_map[$vn_i], 1); } } $va_limit_info = $opo_statement->getLimit(); if ($va_limit_info["limit"] > 0 || $va_limit_info["offset"] > 0) { if (!preg_match("/LIMIT[ ]+[\\d]+[,]{0,1}[\\d]*\$/i", $vs_sql)) { // check for LIMIT clause is raw SQL $vn_limit = $va_limit_info["limit"]; if ($vn_limit == 0) { $vn_limit = 4000000000.0; } $vs_sql .= " LIMIT " . intval($va_limit_info["offset"]) . "," . intval($vn_limit); } } if (Db::$monitor) { $t = new Timer(); } if (!($r_res = mysql_query($vs_sql, $this->opr_db))) { $vn_mysql_err = (int) mysql_errno($this->opr_db); switch ($vn_mysql_err) { case 1205: // deadlock // deadlock case 1216: // deadlock $vn_tries = 0; // wait a bit and try the query again (up to 10 times) while ($vn_tries < 10) { usleep(500); if ($r_res = mysql_query($vs_sql, $this->opr_db)) { break; } $vn_tries++; } if (!$r_res) { $opo_statement->postError($this->nativeToDbError($vn_mysql_err), mysql_error($this->opr_db) . (__CA_ENABLE_DEBUG_OUTPUT__ ? "\n<pre>" . caPrintStacktrace() . "</pre>" : ""), "Db->mysql->execute()"); return false; } return new DbResult($this, $r_res); break; case 2006: // gone away // reconnect if ($this->connect()) { if ($r_res = mysql_query($vs_sql, $this->opr_db)) { return new DbResult($this, $r_res); } } $opo_statement->postError($this->nativeToDbError($vn_mysql_err), mysql_error($this->opr_db) . (__CA_ENABLE_DEBUG_OUTPUT__ ? "\n<pre>" . caPrintStacktrace() . "</pre>" : ""), "Db->mysql->execute()"); break; default: $opo_statement->postError($this->nativeToDbError($vn_mysql_err), mysql_error($this->opr_db) . (__CA_ENABLE_DEBUG_OUTPUT__ ? "\n<pre>" . caPrintStacktrace() . "</pre>" : ""), "Db->mysql->execute()"); break; } return false; } if (Db::$monitor) { Db::$monitor->logQuery($ps_sql, $pa_values, $t->getTime(4), is_bool($r_res) ? null : mysql_num_rows($r_res)); } return new DbResult($this, $r_res); }
/** * */ public function search($pn_subject_tablenum, $ps_search_expression, $pa_filters = array(), $po_rewritten_query = null) { $t = new Timer(); $this->_setMode('search'); $this->opa_filters = $pa_filters; if (!($t_instance = $this->opo_datamodel->getInstanceByTableNum($pn_subject_tablenum, true))) { // TODO: Better error message die("Invalid subject table"); } $va_restrict_to_fields = array(); if (is_array($this->getOption('restrictSearchToFields'))) { foreach ($this->getOption('restrictSearchToFields') as $vs_f) { $va_restrict_to_fields[] = $this->_getElementIDForAccessPoint($pn_subject_tablenum, $vs_f); } } $this->opo_db->query('SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED'); if (trim($ps_search_expression) === '((*))') { $vs_table_name = $t_instance->tableName(); $vs_pk = $t_instance->primaryKey(); // do we need to filter? $va_filters = $this->getFilters(); $va_joins = array(); $va_wheres = array(); if (is_array($va_filters) && sizeof($va_filters)) { foreach ($va_filters as $va_filter) { $va_tmp = explode('.', $va_filter['field']); $va_path = array(); if ($va_tmp[0] != $vs_table_name) { $va_path = $this->opo_datamodel->getPath($vs_table_name, $va_tmp[0]); } if (sizeof($va_path)) { $vs_last_table = null; // generate related joins foreach ($va_path as $vs_table => $va_info) { $t_table = $this->opo_datamodel->getInstanceByTableName($vs_table, true); if ($vs_last_table) { $va_rels = $this->opo_datamodel->getOneToManyRelations($vs_last_table, $vs_table); if (!sizeof($va_rels)) { $va_rels = $this->opo_datamodel->getOneToManyRelations($vs_table, $vs_last_table); } if ($vs_table == $va_rels['one_table']) { $va_joins[$vs_table] = "INNER JOIN " . $va_rels['one_table'] . " ON " . $va_rels['one_table'] . "." . $va_rels['one_table_field'] . " = " . $va_rels['many_table'] . "." . $va_rels['many_table_field']; } else { $va_joins[$vs_table] = "INNER JOIN " . $va_rels['many_table'] . " ON " . $va_rels['many_table'] . "." . $va_rels['many_table_field'] . " = " . $va_rels['one_table'] . "." . $va_rels['one_table_field']; } } $t_last_table = $t_table; $vs_last_table = $vs_table; } $vs_where = "(" . $va_filter['field'] . " " . $va_filter['operator'] . " " . $this->_filterValueToQueryValue($va_filter) . ")"; } else { // join in primary table $vs_where = "(" . $va_filter['field'] . " " . $va_filter['operator'] . " " . $this->_filterValueToQueryValue($va_filter) . ")"; } if (in_array('NULL', $va_filter)) { switch ($va_filter['operator']) { case 'in': if (strpos(strtolower($va_filter['value']), 'null') !== false) { $vs_where = "({$vs_where} OR (" . $va_filter['field'] . " IS NULL))"; } break; case 'not in': if (strpos(strtolower($va_filter['value']), 'null') !== false) { $vs_where = "({$vs_where} OR (" . $va_filter['field'] . " IS NOT NULL))"; } break; } } $va_wheres[] = $vs_where; } } $vs_join_sql = join("\n", $va_joins); $vs_where_sql = ''; if (sizeof($va_wheres)) { $vs_where_sql = " WHERE " . join(" AND ", $va_wheres); } $vs_sql = "\n\t\t\t\tSELECT {$vs_table_name}.{$vs_pk} row_id \n\t\t\t\tFROM {$vs_table_name}\n\t\t\t\t{$vs_join_sql}\n\t\t\t\t{$vs_where_sql}\n\t\t\t\tORDER BY\n\t\t\t\t\trow_id\n\t\t\t"; $qr_res = $this->opo_db->query($vs_sql); } else { $this->_createTempTable('ca_sql_search_search_final'); $this->_doQueriesForSqlSearch($po_rewritten_query, $pn_subject_tablenum, 'ca_sql_search_search_final', 0, array('restrictSearchToFields' => $va_restrict_to_fields)); Debug::msg("doqueries for {$ps_search_expression} took " . $t->getTime(4)); // do we need to filter? $va_filters = $this->getFilters(); $va_joins = array(); $va_wheres = array(); if (is_array($va_filters) && sizeof($va_filters)) { foreach ($va_filters as $va_filter) { $va_tmp = explode('.', $va_filter['field']); $va_path = array(); if ($va_tmp[0] != $vs_table_name) { $va_path = $this->opo_datamodel->getPath($vs_table_name, $va_tmp[0]); } if (sizeof($va_path)) { $vs_last_table = null; // generate related joins foreach ($va_path as $vs_table => $va_info) { $t_table = $this->opo_datamodel->getInstanceByTableName($vs_table, true); if (!$vs_last_table) { $va_joins[$vs_table] = "INNER JOIN " . $vs_table . " ON " . $vs_table . "." . $t_table->primaryKey() . " = ca_sql_search_search_final.row_id"; } else { $va_rels = $this->opo_datamodel->getOneToManyRelations($vs_last_table, $vs_table); if (!sizeof($va_rels)) { $va_rels = $this->opo_datamodel->getOneToManyRelations($vs_table, $vs_last_table); } if ($vs_table == $va_rels['one_table']) { $va_joins[$vs_table] = "INNER JOIN " . $va_rels['one_table'] . " ON " . $va_rels['one_table'] . "." . $va_rels['one_table_field'] . " = " . $va_rels['many_table'] . "." . $va_rels['many_table_field']; } else { $va_joins[$vs_table] = "INNER JOIN " . $va_rels['many_table'] . " ON " . $va_rels['many_table'] . "." . $va_rels['many_table_field'] . " = " . $va_rels['one_table'] . "." . $va_rels['one_table_field']; } } $t_last_table = $t_table; $vs_last_table = $vs_table; } $vs_where = "(" . $va_filter['field'] . " " . $va_filter['operator'] . " " . $this->_filterValueToQueryValue($va_filter) . ")"; } else { $t_table = $this->opo_datamodel->getInstanceByTableName($va_tmp[0], true); // join in primary table if (!isset($va_joins[$va_tmp[0]])) { $va_joins[$va_tmp[0]] = "INNER JOIN " . $va_tmp[0] . " ON " . $va_tmp[0] . "." . $t_table->primaryKey() . " = ca_sql_search_search_final.row_id"; } $vs_where = "(" . $va_filter['field'] . " " . $va_filter['operator'] . " " . $this->_filterValueToQueryValue($va_filter) . ")"; } switch ($va_filter['operator']) { case 'in': if (strpos(strtolower($va_filter['value']), 'null') !== false) { $vs_where = "({$vs_where} OR (" . $va_filter['field'] . " IS NULL))"; } break; case 'not in': if (strpos(strtolower($va_filter['value']), 'null') !== false) { $vs_where = "({$vs_where} OR (" . $va_filter['field'] . " IS NOT NULL))"; } break; } $va_wheres[] = $vs_where; } Debug::msg("set up filters for {$ps_search_expression} took " . $t->getTime(4)); } $vs_join_sql = join("\n", $va_joins); $vs_where_sql = ''; if (sizeof($va_wheres)) { $vs_where_sql = " WHERE " . join(" AND ", $va_wheres); } $vs_sql = "\n\t\t\t\tSELECT DISTINCT boost, row_id\n\t\t\t\tFROM ca_sql_search_search_final\n\t\t\t\t{$vs_join_sql}\n\t\t\t\t{$vs_where_sql}\n\t\t\t\tORDER BY\n\t\t\t\t\tboost DESC, row_id\n\t\t\t"; $qr_res = $this->opo_db->query($vs_sql); Debug::msg("search for {$ps_search_expression} took " . $t->getTime(4)); $this->_dropTempTable('ca_sql_search_search_final'); } $this->opo_db->query('SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ'); $va_hits = $qr_res->getAllFieldValues('row_id'); return new WLPlugSearchEngineSqlSearchResult($va_hits, $pn_subject_tablenum); }