]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/ArchiveCleaner.php
Use ...version() $need_content argument in WikiDB also:
[SourceForge/phpwiki.git] / lib / ArchiveCleaner.php
1 <?php rcs_id('$Id: ArchiveCleaner.php,v 1.4 2004-06-29 08:52:22 rurban 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(false);
37
38         for ( $revision = $page->getRevisionBefore($current,false);
39               $revision->getVersion() > 0;
40               $revision = $page->getRevisionBefore($revision,false) ) {
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         if (!empty($params))
75             extract($params);
76         $INFINITY = 0x7fffffff;
77
78         $this->max_keep = isset($max_keep) ? $max_keep : $INFINITY;
79
80         $this->min_age  = isset($min_age)  ? $min_age  : 0;
81         $this->min_keep = isset($min_keep) ? $min_keep : 0;
82
83         $this->max_age  = isset($max_age)  ? $max_age  : $INFINITY;
84         $this->keep     = isset($keep)     ? $keep     : $INFINITY;
85
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;
90
91         if ($this->min_age > $this->max_age)
92             $this->min_age = $this->max_age;
93             
94         $this->now = time();
95         $this->count = 0;
96         $this->previous_supplanted = false;
97         
98     }
99
100     function computeAge($revision) {
101         $supplanted = $revision->get('_supplanted');
102
103         if (!$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()),
109                           E_USER_NOTICE);
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;
114             else {
115                 // no supplanted timestamp.
116                 // don't delete this revision based on age.
117                 return 0;
118             }
119         }
120
121         $this->previous_supplanted = $supplanted;
122         return ($this->now - $supplanted) / (24 * 3600);
123     }
124         
125     function keep($revision) {
126         $count = ++$this->count;
127         $age = $this->computeAge($revision);
128         
129         if ($count > $this->max_keep)
130             return false;
131         if ($age <= $this->min_age || $count <= $this->min_keep)
132             return true;
133         return $age <= $this->max_age && $count <= $this->keep;
134     }
135 }
136
137
138 // Local Variables:
139 // mode: php
140 // tab-width: 8
141 // c-basic-offset: 4
142 // c-hanging-comment-ender-p: nil
143 // indent-tabs-mode: nil
144 // End:   
145 ?>