Completed
Push — release-2.1 ( 967873...229f76 )
by Mathias
09:51
created

getLastPosts()   C

Complexity

Conditions 12
Paths 18

Size

Total Lines 81
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 12
eloc 47
nc 18
nop 1
dl 0
loc 81
rs 6.9666
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
	$rows = $smcFunc['db_fetch_all']($request);
55
56
	// If the ability to embed attachments in posts is enabled, load the attachments now for efficiency
57
	if (!empty($modSettings['attachmentEnable']) && (empty($modSettings['disabledBBC']) || !in_array('attach', explode(',', strtolower($modSettings['disabledBBC'])))))
58
	{
59
		$msgIDs = array();
60
		foreach ($rows as $row)
61
			$msgIDs[] = $row['id_msg'];
62
63
		require_once($sourcedir . '/Subs-Attachments.php');
64
		prepareAttachsByMsg($msgIDs);
65
	}
66
67
	$posts = array();
68
	foreach ($rows as $row)
69
	{
70
		// Censor the subject and post for the preview ;).
71
		censorText($row['subject']);
72
		censorText($row['body']);
73
74
		$row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']), array('<br>' => '&#10;')));
75
		if ($smcFunc['strlen']($row['body']) > 128)
76
			$row['body'] = $smcFunc['substr']($row['body'], 0, 128) . '...';
77
78
		// Build the array.
79
		$posts[] = array(
80
			'board' => array(
81
				'id' => $row['id_board'],
82
				'name' => $row['board_name'],
83
				'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
84
				'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>'
85
			),
86
			'topic' => $row['id_topic'],
87
			'poster' => array(
88
				'id' => $row['id_member'],
89
				'name' => $row['poster_name'],
90
				'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
91
				'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
92
			),
93
			'subject' => $row['subject'],
94
			'short_subject' => shorten_subject($row['subject'], 24),
95
			'preview' => $row['body'],
96
			'time' => timeformat($row['poster_time']),
97
			'timestamp' => forum_time(true, $row['poster_time']),
98
			'raw_timestamp' => $row['poster_time'],
99
			'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#msg' . $row['id_msg'],
100
			'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>'
101
		);
102
	}
103
	$smcFunc['db_free_result']($request);
104
105
	return $posts;
106
}
107
108
/**
109
 * Callback-function for the cache for getLastPosts().
110
 *
111
 * @param array $latestPostOptions
112
 */
113
function cache_getLastPosts($latestPostOptions)
114
{
115
	return array(
116
		'data' => getLastPosts($latestPostOptions),
117
		'expires' => time() + 60,
118
		'post_retri_eval' => '
119
			foreach ($cache_block[\'data\'] as $k => $post)
120
			{
121
				$cache_block[\'data\'][$k][\'time\'] = timeformat($post[\'raw_timestamp\']);
122
				$cache_block[\'data\'][$k][\'timestamp\'] = forum_time(true, $post[\'raw_timestamp\']);
123
			}',
124
	);
125
}
126
127
?>