Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
linkurlmigration.php
1<?php
2
3namespace Bitrix\Im\Update;
4
6use Bitrix\Im\Model\EO_LinkUrl;
7use Bitrix\Im\Model\EO_LinkUrl_Collection;
8use Bitrix\Im\Model\EO_MessageParam_Collection;
19
20final class LinkUrlMigration extends Stepper
21{
22 protected static $moduleId = 'im';
23 public const OPTION_NAME = 'im_link_url_migration';
24 public const OPTION_NAME_LIMIT = 'im_link_url_migration_limit';
25 public const OPTION_NAME_ITERATION_COUNT = 'im_link_url_migration_iteration';
26 public const LIMIT_DEFAULT = 500;
27 public const ITERATION_COUNT_DEFAULT = 4;
28
29 function execute(array &$option)
30 {
31 if (!Loader::includeModule(self::$moduleId))
32 {
34 }
35
36 $numOfIterations = (int)Option::get(self::$moduleId, self::OPTION_NAME_ITERATION_COUNT, self::ITERATION_COUNT_DEFAULT);
37
39 for ($i = 0; $i < $numOfIterations; ++$i)
40 {
41 $result = $this->makeMigrationIteration($option);
42
43 if ($result === self::FINISH_EXECUTION)
44 {
45 return $result;
46 }
47 }
48
49 return $result;
50 }
51
52 private function makeMigrationIteration(array &$option): bool
53 {
54 $isFinished = Option::get(self::$moduleId, self::OPTION_NAME, '');
55
56 if ($isFinished === '')
57 {
58 Option::set(self::$moduleId, self::OPTION_NAME, 'N');
59 }
60
61 if ($isFinished === 'Y')
62 {
64 }
65
66 $lastId = $option['lastId'] ?? 0;
67 $params = $this->getParams($lastId);
68
69 if ($params->count() === 0)
70 {
71 Option::set(self::$moduleId, self::OPTION_NAME, 'Y');
72 if (\Bitrix\Main\Loader::includeModule('pull'))
73 {
74 Event::add(
75 Event::SHARED_CHANNEL,
76 [
77 'module_id' => 'im',
78 'command' => 'linkUrlMigrationFinished',
79 'extra' => Common::getPullExtra(),
80 ],
81 \CPullChannel::TYPE_SHARED
82 );
83 }
84
86 }
87
88 $ids = $params->getParamValueList();
89 $lastId = max($params->getIdList());
90
91 $urlsPreview = \Bitrix\Main\UrlPreview\UrlPreview::getMetadataByIds($ids);
92 $urlCollection = new EO_LinkUrl_Collection();
93
94 foreach ($params as $param)
95 {
96 $urlPreview = $urlsPreview[$param->getParamValue()];
97 $uri = new Uri($urlPreview['URL']);
98 if ($uri->getHost() === '')
99 {
100 continue;
101 }
102 $message = $param->getMessage();
103 if ($message === null)
104 {
105 continue;
106 }
107 $url = new EO_LinkUrl();
108 $url
109 ->setChatId($message->getChatId())
110 ->setAuthorId($message->getAuthorId())
111 ->setMessageId($param->getMessageId())
112 ->setDateCreate($message->getDateCreate())
113 ->setPreviewUrlId((int)$urlPreview['ID'])
114 ->setUrl($urlPreview['URL'])
115 ;
116 $urlCollection[] = $url;
117 }
118 $urlCollection->save(true);
119 LinkUrlIndexTable::index((int)Option::get(self::$moduleId, self::OPTION_NAME_LIMIT, self::LIMIT_DEFAULT));
120 $option['lastId'] = $lastId;
121 $steps = LinkUrlTable::getCount();
122 $count = MessageParamTable::getCount(Query::filter()->where('PARAM_NAME', 'URL_ID'));
123 $option['steps'] = $steps;
124 $option['count'] = $count;
125
127 }
128
129 private function getParams(int $lastId): EO_MessageParam_Collection
130 {
131 $params = MessageParamTable::query()
132 ->setSelect(['ID'])
133 ->where('PARAM_NAME', 'URL_ID')
134 ->where('ID', '>', $lastId)
135 ->setOrder(['ID' => 'ASC'])
136 ->setLimit((int)Option::get(self::$moduleId, self::OPTION_NAME_LIMIT, self::LIMIT_DEFAULT))
137 ->fetchCollection()
138 ;
139
140 if ($params->count() === 0)
141 {
142 return $params;
143 }
144
145 $params->fill(['MESSAGE_ID', 'PARAM_VALUE']);
146
147 $messageIds = $params->getMessageIdList();
148
149 if (empty($messageIds))
150 {
151 return $params;
152 }
153
154 $messages = MessageTable::query()
155 ->setSelect(['ID', 'AUTHOR_ID', 'DATE_CREATE', 'CHAT_ID'])
156 ->whereIn('ID', $messageIds)
157 ->fetchCollection()
158 ;
159
160 foreach ($params as $param)
161 {
162 $message = $messages->getByPrimary($param->getMessageId());
163 if ($message !== null)
164 {
165 $param->setMessage($message);
166 }
167 }
168
169 return $params;
170 }
171}
static getPullExtra()
Definition common.php:128
static getCount($filter=array(), array $cache=array())