public function performPreInstallTasks() { $o_config = Configuration::load(); CompositeCache::flush(); // avoid stale cache // create tmp dir if (!file_exists($o_config->get('taskqueue_tmp_directory'))) { if (!self::createDirectoryPath($o_config->get('taskqueue_tmp_directory'))) { $this->addError("Couldn't create tmp directory at " . $o_config->get('taskqueue_tmp_directory')); return false; } } else { // if already exists then remove all contents to avoid stale cache caRemoveDirectory($o_config->get('taskqueue_tmp_directory'), false); } // Create media directories $o_media_volumes = new MediaVolumes(); $va_media_volumes = $o_media_volumes->getAllVolumeInformation(); $vs_base_dir = $o_config->get('ca_base_dir'); $va_dir_creation_errors = array(); foreach ($va_media_volumes as $vs_label => $va_volume_info) { if (preg_match('!^' . $vs_base_dir . '!', $va_volume_info['absolutePath'])) { if (!self::createDirectoryPath($va_volume_info['absolutePath'])) { $this->addError("Couldn't create directory for media volume {$vs_label}"); return false; } } } return true; }
/** * @param EasyRdf_Graph $po_graph * @param string $ps_base_node * @param string $ps_node_uri * @param int $pn_limit * @param bool $pb_recursive * @return array */ static function getListOfRelatedGraphs($po_graph, $ps_base_node, $ps_node_uri, $pn_limit, $pb_recursive = false) { $vs_cache_key = md5(serialize(func_get_args())); if (CompositeCache::contains($vs_cache_key, 'GettyLinkedDataRelatedGraphs')) { return CompositeCache::fetch($vs_cache_key, 'GettyLinkedDataRelatedGraphs'); } $va_related_nodes = $po_graph->all($ps_base_node, $ps_node_uri); $va_pull_graphs = array(); if (is_array($va_related_nodes)) { $vn_i = 0; foreach ($va_related_nodes as $o_related_node) { $vs_pull_uri = (string) $o_related_node; if (!($o_pull_graph = self::getURIAsRDFGraph($vs_pull_uri))) { return false; } $va_pull_graphs[$vs_pull_uri] = $o_pull_graph; if (++$vn_i >= $pn_limit) { break; } } } if ($pb_recursive) { $va_sub_pull_graphs = array(); foreach ($va_pull_graphs as $vs_pull_uri => $o_pull_graph) { // avoid circular references if (isset($va_pull_graphs[$vs_pull_uri]) || isset($va_sub_pull_graphs[$vs_pull_uri])) { continue; } $va_sub_pull_graphs = array_merge($va_sub_pull_graphs, self::getListOfRelatedGraphs($o_pull_graph, $vs_pull_uri, $ps_node_uri, $pn_limit, true)); } $va_pull_graphs = array_merge($va_pull_graphs, $va_sub_pull_graphs); } CompositeCache::save($vs_cache_key, $va_pull_graphs, 'GettyLinkedDataRelatedGraphs'); return $va_pull_graphs; }
/** * * */ public function parseValue($ps_value, $pa_element_info, $pa_options = null) { $ps_value = trim(preg_replace("![\t\n\r]+!", ' ', $ps_value)); $vs_service = caGetOption('service', $this->getSettingValuesFromElementArray($pa_element_info, array('service'))); //if (!trim($ps_value)) { //$this->postError(1970, _t('Entry was blank.'), 'InformationServiceAttributeValue->parseValue()'); // return false; //} if (trim($ps_value)) { $va_tmp = explode('|', $ps_value); $va_info = array(); if (sizeof($va_tmp) == 3) { /// value is already in desired format (from autocomplete lookup) // get extra indexing info for this uri from plugin implementation $this->opo_plugin = InformationServiceManager::getInformationServiceInstance($vs_service); $vs_display_text = $this->opo_plugin->getDisplayValueFromLookupText($va_tmp[0]); $va_info['indexing_info'] = $this->opo_plugin->getDataForSearchIndexing($pa_element_info['settings'], $va_tmp[2]); $va_info['extra_info'] = $this->opo_plugin->getExtraInfo($pa_element_info['settings'], $va_tmp[2]); return array('value_longtext1' => $vs_display_text, 'value_longtext2' => $va_tmp[2], 'value_decimal1' => $va_tmp[1], 'value_blob' => caSerializeForDatabase($va_info)); } elseif (sizeof($va_tmp) == 1 && (isURL($va_tmp[0], array('strict' => true)) || is_numeric($va_tmp[0]))) { // URI or ID -> try to look it up. we match hit when exactly 1 hit comes back // try lookup cache if (CompositeCache::contains($va_tmp[0], "InformationServiceLookup{$vs_service}")) { return CompositeCache::fetch($va_tmp[0], "InformationServiceLookup{$vs_service}"); } // try lookup $this->opo_plugin = InformationServiceManager::getInformationServiceInstance($vs_service); $va_ret = $this->opo_plugin->lookup($pa_element_info['settings'], $va_tmp[0]); // only match exact results. at some point we might want to try to get fancy // and pick one (or rather, have the plugin pick one) if there's more than one if (is_array($va_ret['results']) && sizeof($va_ret['results']) == 1) { $va_hit = array_shift($va_ret['results']); $va_info['indexing_info'] = $this->opo_plugin->getDataForSearchIndexing($pa_element_info['settings'], $va_hit['url']); $va_info['extra_info'] = $this->opo_plugin->getExtraInfo($pa_element_info['settings'], $va_hit['url']); $vs_display_text = $this->opo_plugin->getDisplayValueFromLookupText($va_hit['label']); $va_return = array('value_longtext1' => $vs_display_text, 'value_longtext2' => $va_hit['url'], 'value_decimal1' => $va_hit['id'], 'value_blob' => caSerializeForDatabase($va_info)); } else { $this->postError(1970, _t('Value for InformationService lookup has to be an ID or URL that returns exactly 1 hit. We got more or no hits. Value was %1', $ps_value), 'ListAttributeValue->parseValue()'); return false; } CompositeCache::save($va_tmp[0], $va_return, "InformationServiceLookup{$vs_service}"); return $va_return; } else { // don't save if value hasn't changed return array('_dont_save' => true); } } return array('value_longtext1' => '', 'value_longtext2' => '', 'value_decimal1' => null, 'value_blob' => null); }
/** * Get the per-job_id progress bar cache with the current state of the progress bar * * @return array|null The cached data or null if no job_id was set or the cache could not be opened. */ private function getCache() { $vs_data = null; if ($this->ops_job_id) { if (CompositeCache::contains($this->ops_job_id, 'ProgressBar')) { $va_data = CompositeCache::fetch($this->ops_job_id, 'ProgressBar'); } else { $va_data = array('total' => $this->getTotal(), 'start' => $this->opn_start, 'position' => $this->getCurrentPosition(), 'message' => $this->getMessage()); } } return $va_data; }
public function Save($pa_values = null) { $t_element = $this->getElementObject(false); $t_element->setMode(ACCESS_WRITE); $va_request = $_REQUEST; /* we don't want to modify $_REQUEST since this may cause ugly side-effects */ foreach ($t_element->getFormFields() as $vs_f => $va_field_info) { $t_element->set($vs_f, $_REQUEST[$vs_f]); unset($va_request[$vs_f]); if ($t_element->numErrors()) { foreach ($t_element->errors() as $o_e) { $this->request->addActionError($o_e, 'general'); $this->notification->addNotification($o_e->getErrorDescription(), __NOTIFICATION_TYPE_ERROR__); } } } if ($vn_parent_id = $this->request->getParameter('parent_id', pInteger)) { $t_element->set('parent_id', $vn_parent_id); } if (!$t_element->getPrimaryKey()) { $vb_new = true; $vo_db = $t_element->getDb(); if ($vn_parent_id) { $qr_tmp = $vo_db->query("\n\t\t\t\t\tSELECT MAX(rank) AS rank\n\t\t\t\t\tFROM ca_metadata_elements\n\t\t\t\t\tWHERE parent_id=?\n\t\t\t\t", $vn_parent_id); if (!$qr_tmp->nextRow()) { $t_element->set('rank', 1); } else { $t_element->set('rank', intval($qr_tmp->get('rank')) + 1); } } $t_element->insert(); $vs_message = _t("Added metadata element"); $this->request->setParameter('element_id', $t_element->getPrimaryKey()); } else { $t_element->update(); $vb_new = false; $vs_message = _t("Saved changes to metadata element"); } if ($t_element->numErrors()) { foreach ($t_element->errors() as $o_e) { $this->request->addActionError($o_e, 'general'); $this->notification->addNotification($o_e->getErrorDescription(), __NOTIFICATION_TYPE_ERROR__); } } else { $this->notification->addNotification($vs_message, __NOTIFICATION_TYPE_INFO__); } if ($t_element->getPrimaryKey()) { $va_new_labels = array(); $va_old_labels = array(); $va_delete_labels = array(); foreach ($va_request as $vs_key => $vs_val) { if (!(strpos($vs_key, 'element_labels_Pref') === false)) { /* label field */ $va_matches = array(); if (!(strpos($vs_key, '_new') === false)) { /* new label field */ preg_match('/element_labels_Pref(.*)_new_([0-9]+)/', $vs_key, $va_matches); $va_new_labels[$va_matches[2]][$va_matches[1]] = $vs_val; } else { if (!(strpos($vs_key, '_delete') === false)) { /* delete label */ preg_match('/element_labels_PrefLabel_([0-9]+)_delete/', $vs_key, $va_matches); $va_delete_labels[] = $va_matches[1]; } else { /* existing label field */ preg_match('/element_labels_Pref(.*)_([0-9]+)/', $vs_key, $va_matches); $va_old_labels[$va_matches[2]][$va_matches[1]] = $vs_val; } } unset($va_request[$vs_key]); } } /* insert new labels */ $t_element_label = new ca_metadata_element_labels(); foreach ($va_new_labels as $va_label) { $t_element_label->clear(); foreach ($va_label as $vs_f => $vs_val) { $t_element_label->set($vs_f, $vs_val); } $t_element_label->set('element_id', $t_element->getPrimaryKey()); $t_element_label->setMode(ACCESS_WRITE); $t_element_label->insert(); if ($t_element_label->numErrors()) { foreach ($t_element_label->errors() as $o_e) { $this->request->addActionError($o_e, 'general'); $this->notification->addNotification($o_e->getErrorDescription(), __NOTIFICATION_TYPE_ERROR__); } } } /* delete labels */ foreach ($va_delete_labels as $vn_label) { $t_element_label->load($vn_label); $t_element_label->setMode(ACCESS_WRITE); $t_element_label->delete(false); } /* process old labels */ foreach ($va_old_labels as $vn_key => $va_label) { $t_element_label->load($vn_key); foreach ($va_label as $vs_f => $vs_val) { $t_element_label->set($vs_f, $vs_val); } $t_element_label->set('element_id', $t_element->getPrimaryKey()); $t_element_label->setMode(ACCESS_WRITE); if ($vb_new) { $t_element_label->insert(); } else { $t_element_label->update(); } if ($t_element_label->numErrors()) { foreach ($t_element_label->errors() as $o_e) { $this->request->addActionError($o_e, 'general'); $this->notification->addNotification($o_e->getErrorDescription(), __NOTIFICATION_TYPE_ERROR__); } } } /* process settings */ if (is_array($va_settings = $t_element->getAvailableSettings())) { $vb_need_to_update = false; foreach ($va_settings as $vs_setting_key => $va_setting_info) { if (isset($va_setting_info['refreshOnChange']) && (bool) $va_setting_info['refreshOnChange']) { $t_element->setSetting($vs_setting_key, $va_request['setting_' . $vs_setting_key]); $vb_need_to_update = true; } } if ($vb_need_to_update) { $t_element->update(); $va_settings = $t_element->getAvailableSettings(); } // we need to unset the form timestamp to disable the 'Changes have been made since you loaded this data' warning // when we update() below. the warning makes sense because an update() is called before we get here, but if there // was an actual concurrent save problem , that very update above would have triggered the warning already $vn_timestamp = $_REQUEST['form_timestamp']; unset($_REQUEST['form_timestamp']); foreach ($va_settings as $vs_setting_key => $va_setting_info) { if (isset($va_request['setting_' . $vs_setting_key . '[]'])) { $vs_val = $va_request['setting_' . $vs_setting_key . '[]']; } else { $vs_val = $va_request['setting_' . $vs_setting_key]; } $vs_error = null; if (!$t_element->setSetting($vs_setting_key, $vs_val, $vs_error)) { $this->notification->addNotification(_t("Setting %2 is not valid: %1", $vs_error, $vs_setting_key), __NOTIFICATION_TYPE_ERROR__); continue; } $t_element->update(); } $_REQUEST['form_timestamp'] = $vn_timestamp; } /* process type restrictions */ $t_restriction = new ca_metadata_type_restrictions(null, true); $va_settings = array_keys($t_restriction->getAvailableSettings()); foreach ($_REQUEST as $vs_key => $vs_value) { if (preg_match('!^type_restrictions_table_num_([\\d]+)$!', $vs_key, $va_matches)) { // got one to update if ($t_restriction->load($va_matches[1])) { $t_restriction->setMode(ACCESS_WRITE); $t_restriction->set('table_num', $this->request->getParameter('type_restrictions_table_num_' . $va_matches[1], pInteger)); $t_restriction->set('type_id', ($vn_type_id = $this->request->getParameter('type_restrictions_type_id_' . $va_matches[1], pInteger)) ? $vn_type_id : null); $t_restriction->set('include_subtypes', ($vn_include_subtypes = $this->request->getParameter('type_restrictions_include_subtypes_' . $va_matches[1], pInteger)) ? $vn_include_subtypes : null); foreach ($va_settings as $vs_setting) { $t_restriction->setSetting($vs_setting, $this->request->getParameter('type_restrictions_setting_' . $vs_setting . '_' . $va_matches[1], pString)); } $t_restriction->update(); } continue; } if (preg_match('!^type_restrictions_table_num_new_([\\d]+)$!', $vs_key, $va_matches)) { // got one to create $t_restriction->setMode(ACCESS_WRITE); $t_restriction->set('element_id', $t_element->getPrimaryKey()); $t_restriction->set('table_num', $this->request->getParameter('type_restrictions_table_num_new_' . $va_matches[1], pInteger)); $t_restriction->set('type_id', ($vn_type_id = $this->request->getParameter('type_restrictions_type_id_new_' . $va_matches[1], pInteger)) ? $vn_type_id : null); $t_restriction->set('include_subtypes', ($vn_include_subtypes = $this->request->getParameter('type_restrictions_include_subtypes_new_' . $va_matches[1], pInteger)) ? $vn_include_subtypes : null); foreach ($va_settings as $vs_setting) { $t_restriction->setSetting($vs_setting, $this->request->getParameter('type_restrictions_setting_' . $vs_setting . '_new_' . $va_matches[1], pString)); } $t_restriction->insert(); continue; } if (preg_match('!^type_restrictions_([\\d]+)_delete$!', $vs_key, $va_matches)) { // got one to delete if ($t_restriction->load($va_matches[1])) { $t_restriction->setMode(ACCESS_WRITE); $t_restriction->delete(); } continue; } } CompositeCache::delete($t_element->getPrimaryKey(), 'ElementSets'); CompositeCache::delete($t_element->getPrimaryKey(), 'ElementSetIds'); } $this->Edit(); return; }
/** * Returns the most recently viewed items, up to a maximum of $pn_limit (default is 10) * Note that the limit is just that – a limit. getRecentlyViewedItems() may return fewer * than the limit either because there fewer viewed items than your limit or because fetching * additional views would take too long. (To ensure adequate performance getRecentlyViewedItems() uses a cache of * recent views. If there is no cache available it will query the database to look at the most recent (4 x your limit) viewings. * If there are many views of the same item in that group then it is possible that fewer items than your limit will be returned) * * @param int $pn_limit The maximum number of items to return. Default is 10. * @param array $pa_options Supported options are: * restrictToTypes = array of type names or type_ids to restrict to. Only items with a type_id in the list will be returned. * checkAccess = array of access values to filter results by; if defined only items with the specified access code(s) are returned * hasRepresentations = if set to a non-zero (boolean true) value only items with representations will be returned * dontUseCache = if set to true, forces list to be generated from database; default is false. * @return array List of primary key values for recently viewed items. */ public function getRecentlyViewedItems($pn_limit = 10, $pa_options = null) { if (!isset($pa_options['dontUseCache']) || !$pa_options['dontUseCache']) { $va_recently_viewed_items = CompositeCache::fetch('caRecentlyViewed'); $vn_table_num = $this->tableNum(); if (is_array($va_recently_viewed_items) && is_array($va_recently_viewed_items[$vn_table_num])) { if (sizeof($va_recently_viewed_items[$vn_table_num]) > $pn_limit) { $va_recently_viewed_items[$vn_table_num] = array_slice($va_recently_viewed_items[$vn_table_num], 0, $pn_limit); } return $va_recently_viewed_items[$vn_table_num]; } } $o_db = $this->getDb(); $vs_limit_sql = ''; if ($pn_limit > 0) { $vs_limit_sql = "LIMIT " . intval($pn_limit) * 4; } $va_wheres = array('(civ.table_num = ' . intval($this->tableNum()) . ')'); if (is_array($pa_options['checkAccess']) && sizeof($pa_options['checkAccess']) && $this->hasField('access')) { $va_wheres[] = 't.access IN (' . join(',', $pa_options['checkAccess']) . ')'; } if (method_exists($this, 'getTypeFieldName') && ($vs_type_field_name = $this->getTypeFieldName())) { $va_type_ids = caMergeTypeRestrictionLists($this, $pa_options); if (is_array($va_type_ids) && sizeof($va_type_ids)) { $va_wheres[] = "(t.{$vs_type_field_name} IN (" . join(',', $va_type_ids) . ')' . ($this->getFieldInfo($vs_type_field_name, 'IS_NULL') ? " OR t.{$vs_type_field_name} IS NULL" : '') . ')'; } } if (method_exists($this, 'getSourceFieldName') && ($vs_source_id_field_name = $this->getSourceFieldName())) { $va_source_ids = caMergeSourceRestrictionLists($this, $pa_options); if (is_array($va_source_ids) && sizeof($va_source_ids)) { $va_wheres[] = 't.' . $vs_source_id_field_name . ' IN (' . join(',', $va_source_ids) . ')'; } } $vs_join_sql = ''; if (isset($pa_options['hasRepresentations']) && $pa_options['hasRepresentations'] && $this->tableName() == 'ca_objects') { $vs_join_sql = ' INNER JOIN ca_objects_x_object_representations ON ca_objects_x_object_representations.object_id = t.object_id'; $va_wheres[] = 'ca_objects_x_object_representations.is_primary = 1'; } if ($this->hasField('deleted')) { $va_wheres[] = "(t.deleted = 0)"; } if ($vs_where_sql = join(' AND ', $va_wheres)) { $vs_where_sql = ' WHERE ' . $vs_where_sql; } $qr_res = $o_db->query($vs_sql = "\n\t\t\tSELECT t." . $this->primaryKey() . "\n \t\t\tFROM " . $this->tableName() . " t\n \t\t\tINNER JOIN ca_item_views AS civ ON civ.row_id = t." . $this->primaryKey() . "\n \t\t\t{$vs_join_sql}\n\t\t\t{$vs_where_sql}\n\t\t\tORDER BY\n\t\t\t\tciv.view_id DESC\n\t\t\t{$vs_limit_sql}\n\t\t"); $va_recently_viewed_items = array(); $vn_c = 0; while ($qr_res->nextRow() && $vn_c < $pn_limit) { if (!isset($va_recently_viewed_items[$vn_id = $qr_res->get($this->primaryKey())])) { $va_recently_viewed_items[$vn_id] = true; $vn_c++; } } return array_keys($va_recently_viewed_items); }
/** * Returns list of user actions defined by all widgets * * @return array List of user actions keyed by action code */ public static function getWidgetRoleActions() { if ($va_actions = CompositeCache::fetch('role_action_list', 'WidgetManager')) { return $va_actions; } $va_actions = array(); $o_config = Configuration::load(); $vs_base_widget_dir = $o_config->get('dashboard_widgets'); $va_widget_dirs = WidgetManager::getWidgetNames(); foreach ($va_widget_dirs as $vs_widget_dir) { if (!file_exists($vs_base_widget_dir . '/' . $vs_widget_dir . '/' . $vs_widget_dir . 'Widget.php')) { continue; } require_once $vs_base_widget_dir . '/' . $vs_widget_dir . '/' . $vs_widget_dir . 'Widget.php'; $vs_widget_classname = $vs_widget_dir . 'Widget'; $va_actions = array_merge($va_actions, call_user_func(array($vs_widget_classname, 'getRoleActionList'))); } CompositeCache::save('role_action_list', $va_actions, 'WidgetManager'); return $va_actions; }
/** * Returns list of user actions defined by all plugins * * @return array List of user actions keyed by action code */ public static function getPluginRoleActions() { if ($va_actions = CompositeCache::fetch('role_action_list', 'ApplicationPluginManager')) { return $va_actions; } $va_actions = array(); $o_config = Configuration::load(); $vs_app_plugin_dir = $o_config->get('application_plugins'); $va_app_plugin_dirs = ApplicationPluginManager::getPluginNames(); foreach ($va_app_plugin_dirs as $vs_plugin_dir) { if (!file_exists($vs_app_plugin_dir . '/' . $vs_plugin_dir . '/' . $vs_plugin_dir . 'Plugin.php')) { continue; } require_once $vs_app_plugin_dir . '/' . $vs_plugin_dir . '/' . $vs_plugin_dir . 'Plugin.php'; $vs_plugin_classname = $vs_plugin_dir . 'Plugin'; $va_actions = array_merge($va_actions, call_user_func(array($vs_plugin_classname, 'getRoleActionList'))); } CompositeCache::save('role_action_list', $va_actions, 'ApplicationPluginManager'); return $va_actions; }
/** * Fetch details on an item from a remote data source and output results of the 'display' key in the response. * */ public function GetDetail() { $pn_element_id = $this->request->getParameter('element_id', pInteger); $t_element = new ca_metadata_elements($pn_element_id); $va_data = array(); if (!$t_element->getPrimaryKey()) { // error $va_items['error'] = array('label' => _t('ERROR: Invalid element_id'), 'idno' => ''); } else { $vs_service = $t_element->getSetting('service'); $va_settings = $t_element->getSettings(); $pn_attribute_id = $this->request->getParameter('id', pInteger); $t_attr_val = new ca_attribute_values(); if ($t_attr_val->load(array('attribute_id' => $pn_attribute_id, 'element_id' => $pn_element_id))) { $t_attr = new ca_attributes(); if ($t_attr->load($pn_attribute_id)) { if (!caCanRead($this->request->getUserID(), $t_attr->get('table_num'), $t_attr->get('row_id'), $t_element->get('element_code'))) { $va_items['error'] = array('label' => _t('ERROR: You do not have access to this item'), 'idno' => ''); } else { $vs_url = $t_attr_val->get('value_longtext2'); if (!($o_plugin = InformationServiceManager::getInformationServiceInstance($vs_service))) { $va_items['error'] = array('label' => _t('ERROR: Invalid service'), 'idno' => ''); } else { $vs_cache_key = md5(print_r($va_settings, true) . $vs_url); if (CompositeCache::contains($vs_cache_key, 'InformationServiceExtendedInfo')) { $va_data = CompositeCache::fetch($vs_cache_key, 'InformationServiceExtendedInfo'); } else { $va_data = $o_plugin->getExtendedInformation($va_settings, $vs_url); CompositeCache::save($vs_cache_key, $va_data, 'InformationServiceExtendedInfo'); } } } } } } $this->view->setVar('detail', $va_data); return $this->render('ajax_information_service_detail_html.php'); }
public function performPreInstallTasks() { $o_config = Configuration::load(); CompositeCache::flush(); // avoid stale cache // create tmp dir if (!file_exists($o_config->get('taskqueue_tmp_directory'))) { if (!self::createDirectoryPath($o_config->get('taskqueue_tmp_directory'))) { $this->addError("Couldn't create tmp directory at " . $o_config->get('taskqueue_tmp_directory')); return false; } } else { // if already exists then remove all contents to avoid stale cache caRemoveDirectory($o_config->get('taskqueue_tmp_directory'), false); } // Create media directories $o_media_volumes = new MediaVolumes(); $va_media_volumes = $o_media_volumes->getAllVolumeInformation(); $vs_base_dir = $o_config->get('ca_base_dir'); $va_dir_creation_errors = array(); foreach ($va_media_volumes as $vs_label => $va_volume_info) { if (preg_match('!^' . $vs_base_dir . '!', $va_volume_info['absolutePath'])) { if (!self::createDirectoryPath($va_volume_info['absolutePath'])) { $this->addError("Couldn't create directory for media volume {$vs_label}"); return false; } } } // nuke search index if we using ElasticSearch (the SqlSearch index is nuked when we drop the database) if ($o_config->get('search_engine_plugin') == 'ElasticSearch') { require_once __CA_LIB_DIR__ . '/core/Plugins/SearchEngine/ElasticSearch.php'; $o_es = new WLPlugSearchEngineElasticSearch(); $o_es->truncateIndex(null, true); } return true; }
/** * */ public static function getLocaleList($pa_options = null) { $vs_sort_field = isset($pa_options['sort_field']) ? $pa_options['sort_field'] : ''; $vs_sort_direction = isset($pa_options['sort_direction']) ? $pa_options['sort_direction'] : 'asc'; $vb_index_by_code = isset($pa_options['index_by_code']) && $pa_options['index_by_code'] ? true : false; $vb_return_display_values = isset($pa_options['return_display_values']) && $pa_options['return_display_values'] ? true : false; $vb_available_for_cataloguing_only = isset($pa_options['available_for_cataloguing_only']) && $pa_options['available_for_cataloguing_only'] ? true : false; $va_valid_sorts = array('name', 'language', 'country', 'dialect'); if (!in_array($vs_sort_field, $va_valid_sorts)) { $vs_sort_field = 'name'; } $vs_cache_key = $vs_sort_field . '/' . $vs_sort_direction . '/' . ($vb_index_by_code ? 1 : 0) . '/' . ($vb_return_display_values ? 1 : 0) . '/' . ($vb_available_for_cataloguing_only ? 1 : 0); if (CompositeCache::contains($vs_cache_key, 'LocaleList')) { $va_locales = CompositeCache::fetch($vs_cache_key, 'LocaleList'); // Check if memory cache has been populated with necessary data yet. // This might not be the case if $va_locales comes from disk and the SQL code below was not executed. // Unfortunately the other helpers like loadLocaleByCode() rely on this side-effect of getLocaleList(). if (MemoryCache::itemCountForNamespace('LocaleCodeToId') == 0) { foreach ($va_locales as $va_locale) { if ($vb_available_for_cataloguing_only && $va_locale['dont_use_for_cataloguing']) { continue; } MemoryCache::save($va_locale['language'] . '_' . $va_locale['country'], $va_locale['locale_id'], 'LocaleCodeToId'); MemoryCache::save($va_locale['locale_id'], $va_locale['language'] . '_' . $va_locale['country'], 'LocaleIdToCode'); MemoryCache::save($va_locale['locale_id'], $va_locale['name'], 'LocaleIdToName'); } } return $va_locales; } $o_db = new Db(); $vs_sort = 'ORDER BY ' . $vs_sort_field; $qr_locales = $o_db->query("\n\t\t\tSELECT *\n\t\t\tFROM ca_locales\n\t\t\t{$vs_sort}\n\t\t"); $va_locales = array(); while ($qr_locales->nextRow()) { if ($vb_available_for_cataloguing_only && $qr_locales->get('dont_use_for_cataloguing')) { continue; } $vs_name = $qr_locales->get('name'); if ($vb_return_display_values) { $vm_val = $vs_name; } else { $vm_val = $qr_locales->getRow(); } $vs_code = $qr_locales->get('language') . '_' . $qr_locales->get('country'); $vn_id = $qr_locales->get('locale_id'); if (!$vb_return_display_values) { $vm_val['code'] = $vs_code; } if ($vb_index_by_code) { $va_locales[$vs_code] = $vm_val; } else { $va_locales[$vn_id] = $vm_val; } MemoryCache::save($vs_code, $vn_id, 'LocaleCodeToId'); MemoryCache::save($vn_id, $vs_code, 'LocaleIdToCode'); MemoryCache::save($vn_id, $vs_name, 'LocaleIdToName'); } CompositeCache::save($vs_cache_key, $va_locales, 'LocaleList'); return $va_locales; }
/** * */ public function setGlobalParameter($ps_param, $pm_value) { CompositeCache::save("search_global_{$ps_param}", $pm_value, 'SearchCache'); }
/** * Returns all elements in system as list * * @param $pb_root_elements_only boolean If true, then only root elements are returned; default is false * @param $pm_table_name_or_num mixed Optional table name or number to filter list with. If specified then only elements that have a type restriction to the table are returned. If omitted (default) then all elements, regardless of type restrictions, are returned. * @param $pm_type_name_or_id mixed Optional type code or type_id to restrict elements to. If specified then only elements that have a type restriction to the specified table and type are returned. * @param $pb_use_cache boolean Optional control for list cache; if true [default] then the will be cached for the request; if false the list will be generated from the database. The list is always generated at least once in the current request - there is no inter-request caching * @param $pa_data_types array Optional list of element data types to filter on. * * @return array A List of elements. Each list item is an array with keys set to field names; there is one additional value added with key "display_label" set to the display label of the element in the current locale */ public static function getElementsAsList($pb_root_elements_only = false, $pm_table_name_or_num = null, $pm_type_name_or_id = null, $pb_use_cache = true, $pb_return_stats = false, $pb_index_by_element_code = false, $pa_data_types = null) { $o_dm = Datamodel::load(); $vn_table_num = $o_dm->getTableNum($pm_table_name_or_num); $vs_cache_key = md5($pm_table_name_or_num . '/' . $pm_type_name_or_id . '/' . ($pb_root_elements_only ? '1' : '0') . '/' . ($pb_index_by_element_code ? '1' : '0') . print_R($pa_data_types, true)); if ($pb_use_cache && CompositeCache::contains($vs_cache_key, 'ElementList')) { $va_element_list = CompositeCache::fetch($vs_cache_key, 'ElementList'); if ($pb_return_stats && isset($va_element_list['ui_counts']) || !$pb_return_stats) { return $va_element_list; } } if ($pb_return_stats) { $va_counts_by_attribute = ca_metadata_elements::getUIUsageCounts(); $va_restrictions_by_attribute = ca_metadata_elements::getTypeRestrictionsAsList(); } $vo_db = new Db(); $va_wheres = array(); $va_where_params = array(); if ($pb_root_elements_only) { $va_wheres[] = 'cme.parent_id is NULL'; } if ($vn_table_num) { $va_wheres[] = 'cmtr.table_num = ?'; $va_where_params[] = (int) $vn_table_num; if ($pm_type_name_or_id) { $t_list_item = new ca_list_items(); if (!is_numeric($pm_type_name_or_id)) { $t_list_item->load(array('idno' => $pm_type_name_or_id)); } else { $t_list_item->load((int) $pm_type_name_or_id); } $va_type_ids = array(); if ($vn_type_id = $t_list_item->getPrimaryKey()) { $va_type_ids[$vn_type_id] = true; if ($qr_children = $t_list_item->getHierarchy($vn_type_id, array())) { while ($qr_children->nextRow()) { $va_type_ids[$qr_children->get('item_id')] = true; } } $va_wheres[] = '((cmtr.type_id = ?) OR (cmtr.include_subtypes = 1 AND cmtr.type_id IN (?)))'; $va_where_params[] = (int) $vn_type_id; $va_where_params[] = $va_type_ids; } } $vs_wheres = ' WHERE ' . join(' AND ', $va_wheres); $qr_tmp = $vo_db->query("\n\t\t\t\tSELECT cme.*\n\t\t\t\tFROM ca_metadata_elements cme\n\t\t\t\tINNER JOIN ca_metadata_type_restrictions AS cmtr ON cme.hier_element_id = cmtr.element_id\n\t\t\t\t{$vs_wheres}\n\t\t\t", $va_where_params); } else { if (sizeof($va_wheres)) { $vs_wheres = ' WHERE ' . join(' AND ', $va_wheres); } else { $vs_wheres = ''; } $qr_tmp = $vo_db->query("\n\t\t\t\tSELECT * \n\t\t\t\tFROM ca_metadata_elements cme\n\t\t\t\t{$vs_wheres}\n\t\t\t"); } $va_return = array(); $t_element = new ca_metadata_elements(); while ($qr_tmp->nextRow()) { $vn_element_id = $qr_tmp->get('element_id'); $vs_datatype = $qr_tmp->get('datatype'); if (is_array($pa_data_types) && !in_array($vs_datatype, $pa_data_types)) { continue; } foreach ($t_element->getFields() as $vs_field) { $va_record[$vs_field] = $qr_tmp->get($vs_field); } $va_record['settings'] = caUnserializeForDatabase($qr_tmp->get('settings')); if ($pb_return_stats) { $va_record['ui_counts'] = $va_counts_by_attribute[$vs_code = $qr_tmp->get('element_code')]; $va_record['restrictions'] = $va_restrictions_by_attribute[$vs_code]; } $va_return[$vn_element_id] = $va_record; } // Get labels $va_labels = $t_element->getPreferredDisplayLabelsForIDs(array_keys($va_return)); foreach ($va_labels as $vn_id => $vs_label) { $va_return[$vn_id]['display_label'] = $vs_label; } if ($pb_index_by_element_code) { $va_return_proc = array(); foreach ($va_return as $vn_id => $va_element) { $va_return_proc[$va_element['element_code']] = $va_element; } $va_return = $va_return_proc; } $vm_return = sizeof($va_return) > 0 ? $va_return : false; CompositeCache::save($vs_cache_key, $vm_return, 'ElementList'); return $vm_return; }
/** * Returns a list of ui id, screen id and placement id for a given setting key (editor_code/screen_idno/placement_code) * @param string $ps_key * @return array|bool */ public static function resolveHideIfSelectedKey($ps_key) { if (CompositeCache::contains($ps_key, 'ListAttrHideIfSelected')) { return CompositeCache::fetch($ps_key, 'ListAttrHideIfSelected'); } $va_tmp = explode('/', $ps_key); if (!sizeof($va_tmp) == 3) { return false; } // ui $t_ui = new ca_editor_uis(); if (!$t_ui->load(array('editor_code' => $va_tmp[0]))) { return false; } // screen $t_screen = new ca_editor_ui_screens(); if (!$t_screen->load(array('ui_id' => $t_ui->getPrimaryKey(), 'idno' => $va_tmp[1]))) { return false; } // placement $t_placement = new ca_editor_ui_bundle_placements(); if (!$t_placement->load(array('screen_id' => $t_screen->getPrimaryKey(), 'placement_code' => $va_tmp[2]))) { return false; } $va_ret = array($t_screen->getPrimaryKey(), $t_placement->getPrimaryKey()); CompositeCache::save($ps_key, $va_ret, 'ListAttrHideIfSelected'); return $va_ret; }
/** * Check if setup.php has changed since we last cached the mtime. * If it has, cache the new mtime * @return bool */ function caSetupPhpHasChanged() { $vn_setup_mtime = caGetFileMTime(__CA_BASE_DIR__ . '/setup.php'); if (!CompositeCache::contains('setup_php_mtime') || $vn_setup_mtime != CompositeCache::fetch('setup_php_mtime')) { CompositeCache::save('setup_php_mtime', $vn_setup_mtime, 'default', 0); return true; } return false; }
/** * */ public function getPath($ps_left_table, $ps_right_table) { if (is_numeric($ps_left_table)) { $ps_left_table = $this->getTableName($ps_left_table); } if (is_numeric($ps_right_table)) { $ps_right_table = $this->getTableName($ps_right_table); } if (CompositeCache::contains("{$ps_left_table}/{$ps_right_table}", 'DatamodelPaths')) { return CompositeCache::fetch("{$ps_left_table}/{$ps_right_table}", 'DatamodelPaths'); } # handle self relationships as a special case if ($ps_left_table == $ps_right_table) { //define rel table $rel_table = $ps_left_table . "_x_" . str_replace("ca_", "", $ps_left_table); if (!$this->getInstanceByTableName($rel_table, true)) { return array(); // self relation doesn't exist } return array($ps_left_table => $this->getTableNum($ps_left_table), $rel_table => $this->getTableNum($rel_table)); } $vs_path = $this->opo_graph->getPath($ps_left_table, $ps_right_table); CompositeCache::save("{$ps_left_table}/{$ps_right_table}", $vs_path, 'DatamodelPaths'); return $vs_path; }
/** * @expectedException MemoryCacheInvalidParameterException */ public function testInvalidKey() { CompositeCache::save('', 'data1', 'this is invalid'); }
/** * Try to load a given URI as RDF Graph * @param string $ps_uri * @return bool|EasyRdf_Graph */ static function getURIAsRDFGraph($ps_uri) { if (!$ps_uri) { return false; } if (CompositeCache::contains($ps_uri, 'GettyLinkedDataRDFGraphs')) { return CompositeCache::fetch($ps_uri, 'GettyLinkedDataRDFGraphs'); } try { $o_graph = new EasyRdf_Graph("http://vocab.getty.edu/download/rdf?uri={$ps_uri}.rdf"); $o_graph->load(); } catch (Exception $e) { return false; } CompositeCache::save($ps_uri, $o_graph, 'GettyLinkedDataRDFGraphs'); return $o_graph; }
/** * Returns the most recently viewed items, up to a maximum of $pn_limit (default is 10) * Note that the limit is just that – a limit. getRecentlyViewedItems() may return fewer * than the limit either because there fewer viewed items than your limit or because fetching * additional views would take too long. (To ensure adequate performance getRecentlyViewedItems() uses a cache of * recent views. If there is no cache available it will query the database to look at the most recent (4 x your limit) viewings. * If there are many views of the same item in that group then it is possible that fewer items than your limit will be returned) * * @param int $pn_limit The maximum number of items to return. Default is 10. * @param array $pa_options Supported options are: * restrictToTypes = array of type names or type_ids to restrict to. Only items with a type_id in the list will be returned. * checkAccess = array of access values to filter results by; if defined only items with the specified access code(s) are returned * hasRepresentations = if set to a non-zero (boolean true) value only items with representations will be returned * @return array List of primary key values for recently viewed items. */ public function getRecentlyViewedItems($pn_limit = 10, $pa_options = null) { $va_recently_viewed_items = CompositeCache::fetch('caRecentlyViewed'); $vn_table_num = $this->tableNum(); $vs_table_name = $this->tableName(); $va_ids = array(); if (is_array($va_recently_viewed_items) && is_array($va_recently_viewed_items[$vn_table_num])) { if ($qr_res = caMakeSearchResult($vs_table_name, $va_recently_viewed_items[$vn_table_num])) { $va_type_ids = caMergeTypeRestrictionLists($this, $pa_options); $vs_type_field_name = method_exists($this, 'getTypeFieldName') ? $this->getTypeFieldName() : null; $va_source_ids = caMergeSourceRestrictionLists($this, $pa_options); $vs_source_field_name = method_exists($this, 'getSourceFieldName') ? $this->getSourceFieldName() : null; while ($qr_res->nextHit()) { if ($this->hasField('deleted') && $this->get('deleted') != 0) { continue; } if (isset($pa_options['hasRepresentations']) && $pa_options['hasRepresentations']) { if (!($va_reps = $qr_res->get('ca_object_representations.representation_id', array('returnAsArray' => true))) || !is_array($va_reps) || !sizeof($va_reps)) { continue; } } if (is_array($pa_options['checkAccess']) && sizeof($pa_options['checkAccess']) && $this->hasField('access') && !in_array($qr_res->get("{$vs_table_name}.access"))) { continue; } if ($vs_type_field_name && (is_array($va_type_ids) && sizeof($va_type_ids))) { $vn_type_id = $qr_res->get($vs_type_field_name); if (!in_array($vn_type_id, $va_type_ids) || is_null($vn_type_id) && !$this->getFieldInfo($vs_type_field_name, 'IS_NULL')) { continue; } } if ($vs_source_field_name && (is_array($va_source_ids) && sizeof($va_source_ids))) { $vn_source_id = $qr_res->get($vs_source_field_name); if (!in_array($vn_source_id, $va_source_ids) || is_null($vn_source_id) && !$this->getFieldInfo($vs_source_field_name, 'IS_NULL')) { continue; } } $va_ids[] = $qr_res->getPrimaryKey(); if (sizeof($va_ids) >= $pn_limit) { break; } } } } return $va_ids; return array_keys($va_recently_viewed_items); }
/** * Fetch the type code for a given relationship type id (primary key value) * @param $pn_type_id * @return Array|bool|mixed|null|string */ function caGetRelationshipTypeCode($pn_type_id) { if (CompositeCache::contains($pn_type_id, 'RelationshipIDsToCodes')) { return CompositeCache::fetch($pn_type_id, 'RelationshipIDsToCodes'); } $t_rel_types = new ca_relationship_types($pn_type_id); if (!$t_rel_types->getPrimaryKey()) { return false; } $vs_code = $t_rel_types->get('type_code'); CompositeCache::save($pn_type_id, $vs_code, 'RelationshipIDsToCodes'); return $vs_code; }