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