2 /***************************************************************************
5 * begin : Thursday, June 14, 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : support@phpbb.com
9 * $Id: prune.php,v 1.19.2.6 2003/03/18 23:23:57 acydburn Exp $
12 ***************************************************************************/
14 /***************************************************************************
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 ***************************************************************************/
23 if ( !defined('IN_PHPBB') )
25 die("Hacking attempt");
28 require($phpbb_root_path . 'includes/functions_search.'.$phpEx);
30 function prune($forum_id, $prune_date, $prune_all = false)
34 $prune_all = ($prune_all) ? '' : 'AND t.topic_vote = 0 AND t.topic_type <> ' . POST_ANNOUNCE;
36 // Those without polls and announcements ... unless told otherwise!
38 $sql = "SELECT t.topic_id
39 FROM " . POSTS_TABLE . " p, " . TOPICS_TABLE . " t
40 WHERE t.forum_id = $forum_id
42 AND ( p.post_id = t.topic_last_post_id
43 OR t.topic_last_post_id = 0 )";
44 if ( $prune_date != '' )
46 $sql .= " AND p.post_time < $prune_date";
49 if ( !($result = $db->sql_query($sql)) )
51 message_die(GENERAL_ERROR, 'Could not obtain lists of topics to prune', '', __LINE__, __FILE__, $sql);
55 while( $row = $db->sql_fetchrow($result) )
57 $sql_topics .= ( ( $sql_topics != '' ) ? ', ' : '' ) . $row['topic_id'];
59 $db->sql_freeresult($result);
61 if( $sql_topics != '' )
63 $sql = "SELECT post_id
64 FROM " . POSTS_TABLE . "
65 WHERE forum_id = $forum_id
66 AND topic_id IN ($sql_topics)";
67 if ( !($result = $db->sql_query($sql)) )
69 message_die(GENERAL_ERROR, 'Could not obtain list of posts to prune', '', __LINE__, __FILE__, $sql);
73 while ( $row = $db->sql_fetchrow($result) )
75 $sql_post .= ( ( $sql_post != '' ) ? ', ' : '' ) . $row['post_id'];
77 $db->sql_freeresult($result);
79 if ( $sql_post != '' )
81 $sql = "DELETE FROM " . TOPICS_WATCH_TABLE . "
82 WHERE topic_id IN ($sql_topics)";
83 if ( !$db->sql_query($sql, BEGIN_TRANSACTION) )
85 message_die(GENERAL_ERROR, 'Could not delete watched topics during prune', '', __LINE__, __FILE__, $sql);
88 $sql = "DELETE FROM " . TOPICS_TABLE . "
89 WHERE topic_id IN ($sql_topics)";
90 if ( !$db->sql_query($sql) )
92 message_die(GENERAL_ERROR, 'Could not delete topics during prune', '', __LINE__, __FILE__, $sql);
95 $pruned_topics = $db->sql_affectedrows();
97 $sql = "DELETE FROM " . POSTS_TABLE . "
98 WHERE post_id IN ($sql_post)";
99 if ( !$db->sql_query($sql) )
101 message_die(GENERAL_ERROR, 'Could not delete post_text during prune', '', __LINE__, __FILE__, $sql);
104 $pruned_posts = $db->sql_affectedrows();
106 $sql = "DELETE FROM " . POSTS_TEXT_TABLE . "
107 WHERE post_id IN ($sql_post)";
108 if ( !$db->sql_query($sql) )
110 message_die(GENERAL_ERROR, 'Could not delete post during prune', '', __LINE__, __FILE__, $sql);
113 remove_search_post($sql_post);
115 return array ('topics' => $pruned_topics, 'posts' => $pruned_posts);
119 return array('topics' => 0, 'posts' => 0);
123 // Function auto_prune(), this function will read the configuration data from
124 // the auto_prune table and call the prune function with the necessary info.
126 function auto_prune($forum_id = 0)
131 FROM " . PRUNE_TABLE . "
132 WHERE forum_id = $forum_id";
133 if ( !($result = $db->sql_query($sql)) )
135 message_die(GENERAL_ERROR, 'Could not read auto_prune table', '', __LINE__, __FILE__, $sql);
138 if ( $row = $db->sql_fetchrow($result) )
140 if ( $row['prune_freq'] && $row['prune_days'] )
142 $prune_date = time() - ( $row['prune_days'] * 86400 );
143 $next_prune = time() + ( $row['prune_freq'] * 86400 );
145 prune($forum_id, $prune_date);
146 sync('forum', $forum_id);
148 $sql = "UPDATE " . FORUMS_TABLE . "
149 SET prune_next = $next_prune
150 WHERE forum_id = $forum_id";
151 if ( !$db->sql_query($sql) )
153 message_die(GENERAL_ERROR, 'Could not update forum table', '', __LINE__, __FILE__, $sql);