29 if (!Loader::includeModule(self::$moduleId))
34 $numOfIterations = (int)Option::get(self::$moduleId, self::OPTION_NAME_ITERATION_COUNT, self::ITERATION_COUNT_DEFAULT);
37 for ($i = 0; $i < $numOfIterations; ++$i)
39 $result = $this->makeMigrationIteration($option);
41 if ($result === self::FINISH_EXECUTION)
50 private function makeMigrationIteration(array &$option): bool
52 $isFinished = Option::get(self::$moduleId, self::OPTION_NAME,
'');
54 if ($isFinished ===
'')
56 Option::set(self::$moduleId, self::OPTION_NAME,
'N');
59 if ($isFinished ===
'Y')
64 $lastId = $option[
'lastId'] ?? 0;
65 $params = $this->getParams($lastId);
67 if ($params->count() === 0)
69 Option::set(self::$moduleId, self::OPTION_NAME,
'Y');
70 if (\
Bitrix\Main\Loader::includeModule(
'pull'))
73 Event::SHARED_CHANNEL,
76 'command' =>
'linkFileMigrationFinished',
79 \CPullChannel::TYPE_SHARED
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)
93 $fileEntity = $fileEntities->getById((
int)$param->getParamValue());
94 if ($fileEntity ===
null)
98 $message = $param->getMessage();
99 if ($message ===
null)
103 $file =
new FileItem();
105 ->setChatId($message->getChatId())
106 ->setAuthorId($message->getAuthorId())
107 ->setMessageId($param->getMessageId())
108 ->setEntity($fileEntity)
109 ->setDateCreate($message->getDateCreate())
111 $fileCollection->add($file);
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;
124 private function getParams(
int $lastId): EO_MessageParam_Collection
126 $params = MessageParamTable::query()
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))
135 if ($params->count() === 0)
140 $params->fill([
'MESSAGE_ID',
'PARAM_VALUE']);
142 $messageIds = $params->getMessageIdList();
144 if (empty($messageIds))
149 $messages = MessageTable::query()
150 ->setSelect([
'ID',
'AUTHOR_ID',
'DATE_CREATE',
'CHAT_ID'])
151 ->whereIn(
'ID', $messageIds)
155 foreach ($params as $param)
157 $message = $messages->getByPrimary($param->getMessageId());
158 if ($message !==
null)
160 $param->setMessage($message);
167 private function changeMigrationFlag(
bool $flag): void
169 $this->changeMigrationFlagForClass(FileCollection::class, $flag);
170 $this->changeMigrationFlagForClass(FileItem::class, $flag);
173 private function changeMigrationFlagForClass(
string $className,
bool $flag): void
175 $migrationFlagName =
'isMigrationFinished';
176 $migrationFlag = new \ReflectionProperty($className, $migrationFlagName);
177 $migrationFlag->setAccessible(
true);
178 $migrationFlag->setValue($flag);