Conditions | 52 |
Paths | > 20000 |
Total Lines | 272 |
Code Lines | 136 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
85 | public function execute() |
||
86 | { |
||
87 | global $smcFunc, $sourcedir, $scripturl, $language, $modSettings, $user_info, $txt; |
||
88 | |||
89 | require_once($sourcedir . '/Subs-Post.php'); |
||
90 | require_once($sourcedir . '/Mentions.php'); |
||
91 | require_once($sourcedir . '/Subs-Notify.php'); |
||
92 | require_once($sourcedir . '/Subs.php'); |
||
93 | require_once($sourcedir . '/ScheduledTasks.php'); |
||
94 | loadEssentialThemeData(); |
||
95 | |||
96 | $msgOptions = &$this->_details['msgOptions']; |
||
97 | $topicOptions = &$this->_details['topicOptions']; |
||
98 | $posterOptions = &$this->_details['posterOptions']; |
||
99 | $type = &$this->_details['type']; |
||
100 | |||
101 | // Board id is required; if missing, log an error and return |
||
102 | if (!isset($topicOptions['board'])) |
||
103 | { |
||
104 | require_once($sourcedir . '/Errors.php'); |
||
105 | loadLanguage('Errors'); |
||
106 | log_error($txt['missing_board_id'], 'general', __FILE__, __LINE__); |
||
107 | return true; |
||
108 | } |
||
109 | |||
110 | // poster_time not always supplied, but used throughout |
||
111 | if (empty($msgOptions['poster_time'])) |
||
112 | $msgOptions['poster_time'] = 0; |
||
113 | |||
114 | $this->mention_mail_time = $msgOptions['poster_time'] + self::MENTION_DELAY * 60; |
||
115 | |||
116 | // We need some more info about the quoted and mentioned members. |
||
117 | if (!empty($msgOptions['quoted_members'])) |
||
118 | $this->members['quoted'] = Mentions::getMentionsByContent('quote', $msgOptions['id'], array_keys($msgOptions['quoted_members'])); |
||
119 | if (!empty($msgOptions['mentioned_members'])) |
||
120 | $this->members['mentioned'] = Mentions::getMentionsByContent('msg', $msgOptions['id'], array_keys($msgOptions['mentioned_members'])); |
||
121 | |||
122 | $group_permissions = array( |
||
123 | 'allowed' => array(), |
||
124 | 'denied' => array(), |
||
125 | ); |
||
126 | $request = $smcFunc['db_query']('', ' |
||
127 | SELECT id_group, deny |
||
128 | FROM {db_prefix}board_permissions_view |
||
129 | WHERE id_board = {int:current_board}', |
||
130 | array( |
||
131 | 'current_board' => $topicOptions['board'], |
||
132 | ) |
||
133 | ); |
||
134 | while (list ($id_group, $deny) = $smcFunc['db_fetch_row']($request)) |
||
135 | $group_permissions[$deny === '0' ? 'allowed' : 'denied'][] = $id_group; |
||
136 | $smcFunc['db_free_result']($request); |
||
137 | |||
138 | // Find the people interested in receiving notifications for this topic |
||
139 | $request = $smcFunc['db_query']('', ' |
||
140 | SELECT |
||
141 | ln.id_member, ln.id_board, ln.id_topic, ln.sent, |
||
142 | mem.email_address, mem.lngfile, mem.pm_ignore_list, |
||
143 | mem.id_group, mem.id_post_group, mem.additional_groups, |
||
144 | mem.time_format, mem.time_offset, mem.timezone, |
||
145 | t.id_member_started, t.id_member_updated |
||
146 | FROM {db_prefix}log_notify AS ln |
||
147 | INNER JOIN {db_prefix}members AS mem ON (ln.id_member = mem.id_member) |
||
148 | LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = ln.id_topic) |
||
149 | WHERE ' . ($type == 'topic' ? 'ln.id_board = {int:board}' : 'ln.id_topic = {int:topic}') . ' |
||
150 | AND ln.id_member != {int:member}', |
||
151 | array( |
||
152 | 'member' => $posterOptions['id'], |
||
153 | 'topic' => $topicOptions['id'], |
||
154 | 'board' => $topicOptions['board'], |
||
155 | ) |
||
156 | ); |
||
157 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
158 | { |
||
159 | // Skip members who aren't allowed to see this board |
||
160 | $groups = array_merge(array($row['id_group'], $row['id_post_group']), (empty($row['additional_groups']) ? array() : explode(',', $row['additional_groups']))); |
||
161 | |||
162 | $is_denied = array_intersect($group_permissions['denied'], $groups) != array(); |
||
163 | if (!in_array(1, $groups) && ($is_denied || array_intersect($groups, $group_permissions['allowed']) == array())) |
||
164 | continue; |
||
165 | else |
||
166 | { |
||
167 | $row['groups'] = $groups; |
||
168 | unset($row['id_group'], $row['id_post_group'], $row['additional_groups']); |
||
169 | } |
||
170 | |||
171 | // If this user subscribes both to the topic and the board there will be two records returned. |
||
172 | // Copy board/topic data to the new record or it will be lost. |
||
173 | if (!empty($this->members['watching'][$row['id_member']])) { |
||
174 | if ($this->members['watching'][$row['id_member']]['id_board'] > 0) { |
||
175 | $row['id_board'] = $this->members['watching'][$row['id_member']]['id_board']; |
||
176 | } |
||
177 | if ($this->members['watching'][$row['id_member']]['id_topic'] > 0) { |
||
178 | $row['id_topic'] = $this->members['watching'][$row['id_member']]['id_topic']; |
||
179 | } |
||
180 | } |
||
181 | |||
182 | $this->members['watching'][$row['id_member']] = $row; |
||
183 | } |
||
184 | $smcFunc['db_free_result']($request); |
||
185 | |||
186 | // Filter out mentioned and quoted members who can't see this board. |
||
187 | if (!empty($this->members['mentioned']) || !empty($this->members['quoted'])) |
||
188 | { |
||
189 | foreach (array('mentioned', 'quoted') as $member_type) |
||
190 | { |
||
191 | foreach ($this->members[$member_type] as $member_id => $member_data) |
||
192 | { |
||
193 | // The member receiving the alert has ignored the member mentioning them. |
||
194 | if (!empty($member_data['mentioned_by']['ignored'])) { |
||
195 | unset($this->members[$member_type][$member_id], $msgOptions[$member_type . '_members'][$member_id]); |
||
196 | } |
||
197 | |||
198 | $is_denied = array_intersect($group_permissions['denied'], $member_data['groups']) != array(); |
||
199 | if (!in_array(1, $member_data['groups']) && ($is_denied || array_intersect($member_data['groups'], $group_permissions['allowed']) == array())) |
||
200 | unset($this->members[$member_type][$member_id], $msgOptions[$member_type . '_members'][$member_id]); |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | |||
205 | $unnotified = array_filter($this->members['watching'], function ($member) { |
||
206 | return empty($member['sent']); |
||
207 | }); |
||
208 | |||
209 | |||
210 | // Modified post, or dealing with delayed mention and quote notifications. |
||
211 | if ($type == 'edit' || !empty($this->_details['respawns'])) |
||
212 | { |
||
213 | // Notifications about modified posts only go to members who were mentioned or quoted |
||
214 | $this->members['watching'] = $type == 'edit' ? array(): $unnotified; |
||
215 | |||
216 | // If this post has no quotes or mentions, just delete any obsolete alerts and bail out. |
||
217 | if (empty($this->members['quoted']) && empty($this->members['mentioned'])) |
||
218 | { |
||
219 | $this->updateAlerts($msgOptions['id']); |
||
220 | return true; |
||
221 | } |
||
222 | |||
223 | // Never notify about edits to ancient posts. |
||
224 | if (!empty($modSettings['oldTopicDays']) && time() > $msgOptions['poster_time'] + $modSettings['oldTopicDays'] * 86400) |
||
225 | return true; |
||
226 | |||
227 | // If editing is only allowed for a brief time, send after editing becomes disabled. |
||
228 | if (!empty($modSettings['edit_disable_time']) && $modSettings['edit_disable_time'] <= self::MENTION_DELAY) |
||
229 | { |
||
230 | $this->mention_mail_time = $msgOptions['poster_time'] + $modSettings['edit_disable_time'] * 60; |
||
231 | } |
||
232 | // Otherwise, impose a delay before sending notifications about edited posts. |
||
233 | else |
||
234 | { |
||
235 | if (!empty($this->_details['respawns'])) |
||
236 | { |
||
237 | $request = $smcFunc['db_query']('', ' |
||
238 | SELECT modified_time |
||
239 | FROM {db_prefix}messages |
||
240 | WHERE id_msg = {int:msg} |
||
241 | LIMIT 1', |
||
242 | array( |
||
243 | 'msg' => $msgOptions['id'], |
||
244 | ) |
||
245 | ); |
||
246 | list($real_modified_time) = $smcFunc['db_fetch_row']($request); |
||
247 | $smcFunc['db_free_result']($request); |
||
248 | |||
249 | // If it was modified again while we weren't looking, bail out. |
||
250 | // A future instance of this task will take care of it instead. |
||
251 | if ((!empty($msgOptions['modify_time']) ? $msgOptions['modify_time'] : $msgOptions['poster_time']) < $real_modified_time) |
||
252 | return true; |
||
253 | } |
||
254 | |||
255 | $this->mention_mail_time = (!empty($msgOptions['modify_time']) ? $msgOptions['modify_time'] : $msgOptions['poster_time']) + self::MENTION_DELAY * 60; |
||
256 | } |
||
257 | } |
||
258 | |||
259 | $this->members['all'] = array_unique(array_merge(array_keys($this->members['watching']), array_keys($this->members['quoted']), array_keys($this->members['mentioned']))); |
||
260 | |||
261 | if (empty($this->members['all'])) |
||
262 | return true; |
||
263 | |||
264 | $this->prefs = getNotifyPrefs($this->members['all'], '', true); |
||
265 | |||
266 | // May as well disable these, since they'll be stripped out anyway. |
||
267 | $disable = array('attach', 'img', 'iurl', 'url', 'youtube'); |
||
268 | if (!empty($modSettings['disabledBBC'])) |
||
269 | { |
||
270 | $disabledBBC = $modSettings['disabledBBC']; |
||
271 | $disable = array_unique(array_merge($disable, explode(',', $modSettings['disabledBBC']))); |
||
272 | } |
||
273 | $modSettings['disabledBBC'] = implode(',', $disable); |
||
274 | |||
275 | // Notify any members who were mentioned. |
||
276 | if (!empty($this->members['mentioned'])) |
||
277 | $this->handleMentionedNotifications(); |
||
278 | |||
279 | // Notify any members who were quoted. |
||
280 | if (!empty($this->members['quoted'])) |
||
281 | $this->handleQuoteNotifications(); |
||
282 | |||
283 | // Handle rest of the notifications for watched topics and boards |
||
284 | if (!empty($this->members['watching'])) |
||
285 | $this->handleWatchedNotifications(); |
||
286 | |||
287 | // Put this back the way we found it. |
||
288 | if (!empty($disabledBBC)) |
||
289 | $modSettings['disabledBBC'] = $disabledBBC; |
||
290 | |||
291 | // Track what we sent. |
||
292 | $members_to_log = array_intersect($this->members['emailed'], array_keys($this->members['watching'])); |
||
293 | if (!empty($members_to_log)) |
||
294 | { |
||
295 | $smcFunc['db_query']('', ' |
||
296 | UPDATE {db_prefix}log_notify |
||
297 | SET sent = {int:is_sent} |
||
298 | WHERE ' . ($type == 'topic' ? 'id_board = {int:board}' : 'id_topic = {int:topic}') . ' |
||
299 | AND id_member IN ({array_int:members})', |
||
300 | array( |
||
301 | 'topic' => $topicOptions['id'], |
||
302 | 'board' => $topicOptions['board'], |
||
303 | // 'members' => $this->members['emailed'], |
||
304 | 'members' => $members_to_log, |
||
305 | 'is_sent' => 1, |
||
306 | ) |
||
307 | ); |
||
308 | } |
||
309 | |||
310 | // Insert it into the digest for daily/weekly notifications |
||
311 | if ($type != 'edit' && empty($this->_details['respawns'])) |
||
312 | { |
||
313 | $smcFunc['db_insert']('', |
||
314 | '{db_prefix}log_digest', |
||
315 | array( |
||
316 | 'id_topic' => 'int', 'id_msg' => 'int', 'note_type' => 'string', 'exclude' => 'int', |
||
317 | ), |
||
318 | array($topicOptions['id'], $msgOptions['id'], $type, $posterOptions['id']), |
||
319 | array() |
||
320 | ); |
||
321 | } |
||
322 | |||
323 | // Insert the alerts if any |
||
324 | $this->updateAlerts($msgOptions['id']); |
||
325 | |||
326 | // If there is anyone still to notify via email, create a new task later. |
||
327 | $unnotified = array_diff_key($unnotified, array_flip($this->members['emailed'])); |
||
328 | if (!empty($unnotified) || !empty($msgOptions['mentioned_members']) || !empty($msgOptions['quoted_members'])) |
||
329 | { |
||
330 | $new_details = $this->_details; |
||
331 | |||
332 | if (empty($new_details['respawns'])) |
||
333 | $new_details['respawns'] = 0; |
||
334 | |||
335 | if ($new_details['respawns']++ < 10) |
||
336 | { |
||
337 | $smcFunc['db_insert']('', |
||
338 | '{db_prefix}background_tasks', |
||
339 | array( |
||
340 | 'task_file' => 'string', |
||
341 | 'task_class' => 'string', |
||
342 | 'task_data' => 'string', |
||
343 | 'claimed_time' => 'int', |
||
344 | ), |
||
345 | array( |
||
346 | '$sourcedir/tasks/CreatePost-Notify.php', |
||
347 | 'CreatePost_Notify_Background', |
||
348 | $smcFunc['json_encode']($new_details), |
||
349 | max(0, $this->mention_mail_time - MAX_CLAIM_THRESHOLD), |
||
350 | ), |
||
351 | array('id_task') |
||
352 | ); |
||
353 | } |
||
354 | } |
||
355 | |||
356 | return true; |
||
357 | } |
||
785 | ?> |