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 __construct($expire_params)
26 $this->expire_params = $expire_params;
33 function isMergeable($revision)
35 if (!$revision->get('is_minor_edit'))
38 $page = $revision->getPage();
39 $author_id = $revision->get('author_id');
41 $previous = $page->getRevisionBefore($revision, false);
43 return !empty($author_id)
44 && $author_id == $previous->get('author_id');
47 function cleanDatabase($dbi)
49 $iter = $dbi->getAllPages();
50 while ($page = $iter->next())
51 $this->cleanPageRevisions($page);
54 function cleanPageRevisions($page)
56 $INFINITY = 0x7fffffff;
58 $expire = &$this->expire_params;
60 foreach (array('major', 'minor', 'author') as $class)
61 $counter[$class] = new ArchiveCleaner_Counter($expire[$class]);
62 // shortcut to keep all
63 if (($counter['minor']->min_keep == $INFINITY)
64 and ($counter['major']->min_keep == $INFINITY)
68 $authors_seen = array();
70 $current = $page->getCurrentRevision(false);
72 for ($revision = $page->getRevisionBefore($current, false);
73 $revision->getVersion() > 0;
74 $revision = $page->getRevisionBefore($revision, false)) {
76 if ($revision->get('is_minor_edit'))
77 $keep = $counter['minor']->keep($revision);
79 $keep = $counter['major']->keep($revision);
81 if ($this->isMergeable($revision)) {
83 $page->mergeRevision($revision);
86 $author_id = $revision->get('author_id');
87 if (empty($authors_seen[$author_id])) {
88 if ($counter['author']->keep($revision))
90 $authors_seen[$author_id] = true;
93 $page->deleteRevision($revision);
103 class ArchiveCleaner_Counter
105 function __construct($params)
110 $INFINITY = 0x7fffffff;
112 $this->max_keep = isset($max_keep) ? $max_keep : $INFINITY;
114 $this->min_age = isset($min_age) ? $min_age : 0;
115 $this->min_keep = isset($min_keep) ? $min_keep : 0;
117 $this->max_age = isset($max_age) ? $max_age : $INFINITY;
118 $this->keep = isset($keep) ? $keep : $INFINITY;
120 if ($this->keep > $this->max_keep)
121 $this->keep = $this->max_keep;
122 if ($this->min_keep == $INFINITY) { // shortcut to keep all
123 $this->max_keep = $this->keep = $this->min_age = $this->max_age = $INFINITY;
125 if ($this->min_keep > $this->keep)
126 $this->min_keep = $this->keep;
128 if ($this->min_age > $this->max_age)
129 $this->min_age = $this->max_age;
133 $this->previous_supplanted = false;
137 function computeAge($revision)
139 $supplanted = $revision->get('_supplanted');
142 // Every revision but the most recent should have a supplanted time.
143 // However, if it doesn't...
144 trigger_error(sprintf("Warning: Page ā%sā, version '%d' has no '_supplanted' timestamp",
145 $revision->getPageName(),
146 $revision->getVersion()),
148 // Assuming revisions are chronologically ordered, the previous
149 // supplanted time is a good value to use...
150 if ($this->previous_supplanted > 0)
151 $supplanted = $this->previous_supplanted;
153 // no supplanted timestamp.
154 // don't delete this revision based on age.
159 $this->previous_supplanted = $supplanted;
160 return ($this->now - $supplanted) / (24 * 3600);
163 function keep($revision)
165 $INFINITY = 0x7fffffff;
166 if ($this->min_keep == $INFINITY)
168 $count = ++$this->count;
169 $age = $this->computeAge($revision);
171 if ($count > $this->max_keep)
173 if ($age <= $this->min_age || $count <= $this->min_keep)
175 return $age <= $this->max_age && $count <= $this->keep;
183 // c-hanging-comment-ender-p: nil
184 // indent-tabs-mode: nil