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 along
18 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 function ArchiveCleaner($expire_params)
26 $this->expire_params = $expire_params;
29 function isMergeable($revision)
31 if (!$revision->get('is_minor_edit'))
34 $page = $revision->getPage();
35 $author_id = $revision->get('author_id');
37 $previous = $page->getRevisionBefore($revision, false);
39 return !empty($author_id)
40 && $author_id == $previous->get('author_id');
43 function cleanDatabase($dbi)
45 $iter = $dbi->getAllPages();
46 while ($page = $iter->next())
47 $this->cleanPageRevisions($page);
50 function cleanPageRevisions($page)
52 $INFINITY = 0x7fffffff;
54 $expire = &$this->expire_params;
55 foreach (array('major', 'minor', 'author') as $class)
56 $counter[$class] = new ArchiveCleaner_Counter($expire[$class]);
57 // shortcut to keep all
58 if (($counter['minor']->min_keep == $INFINITY)
59 and ($counter['major']->min_keep == $INFINITY)
63 $authors_seen = array();
65 $current = $page->getCurrentRevision(false);
67 for ($revision = $page->getRevisionBefore($current, false);
68 $revision->getVersion() > 0;
69 $revision = $page->getRevisionBefore($revision, false)) {
71 if ($revision->get('is_minor_edit'))
72 $keep = $counter['minor']->keep($revision);
74 $keep = $counter['major']->keep($revision);
76 if ($this->isMergeable($revision)) {
78 $page->mergeRevision($revision);
81 $author_id = $revision->get('author_id');
82 if (empty($authors_seen[$author_id])) {
83 if ($counter['author']->keep($revision))
85 $authors_seen[$author_id] = true;
88 $page->deleteRevision($revision);
98 class ArchiveCleaner_Counter
100 function ArchiveCleaner_Counter($params)
105 $INFINITY = 0x7fffffff;
107 $this->max_keep = isset($max_keep) ? $max_keep : $INFINITY;
109 $this->min_age = isset($min_age) ? $min_age : 0;
110 $this->min_keep = isset($min_keep) ? $min_keep : 0;
112 $this->max_age = isset($max_age) ? $max_age : $INFINITY;
113 $this->keep = isset($keep) ? $keep : $INFINITY;
115 if ($this->keep > $this->max_keep)
116 $this->keep = $this->max_keep;
117 if ($this->min_keep == $INFINITY) { // shortcut to keep all
118 $this->max_keep = $this->keep = $this->min_age = $this->max_age = $INFINITY;
120 if ($this->min_keep > $this->keep)
121 $this->min_keep = $this->keep;
123 if ($this->min_age > $this->max_age)
124 $this->min_age = $this->max_age;
128 $this->previous_supplanted = false;
132 function computeAge($revision)
134 $supplanted = $revision->get('_supplanted');
137 // Every revision but the most recent should have a supplanted time.
138 // However, if it doesn't...
139 trigger_error(sprintf("Warning: Page ā%sā, version '%d' has no '_supplanted' timestamp",
140 $revision->getPageName(),
141 $revision->getVersion()),
143 // Assuming revisions are chronologically ordered, the previous
144 // supplanted time is a good value to use...
145 if ($this->previous_supplanted > 0)
146 $supplanted = $this->previous_supplanted;
148 // no supplanted timestamp.
149 // don't delete this revision based on age.
154 $this->previous_supplanted = $supplanted;
155 return ($this->now - $supplanted) / (24 * 3600);
158 function keep($revision)
160 $INFINITY = 0x7fffffff;
161 if ($this->min_keep == $INFINITY)
163 $count = ++$this->count;
164 $age = $this->computeAge($revision);
166 if ($count > $this->max_keep)
168 if ($age <= $this->min_age || $count <= $this->min_keep)
170 return $age <= $this->max_age && $count <= $this->keep;
178 // c-hanging-comment-ender-p: nil
179 // indent-tabs-mode: nil