Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
deletedmessagefiles.php
1<?php
2
3namespace Bitrix\Im\Update;
4
5use Bitrix\Disk\File;
6use Bitrix\Disk\Internals\FileTable;
7use Bitrix\Disk\SystemUser;
15
16final class DeletedMessageFiles extends Stepper
17{
18 protected static $moduleId = 'im';
19 public const LIMIT = 50;
20 public const DISK_MODULE_ID = 'disk';
21
22 function execute(array &$option)
23 {
24 if (!Loader::includeModule(self::$moduleId) || !Loader::includeModule(self::DISK_MODULE_ID))
25 {
27 }
28
29 $fileIds = $this->getFileIdsFromDeletedMessage($option);
30
31 if (empty($fileIds))
32 {
34 }
35
36 $diskFiles = File::getModelList([
37 'filter' => Query::filter()->whereIn('ID', $fileIds)->where('TYPE', FileTable::TYPE)
38 ]);
39
40 foreach ($diskFiles as $diskFile)
41 {
42 $diskFile->delete(SystemUser::SYSTEM_USER_ID);
43 }
44
46 }
47
48 private function getFileIdsFromDeletedMessage(array &$option): array
49 {
50 $result = MessageParamTable::query()
51 ->setSelect(['LINK_MESSAGE_ID' => 'LINK_FILE.MESSAGE_ID'])
52 ->where('MESSAGE_ID', '>', $option['lastId'] ?? 0)
53 ->setOrder(['MESSAGE_ID'])
54 ->where('PARAM_NAME', 'IS_DELETED')
55 ->registerRuntimeField(
56 'LINK_FILE',
57 new Reference(
58 'LINK_FILE',
59 LinkFileTable::class,
60 Join::on('this.MESSAGE_ID', 'ref.MESSAGE_ID'),
61 ['join_type' => Join::TYPE_INNER]
62 )
63 )
64 ->setLimit(self::LIMIT)
65 ->fetchAll()
66 ;
67
68 if (empty($result))
69 {
70 return [];
71 }
72
73 $messageIds = array_unique(array_map('intval', array_column($result, 'LINK_MESSAGE_ID')));
74 $option['lastId'] = max($messageIds);
75
76 return $this->getFileIdsByMessageIds($messageIds);
77 }
78
79 private function getFileIdsByMessageIds(array $messageIds): array
80 {
81 if (empty($messageIds))
82 {
83 return [];
84 }
85
86 return LinkFileTable::query()
87 ->setSelect(['DISK_FILE_ID'])
88 ->whereIn('MESSAGE_ID', $messageIds)
89 ->fetchCollection()
90 ->getDiskFileIdList()
91 ;
92 }
93}