/** * Test load method correctly */ public function testLoadObject() { $object = new TestClass('value'); $object->publicParam = 'public'; $this->memoryCache->save(self::KEY, $object); $this->assertEquals($object, $this->memoryCache->load(self::KEY)); $object->setPrivateParam('new value'); $this->assertNotEquals($object, $this->memoryCache->load(self::KEY)); }
/** * Try to match given (partial) hierarchy path to a single subject in getty linked data AAT service * @param array $pa_hierarchy_path * @param int $pn_threshold * @param array $pa_options * removeParensFromLabels = Remove parens from labels for search and string comparison. This can improve results in specific cases. * @return bool|string */ function caMatchAAT($pa_hierarchy_path, $pn_threshold = 180, $pa_options = array()) { $vs_cache_key = md5(print_r($pa_hierarchy_path, true)); if (MemoryCache::contains($vs_cache_key, 'AATMatches')) { return MemoryCache::fetch($vs_cache_key, 'AATMatches'); } if (!is_array($pa_hierarchy_path)) { return false; } $pb_remove_parens_from_labels = caGetOption('removeParensFromLabels', $pa_options, false); // search the bottom-most component (the actual term) $vs_bot = trim(array_pop($pa_hierarchy_path)); if ($pb_remove_parens_from_labels) { $vs_lookup = trim(preg_replace("/\\([\\p{L}\\-\\_\\s]+\\)/", '', $vs_bot)); } else { $vs_lookup = $vs_bot; } $o_service = new WLPlugInformationServiceAAT(); $va_hits = $o_service->lookup(array(), $vs_lookup, array('phrase' => true, 'raw' => true, 'limit' => 2000)); if (!is_array($va_hits)) { return false; } $vn_best_distance = 0; $vn_pick = -1; foreach ($va_hits as $vn_i => $va_hit) { if (stripos($va_hit['TermPrefLabel']['value'], $vs_lookup) !== false) { // only consider terms that match what we searched // calculate similarity as a number by comparing both the term and the parent string $vs_label_with_parens = $va_hit['TermPrefLabel']['value']; $vs_label_without_parens = trim(preg_replace("/\\([\\p{L}\\s]+\\)/", '', $vs_label_with_parens)); $va_label_percentages = array(); // we try every combination with and without parens on both sides // unfortunately this code gets rather ugly because getting the similarity // as percentage is only possible by passing a reference parameter :-( similar_text($vs_label_with_parens, $vs_bot, $vn_label_percent); $va_label_percentages[] = $vn_label_percent; similar_text($vs_label_with_parens, $vs_lookup, $vn_label_percent); $va_label_percentages[] = $vn_label_percent; similar_text($vs_label_without_parens, $vs_bot, $vn_label_percent); $va_label_percentages[] = $vn_label_percent; similar_text($vs_label_without_parens, $vs_lookup, $vn_label_percent); $va_label_percentages[] = $vn_label_percent; // similarity to parent path similar_text($va_hit['ParentsFull']['value'], join(' ', array_reverse($pa_hierarchy_path)), $vn_parent_percent); // it's a weighted sum because the term label is more important than the exact path $vn_tmp = 2 * max($va_label_percentages) + $vn_parent_percent; //var_dump($va_hit); var_dump($vn_tmp); if ($vn_tmp > $vn_best_distance) { $vn_best_distance = $vn_tmp; $vn_pick = $vn_i; } } } if ($vn_pick >= 0 && $vn_best_distance > $pn_threshold) { $va_pick = $va_hits[$vn_pick]; MemoryCache::save($vs_cache_key, $va_pick['ID']['value'], 'AATMatches'); return $va_pick['ID']['value']; } return false; }
/** * The same as _t(), but rather than returning the translated string, it prints it **/ function _p($ps_key) { if (!$ps_key) { return; } global $_; if (!sizeof(func_get_args()) && MemoryCache::contains($ps_key, 'translation')) { print MemoryCache::fetch($ps_key, 'translation'); return; } if (is_array($_)) { $vs_str = $ps_key; foreach ($_ as $o_locale) { if ($o_locale->isTranslated($ps_key)) { $vs_str = $o_locale->_($ps_key); break; } } } else { if (!is_object($_)) { $vs_str = $ps_key; } else { $vs_str = $_->_($ps_key); } } if (sizeof($va_args = func_get_args()) > 1) { $vn_num_args = sizeof($va_args) - 1; for ($vn_i = $vn_num_args; $vn_i >= 1; $vn_i--) { $vs_str = str_replace("%{$vn_i}", $va_args[$vn_i], $vs_str); } } MemoryCache::save($ps_key, $vs_str, 'translation'); print $vs_str; return; }
/** * @param string $ps_file_path * @param bool $pb_dont_cache * @param bool $pb_dont_cache_instance * @return Configuration */ static function load($ps_file_path = __CA_APP_CONFIG__, $pb_dont_cache = false, $pb_dont_cache_instance = false) { if (!$ps_file_path) { $ps_file_path = __CA_APP_CONFIG__; } if (!MemoryCache::contains($ps_file_path, 'ConfigurationInstances') || $pb_dont_cache || $pb_dont_cache_instance) { MemoryCache::save($ps_file_path, new Configuration($ps_file_path, true, $pb_dont_cache), 'ConfigurationInstances'); } return MemoryCache::fetch($ps_file_path, 'ConfigurationInstances'); }
/** * @expectedException MemoryCacheInvalidParameterException */ public function testInvalidKey() { MemoryCache::save('', 'data1', 'barNamespace'); }
/** * Puts data into the cache. Overwrites existing items! * @param string $ps_key * @param mixed $pm_data * @param string $ps_namespace * @param int $pn_ttl * @return bool success state */ public static function save($ps_key, $pm_data, $ps_namespace = 'default', $pn_ttl = null) { MemoryCache::save($ps_key, $pm_data, $ps_namespace); ExternalCache::save($ps_key, $pm_data, $ps_namespace, $pn_ttl); return true; }
/** * Returns element_id of ca_metadata_element with specified element_code or NULL if the element doesn't exist * Because of dependency and performance issues we do a straight query here rather than go through the ca_metadata_elements model */ private function _getElementID($ps_element_code) { if (MemoryCache::contains($ps_element_code, 'SearchIndexerElementIds')) { return MemoryCache::fetch($ps_element_code, 'SearchIndexerElementIds'); } if (is_numeric($ps_element_code)) { $qr_res = $this->opo_db->query("\n\t\t\t\tSELECT element_id, datatype, list_id FROM ca_metadata_elements WHERE element_id = ?\n\t\t\t", intval($ps_element_code)); } else { $qr_res = $this->opo_db->query("\n\t\t\t\tSELECT element_id, datatype, list_id FROM ca_metadata_elements WHERE element_code = ?\n\t\t\t", $ps_element_code); } if (!$qr_res->nextRow()) { return null; } $vn_element_id = $qr_res->get('element_id'); MemoryCache::save($ps_element_code, $qr_res->get('datatype'), 'SearchIndexerElementDataTypes'); MemoryCache::save($vn_element_id, $qr_res->get('datatype'), 'SearchIndexerElementDataTypes'); MemoryCache::save($ps_element_code, $qr_res->get('list_id'), 'SearchIndexerElementListIds'); MemoryCache::save($vn_element_id, $qr_res->get('list_id'), 'SearchIndexerElementListIds'); MemoryCache::save($vn_element_id, $ps_element_code, 'SearchIndexerElementIds'); MemoryCache::save($ps_element_code, $vn_element_id, 'SearchIndexerElementIds'); return $vn_element_id; }
/** * Fetch item_id for item with specified value. Value must match exactly. * * @param string $ps_list_code List code * @param string $ps_value The item value to search for * @param array $pa_options Options include: * transaction = transaction to execute queries within. [Default=null] * @return int item_id of list item or null if no matching item was found */ function caGetListItemIDForValue($ps_list_code, $ps_value, $pa_options = null) { $vs_cache_key = md5($ps_list_code . $ps_value . serialize($pa_options)); if (MemoryCache::contains($vs_cache_key, 'ListItemIDsForValues')) { return MemoryCache::fetch($vs_cache_key, 'ListItemIDsForValues'); } $t_list = new ca_lists(); if ($o_trans = caGetOption('transaction', $pa_options, null)) { $t_list->setTransaction($o_trans); } if ($va_item = $t_list->getItemFromListByItemValue($ps_list_code, $ps_value)) { $vs_ret = array_shift(array_keys($va_item)); MemoryCache::save($vs_cache_key, $vs_ret, 'ListItemIDsForValues'); return $vs_ret; } return null; }
/** * */ public function getRelationships($ps_left_table, $ps_right_table) { if (MemoryCache::contains("{$ps_left_table}/{$ps_right_table}", 'DatamodelRelationships')) { return MemoryCache::fetch("{$ps_left_table}/{$ps_right_table}", 'DatamodelRelationships'); } $va_relationships = $this->opo_graph->getAttribute("relationships", $ps_left_table, $ps_right_table); MemoryCache::save("{$ps_left_table}/{$ps_right_table}", $va_relationships, 'DatamodelRelationships'); return $va_relationships; }
/** * Establishes a connection to the database * * @param mixed $po_caller representation of the caller, usually a Db() object * @param array $pa_options array containing options like host, username, password * @return bool success state */ public function connect($po_caller, $pa_options) { $vs_db_connection_key = $pa_options["host"] . '/' . $pa_options["database"]; // reuse connection if (!($vb_unique_connection = caGetOption('uniqueConnection', $pa_options, false)) && MemoryCache::contains($vs_db_connection_key, 'PdoConnectionCache')) { $this->opr_db = MemoryCache::fetch($vs_db_connection_key, 'PdoConnectionCache'); return true; } if (!class_exists("PDO")) { die(_t("Your PHP installation lacks PDO MySQL support. Please add it and retry...")); } try { $this->opr_db = new PDO('mysql:host=' . $pa_options["host"] . ';dbname=' . $pa_options["database"], $pa_options["username"], $pa_options["password"], array(PDO::ATTR_PERSISTENT => caGetOption("persistentConnections", $pa_options, true), PDO::ATTR_EMULATE_PREPARES => true, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); } catch (Exception $e) { $po_caller->postError(200, $e->getMessage(), "Db->pdo_mysql->connect()"); return false; } $this->opr_db->exec('SET NAMES \'utf8\''); $this->opr_db->exec('SET character_set_results = NULL'); if (!$vb_unique_connection) { MemoryCache::save($vs_db_connection_key, $this->opr_db, 'PdoConnectionCache'); } return true; }
/** * Extracts media metadata using MediaInfo * * @param string $ps_filepath file path * @param string $ps_mediainfo_path optional path to MediaInfo binary. If omitted the path configured in external_applications.conf is used. * * @return array Extracted metadata */ function caExtractMetadataWithMediaInfo($ps_filepath, $ps_mediainfo_path = null) { if (!$ps_mediainfo_path) { $ps_mediainfo_path = caGetExternalApplicationPath('mediainfo'); } if (!caIsValidFilePath($ps_mediainfo_path)) { return false; } if (MemoryCache::contains($ps_filepath, 'MediaInfoMetadata')) { return MemoryCache::fetch($ps_filepath, 'MediaInfoMetadata'); } // // TODO: why don't we parse this from the XML output like civilized people? // exec($ps_mediainfo_path . " " . caEscapeShellArg($ps_filepath), $va_output, $vn_return); $vs_cat = "GENERIC"; $va_return = array(); foreach ($va_output as $vs_line) { $va_split = explode(":", $vs_line); $vs_left = trim(array_shift($va_split)); $vs_right = trim(join(":", $va_split)); if (strlen($vs_right) == 0) { // category line $vs_cat = strtoupper($vs_left); continue; } if (strlen($vs_left) && strlen($vs_right)) { if ($vs_left != "Complete name") { // we probably don't want to display temporary filenames $va_return[$vs_cat][$vs_left] = $vs_right; } } } MemoryCache::save($ps_filepath, $va_return, 'MediaInfoMetadata'); return $va_return; }
/** * @param string $key * @param mixed $value * @return void * @throws FileOpenFailedException * @throws FileWriteFailedException */ public function save($key, $value) { $this->memoryCache->save($key, $value); $this->fileCache->save($key, $value); }
/** * Returns number of locales configured * * @param array $pa_options Array of options. Supported options include: * forCataloguing - if set then only locales that are marked as available for cataloguing are counted * @return int Number of locales */ public function numberOfLocales($pa_options = null) { $vs_for_cataloguing_sql = ''; $vs_cache_key = 'all'; if (isset($pa_options['forCataloguing']) && (bool) $pa_options['forCataloguing']) { $vs_for_cataloguing_sql = " WHERE dont_use_for_cataloguing = 0"; $vs_cache_key = 'forCataloguing'; } if (MemoryCache::contains($vs_cache_key, 'LocaleCount')) { return MemoryCache::fetch($vs_cache_key, 'LocaleCount'); } $qr_res = $this->getDb()->query("SELECT count(*) c FROM ca_locales {$vs_for_cataloguing_sql}"); $vn_num_locales = 0; if ($qr_res->nextRow()) { $vn_num_locales = $qr_res->get('c'); } MemoryCache::save($vs_cache_key, $vn_num_locales, 'LocaleCount'); return $vn_num_locales; }
/** * */ public static function getInstance($pm_element_code_or_id) { if (!$pm_element_code_or_id) { return null; } if (MemoryCache::contains($pm_element_code_or_id, 'ElementInstances')) { return MemoryCache::fetch($pm_element_code_or_id, 'ElementInstances'); } $t_element = new ca_metadata_elements(is_numeric($pm_element_code_or_id) ? $pm_element_code_or_id : null); if (!($vn_element_id = $t_element->getPrimaryKey())) { if ($t_element->load(array('element_code' => $pm_element_code_or_id))) { MemoryCache::save($t_element->getPrimaryKey(), $t_element, 'ElementInstances'); MemoryCache::save($t_element->get('element_code'), $t_element, 'ElementInstances'); return $t_element; } } else { MemoryCache::save($vn_element_id, $t_element, 'ElementInstances'); MemoryCache::save($t_element->get('element_code'), $t_element, 'ElementInstances'); return $t_element; } return null; }
protected function search($pa_bundles = null) { $va_return = parent::search($pa_bundles); if ($this->getTableName() == 'ca_entities' && is_array($va_return['results']) && sizeof($va_return['results']) > 0) { $o_db = new Db(); $t_element = new ca_metadata_elements(); $t_element->load(array('element_code' => 'is_index_display')); $vn_element_id = $t_element->getPrimaryKey(); if (!$vn_element_id) { return $va_return; } $vn_yes_id = caGetListItemID('yn', 'yes'); foreach ($va_return['results'] as &$va_result) { $vn_entity_id = $va_result['entity_id']; $qr_objects = $o_db->query("\n\t\t\t\t\tSELECT DISTINCT ca_objects.object_id FROM ca_entities, ca_objects_x_entities, ca_objects, ca_attributes, ca_attribute_values\n\t\t\t\t\tWHERE ca_entities.entity_id = ca_objects_x_entities.entity_id\n\t\t\t\t\tAND ca_attributes.row_id = ca_objects.object_id\n\t\t\t\t\tAND ca_attribute_values.attribute_id = ca_attributes.attribute_id\n\t\t\t\t\tAND ca_objects_x_entities.object_id = ca_objects.object_id\n\t\t\t\t\tAND ca_attribute_values.element_id = ?\n\t\t\t\t\tAND ca_entities.entity_id = ?\n\t\t\t\t\tAND ca_attribute_values.item_id = ?\n\t\t\t\t\tAND ca_objects.deleted = 0\n\t\t\t\t", $vn_element_id, $vn_entity_id, $vn_yes_id); while ($qr_objects->nextRow()) { $va_object_info = array(); $vn_object_id = $qr_objects->get('ca_objects.object_id'); if (MemoryCache::contains($vn_object_id, 'AboutDrawingServices')) { $va_object_info = MemoryCache::fetch($vn_object_id, 'AboutDrawingServices'); } else { $va_object_info['object_id'] = $vn_object_id; $qr_labels = $o_db->query("\n\t\t\t\t\t\t\tSELECT ca_object_labels.* FROM ca_object_labels, ca_objects\n\t\t\t\t\t\t\tWHERE ca_object_labels.object_id = ca_objects.object_id\n\t\t\t\t\t\t\tAND ca_objects.object_id = ?\n\t\t\t\t\t\t", $vn_object_id); $vs_label = ''; while ($qr_labels->nextRow()) { $va_object_info['labels'][] = $qr_labels->getRow(); $vs_label = $qr_labels->get('name'); // keep the last label around } $va_object_info['label'] = $vs_label; $qr_reps = $o_db->query("\n\t\t\t\t\t\t\tSELECT ca_object_representations.* FROM ca_object_representations, ca_objects_x_object_representations, ca_objects\n\t\t\t\t\t\t\tWHERE ca_object_representations.representation_id = ca_objects_x_object_representations.representation_id\n\t\t\t\t\t\t\tAND ca_objects_x_object_representations.object_id = ca_objects.object_id\n\t\t\t\t\t\t\tAND ca_objects.object_id = ?\n\t\t\t\t\t\t\tAND ca_objects.deleted = 0 AND ca_object_representations.deleted = 0\n\t\t\t\t\t\t", $vn_object_id); while ($qr_reps->nextRow()) { $va_object_info['representations'][] = array('representation_id' => $qr_reps->get('representation_id'), 'icon' => $qr_reps->getMediaUrl('media', 'icon'), 'preview170' => $qr_reps->getMediaUrl('media', 'preview170'), 'medium' => $qr_reps->getMediaUrl('media', 'medium'), 'original' => $qr_reps->getMediaUrl('media', 'original')); } MemoryCache::save($vn_object_id, $va_object_info, 'AboutDrawingServices'); } $va_result['ca_objects'][] = $va_object_info; } /* the old&simple, but very slow code $t_entity = new ca_entities($va_result['entity_id']); $va_objects = $t_entity->getRelatedItems('ca_objects'); foreach($va_objects as $va_object) { $t_object = new ca_objects($va_object['object_id']); $va_reps = $t_object->getRepresentations(array('icon', 'preview170', 'medium', 'original')); $va_representation_info = array(); foreach($va_reps as $va_rep) { $va_representation_info[] = array( 'representation_id' => $va_rep['representation_id'], 'urls' => $va_rep['urls'], ); } $va_object_info = array(); $va_object_info['object_id'] = $va_object['object_id']; $va_object_info['labels'] = $va_object['labels']; $va_object_info['label'] = $va_object['label']; $va_object_info['representations'] = $va_representation_info; $va_result['ca_objects'][] = $va_object_info; }*/ } } return $va_return; }
/** * Get list of rules for currently loaded row * @return array|null */ public function getRules() { if (!$this->getPrimaryKey()) { return null; } if (MemoryCache::contains($this->getPrimaryKey(), 'MDDictRuleList')) { return MemoryCache::fetch($this->getPrimaryKey(), 'MDDictRuleList'); } $o_db = $this->getDb(); $qr_rules = $o_db->query("\n\t\t\tSELECT * FROM ca_metadata_dictionary_rules ORDER BY rule_id\n\t\t"); $va_return = array(); while ($qr_rules->nextRow()) { $va_return[$qr_rules->get('rule_id')] = $qr_rules->getRow(); $va_return[$qr_rules->get('rule_id')]['settings'] = caUnserializeForDatabase($qr_rules->get('settings')); } MemoryCache::save($this->getPrimaryKey(), $va_return, 'MDDictRuleList'); return $va_return; }