1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
linkfilemigration.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\Update;
4
5use Bitrix\Im\Common;
6use Bitrix\Im\Model\EO_MessageParam_Collection;
7use Bitrix\Im\Model\LinkFileTable;
8use Bitrix\Im\Model\MessageParamTable;
9use Bitrix\Im\Model\MessageTable;
10use Bitrix\Im\V2\Link\File\FileCollection;
11use Bitrix\Im\V2\Link\File\FileItem;
12use Bitrix\Main\Config\Option;
13use Bitrix\Main\Loader;
14use Bitrix\Main\ORM\Query\Query;
15use Bitrix\Main\Update\Stepper;
16use Bitrix\Pull\Event;
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
28 {
29 if (!Loader::includeModule(self::$moduleId))
30 {
31 return self::CONTINUE_EXECUTION;
32 }
33
34 $numOfIterations = (int)Option::get(self::$moduleId, self::OPTION_NAME_ITERATION_COUNT, self::ITERATION_COUNT_DEFAULT);
35
36 $result = self::CONTINUE_EXECUTION;
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 {
61 return self::FINISH_EXECUTION;
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 ],
80 );
81 }
82
83 return self::FINISH_EXECUTION;
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
121 return self::CONTINUE_EXECUTION;
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}
$count
Определения admin_tab.php:4
static getPullExtra()
Определения common.php:127
const TYPE_SHARED
Определения pull_channel.php:10
if(!\Bitrix\Main\Loader::includeModule('clouds')) $lastId
Определения sync.php:68
</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
$message
Определения payment.php:8
$i
Определения factura.php:643
$messages
Определения template.php:8
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$option
Определения options.php:1711