Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
messagecleaner.php
1<?php
2
4
5use \Bitrix\Main;
6use \Bitrix\Forum;
7
9{
10 protected static int $limit = 10;
11 protected static $moduleId = "forum";
12
13 public static function getTitle(): string
14 {
15 return 'Message cleaner';
16 }
17
18 public function execute(array &$option)
19 {
20 $option["steps"] = 1;
21 $option["count"] = 1;
22 if (self::do() > 0)
23 {
25 }
27 }
28
29 private static function do()
30 {
32 $dbRes = Main\Application::getConnection()->query(<<<SQL
33SELECT ID, FORUM_ID, TOPIC_ID, MESSAGE_ID, NEW_TOPIC, APPROVED, PARAM1, PARAM2, AUTHOR_ID
34FROM b_forum_service_deleted_message
35ORDER BY ID ASC
36LIMIT {$limit}
37SQL
38 );
39 $votes = [];
40 global $USER_FIELD_MANAGER;
41 $last = null;
42 while ($message = $dbRes->fetch())
43 {
44 if ($message["PARAM1"] == 'VT')
45 {
46 $votes[] = $message["PARAM2"];
47 }
48
49 $selectSql = "SELECT * FROM b_forum_file where MESSAGE_ID=" . intval($message['MESSAGE_ID']) . " ORDER BY ID ASC";
50
51 $dbFileRes = Main\Application::getConnection()->query($selectSql);
52
53 if ($dbFileRes && ($file = $dbFileRes->fetch()))
54 {
55 do
56 {
57 \CFile::Delete($file["FILE_ID"]);
58 }
59 while ($file = $dbFileRes->fetch());
60 $deleteSql = "DELETE FROM b_forum_file where MESSAGE_ID=" . intval($message['MESSAGE_ID']);
61 Main\Application::getConnection()->queryExecute($deleteSql);
62 }
63
64 $USER_FIELD_MANAGER->Delete("FORUM_MESSAGE", $message["MESSAGE_ID"]);
65 $limit--;
66 $last = $message;
67 }
68
69 if (!empty($votes) && IsModuleInstalled("vote") && \CModule::IncludeModule("vote"))
70 {
71 array_map(function($voteId) {
72 \CVote::Delete($voteId);
73 }, $votes);
74 }
75
76 if ($last)
77 {
78 Main\Application::getConnection()->queryExecute(<<<SQL
79DELETE FROM b_forum_service_deleted_message WHERE ID <= {$last['ID']}
80SQL
81 );
82 }
83 return $limit;
84 }
85
86 public static function runForTopic(int $topicId)
87 {
88 Main\Application::getConnection()->queryExecute(<<<SQL
89INSERT IGNORE INTO b_forum_service_deleted_message
90 (FORUM_ID, TOPIC_ID, MESSAGE_ID, NEW_TOPIC, APPROVED, PARAM1, PARAM2, AUTHOR_ID)
91SELECT FORUM_ID, TOPIC_ID, ID, NEW_TOPIC, APPROVED, PARAM1, PARAM2, AUTHOR_ID
92FROM b_forum_message
93WHERE TOPIC_ID = {$topicId}
94SQL
95 );
96 self::bind(0);
97 }
98}