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