Completed
Pull Request — development (#3050)
by John
23:37
created

Recent.subs.php ➔ prepareRecentPosts()   C

Complexity

Conditions 10
Paths 161

Size

Total Lines 71
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 10.001

Importance

Changes 0
Metric Value
cc 10
eloc 53
nc 161
nop 2
dl 0
loc 71
ccs 43
cts 44
cp 0.9773
crap 10.001
rs 5.5881
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * This file contains a couple of functions for the latest posts on forum.
5
 *
6
 * @name      ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
9
 *
10
 * This file contains code covered by:
11
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
12
 * license:  	BSD, See included LICENSE.TXT for terms and conditions.
13
 *
14
 * @version 2.0 dev
15
 *
16
 */
17
18
/**
19
 * Get the latest posts of a forum.
20
 *
21
 * @param mixed[] $latestPostOptions
22
 * @return array
23
 */
24
function getLastPosts($latestPostOptions)
25
{
26
	global $scripturl, $modSettings;
27
28
	$db = database();
29
30
	// Find all the posts. Newer ones will have higher IDs. (assuming the last 20 * number are accessible...)
31
	// @todo SLOW This query is now slow, NEEDS to be fixed.  Maybe break into two?
32
	$request = $db->query('substring', '
33
		SELECT
34
			m.poster_time, m.subject, m.id_topic, m.id_member, m.id_msg,
35
			COALESCE(mem.real_name, m.poster_name) AS poster_name, t.id_board, b.name AS board_name,
36
			SUBSTRING(m.body, 1, 385) AS body, m.smileys_enabled
37
		FROM {db_prefix}messages AS m
38
			INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
39
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
40
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
41
		WHERE m.id_msg >= {int:likely_max_msg}' .
42
			(!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
43
			AND b.id_board != {int:recycle_board}' : '') . '
44
			AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
45
			AND t.approved = {int:is_approved}
46
			AND m.approved = {int:is_approved}' : '') . '
47
		ORDER BY m.id_msg DESC
48
		LIMIT ' . $latestPostOptions['number_posts'],
49
		array(
50
			'likely_max_msg' => max(0, $modSettings['maxMsgID'] - 50 * $latestPostOptions['number_posts']),
51
			'recycle_board' => $modSettings['recycle_board'],
52
			'is_approved' => 1,
53
		)
54
	);
55
56
	$posts = array();
57
	$bbc_parser = \BBC\ParserWrapper::instance();
58
59
	while ($row = $db->fetch_assoc($request))
60
	{
61
		// Censor the subject and post for the preview ;).
62
		$row['subject'] = censor($row['subject']);
63
		$row['body'] = censor($row['body']);
64
65
		$row['body'] = strip_tags(strtr($bbc_parser->parseMessage($row['body'], $row['smileys_enabled']), array('<br />' => '&#10;')));
66
		$row['body'] = Util::shorten_text($row['body'], !empty($modSettings['lastpost_preview_characters']) ? $modSettings['lastpost_preview_characters'] : 128, true);
67
68
		// Build the array.
69
		$posts[] = array(
70
			'board' => array(
71
				'id' => $row['id_board'],
72
				'name' => $row['board_name'],
73
				'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
74
				'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>'
75
			),
76
			'topic' => $row['id_topic'],
77
			'poster' => array(
78
				'id' => $row['id_member'],
79
				'name' => $row['poster_name'],
80
				'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
81
				'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
82
			),
83
			'subject' => $row['subject'],
84
			'short_subject' => Util::shorten_text($row['subject'], $modSettings['subject_length']),
85
			'preview' => $row['body'],
86
			'time' => standardTime($row['poster_time']),
87
			'html_time' => htmlTime($row['poster_time']),
88
			'timestamp' => forum_time(true, $row['poster_time']),
89
			'raw_timestamp' => $row['poster_time'],
90
			'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#msg' . $row['id_msg'],
91
			'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>'
92
		);
93
	}
94
	$db->free_result($request);
95
96
	return $posts;
97
}
98
99
/**
100
 * Callback-function for the cache for getLastPosts().
101
 *
102
 * @param mixed[] $latestPostOptions
103
 */
104
function cache_getLastPosts($latestPostOptions)
105
{
106
	return array(
107
		'data' => getLastPosts($latestPostOptions),
108
		'expires' => time() + 60,
109
		'post_retri_eval' => '
110
			foreach ($cache_block[\'data\'] as $k => $post)
111
			{
112
				$cache_block[\'data\'][$k] += array(
113
					\'time\' => standardTime($post[\'raw_timestamp\']),
114
					\'html_time\' => htmlTime($post[\'raw_timestamp\']),
115
					\'timestamp\' => $post[\'raw_timestamp\'],
116
				);
117
			}',
118
	);
119
}
120
121
/**
122
 * Formats data supplied into a form that can be used in the template
123
 *
124
 * @param mixed[] $messages
125
 * @param int $start
126
 */
127
function prepareRecentPosts($messages, $start)
128
{
129 1
	global $user_info, $scripturl, $modSettings;
130
131 1
	$counter = $start + 1;
132 1
	$posts = array();
133 1
	$board_ids = array('own' => array(), 'any' => array());
134 1
	$bbc_parser = \BBC\ParserWrapper::instance();
135 1
	foreach ($messages as $row)
136
	{
137
		// Censor everything.
138 1
		$row['body'] = censor($row['body']);
139 1
		$row['subject'] = censor($row['subject']);
140
141
		// BBC-atize the message.
142 1
		$row['body'] = $bbc_parser->parseMessage($row['body'], $row['smileys_enabled']);
143
144
		// And build the array.
145 1
		$posts[$row['id_msg']] = array(
146 1
			'id' => $row['id_msg'],
147 1
			'counter' => $counter++,
148 1
			'alternate' => $counter % 2,
149
			'category' => array(
150 1
				'id' => $row['id_cat'],
151 1
				'name' => $row['cname'],
152 1
				'href' => $scripturl . $modSettings['default_forum_action'] . '#c' . $row['id_cat'],
153 1
				'link' => '<a href="' . $scripturl . $modSettings['default_forum_action'] . '#c' . $row['id_cat'] . '">' . $row['cname'] . '</a>'
154
			),
155
			'board' => array(
156 1
				'id' => $row['id_board'],
157 1
				'name' => $row['bname'],
158 1
				'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
159 1
				'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['bname'] . '</a>'
160
			),
161 1
			'topic' => $row['id_topic'],
162 1
			'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
163 1
			'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>',
164 1
			'start' => $row['num_replies'],
165 1
			'subject' => $row['subject'],
166 1
			'time' => standardTime($row['poster_time']),
167 1
			'html_time' => htmlTime($row['poster_time']),
168 1
			'timestamp' => forum_time(true, $row['poster_time']),
169
			'first_poster' => array(
170 1
				'id' => $row['first_id_member'],
171 1
				'name' => $row['first_display_name'],
172 1
				'href' => empty($row['first_id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['first_id_member'],
173 1
				'link' => empty($row['first_id_member']) ? $row['first_display_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['first_id_member'] . '">' . $row['first_display_name'] . '</a>'
174
			),
175
			'poster' => array(
176 1
				'id' => $row['id_member'],
177 1
				'name' => $row['poster_name'],
178 1
				'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
179 1
				'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
180
			),
181 1
			'body' => $row['body'],
182 1
			'message' => $row['body'],
183
			'tests' => array(
184
				'can_reply' => false,
185
				'can_mark_notify' => false,
186
				'can_delete' => false,
187
			),
188 1
			'delete_possible' => ($row['id_first_msg'] != $row['id_msg'] || $row['id_last_msg'] == $row['id_msg']) && (empty($modSettings['edit_disable_time']) || $row['poster_time'] + $modSettings['edit_disable_time'] * 60 >= time()),
189
		);
190
191 1
		if ($user_info['id'] == $row['first_id_member'])
192
			$board_ids['own'][$row['id_board']][] = $row['id_msg'];
193 1
		$board_ids['any'][$row['id_board']][] = $row['id_msg'];
194
	}
195
196 1
	return array($posts, $board_ids);
197
}
198
199
/**
200
 * Return the earliest message a user can...see?
201
 */
202
function earliest_msg()
203
{
204
	global $board, $user_info;
205
206
	$db = database();
207
208
	if (!empty($board))
209
	{
210
		$request = $db->query('', '
211
			SELECT MIN(id_msg)
212
			FROM {db_prefix}log_mark_read
213
			WHERE id_member = {int:current_member}
214
				AND id_board = {int:current_board}',
215
			array(
216
				'current_board' => $board,
217
				'current_member' => $user_info['id'],
218
			)
219
		);
220
		list ($earliest_msg) = $db->fetch_row($request);
221
		$db->free_result($request);
222
	}
223
	else
224
	{
225
		$request = $db->query('', '
226
			SELECT MIN(lmr.id_msg)
227
			FROM {db_prefix}boards AS b
228
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = {int:current_member})
229
			WHERE {query_see_board}',
230
			array(
231
				'current_member' => $user_info['id'],
232
			)
233
		);
234
		list ($earliest_msg) = $db->fetch_row($request);
235
		$db->free_result($request);
236
	}
237
238
	// This is needed in case of topics marked unread.
239
	if (empty($earliest_msg))
240
		$earliest_msg = 0;
241
	else
242
	{
243
		// Using caching, when possible, to ignore the below slow query.
244
		if (isset($_SESSION['cached_log_time']) && $_SESSION['cached_log_time'][0] + 45 > time())
245
			$earliest_msg2 = $_SESSION['cached_log_time'][1];
246
		else
247
		{
248
			// This query is pretty slow, but it's needed to ensure nothing crucial is ignored.
249
			$request = $db->query('', '
250
				SELECT MIN(id_msg)
251
				FROM {db_prefix}log_topics
252
				WHERE id_member = {int:current_member}',
253
				array(
254
					'current_member' => $user_info['id'],
255
				)
256
			);
257
			list ($earliest_msg2) = $db->fetch_row($request);
258
			$db->free_result($request);
259
260
			// In theory this could be zero, if the first ever post is unread, so fudge it ;)
261
			if ($earliest_msg2 == 0)
262
				$earliest_msg2 = -1;
263
264
			$_SESSION['cached_log_time'] = array(time(), $earliest_msg2);
265
		}
266
267
		$earliest_msg = min($earliest_msg2, $earliest_msg);
268
	}
269
270
	return $earliest_msg;
271
}
272
273
/**
274
 * Callback-function for the cache for getLastTopics().
275
 *
276
 * @param mixed[] $latestTopicOptions
277
 */
278
function cache_getLastTopics($latestTopicOptions)
279
{
280
	return array(
281
		'data' => getLastTopics($latestTopicOptions),
282
		'expires' => time() + 60,
283
		'post_retri_eval' => '
284
			foreach ($cache_block[\'data\'] as $k => $post)
285
			{
286
				$cache_block[\'data\'][$k] += array(
287
					\'time\' => standardTime($post[\'raw_timestamp\']),
288
					\'html_time\' => htmlTime($post[\'raw_timestamp\']),
289
					\'timestamp\' => $post[\'raw_timestamp\'],
290
				);
291
			}',
292
	);
293
}
294
295
/**
296
 * Get the latest posts of a forum.
297
 *
298
 * @param mixed[] $latestTopicOptions
299
 * @return array
300
 */
301
function getLastTopics($latestTopicOptions)
302
{
303
	global $scripturl, $modSettings, $txt;
304
305
	$db = database();
306
307
	// Find all the posts. Newer ones will have higher IDs. (assuming the last 20 * number are accessable...)
308
	// @todo SLOW This query is now slow, NEEDS to be fixed.  Maybe break into two?
309
	$request = $db->query('substring', '
310
		SELECT
311
			ml.poster_time, mf.subject, ml.id_topic, ml.id_member, ml.id_msg, t.id_first_msg, ml.id_msg_modified,
312
			' . ($latestTopicOptions['id_member'] == 0 ? '0' : 'COALESCE(lt.id_msg, lmr.id_msg, -1) + 1') . ' AS new_from,
313
			COALESCE(mem.real_name, ml.poster_name) AS poster_name, t.id_board, b.name AS board_name,
314
			SUBSTRING(ml.body, 1, 385) AS body, ml.smileys_enabled
315
		FROM {db_prefix}topics AS t
316
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
317
			LEFT JOIN {db_prefix}messages AS mf ON (t.id_first_msg = mf.id_msg)
318
			LEFT JOIN {db_prefix}messages AS ml ON (t.id_last_msg = ml.id_msg)
319
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ml.id_member)' . ($latestTopicOptions['id_member'] == 0 ? '' : '
320
			LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
321
			LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})') . '
322
		WHERE ml.id_msg >= {int:likely_max_msg}' .
323
			(!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
324
			AND b.id_board != {int:recycle_board}' : '') . '
325
			AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
326
			AND t.approved = {int:is_approved}' : '') . '
327
		ORDER BY t.id_last_msg DESC
328
		LIMIT {int:num_msgs}',
329
		array(
330
			'likely_max_msg' => max(0, $modSettings['maxMsgID'] - 50 * $latestTopicOptions['number_posts']),
331
			'recycle_board' => $modSettings['recycle_board'],
332
			'is_approved' => 1,
333
			'num_msgs' =>  $latestTopicOptions['number_posts'],
334
			'current_member' =>  $latestTopicOptions['id_member'],
335
		)
336
	);
337
338
	$posts = array();
339
	$bbc_parser = \BBC\ParserWrapper::instance();
340
341
	while ($row = $db->fetch_assoc($request))
342
	{
343
		// Censor the subject and post for the preview ;).
344
		$row['subject'] = censor($row['subject']);
345
		$row['body'] = censor($row['body']);
346
347
		$row['body'] = strip_tags(strtr($bbc_parser->parseMessage($row['body'], $row['smileys_enabled']), array('<br />' => '&#10;')));
348
		$row['body'] = Util::shorten_text($row['body'], !empty($modSettings['lastpost_preview_characters']) ? $modSettings['lastpost_preview_characters'] : 128, true);
349
350
		// Build the array.
351
		$post = array(
352
			'board' => array(
353
				'id' => $row['id_board'],
354
				'name' => $row['board_name'],
355
				'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
356
				'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>'
357
			),
358
			'topic' => $row['id_topic'],
359
			'poster' => array(
360
				'id' => $row['id_member'],
361
				'name' => $row['poster_name'],
362
				'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
363
				'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
364
			),
365
			'subject' => $row['subject'],
366
			'short_subject' => Util::shorten_text($row['subject'], $modSettings['subject_length']),
367
			'preview' => $row['body'],
368
			'time' => standardTime($row['poster_time']),
369
			'html_time' => htmlTime($row['poster_time']),
370
			'timestamp' => forum_time(true, $row['poster_time']),
371
			'raw_timestamp' => $row['poster_time'],
372
			'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#msg' . $row['id_msg'],
373
			'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>',
374
			'new' => $row['new_from'] <= $row['id_msg_modified'],
375
			'new_from' => $row['new_from'],
376
			'newtime' => $row['new_from'],
377
			'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . '#new',
378
		);
379
		if ($post['new'])
380
			$post['link'] .= '
381
							<a class="new_posts" href="' . $post['new_href'] . '" id="newicon' . $row['id_msg'] . '">' . $txt['new'] . '</a>';
382
383
		$posts[] = $post;
384
	}
385
	$db->free_result($request);
386
387
	return $posts;
388
}