Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
phrasefts.php
1<?php
2
4
12
13
14abstract class PhraseFts extends DataManager
15{
16 use Index\Internals\BulkOperation;
17
19 protected static array $ftsEntities = [];
20
26 public static function getMap(): array
27 {
28 return [
29 'ID' => [
30 'data_type' => 'integer',
31 'primary' => true,
32 ],
33 'FILE_ID' => [
34 'data_type' => 'integer',
35 ],
36 'PATH_ID' => [
37 'data_type' => 'integer',
38 ],
39 'CODE' => [
40 'data_type' => 'string',
41 ],
42 'PHRASE' => [
43 'data_type' => 'string',
44 ],
45 'FILE' => [
46 'data_type' => Index\Internals\FileIndexTable::class,
47 'reference' => [
48 '=this.FILE_ID' => 'ref.ID',
49 ],
50 'join_type' => 'INNER',
51 ],
52 'PATH' => [
53 'data_type' => Index\Internals\PathIndexTable::class,
54 'reference' => [
55 '=this.PATH_ID' => 'ref.ID',
56 ],
57 'join_type' => 'INNER',
58 ],
59 ];
60 }
61
66 public static function checkTables(): void
67 {
68 $tables = [];
69 $tablesRes = Application::getConnection()->query("SHOW TABLES LIKE 'b_translate_phrase_fts_%'");
70 while ($row = $tablesRes->fetch())
71 {
72 $tableName = array_shift($row);
73 $langId = substr($tableName, -2);
74 $tables[$langId] = $tableName;
75 }
76 foreach (Translate\Config::getEnabledLanguages() as $langId)
77 {
78 if (!preg_match("/[a-z0-9]{2}/i", $langId))
79 {
80 continue;
81 }
82 if (!isset($tables[$langId]))
83 {
84 self::createTable($langId);
85 }
86 else
87 {
88 unset($tables[$langId]);
89 }
90 }
91 foreach ($tables as $langId => $table)
92 {
93 self::dropTable($langId);
94 }
95 }
96
102 public static function getPartitionTableName(string $langId): string
103 {
104 if (!in_array($langId, Translate\Config::getLanguages(), true))
105 {
106 throw new ArgumentException('Parameter langId has wrong value');
107 }
108
109 return "b_translate_phrase_fts_{$langId}";
110 }
111
118 public static function createTable(string $langId): void
119 {
120 $partitionTable = self::getPartitionTableName($langId);
121
122 $suffix = mb_strtoupper($langId);
123
124 Application::getConnection()->queryExecute("
125 CREATE TABLE IF NOT EXISTS `{$partitionTable}` (
126 `ID` int not null,
127 `FILE_ID` int not null,
128 `PATH_ID` int not null,
129 `CODE` varbinary(255) not null,
130 `PHRASE` text,
131 PRIMARY KEY (`ID`),
132 UNIQUE KEY `IXU_TRNSL_FTS_PT_{$suffix}` (`PATH_ID`, `CODE`),
133 UNIQUE KEY `IXU_TRNSL_FTS_FL_{$suffix}` (`FILE_ID`, `CODE`),
134 FULLTEXT INDEX `IXF_TRNSL_FTS_PH_{$suffix}` (`PHRASE`)
135 ) DELAY_KEY_WRITE=1
136 ");
137 }
138
145 public static function dropTable(string $langId): void
146 {
147 $partitionTable = self::getPartitionTableName($langId);
148
149 Application::getConnection()->queryExecute("DROP TABLE IF EXISTS `{$partitionTable}`");
150 }
151
157 public static function getFtsEntityClass(string $langId): string
158 {
159 static $cache = [];
160 if (!isset($cache[$langId]))
161 {
162 $entity = self::getFtsEntity($langId);
163 $cache[$langId] = $entity->getDataClass();
164 }
165
166 return $cache[$langId];
167 }
168
174 public static function getFtsEntity(string $langId): ORM\Entity
175 {
176 if (!in_array($langId, Translate\Config::getEnabledLanguages(), true))
177 {
178 throw new ArgumentException('Parameter langId has wrong value');
179 }
180 if (!isset(self::$ftsEntities[$langId]))
181 {
182 self::$ftsEntities[$langId] = ORM\Entity::compileEntity(
183 'PhraseIndexTfsEntity'. mb_strtoupper($langId),
184 [],
185 [
186 'table_name' => self::getPartitionTableName($langId),
187 'namespace' => __NAMESPACE__,
188 'parent' => Index\Internals\PhraseFts::class
189 ]
190 );
191 }
192
193 return self::$ftsEntities[$langId];
194 }
195
203 public static function purge(?Translate\Filter $filter = null): void
204 {
205 $filterOut = static::processFilter($filter);
206 static::bulkDelete($filterOut);
207 }
208
216 public static function processFilter(?Translate\Filter $filter = null): array
217 {
218 $filterOut = [];
219
220 if ($filter !== null)
221 {
222 foreach ($filter as $key => $value)
223 {
224 if (empty($value) && $value !== '0')
225 {
226 continue;
227 }
228
229 if ($key === 'path')
230 {
231 $filterOut['=%PATH.PATH'] = $value.'%';
232 }
233 elseif ($key === 'fileId')
234 {
235 $filterOut['=FILE_ID'] = $value;
236 }
237 elseif ($key === 'pathId')
238 {
239 $filterOut['=PATH_ID'] = $value;
240 }
241 else
242 {
243 if (static::getEntity()->hasField(trim($key, '<>!=@~%*')))
244 {
245 $filterOut[$key] = $value;
246 }
247 }
248 }
249 }
250
251 return $filterOut;
252 }
253}
static getConnection($name="")
static processFilter(?Translate\Filter $filter=null)
static getPartitionTableName(string $langId)
static purge(?Translate\Filter $filter=null)