You are here

class SuggestionStorage in Autocomplete Search Suggestions 8

Same name and namespace in other branches
  1. 8.2 src/SuggestionStorage.php \Drupal\suggestion\SuggestionStorage
  2. 3.0.x src/SuggestionStorage.php \Drupal\suggestion\SuggestionStorage

Database CRUD.

Hierarchy

Expanded class hierarchy of SuggestionStorage

6 files declare their use of SuggestionStorage
suggestion.module in ./suggestion.module
Contains suggestion.module.
SuggestionAdminForm.php in src/Form/SuggestionAdminForm.php
SuggestionController.php in src/Controller/SuggestionController.php
SuggestionEditForm.php in src/Form/SuggestionEditForm.php
SuggestionIndexForm.php in src/Form/SuggestionIndexForm.php

... See full list

File

src/SuggestionStorage.php, line 8

Namespace

Drupal\suggestion
View source
class SuggestionStorage {

  // Source bitmaps.
  const CONTENT_BIT = 1;
  const DISABLED_BIT = 0;
  const PRIORITY_BIT = 4;
  const SURFER_BIT = 2;

  /**
   * Delete all content suggestions.
   *
   * @return object
   *   A Delete object.
   */
  public static function deleteContentSuggestion() {
    return db_delete('suggestion')
      ->condition('src', self::CONTENT_BIT)
      ->execute();
  }

  /**
   * Fetch all the suggestions.
   *
   * @param int $start
   *   The starting offset.
   * @param int $limit
   *   The query limit.
   *
   * @return array
   *   An array of suggestion objects.
   */
  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();
  }

  /**
   * Fetch a set of suggestions.
   *
   * @param string $ngram
   *   The search string.
   * @param int $atoms
   *   The number of atoms.
   * @param int $limit
   *   The query limit.
   *
   * @return array
   *   An array of suggestions.
   */
  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();
  }

  /**
   * Calculate the suggestion's bitmap.
   *
   * @param string $ngram
   *   The text to index.
   * @param int $src
   *   The bits to OR with the current bitmap.
   *
   * @return int
   *   The new bitmap value.
   */
  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();
  }

  /**
   * Fetch an array of content types.
   *
   * @return array
   *   An array of content types.
   */
  public static function getContentTypes() {
    return db_query("SELECT DISTINCT(type), type FROM {node} ORDER BY type ASC")
      ->fetchAllKeyed();
  }

  /**
   * Fetch the row count.
   *
   * @param string $ngram
   *   The text to search for.
   *
   * @return int
   *   The number of rows in the suggestion table.
   */
  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();
  }

  /**
   * Fetch an array of priority suggestions.
   *
   * @return array
   *   An array of suggestions.
   */
  public static function getKeywords() {
    return db_query("SELECT ngram FROM {suggestion} WHERE src & :src ORDER BY ngram ASC", [
      ':src' => self::PRIORITY_BIT,
    ])
      ->fetchCol();
  }

  /**
   * Fetch the quantity for the supplied ngram.
   *
   * @param string $ngram
   *   The ngram value.
   *
   * @return int
   *   The qty value for the supplied ngram.
   */
  public static function getNgramQty($ngram = '') {
    return db_query("SELECT IFNULL(SUM(qty), 0) FROM {suggestion} WHERE ngram = :ngram", [
      ':ngram' => $ngram,
    ])
      ->fetchField();
  }

  /**
   * Calculate a suggestion's score.
   *
   * @param array $atoms
   *   An array of strings.
   *
   * @return int
   *   The suggestion's score.
   */
  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;
  }

  /**
   * Build an array of source options.
   *
   * @return array
   *   An array of source options.
   */
  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'),
    ];
  }

  /**
   * Fetch the data for the suplied ngram.
   *
   * @param string $ngram
   *   The requested ngram.
   *
   * @return object
   *   An array of suggestion objects.
   */
  public static function getSuggestion($ngram = '') {
    return db_query("SELECT * FROM {suggestion} WHERE ngram = :ngram", [
      ':ngram' => $ngram,
    ])
      ->fetchObject();
  }

  /**
   * Fetch an array of node titles.
   *
   * @param int $nid
   *   The Node ID of the last node batched.
   * @param int $limit
   *   The query limit.
   *
   * @return array
   *   A node ID to title hash.
   */
  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();
  }

  /**
   * Merge the supplied suggestion.
   *
   * @param array $key
   *   The suggestion key array.
   * @param array $fields
   *   The suggestion field array.
   *
   * @return object
   *   A Merge object.
   */
  public static function mergeSuggestion(array $key = [], array $fields = []) {
    return db_merge('suggestion')
      ->key($key)
      ->fields($fields)
      ->execute();
  }

  /**
   * Search suggestions.
   *
   * @param string $ngram
   *   The requested ngram.
   * @param int $start
   *   The starting offset.
   * @param int $limit
   *   The query limit.
   *
   * @return array
   *   An array of suggestion objects.
   */
  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();
  }

  /**
   * Truncate the suggestion table.
   *
   * @return object
   *   A new TruncateQuery object for this connection.
   */
  public static function truncateSuggestion() {
    return db_truncate('suggestion')
      ->execute();
  }

  /**
   * Remove the content bit from the source bitmap.
   *
   * @return object
   *   An Update object.
   */
  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();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SuggestionStorage::CONTENT_BIT constant
SuggestionStorage::deleteContentSuggestion public static function Delete all content suggestions.
SuggestionStorage::DISABLED_BIT constant
SuggestionStorage::getAllSuggestions public static function Fetch all the suggestions.
SuggestionStorage::getAutocomplete public static function Fetch a set of suggestions.
SuggestionStorage::getBitmap public static function Calculate the suggestion's bitmap.
SuggestionStorage::getContentTypes public static function Fetch an array of content types.
SuggestionStorage::getCount public static function Fetch the row count.
SuggestionStorage::getKeywords public static function Fetch an array of priority suggestions.
SuggestionStorage::getNgramQty public static function Fetch the quantity for the supplied ngram.
SuggestionStorage::getScore public static function Calculate a suggestion's score.
SuggestionStorage::getSrcOptions public static function Build an array of source options.
SuggestionStorage::getSuggestion public static function Fetch the data for the suplied ngram.
SuggestionStorage::getTitles public static function Fetch an array of node titles.
SuggestionStorage::mergeSuggestion public static function Merge the supplied suggestion.
SuggestionStorage::PRIORITY_BIT constant
SuggestionStorage::search public static function Search suggestions.
SuggestionStorage::SURFER_BIT constant
SuggestionStorage::truncateSuggestion public static function Truncate the suggestion table.
SuggestionStorage::updateContentSrc public static function Remove the content bit from the source bitmap.