3 /* Copyright (C) 2002 Geoffrey T. Dairiki <dairiki@dairiki.org>
5 * This file is part of PhpWiki.
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.
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.
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
24 function ArchiveCleaner ($expire_params) {
25 $this->expire_params = $expire_params;
28 function isMergeable($revision) {
29 if ( ! $revision->get('is_minor_edit') )
32 $page = $revision->getPage();
33 $author_id = $revision->get('author_id');
35 $previous = $page->getRevisionBefore($revision, false);
37 return !empty($author_id)
38 && $author_id == $previous->get('author_id');
41 function cleanDatabase($dbi) {
42 $iter = $dbi->getAllPages();
43 while ($page = $iter->next())
44 $this->cleanPageRevisions($page);
47 function cleanPageRevisions($page) {
48 $INFINITY = 0x7fffffff;
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))
58 $authors_seen = array();
60 $current = $page->getCurrentRevision(false);
62 for ( $revision = $page->getRevisionBefore($current,false);
63 $revision->getVersion() > 0;
64 $revision = $page->getRevisionBefore($revision,false) ) {
66 if ($revision->get('is_minor_edit'))
67 $keep = $counter['minor']->keep($revision);
69 $keep = $counter['major']->keep($revision);
71 if ($this->isMergeable($revision)) {
73 $page->mergeRevision($revision);
77 $author_id = $revision->get('author_id');
78 if (empty($authors_seen[$author_id])) {
79 if ($counter['author']->keep($revision))
81 $authors_seen[$author_id] = true;
84 $page->deleteRevision($revision);
94 class ArchiveCleaner_Counter
96 function ArchiveCleaner_Counter($params) {
100 $INFINITY = 0x7fffffff;
102 $this->max_keep = isset($max_keep) ? $max_keep : $INFINITY;
104 $this->min_age = isset($min_age) ? $min_age : 0;
105 $this->min_keep = isset($min_keep) ? $min_keep : 0;
107 $this->max_age = isset($max_age) ? $max_age : $INFINITY;
108 $this->keep = isset($keep) ? $keep : $INFINITY;
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;
115 if ($this->min_keep > $this->keep)
116 $this->min_keep = $this->keep;
118 if ($this->min_age > $this->max_age)
119 $this->min_age = $this->max_age;
123 $this->previous_supplanted = false;
127 function computeAge($revision) {
128 $supplanted = $revision->get('_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()),
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;
142 // no supplanted timestamp.
143 // don't delete this revision based on age.
148 $this->previous_supplanted = $supplanted;
149 return ($this->now - $supplanted) / (24 * 3600);
152 function keep($revision) {
153 $INFINITY = 0x7fffffff;
154 if ($this->min_keep == $INFINITY)
156 $count = ++$this->count;
157 $age = $this->computeAge($revision);
159 if ($count > $this->max_keep)
161 if ($age <= $this->min_age || $count <= $this->min_keep)
163 return $age <= $this->max_age && $count <= $this->keep;
171 // c-hanging-comment-ender-p: nil
172 // indent-tabs-mode: nil