/**
  * Merge posts within thread
  *
  * @param array $pids Post IDs to be merged
  * @param int $tid Thread ID (Set to 0 if posts from multiple threads are selected)
  * @return int ID of the post into which all other posts are merged
  */
 function merge_posts($pids, $tid = 0, $sep = "new_line")
 {
     global $db, $plugins;
     // Make sure we only have valid values
     $pids = array_map('intval', $pids);
     if (empty($pids) || count($pids) < 2) {
         return false;
     }
     $pidin = implode(',', $pids);
     $attachment_count = 0;
     $first = 1;
     // Get the messages to be merged
     $query = $db->query("\n\t\t\tSELECT p.pid, p.uid, p.fid, p.tid, p.visible, p.message, t.visible AS threadvisible, t.replies AS threadreplies, t.firstpost AS threadfirstpost, t.unapprovedposts AS threadunapprovedposts, COUNT(a.aid) AS attachmentcount\n\t\t\tFROM " . TABLE_PREFIX . "posts p\n\t\t\tLEFT JOIN " . TABLE_PREFIX . "threads t ON (t.tid=p.tid)\n\t\t\tLEFT JOIN " . TABLE_PREFIX . "attachments a ON (a.pid=p.pid AND a.visible=1)\n\t\t\tWHERE p.pid IN({$pidin})\n\t\t\tGROUP BY p.pid\n\t\t\tORDER BY p.dateline ASC\n\t\t");
     $message = '';
     $threads = $forum_counters = $thread_counters = $user_counters = array();
     while ($post = $db->fetch_array($query)) {
         $threads[$post['tid']] = $post['tid'];
         if (!isset($thread_counters[$post['tid']])) {
             $thread_counters[$post['tid']] = array('replies' => 0, 'unapprovedposts' => 0, 'deletedposts' => 0, 'attachmentcount' => 0);
         }
         if ($first == 1) {
             // all posts will be merged into this one
             $masterpid = $post['pid'];
             $message = $post['message'];
             $fid = $post['fid'];
             $mastertid = $post['tid'];
             $first = 0;
         } else {
             // these are the selected posts
             if ($sep == "new_line") {
                 $message .= "\n\n {$post['message']}";
             } else {
                 $message .= "[hr]{$post['message']}";
             }
             if (!isset($forum_counters[$post['fid']])) {
                 $forum_counters[$post['fid']] = array('num_posts' => 0, 'unapprovedposts' => 0, 'deletedposts' => 0);
             }
             if ($post['visible'] == 1) {
                 --$thread_counters[$post['tid']]['replies'];
                 $forum = get_forum($post['fid']);
                 if (!isset($user_counters[$post['uid']])) {
                     $user_counters[$post['uid']] = array('num_posts' => 0, 'num_threads' => 0);
                 }
                 // Subtract 1 from user's post count
                 if ($forum['usepostcounts'] != 0 && $post['threadvisible'] == 1) {
                     // Update post count of the user of the merged posts
                     --$user_counters[$post['uid']]['num_posts'];
                 }
                 if ($post['threadfirstpost'] == $post['pid'] && $forum['usethreadcounts'] != 0 && $post['threadvisible'] == 1) {
                     --$user_counters[$post['uid']]['num_threads'];
                 }
             } elseif ($post['visible'] == 0) {
                 // Subtract 1 unapproved post from post's thread
                 --$thread_counters[$post['tid']]['unapprovedposts'];
             } elseif ($post['visible'] == -1) {
                 // Subtract 1 deleted post from post's thread
                 --$thread_counters[$post['tid']]['deletedposts'];
             }
             $thread_counters[$post['tid']]['attachmentcount'] -= $post['attachmentcount'];
             // Subtract 1 post from post's forum
             if ($post['threadvisible'] == 1 && $post['visible'] == 1) {
                 --$forum_counters[$post['fid']]['num_posts'];
             } elseif ($post['threadvisible'] == 0 || $post['visible'] == 0 && $post['threadvisible'] != -1) {
                 --$forum_counters[$post['fid']]['unapprovedposts'];
             } else {
                 --$forum_counters[$post['fid']]['deletedposts'];
             }
         }
     }
     // Update the message
     $mergepost = array("message" => $db->escape_string($message));
     $db->update_query("posts", $mergepost, "pid = '{$masterpid}'");
     // Delete the extra posts
     $db->delete_query("posts", "pid IN({$pidin}) AND pid != '{$masterpid}'");
     // Update pid for attachments
     $mergepost2 = array("pid" => $masterpid);
     $db->update_query("attachments", $mergepost2, "pid IN({$pidin})");
     // If the first post of a thread is merged out, the first should be updated
     $query = $db->simple_select("threads", "tid, uid, fid, visible", "firstpost IN({$pidin}) AND firstpost != '{$masterpid}'");
     while ($thread = $db->fetch_array($query)) {
         // In some cases the first post of a thread changes
         // Therefore resync the visible field to make sure they're the same if they're not
         $query = $db->simple_select("posts", "pid, uid, visible", "tid='{$thread['tid']}'", array('order_by' => 'dateline', 'order_dir' => 'asc', 'limit' => 1));
         $new_firstpost = $db->fetch_array($query);
         if ($thread['visible'] != $new_firstpost['visible']) {
             $db->update_query("posts", array('visible' => $thread['visible']), "pid='{$new_firstpost['pid']}'");
             // Correct counters
             if ($new_firstpost['visible'] == 1) {
                 --$thread_counters[$thread['tid']]['replies'];
             } elseif ($new_firstpost['visible'] == -1) {
                 --$thread_counters[$thread['tid']]['deletedposts'];
             } else {
                 --$thread_counters[$thread['tid']]['unapprovedposts'];
             }
             if ($thread['visible'] == 1) {
                 ++$thread_counters[$thread['tid']]['replies'];
             } elseif ($thread['visible'] == -1) {
                 ++$thread_counters[$thread['tid']]['deletedposts'];
             } else {
                 ++$thread_counters[$thread['tid']]['unapprovedposts'];
             }
         }
         if ($new_firstpost['uid'] != $thread['uid'] && $forum['usethreadcounts'] != 0 && $thread['visible'] == 1) {
             if (!isset($user_counters[$new_firstpost['uid']])) {
                 $user_counters[$new_firstpost['uid']] = array('num_posts' => 0, 'num_threads' => 0);
             }
             ++$user_counters[$new_firstpost['uid']]['num_threads'];
         }
         update_first_post($thread['tid']);
     }
     $arguments = array("pids" => $pids, "tid" => $tid);
     $plugins->run_hooks("class_moderation_merge_posts", $arguments);
     if (!empty($thread_counters)) {
         foreach ($thread_counters as $tid => $counters) {
             $counters = array('replies' => signed($counters['replies']), 'unapprovedposts' => signed($counters['unapprovedposts']), 'deletedposts' => signed($counters['deletedposts']), 'attachmentcount' => signed($counters['attachmentcount']));
             update_thread_counters($tid, $counters);
             update_last_post($tid);
         }
     }
     if (!empty($forum_counters)) {
         foreach ($forum_counters as $fid => $counters) {
             $updated_forum_stats = array('posts' => signed($counters['num_posts']), 'unapprovedposts' => signed($counters['unapprovedposts']), 'deletedposts' => signed($counters['deletedposts']));
             update_forum_counters($fid, $updated_forum_stats);
             update_forum_lastpost($fid);
         }
     }
     if (!empty($user_counters)) {
         foreach ($user_counters as $uid => $counters) {
             $update_array = array("postnum" => "+{$counters['num_posts']}", "threadnum" => "+{$counters['num_threads']}");
             update_user_counters($uid, $update_array);
         }
     }
     return $masterpid;
 }
