Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
linkfilemigration.php
1<?php
2
3namespace Bitrix\Im\Update;
4
6use Bitrix\Im\Model\EO_MessageParam_Collection;
17
18final class LinkFileMigration extends Stepper
19{
20 protected static $moduleId = 'im';
21 public const OPTION_NAME = 'im_link_file_migration';
22 public const OPTION_NAME_LIMIT = 'im_link_file_migration_limit';
23 public const OPTION_NAME_ITERATION_COUNT = 'im_link_file_migration_iteration';
24 public const LIMIT_DEFAULT = 500;
25 public const ITERATION_COUNT_DEFAULT = 4;
26
27 function execute(array &$option)
28 {
29 if (!Loader::includeModule(self::$moduleId))
30 {
32 }
33
34 $numOfIterations = (int)Option::get(self::$moduleId, self::OPTION_NAME_ITERATION_COUNT, self::ITERATION_COUNT_DEFAULT);
35
37 for ($i = 0; $i < $numOfIterations; ++$i)
38 {
39 $result = $this->makeMigrationIteration($option);
40
41 if ($result === self::FINISH_EXECUTION)
42 {
43 return $result;
44 }
45 }
46
47 return $result;
48 }
49
50 private function makeMigrationIteration(array &$option): bool
51 {
52 $isFinished = Option::get(self::$moduleId, self::OPTION_NAME, '');
53
54 if ($isFinished === '')
55 {
56 Option::set(self::$moduleId, self::OPTION_NAME, 'N');
57 }
58
59 if ($isFinished === 'Y')
60 {
62 }
63
64 $lastId = $option['lastId'] ?? 0;
65 $params = $this->getParams($lastId);
66
67 if ($params->count() === 0)
68 {
69 Option::set(self::$moduleId, self::OPTION_NAME, 'Y');
70 if (\Bitrix\Main\Loader::includeModule('pull'))
71 {
72 Event::add(
73 Event::SHARED_CHANNEL,
74 [
75 'module_id' => 'im',
76 'command' => 'linkFileMigrationFinished',
77 'extra' => Common::getPullExtra(),
78 ],
79 \CPullChannel::TYPE_SHARED
80 );
81 }
82
84 }
85
86 $ids = $params->getParamValueList();
87 $lastId = max($params->getIdList());
88 $this->changeMigrationFlag(true);
89 $fileCollection = new FileCollection();
90 $fileEntities = \Bitrix\Im\V2\Entity\File\FileCollection::initByDiskFilesIds($ids);
91 foreach ($params as $param)
92 {
93 $fileEntity = $fileEntities->getById((int)$param->getParamValue());
94 if ($fileEntity === null)
95 {
96 continue;
97 }
98 $message = $param->getMessage();
99 if ($message === null)
100 {
101 continue;
102 }
103 $file = new FileItem();
104 $file
105 ->setChatId($message->getChatId())
106 ->setAuthorId($message->getAuthorId())
107 ->setMessageId($param->getMessageId())
108 ->setEntity($fileEntity)
109 ->setDateCreate($message->getDateCreate())
110 ;
111 $fileCollection->add($file);
112 }
113 $fileCollection->save(true);
114 $this->changeMigrationFlag(false);
115 $option['lastId'] = $lastId;
116 $steps = LinkFileTable::getCount();
117 $count = MessageParamTable::getCount(Query::filter()->where('PARAM_NAME', 'FILE_ID'));
118 $option['steps'] = $steps;
119 $option['count'] = $count;
120
122 }
123
124 private function getParams(int $lastId): EO_MessageParam_Collection
125 {
126 $params = MessageParamTable::query()
127 ->setSelect(['ID'])
128 ->where('PARAM_NAME', 'FILE_ID')
129 ->where('ID', '>', $lastId)
130 ->setOrder(['ID' => 'ASC'])
131 ->setLimit((int)Option::get(self::$moduleId, self::OPTION_NAME_LIMIT, self::LIMIT_DEFAULT))
132 ->fetchCollection()
133 ;
134
135 if ($params->count() === 0)
136 {
137 return $params;
138 }
139
140 $params->fill(['MESSAGE_ID', 'PARAM_VALUE']);
141
142 $messageIds = $params->getMessageIdList();
143
144 if (empty($messageIds))
145 {
146 return $params;
147 }
148
149 $messages = MessageTable::query()
150 ->setSelect(['ID', 'AUTHOR_ID', 'DATE_CREATE', 'CHAT_ID'])
151 ->whereIn('ID', $messageIds)
152 ->fetchCollection()
153 ;
154
155 foreach ($params as $param)
156 {
157 $message = $messages->getByPrimary($param->getMessageId());
158 if ($message !== null)
159 {
160 $param->setMessage($message);
161 }
162 }
163
164 return $params;
165 }
166
167 private function changeMigrationFlag(bool $flag): void
168 {
169 $this->changeMigrationFlagForClass(FileCollection::class, $flag);
170 $this->changeMigrationFlagForClass(FileItem::class, $flag);
171 }
172
173 private function changeMigrationFlagForClass(string $className, bool $flag): void
174 {
175 $migrationFlagName = 'isMigrationFinished';
176 $migrationFlag = new \ReflectionProperty($className, $migrationFlagName);
177 $migrationFlag->setAccessible(true);
178 $migrationFlag->setValue($flag);
179 }
180}
static getPullExtra()
Definition common.php:128