]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/ArchiveCleaner.php
No tabs
[SourceForge/phpwiki.git] / lib / ArchiveCleaner.php
1 <?php
2 // $Id$
3 /* Copyright (C) 2002 Geoffrey T. Dairiki <dairiki@dairiki.org>
4  *
5  * This file is part of PhpWiki.
6  *
7  * PhpWiki is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * PhpWiki is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with PhpWiki; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 class ArchiveCleaner
23 {
24     function ArchiveCleaner ($expire_params) {
25         $this->expire_params = $expire_params;
26     }
27
28     function isMergeable($revision) {
29         if ( ! $revision->get('is_minor_edit') )
30             return false;
31
32         $page = $revision->getPage();
33         $author_id = $revision->get('author_id');
34
35         $previous = $page->getRevisionBefore($revision, false);
36
37         return !empty($author_id)
38             && $author_id == $previous->get('author_id');
39     }
40
41     function cleanDatabase($dbi) {
42         $iter = $dbi->getAllPages();
43         while ($page = $iter->next())
44             $this->cleanPageRevisions($page);
45     }
46
47     function cleanPageRevisions($page) {
48         $INFINITY = 0x7fffffff;
49
50         $expire = &$this->expire_params;
51         foreach (array('major', 'minor', 'author') as $class)
52             $counter[$class] = new ArchiveCleaner_Counter($expire[$class]);
53         // shortcut to keep all
54         if (($counter['minor']->min_keep == $INFINITY)
55             and ($counter['major']->min_keep == $INFINITY))
56             return;
57
58         $authors_seen = array();
59
60         $current = $page->getCurrentRevision(false);
61
62         for ( $revision = $page->getRevisionBefore($current,false);
63               $revision->getVersion() > 0;
64               $revision = $page->getRevisionBefore($revision,false) ) {
65
66             if ($revision->get('is_minor_edit'))
67                 $keep = $counter['minor']->keep($revision);
68             else
69                 $keep = $counter['major']->keep($revision);
70
71             if ($this->isMergeable($revision)) {
72                 if (!$keep) {
73                     $page->mergeRevision($revision);
74                 }
75             }
76             else {
77                 $author_id = $revision->get('author_id');
78                 if (empty($authors_seen[$author_id])) {
79                     if ($counter['author']->keep($revision))
80                         $keep = true;
81                     $authors_seen[$author_id] = true;
82                 }
83                 if (!$keep) {
84                     $page->deleteRevision($revision);
85                 }
86             }
87         }
88     }
89 }
90
91 /**
92  * @access private
93  */
94 class ArchiveCleaner_Counter
95 {
96     function ArchiveCleaner_Counter($params) {
97
98         if (!empty($params))
99             extract($params);
100         $INFINITY = 0x7fffffff;
101
102         $this->max_keep = isset($max_keep) ? $max_keep : $INFINITY;
103
104         $this->min_age  = isset($min_age)  ? $min_age  : 0;
105         $this->min_keep = isset($min_keep) ? $min_keep : 0;
106
107         $this->max_age  = isset($max_age)  ? $max_age  : $INFINITY;
108         $this->keep     = isset($keep)     ? $keep     : $INFINITY;
109
110         if ($this->keep > $this->max_keep)
111             $this->keep = $this->max_keep;
112         if ($this->min_keep == $INFINITY) { // shortcut to keep all
113             $this->max_keep = $this->keep = $this->min_age = $this->max_age = $INFINITY;
114         }
115         if ($this->min_keep > $this->keep)
116             $this->min_keep = $this->keep;
117
118         if ($this->min_age > $this->max_age)
119             $this->min_age = $this->max_age;
120
121         $this->now = time();
122         $this->count = 0;
123         $this->previous_supplanted = false;
124
125     }
126
127     function computeAge($revision) {
128         $supplanted = $revision->get('_supplanted');
129
130         if (!$supplanted) {
131             // Every revision but the most recent should have a supplanted time.
132             // However, if it doesn't...
133             trigger_error(sprintf("Warning: Page '%s', version '%d' has no '_supplanted' timestamp",
134                                   $revision->getPageName(),
135                                   $revision->getVersion()),
136                           E_USER_NOTICE);
137             // Assuming revisions are chronologically ordered, the previous
138             // supplanted time is a good value to use...
139             if ($this->previous_supplanted > 0)
140                 $supplanted = $this->previous_supplanted;
141             else {
142                 // no supplanted timestamp.
143                 // don't delete this revision based on age.
144                 return 0;
145             }
146         }
147
148         $this->previous_supplanted = $supplanted;
149         return ($this->now - $supplanted) / (24 * 3600);
150     }
151
152     function keep($revision) {
153         $INFINITY = 0x7fffffff;
154         if ($this->min_keep == $INFINITY)
155             return true;
156         $count = ++$this->count;
157         $age = $this->computeAge($revision);
158
159         if ($count > $this->max_keep)
160             return false;
161         if ($age <= $this->min_age || $count <= $this->min_keep)
162             return true;
163         return $age <= $this->max_age && $count <= $this->keep;
164     }
165 }
166
167 // Local Variables:
168 // mode: php
169 // tab-width: 8
170 // c-basic-offset: 4
171 // c-hanging-comment-ender-p: nil
172 // indent-tabs-mode: nil
173 // End:
174 ?>