1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
pull_watch.php
См. документацию.
1<?php
2
4{
5 const bucket_size = 100;
6
7 private static $arUpdate = Array();
8 private static $arInsert = Array();
9 private static array $tagCache = [];
10
11 public static function Add($userId, $tag, $immediate = false)
12 {
13 global $DB, $CACHE_MANAGER;
14
15 $userId = intval($userId);
16 if ($userId == 0 || $tag == '')
17 return false;
18
19 $arResult = $CACHE_MANAGER->Read(3600, $cache_id="b_pw_".$userId, "b_pull_watch");
20 if ($arResult)
21 $arResult = $CACHE_MANAGER->Get($cache_id);
22
23 if(!$arResult)
24 {
25 CTimeZone::Disable();
26 $strSql = "
27 SELECT ID, USER_ID, TAG, ".$DB->DatetimeToTimestampFunction("DATE_CREATE")." AS DATE_CREATE
28 FROM b_pull_watch
29 WHERE USER_ID = ".intval($userId)."
30 ";
31 CTimeZone::Enable();
32 $dbRes = $DB->Query($strSql);
33 while ($arRes = $dbRes->Fetch())
34 $arResult[$arRes["TAG"]] = $arRes;
35
36 $CACHE_MANAGER->Set($cache_id, $arResult);
37 }
38 if (!empty($arResult[$tag]))
39 {
40 if ($arResult[$tag]['DATE_CREATE']+1860 > time())
41 {
42 self::$arUpdate[intval($arResult[$tag]['ID'])] = intval($arResult[$tag]['ID']);
43 return true;
44 }
45 else
46 {
47 self::Delete($userId, $tag);
48 return self::Add($userId, $tag);
49 }
50 }
51 $CACHE_MANAGER->Clean("b_pw_".$userId, "b_pull_watch");
52
53 self::$arInsert[trim($tag)] = trim($tag);
54
55 if ($immediate || defined('BX_CHECK_AGENT_START') && !defined('BX_WITH_ON_AFTER_EPILOG'))
56 {
58 }
59
60 self::cleanCacheByTag((string)$tag);
61
62 return true;
63 }
64
65 public static function DeferredSql($userId = false)
66 {
67 global $DB, $USER;
68 if (empty(self::$arUpdate) && empty(self::$arInsert))
69 return false;
70
71 $userId = intval($userId);
72 if (!$userId)
73 {
74 if (defined('PULL_USER_ID'))
75 {
76 $userId = PULL_USER_ID;
77 }
78 else if (is_object($GLOBALS['USER']) && $GLOBALS['USER']->GetID() > 0)
79 {
80 $userId = $GLOBALS['USER']->GetId();
81 }
82 else if (intval($_SESSION["SESS_SEARCHER_ID"]) <= 0 && intval($_SESSION["SESS_GUEST_ID"]) > 0 && \CPullOptions::GetGuestStatus())
83 {
84 $userId = intval($_SESSION["SESS_GUEST_ID"])*-1;
85 }
86 }
87 if ($userId === 0)
88 {
89 return false;
90 }
91
92 $arChannel = CPullChannel::Get($userId);
93 if (!$arChannel)
94 {
95 return false;
96 }
97 if (!empty(self::$arUpdate))
98 {
99 $DB->Query("
100 UPDATE b_pull_watch
101 SET DATE_CREATE = ".$DB->CurrentTimeFunction().", CHANNEL_ID = '".$DB->ForSQL($arChannel['CHANNEL_ID'])."'
102 WHERE ID IN (".(implode(',', self::$arUpdate)).")
103 ");
104 }
105
106 if ($DB->type == "MYSQL")
107 {
108 if (!empty(self::$arInsert))
109 {
110 $strSqlPrefix = "INSERT INTO b_pull_watch (USER_ID, CHANNEL_ID, TAG, DATE_CREATE) VALUES ";
111 $maxValuesLen = 2048;
112 $strSqlValues = "";
113
114 foreach(self::$arInsert as $tag)
115 {
116 $strSqlValues .= ",\n(".intval($userId).", '".$DB->ForSql($arChannel['CHANNEL_ID'])."', '".$DB->ForSql($tag)."', ".$DB->CurrentTimeFunction().")";
117 if(mb_strlen($strSqlValues) > $maxValuesLen)
118 {
119 $DB->Query($strSqlPrefix.mb_substr($strSqlValues, 2));
120 $strSqlValues = "";
121 }
122 }
123 if($strSqlValues <> '')
124 {
125 $DB->Query($strSqlPrefix.mb_substr($strSqlValues, 2));
126 }
127 }
128 }
129 else if (!empty(self::$arInsert))
130 {
131 foreach(self::$arInsert as $tag)
132 {
133 $DB->Query("INSERT INTO b_pull_watch (USER_ID, CHANNEL_ID, TAG, DATE_CREATE) VALUES (".intval($userId).", '".$DB->ForSql($arChannel['CHANNEL_ID'])."', '".$DB->ForSql($tag)."', ".$DB->CurrentTimeFunction().")");
134 }
135 }
136
137 self::$arInsert = Array();
138 self::$arUpdate = Array();
139
140 return true;
141 }
142
143 public static function Delete($userId, $tag = null)
144 {
145 global $DB, $CACHE_MANAGER;
146
147 $strSql = "DELETE FROM b_pull_watch WHERE USER_ID = ".intval($userId).(!is_null($tag)? " AND TAG = '".$DB->ForSQL($tag)."'": "");
148 $DB->Query($strSql);
149
150 $CACHE_MANAGER->Clean("b_pw_".$userId, "b_pull_watch");
151
152 if ($tag === null)
153 {
154 self::cleanCacheByUserId((int)$userId);
155 }
156 else
157 {
158 self::cleanCacheByTag((string)$tag);
159 }
160
161 return true;
162 }
163
164 public static function Extend($userId, $tags)
165 {
166 global $DB, $CACHE_MANAGER;
167
168 if (intval($userId) == 0)
169 {
170 return false;
171 }
172
173 if (is_array($tags))
174 {
175 $isMulti = true;
176 $searchTag = '';
177 if (empty($tags))
178 {
179 return false;
180 }
181 }
182 else
183 {
184 $isMulti = false;
185 $searchTag = trim($tags);
186 if ($searchTag == '')
187 {
188 return false;
189 }
190 else
191 {
192 $tags = Array($searchTag);
193 }
194 }
195
196 $result = Array();
197 foreach ($tags as $id => $tag)
198 {
199 $result[$tag] = false;
200 $tags[$id] = $DB->ForSQL($tag);
201 }
202
203 $updateIds = Array();
204 $strSql = "SELECT ID, TAG FROM b_pull_watch WHERE USER_ID = ".intval($userId)." AND TAG IN ('".implode("', '", $tags)."')";
205 $dbRes = $DB->Query($strSql);
206 while ($arRes = $dbRes->Fetch())
207 {
208 $updateIds[] = $arRes['ID'];
209 $result[$arRes['TAG']] = true;
210 }
211
212 if ($updateIds)
213 {
214 $DB->Query("UPDATE b_pull_watch SET DATE_CREATE = ".$DB->CurrentTimeFunction()." WHERE ID IN (".implode(', ', $updateIds).")");
215 $CACHE_MANAGER->Clean("b_pw_".$userId, "b_pull_watch");
216 }
217
218 return $isMulti? $result: $result[$searchTag];
219 }
220
229 public static function AddToStack($tag, $parameters, $channelType = \CPullChannel::TYPE_PRIVATE)
230 {
231 if (empty($tag))
232 {
233 return false;
234 }
235
236 if (!is_array($tag))
237 {
238 $tag = [$tag];
239 }
240
241 $userIds = array_unique(array_merge(...self::getUsersByTags($tag)));
242
243 if (isset($parameters['skip_users']) && !empty($parameters['skip_users']) && is_array($parameters['skip_users']))
244 {
245 $userIds = array_diff($userIds, $parameters['skip_users']);
246 }
247
248 if (!empty($userIds))
249 {
250 \Bitrix\Pull\Event::add($userIds, $parameters, $channelType);
251 }
252
253 return true;
254 }
255
256 protected static function getUsersByTags(array $tags): array
257 {
258 $userIds = $nonCachedTags = [];
259
260 foreach ($tags as $tag)
261 {
262 if (array_key_exists($tag, self::$tagCache))
263 {
264 if (is_array(self::$tagCache[$tag]))
265 {
266 $userIds[] = self::$tagCache[$tag];
267 }
268
269 continue;
270 }
271
272 $nonCachedTags[] = $tag;
273 }
274
275 if (!empty($nonCachedTags))
276 {
277 $userIds[] = self::getUsersByTagsInternal($nonCachedTags);
278 }
279
280 return $userIds;
281 }
282
283 public static function getUsersByTag(string $tag): array
284 {
285 self::getUsersByTags([$tag]);
286
287 return self::$tagCache[$tag] ?? [];
288 }
289
290 private static function getUsersByTagsInternal(array $tags): array
291 {
293 ->setSelect(['USER_ID', 'TAG'])
294 ->whereIn('TAG', $tags)
295 ->fetchAll()
296 ;
297
298 $usersByTag = $users = [];
299 foreach ($result as $data)
300 {
301 $userId = (int)$data['USER_ID'];
302 $usersByTag[$data['TAG']][$userId] = $userId;
303 $users[$userId] = $userId;
304 }
305
306 foreach ($tags as $tag)
307 {
308 self::$tagCache[$tag] = $usersByTag[$tag] ?? null;
309 }
310
311 return $users;
312 }
313
314 public static function GetUserList($tag)
315 {
316 $userIds = \CPullWatch::getUsersByTag($tag);
317
318 return array_map('strval', $userIds);
319 }
320
321 public static function cleanCache(): void
322 {
323 self::$tagCache = [];
324 }
325
326 private static function cleanCacheByTag(string $tag): void
327 {
328 unset(self::$tagCache[$tag]);
329 }
330
331 private static function cleanCacheByUserId(int $userId): void
332 {
333 foreach (self::$tagCache as $tag => $userIds)
334 {
335 if (is_array($userIds) && in_array($userId, $userIds, true))
336 {
337 unset(self::$tagCache[$tag]);
338 }
339 }
340 }
341}
$arResult
Определения generate_coupon.php:16
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static add($recipient, array $parameters, $channelType=\CPullChannel::TYPE_PRIVATE)
Определения event.php:22
Определения pull_watch.php:4
static DeferredSql($userId=false)
Определения pull_watch.php:65
static cleanCache()
Определения pull_watch.php:321
static Extend($userId, $tags)
Определения pull_watch.php:164
static GetUserList($tag)
Определения pull_watch.php:314
static AddToStack($tag, $parameters, $channelType=\CPullChannel::TYPE_PRIVATE)
Определения pull_watch.php:229
static Delete($userId, $tag=null)
Определения pull_watch.php:143
static getUsersByTags(array $tags)
Определения pull_watch.php:256
const bucket_size
Определения pull_watch.php:5
static Add($userId, $tag, $immediate=false)
Определения pull_watch.php:11
static getUsersByTag(string $tag)
Определения pull_watch.php:283
static Get(int $userId, $cache=true, $reOpen=false, $channelType=self::TYPE_PRIVATE)
Определения pull_channel.php:46
const TYPE_PRIVATE
Определения pull_channel.php:9
global $CACHE_MANAGER
Определения clear_component_cache.php:7
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
global $DB
Определения cron_frame.php:29
global $USER
Определения csv_new_run.php:40
$arRes
Определения options.php:104
$GLOBALS['_____370096793']
Определения update_client.php:1
$dbRes
Определения yandex_detail.php:168