示例#2
0
 /**
  * Merge posts within thread
  *
  * @param array Post IDs to be merged
  * @param int Thread ID (Set to 0 if posts from multiple threads are
  * selected)
  * @return int ID of the post into which all other posts are merged
  */
 function merge_posts($pids, $tid = 0, $sep = "new_line")
 {
     global $db, $plugins;
     // Make sure we only have valid values
     $pids = array_map('intval', $pids);
     $tid = intval($tid);
     $pidin = implode(',', $pids);
     $attachment_count = 0;
     $first = 1;
     // Get the messages to be merged
     $query = $db->query("\n\t\t\tSELECT p.pid, p.uid, p.fid, p.tid, p.visible, p.message, f.usepostcounts, t.visible AS threadvisible, t.replies AS threadreplies, t.firstpost AS threadfirstpost, t.unapprovedposts AS threadunapprovedposts\n\t\t\tFROM " . TABLE_PREFIX . "posts p\n\t\t\tLEFT JOIN " . TABLE_PREFIX . "threads t ON (t.tid=p.tid)\n\t\t\tLEFT JOIN " . TABLE_PREFIX . "forums f ON (f.fid=p.fid)\n\t\t\tWHERE p.pid IN({$pidin})\n\t\t\tORDER BY p.dateline ASC\n\t\t");
     $num_unapproved_posts = $num_approved_posts = 0;
     $message = '';
     $threads = array();
     while ($post = $db->fetch_array($query)) {
         $threads[$post['tid']] = $post['tid'];
         if ($first == 1) {
             // all posts will be merged into this one
             $masterpid = $post['pid'];
             $message = $post['message'];
             $fid = $post['fid'];
             $mastertid = $post['tid'];
             $first = 0;
         } else {
             // these are the selected posts
             if ($sep == "new_line") {
                 $message .= "\n\n {$post['message']}";
             } else {
                 $message .= "[hr]{$post['message']}";
             }
             if ($post['visible'] == 1 && $post['threadvisible'] == 1) {
                 // Subtract 1 approved post from post's thread
                 if (!$thread_counters[$post['tid']]['replies']) {
                     $thread_counters[$post['tid']]['replies'] = $post['threadreplies'];
                 }
                 --$thread_counters[$post['tid']]['replies'];
                 // Subtract 1 approved post from post's forum
                 if (!isset($forum_counters[$post['fid']]['num_posts'])) {
                     $forum_counters[$post['fid']]['num_posts'] = 0;
                 }
                 --$forum_counters[$post['fid']]['num_posts'];
                 // Subtract 1 from user's post count
                 if ($post['usepostcounts'] != 0) {
                     // Update post count of the user of the merged posts
                     $db->update_query("users", array("postnum" => "postnum-1"), "uid='{$post['uid']}'", 1, true);
                 }
             } elseif ($post['visible'] == 0) {
                 // Subtract 1 unapproved post from post's thread
                 if (!$thread_counters[$post['tid']]['unapprovedposts']) {
                     $thread_counters[$post['tid']]['unapprovedposts'] = $post['threadunapprovedposts'];
                 }
                 --$thread_counters[$post['tid']]['unapprovedposts'];
                 // Subtract 1 unapproved post from post's forum
                 if (!isset($forum_counters[$post['fid']]['unapprovedposts'])) {
                     $forum_counters[$post['fid']]['unapprovedposts'] = 0;
                 }
                 --$forum_counters[$post['fid']]['unapprovedposts'];
             }
         }
     }
     // Update the message
     $mergepost = array("message" => $db->escape_string($message));
     $db->update_query("posts", $mergepost, "pid = '{$masterpid}'");
     // Delete the extra posts
     $db->delete_query("posts", "pid IN({$pidin}) AND pid != '{$masterpid}'");
     // Update pid for attachments
     $mergepost2 = array("pid" => $masterpid);
     $db->update_query("attachments", $mergepost2, "pid IN({$pidin})");
     // If the first post of a thread is merged out, the thread should be deleted
     $query = $db->simple_select("threads", "tid, fid, visible", "firstpost IN({$pidin}) AND firstpost != '{$masterpid}'");
     while ($thread = $db->fetch_array($query)) {
         $this->delete_thread($thread['tid']);
         // Subtract 1 thread from the forum's stats
         if ($thread['visible']) {
             if (!isset($forum_counters[$thread['fid']]['threads'])) {
                 $forum_counters[$thread['fid']]['threads'] = 0;
             }
             --$forum_counters[$thread['fid']]['threads'];
         } else {
             if (!isset($forum_counters[$thread['fid']]['unapprovedthreads'])) {
                 $forum_counters[$thread['fid']]['unapprovedthreads'] = 0;
             }
             --$forum_counters[$thread['fid']]['unapprovedthreads'];
         }
     }
     $arguments = array("pids" => $pids, "tid" => $tid);
     $plugins->run_hooks("class_moderation_merge_posts", $arguments);
     if (is_array($thread_counters)) {
         foreach ($thread_counters as $tid => $counters) {
             $db->update_query("threads", $counters, "tid='{$tid}'");
             update_thread_data($tid);
         }
     }
     update_thread_data($mastertid);
     update_forum_lastpost($fid);
     foreach ($threads as $tid) {
         $count = array();
         // Attachment count
         $query = $db->query("\n\t\t\t\t\tSELECT COUNT(aid) AS attachment_count\n\t\t\t\t\tFROM " . TABLE_PREFIX . "attachments a\n\t\t\t\t\tLEFT JOIN " . TABLE_PREFIX . "posts p ON (a.pid=p.pid)\n\t\t\t\t\tWHERE p.tid='{$tid}'\n\t\t\t");
         $count['attachmentcount'] = $db->fetch_field($query, "attachment_count");
         if (!$count['attachmentcount']) {
             $count['attachmentcount'] = 0;
         }
         update_thread_counters($tid, $count);
     }
     if (is_array($forum_counters)) {
         foreach ($forum_counters as $fid => $counters) {
             $updated_forum_stats = array('posts' => signed($counters['num_posts']), 'unapprovedposts' => signed($counters['unapprovedposts']), 'threads' => signed($counters['threads']));
             update_forum_counters($fid, $updated_forum_stats);
         }
     }
     return $masterpid;
 }