]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/ArchiveCleaner.php
Use __construct
[SourceForge/phpwiki.git] / lib / ArchiveCleaner.php
1 <?php
2
3 /* Copyright (C) 2002 Geoffrey T. Dairiki <dairiki@dairiki.org>
4  *
5  * This file is part of PhpWiki.
6  *
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.
11  *
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.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 class ArchiveCleaner
23 {
24     function __construct($expire_params)
25     {
26         $this->expire_params = $expire_params;
27     }
28
29     /**
30      * @param $revision
31      * @return bool
32      */
33     function isMergeable($revision)
34     {
35         if (!$revision->get('is_minor_edit'))
36             return false;
37
38         $page = $revision->getPage();
39         $author_id = $revision->get('author_id');
40
41         $previous = $page->getRevisionBefore($revision, false);
42
43         return !empty($author_id)
44             && $author_id == $previous->get('author_id');
45     }
46
47     function cleanDatabase($dbi)
48     {
49         $iter = $dbi->getAllPages();
50         while ($page = $iter->next())
51             $this->cleanPageRevisions($page);
52     }
53
54     function cleanPageRevisions($page)
55     {
56         $INFINITY = 0x7fffffff;
57
58         $expire = &$this->expire_params;
59         $counter = array();
60         foreach (array('major', 'minor', 'author') as $class)
61             $counter[$class] = new ArchiveCleaner_Counter($expire[$class]);
62         // shortcut to keep all
63         if (($counter['minor']->min_keep == $INFINITY)
64             and ($counter['major']->min_keep == $INFINITY)
65         )
66             return;
67
68         $authors_seen = array();
69
70         $current = $page->getCurrentRevision(false);
71
72         for ($revision = $page->getRevisionBefore($current, false);
73              $revision->getVersion() > 0;
74              $revision = $page->getRevisionBefore($revision, false)) {
75
76             if ($revision->get('is_minor_edit'))
77                 $keep = $counter['minor']->keep($revision);
78             else
79                 $keep = $counter['major']->keep($revision);
80
81             if ($this->isMergeable($revision)) {
82                 if (!$keep) {
83                     $page->mergeRevision($revision);
84                 }
85             } else {
86                 $author_id = $revision->get('author_id');
87                 if (empty($authors_seen[$author_id])) {
88                     if ($counter['author']->keep($revision))
89                         $keep = true;
90                     $authors_seen[$author_id] = true;
91                 }
92                 if (!$keep) {
93                     $page->deleteRevision($revision);
94                 }
95             }
96         }
97     }
98 }
99
100 /**
101  * @access private
102  */
103 class ArchiveCleaner_Counter
104 {
105     function __construct($params)
106     {
107
108         if (!empty($params))
109             extract($params);
110         $INFINITY = 0x7fffffff;
111
112         $this->max_keep = isset($max_keep) ? $max_keep : $INFINITY;
113
114         $this->min_age = isset($min_age) ? $min_age : 0;
115         $this->min_keep = isset($min_keep) ? $min_keep : 0;
116
117         $this->max_age = isset($max_age) ? $max_age : $INFINITY;
118         $this->keep = isset($keep) ? $keep : $INFINITY;
119
120         if ($this->keep > $this->max_keep)
121             $this->keep = $this->max_keep;
122         if ($this->min_keep == $INFINITY) { // shortcut to keep all
123             $this->max_keep = $this->keep = $this->min_age = $this->max_age = $INFINITY;
124         }
125         if ($this->min_keep > $this->keep)
126             $this->min_keep = $this->keep;
127
128         if ($this->min_age > $this->max_age)
129             $this->min_age = $this->max_age;
130
131         $this->now = time();
132         $this->count = 0;
133         $this->previous_supplanted = false;
134
135     }
136
137     function computeAge($revision)
138     {
139         $supplanted = $revision->get('_supplanted');
140
141         if (!$supplanted) {
142             // Every revision but the most recent should have a supplanted time.
143             // However, if it doesn't...
144             trigger_error(sprintf("Warning: Page ā€œ%sā€, version '%d' has no '_supplanted' timestamp",
145                     $revision->getPageName(),
146                     $revision->getVersion()),
147                 E_USER_NOTICE);
148             // Assuming revisions are chronologically ordered, the previous
149             // supplanted time is a good value to use...
150             if ($this->previous_supplanted > 0)
151                 $supplanted = $this->previous_supplanted;
152             else {
153                 // no supplanted timestamp.
154                 // don't delete this revision based on age.
155                 return 0;
156             }
157         }
158
159         $this->previous_supplanted = $supplanted;
160         return ($this->now - $supplanted) / (24 * 3600);
161     }
162
163     function keep($revision)
164     {
165         $INFINITY = 0x7fffffff;
166         if ($this->min_keep == $INFINITY)
167             return true;
168         $count = ++$this->count;
169         $age = $this->computeAge($revision);
170
171         if ($count > $this->max_keep)
172             return false;
173         if ($age <= $this->min_age || $count <= $this->min_keep)
174             return true;
175         return $age <= $this->max_age && $count <= $this->keep;
176     }
177 }
178
179 // Local Variables:
180 // mode: php
181 // tab-width: 8
182 // c-basic-offset: 4
183 // c-hanging-comment-ender-p: nil
184 // indent-tabs-mode: nil
185 // End: