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.7 2006/01/29 17:31:16 grahamje 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 // Before pruning, lets try to clean up the invalid topic entries
35 $sql = 'SELECT topic_id FROM ' . TOPICS_TABLE . '
36 WHERE topic_last_post_id = 0';
37 if ( !($result = $db->sql_query($sql)) )
39 message_die(GENERAL_ERROR, 'Could not obtain lists of topics to sync', '', __LINE__, __FILE__, $sql);
42 while( $row = $db->sql_fetchrow($result) )
44 sync('topic', $row['topic_id']);
47 $db->sql_freeresult($result);
49 $prune_all = ($prune_all) ? '' : 'AND t.topic_vote = 0 AND t.topic_type <> ' . POST_ANNOUNCE;
51 // Those without polls and announcements ... unless told otherwise!
53 $sql = "SELECT t.topic_id
54 FROM " . POSTS_TABLE . " p, " . TOPICS_TABLE . " t
55 WHERE t.forum_id = $forum_id
57 AND p.post_id = t.topic_last_post_id";
58 if ( $prune_date != '' )
60 $sql .= " AND p.post_time < $prune_date";
63 if ( !($result = $db->sql_query($sql)) )
65 message_die(GENERAL_ERROR, 'Could not obtain lists of topics to prune', '', __LINE__, __FILE__, $sql);
69 while( $row = $db->sql_fetchrow($result) )
71 $sql_topics .= ( ( $sql_topics != '' ) ? ', ' : '' ) . $row['topic_id'];
73 $db->sql_freeresult($result);
75 if( $sql_topics != '' )
77 $sql = "SELECT post_id
78 FROM " . POSTS_TABLE . "
79 WHERE forum_id = $forum_id
80 AND topic_id IN ($sql_topics)";
81 if ( !($result = $db->sql_query($sql)) )
83 message_die(GENERAL_ERROR, 'Could not obtain list of posts to prune', '', __LINE__, __FILE__, $sql);
87 while ( $row = $db->sql_fetchrow($result) )
89 $sql_post .= ( ( $sql_post != '' ) ? ', ' : '' ) . $row['post_id'];
91 $db->sql_freeresult($result);
93 if ( $sql_post != '' )
95 $sql = "DELETE FROM " . TOPICS_WATCH_TABLE . "
96 WHERE topic_id IN ($sql_topics)";
97 if ( !$db->sql_query($sql, BEGIN_TRANSACTION) )
99 message_die(GENERAL_ERROR, 'Could not delete watched topics during prune', '', __LINE__, __FILE__, $sql);
102 $sql = "DELETE FROM " . TOPICS_TABLE . "
103 WHERE topic_id IN ($sql_topics)";
104 if ( !$db->sql_query($sql) )
106 message_die(GENERAL_ERROR, 'Could not delete topics during prune', '', __LINE__, __FILE__, $sql);
109 $pruned_topics = $db->sql_affectedrows();
111 $sql = "DELETE FROM " . POSTS_TABLE . "
112 WHERE post_id IN ($sql_post)";
113 if ( !$db->sql_query($sql) )
115 message_die(GENERAL_ERROR, 'Could not delete post_text during prune', '', __LINE__, __FILE__, $sql);
118 $pruned_posts = $db->sql_affectedrows();
120 $sql = "DELETE FROM " . POSTS_TEXT_TABLE . "
121 WHERE post_id IN ($sql_post)";
122 if ( !$db->sql_query($sql) )
124 message_die(GENERAL_ERROR, 'Could not delete post during prune', '', __LINE__, __FILE__, $sql);
127 remove_search_post($sql_post);
129 return array ('topics' => $pruned_topics, 'posts' => $pruned_posts);
133 return array('topics' => 0, 'posts' => 0);
137 // Function auto_prune(), this function will read the configuration data from
138 // the auto_prune table and call the prune function with the necessary info.
140 function auto_prune($forum_id = 0)
145 FROM " . PRUNE_TABLE . "
146 WHERE forum_id = $forum_id";
147 if ( !($result = $db->sql_query($sql)) )
149 message_die(GENERAL_ERROR, 'Could not read auto_prune table', '', __LINE__, __FILE__, $sql);
152 if ( $row = $db->sql_fetchrow($result) )
154 if ( $row['prune_freq'] && $row['prune_days'] )
156 $prune_date = time() - ( $row['prune_days'] * 86400 );
157 $next_prune = time() + ( $row['prune_freq'] * 86400 );
159 prune($forum_id, $prune_date);
160 sync('forum', $forum_id);
162 $sql = "UPDATE " . FORUMS_TABLE . "
163 SET prune_next = $next_prune
164 WHERE forum_id = $forum_id";
165 if ( !$db->sql_query($sql) )
167 message_die(GENERAL_ERROR, 'Could not update forum table', '', __LINE__, __FILE__, $sql);