View source
<?php
namespace Drupal\suggestion;
class SuggestionStorage {
const CONTENT_BIT = 1;
const DISABLED_BIT = 0;
const PRIORITY_BIT = 4;
const SURFER_BIT = 2;
public static function deleteContentSuggestion() {
return db_delete('suggestion')
->condition('src', self::CONTENT_BIT)
->execute();
}
public static function getAllSuggestions($start = NULL, $limit = 100) {
if (is_numeric($start) && intval($limit)) {
return db_query_range("SELECT * FROM {suggestion} ORDER BY ngram ASC", $start, $limit)
->fetchAll();
}
return db_query("SELECT * FROM {suggestion} ORDER BY ngram ASC")
->fetchAll();
}
public static function getAutocomplete($ngram = '', $atoms = 0, $limit = 100) {
$args = [
':ngram' => $ngram,
':atoms' => (int) $atoms,
];
$stmt = "\n SELECT\n ngram,\n ngram\n FROM\n {suggestion}\n WHERE\n ngram LIKE :ngram\n AND src\n AND atoms <= :atoms\n ORDER BY\n density DESC,\n ngram ASC,\n atoms ASC\n ";
return db_query_range($stmt, 0, (int) $limit, $args)
->fetchAllKeyed();
}
public static function getBitmap($ngram = '', $src = self::CONTENT_BIT) {
$args = [
':ngram' => $ngram,
':src' => (int) $src,
];
return db_query("SELECT IFNULL(SUM(src), 0) | :src FROM {suggestion} WHERE ngram = :ngram", $args)
->fetchField();
}
public static function getContentTypes() {
return db_query("SELECT DISTINCT(type), type FROM {node} ORDER BY type ASC")
->fetchAllKeyed();
}
public static function getCount($ngram = '') {
if ($ngram) {
return db_query("SELECT COUNT(*) FROM {suggestion} WHERE ngram LIKE :ngram", [
':ngram' => $ngram,
])
->fetchField();
}
return db_query("SELECT COUNT(*) FROM {suggestion}")
->fetchField();
}
public static function getKeywords() {
return db_query("SELECT ngram FROM {suggestion} WHERE src & :src ORDER BY ngram ASC", [
':src' => self::PRIORITY_BIT,
])
->fetchCol();
}
public static function getNgramQty($ngram = '') {
return db_query("SELECT IFNULL(SUM(qty), 0) FROM {suggestion} WHERE ngram = :ngram", [
':ngram' => $ngram,
])
->fetchField();
}
public static function getScore(array $atoms = []) {
$types = SuggestionHelper::types();
if (!count($types)) {
return 0;
}
$query = db_select('node__body', 'b');
$query
->fields('b', [
'entity_id',
]);
$query
->join('node_field_data', 'n', 'n.nid = b.entity_id');
$query
->condition('n.status', 1);
$query
->condition('n.type', $types, 'IN');
foreach ($atoms as $atom) {
$query
->condition('b.body_value', '%' . db_like($atom) . '%', 'LIKE');
}
return count($atoms) ? count($query
->execute()
->fetchCol()) : 0;
}
public static function getSrcOptions() {
return [
self::DISABLED_BIT => t('Disabled'),
self::CONTENT_BIT => t('Content'),
self::SURFER_BIT => t('Surfer'),
self::PRIORITY_BIT => t('Priority'),
];
}
public static function getSuggestion($ngram = '') {
return db_query("SELECT * FROM {suggestion} WHERE ngram = :ngram", [
':ngram' => $ngram,
])
->fetchObject();
}
public static function getTitles($nid = 0, $limit = NULL) {
$args = [
':nid' => (int) $nid,
':types[]' => SuggestionHelper::types(),
];
if (!count($args[':types[]'])) {
return [];
}
$stmt = "\n SELECT\n nid,\n title\n FROM\n {node_field_data}\n WHERE\n status = 1\n AND nid > :nid\n AND type IN (:types[])\n ORDER BY\n nid ASC\n ";
if (is_numeric($limit)) {
return db_query_range($stmt, 0, $limit, $args)
->fetchAllKeyed();
}
return db_query($stmt, $args)
->fetchAllKeyed();
}
public static function mergeSuggestion(array $key = [], array $fields = []) {
return db_merge('suggestion')
->key($key)
->fields($fields)
->execute();
}
public static function search($ngram, $start = NULL, $limit = 100) {
$args = [
':ngram' => $ngram,
];
if (is_numeric($start) && intval($limit)) {
return db_query_range("SELECT * FROM {suggestion} WHERE ngram LIKE :ngram ORDER BY ngram ASC", $start, $limit, $args)
->fetchAll();
}
return db_query("SELECT * FROM {suggestion} WHERE ngram LIKE :ngram ORDER BY ngram ASC", $args)
->fetchAll();
}
public static function truncateSuggestion() {
return db_truncate('suggestion')
->execute();
}
public static function updateContentSrc() {
return db_update('suggestion')
->expression('src', 'src & :src', [
':src' => intval(self::PRIORITY_BIT | self::SURFER_BIT),
])
->condition('src', self::CONTENT_BIT, '&')
->execute();
}
}