diff --git a/phpBB/includes/prune.php b/phpBB/includes/prune.php index 5dc3c9e4cb..fe00b77b5a 100644 --- a/phpBB/includes/prune.php +++ b/phpBB/includes/prune.php @@ -20,78 +20,112 @@ * \**************************************************************************/ -// I am currently seperating the prune functions from functions.php due to the -// fact that they are only really needed in one or two places so I don't see -// the need to include them everywhere. If someone else thinks this is a bad -// idea I am not opposed to moving them elsewhere ;) Jonathan "The_Systech" - -/***************************************************************************\ -* -* function prune. This function takes as it's arguments the forum id to -* perform the prune on, and the date before which topics should be pruned. -* -* This function returns the number of topics pruned upon success. -* -\***************************************************************************/ function prune($forum_id, $prune_date) { global $db, $lang; - $sql = 'SELECT t.topic_id - FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p - WHERE t.forum_id = $forum_id - AND p.post_id = t.topic_last_post_id - AND t.topic_type = " . POST_NORMAL . " - AND p.post_time < $prune_date"; + + $sql = "SELECT t.topic_id + FROM " . POSTS_TABLE . " p, " . TOPICS_TABLE . " t + WHERE t.forum_id = $forum_id + AND t.topic_type = " . POST_NORMAL . " + AND p.post_id = t.topic_last_post_id + AND p.post_time < $prune_date"; + if(!$result_topics = $db->sql_query($sql)) + { + message_die(GENERAL_ERROR, "Couldn't obtain lists of topics to prune.", "", __LINE__, __FILE__, $sql); + } + $pruned_topics = $db->sql_numrows($result_topics); + + $sql = "SELECT p.post_id + FROM " . POSTS_TABLE . " p, " . TOPICS_TABLE . " t + WHERE p.forum_id = $forum_id + AND p.post_time < $prune_date + AND t.topic_id = p.topic_id + AND t.topic_type = " . POST_NORMAL; + if(!$result_posts = $db->sql_query($sql)) + { + message_die(GENERAL_ERROR, "Couldn't obtain list of posts to prune.", "", __LINE__, __FILE__, $sql); + } + $pruned_posts = $db->sql_numrows($result_posts); + + if( $pruned_topics > 0 ) + { + $pruned_topic_list = $db->sql_fetchrowset($result_topics); + + $sql_topics = ""; + + for($i = 0; $i < $pruned_topics; $i++) + { + if($sql_topics != "") + { + $sql_topics .= " OR "; + } + $sql_topics .= "topic_id = " . $pruned_topic_list[$i]['topic_id']; + } + + $sql_topics = "DELETE FROM " . TOPICS_TABLE . " WHERE " . $sql_topics; + + if(!$result = $db->sql_query($sql_topics)) + { + message_die(GENERAL_ERROR, "Couldn't delete topics during prune.", "", __LINE__, __FILE__, $sql_topics); + } + } + + if( $pruned_posts > 0 ) + { + $pruned_post_list = $db->sql_fetchrowset($result_posts); + + $sql_post_text = ""; + $sql_post = ""; + + for($i = 0; $i < $pruned_posts; $i++) + { + $post_id = $pruned_post_list[$i]['post_id']; + + if($sql_post_text != "") + { + $sql_post_text .= " OR "; + } + $sql_post_text .= "post_id = $post_id"; + + if($sql_post != "") + { + $sql_post .= " OR "; + } + $sql_post .= "post_id = $post_id"; + } + + $sql_post_text = "DELETE FROM " . POSTS_TEXT_TABLE . " WHERE " . $sql_post_text; + $sql_post = "DELETE FROM " . POSTS_TABLE . " WHERE " . $sql_post; + + if(!$result = $db->sql_query($sql_post_text, BEGIN_TRANSACTION)) + { + message_die(GENERAL_ERROR, "Couldn't delete post_text during prune.", "", __LINE__, __FILE__, $sql_post_text); + } + else + { + if(!$result = $db->sql_query($sql_post, END_TRANSACTION)) + { + message_die(GENERAL_ERROR, "Couldn't delete post during prune.", "", __LINE__, __FILE__, $sql_post); + } + } + } + + $sql = "UPDATE " . FORUMS_TABLE . " + SET forum_topics = forum_topics - $pruned_topics, forum_posts = forum_posts - $pruned_posts + WHERE forum_id = $forum_id"; if(!$result = $db->sql_query($sql)) { - message_die(GENERAL_ERROR, "Couldn't obtain list of topics to prune.", __LINE__, __FILE__); - } // End if(!$result... - $pruned_topics = $db->sql_numrows($result); - if($pruned_topics > 0) - { - $prune_posts_sql = 'DELETE FROM ' . POSTS_TABLE . " - WHERE forum_id = $forum_id AND ("; - $prune_topic_sql = 'DELETE FROM ' . TOPICS_TABLE . " - WHERE forum_id = $forum_id AND ("; - // ADD a list ORing all Topic ID's to prune.... - while($row = $db->sql_fetchrow($result)) - { - $prune_posts_sql .= 'topic_id = ' . $row['topic_id'] . ' OR '; - $prune_topic_sql .= 'topic_id = ' . $row['topic_id'] . ' OR '; - } // End while loop - // Remove the final OR... - $prune_posts_sql = substr($prune_posts_sql, 0, (strlen($prune_posts_sql) - 4)); - $prune_topic_sql = substr($prune_topic_sql, 0, strlen($prune_topic_sql) - 4); - $prune_posts_sql .= ')'; - $prune_topic_sql .= ')'; - if(!$result = $db->sql_query($prune_posts_sql)) - { - message_die(GENERAL_ERROR, "While Pruning: Couldn't remove affected posts.
$prune_posts_sql", __LINE__, __FILE__); - } // end if(!$result... - $pruned_posts = $db->sql_affectedrows(); - if(!$result = $db->sql_query($prune_topic_sql)) - { - message_die(GENERAL_ERROR, "While Pruning: Couldn't remove affected topics.", __LINE__, __FILE__); - } // end if(!$result... - - // - // Update forum info to reflect proper topic and post counts... - // - - $sql = "UPDATE " . FORUMS_TABLE . " - SET forum_posts = (forum_posts - $pruned_posts), forum_topics = (forum_topics - $pruned_topics) - WHERE forum_id = $forum_id"; - if(!$result = $db->sql_query($sql)) - { - message_die(GENERAL_ERROR, "While Pruning: Couldn't update topic/post counts
" .mysql_error() , __LINE__, __FILE__); - } - - } // End if $prune_topics + message_die(GENERAL_ERROR, "Couldn't update forum data after prune.", "", __LINE__, __FILE__, $sql); + } + $returnval = array ( "topics" => $pruned_topics, "posts" => $pruned_posts); + return $returnval; -} // End function prune. + +} /***************************************************************************\ * @@ -102,27 +136,39 @@ function prune($forum_id, $prune_date) function auto_prune($forum_id = 0) { global $db, $lang; + $one_day = 60 * 60 * 24; - $sql = 'SELECT * - FROM ' . PRUNE_TABLE . " - WHERE forum_id = '$forum_id'"; + + $sql = "SELECT * + FROM " . PRUNE_TABLE . " + WHERE forum_id = $forum_id"; if(!$result = $db->sql_query($sql)) { message_die(GENERAL_ERROR, "Auto-Prune: Couldn't read auto_prune table.", __LINE__, __FILE__); - } // End if(!$result... + } + while($row = $db->sql_fetchrow($result)) { $forum_id = $row['forum_id']; + $prune_date = time() - ($row['prune_days'] * $one_day); + $pruned = prune($forum_id, $prune_date); + $next_prune = time() + ($row['prune_freq'] * $one_day); - $sql = 'UPDATE ' . FORUMS_TABLE . " - SET prune_next = '$next_prune' - WHERE forum_id = '$forum_id'"; + + $sql = "UPDATE " . FORUMS_TABLE . " + SET prune_next = $next_prune + WHERE forum_id = $forum_id"; if(!$db->sql_query($sql)) { message_die(GENERAL_ERROR, "Auto-Prune: Couldn't update forum table.", __LINE__, __FILE__); - } // End if(!$db->sql.. - } // End While Loop. -} // End auto_prune function. + } + + } + + return; +} + +?> \ No newline at end of file