2 rcs_id('$Id: ArchiveCleaner.php,v 1.7 2005-10-29 08:57:57 rurban Exp $');
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);
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) {
49 $expire = &$this->expire_params;
50 foreach (array('major', 'minor', 'author') as $class)
51 $counter[$class] = new ArchiveCleaner_Counter($expire[$class]);
53 $authors_seen = array();
55 $current = $page->getCurrentRevision(false);
57 for ( $revision = $page->getRevisionBefore($current,false);
58 $revision->getVersion() > 0;
59 $revision = $page->getRevisionBefore($revision,false) ) {
61 if ($revision->get('is_minor_edit'))
62 $keep = $counter['minor']->keep($revision);
64 $keep = $counter['major']->keep($revision);
66 if ($this->isMergeable($revision)) {
68 $page->mergeRevision($revision);
72 $author_id = $revision->get('author_id');
73 if (empty($authors_seen[$author_id])) {
74 if ($counter['author']->keep($revision))
76 $authors_seen[$author_id] = true;
79 $page->deleteRevision($revision);
89 class ArchiveCleaner_Counter
91 function ArchiveCleaner_Counter($params) {
95 $INFINITY = 0x7fffffff;
97 $this->max_keep = isset($max_keep) ? $max_keep : $INFINITY;
99 $this->min_age = isset($min_age) ? $min_age : 0;
100 $this->min_keep = isset($min_keep) ? $min_keep : 0;
102 $this->max_age = isset($max_age) ? $max_age : $INFINITY;
103 $this->keep = isset($keep) ? $keep : $INFINITY;
105 if ($this->keep > $this->max_keep)
106 $this->keep = $this->max_keep;
107 if ($this->min_keep > $this->keep)
108 $this->min_keep = $this->keep;
110 if ($this->min_age > $this->max_age)
111 $this->min_age = $this->max_age;
115 $this->previous_supplanted = false;
119 function computeAge($revision) {
120 $supplanted = $revision->get('_supplanted');
123 // Every revision but the most recent should have a supplanted time.
124 // However, if it doesn't...
125 trigger_error(sprintf("Warning: Page '%s', version '%d' has no '_supplanted' timestamp",
126 $revision->getPageName(),
127 $revision->getVersion()),
129 // Assuming revisions are chronologically ordered, the previous
130 // supplanted time is a good value to use...
131 if ($this->previous_supplanted > 0)
132 $supplanted = $this->previous_supplanted;
134 // no supplanted timestamp.
135 // don't delete this revision based on age.
140 $this->previous_supplanted = $supplanted;
141 return ($this->now - $supplanted) / (24 * 3600);
144 function keep($revision) {
145 $count = ++$this->count;
146 $age = $this->computeAge($revision);
148 if ($count > $this->max_keep)
150 if ($age <= $this->min_age || $count <= $this->min_keep)
152 return $age <= $this->max_age && $count <= $this->keep;
156 // $Log: not supported by cvs2svn $
162 // c-hanging-comment-ender-p: nil
163 // indent-tabs-mode: nil