Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
livefeedconvertservicemessage.php
1<?
3
5use \Bitrix\Main\Update\Stepper;
6use \Bitrix\Main\Localization\Loc;
7use \Bitrix\Forum\Integration;
8use \Bitrix\Main\Loader;
9use \Bitrix\Forum;
10use \Bitrix\Main;
11use \Bitrix\Socialnetwork;
13
14Loc::loadMessages(__FILE__);
15
17{
18 protected static $moduleId = "forum";
19 protected const MESSAGE_LIMIT = 500;
20
21 public function execute(array &$result)
22 {
23 if (!(
24 Loader::includeModule("forum")
25 && Loader::includeModule("socialnetwork")
26 ))
27 {
28 return self::finishExecution();
29 }
30
31 if (!array_key_exists("handlers", $result) || !is_array($result["handlers"]))
32 {
33 $result["handlers"] = array_merge(self::getSocialnetworkHandlersPostText(), ["innerForum"]);
34 $result["steps"] = count($result["handlers"]);
35 $result["count"] = 0;
36 }
37
38 if ($postMessage = reset($result["handlers"]))
39 {
40 $status = self::FINISH_EXECUTION;
41 Main\Config\Option::set("forum", "LivefeedConvertServiceMessageStepper", "inProgress");
42 if ($postMessage === "innerForum")
43 {
44 $status = $this->replaceServiceField($result["lastId"]);
45 }
46 else if ($handler = (new Socialnetwork\CommentAux\HandlerManager())->getHandlerByPostText($postMessage))
47 {
48 $status = $this->convert($handler, $result["lastId"]);
49 }
50 if ($status === self::FINISH_EXECUTION)
51 {
52 array_shift($result["handlers"]);
53 unset($result["lastId"]);
54 }
55 else
56 {
57 $result["lastId"] = $status;
58 }
59 $result["count"] = $result["steps"] - count($result["handlers"]);
60 }
61
62 return count($result["handlers"]) > 0 ? self::CONTINUE_EXECUTION : self::finishExecution();
63 }
64
65 protected static function finishExecution()
66 {
67 Main\Config\Option::delete("forum", ["name" => "LivefeedConvertServiceMessageStepper"]);
69 }
70
71 public function convert(Socialnetwork\CommentAux\Base $handler, $lastId)
72 {
73 $comments = $this->getCommentsCollection(["POST_MESSAGE" => $handler::getPostText()], $lastId);
74 $rowsCount = $comments->count();
75 $lastId = 0;
76 if ($rowsCount > 0)
77 {
78 $dbRes = Socialnetwork\LogCommentTable::getList([
79 "select" => ["ID", "EVENT_ID", "SOURCE_ID", "SHARE_DEST"],
80 "filter" => [
81 "SOURCE_ID" => $comments->getIdList(),
82 "%RATING_TYPE_ID" => "FORUM",
83 ]
84 ]);
85 $socnetComments = [];
86 while ($res = $dbRes->fetch())
87 {
88 $socnetComments[$res["SOURCE_ID"]] = $res;
89 }
90 $comments->rewind();
91 while ($comment = $comments->current())
92 {
93 $socnetInfo = array_key_exists($comment["ID"], $socnetComments) ? $socnetComments[$comment["ID"]] : ["SHARE_DEST" => ""];
94 if ($handler instanceof Socialnetwork\CommentAux\TaskInfo && !empty($socnetInfo["SHARE_DEST"]))
95 {
96 $postMessage = self::decodeSocnetText($socnetInfo["SHARE_DEST"], 'serialized');
97 if (!is_array($postMessage))
98 {
99 $postMessage = self::decodeSocnetText($socnetInfo["SHARE_DEST"], 'bar');
100 if (empty($postMessage))
101 {
102 $postMessage = false;
103 }
104 }
105
106 if (is_array($postMessage))
107 {
108 $comment->setServiceType(Forum\Comments\Service\Manager::TYPE_TASK_INFO);
109 $serviceData = Json::encode(is_array($postMessage) ? $postMessage : []);
110 $comment->setServiceData($serviceData);
111 $comment->setPostMessage(Forum\Comments\Service\Manager::find([
112 "SERVICE_TYPE" => Forum\Comments\Service\Manager::TYPE_TASK_INFO
113 ])->getText($serviceData));
114 $comment->setPostMessageHtml($socnetInfo["SHARE_DEST"]);
115 $comment->setPostMessageFilter($handler::getPostText());
116 }
117 }
118 elseif ($handler instanceof Socialnetwork\CommentAux\CreateTask)
119 {
120 $postMessage = self::decodeSocnetText($socnetInfo["SHARE_DEST"], 'bar');
121 if (is_array($postMessage))
122 {
123 $comment->setServiceType(Forum\Comments\Service\Manager::TYPE_TASK_CREATED);
124 $serviceData = Json::encode(is_array($postMessage) ? $postMessage : []);
125 $comment->setServiceData($serviceData);
126 $comment->setPostMessage(Forum\Comments\Service\Manager::find([
127 "SERVICE_TYPE" => Forum\Comments\Service\Manager::TYPE_TASK_CREATED
128 ])->getText($serviceData));
129 $comment->setPostMessageHtml($socnetInfo["SHARE_DEST"]);
130 $comment->setPostMessageFilter($handler::getPostText());
131 }
132 }
133 elseif ($handler instanceof Socialnetwork\CommentAux\FileVersion && !empty($socnetInfo["SHARE_DEST"]))
134 {
135 if (false && !empty($socnetInfo["SHARE_DEST"]))
136 {
137 $comment->setPostMessageFilter($handler::getPostText());
138 }
139 }
140
141 if ($comment->state !== State::RAW)
142 {
143 $comment->save();
144 }
145 $lastId = $comment->getId();
146 $comments->next();
147 }
148 }
149 return $rowsCount < static::MESSAGE_LIMIT ? self::FINISH_EXECUTION : $lastId;
150 }
151
152 protected function getCommentsCollection(array $filter, $lastId)
153 {
154 return Forum\MessageTable::getList([
155 "select" => ["ID", "POST_MESSAGE", "SERVICE_TYPE"],
156 "filter" => $filter + ($lastId > 0 ? [
157 "<ID" => $lastId
158 ] : []),
159 "limit" => self::MESSAGE_LIMIT,
160 "order" => ["ID" => "DESC"]
161 ])->fetchCollection();
162 }
163 protected static function getSocialnetworkHandlers()
164 {
165 static $result = null;
166 if ($result === null)
167 {
168 $result = [];
169 if (Loader::includeModule("socialnetwork"))
170 {
171 $result = [
173/* new Socialnetwork\CommentAux\FileVersion,
174 new Socialnetwork\CommentAux\Share,*/
176 ];
177 }
178 }
179 return $result;
180 }
181
182 public static function getSocialnetworkHandlersPostText()
183 {
184 $result = [];
185 /* @var $handler Socialnetwork\CommentAux\Base */
186 foreach (self::getSocialnetworkHandlers() as $handler)
187 {
188 $result[] = $handler->getPostText();
189 }
190 return $result;
191 }
192
193 protected static function decodeSocnetText($text = '', $type = 'serialized')
194 {
195 $result = [];
196 if ($type === 'serialized')
197 {
198 $result = @unserialize($text, ["allowed_classes" => false]);
199 }
200 elseif ($type === 'bar')
201 {
202 $paramsList = explode('|', $text);
203 if (!empty($paramsList))
204 {
205 foreach($paramsList as $pair)
206 {
207 list($key, $value) = explode('=', $pair);
208 if (isset($key) && isset($value))
209 {
210 $result[$key] = $value;
211 }
212 }
213 }
214 }
215
216 return $result;
217 }
218
219 public function replaceServiceField($lastId)
220 {
221 $comments = $this->getCommentsCollection([
222 "!SERVICE_TYPE" => null,
223 "SERVICE_DATA" => null
224 ], $lastId);
225 $rowsCount = $comments->count();
226
227 $lastId = 0;
228 if ($rowsCount > 0)
229 {
230 $currentLang = Main\Localization\Loc::getCurrentLang();
231 $defaultLanguage= Main\Localization\Loc::getDefaultLang(LANGUAGE_ID);
232 if ($currentLang !== $defaultLanguage)
233 {
234 Main\Localization\Loc::setCurrentLang($defaultLanguage);
235 }
236 while ($comment = $comments->current())
237 {
238 $serviceData = $comment->getPostMessage();
239 if ($handler = Forum\Comments\Service\Manager::find([
240 "SERVICE_TYPE" => $comment->getServiceType()
241 ]))
242 {
243 $postMessage = $handler->getText($serviceData);
244 if ($postMessage !== '')
245 {
246 $comment->setPostMessage($postMessage);
247 $comment->setServiceData(is_null($serviceData) ? Json::encode([]) : $serviceData);
248 $comment->save();
249 }
250 }
251 $lastId = $comment->getId();
252 $comments->next();
253 }
254 if ($currentLang !== $defaultLanguage)
255 {
256 Main\Localization\Loc::setCurrentLang($currentLang);
257 }
258 }
259 return $rowsCount < static::MESSAGE_LIMIT ? self::FINISH_EXECUTION : $lastId;
260 }
261}
262?>
convert(Socialnetwork\CommentAux\Base $handler, $lastId)
static loadMessages($file)
Definition loc.php:64