]> scripts.mit.edu Git - autoinstalls/phpBB.git/blob - includes/prune.php
phpBB 2.0.19
[autoinstalls/phpBB.git] / includes / prune.php
1 <?php
2 /***************************************************************************
3 *                                 prune.php
4 *                            -------------------
5 *   begin                : Thursday, June 14, 2001
6 *   copyright            : (C) 2001 The phpBB Group
7 *   email                : support@phpbb.com
8 *
9 *   $Id: prune.php,v 1.19.2.6 2003/03/18 23:23:57 acydburn Exp $
10 *
11 *
12 ***************************************************************************/
13
14 /***************************************************************************
15  *
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.
20  *
21  ***************************************************************************/
22
23 if ( !defined('IN_PHPBB') )
24 {
25    die("Hacking attempt");
26 }
27
28 require($phpbb_root_path . 'includes/functions_search.'.$phpEx);
29
30 function prune($forum_id, $prune_date, $prune_all = false)
31 {
32         global $db, $lang;
33
34         $prune_all = ($prune_all) ? '' : 'AND t.topic_vote = 0 AND t.topic_type <> ' . POST_ANNOUNCE;
35         //
36         // Those without polls and announcements ... unless told otherwise!
37         //
38         $sql = "SELECT t.topic_id 
39                 FROM " . POSTS_TABLE . " p, " . TOPICS_TABLE . " t
40                 WHERE t.forum_id = $forum_id
41                         $prune_all 
42                         AND ( p.post_id = t.topic_last_post_id 
43                                 OR t.topic_last_post_id = 0 )";
44         if ( $prune_date != '' )
45         {
46                 $sql .= " AND p.post_time < $prune_date";
47         }
48
49         if ( !($result = $db->sql_query($sql)) )
50         {
51                 message_die(GENERAL_ERROR, 'Could not obtain lists of topics to prune', '', __LINE__, __FILE__, $sql);
52         }
53
54         $sql_topics = '';
55         while( $row = $db->sql_fetchrow($result) )
56         {
57                 $sql_topics .= ( ( $sql_topics != '' ) ? ', ' : '' ) . $row['topic_id'];
58         }
59         $db->sql_freeresult($result);
60                 
61         if( $sql_topics != '' )
62         {
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)) )
68                 {
69                         message_die(GENERAL_ERROR, 'Could not obtain list of posts to prune', '', __LINE__, __FILE__, $sql);
70                 }
71
72                 $sql_post = '';
73                 while ( $row = $db->sql_fetchrow($result) )
74                 {
75                         $sql_post .= ( ( $sql_post != '' ) ? ', ' : '' ) . $row['post_id'];
76                 }
77                 $db->sql_freeresult($result);
78
79                 if ( $sql_post != '' )
80                 {
81                         $sql = "DELETE FROM " . TOPICS_WATCH_TABLE . " 
82                                 WHERE topic_id IN ($sql_topics)";
83                         if ( !$db->sql_query($sql, BEGIN_TRANSACTION) )
84                         {
85                                 message_die(GENERAL_ERROR, 'Could not delete watched topics during prune', '', __LINE__, __FILE__, $sql);
86                         }
87
88                         $sql = "DELETE FROM " . TOPICS_TABLE . " 
89                                 WHERE topic_id IN ($sql_topics)";
90                         if ( !$db->sql_query($sql) )
91                         {
92                                 message_die(GENERAL_ERROR, 'Could not delete topics during prune', '', __LINE__, __FILE__, $sql);
93                         }
94
95                         $pruned_topics = $db->sql_affectedrows();
96
97                         $sql = "DELETE FROM " . POSTS_TABLE . " 
98                                 WHERE post_id IN ($sql_post)";
99                         if ( !$db->sql_query($sql) )
100                         {
101                                 message_die(GENERAL_ERROR, 'Could not delete post_text during prune', '', __LINE__, __FILE__, $sql);
102                         }
103
104                         $pruned_posts = $db->sql_affectedrows();
105
106                         $sql = "DELETE FROM " . POSTS_TEXT_TABLE . " 
107                                 WHERE post_id IN ($sql_post)";
108                         if ( !$db->sql_query($sql) )
109                         {
110                                 message_die(GENERAL_ERROR, 'Could not delete post during prune', '', __LINE__, __FILE__, $sql);
111                         }
112
113                         remove_search_post($sql_post);
114
115                         return array ('topics' => $pruned_topics, 'posts' => $pruned_posts);
116                 }
117         }
118
119         return array('topics' => 0, 'posts' => 0);
120 }
121
122 //
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.
125 //
126 function auto_prune($forum_id = 0)
127 {
128         global $db, $lang;
129
130         $sql = "SELECT *
131                 FROM " . PRUNE_TABLE . "
132                 WHERE forum_id = $forum_id";
133         if ( !($result = $db->sql_query($sql)) )
134         {
135                 message_die(GENERAL_ERROR, 'Could not read auto_prune table', '', __LINE__, __FILE__, $sql);
136         }
137
138         if ( $row = $db->sql_fetchrow($result) )
139         {
140                 if ( $row['prune_freq'] && $row['prune_days'] )
141                 {
142                         $prune_date = time() - ( $row['prune_days'] * 86400 );
143                         $next_prune = time() + ( $row['prune_freq'] * 86400 );
144
145                         prune($forum_id, $prune_date);
146                         sync('forum', $forum_id);
147
148                         $sql = "UPDATE " . FORUMS_TABLE . " 
149                                 SET prune_next = $next_prune 
150                                 WHERE forum_id = $forum_id";
151                         if ( !$db->sql_query($sql) )
152                         {
153                                 message_die(GENERAL_ERROR, 'Could not update forum table', '', __LINE__, __FILE__, $sql);
154                         }
155                 }
156         }
157
158         return;
159 }
160
161 ?>