private static function getAllValuesForProperty($property_name, $substring, $base_property_name = null, $base_value = null) { global $sfgMaxAutocompleteValues, $sfgCacheAutocompleteValues, $sfgAutocompleteCacheTimeout; $values = array(); $db = wfGetDB(DB_SLAVE); $sql_options = array(); $sql_options['LIMIT'] = $sfgMaxAutocompleteValues; $property = SMWPropertyValue::makeUserProperty($property_name); $is_relation = $property->getPropertyTypeID() == '_wpg'; $property_name = str_replace(' ', '_', $property_name); $conditions = array('p_ids.smw_title' => $property_name); // Use cache if allowed if ($sfgCacheAutocompleteValues) { $cache = SFFormUtils::getFormCache(); // Remove trailing whitespace to avoid unnecessary database selects $cacheKeyString = $property_name . '::' . rtrim($substring); if (!is_null($base_property_name)) { $cacheKeyString .= ',' . $base_property_name . ',' . $base_value; } $cacheKey = wfMemcKey('sf-autocomplete', md5($cacheKeyString)); $values = $cache->get($cacheKey); if (!empty($values)) { // Return with results immediately return $values; } } if ($is_relation) { $value_field = 'o_ids.smw_title'; $from_clause = $db->tableName('smw_rels2') . " r JOIN " . $db->tableName('smw_ids') . " p_ids ON r.p_id = p_ids.smw_id JOIN " . $db->tableName('smw_ids') . " o_ids ON r.o_id = o_ids.smw_id"; } else { $value_field = 'a.value_xsd'; $from_clause = $db->tableName('smw_atts2') . " a JOIN " . $db->tableName('smw_ids') . " p_ids ON a.p_id = p_ids.smw_id"; } if (!is_null($base_property_name)) { $base_property = SMWPropertyValue::makeUserProperty($base_property_name); $base_is_relation = $base_property->getPropertyTypeID() == '_wpg'; $base_property_name = str_replace(' ', '_', $base_property_name); $conditions['base_p_ids.smw_title'] = $base_property_name; $main_prop_alias = $is_relation ? 'r' : 'a'; if ($base_is_relation) { $from_clause .= " JOIN " . $db->tableName('smw_rels2') . " r_base ON {$main_prop_alias}.s_id = r_base.s_id"; $from_clause .= " JOIN " . $db->tableName('smw_ids') . " base_p_ids ON r_base.p_id = base_p_ids.smw_id JOIN " . $db->tableName('smw_ids') . " base_o_ids ON r_base.o_id = base_o_ids.smw_id"; $base_value = str_replace(' ', '_', $base_value); $conditions['base_o_ids.smw_title'] = $base_value; } else { $from_clause .= " JOIN " . $db->tableName('smw_atts2') . " a_base ON {$main_prop_alias}.s_id = a_base.s_id"; $from_clause .= " JOIN " . $db->tableName('smw_ids') . " base_p_ids ON a_base.p_id = base_p_ids.smw_id"; $conditions['a_base.value_xsd'] = $base_value; } } if (!is_null($substring)) { $conditions[] = SFUtils::getSQLConditionForAutocompleteInColumn($value_field, $substring); } $sql_options['ORDER BY'] = $value_field; $res = $db->select($from_clause, "DISTINCT {$value_field}", $conditions, __METHOD__, $sql_options); while ($row = $db->fetchRow($res)) { $values[] = str_replace('_', ' ', $row[0]); } $db->freeResult($res); if ($sfgCacheAutocompleteValues) { // Save to cache. $cache->set($cacheKey, $values, $sfgAutocompleteCacheTimeout); } return $values; }
private static function getAllValuesForProperty($property_name, $substring, $basePropertyName = null, $baseValue = null) { global $sfgMaxAutocompleteValues, $sfgCacheAutocompleteValues, $sfgAutocompleteCacheTimeout; global $smwgDefaultStore; $values = array(); $db = wfGetDB(DB_SLAVE); $sqlOptions = array(); $sqlOptions['LIMIT'] = $sfgMaxAutocompleteValues; $property = SMWPropertyValue::makeUserProperty($property_name); $propertyHasTypePage = $property->getPropertyTypeID() == '_wpg'; $property_name = str_replace(' ', '_', $property_name); $conditions = array('p_ids.smw_title' => $property_name); // Use cache if allowed if ($sfgCacheAutocompleteValues) { $cache = SFFormUtils::getFormCache(); // Remove trailing whitespace to avoid unnecessary database selects $cacheKeyString = $property_name . '::' . rtrim($substring); if (!is_null($basePropertyName)) { $cacheKeyString .= ',' . $basePropertyName . ',' . $baseValue; } $cacheKey = wfMemcKey('sf-autocomplete', md5($cacheKeyString)); $values = $cache->get($cacheKey); if (!empty($values)) { // Return with results immediately return $values; } } if ($propertyHasTypePage) { $valueField = 'o_ids.smw_title'; if ($smwgDefaultStore === 'SMWSQLStore3') { $idsTable = $db->tableName('smw_object_ids'); $propsTable = $db->tableName('smw_di_wikipage'); } else { $idsTable = $db->tableName('smw_ids'); $propsTable = $db->tableName('smw_rels2'); } $fromClause = "{$propsTable} p JOIN {$idsTable} p_ids ON p.p_id = p_ids.smw_id JOIN {$idsTable} o_ids ON p.o_id = o_ids.smw_id"; } else { if ($smwgDefaultStore === 'SMWSQLStore3') { $valueField = 'p.o_hash'; $idsTable = $db->tableName('smw_object_ids'); $propsTable = $db->tableName('smw_di_blob'); } else { $valueField = 'p.value_xsd'; $idsTable = $db->tableName('smw_ids'); $propsTable = $db->tableName('smw_atts2'); } $fromClause = "{$propsTable} p JOIN {$idsTable} p_ids ON p.p_id = p_ids.smw_id"; } if (!is_null($basePropertyName)) { $baseProperty = SMWPropertyValue::makeUserProperty($basePropertyName); $basePropertyHasTypePage = $baseProperty->getPropertyTypeID() == '_wpg'; $basePropertyName = str_replace(' ', '_', $basePropertyName); $conditions['base_p_ids.smw_title'] = $basePropertyName; if ($basePropertyHasTypePage) { if ($smwgDefaultStore === 'SMWSQLStore3') { $idsTable = $db->tableName('smw_object_ids'); $propsTable = $db->tableName('smw_di_wikipage'); } else { $idsTable = $db->tableName('smw_ids'); $propsTable = $db->tableName('smw_rels2'); } $fromClause .= " JOIN {$propsTable} p_base ON p.s_id = p_base.s_id"; $fromClause .= " JOIN {$idsTable} base_p_ids ON p_base.p_id = base_p_ids.smw_id JOIN {$idsTable} base_o_ids ON p_base.o_id = base_o_ids.smw_id"; $baseValue = str_replace(' ', '_', $baseValue); $conditions['base_o_ids.smw_title'] = $baseValue; } else { if ($smwgDefaultStore === 'SMWSQLStore3') { $baseValueField = 'p_base.o_hash'; $idsTable = $db->tableName('smw_object_ids'); $propsTable = $db->tableName('smw_di_blob'); } else { $baseValueField = 'p_base.value_xsd'; $idsTable = $db->tableName('smw_ids'); $propsTable = $db->tableName('smw_atts2'); } $fromClause .= " JOIN {$propsTable} p_base ON p.s_id = p_base.s_id"; $fromClause .= " JOIN {$idsTable} base_p_ids ON p_base.p_id = base_p_ids.smw_id"; $conditions[$baseValueField] = $baseValue; } } if (!is_null($substring)) { // "Page" type property valeus are stored differently // in the DB, i.e. underlines instead of spaces. $conditions[] = SFUtils::getSQLConditionForAutocompleteInColumn($valueField, $substring, $propertyHasTypePage); } $sqlOptions['ORDER BY'] = $valueField; $res = $db->select($fromClause, "DISTINCT {$valueField}", $conditions, __METHOD__, $sqlOptions); while ($row = $db->fetchRow($res)) { $values[] = str_replace('_', ' ', $row[0]); } $db->freeResult($res); if ($sfgCacheAutocompleteValues) { // Save to cache. $cache->set($cacheKey, $values, $sfgAutocompleteCacheTimeout); } return $values; }
private static function getAllValuesForCargoField($cargoTable, $cargoField, $fieldIsArray, $substring, $baseCargoTable = null, $baseCargoField = null, $baseValue = null) { global $sfgMaxAutocompleteValues, $sfgCacheAutocompleteValues, $sfgAutocompleteCacheTimeout; global $sfgAutocompleteOnAllChars; $values = array(); $tablesStr = $cargoTable; $fieldsStr = $cargoField; $joinOnStr = ''; $whereStr = ''; // Use cache if allowed if ($sfgCacheAutocompleteValues) { $cache = SFFormUtils::getFormCache(); // Remove trailing whitespace to avoid unnecessary database selects $cacheKeyString = $cargoTable . '|' . $cargoField . '|' . rtrim($substring); if (!is_null($baseCargoTable)) { $cacheKeyString .= '|' . $baseCargoTable . '|' . $baseCargoField . '|' . $baseValue; } $cacheKey = wfMemcKey('sf-autocomplete', md5($cacheKeyString)); $values = $cache->get($cacheKey); if (!empty($values)) { // Return with results immediately return $values; } } if (!is_null($baseCargoTable) && !is_null($baseCargoField)) { if ($baseCargoTable != $cargoTable) { $tablesStr .= ", {$baseCargoTable}"; $joinOnStr = "{$cargoTable}._pageName = {$baseCargoTable}._pageName"; } $whereStr = "{$baseCargoTable}.{$baseCargoField} = \"{$baseValue}\""; } if (!is_null($substring)) { if ($whereStr != '') { $whereStr .= " AND "; } $operator = $fieldIsArray ? "HOLDS LIKE" : "LIKE"; if ($sfgAutocompleteOnAllChars) { $whereStr .= "({$cargoField} {$operator} \"%{$substring}%\")"; } else { $whereStr .= "({$cargoField} {$operator} \"{$substring}%\" OR {$cargoField} {$operator} \"% {$substring}%\")"; } } $sqlQuery = CargoSQLQuery::newFromValues($tablesStr, $fieldsStr, $whereStr, $joinOnStr, $cargoField, $havingStr = null, $cargoField, $sfgMaxAutocompleteValues); $cargoFieldAlias = str_replace('_', ' ', $cargoField); $queryResults = $sqlQuery->run(); foreach ($queryResults as $row) { // @TODO - this check should not be neceaary. if (($value = $row[$cargoFieldAlias]) != '') { $values[] = $value; } } if ($sfgCacheAutocompleteValues) { // Save to cache. $cache->set($cacheKey, $values, $sfgAutocompleteCacheTimeout); } return $values; }