Completed
Pull Request — development (#3098)
by John
09:23
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
 * @return array
105
 */
106
function cache_getLastPosts($latestPostOptions)
107
{
108
	return array(
109
		'data' => getLastPosts($latestPostOptions),
110
		'expires' => time() + 60,
111
		'post_retri_eval' => '
112
			foreach ($cache_block[\'data\'] as $k => $post)
113
			{
114
				$cache_block[\'data\'][$k] += array(
115
					\'time\' => standardTime($post[\'raw_timestamp\']),
116
					\'html_time\' => htmlTime($post[\'raw_timestamp\']),
117
					\'timestamp\' => $post[\'raw_timestamp\'],
118
				);
119
			}',
120
	);
121
}
122
123
/**
124
 * Formats data supplied into a form that can be used in the template
125
 *
126
 * @param mixed[] $messages
127
 * @param int $start
128
 *
129
 * @return array
130
 */
131
function prepareRecentPosts($messages, $start)
132
{
133 1
	global $user_info, $scripturl, $modSettings;
134
135 1
	$counter = $start + 1;
136 1
	$posts = array();
137 1
	$board_ids = array('own' => array(), 'any' => array());
138 1
	$bbc_parser = \BBC\ParserWrapper::instance();
139 1
	foreach ($messages as $row)
140
	{
141
		// Censor everything.
142 1
		$row['body'] = censor($row['body']);
143 1
		$row['subject'] = censor($row['subject']);
144
145
		// BBC-atize the message.
146 1
		$row['body'] = $bbc_parser->parseMessage($row['body'], $row['smileys_enabled']);
147
148
		// And build the array.
149 1
		$posts[$row['id_msg']] = array(
150 1
			'id' => $row['id_msg'],
151 1
			'counter' => $counter++,
152 1
			'alternate' => $counter % 2,
153
			'category' => array(
154 1
				'id' => $row['id_cat'],
155 1
				'name' => $row['cname'],
156 1
				'href' => $scripturl . $modSettings['default_forum_action'] . '#c' . $row['id_cat'],
157 1
				'link' => '<a href="' . $scripturl . $modSettings['default_forum_action'] . '#c' . $row['id_cat'] . '">' . $row['cname'] . '</a>'
158
			),
159
			'board' => array(
160 1
				'id' => $row['id_board'],
161 1
				'name' => $row['bname'],
162 1
				'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
163 1
				'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['bname'] . '</a>'
164
			),
165 1
			'topic' => $row['id_topic'],
166 1
			'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
167 1
			'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>',
168 1
			'start' => $row['num_replies'],
169 1
			'subject' => $row['subject'],
170 1
			'time' => standardTime($row['poster_time']),
171 1
			'html_time' => htmlTime($row['poster_time']),
172 1
			'timestamp' => forum_time(true, $row['poster_time']),
173
			'first_poster' => array(
174 1
				'id' => $row['first_id_member'],
175 1
				'name' => $row['first_display_name'],
176 1
				'href' => empty($row['first_id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['first_id_member'],
177 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>'
178
			),
179
			'poster' => array(
180 1
				'id' => $row['id_member'],
181 1
				'name' => $row['poster_name'],
182 1
				'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
183 1
				'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
184
			),
185 1
			'body' => $row['body'],
186 1
			'message' => $row['body'],
187
			'tests' => array(
188
				'can_reply' => false,
189
				'can_mark_notify' => false,
190
				'can_delete' => false,
191
			),
192 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()),
193
		);
194
195 1
		if ($user_info['id'] == $row['first_id_member'])
196
			$board_ids['own'][$row['id_board']][] = $row['id_msg'];
197 1
		$board_ids['any'][$row['id_board']][] = $row['id_msg'];
198
	}
199
200 1
	return array($posts, $board_ids);
201
}
202
203
/**
204
 * Return the earliest message a user can...see?
205
 */
206
function earliest_msg()
207
{
208
	global $board, $user_info;
209
210
	$db = database();
211
212
	if (!empty($board))
213
	{
214
		$request = $db->query('', '
215
			SELECT MIN(id_msg)
216
			FROM {db_prefix}log_mark_read
217
			WHERE id_member = {int:current_member}
218
				AND id_board = {int:current_board}',
219
			array(
220
				'current_board' => $board,
221
				'current_member' => $user_info['id'],
222
			)
223
		);
224
		list ($earliest_msg) = $db->fetch_row($request);
225
		$db->free_result($request);
226
	}
227
	else
228
	{
229
		$request = $db->query('', '
230
			SELECT MIN(lmr.id_msg)
231
			FROM {db_prefix}boards AS b
232
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = {int:current_member})
233
			WHERE {query_see_board}',
234
			array(
235
				'current_member' => $user_info['id'],
236
			)
237
		);
238
		list ($earliest_msg) = $db->fetch_row($request);
239
		$db->free_result($request);
240
	}
241
242
	// This is needed in case of topics marked unread.
243
	if (empty($earliest_msg))
244
		$earliest_msg = 0;
245
	else
246
	{
247
		// Using caching, when possible, to ignore the below slow query.
248
		if (isset($_SESSION['cached_log_time']) && $_SESSION['cached_log_time'][0] + 45 > time())
249
			$earliest_msg2 = $_SESSION['cached_log_time'][1];
250
		else
251
		{
252
			// This query is pretty slow, but it's needed to ensure nothing crucial is ignored.
253
			$request = $db->query('', '
254
				SELECT MIN(id_msg)
255
				FROM {db_prefix}log_topics
256
				WHERE id_member = {int:current_member}',
257
				array(
258
					'current_member' => $user_info['id'],
259
				)
260
			);
261
			list ($earliest_msg2) = $db->fetch_row($request);
262
			$db->free_result($request);
263
264
			// In theory this could be zero, if the first ever post is unread, so fudge it ;)
265
			if ($earliest_msg2 == 0)
266
				$earliest_msg2 = -1;
267
268
			$_SESSION['cached_log_time'] = array(time(), $earliest_msg2);
269
		}
270
271
		$earliest_msg = min($earliest_msg2, $earliest_msg);
272
	}
273
274
	return $earliest_msg;
275
}
276
277
/**
278
 * Callback-function for the cache for getLastTopics().
279
 *
280
 * @param mixed[] $latestTopicOptions
281
 *
282
 * @return array
283
 */
284
function cache_getLastTopics($latestTopicOptions)
285
{
286
	return array(
287
		'data' => getLastTopics($latestTopicOptions),
288
		'expires' => time() + 60,
289
		'post_retri_eval' => '
290
			foreach ($cache_block[\'data\'] as $k => $post)
291
			{
292
				$cache_block[\'data\'][$k] += array(
293
					\'time\' => standardTime($post[\'raw_timestamp\']),
294
					\'html_time\' => htmlTime($post[\'raw_timestamp\']),
295
					\'timestamp\' => $post[\'raw_timestamp\'],
296
				);
297
			}',
298
	);
299
}
300
301
/**
302
 * Get the latest posts of a forum.
303
 *
304
 * @param mixed[] $latestTopicOptions
305
 * @return array
306
 */
307
function getLastTopics($latestTopicOptions)
308
{
309
	global $scripturl, $modSettings, $txt;
310
311
	$db = database();
312
313
	// Find all the posts. Newer ones will have higher IDs. (assuming the last 20 * number are accessable...)
314
	// @todo SLOW This query is now slow, NEEDS to be fixed.  Maybe break into two?
315
	$request = $db->query('substring', '
316
		SELECT
317
			ml.poster_time, mf.subject, ml.id_topic, ml.id_member, ml.id_msg, t.id_first_msg, ml.id_msg_modified,
318
			' . ($latestTopicOptions['id_member'] == 0 ? '0' : 'COALESCE(lt.id_msg, lmr.id_msg, -1) + 1') . ' AS new_from,
319
			COALESCE(mem.real_name, ml.poster_name) AS poster_name, t.id_board, b.name AS board_name,
320
			SUBSTRING(ml.body, 1, 385) AS body, ml.smileys_enabled
321
		FROM {db_prefix}topics AS t
322
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
323
			LEFT JOIN {db_prefix}messages AS mf ON (t.id_first_msg = mf.id_msg)
324
			LEFT JOIN {db_prefix}messages AS ml ON (t.id_last_msg = ml.id_msg)
325
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ml.id_member)' . ($latestTopicOptions['id_member'] == 0 ? '' : '
326
			LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
327
			LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})') . '
328
		WHERE ml.id_msg >= {int:likely_max_msg}' .
329
			(!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
330
			AND b.id_board != {int:recycle_board}' : '') . '
331
			AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
332
			AND t.approved = {int:is_approved}' : '') . '
333
		ORDER BY t.id_last_msg DESC
334
		LIMIT {int:num_msgs}',
335
		array(
336
			'likely_max_msg' => max(0, $modSettings['maxMsgID'] - 50 * $latestTopicOptions['number_posts']),
337
			'recycle_board' => $modSettings['recycle_board'],
338
			'is_approved' => 1,
339
			'num_msgs' =>  $latestTopicOptions['number_posts'],
340
			'current_member' =>  $latestTopicOptions['id_member'],
341
		)
342
	);
343
344
	$posts = array();
345
	$bbc_parser = \BBC\ParserWrapper::instance();
346
347
	while ($row = $db->fetch_assoc($request))
348
	{
349
		// Censor the subject and post for the preview ;).
350
		$row['subject'] = censor($row['subject']);
351
		$row['body'] = censor($row['body']);
352
353
		$row['body'] = strip_tags(strtr($bbc_parser->parseMessage($row['body'], $row['smileys_enabled']), array('<br />' => '&#10;')));
354
		$row['body'] = Util::shorten_text($row['body'], !empty($modSettings['lastpost_preview_characters']) ? $modSettings['lastpost_preview_characters'] : 128, true);
355
356
		// Build the array.
357
		$post = array(
358
			'board' => array(
359
				'id' => $row['id_board'],
360
				'name' => $row['board_name'],
361
				'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
362
				'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>'
363
			),
364
			'topic' => $row['id_topic'],
365
			'poster' => array(
366
				'id' => $row['id_member'],
367
				'name' => $row['poster_name'],
368
				'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
369
				'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
370
			),
371
			'subject' => $row['subject'],
372
			'short_subject' => Util::shorten_text($row['subject'], $modSettings['subject_length']),
373
			'preview' => $row['body'],
374
			'time' => standardTime($row['poster_time']),
375
			'html_time' => htmlTime($row['poster_time']),
376
			'timestamp' => forum_time(true, $row['poster_time']),
377
			'raw_timestamp' => $row['poster_time'],
378
			'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#msg' . $row['id_msg'],
379
			'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>',
380
			'new' => $row['new_from'] <= $row['id_msg_modified'],
381
			'new_from' => $row['new_from'],
382
			'newtime' => $row['new_from'],
383
			'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . '#new',
384
		);
385
		if ($post['new'])
386
			$post['link'] .= '
387
							<a class="new_posts" href="' . $post['new_href'] . '" id="newicon' . $row['id_msg'] . '">' . $txt['new'] . '</a>';
388
389
		$posts[] = $post;
390
	}
391
	$db->free_result($request);
392
393
	return $posts;
394
}