/** * 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 testFlush() { $vm_ret = CompositeCache::save('foo', array('foo' => 'bar'), 'barNamespace'); $this->assertTrue($vm_ret, 'Setting item in cache should return true'); CompositeCache::flush(); $vm_ret = CompositeCache::contains('foo', 'barNamespace'); $this->assertFalse($vm_ret, 'Should not return anything after deleting'); }
/** * @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); }
/** * 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; }
/** * 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; }
/** * 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 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; }
/** * */ 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 getGlobalParameter($ps_param) { if (CompositeCache::contains("search_global_{$ps_param}", 'SearchCache')) { return CompositeCache::fetch("search_global_{$ps_param}", 'SearchCache'); } return false; }
/** * 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; }
/** * 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; }