1 <?php rcs_id('$Id: ArchiveCleaner.php,v 1.3 2004-04-29 20:41:03 rurban Exp $');
5 function ArchiveCleaner ($expire_params) {
6 $this->expire_params = $expire_params;
9 function isMergeable($revision) {
10 if ( ! $revision->get('is_minor_edit') )
13 $page = $revision->getPage();
14 $author_id = $revision->get('author_id');
16 $previous = $page->getRevisionBefore($revision);
18 return !empty($author_id)
19 && $author_id == $previous->get('author_id');
22 function cleanDatabase($dbi) {
23 $iter = $dbi->getAllPages();
24 while ($page = $iter->next())
25 $this->cleanPageRevisions($page);
28 function cleanPageRevisions($page) {
30 $expire = &$this->expire_params;
31 foreach (array('major', 'minor', 'author') as $class)
32 $counter[$class] = new ArchiveCleaner_Counter($expire[$class]);
34 $authors_seen = array();
36 $current = $page->getCurrentRevision();
38 for ( $revision = $page->getRevisionBefore($current);
39 $revision->getVersion() > 0;
40 $revision = $page->getRevisionBefore($revision) ) {
42 if ($revision->get('is_minor_edit'))
43 $keep = $counter['minor']->keep($revision);
45 $keep = $counter['major']->keep($revision);
47 if ($this->isMergeable($revision)) {
49 $page->mergeRevision($revision);
53 $author_id = $revision->get('author_id');
54 if (empty($authors_seen[$author_id])) {
55 if ($counter['author']->keep($revision))
57 $authors_seen[$author_id] = true;
60 $page->deleteRevision($revision);
70 class ArchiveCleaner_Counter
72 function ArchiveCleaner_Counter($params) {
76 $INFINITY = 0x7fffffff;
78 $this->max_keep = isset($max_keep) ? $max_keep : $INFINITY;
80 $this->min_age = isset($min_age) ? $min_age : 0;
81 $this->min_keep = isset($min_keep) ? $min_keep : 0;
83 $this->max_age = isset($max_age) ? $max_age : $INFINITY;
84 $this->keep = isset($keep) ? $keep : $INFINITY;
86 if ($this->keep > $this->max_keep)
87 $this->keep = $this->max_keep;
88 if ($this->min_keep > $this->keep)
89 $this->min_keep = $this->keep;
91 if ($this->min_age > $this->max_age)
92 $this->min_age = $this->max_age;
96 $this->previous_supplanted = false;
100 function computeAge($revision) {
101 $supplanted = $revision->get('_supplanted');
104 // Every revision but the most recent should have a supplanted time.
105 // However, if it doesn't...
106 trigger_error(sprintf("Warning: Page '%s', version '%d' has no '_supplanted' timestamp",
107 $revision->getPageName(),
108 $revision->getVersion()),
110 // Assuming revisions are chronologically ordered, the previous
111 // supplanted time is a good value to use...
112 if ($this->previous_supplanted > 0)
113 $supplanted = $this->previous_supplanted;
115 // no supplanted timestamp.
116 // don't delete this revision based on age.
121 $this->previous_supplanted = $supplanted;
122 return ($this->now - $supplanted) / (24 * 3600);
125 function keep($revision) {
126 $count = ++$this->count;
127 $age = $this->computeAge($revision);
129 if ($count > $this->max_keep)
131 if ($age <= $this->min_age || $count <= $this->min_keep)
133 return $age <= $this->max_age && $count <= $this->keep;
142 // c-hanging-comment-ender-p: nil
143 // indent-tabs-mode: nil