1 <?php rcs_id('$Id: ArchiveCleaner.php,v 1.1 2001-09-18 19:16:23 dairiki 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) {
75 $INFINITY = 0x7fffffff;
77 $this->max_keep = isset($max_keep) ? $max_keep : $INFINITY;
79 $this->min_age = isset($min_age) ? $min_age : 0;
80 $this->min_keep = isset($min_keep) ? $min_keep : 0;
82 $this->max_age = isset($max_age) ? $max_age : $INFINITY;
83 $this->keep = isset($keep) ? $keep : $INFINITY;
85 if ($this->keep > $this->max_keep)
86 $this->keep = $this->max_keep;
87 if ($this->min_keep > $this->keep)
88 $this->min_keep = $this->keep;
90 if ($this->min_age > $this->max_age)
91 $this->min_age = $this->max_age;
95 $this->previous_supplanted = false;
99 function computeAge($revision) {
100 $supplanted = $revision->get('_supplanted');
103 // Every revision but the most recent should have a supplanted time.
104 // However, if it doesn't...
105 assert($supplanted > 0);
106 // Assuming revisions are chronologically ordered, the previous
107 // supplanted time is a good value to use...
108 if ($this->previous_supplanted > 0)
109 $supplanted = $this->previous_supplanted;
111 // no supplanted timestamp.
112 // don't delete this revision based on age.
117 $this->last_supplanted = $supplanted;
118 return ($this->now - $supplanted) / (24 * 3600);
121 function keep($revision) {
122 $count = ++$this->count;
123 $age = $this->computeAge($revision);
125 if ($count > $this->max_keep)
127 if ($age <= $this->min_age || $count <= $this->min_keep)
129 return $age <= $this->max_age && $count <= $this->keep;
138 // c-hanging-comment-ender-p: nil
139 // indent-tabs-mode: nil