<commit_before><?php
/*!
 * qwp: https://github.com/steem/qwp
 *
 * Copyright (c) 2015 Steem
 * Released under the MIT license
 */
require_once(DRUPAL_DB_ROOT . '/database.inc');

global $QWP_ACTIVE_DB;
if (isset($QWP_ACTIVE_DB)) {
    db_set_active($QWP_ACTIVE_DB);
}
function qwp_db_try_connect_db() {
    try {
        db_query('select version()')->execute();
    } catch (PDOException $e) {
        log_db_exception($e, "try_connect_db");
        db_remove_active();
    }
}
function qwp_db_or_from_array(&$ids, $field = 'id') {
    $or = '(';
    $sep = '';
    foreach ($ids as &$id) {
        $or .= $sep . "$field='$id'";
        if (!$sep) $sep = ' or ';
    }
    return $or . ')';
}
function qwp_db_error_is_duplicated(&$pdo_exception) {
    return $pdo_exception->errorInfo[1] == 1062;
}
function qwp_db_error_is_connection_gone(&$pdo_exception) {
    return $pdo_exception->errorInfo[1] == 2006;
}
function qwp_db_has_record($table_name, $where = null, $conditions = null) {
    $query = db_select($table_name, 't');
    if ($where) {
        $query->where($where);
    }
    if ($conditions) {
        foreach ($conditions as $con) {
            $query->condition($con[0], $con[1], isset($con[2]) ? $con[2] : null);
        }
    }
    $query->addExpression("count(1)", "n");
    $result = $query->execute();
    if ($result && $result->rowCount() > 0) {
        $r = $result->fetchAssoc();
        return intval($r["n"]) > 0;
    }
    return false;
}
function qwp_db_records_count($table_name, $where = null, $conditions = null) {
    $query = db_select($table_name, 't');
    if ($where) {
        $query->where($where);
    }
    if ($conditions) {
        foreach ($conditions as $con) {
            $query->condition($con[0], $con[1], isset($con[2]) ? $con[2] : null);
        }
    }
    $query->addExpression("count(1)", "n");
    $result = $query->execute();
    if ($result && $result->rowCount() > 0) {
        $r = $result->fetchAssoc();
        return intval($r["n"]);
    }
    return 0;
}
function qwp_db_get_one_record($table_name, $fields, $conditions, $where = null) {
    $query = db_select($table_name, 't')->fields('t', $fields);
    if ($conditions) {
        foreach ($conditions as $con) {
            $query->condition($con[0], $con[1], isset($con[2]) ? $con[2] : null);
        }
    }
    if ($where) {
        $query->where($where);
    }
    $result = $query->execute();
    return $result->rowCount() === 0 ? false : $result->fetchAssoc();
}
function qwp_db_get_fields_from_modal(&$modal, &$fields) {
    $fields = array();
    foreach ($modal as $idx => &$item) {
        if ($idx === 'alias' || !isset($item['table'])) {
            continue;
        }
        $table = $item['table'];
        if (!isset($fields[$table])) {
            $fields[$table] = array();
        }
        foreach ($item as $k => $v) {
            if ($k === 'table' || $k === 'group') {
                continue;
            } else if (is_string($v)) {
                if (strpos($v, ',') !== false) {
                    $fields[$table] = array_merge($fields[$table], explode(',', $v));
                } else {
                    $fields[$table][] = $v;
                }
            } else if ($v[0]) {
                $fields[$table][] = $v[0];
            }
        }
    }
}
function qwp_db_get_table_header_from_modal(&$modal, &$header) {
    $header = array(
        'names' => array(),
    );
    $groups = array();
    $has_alias = isset($modal['alias']);
    if ($has_alias) {
        $header['fields'] = array();
    }
    foreach ($modal as $idx => &$item) {
        if ($idx === 'alias') {
            continue;
        }
        $table = isset($item['table']) ? $item['table'] : '';
        $is_complex_arr = false;
        $group_fields = array();
        $group_name = false;
        foreach ($item as $k => $v) {
            if ($k === 'table') {
                continue;
            }
            if ($k === 'group') {
                $group_name = $v[0];
                $groups[$group_name] = array($v[1]);
                continue;
            }
            $is_string = is_string($v);
            if ($is_string && ($table || $is_complex_arr)) continue;
            if ($is_string) $v = $item;
            if (count($v) == 1) continue;
            if ($has_alias) {
                $ak = $table . '.' . $v[0];
                $header['fields'][] = $ak;
                if ($has_alias) $group_fields[] = $ak;
                if (isset($modal['alias'][$ak])) {
                    $v[0] = $modal['alias'][$ak];
                }
            } else {
                $group_fields[] = $v[0];
            }
            $header['names'][] = $v;
            if ($is_string) break;
            if (!$is_complex_arr) $is_complex_arr = !$is_string;
        }
        if ($group_name) $groups[$group_name][] = $group_fields;
    }
    if (count($groups) > 0) $header['group'] = $groups;
}
function qwp_db_calc_data_count(&$query) {
    return intval($query->countQuery()->execute()->fetchField());
}
function qwp_db_set_search_condition_internal(&$field_values, &$query, &$allow_empty, &$field_conditions) {
    $op = "and";
    if (isset($field_conditions["op"])) {
        $op = $field_conditions["op"];
    }
    if ($op == "or") {
        $obj = db_or();
    } else {
        $obj = &$query;
    }
    if (isset($field_conditions["condition"])) {
        qwp_db_set_search_condition_internal($field_values, $obj, $allow_empty, $field_conditions["condition"]);
    }
    $has_fields = false;
    if (isset($field_conditions["fields"])) {
        foreach ($field_conditions["fields"] as $field => &$field_con) {
            if (!isset($field_values[$field])) {
                continue;
            }
            $value = $field_values[$field];
            unset($field_values[$field]);
            $is_empty = $value === '';
            if ($is_empty && !isset($allow_empty[$field])) {
                continue;
            }
            $has_fields = true;
            if (is_array($value)) {
                if ($field_con == 'in') {
                    $obj->condition($field, $value, $field_con);
                } else if ($field_con == '[]') {
                    $obj->condition($field, $value[0], '>=');
                    $obj->condition($field, $value[1], '<=');
                } else if ($field_con == '(]') {
                    $obj->condition($field, $value[0], '>');
                    $obj->condition($field, $value[1], '<=');
                } else if ($field_con == '[)') {
                    $obj->condition($field, $value[0], '>=');
                    $obj->condition($field, $value[1], '<');
                } else if ($field_con == '()') {
                    $obj->condition($field, $value[0], '>');
                    $obj->condition($field, $value[1], '<');
                }
            } else if ($field_con == 'like') {
                if (strpos($value, '%') === false && strpos($value, '?') === false) {
                    $value = '%' . $value . '%';
                }
                $obj->condition($field, $value, $field_con);
            } else if ($field_con == 'null') {
                $obj->isNull($field);
            } else if ($field_con == 'not null') {
                $obj->isNotNull($field);
            } else {
                if (isset($field_con[$value])) {
                    $fn_con = &$field_con[$value];
                    if (is_array($fn_con)) {
                        $value = $fn_con[1];
                        $fn_con = $fn_con[0];
                    }
                    if (function_exists($fn_con)) {
                        $fn_con = $fn_con($value);
                    }
                    if (isset($fn_con['where'])) {
                        $obj->where($fn_con['where']);
                        continue;
                    }
                } else {
                    $fn_con = null;
                }
                if ($fn_con == 'null') {
                    $obj->isNull($field);
                } else if ($fn_con == 'not null') {
                    $obj->isNotNull($field);
                } else {
                    $obj->condition($field, $value, $fn_con);
                }
            }
        }
    }
    if ($op == "or") {
        if ($has_fields) {
            $query->condition($obj);
        } else {
            unset($obj);
        }
    }
}
function qwp_db_set_search_condition(&$query, &$field_values, &$field_conditions) {
    $allow_empty = array();
    if ($field_conditions) {
        if (isset($field_conditions["allow empty"])) {<commit_msg>Refine qwp_db_set_search_condition<commit_after><?php
/*!
 * qwp: https://github.com/steem/qwp
 *
 * Copyright (c) 2015 Steem
 * Released under the MIT license
 */
require_once(DRUPAL_DB_ROOT . '/database.inc');

global $QWP_ACTIVE_DB;
if (isset($QWP_ACTIVE_DB)) {
    db_set_active($QWP_ACTIVE_DB);
}
function qwp_db_try_connect_db() {
    try {
        db_query('select version()')->execute();
    } catch (PDOException $e) {
        log_db_exception($e, "try_connect_db");
        db_remove_active();
    }
}
function qwp_db_or_from_array(&$ids, $field = 'id') {
    $or = '(';
    $sep = '';
    foreach ($ids as &$id) {
        $or .= $sep . "$field='$id'";
        if (!$sep) $sep = ' or ';
    }
    return $or . ')';
}
function qwp_db_error_is_duplicated(&$pdo_exception) {
    return $pdo_exception->errorInfo[1] == 1062;
}
function qwp_db_error_is_connection_gone(&$pdo_exception) {
    return $pdo_exception->errorInfo[1] == 2006;
}
function qwp_db_has_record($table_name, $where = null, $conditions = null) {
    $query = db_select($table_name, 't');
    if ($where) {
        $query->where($where);
    }
    if ($conditions) {
        foreach ($conditions as $con) {
            $query->condition($con[0], $con[1], isset($con[2]) ? $con[2] : null);
        }
    }
    $query->addExpression("count(1)", "n");
    $result = $query->execute();
    if ($result && $result->rowCount() > 0) {
        $r = $result->fetchAssoc();
        return intval($r["n"]) > 0;
    }
    return false;
}
function qwp_db_records_count($table_name, $where = null, $conditions = null) {
    $query = db_select($table_name, 't');
    if ($where) {
        $query->where($where);
    }
    if ($conditions) {
        foreach ($conditions as $con) {
            $query->condition($con[0], $con[1], isset($con[2]) ? $con[2] : null);
        }
    }
    $query->addExpression("count(1)", "n");
    $result = $query->execute();
    if ($result && $result->rowCount() > 0) {
        $r = $result->fetchAssoc();
        return intval($r["n"]);
    }
    return 0;
}
function qwp_db_get_one_record($table_name, $fields, $conditions, $where = null) {
    $query = db_select($table_name, 't')->fields('t', $fields);
    if ($conditions) {
        foreach ($conditions as $con) {
            $query->condition($con[0], $con[1], isset($con[2]) ? $con[2] : null);
        }
    }
    if ($where) {
        $query->where($where);
    }
    $result = $query->execute();
    return $result->rowCount() === 0 ? false : $result->fetchAssoc();
}
function qwp_db_get_fields_from_modal(&$modal, &$fields) {
    $fields = array();
    foreach ($modal as $idx => &$item) {
        if ($idx === 'alias' || !isset($item['table'])) {
            continue;
        }
        $table = $item['table'];
        if (!isset($fields[$table])) {
            $fields[$table] = array();
        }
        foreach ($item as $k => $v) {
            if ($k === 'table' || $k === 'group') {
                continue;
            } else if (is_string($v)) {
                if (strpos($v, ',') !== false) {
                    $fields[$table] = array_merge($fields[$table], explode(',', $v));
                } else {
                    $fields[$table][] = $v;
                }
            } else if ($v[0]) {
                $fields[$table][] = $v[0];
            }
        }
    }
}
function qwp_db_get_table_header_from_modal(&$modal, &$header) {
    $header = array(
        'names' => array(),
    );
    $groups = array();
    $has_alias = isset($modal['alias']);
    if ($has_alias) {
        $header['fields'] = array();
    }
    foreach ($modal as $idx => &$item) {
        if ($idx === 'alias') {
            continue;
        }
        $table = isset($item['table']) ? $item['table'] : '';
        $is_complex_arr = false;
        $group_fields = array();
        $group_name = false;
        foreach ($item as $k => $v) {
            if ($k === 'table') {
                continue;
            }
            if ($k === 'group') {
                $group_name = $v[0];
                $groups[$group_name] = array($v[1]);
                continue;
            }
            $is_string = is_string($v);
            if ($is_string && ($table || $is_complex_arr)) continue;
            if ($is_string) $v = $item;
            if (count($v) == 1) continue;
            if ($has_alias) {
                $ak = $table . '.' . $v[0];
                $header['fields'][] = $ak;
                if ($has_alias) $group_fields[] = $ak;
                if (isset($modal['alias'][$ak])) {
                    $v[0] = $modal['alias'][$ak];
                }
            } else {
                $group_fields[] = $v[0];
            }
            $header['names'][] = $v;
            if ($is_string) break;
            if (!$is_complex_arr) $is_complex_arr = !$is_string;
        }
        if ($group_name) $groups[$group_name][] = $group_fields;
    }
    if (count($groups) > 0) $header['group'] = $groups;
}
function qwp_db_calc_data_count(&$query) {
    return intval($query->countQuery()->execute()->fetchField());
}
function qwp_db_set_search_condition_internal(&$field_values, &$query, &$allow_empty, &$field_conditions) {
    $op = "and";
    if (isset($field_conditions["op"])) {
        $op = $field_conditions["op"];
    }
    if ($op == "or") {
        $obj = db_or();
    } else {
        $obj = &$query;
    }
    if (isset($field_conditions["condition"])) {
        qwp_db_set_search_condition_internal($field_values, $obj, $allow_empty, $field_conditions["condition"]);
    }
    $has_fields = false;
    if (isset($field_conditions["fields"])) {
        foreach ($field_conditions["fields"] as $field => &$field_con) {
            if (!isset($field_values[$field])) {
                continue;
            }
            $value = $field_values[$field];
            unset($field_values[$field]);
            $is_empty = $value === '';
            if ($is_empty && !isset($allow_empty[$field])) {
                continue;
            }
            $has_fields = true;
            if (is_array($value)) {
                if ($field_con == 'in') {
                    $obj->condition($field, $value, $field_con);
                } else if ($field_con == '[]') {
                    $obj->condition($field, $value[0], '>=');
                    $obj->condition($field, $value[1], '<=');
                } else if ($field_con == '(]') {
                    $obj->condition($field, $value[0], '>');
                    $obj->condition($field, $value[1], '<=');
                } else if ($field_con == '[)') {
                    $obj->condition($field, $value[0], '>=');
                    $obj->condition($field, $value[1], '<');
                } else if ($field_con == '()') {
                    $obj->condition($field, $value[0], '>');
                    $obj->condition($field, $value[1], '<');
                }
            } else if ($field_con == 'like') {
                if (strpos($value, '%') === false && strpos($value, '?') === false) {
                    $value = '%' . $value . '%';
                }
                $obj->condition($field, $value, $field_con);
            } else if ($field_con == 'null') {
                $obj->isNull($field);
            } else if ($field_con == 'not null') {
                $obj->isNotNull($field);
            } else {
                if (is_array($field_con) && isset($field_con[$value])) {
                    $fn_con = $field_con[$value];
                    if (is_array($fn_con)) {
                        $value = $fn_con[1];
                        $fn_con = $fn_con[0];
                    }
                } else if (function_exists($field_con)) {
                    $fn_con = $field_con;
                } else {
                    $fn_con = null;
                }
                if ($fn_con && function_exists($fn_con)) {
                    $fn_con = $fn_con($value);
                }
                if ($fn_con && isset($fn_con['where'])) {
                    $obj->where($fn_con['where']);
                    continue;
                }
                if ($fn_con == 'null') {
                    $obj->isNull($field);
                } else if ($fn_con == 'not null') {
                    $obj->isNotNull($field);
                } else {
                    $obj->condition($field, $value, $fn_con);
                }
            }
        }
    }
    if ($op == "or") {
        if ($has_fields) {
            $query->condition($obj);
        } else {
            unset($obj);
        }
    }
}
function qwp_db_set_search_condition(&$query, &$field_values, &$field_conditions) {
    $allow_empty = array();
    if ($field_conditions) {
        if (isset($field_conditions["allow empty"])) {
{'fraction_non_alphanumeric': 0.18900680585996077, 'fraction_numerical': 0.005998385050178798, 'mean_word_length': 1.7645089285714286, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 113, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6460161', 'n_tokens_mistral': 5549, 'n_tokens_neox': 5383, 'n_words': 977}