|
Apache/2.4.41 (Ubuntu) Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) server ip : 62.171.164.128 | your ip : 127.0.0.1 safemode OFF > / home / Mirasvit / Misspell / Provider / |
Filename | /home/Mirasvit/Misspell/Provider/Indexer.php |
Size | 6.33 kb |
Permission | rw-r--r-- |
Owner | root : root |
Create time | 01-Jul-2024 20:52 |
Last modified | 06-Apr-2021 18:06 |
Last accessed | 21-Aug-2025 18:19 |
Actions | edit | rename | delete | download (gzip) |
View | text | code | image |
<?php
/**
* Mirasvit
*
* This source file is subject to the Mirasvit Software License, which is available at https://mirasvit.com/license/.
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
* If you wish to customize this module for your needs.
* Please refer to http://www.magentocommerce.com for more information.
*
* @category Mirasvit
* @package mirasvit/module-misspell
* @version 1.0.38
* @copyright Copyright (C) 2020 Mirasvit (https://mirasvit.com/)
*/
namespace Mirasvit\Misspell\Provider;
use Magento\Framework\App\ResourceConnection;
use Mirasvit\Misspell\Helper\Text as TextHelper;
class Indexer
{
/**
* @var array
*/
private $allowedTables
= [
'catalogsearch_fulltext',
'mst_searchindex_',
'catalog_product_entity_text',
'catalog_product_entity_varchar',
'catalog_category_entity_text',
'catalog_category_entity_varchar',
];
/**
* @var array
*/
private $disallowedTables
= [
'mst_searchindex_mage_catalogsearch_query',
];
/**
* @var \Magento\Framework\App\ResourceConnection
*/
private $resource;
/**
* @var \Magento\Framework\DB\Adapter\AdapterInterface
*/
private $connection;
/**
* @var \Mirasvit\Misspell\Helper\Text
*/
private $text;
/**
* Indexer constructor.
*
* @param ResourceConnection $resource
* @param TextHelper $textHelper
*/
public function __construct(
ResourceConnection $resource,
TextHelper $textHelper
) {
$this->resource = $resource;
$this->connection = $this->resource->getConnection();
$this->text = $textHelper;
}
/**
* @return void
*/
public function reindex()
{
$indexTable = $this->resource->getTableName('mst_misspell_index');
$this->connection->delete($indexTable);
foreach ($this->getTables() as $table => $columns) {
$results = [];
if (!count($columns)) {
continue;
}
foreach ($columns as $idx => $col) {
$columns[$idx] = '`' . $col . '`';
}
$select = $this->connection->select();
$fromColumns = new \Zend_Db_Expr("CONCAT_WS(' '," . implode(',', $columns) . ") as data_index");
$select->from($table, $fromColumns);
$results = $this->getTablesData($select);
$rows = [];
foreach ($results as $word => $freq) {
$localFrequency = $freq / count($results);
$rows[] = [
'keyword' => $word,
'trigram' => $this->text->getTrigram($word),
'frequency' => $localFrequency,
];
if (count($rows) > 1000) {
$this->connection->insertArray($indexTable, ['keyword', 'trigram', 'frequency'], $rows);
$rows = [];
}
}
if (count($rows) > 0) {
$this->connection->insertArray($indexTable, ['keyword', 'trigram', 'frequency'], $rows);
}
}
$this->connection->delete($this->resource->getTableName('mst_misspell_suggest'));
}
/**
* @param \Magento\Framework\DB\Select $select
* @param array $results
* @param int $offset
*
* @return array
* @throws \Zend_Db_Statement_Exception
*/
private function getTablesData($select, $results = [], $offset = 0)
{
while (true) {
$select->limit('10000', $offset);
$result = $this->connection->query($select);
$rows = $result->fetchAll();
if (!$rows) {
return $results;
}
foreach ($rows as $row) {
$data = $row['data_index'];
if (!empty($data)) {
$this->split($data, $results);
}
}
$offset += 10000;
}
return $results;
}
/**
* Split string to words
*
* @param string $string
* @param array $results
* @param int $increment
*
* @return void
*/
protected function split($string, &$results, $increment = 1)
{
$string = $this->text->cleanString($string);
$words = $this->text->splitWords($string);
foreach ($words as $word) {
if ($this->text->strlen($word) >= $this->text->getGram()
&& !is_numeric($word)
) {
$word = $this->text->strtolower($word);
if (!isset($results[$word])) {
$results[$word] = $increment;
} else {
$results[$word] += $increment;
}
}
}
}
/**
* List of tables that follow allowedTables, disallowedTables conditions
* @return array
*/
protected function getTables()
{
$result = [];
$tables = $this->connection->getTables();
foreach ($tables as $table) {
$isAllowed = false;
foreach ($this->allowedTables as $allowedTable) {
if (mb_strpos($table, $allowedTable) !== false) {
$isAllowed = true;
}
}
foreach ($this->disallowedTables as $disallowedTable) {
if (mb_strpos($table, $disallowedTable) !== false) {
$isAllowed = false;
}
}
if (!$isAllowed) {
continue;
}
$result[$table] = $this->getTextColumns($table);
}
return $result;
}
/**
* Text columns
*
* @param string $table Database table name
*
* @return array list of columns with text type
*/
protected function getTextColumns($table)
{
$result = [];
$allowedTypes = ['text', 'varchar', 'mediumtext', 'longtext'];
$columns = $this->connection->describeTable($table);
foreach ($columns as $column => $info) {
if (in_array($info['DATA_TYPE'], $allowedTypes)) {
$result[] = $column;
}
}
return $result;
}
}
/**
* Mirasvit
*
* This source file is subject to the Mirasvit Software License, which is available at https://mirasvit.com/license/.
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
* If you wish to customize this module for your needs.
* Please refer to http://www.magentocommerce.com for more information.
*
* @category Mirasvit
* @package mirasvit/module-misspell
* @version 1.0.38
* @copyright Copyright (C) 2020 Mirasvit (https://mirasvit.com/)
*/
namespace Mirasvit\Misspell\Provider;
use Magento\Framework\App\ResourceConnection;
use Mirasvit\Misspell\Helper\Text as TextHelper;
class Indexer
{
/**
* @var array
*/
private $allowedTables
= [
'catalogsearch_fulltext',
'mst_searchindex_',
'catalog_product_entity_text',
'catalog_product_entity_varchar',
'catalog_category_entity_text',
'catalog_category_entity_varchar',
];
/**
* @var array
*/
private $disallowedTables
= [
'mst_searchindex_mage_catalogsearch_query',
];
/**
* @var \Magento\Framework\App\ResourceConnection
*/
private $resource;
/**
* @var \Magento\Framework\DB\Adapter\AdapterInterface
*/
private $connection;
/**
* @var \Mirasvit\Misspell\Helper\Text
*/
private $text;
/**
* Indexer constructor.
*
* @param ResourceConnection $resource
* @param TextHelper $textHelper
*/
public function __construct(
ResourceConnection $resource,
TextHelper $textHelper
) {
$this->resource = $resource;
$this->connection = $this->resource->getConnection();
$this->text = $textHelper;
}
/**
* @return void
*/
public function reindex()
{
$indexTable = $this->resource->getTableName('mst_misspell_index');
$this->connection->delete($indexTable);
foreach ($this->getTables() as $table => $columns) {
$results = [];
if (!count($columns)) {
continue;
}
foreach ($columns as $idx => $col) {
$columns[$idx] = '`' . $col . '`';
}
$select = $this->connection->select();
$fromColumns = new \Zend_Db_Expr("CONCAT_WS(' '," . implode(',', $columns) . ") as data_index");
$select->from($table, $fromColumns);
$results = $this->getTablesData($select);
$rows = [];
foreach ($results as $word => $freq) {
$localFrequency = $freq / count($results);
$rows[] = [
'keyword' => $word,
'trigram' => $this->text->getTrigram($word),
'frequency' => $localFrequency,
];
if (count($rows) > 1000) {
$this->connection->insertArray($indexTable, ['keyword', 'trigram', 'frequency'], $rows);
$rows = [];
}
}
if (count($rows) > 0) {
$this->connection->insertArray($indexTable, ['keyword', 'trigram', 'frequency'], $rows);
}
}
$this->connection->delete($this->resource->getTableName('mst_misspell_suggest'));
}
/**
* @param \Magento\Framework\DB\Select $select
* @param array $results
* @param int $offset
*
* @return array
* @throws \Zend_Db_Statement_Exception
*/
private function getTablesData($select, $results = [], $offset = 0)
{
while (true) {
$select->limit('10000', $offset);
$result = $this->connection->query($select);
$rows = $result->fetchAll();
if (!$rows) {
return $results;
}
foreach ($rows as $row) {
$data = $row['data_index'];
if (!empty($data)) {
$this->split($data, $results);
}
}
$offset += 10000;
}
return $results;
}
/**
* Split string to words
*
* @param string $string
* @param array $results
* @param int $increment
*
* @return void
*/
protected function split($string, &$results, $increment = 1)
{
$string = $this->text->cleanString($string);
$words = $this->text->splitWords($string);
foreach ($words as $word) {
if ($this->text->strlen($word) >= $this->text->getGram()
&& !is_numeric($word)
) {
$word = $this->text->strtolower($word);
if (!isset($results[$word])) {
$results[$word] = $increment;
} else {
$results[$word] += $increment;
}
}
}
}
/**
* List of tables that follow allowedTables, disallowedTables conditions
* @return array
*/
protected function getTables()
{
$result = [];
$tables = $this->connection->getTables();
foreach ($tables as $table) {
$isAllowed = false;
foreach ($this->allowedTables as $allowedTable) {
if (mb_strpos($table, $allowedTable) !== false) {
$isAllowed = true;
}
}
foreach ($this->disallowedTables as $disallowedTable) {
if (mb_strpos($table, $disallowedTable) !== false) {
$isAllowed = false;
}
}
if (!$isAllowed) {
continue;
}
$result[$table] = $this->getTextColumns($table);
}
return $result;
}
/**
* Text columns
*
* @param string $table Database table name
*
* @return array list of columns with text type
*/
protected function getTextColumns($table)
{
$result = [];
$allowedTypes = ['text', 'varchar', 'mediumtext', 'longtext'];
$columns = $this->connection->describeTable($table);
foreach ($columns as $column => $info) {
if (in_array($info['DATA_TYPE'], $allowedTypes)) {
$result[] = $column;
}
}
return $result;
}
}