Skip to content

Commit 0cc60aa

Browse files
committed
1.2.0-b3 Optimized the sql-query for first unread post data.
* SQL query string modified to retrieve unread posts data from multiple topics * New function `get_first_unread_post_data()` for db-query *Added new var `topic_unread_post_counter` to `$row` * Updated version number in the language files
1 parent 90c1aa2 commit 0cc60aa

6 files changed

Lines changed: 83 additions & 64 deletions

File tree

imcger/recenttopicsng/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"type": "phpbb-extension",
44
"description": "Adds a list with a number of recent topics to the board index.",
55
"homepage": "https://github.com/IMC-GER/RecentTopicsNG",
6-
"version": "1.2.0-b2",
6+
"version": "1.2.0-b3",
77
"time": "2026-04-04",
88
"license": "GPL-2.0-only",
99
"authors": [

imcger/recenttopicsng/core/rtng_functions.php

Lines changed: 77 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ private function get_allowed_topics_sql(array $excluded_topics, int $min_topic_l
340340
],
341341
[
342342
'FROM' => [TOPICS_POSTED_TABLE => 'tp', ],
343-
'ON' => 'tp.topic_id = t.topic_id AND tp.user_id = ' . (int) $this->user->data['user_id'],
343+
'ON' => 'tp.topic_id = t.topic_id AND tp.user_id = ' . (int) $this->user->data['user_id'],
344344
],
345345
],
346346
'WHERE' => $this->db->sql_in_set('t.topic_id', $excluded_topics, true) . '
@@ -455,82 +455,54 @@ private function fill_template(array $icons, string $tpl_loopname, array $topic_
455455
$vars = ['topic_list', 'rowset'];
456456
extract($this->dispatcher->trigger_event('imcger.recenttopicsng.modify_topics_list', compact($vars)));
457457

458-
$s_type_switch = 0;
458+
// Get author, posttime, id and title of first unread post in topic
459+
$first_unread_post_data = [];
460+
if ($this->user->data['is_registered'] && $this->config['load_db_lastread'] && $this->config['rtng_load_first_unrd_post'])
461+
{
462+
$first_unread_post_data = $this->get_first_unread_post_data($topic_list);
463+
}
464+
465+
$s_type_switch = false;
459466
foreach ($rowset as $row)
460467
{
461-
$first_unread = [];
462468
$topic_id = $row['topic_id'];
463469
$forum_id = $row['forum_id'];
464470
$unread_topic = false;
465471
$replies = $this->content_visibility->get_count('topic_posts', $row, $forum_id) - 1;
466-
$s_type_switch_test = ($row['topic_type'] == POST_ANNOUNCE || $row['topic_type'] == POST_GLOBAL) ? 1 : 0;
472+
$s_type_switch_test = ($row['topic_type'] == POST_ANNOUNCE || $row['topic_type'] == POST_GLOBAL) ? true : false;
467473
$disp_topic_title = $this->user_setting['user_rtng_disp_last_post'] ? 'last_post' : 'first_post';
468474

469-
if ($this->user->data['is_registered'] && $this->config['load_db_lastread'] && $this->config['rtng_load_first_unrd_post'])
475+
if (isset($first_unread_post_data[$topic_id]))
470476
{
471-
// Get author, posttime, id and title of first unread post in topic
472-
$sql_array = [
473-
'SELECT' => 'p.poster_id, u.username, u.user_colour, p.post_id, p.post_subject, p.post_time',
474-
'FROM' => [POSTS_TABLE => 'p', ],
475-
'LEFT_JOIN' => [
476-
[
477-
'FROM' => [TOPICS_TABLE => 't', ],
478-
'ON' => "t.topic_id = p.topic_id",
479-
],
480-
[
481-
'FROM' => [TOPICS_TRACK_TABLE => 'tt', ],
482-
'ON' => "tt.user_id = {$this->user->data['user_id']}
483-
AND tt.topic_id = p.topic_id",
484-
],
485-
[
486-
'FROM' => [FORUMS_TRACK_TABLE => 'ft', ],
487-
'ON' => "ft.user_id = {$this->user->data['user_id']}
488-
AND ft.forum_id = t.forum_id",
489-
],
490-
[
491-
'FROM' => [USERS_TABLE => 'u', ],
492-
'ON' => 'u.user_id = p.poster_id',
493-
],
494-
],
495-
'WHERE' => "p.topic_id = $topic_id
496-
AND p.post_time > COALESCE(tt.mark_time, ft.mark_time, {$this->user->data['user_lastmark']}, 0)
497-
AND p.forum_id = $forum_id",
498-
'ORDER_BY' => 'p.post_time ASC, p.post_id ASC',
499-
];
500-
501-
$sql = $this->db->sql_build_query('SELECT', $sql_array);
502-
$result = $this->db->sql_query_limit($sql, 1);
503-
$first_unread = $this->db->sql_fetchrow($result);
504-
$this->db->sql_freeresult($result);
505-
506-
$unread_topic = !empty($first_unread['post_id']);
507-
508-
if ($this->user_setting['user_rtng_disp_first_unrd_post'] && $unread_topic)
477+
$unread_topic = true;
478+
479+
if ($this->user_setting['user_rtng_disp_first_unrd_post'])
509480
{
510481
$disp_topic_title = 'first_unread_post';
511482

512-
$first_unread_post_author = get_username_string('username', $first_unread['poster_id'], $first_unread['username'], $first_unread['user_colour']);
513-
$first_unread_post_author_color = get_username_string('colour', $first_unread['poster_id'], $first_unread['username'], $first_unread['user_colour']);
514-
$first_unread_post_author_full = get_username_string('full', $first_unread['poster_id'], $first_unread['username'], $first_unread['user_colour']);
515-
$first_unread_post_time = $this->user->format_date($first_unread['post_time']);
516-
$u_first_unread_post_author = get_username_string('profile', $first_unread['poster_id'], $first_unread['username'], $first_unread['user_colour']);
483+
$first_unread_post_author = get_username_string('username', $first_unread_post_data[$topic_id]['poster_id'], $first_unread_post_data[$topic_id]['username'], $first_unread_post_data[$topic_id]['user_colour']);
484+
$first_unread_post_author_color = get_username_string('colour', $first_unread_post_data[$topic_id]['poster_id'], $first_unread_post_data[$topic_id]['username'], $first_unread_post_data[$topic_id]['user_colour']);
485+
$first_unread_post_author_full = get_username_string('full', $first_unread_post_data[$topic_id]['poster_id'], $first_unread_post_data[$topic_id]['username'], $first_unread_post_data[$topic_id]['user_colour']);
486+
$first_unread_post_time = $this->user->format_date($first_unread_post_data[$topic_id]['post_time']);
487+
$u_first_unread_post_author = get_username_string('profile', $first_unread_post_data[$topic_id]['poster_id'], $first_unread_post_data[$topic_id]['username'], $first_unread_post_data[$topic_id]['user_colour']);
517488
}
518489
}
519490
else
520491
{
521492
$unread_topic = isset($topic_tracking_info[$forum_id][$row['topic_id']]) && ($row['topic_last_post_time'] > $topic_tracking_info[$forum_id][$row['topic_id']]);
522493
}
523494

524-
$row['topic_first_unread_post_id'] = $first_unread['post_id'] ?? '';
525-
$row['topic_first_unread_poster_id'] = $first_unread['poster_id'] ?? '';
526-
$row['topic_first_unread_poster_name'] = $first_unread['username'] ?? '';
527-
$row['topic_first_unread_poster_colour'] = $first_unread['user_colour'] ?? '';
528-
$row['topic_first_unread_post_subject'] = $first_unread['post_subject'] ?? '';
529-
$row['topic_first_unread_post_time'] = $first_unread['post_time'] ?? '';
495+
$row['topic_first_unread_post_id'] = $first_unread_post_data[$topic_id]['post_id'] ?? '';
496+
$row['topic_first_unread_poster_id'] = $first_unread_post_data[$topic_id]['poster_id'] ?? '';
497+
$row['topic_first_unread_poster_name'] = $first_unread_post_data[$topic_id]['username'] ?? '';
498+
$row['topic_first_unread_poster_colour'] = $first_unread_post_data[$topic_id]['user_colour'] ?? '';
499+
$row['topic_first_unread_post_subject'] = $first_unread_post_data[$topic_id]['post_subject'] ?? '';
500+
$row['topic_first_unread_post_time'] = $first_unread_post_data[$topic_id]['post_time'] ?? '';
501+
$row['topic_unread_post_counter'] = $first_unread_post_data[$topic_id]['unread_post_counter'] ?? 0;
530502

531503
$view_topic_url = append_sid("{$this->root_path}viewtopic.$this->phpEx", 't=' . $topic_id);
532504
$view_last_post_url = append_sid("{$this->root_path}viewtopic.$this->phpEx", 'p=' . $row['topic_last_post_id'] . '#p' . $row['topic_last_post_id']);
533-
$view_first_unread_post_url = !empty($first_unread['post_id']) ? append_sid("{$this->root_path}viewtopic.$this->phpEx", 'p=' . $first_unread['post_id'] . '#p' . $first_unread['post_id']) : '';
505+
$view_first_unread_post_url = !empty($first_unread_post_data[$topic_id]['post_id']) ? append_sid("{$this->root_path}viewtopic.$this->phpEx", 'p=' . $first_unread_post_data[$topic_id]['post_id'] . '#p' . $first_unread_post_data[$topic_id]['post_id']) : '';
534506
$view_report_url = append_sid("{$this->root_path}mcp.$this->phpEx", 'i=reports&mode=reports&t=' . $topic_id, true, $this->user->session_id);
535507
$view_forum_url = append_sid("{$this->root_path}viewforum.$this->phpEx", 'f=' . $forum_id);
536508
$topic_unapproved = ($row['topic_visibility'] == ITEM_UNAPPROVED && $this->auth->acl_get('m_approve', $forum_id));
@@ -556,6 +528,7 @@ private function fill_template(array $icons, string $tpl_loopname, array $topic_
556528
if ($this->config['rtng_parents'])
557529
{
558530
$forum_parents = get_forum_parents($row);
531+
$parent_forums = [];
559532
foreach ($forum_parents as $parent_id => $data)
560533
{
561534
$parent_forums[] = [
@@ -646,7 +619,7 @@ private function fill_template(array $icons, string $tpl_loopname, array $topic_
646619
'row',
647620
'tpl_ary',
648621
's_type_switch',
649-
's_type_switch_test'
622+
's_type_switch_test',
650623
];
651624
extract($this->dispatcher->trigger_event('imcger.recenttopicsng.modify_tpl_ary', compact($vars)));
652625

@@ -703,7 +676,54 @@ private function fill_template(array $icons, string $tpl_loopname, array $topic_
703676
} // topics found
704677
}
705678

706-
public function validate_start($start, $per_page, $num_items)
679+
public function get_first_unread_post_data(array $topic_list): array
680+
{
681+
// Get author, posttime, id and title of first unread post in topic
682+
$sql_array = [
683+
'SELECT' => 'p.topic_id, p.poster_id, u.username, u.user_colour,
684+
p.post_id, p.post_subject, p.post_time, COUNT(p.topic_id) AS unread_post_counter',
685+
'FROM' => [POSTS_TABLE => 'p', ],
686+
'LEFT_JOIN' => [
687+
[
688+
'FROM' => [TOPICS_TABLE => 't', ],
689+
'ON' => "t.topic_id = p.topic_id",
690+
],
691+
[
692+
'FROM' => [TOPICS_TRACK_TABLE => 'tt', ],
693+
'ON' => "tt.user_id = {$this->user->data['user_id']}
694+
AND tt.topic_id = p.topic_id",
695+
],
696+
[
697+
'FROM' => [FORUMS_TRACK_TABLE => 'ft', ],
698+
'ON' => "ft.user_id = {$this->user->data['user_id']}
699+
AND ft.forum_id = t.forum_id",
700+
],
701+
[
702+
'FROM' => [USERS_TABLE => 'u', ],
703+
'ON' => 'u.user_id = p.poster_id',
704+
],
705+
],
706+
'WHERE' => $this->db->sql_in_set('p.topic_id', $topic_list) . "
707+
AND p.post_time > COALESCE(tt.mark_time, ft.mark_time, {$this->user->data['user_lastmark']}, 0)",
708+
'GROUP_BY' => 'p.topic_id',
709+
'ORDER_BY' => 'p.post_time ASC, p.post_id ASC',
710+
];
711+
712+
$sql = $this->db->sql_build_query('SELECT', $sql_array);
713+
$result = $this->db->sql_query($sql);
714+
$row_set = $this->db->sql_fetchrowset($result);
715+
$this->db->sql_freeresult($result);
716+
717+
$first_unread_posts = [];
718+
foreach ($row_set as $row)
719+
{
720+
$first_unread_posts[$row['topic_id']] = $row;
721+
}
722+
723+
return $first_unread_posts;
724+
}
725+
726+
public function validate_start(int $start, int $per_page, int $num_items): int
707727
{
708728
$start = $start >= $num_items ? $num_items - 1 : $start;
709729
$start = intdiv($start, $per_page) * $per_page;

imcger/recenttopicsng/docs/CHANGELOG.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
## Changelog Recent Topics NG V1.2.0-b2
1+
## Changelog Recent Topics NG V1.2.0-b3
22
This is a non-exhaustive (but still near complete) changelog for Recent Topics NG 1.x including release candidate versions.
33

44
#### Changes since V1.1.0 (04/04/2026)
55
- [Change] Template vars for topic parents moved into `$tpl_ary`.
66
- [Change] Improve the code for the pagination URL parameters.
77
- [Chamge] Updated the `switch()` macro to the version of TC 1.3.
88
- [Change] Optimized the Twig code in the ACP & UCP module based on TC 1.3.
9+
- [Change] Optimized the sql-query for first unread post data.
910
- [Feature] Added vars in event `modify_tpl_ary`. `$s_type_switch` and `$s_type_switch_test`.
1011
- [Feature] Added event `topic_row_after `in `rtng_functions\fill_template`.
1112

12-
1313
#### Changes since V1.0.1 (08/03/2026)
1414
- [Change] Classes have been switched to constructor property promotion.
1515
- [Change] Minor changes have been made to the language files.
@@ -38,7 +38,6 @@ This is a non-exhaustive (but still near complete) changelog for Recent Topics N
3838
- [Fixed] The red fa-file icon is not removed when topics are marked as unread.
3939
- [Fixed] Displays locked passworded forums.
4040

41-
4241
#### Changes since V1.0.0 (27/09/2025)
4342
- [Change] Removed unnecessary NCO.
4443
- [Fixed] Ensured that the correct variable type is always returned.

imcger/recenttopicsng/language/de/info_acp_rtng.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
$lang = array_merge($lang, [
4242
// Language pack author
4343
'RTNG_LANG_DESC' => 'Deutsch (Du)',
44-
'RTNG_LANG_VER' => '1.1.0',
44+
'RTNG_LANG_VER' => '1.2.0',
4545
'RTNG_LANG_AUTHOR' => 'IMC-GER / LukeWCS',
4646

4747
// ACP forums

imcger/recenttopicsng/language/de_x_sie/info_acp_rtng.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
$lang = array_merge($lang, [
4242
// Language pack author
4343
'RTNG_LANG_DESC' => 'Deutsch (Sie)',
44-
'RTNG_LANG_VER' => '1.1.0',
44+
'RTNG_LANG_VER' => '1.2.0',
4545
'RTNG_LANG_AUTHOR' => 'IMC-GER / LukeWCS',
4646

4747
// ACP forums

imcger/recenttopicsng/language/en/info_acp_rtng.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
$lang = array_merge($lang, [
4242
// Language pack author
4343
'RTNG_LANG_DESC' => 'English',
44-
'RTNG_LANG_VER' => '1.1.0',
44+
'RTNG_LANG_VER' => '1.2.0',
4545
'RTNG_LANG_AUTHOR' => 'IMC-GER / LukeWCS',
4646

4747
// ACP forums

0 commit comments

Comments
 (0)