Skip to content

Commit

Permalink
Optimize toplist code, fix MSSQL errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
rxu committed Oct 8, 2023
1 parent 09e87df commit 4fd5057
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 71 deletions.
1 change: 1 addition & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ services:
- '@gfksx.thanksforposts.helper'
- '@request'
- '@controller.helper'
- '@user_loader'
- '%tables.forums%'
- '%tables.thanks%'
- '%tables.users%'
Expand Down
136 changes: 65 additions & 71 deletions controller/toplist.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class toplist
* @param \gfksx\thanksforposts\core\helper $gfksx_helper Helper object
* @param \phpbb\request\request_interface $request Request object
* @param \phpbb\controller\helper $controller_helper Controller helper object
* @param \phpbb\user_loader $user_loader User loader object
* @param string $forums_table FORUMS_TABLE
* @param string $thanks_table THANKS_TABLE
* @param string $users_table USERS_TABLE
Expand All @@ -100,6 +101,7 @@ public function __construct(
\gfksx\thanksforposts\core\helper $gfksx_helper,
\phpbb\request\request_interface $request,
\phpbb\controller\helper $controller_helper,
\phpbb\user_loader $user_loader,
$forums_table, $thanks_table, $users_table, $posts_table
)
{
Expand All @@ -116,6 +118,7 @@ public function __construct(
$this->gfksx_helper = $gfksx_helper;
$this->request = $request;
$this->controller_helper = $controller_helper;
$this->user_loader = $user_loader;
$this->forums_table = $forums_table;
$this->thanks_table = $thanks_table;
$this->users_table = $users_table;
Expand All @@ -133,10 +136,10 @@ public function main()
$u_search_post = $u_search_topic = $u_search_forum = '';
$topic_id = $this->request->variable('t', 0);
$return_chars = $this->request->variable('ch', ($topic_id) ? -1 : 300);
$words = [];
$rowset = $user_list = [];
$ex_fid_ary = array_keys($this->auth->acl_getf('!f_read', true));
$ex_fid_ary = (count($ex_fid_ary)) ? $ex_fid_ary : true;
$pagination_url = $this->controller_helper->route('gfksx_thanksforposts_toplist_controller', ['mode' => $mode, 'tslash' => '']);
$pagination_url = $this->controller_helper->route('gfksx_thanksforposts_toplist_controller', ['mode' => $mode]);

if (!$this->auth->acl_gets('u_viewtoplist'))
{
Expand Down Expand Up @@ -204,11 +207,10 @@ public function main()
$end = ($full_post_rating) ? $this->config['topics_per_page'] : $this->config['thanks_number_row_reput'];

$sql_p_array = [
'SELECT' => 'u.user_id, p.post_subject, p.post_id, p.post_time,
p.poster_id, p.post_username, p.topic_id, p.forum_id, p.post_text,
p.bbcode_uid, p.bbcode_bitfield,p.post_attachment, t.post_id, COUNT(*) AS post_thanks',
'FROM' => [$this->thanks_table => 't'],
'SELECT' => 'u.user_id, u.username, u.user_colour,
p.post_subject, p.post_id, p.post_time, p.poster_id, p.post_username,
p.topic_id, p.forum_id, p.post_text, p.bbcode_uid, p.bbcode_bitfield,
p.post_attachment, t.post_id, COUNT(*) AS post_thanks',
'LEFT_JOIN' => [
[
'FROM' => [$this->posts_table => 'p'],
Expand All @@ -227,7 +229,7 @@ public function main()
$sql = $this->db->sql_build_query('SELECT',$sql_p_array);
$result = $this->db->sql_query_limit($sql, $end, $start);

$u_search_post = $this->controller_helper->route('gfksx_thanksforposts_toplist_controller', ['mode' => 'post', 'tslash' => '']);
$u_search_post = $this->controller_helper->route('gfksx_thanksforposts_toplist_controller', ['mode' => 'post']);

if (!$row = $this->db->sql_fetchrow($result))
{
Expand All @@ -239,72 +241,67 @@ public function main()
$bbcode_bitfield = $text_only_message = '';
do
{
// We pre-process some variables here for later usage
$row['post_text'] = censor_text($row['post_text']);
$text_only_message = $row['post_text'];
// We pre-process some variables here for later usage
$row['post_text'] = censor_text($row['post_text']);
$text_only_message = $row['post_text'];

// Make list items visible as such
if ($row['bbcode_uid'])
{
$text_only_message = str_replace('[*:' . $row['bbcode_uid'] . ']', '⋅ ', $text_only_message);
// Make list items visible as such
if ($row['bbcode_uid'])
{
$text_only_message = str_replace('[*:' . $row['bbcode_uid'] . ']', '⋅ ', $text_only_message);

// No BBCode in text only message
strip_bbcode($text_only_message, $row['bbcode_uid']);
}
// No BBCode in text only message
strip_bbcode($text_only_message, $row['bbcode_uid']);
}

if ($return_chars == -1 || utf8_strlen($text_only_message) < ($return_chars + 3))
{
$row['display_text_only'] = false;
$bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);

// Does this post have an attachment? If so, add it to the list
if ($row['post_attachment'] && $this->config['allow_attachments'])
{
$attach_list[$row['forum_id']][] = $row['post_id'];
}
}
else
{
$row['post_text'] = $text_only_message;
$row['display_text_only'] = true;
}
$rowset[] = $row;
unset($text_only_message);
if ($return_chars == -1 || utf8_strlen($text_only_message) < ($return_chars + 3))
{
$row['display_text_only'] = false;
$bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);

// Instantiate BBCode if needed
if ($bbcode_bitfield !== '' && !class_exists('bbcode'))
// Does this post have an attachment? If so, add it to the list
if ($row['post_attachment'] && $this->config['allow_attachments'])
{
include($this->phpbb_root_path . 'includes/bbcode.' . $this->php_ext);
$bbcode = new \bbcode(base64_encode($bbcode_bitfield));
$attach_list[$row['forum_id']][] = $row['post_id'];
}
}
else
{
$row['post_text'] = $text_only_message;
$row['display_text_only'] = true;
}
unset($text_only_message);

// Replace naughty words such as farty pants
$row['post_subject'] = censor_text($row['post_subject']);
// Replace naughty words such as farty pants
$row['post_subject'] = censor_text($row['post_subject']);

if ($row['display_text_only'])
{
$row['post_text'] = get_context($row['post_text'], $words, $return_chars);
$row['post_text'] = bbcode_nl2br($row['post_text']);
}
else
{
// Second parse bbcode here
if ($row['bbcode_bitfield'])
{
$bbcode->bbcode_second_pass($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield']);
}

$row['post_text'] = bbcode_nl2br($row['post_text']);
$row['post_text'] = smiley_text($row['post_text']);
}
if ($row['display_text_only'])
{
$row['post_text'] = bbcode_nl2br($row['post_text']);
}
else
{
$parse_flags = ($row['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
$row['post_text'] = generate_text_for_display($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $parse_flags, false);
}

$post_url = append_sid("{$this->phpbb_root_path}viewtopic.$this->php_ext", 'p=' . $row['post_id'] . '#p' . $row['post_id']);
$rowset[] = $row;
$user_list[] = $row['user_id'];
}
while ($row = $this->db->sql_fetchrow($result));
$this->db->sql_freeresult($result);
unset($row);

$this->user_loader->load_users($user_list);

foreach ($rowset as $row)
{
$this->template->assign_block_vars('toppostrow', [
'MESSAGE' => $this->auth->acl_get('f_read', $row['forum_id']) ? $row['post_text'] : ((!empty($row['forum_id'])) ? $this->language->lang('SORRY_AUTH_READ') : ''),
'POST_DATE' => !empty($row['post_time']) ? $this->user->format_date($row['post_time']) : '',
'POST_ID' => $post_url,
'POST_ID' => append_sid("{$this->phpbb_root_path}viewtopic.$this->php_ext", 'p=' . $row['post_id'] . '#p' . $row['post_id']),
'POST_SUBJECT' => $this->auth->acl_get('f_read', $row['forum_id']) ? $row['post_subject'] : ((!empty($row['forum_id'])) ? $this->language->lang('SORRY_AUTH_READ') : ''),
'POST_AUTHOR' => get_username_string('full', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
'POST_AUTHOR' => $this->user_loader->get_username($row['user_id'], 'full', $row['post_username']),
'POST_REPUT' => round($row['post_thanks'] / ($max_post_thanks / 100), (int) $this->config['thanks_number_digits']) . '%',
'POST_THANKS' => $row['post_thanks'],
'S_THANKS_POST_REPUT_VIEW' => (bool) $this->config['thanks_post_reput_view'],
Expand All @@ -315,8 +312,6 @@ public function main()
'THANKS_REPUT_IMAGE_BACK' => !empty($this->config['thanks_reput_image_back']) ? generate_board_url() . '/' . $this->config['thanks_reput_image_back'] : '',
]);
}
while ($row = $this->db->sql_fetchrow($result));
$this->db->sql_freeresult($result);
}
}

Expand All @@ -326,11 +321,10 @@ public function main()
$end = ($full_topic_rating) ? $this->config['topics_per_page'] : $this->config['thanks_number_row_reput'];

$sql_t_array = [
'SELECT' => 'u.user_id, t.topic_title, t.topic_id, t.topic_time,
t.topic_poster, t.topic_first_poster_name, t.topic_first_poster_colour,
t.forum_id, t.topic_type, t.topic_status, t.poll_start, f.topic_id, COUNT(*) AS topic_thanks',
'FROM' => [$this->thanks_table => 'f'],
'SELECT' => 'u.user_id, u.username, u.user_colour, t.topic_title,
t.topic_id, t.topic_time, t.topic_poster, t.topic_first_poster_name,
t.topic_first_poster_colour, t.forum_id, t.topic_type, t.topic_status,
t.poll_start, f.topic_id, COUNT(*) AS topic_thanks',
'LEFT_JOIN' => [
[
'FROM' => [TOPICS_TABLE => 't'],
Expand All @@ -348,7 +342,7 @@ public function main()

$sql = $this->db->sql_build_query('SELECT',$sql_t_array);
$result = $this->db->sql_query_limit($sql, $end, $start);
$u_search_topic = $this->controller_helper->route('gfksx_thanksforposts_toplist_controller', ['mode' => 'topic', 'tslash' => '']);
$u_search_topic = $this->controller_helper->route('gfksx_thanksforposts_toplist_controller', ['mode' => 'topic']);

if (!$row = $this->db->sql_fetchrow($result))
{
Expand All @@ -375,7 +369,7 @@ public function main()
'TOPIC_FOLDER_IMG_SRC' => $row['forum_id'] ? 'topic_read' : 'announce_read',
'TOPIC_TITLE' => ($this->auth->acl_get('f_read', $row['forum_id'])) ? $row['topic_title'] : ((!empty($row['forum_id'])) ? $this->language->lang('SORRY_AUTH_READ') : ''),
'U_VIEW_TOPIC' => $view_topic_url,
'TOPIC_AUTHOR' => get_username_string('full', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
'TOPIC_AUTHOR' => $this->user_loader->get_username($row['topic_poster'], 'full', $row['topic_first_poster_name'], false, true),
'TOPIC_THANKS' => $row['topic_thanks'],
'TOPIC_REPUT' => round($row['topic_thanks'] / ($max_topic_thanks / 100), (int) $this->config['thanks_number_digits']) . '%',
'S_THANKS_TOPIC_REPUT_VIEW' => (bool) $this->config['thanks_topic_reput_view'],
Expand All @@ -397,8 +391,8 @@ public function main()
$end = ($full_forum_rating) ? (int) $this->config['topics_per_page'] : (int) $this->config['thanks_number_row_reput'];

$sql_f_array = [
'FROM' => [$this->thanks_table => 't'],
'SELECT' => 'f.forum_name, f.forum_id, t.forum_id, COUNT(*) AS forum_thanks',
'FROM' => [$this->thanks_table => 't'],
'LEFT_JOIN' => [
[
'FROM' => [$this->forums_table => 'f'],
Expand All @@ -412,7 +406,7 @@ public function main()

$sql = $this->db->sql_build_query('SELECT',$sql_f_array);
$result = $this->db->sql_query_limit($sql, $end, $start);
$u_search_forum = $this->controller_helper->route('gfksx_thanksforposts_toplist_controller', ['mode' => 'forum', 'tslash' => '']);
$u_search_forum = $this->controller_helper->route('gfksx_thanksforposts_toplist_controller', ['mode' => 'forum']);

if (!$row = $this->db->sql_fetchrow($result))
{
Expand Down Expand Up @@ -474,6 +468,6 @@ public function main()
make_jumpbox(append_sid("{$this->phpbb_root_path}viewforum.$this->php_ext"));

// Send all data to the template file
return $this->controller_helper->render('toplist_body.html', $page_title);
return $this->controller_helper->render('@gfksx_thanksforposts/toplist_body.html', $page_title);
}
}

0 comments on commit 4fd5057

Please sign in to comment.