]> git.vanrenterghem.biz Git - www.vanrenterghem.biz.git/blob - phpBB2/includes/prune.php
Baseline
[www.vanrenterghem.biz.git] / phpBB2 / 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.7 2006/01/29 17:31:16 grahamje Exp $
10 *
11 *
12 ***************************************************************************/
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  ***************************************************************************/
23 if ( !defined('IN_PHPBB') )
24 {
25    die("Hacking attempt");
26 }
28 require($phpbb_root_path . 'includes/functions_search.'.$phpEx);
30 function prune($forum_id, $prune_date, $prune_all = false)
31 {
32         global $db, $lang;
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)) )
38         {
39                 message_die(GENERAL_ERROR, 'Could not obtain lists of topics to sync', '', __LINE__, __FILE__, $sql);
40         }
42         while( $row = $db->sql_fetchrow($result) )
43         {
44                 sync('topic', $row['topic_id']);
45         }
47         $db->sql_freeresult($result);
49         $prune_all = ($prune_all) ? '' : 'AND t.topic_vote = 0 AND t.topic_type <> ' . POST_ANNOUNCE;
50         //
51         // Those without polls and announcements ... unless told otherwise!
52         //
53         $sql = "SELECT t.topic_id 
54                 FROM " . POSTS_TABLE . " p, " . TOPICS_TABLE . " t
55                 WHERE t.forum_id = $forum_id
56                         $prune_all 
57                         AND p.post_id = t.topic_last_post_id";
58         if ( $prune_date != '' )
59         {
60                 $sql .= " AND p.post_time < $prune_date";
61         }
63         if ( !($result = $db->sql_query($sql)) )
64         {
65                 message_die(GENERAL_ERROR, 'Could not obtain lists of topics to prune', '', __LINE__, __FILE__, $sql);
66         }
68         $sql_topics = '';
69         while( $row = $db->sql_fetchrow($result) )
70         {
71                 $sql_topics .= ( ( $sql_topics != '' ) ? ', ' : '' ) . $row['topic_id'];
72         }
73         $db->sql_freeresult($result);
74                 
75         if( $sql_topics != '' )
76         {
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)) )
82                 {
83                         message_die(GENERAL_ERROR, 'Could not obtain list of posts to prune', '', __LINE__, __FILE__, $sql);
84                 }
86                 $sql_post = '';
87                 while ( $row = $db->sql_fetchrow($result) )
88                 {
89                         $sql_post .= ( ( $sql_post != '' ) ? ', ' : '' ) . $row['post_id'];
90                 }
91                 $db->sql_freeresult($result);
93                 if ( $sql_post != '' )
94                 {
95                         $sql = "DELETE FROM " . TOPICS_WATCH_TABLE . " 
96                                 WHERE topic_id IN ($sql_topics)";
97                         if ( !$db->sql_query($sql, BEGIN_TRANSACTION) )
98                         {
99                                 message_die(GENERAL_ERROR, 'Could not delete watched topics during prune', '', __LINE__, __FILE__, $sql);
100                         }
102                         $sql = "DELETE FROM " . TOPICS_TABLE . " 
103                                 WHERE topic_id IN ($sql_topics)";
104                         if ( !$db->sql_query($sql) )
105                         {
106                                 message_die(GENERAL_ERROR, 'Could not delete topics during prune', '', __LINE__, __FILE__, $sql);
107                         }
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) )
114                         {
115                                 message_die(GENERAL_ERROR, 'Could not delete post_text during prune', '', __LINE__, __FILE__, $sql);
116                         }
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) )
123                         {
124                                 message_die(GENERAL_ERROR, 'Could not delete post during prune', '', __LINE__, __FILE__, $sql);
125                         }
127                         remove_search_post($sql_post);
129                         return array ('topics' => $pruned_topics, 'posts' => $pruned_posts);
130                 }
131         }
133         return array('topics' => 0, 'posts' => 0);
136 //
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.
139 //
140 function auto_prune($forum_id = 0)
142         global $db, $lang;
144         $sql = "SELECT *
145                 FROM " . PRUNE_TABLE . "
146                 WHERE forum_id = $forum_id";
147         if ( !($result = $db->sql_query($sql)) )
148         {
149                 message_die(GENERAL_ERROR, 'Could not read auto_prune table', '', __LINE__, __FILE__, $sql);
150         }
152         if ( $row = $db->sql_fetchrow($result) )
153         {
154                 if ( $row['prune_freq'] && $row['prune_days'] )
155                 {
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) )
166                         {
167                                 message_die(GENERAL_ERROR, 'Could not update forum table', '', __LINE__, __FILE__, $sql);
168                         }
169                 }
170         }
172         return;
175 ?>