]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/ArchiveCleaner.php
Jeff's hacks II.
[SourceForge/phpwiki.git] / lib / ArchiveCleaner.php
1 <?php rcs_id('$Id: ArchiveCleaner.php,v 1.1 2001-09-18 19:16:23 dairiki Exp $');
2
3 class ArchiveCleaner
4 {
5     function ArchiveCleaner ($expire_params) {
6         $this->expire_params = $expire_params;
7     }
8     
9     function isMergeable($revision) {
10         if ( ! $revision->get('is_minor_edit') )
11             return false;
12
13         $page = $revision->getPage();
14         $author_id = $revision->get('author_id');
15
16         $previous = $page->getRevisionBefore($revision);
17
18         return !empty($author_id)
19             && $author_id == $previous->get('author_id');
20     }
21
22     function cleanDatabase($dbi) {
23         $iter = $dbi->getAllPages();
24         while ($page = $iter->next())
25             $this->cleanPageRevisions($page);
26     }
27         
28     function cleanPageRevisions($page) {
29
30         $expire = &$this->expire_params;
31         foreach (array('major', 'minor', 'author') as $class)
32             $counter[$class] = new ArchiveCleaner_Counter($expire[$class]);
33
34         $authors_seen = array();
35         
36         $current = $page->getCurrentRevision();
37
38         for ( $revision = $page->getRevisionBefore($current);
39               $revision->getVersion() > 0;
40               $revision = $page->getRevisionBefore($revision) ) {
41
42             if ($revision->get('is_minor_edit'))
43                 $keep = $counter['minor']->keep($revision);
44             else
45                 $keep = $counter['major']->keep($revision);
46
47             if ($this->isMergeable($revision)) {
48                 if (!$keep) {
49                     $page->mergeRevision($revision);
50                 }
51             }
52             else {
53                 $author_id = $revision->get('author_id');
54                 if (empty($authors_seen[$author_id])) {
55                     if ($counter['author']->keep($revision))
56                         $keep = true;
57                     $authors_seen[$author_id] = true;
58                 }
59                 if (!$keep) {
60                     $page->deleteRevision($revision);
61                 }
62             }
63         }
64     }
65 }
66
67 /**
68  * @access private
69  */
70 class ArchiveCleaner_Counter
71 {
72     function ArchiveCleaner_Counter($params) {
73         
74         extract($params);
75         $INFINITY = 0x7fffffff;
76
77         $this->max_keep = isset($max_keep) ? $max_keep : $INFINITY;
78
79         $this->min_age  = isset($min_age)  ? $min_age  : 0;
80         $this->min_keep = isset($min_keep) ? $min_keep : 0;
81
82         $this->max_age  = isset($max_age)  ? $max_age  : $INFINITY;
83         $this->keep     = isset($keep)     ? $keep     : $INFINITY;
84
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;
89
90         if ($this->min_age > $this->max_age)
91             $this->min_age = $this->max_age;
92             
93         $this->now = time();
94         $this->count = 0;
95         $this->previous_supplanted = false;
96         
97     }
98
99     function computeAge($revision) {
100         $supplanted = $revision->get('_supplanted');
101
102         if (!$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;
110             else {
111                 // no supplanted timestamp.
112                 // don't delete this revision based on age.
113                 return 0;
114             }
115         }
116
117         $this->last_supplanted = $supplanted;
118         return ($this->now - $supplanted) / (24 * 3600);
119     }
120         
121     function keep($revision) {
122         $count = ++$this->count;
123         $age = $this->computeAge($revision);
124         
125         if ($count > $this->max_keep)
126             return false;
127         if ($age <= $this->min_age || $count <= $this->min_keep)
128             return true;
129         return $age <= $this->max_age && $count <= $this->keep;
130     }
131 }
132
133
134 // Local Variables:
135 // mode: php
136 // tab-width: 8
137 // c-basic-offset: 4
138 // c-hanging-comment-ender-p: nil
139 // indent-tabs-mode: nil
140 // End:   
141 ?>