Fork me on GitHub

Comment by quy (2013-11-21 15:40:03)

Edited by quy (2014-03-16 19:05:17)

 1:  1:
 Revised code for performance gain when there are no tracked forums/topics: Revised code for performance gain when there are no tracked forums/topics:
 [code]  [code]
 if (!$pun_user['is_guest'])  if (!$pun_user['is_guest'])
 {  {
  $result = $db->query('SELECT f.id, f.last_post FROM '.$db->prefix.'forums AS f LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND f.last_post>'.$pun_user['last_visit']) or error('Unable to fetch forum list', __FILE__, __LINE__, $db->error());   $result = $db->query('SELECT f.id, f.last_post FROM '.$db->prefix.'forums AS f LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND f.last_post>'.$pun_user['last_visit']) or error('Unable to fetch forum list', __FILE__, __LINE__, $db->error());
   
  if ($db->num_rows($result))   if ($db->num_rows($result))
  {   {
- $forum_ids = $new_topics = array(); + $forums = $new_topics = array();
  $tracked_topics = get_tracked_topics();   $tracked_topics = get_tracked_topics();
   
  while ($cur_forum = $db->fetch_assoc($result))   while ($cur_forum = $db->fetch_assoc($result))
  {   {
  if (!isset($tracked_topics['forums'][$cur_forum['id']]) || $tracked_topics['forums'][$cur_forum['id']] < $cur_forum['last_post'])   if (!isset($tracked_topics['forums'][$cur_forum['id']]) || $tracked_topics['forums'][$cur_forum['id']] < $cur_forum['last_post'])
- $forum_ids[] = $cur_forum['id']; + $forums[$cur_forum['id']] = $cur_forum['last_post'];
  }   }
   
- if (!empty($forum_ids)) + if (!empty($forums))
  {   {
  if (empty($tracked_topics['topics']))   if (empty($tracked_topics['topics']))
- $new_topics = $forum_ids; + $new_topics = $forums;
  else   else
  {   {
- $result = $db->query('SELECT id, last_post, forum_id FROM '.$db->prefix.'topics WHERE forum_id IN('.implode(',', $forum_ids).') AND last_post>'.$pun_user['last_visit'].' AND moved_to IS NULL') or error('Unable to fetch new topics', __FILE__, __LINE__, $db->error()); + $result = $db->query('SELECT id, last_post, forum_id FROM '.$db->prefix.'topics WHERE forum_id IN('.implode(',', array_keys($forums)).') AND last_post>'.$pun_user['last_visit'].' AND moved_to IS NULL') or error('Unable to fetch new topics', __FILE__, __LINE__, $db->error());
   
  while ($cur_topic = $db->fetch_assoc($result))   while ($cur_topic = $db->fetch_assoc($result))
  {   {
- if (!in_array($cur_topic['forum_id']$new_topics) && (!isset($tracked_topics['topics'][$cur_topic['id']]) || $tracked_topics['topics'][$cur_topic['id']] < $cur_topic['last_post']))  + if (!isset($new_topics[$cur_topic['forum_id']]) && (!isset($tracked_topics['forums'][$cur_topic['forum_id']]) || $tracked_topics['forums'][$cur_topic['forum_id']] < $forums[$cur_topic['forum_id']]) && (!isset($tracked_topics['topics'][$cur_topic['id']]) || $tracked_topics['topics'][$cur_topic['id']] < $cur_topic['last_post']))  
- $new_topics[] = $cur_topic['forum_id']; + $new_topics[$cur_topic['forum_id']] = $forums[$cur_topic['forum_id']];
  }   }
  }   }
  }   }
  }   }
 }  }
 [/code] [/code]
 [code]  [code]
- if (in_array($cur_forum['fid'], $new_topics)) + if (isset($new_topics[$cur_forum['fid']]))
  {   {
  $item_status .= ' inew';   $item_status .= ' inew';
  $forum_field_new = '<span class="newtext">[ <a href="search.php?action=show_new&fid='.$cur_forum['fid'].'">'.$lang_common['New posts'].'</a> ]</span>';   $forum_field_new = '<span class="newtext">[ <a href="search.php?action=show_new&fid='.$cur_forum['fid'].'">'.$lang_common['New posts'].'</a> ]</span>';
  $icon_type = 'icon icon-new';   $icon_type = 'icon icon-new';
  }   }
 [/code] [/code]