Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
wordchain.php
1<?php
9
10use Bitrix\Main;
15
16Loc::loadMessages(__FILE__);
17
18final class WordChainTable extends Entity\DataManager
19{
20 public static function getFilePath()
21 {
22 return __FILE__;
23 }
24
25 public static function getTableName()
26 {
27 return 'b_sale_loc_wordchain';
28 }
29
30 public static function cleanUp()
31 {
32 $dbConnection = Main\HttpApplication::getConnection();
33
34 try
35 {
36 $dbConnection->query('drop table '.static::getTableName());
37
38 $allowedTypes = array('REGION', 'SUBREGION', 'CITY', 'VILLAGE', 'STREET');
39
40 foreach($allowedTypes as $type)
41 {
42 //$dbConnection->query('drop table b_sale_loc_wc_'.ToLower($type)); // here replace according to map: TYPE_CODE => TABLE_ID
43 $dbConnection->query('truncate table b_sale_loc_wc_'.ToLower($type));
44 }
45 }
46 catch(\Bitrix\Main\DB\SqlQueryException $e)
47 {
48 }
49 }
50
51 public static function createTables()
52 {
53 static::cleanUp();
54
55 $sql = "create table b_sale_loc_wordchain
56 (
57 LOCATION_ID int primary key,
58 TYPE_ID int,
59
60 REGION_ID int,
61 SUBREGION_ID int,
62 CITY_ID int,
63 VILLAGE_ID int,
64 STREET_ID int,
65
66 RELEVANCY int default '0'
67 )";
68
69 Main\HttpApplication::getConnection()->query($sql);
70
71 $allowedTypes = array('REGION', 'SUBREGION', 'CITY', 'VILLAGE', 'STREET');
72
73 foreach($allowedTypes as $type)
74 {
75 }
76
77 /*
78 create table b_sale_loc_wc_region
79 (
80 LOCATION_ID int primary key,
81 LANGUAGE_ID char(2) default 'ru',
82
83 W_1 varchar(50),
84 WORD_COUNT tinyint default '0'
85 )
86
87 create table b_sale_loc_wc_subregion
88 (
89 LOCATION_ID int primary key,
90 LANGUAGE_ID char(2) default 'ru',
91
92 W_1 varchar(50),
93 WORD_COUNT tinyint default '0'
94 )
95
96 create table b_sale_loc_wc_city
97 (
98 LOCATION_ID int primary key,
99 LANGUAGE_ID char(2) default 'ru',
100
101 W_1 varchar(50),
102 W_2 varchar(50),
103 W_3 varchar(50),
104 WORD_COUNT tinyint default '0'
105 )
106
107 create table b_sale_loc_wc_village
108 (
109 LOCATION_ID int primary key,
110 LANGUAGE_ID char(2) default 'ru',
111
112 W_1 varchar(50),
113 W_2 varchar(50),
114 W_3 varchar(50),
115 WORD_COUNT tinyint default '0'
116 )
117
118 create table b_sale_loc_wc_street
119 (
120 LOCATION_ID int primary key,
121 LANGUAGE_ID char(2) default 'ru',
122
123 W_1 varchar(50),
124 W_2 varchar(50),
125 W_3 varchar(50),
126 WORD_COUNT tinyint default '0'
127 )
128 */
129
130
131 }
132
133 public static function reCreateIndex()
134 {
135 /*
136 $sql = 'create index IX_SALE_WC on b_sale_loc_wordchain (
137
138 SORT,
139 TYPE_SORT,
140
141 W_1,
142 W_2,
143 W_3,
144 W_4,
145 W_5,
146 W_6,
147 W_7,
148 W_8,
149 W_9,
150 W_10
151 )';
152
153 Main\HttpApplication::getConnection()->query($sql);
154 */
155 }
156
157 const STEP_SIZE = 100;
158
159 public static function reInitData($parameters = array())
160 {
161 static::createTables();
162
163 $offset = 0;
164 $stat = array();
165
166 $types = array();
167 $typeSort = array();
168 $res = Location\TypeTable::getList(array('select' => array('ID', 'CODE', 'SORT')));
169
170 $allowedTypes = array('REGION', 'SUBREGION', 'CITY', 'VILLAGE', 'STREET');
171
172 while($item = $res->fetch())
173 {
174 if(in_array($item['CODE'], $allowedTypes))
175 $types[$item['CODE']] = $item['ID'];
176
177 $typeSort[$item['ID']] = $item['SORT'];
178 }
179
180 $typesBack = array_flip($types);
181
182 //print_r($types);
183 //_print_r($typeSort);
184
185 $wordChain = array();
186 $pathChain = array();
187
188 //_dump_r('GO!');
189
190 $prevDepth = 0;
191 while(true)
192 {
193 $res = Location\LocationTable::getList(array(
194 'select' => array(
195 'ID',
196 'TYPE_ID',
197 'LNAME' => 'NAME.NAME',
198 'DEPTH_LEVEL',
199 'SORT'
200 ),
201 'filter' => array(
202 '=TYPE_ID' => array_values($types),
203 '=NAME.LANGUAGE_ID' => LANGUAGE_ID
204 ),
205 'order' => array(
206 'LEFT_MARGIN' => 'asc'
207 ),
208 'limit' => self::STEP_SIZE,
209 'offset' => $offset
210 ));
211
212 $cnt = 0;
213 while($item = $res->fetch())
214 {
215 if($item['DEPTH_LEVEL'] < $prevDepth)
216 {
217 //print('DROP to '.$item['DEPTH_LEVEL'].'<br />');
218
219 // drop chain to DEPTH_LEVEL inclusively
220 $newWC = array();
221 $newPC = array();
222 foreach($wordChain as $dl => $name)
223 {
224 if($dl >= $item['DEPTH_LEVEL'])
225 break;
226
227 $newWC[$dl] = $name;
228 }
229
230 $wordChain = $newWC;
231
232 foreach($pathChain as $dl => $id)
233 {
234 if($dl >= $item['DEPTH_LEVEL'])
235 break;
236
237 $newPC[$dl] = $id;
238 }
239
240 $pathChain = $newPC;
241 }
242
243 $wordChain[$item['DEPTH_LEVEL']] = $item['LNAME'];
244 $pathChain[$item['DEPTH_LEVEL']] = array('TYPE' => $item['TYPE_ID'], 'ID' => $item['ID']);
245
246 $prevDepth = $item['DEPTH_LEVEL'];
247
248 //print($item['DEPTH_LEVEL'].' - '.implode(' ', WordStatTable::parseQuery(implode(' ', $wordChain))).'<br />');
249
250 $parsed = WordStatTable::parseQuery(implode(' ', $wordChain));
251 $wordMap = array();
252 $i = 1;
253 foreach($parsed as $word)
254 {
255 $wordMap['W_'.$i] = $word;
256 $i++;
257 }
258 $pathMap = array();
259 foreach($pathChain as $elem)
260 $pathMap[$typesBack[$elem['TYPE']].'_ID'] = $elem['ID'];
261
262 $data = array_merge($wordMap, $pathMap, array(
263 'LOCATION_ID' => $item['ID'],
264 'TYPE_ID' => $item['TYPE_ID'],
265 'TYPE_SORT' => $typeSort[$item['TYPE_ID']],
266 'SORT' => $item['SORT'],
267 'WORD_COUNT' => count($wordMap)
268 ));
269
270 //print('<pre>');
271 //print('</pre>');
272
273 try
274 {
275 static::add($data);
276 }
277 catch(\Exception $e)
278 {
279 _dump_r('Cant add '.implode(' ', $wordChain).' ('.count($wordMap).')<br />');
280 // duplicate or smth
281 }
282
283 $cnt++;
284 }
285
286 if(!$cnt)
287 break;
288
289 $offset += self::STEP_SIZE;
290 }
291 }
292
293 public static function search($words, $offset) // very temporal prototype
294 {
295 $dbConnection = Main\HttpApplication::getConnection();
296 $dbHelper = Main\HttpApplication::getConnection()->getSqlHelper();
297
298 $where = array();
299
300 foreach($words as $word)
301 {
302 $whereWord = array();
303 for($k = 1; $k <= 10; $k++)
304 {
305 $whereWord[] = "W_".$k." like '".$dbHelper->forSql($word)."%'";
306 }
307
308 $where[] = '('.implode(' or ', $whereWord).')';
309 }
310
311 $sql = "
312 select SQL_NO_CACHE IX.LOCATION_ID as ID, L.CODE, IX.TYPE_ID, L.LEFT_MARGIN, L.RIGHT_MARGIN, N.NAME as NAME from ".static::getTableName()." IX
313
314 inner join b_sale_location L on IX.LOCATION_ID = L.ID
315 inner join b_sale_loc_name N on IX.LOCATION_ID = N.LOCATION_ID and N.LANGUAGE_ID = 'ru'
316
317 where ".implode(' and ', $where)."
318
319 order by
320 IX.SORT asc,
321 IX.TYPE_SORT asc
322 limit 10
323 ".(intval($offset) ? 'offset '.intval($offset) : '')."
324 ";
325
326 _dump_r($sql);
327
328 /*
329 print('<pre>');
330 print_r($sql);
331 print('</pre>');
332 */
333
334 return $dbConnection->query($sql);
335 }
336
337 public static function getMap()
338 {
339 return array(
340
341 'LOCATION_ID' => array(
342 'data_type' => 'integer',
343 'primary' => true // tmp
344 ),
345 'TYPE_ID' => array(
346 'data_type' => 'integer',
347 ),
348
349
350 'REGION_ID' => array(
351 'data_type' => 'integer',
352 ),
353
354 'SUBREGION_ID' => array(
355 'data_type' => 'integer',
356 ),
357 'CITY_ID' => array(
358 'data_type' => 'integer',
359 ),
360 'VILLAGE_ID' => array(
361 'data_type' => 'integer',
362 ),
363 'STREET_ID' => array(
364 'data_type' => 'integer',
365 ),
366
367 'W_1' => array(
368 'data_type' => 'string',
369 ),
370 'W_2' => array(
371 'data_type' => 'string',
372 ),
373 'W_3' => array(
374 'data_type' => 'string',
375 ),
376 'W_4' => array(
377 'data_type' => 'string',
378 ),
379 'W_5' => array(
380 'data_type' => 'string',
381 ),
382 'W_6' => array(
383 'data_type' => 'string',
384 ),
385 'W_7' => array(
386 'data_type' => 'string',
387 ),
388 'W_8' => array(
389 'data_type' => 'string',
390 ),
391 'W_9' => array(
392 'data_type' => 'string',
393 ),
394 'W_10' => array(
395 'data_type' => 'string',
396 ),
397
398 'WORD_COUNT' => array(
399 'data_type' => 'integer',
400 ),
401
402 'TYPE_SORT' => array(
403 'data_type' => 'integer',
404 ),
405 'SORT' => array(
406 'data_type' => 'integer',
407 ),
408 );
409 }
410}
411
static loadMessages($file)
Definition loc.php:64
static reInitData($parameters=array())