| Conditions | 42 |
| Paths | > 20000 |
| Total Lines | 230 |
| Code Lines | 120 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 128 | public function display_similar_topics($topic_data) |
||
| 129 | { |
||
| 130 | // If the forum should not display similar topics, no need to continue |
||
| 131 | if ($topic_data['similar_topics_hide']) |
||
| 132 | { |
||
| 133 | return; |
||
| 134 | } |
||
| 135 | |||
| 136 | $topic_title = $this->clean_topic_title($topic_data['topic_title']); |
||
| 137 | |||
| 138 | // If the cleaned up topic_title is empty, no need to continue |
||
| 139 | if (empty($topic_title)) |
||
| 140 | { |
||
| 141 | return; |
||
| 142 | } |
||
| 143 | |||
| 144 | // Get stored sensitivity value and divide by 10. In query it should be a number between 0.0 to 1.0. |
||
| 145 | $sensitivity = $this->config->offsetExists('similar_topics_sense') ? $this->config['similar_topics_sense'] / 10 : '0.5'; |
||
| 146 | |||
| 147 | $select = $where = $unix_ts = ''; |
||
| 148 | if ($this->is_postgres()) |
||
| 149 | { |
||
| 150 | $ts_query_text = $this->db->sql_escape(str_replace(' ', '|', $topic_title)); |
||
| 151 | $ts_name = $this->config['similar_topics_postgres_ts_name']; |
||
| 152 | $select = "f.forum_id, f.forum_name, t.*, |
||
| 153 | ts_rank_cd(to_tsvector('$ts_name', t.topic_title), '$ts_query_text', 32) AS score"; |
||
| 154 | $where = "ts_rank_cd(to_tsvector('$ts_name', t.topic_title), '$ts_query_text', 32) >= " . (float) $sensitivity ; |
||
| 155 | $unix_ts = 'extract(epoch from current_timestamp)::integer'; |
||
| 156 | } else { |
||
| 157 | $select = "f.forum_id, f.forum_name, t.*, |
||
| 158 | MATCH (t.topic_title) AGAINST ('" . $this->db->sql_escape($topic_title) . "') AS score"; |
||
| 159 | $where = "MATCH (t.topic_title) AGAINST ('" . $this->db->sql_escape($topic_title) . "') >= " . (float) $sensitivity; |
||
| 160 | $unix_ts = 'UNIX_TIMESTAMP()'; |
||
| 161 | } |
||
| 162 | |||
| 163 | // Similar Topics query |
||
| 164 | $sql_array = array( |
||
| 165 | 'SELECT' => $select, |
||
| 166 | 'FROM' => array( |
||
| 167 | TOPICS_TABLE => 't', |
||
| 168 | ), |
||
| 169 | 'LEFT_JOIN' => array( |
||
| 170 | array( |
||
| 171 | 'FROM' => array(FORUMS_TABLE => 'f'), |
||
| 172 | 'ON' => 'f.forum_id = t.forum_id', |
||
| 173 | ), |
||
| 174 | ), |
||
| 175 | 'WHERE' => $where . ' |
||
| 176 | AND t.topic_status <> ' . ITEM_MOVED . ' |
||
| 177 | AND t.topic_visibility = ' . ITEM_APPROVED . ' |
||
| 178 | AND t.topic_time > (' . $unix_ts . ' - ' . $this->config['similar_topics_time'] . ') |
||
| 179 | AND t.topic_id <> ' . (int) $topic_data['topic_id'], |
||
| 180 | ); |
||
| 181 | |||
| 182 | // Add topic tracking data to the query (only if query caching is off) |
||
| 183 | if ($this->user->data['is_registered'] && $this->config['load_db_lastread'] && !$this->config['similar_topics_cache']) |
||
| 184 | { |
||
| 185 | $sql_array['LEFT_JOIN'][] = array('FROM' => array(TOPICS_TRACK_TABLE => 'tt'), 'ON' => 'tt.topic_id = t.topic_id AND tt.user_id = ' . $this->user->data['user_id']); |
||
| 186 | $sql_array['LEFT_JOIN'][] = array('FROM' => array(FORUMS_TRACK_TABLE => 'ft'), 'ON' => 'ft.forum_id = f.forum_id AND ft.user_id = ' . $this->user->data['user_id']); |
||
| 187 | $sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as f_mark_time'; |
||
| 188 | } |
||
| 189 | else if ($this->config['load_anon_lastread'] || $this->user->data['is_registered']) |
||
| 190 | { |
||
| 191 | // Cookie based tracking copied from search.php |
||
| 192 | $tracking_topics = $this->request->variable($this->config['cookie_name'] . '_track', '', true, \phpbb\request\request_interface::COOKIE); |
||
| 193 | $tracking_topics = $tracking_topics ? tracking_unserialize($tracking_topics) : array(); |
||
| 194 | } |
||
| 195 | |||
| 196 | // We need to exclude passworded forums so we do not leak the topic title |
||
| 197 | $passworded_forums = $this->user->get_passworded_forums(); |
||
| 198 | |||
| 199 | // See if the admin set this forum to only search a specific group of other forums, and include them |
||
| 200 | if (!empty($topic_data['similar_topic_forums'])) |
||
| 201 | { |
||
| 202 | // Remove any passworded forums from this group of forums we will be searching |
||
| 203 | $included_forums = array_diff(json_decode($topic_data['similar_topic_forums'], true), $passworded_forums); |
||
| 204 | // if there's nothing left to display (user has no access to the forums we want to search) |
||
| 205 | if (empty($included_forums)) |
||
| 206 | { |
||
| 207 | return; |
||
| 208 | } |
||
| 209 | |||
| 210 | $sql_array['WHERE'] .= ' AND ' . $this->db->sql_in_set('f.forum_id', $included_forums); |
||
| 211 | } |
||
| 212 | // Otherwise exclude any ignored forums |
||
| 213 | else |
||
| 214 | { |
||
| 215 | // Remove any passworded forums |
||
| 216 | if (count($passworded_forums)) |
||
| 217 | { |
||
| 218 | $sql_array['WHERE'] .= ' AND ' . $this->db->sql_in_set('f.forum_id', $passworded_forums, true); |
||
| 219 | } |
||
| 220 | |||
| 221 | $sql_array['WHERE'] .= ' AND f.similar_topics_ignore = 0'; |
||
| 222 | } |
||
| 223 | |||
| 224 | $sql_array['ORDER_BY'] = 'score DESC, t.topic_time DESC'; |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Event to modify the sql_array for similar topics |
||
| 228 | * |
||
| 229 | * @event vse.similartopics.get_topic_data |
||
| 230 | * @var array sql_array SQL array to get similar topics data |
||
| 231 | * @since 1.3.0 |
||
| 232 | */ |
||
| 233 | $vars = array('sql_array'); |
||
| 234 | extract($this->dispatcher->trigger_event('vse.similartopics.get_topic_data', compact($vars))); |
||
| 235 | |||
| 236 | $rowset = array(); |
||
| 237 | |||
| 238 | $sql = $this->db->sql_build_query('SELECT', $sql_array); |
||
| 239 | $result = $this->db->sql_query_limit($sql, $this->config['similar_topics_limit'], 0, $this->config['similar_topics_cache']); |
||
| 240 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 241 | { |
||
| 242 | $rowset[(int) $row['topic_id']] = $row; |
||
| 243 | } |
||
| 244 | $this->db->sql_freeresult($result); |
||
| 245 | |||
| 246 | // Grab icons |
||
| 247 | $icons = $this->cache->obtain_icons(); |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Modify the rowset data for similar topics |
||
| 251 | * |
||
| 252 | * @event vse.similartopics.modify_rowset |
||
| 253 | * @var array rowset Array with the search results data |
||
| 254 | * @since 1.4.2 |
||
| 255 | */ |
||
| 256 | $vars = array('rowset'); |
||
| 257 | extract($this->dispatcher->trigger_event('vse.similartopics.modify_rowset', compact($vars))); |
||
| 258 | |||
| 259 | foreach ($rowset as $row) |
||
| 260 | { |
||
| 261 | $similar_forum_id = (int) $row['forum_id']; |
||
| 262 | $similar_topic_id = (int) $row['topic_id']; |
||
| 263 | |||
| 264 | if ($this->auth->acl_get('f_read', $similar_forum_id)) |
||
| 265 | { |
||
| 266 | // Get topic tracking info |
||
| 267 | if ($this->user->data['is_registered'] && $this->config['load_db_lastread'] && !$this->config['similar_topics_cache']) |
||
| 268 | { |
||
| 269 | $topic_tracking_info = get_topic_tracking($similar_forum_id, $similar_topic_id, $rowset, array($similar_forum_id => $row['f_mark_time'])); |
||
| 270 | } |
||
| 271 | else if ($this->config['load_anon_lastread'] || $this->user->data['is_registered']) |
||
| 272 | { |
||
| 273 | $topic_tracking_info = get_complete_topic_tracking($similar_forum_id, $similar_topic_id); |
||
| 274 | |||
| 275 | if (!$this->user->data['is_registered']) |
||
| 276 | { |
||
| 277 | $this->user->data['user_lastmark'] = isset($tracking_topics['l']) ? ((int) base_convert($tracking_topics['l'], 36, 10) + (int) $this->config['board_startdate']) : 0; |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | // Replies |
||
| 282 | $replies = $this->content_visibility->get_count('topic_posts', $row, $similar_forum_id) - 1; |
||
| 283 | |||
| 284 | // Get folder img, topic status/type related information |
||
| 285 | $folder_img = $folder_alt = $topic_type = ''; |
||
| 286 | $unread_topic = isset($topic_tracking_info[$similar_topic_id]) && $row['topic_last_post_time'] > $topic_tracking_info[$similar_topic_id]; |
||
| 287 | topic_status($row, $replies, $unread_topic, $folder_img, $folder_alt, $topic_type); |
||
| 288 | |||
| 289 | $topic_unapproved = $row['topic_visibility'] == ITEM_UNAPPROVED && $this->auth->acl_get('m_approve', $similar_forum_id); |
||
| 290 | $posts_unapproved = $row['topic_visibility'] == ITEM_APPROVED && $row['topic_posts_unapproved'] && $this->auth->acl_get('m_approve', $similar_forum_id); |
||
| 291 | $u_mcp_queue = ($topic_unapproved || $posts_unapproved) ? append_sid("{$this->root_path}mcp.{$this->php_ext}", 'i=queue&mode=' . ($topic_unapproved ? 'approve_details' : 'unapproved_posts') . "&t=$similar_topic_id", true, $this->user->session_id) : ''; |
||
| 292 | |||
| 293 | $base_url = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $similar_forum_id . '&t=' . $similar_topic_id); |
||
| 294 | |||
| 295 | $topic_row = array( |
||
| 296 | 'TOPIC_AUTHOR_FULL' => get_username_string('full', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']), |
||
| 297 | 'FIRST_POST_TIME' => $this->user->format_date($row['topic_time']), |
||
| 298 | 'LAST_POST_TIME' => $this->user->format_date($row['topic_last_post_time']), |
||
| 299 | 'LAST_POST_AUTHOR_FULL' => get_username_string('full', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']), |
||
| 300 | |||
| 301 | 'TOPIC_REPLIES' => $replies, |
||
| 302 | 'TOPIC_VIEWS' => $row['topic_views'], |
||
| 303 | 'TOPIC_TITLE' => censor_text($row['topic_title']), |
||
| 304 | 'FORUM_TITLE' => $row['forum_name'], |
||
| 305 | |||
| 306 | 'TOPIC_IMG_STYLE' => $folder_img, |
||
| 307 | 'TOPIC_FOLDER_IMG' => $this->user->img($folder_img, $folder_alt), |
||
| 308 | 'TOPIC_FOLDER_IMG_ALT' => $this->user->lang($folder_alt), |
||
| 309 | |||
| 310 | 'TOPIC_ICON_IMG' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['img'] : '', |
||
| 311 | 'TOPIC_ICON_IMG_WIDTH' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['width'] : '', |
||
| 312 | 'TOPIC_ICON_IMG_HEIGHT' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['height'] : '', |
||
| 313 | 'ATTACH_ICON_IMG' => ($this->auth->acl_get('u_download') && $this->auth->acl_get('f_download', $similar_forum_id) && $row['topic_attachment']) ? $this->user->img('icon_topic_attach', $this->user->lang('TOTAL_ATTACHMENTS')) : '', |
||
| 314 | 'UNAPPROVED_IMG' => ($topic_unapproved || $posts_unapproved) ? $this->user->img('icon_topic_unapproved', $topic_unapproved ? 'TOPIC_UNAPPROVED' : 'POSTS_UNAPPROVED') : '', |
||
| 315 | |||
| 316 | 'S_UNREAD_TOPIC' => $unread_topic, |
||
| 317 | 'S_TOPIC_REPORTED' => !empty($row['topic_reported']) && $this->auth->acl_get('m_report', $similar_forum_id), |
||
| 318 | 'S_TOPIC_UNAPPROVED' => $topic_unapproved, |
||
| 319 | 'S_POSTS_UNAPPROVED' => $posts_unapproved, |
||
| 320 | 'S_HAS_POLL' => (bool) $row['poll_start'], |
||
| 321 | |||
| 322 | 'U_NEWEST_POST' => append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $similar_forum_id . '&t=' . $similar_topic_id . '&view=unread') . '#unread', |
||
| 323 | 'U_LAST_POST' => append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $similar_forum_id . '&t=' . $similar_topic_id . '&p=' . $row['topic_last_post_id']) . '#p' . $row['topic_last_post_id'], |
||
| 324 | 'U_VIEW_TOPIC' => append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $similar_forum_id . '&t=' . $similar_topic_id), |
||
| 325 | 'U_VIEW_FORUM' => append_sid("{$this->root_path}viewforum.{$this->php_ext}", 'f=' . $similar_forum_id), |
||
| 326 | 'U_MCP_REPORT' => append_sid("{$this->root_path}mcp.{$this->php_ext}", 'i=reports&mode=reports&f=' . $similar_forum_id . '&t=' . $similar_topic_id, true, $this->user->session_id), |
||
| 327 | 'U_MCP_QUEUE' => $u_mcp_queue, |
||
| 328 | ); |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Event to modify the similar topics template block |
||
| 332 | * |
||
| 333 | * @event vse.similartopics.modify_topicrow |
||
| 334 | * @var array row Array with similar topic data |
||
| 335 | * @var array topic_row Template block array |
||
| 336 | * @since 1.3.0 |
||
| 337 | */ |
||
| 338 | $vars = array('row', 'topic_row'); |
||
| 339 | extract($this->dispatcher->trigger_event('vse.similartopics.modify_topicrow', compact($vars))); |
||
| 340 | |||
| 341 | $this->template->assign_block_vars('similar', $topic_row); |
||
| 342 | |||
| 343 | $this->pagination->generate_template_pagination($base_url, 'similar.pagination', 'start', $replies + 1, $this->config['posts_per_page'], 1, true, true); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | $this->user->add_lang_ext('vse/similartopics', 'similar_topics'); |
||
| 348 | |||
| 349 | $this->template->assign_vars(array( |
||
| 350 | 'L_SIMILAR_TOPICS' => $this->user->lang('SIMILAR_TOPICS'), |
||
| 351 | 'NEWEST_POST_IMG' => $this->user->img('icon_topic_newest', 'VIEW_NEWEST_POST'), |
||
| 352 | 'LAST_POST_IMG' => $this->user->img('icon_topic_latest', 'VIEW_LATEST_POST'), |
||
| 353 | 'REPORTED_IMG' => $this->user->img('icon_topic_reported', 'TOPIC_REPORTED'), |
||
| 354 | 'POLL_IMG' => $this->user->img('icon_topic_poll', 'TOPIC_POLL'), |
||
| 355 | 'S_PST_BRANCH' => phpbb_version_compare(max($this->config['phpbb_version'], PHPBB_VERSION), '3.2.0-dev', '<') ? '31x' : '32x', |
||
| 356 | )); |
||
| 357 | } |
||
| 358 | |||
| 483 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.