Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
documentfilescleanup.php
1<?php
2
4
6
8{
9 public static function execAgent(): string
10 {
11 $connection = \Bitrix\Main\Application::getConnection();
12 $helper = $connection->getSqlHelper();
13 $queryResult = $connection->query('
14 select doc_files.ID, FILE_ID from b_catalog_store_document_file doc_files
15 left outer join b_catalog_store_docs docs
16 on docs.ID = doc_files.DOCUMENT_ID
17 where docs.ID is null
18 limit 50
19 ');
20 $documentFiles = $queryResult->fetchAll();
21
22 if (empty($documentFiles))
23 {
24 return '';
25 }
26
27 $filesToRemove = array_column($documentFiles, 'FILE_ID');
28 $entriesToRemove = array_column($documentFiles, 'ID');
29
30 foreach ($filesToRemove as $fileId)
31 {
32 \CFile::Delete($fileId);
33 }
34
35 $entriesForQuery = implode(',', $entriesToRemove);
36
37 $connection->queryExecute(
38 'delete from ' . $helper->quote(StoreDocumentFileTable::getTableName())
39 . ' where ' . $helper->quote('ID') . ' in (' . $entriesForQuery . ')'
40 );
41
42 $isCleanupOver = !(bool)$connection->query('
43 select doc_files.ID, FILE_ID from b_catalog_store_document_file doc_files
44 left outer join b_catalog_store_docs docs
45 on docs.ID = doc_files.DOCUMENT_ID
46 where docs.ID is null
47 limit 1
48 ')->fetch();
49
50 if ($isCleanupOver)
51 {
52 return '';
53 }
54
55 return '\Bitrix\Catalog\Update\DocumentFilesCleanup::execAgent();';
56 }
57}