View source
<?php
function search_api_db_uninstall() {
variable_del('search_api_db_autocomplete_max_occurrences');
}
function search_api_db_update_dependencies() {
$dependencies['search_api_db'][7101] = array(
'search_api' => 7102,
);
return $dependencies;
}
function search_api_db_update_7101() {
$query = db_select('search_api_server', 's');
$query
->addField('s', 'machine_name');
$query
->condition('class', 'search_api_db_service');
$index_names = db_select('search_api_index', 'i')
->fields('i', array(
'id',
'machine_name',
))
->condition('server', clone $query, 'IN')
->execute()
->fetchAllKeyed();
$query
->addField('s', 'options');
$servers = $query
->execute();
foreach ($servers
->fetchAllKeyed() as $name => $options) {
$options = unserialize($options);
if (empty($options['indexes'])) {
continue;
}
$indexes = array();
foreach ($options['indexes'] as $id => $info) {
if (isset($index_names[$id])) {
$indexes[$index_names[$id]] = $info;
}
}
$options['indexes'] = $indexes;
$options = serialize($options);
db_update('search_api_server')
->fields(array(
'options' => $options,
))
->condition('machine_name', $name)
->execute();
}
}
function search_api_db_update_7102() {
global $databases;
$server_options = db_select('search_api_server', 's')
->fields('s', array(
'options',
))
->condition('class', 'search_api_db_service')
->execute()
->fetchCol();
foreach ($server_options as $options) {
$options = unserialize($options);
list($key, $target) = explode(':', $options['database'], 2);
$db_driver = $databases[$key][$target]['driver'];
if ($db_driver === 'mysql' && !empty($options['indexes'])) {
$prev_db = db_set_active($key);
foreach ($options['indexes'] as $fields) {
foreach ($fields as $field) {
db_query("ALTER TABLE {{$field['table']}} CONVERT TO CHARACTER SET 'utf8' COLLATE 'utf8_bin'", array(), array(
'target' => $target,
));
}
}
db_set_active($prev_db);
}
}
}
function search_api_db_update_7103() {
$server_options = db_select('search_api_server', 's')
->fields('s', array(
'options',
))
->condition('class', 'search_api_db_service')
->execute()
->fetchCol();
$spec = array(
'type' => 'int',
'size' => 'big',
);
foreach ($server_options as $options) {
$options = unserialize($options);
if (!empty($options['indexes'])) {
list($key, $target) = explode(':', $options['database'], 2);
$connection = Database::getConnection($target, $key);
foreach ($options['indexes'] as $fields) {
foreach ($fields as $field) {
if ($field['type'] == 'date') {
$column = !empty($field['column']) ? $field['column'] : 'value';
$connection
->schema()
->changeField($field['table'], $column, $column, $spec);
}
}
}
}
}
}
function search_api_db_update_7104() {
$servers_query = db_select('search_api_server', 's')
->condition('s.class', 'search_api_db_service');
$servers_query
->innerJoin('search_api_index', 'i', 'i.server = s.machine_name');
$servers_query
->fields('s', array(
'options',
));
$servers_query
->fields('i', array(
'server',
'machine_name',
'item_type',
));
$servers = $servers_query
->execute();
$server_options = array();
foreach ($servers as $server) {
if (!isset($server_options[$server->server])) {
$server_options[$server->server] = unserialize($server->options);
}
$options = $server_options[$server->server];
list($key, $target) = explode(':', $options['database'], 2);
if (!empty($options['indexes'][$server->machine_name])) {
$connection = Database::getConnection($target, $key);
$schema = $connection
->schema();
$text_table = NULL;
foreach ($options['indexes'][$server->machine_name] as $name => $field) {
if (search_api_is_text_type($field['type'])) {
$field_name = strlen($name) > 255 ? md5($name) : $name;
if (!isset($text_table)) {
$text_table = _search_api_db_update_find_free_table('search_api_db_' . $server->machine_name . '_text', $connection);
$schema
->renameTable($field['table'], $text_table);
$spec = array(
'description' => "The name of the field in which the token appears, or an MD5 hash of the field.",
'not null' => TRUE,
'type' => 'varchar',
'length' => 255,
'default' => $field_name,
);
$schema
->addField($text_table, 'field_name', $spec);
unset($spec['default']);
$schema
->changeField($text_table, 'field_name', 'field_name', $spec);
$schema
->dropIndex($text_table, 'word');
$schema
->dropPrimaryKey($text_table);
$schema
->addIndex($text_table, 'word_field', array(
array(
'word',
20,
),
'field_name',
));
$schema
->addPrimaryKey($text_table, array(
'item_id',
'field_name',
'word',
));
}
else {
$query = $connection
->select($field['table'], 't')
->fields('t', array(
'item_id',
'word',
'score',
));
$query
->addExpression(':field_name', 'field_name', array(
':field_name' => $field_name,
));
$connection
->insert($text_table)
->from($query)
->execute();
$schema
->dropTable($field['table']);
}
$server_options[$server->server]['indexes'][$server->machine_name][$name]['table'] = $text_table;
}
}
}
}
foreach ($server_options as $server => $options) {
db_update('search_api_server')
->condition('machine_name', $server)
->fields(array(
'options' => serialize($options),
))
->execute();
}
}
function search_api_db_update_7105() {
$servers_query = db_select('search_api_server', 's')
->condition('s.class', 'search_api_db_service');
$servers_query
->innerJoin('search_api_index', 'i', 'i.server = s.machine_name');
$servers_query
->fields('s', array(
'options',
));
$servers_query
->fields('i', array(
'server',
'machine_name',
'item_type',
));
$servers = $servers_query
->execute();
$server_options = array();
foreach ($servers as $server) {
if (!isset($server_options[$server->server])) {
$server_options[$server->server] = unserialize($server->options);
}
$options = $server_options[$server->server];
list($key, $target) = explode(':', $options['database'], 2);
if (!empty($options['indexes'][$server->machine_name])) {
$connection = Database::getConnection($target, $key);
foreach ($options['indexes'][$server->machine_name] as $field) {
if (search_api_is_text_type($field['type'])) {
if ($connection
->schema()
->indexExists($field['table'], 'word')) {
$connection
->schema()
->dropIndex($field['table'], 'word');
$connection
->schema()
->addIndex($field['table'], 'word_field', array(
array(
'word',
20,
),
'field_name',
));
}
break;
}
}
}
}
}
function search_api_db_update_7106() {
$servers_query = db_select('search_api_server', 's')
->condition('s.class', 'search_api_db_service');
$servers_query
->innerJoin('search_api_index', 'i', 'i.server = s.machine_name');
$servers_query
->fields('s', array(
'options',
));
$servers_query
->fields('i', array(
'server',
'machine_name',
'item_type',
));
$servers = $servers_query
->execute();
$server_options = array();
foreach ($servers as $server) {
if (!isset($server_options[$server->server])) {
$server_options[$server->server] = unserialize($server->options);
}
$options = $server_options[$server->server];
list($key, $target) = explode(':', $options['database'], 2);
if (!empty($options['indexes'][$server->machine_name])) {
$connection = Database::getConnection($target, $key);
foreach ($options['indexes'][$server->machine_name] as $field) {
if (search_api_is_text_type($field['type'])) {
$spec = array(
'description' => 'The score associated with this token.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
);
$connection
->schema()
->addField($field['table'], 'score_int', $spec);
$connection
->update($field['table'])
->expression('score_int', 'score * 1000')
->execute();
$connection
->schema()
->dropField($field['table'], 'score');
$connection
->schema()
->changeField($field['table'], 'score_int', 'score', $spec);
break;
}
}
}
}
}
function search_api_db_update_7107() {
$spec = array(
'description' => "The name of the field in which the token appears, or a base-64 encoded sha-256 hash of the field.",
'not null' => TRUE,
'type' => 'varchar',
'length' => 255,
);
$server_options = db_select('search_api_server', 's')
->fields('s', array(
'id',
'options',
))
->condition('class', 'search_api_db_service')
->execute()
->fetchAllKeyed();
foreach ($server_options as $id => $options) {
$options = unserialize($options);
if (!empty($options['indexes'])) {
list($key, $target) = explode(':', $options['database'], 2);
$connection = Database::getConnection($target, $key);
foreach ($options['indexes'] as $index_id => $fields) {
$text_table = NULL;
foreach ($fields as $field_id => $field) {
if (search_api_is_text_type($field['type'])) {
$text_table = $field['table'];
if (strlen($field_id) > 255) {
$connection
->update($text_table)
->fields(array(
'field_name' => drupal_hash_base64($field_id),
))
->condition('field_name', md5($field_id))
->execute();
}
}
}
if ($text_table && db_table_exists($text_table)) {
$connection
->schema()
->changeField($text_table, 'field_name', 'field_name', $spec);
}
}
}
}
}
function _search_api_db_update_find_free_table($table, DatabaseConnection $connection) {
$maxbytes = 62;
if ($db_prefix = $connection
->tablePrefix()) {
$maxbytes -= strlen($db_prefix);
}
$base = $table = _search_api_db_mb_strcut($table, 0, $maxbytes);
$i = 0;
while ($connection
->schema()
->tableExists($table)) {
$suffix = '_' . ++$i;
$table = _search_api_db_mb_strcut($base, 0, $maxbytes - strlen($suffix)) . $suffix;
}
return $table;
}
function _search_api_db_mb_strcut($str, $start, $length = NULL) {
if (function_exists('mb_strcut')) {
return mb_strcut($str, $start, $length);
}
return substr($str, $start, $length);
}