Passed
Pull Request — release-2.1 (#5615)
by Mathias
05:24
created

getLastPosts()   C

Complexity

Conditions 13
Paths 72

Size

Total Lines 89
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 13
eloc 51
nc 72
nop 1
dl 0
loc 89
rs 6.6166
c 2
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 latests posts on forum.
5
 *
6
 * Simple Machines Forum (SMF)
7
 *
8
 * @package SMF
9
 * @author Simple Machines http://www.simplemachines.org
10
 * @copyright 2019 Simple Machines and individual contributors
11
 * @license http://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 RC2
14
 */
15
16
if (!defined('SMF'))
17
	die('No direct access...');
18
19
/**
20
 * Get the latest posts of a forum.
21
 *
22
 * @param array $latestPostOptions
23
 * @return array
24
 */
25
function getLastPosts($latestPostOptions)
26
{
27
	global $scripturl, $modSettings, $smcFunc, $sourcedir;
28
29
	// Find all the posts.  Newer ones will have higher IDs.  (assuming the last 20 * number are accessible...)
30
	// @todo SLOW This query is now slow, NEEDS to be fixed.  Maybe break into two?
31
	$request = $smcFunc['db_query']('substring', '
32
		SELECT
33
			m.poster_time, m.subject, m.id_topic, m.id_member, m.id_msg,
34
			COALESCE(mem.real_name, m.poster_name) AS poster_name, t.id_board, b.name AS board_name,
35
			SUBSTRING(m.body, 1, 385) AS body, m.smileys_enabled
36
		FROM {db_prefix}messages AS m
37
			INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
38
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
39
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
40
		WHERE m.id_msg >= {int:likely_max_msg}' .
41
			(!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
42
			AND b.id_board != {int:recycle_board}' : '') . '
43
			AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
44
			AND t.approved = {int:is_approved}
45
			AND m.approved = {int:is_approved}' : '') . '
46
		ORDER BY m.id_msg DESC
47
		LIMIT ' . $latestPostOptions['number_posts'],
48
		array(
49
			'likely_max_msg' => max(0, $modSettings['maxMsgID'] - 50 * $latestPostOptions['number_posts']),
50
			'recycle_board' => $modSettings['recycle_board'],
51
			'is_approved' => 1,
52
		)
53
	);
54
55
	$rows = $smcFunc['db_fetch_all']($request);
56
57
	// BBC and the entire attachments feature is enabled
58
	$disabled = array();
59
60
	$temp = !empty($modSettings['disabledBBC']) ? explode(',', strtolower($modSettings['disabledBBC'])) : array();
61
62
	foreach ($temp as $tag)
63
		$disabled[trim($tag)] = true;
64
	if (!empty($modSettings['attachmentEnable']) && empty($disabled['attach']))
65
	{
66
		require_once($sourcedir . '/Subs-Attachments.php');
67
		$msgIDs = array();
68
		foreach ($rows as $key => $value) {
69
			$msgIDs[] = $value['id_msg'];
70
		}
71
72
		prepareAttachsByMsg($msgIDs);
73
	}
74
75
	$posts = array();
76
	foreach ($rows as $key => $row)
77
	{
78
		// Censor the subject and post for the preview ;).
79
		censorText($row['subject']);
80
		censorText($row['body']);
81
82
		$row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']), array('<br>' => '&#10;')));
83
		if ($smcFunc['strlen']($row['body']) > 128)
84
			$row['body'] = $smcFunc['substr']($row['body'], 0, 128) . '...';
85
86
		// Build the array.
87
		$posts[] = array(
88
			'board' => array(
89
				'id' => $row['id_board'],
90
				'name' => $row['board_name'],
91
				'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
92
				'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>'
93
			),
94
			'topic' => $row['id_topic'],
95
			'poster' => array(
96
				'id' => $row['id_member'],
97
				'name' => $row['poster_name'],
98
				'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
99
				'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
100
			),
101
			'subject' => $row['subject'],
102
			'short_subject' => shorten_subject($row['subject'], 24),
103
			'preview' => $row['body'],
104
			'time' => timeformat($row['poster_time']),
105
			'timestamp' => forum_time(true, $row['poster_time']),
106
			'raw_timestamp' => $row['poster_time'],
107
			'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#msg' . $row['id_msg'],
108
			'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>'
109
		);
110
	}
111
	$smcFunc['db_free_result']($request);
112
113
	return $posts;
114
}
115
116
/**
117
 * Callback-function for the cache for getLastPosts().
118
 *
119
 * @param array $latestPostOptions
120
 */
121
function cache_getLastPosts($latestPostOptions)
122
{
123
	return array(
124
		'data' => getLastPosts($latestPostOptions),
125
		'expires' => time() + 60,
126
		'post_retri_eval' => '
127
			foreach ($cache_block[\'data\'] as $k => $post)
128
			{
129
				$cache_block[\'data\'][$k][\'time\'] = timeformat($post[\'raw_timestamp\']);
130
				$cache_block[\'data\'][$k][\'timestamp\'] = forum_time(true, $post[\'raw_timestamp\']);
131
			}',
132
	);
133
}
134
135
?>