lib/PurpleWiki/Database/KeptRevision.pm

filter: Statementstmt 14.4% 15 / 104 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 21.0% 4 / 19 Podpod 0.0% 0 / 12 totaltotal 12.3% 19 / 154 CC 14 SCAR 19.3
CC
14
Cov
12%
hasRevision
12.0
getRevision
12.0
trimKepts
12.0
CRAP 6.9
« lib/PurpleWiki/View/rawtext.pm ↑ Summary lib/PurpleWiki/Search/Google.pm »
1 # PurpleWiki::Database::KeptRevision
2 # vi:sw=4:ts=4:ai:sm:et:tw=0
3 #
4 # $Id: KeptRevision.pm 399 2004-06-25 05:31:46Z cdent $
5 #
6 # Copyright (c) Blue Oxen Associates 2002-2003. All rights reserved.
7 #
8 # This file is part of PurpleWiki. PurpleWiki is derived from:
9 #
10 # UseModWiki v0.92 (c) Clifford A. Adams 2000-2001
11 # AtisWiki v0.3 (c) Markus Denker 1998
12 # CVWiki CVS-patches (c) Peter Merel 1997
13 # The Original WikiWikiWeb (c) Ward Cunningham
14 #
15 # PurpleWiki is free software; you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation; either version 2 of the License, or
18 # (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the
27 # Free Software Foundation, Inc.
28 # 59 Temple Place, Suite 330
29 # Boston, MA 02111-1307 USA
30  
31 package PurpleWiki::Database::KeptRevision;
32  
33 # PurpleWiki Page Data Access
34  
35 # $Id: KeptRevision.pm 399 2004-06-25 05:31:46Z cdent $
36  
37 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use PurpleWiki::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use PurpleWiki::Database;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use PurpleWiki::Database::Section;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 1 our $VERSION;
43 1 $VERSION = sprintf("%d", q$Id: KeptRevision.pm 399 2004-06-25 05:31:46Z cdent $ =~ /\s(\d+)\s/);
44  
45 # Creates a new kept revision reference
46 # Really just a collection of Sections
47 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
48 0 my $proto = shift;
49 0 my %params = @_;
50 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
51 0 my $self = {};
52 0 bless ($self, $class);
53  
54 0 $self->{id} = $params{id};
55 0 $self->{config} = PurpleWiki::Config->instance();
56 0 $self->{sections} = ();
57 0 $self->_makeKeptList();
58  
59 0 return $self;
60 }
61  
62 # Retrieves a list of references to the Sections in
63 # this KeptRevision
64 0 ▶sub getSections {
Subroutine getSections: not called CC=1 SCAR=6.9
Pod undocumented
65 0 my $self = shift;
66  
67 0 return @{$self->{sections}};
68 }
69  
70  
71 # Determines if this collection of KeptRevisions has the given revision
72 0 ▶sub hasRevision {
Subroutine hasRevision: not called CC=3 SCAR=24.8
Pod undocumented
73 0 my $self = shift;
74 0 my $revision = shift;
75  
76 # FIXME: a hash of revision number to sections is
77 # probably in order
78 0 foreach my $section (@{$self->{sections}}) {
79 0 my $sectionRevision = $section->getRevision();
80 0 ▶ return 1 if $revision == $sectionRevision;
Branch: 1 branch
BranchTrueFalseTotal
if $revision == $sectionRevision 0 0 0
81 }
82 0 return 0;
83 }
84  
85 # Retrieves the Section representing to a particular revision
86 0 ▶sub getRevision {
Subroutine getRevision: not called CC=3 SCAR=24.8
Pod undocumented
87 0 my $self = shift;
88 0 my $revision = shift;
89  
90 0 foreach my $section (@{$self->{sections}}) {
91 0 my $sectionRevision = $section->getRevision();
92 0 ▶ return $section if $revision == $sectionRevision;
Branch: 1 branch
BranchTrueFalseTotal
if $revision == $sectionRevision 0 0 0
93 }
94 # FIXME, should probably error here, for now return
95 # most recent
96 0 return pop(@{$self->{sections}});
97 }
98  
99 # Adds the provided Section to this KeptRevision
100 0 ▶sub addSection {
Subroutine addSection: not called CC=1 SCAR=6.9
Pod undocumented
101 0 my $self = shift;
102 0 my $section = shift;
103 0 my $now = shift;
104 0 $section->setKeepTS($now);
105 0 push(@{$self->{sections}}, $section);
106 }
107  
108 # Trims this KeptRevision to include only those
109 # less than $KeepDays old
110 0 ▶sub trimKepts {
Subroutine trimKepts: not called CC=3 SCAR=24.8
Pod undocumented
111 0 my $self = shift;
112 0 my $now = shift;
113  
114 0 my $expirets = $now - ($self->{config}->KeepDays * 24 * 60 * 60);
115  
116 # was using undef here but that doesn't work,
117 # must use splice
118 0 my $count = 0;
119 0 foreach my $section (@{$self->{sections}}) {
120 0 ▶ if ($section->getKeepTS() < $expirets) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section->getKeepTS < $expirets) 0 0 0
121 0 splice(@{$self->{sections}}, $count, 1);
122 }
123 0 $count++;
124 }
125 }
126  
127 # Tests to see if kept file for this KeptRevision exists
128 0 ▶sub keptFileExists {
Subroutine keptFileExists: not called CC=1 SCAR=6.9
Pod undocumented
129 0 my $self = shift;
130  
131 0 my $filename = $self->getKeepFile();
132  
133 0 return (-f $filename);
134 }
135  
136 # Determins the filename of the keep page with this id.
137 0 ▶sub getKeepFile {
Subroutine getKeepFile: not called CC=1 SCAR=6.9
Pod undocumented
138 0 my $self = shift;
139  
140 0 return $self->{config}->KeepDir . '/' . $self->getKeepDirectory() . '/' .
141 $self->getID() . '.kp';
142 }
143  
144 # Starts the process of creating the list of Sections
145 # that make up this KeptRevision by reading in the
146 # KeepFile and sending it to _parseData()
147 0 ▶sub _makeKeptList {
Subroutine _makeKeptList: not called CC=2 SCAR=17.9
148 0 my $self = shift;
149 0 my $data;
150  
151 0 ▶ if ($self->keptFileExists()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keptFileExists) 0 0 0
152 0 my $filename = $self->getKeepFile();
153 0 $data = PurpleWiki::Database::ReadFileOrDie($filename);
154 0 $self->_parseData($data);
155 }
156 }
157  
158 # Parses the KeepFile, turning the contents into
159 # Sections.
160 0 ▶sub _parseData {
Subroutine _parseData: not called CC=3 SCAR=24.8
161 0 my $self = shift;
162 0 my $data = shift;
163  
164 0 my $regexp = $self->{config}->FS1;
165 0 foreach my $section (split(/$regexp/, $data, -1)) {
166 # because of the usemod way of saving data, the first
167 # field is empty
168 0 ▶ if (length($section)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $section) 0 0 0
169 0 push(@{$self->{sections}},
170 new PurpleWiki::Database::Section('data' => $section));
171
172 }
173 }
174  
175 }
176  
177 # Retrieves the page ID associated with this KeptRevision
178 0 ▶sub getID {
Subroutine getID: not called CC=1 SCAR=6.9
Pod undocumented
179 0 my $self = shift;
180 0 return $self->{id};
181 }
182  
183 # Save the KeptRevision by rewriting the entire file.
184 0 ▶sub save {
Subroutine save: not called CC=1 SCAR=6.9
Pod undocumented
185 0 my $self = shift;
186 0 my $data = $self->serialize();
187  
188 0 $self->_createKeepDir();
189 0 PurpleWiki::Database::WriteStringToFile($self->getKeepFile(), $data);
190 }
191  
192 # Creats the directory where KeptRevisions are stored. Uses
193 # Database::CreateDir which only creates the directory if it
194 # is not there.
195 0 ▶sub _createKeepDir {
Subroutine _createKeepDir: not called CC=2 SCAR=17.9
196 0 my $self = shift;
197 0 my $id = $self->getID();
198 0 my $dir = $self->{config}->KeepDir;
199 0 my $subdir;
200  
201 0 PurpleWiki::Database::CreateDir($dir); # Make sure main page exists
202 0 $subdir = $dir . '/' . $self->getKeepDirectory();
203 0 PurpleWiki::Database::CreateDir($subdir);
204  
205 0 ▶ if ($id =~ m|([^/]+)/|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ m[([^/]+)/]) 0 0 0
206 0 $subdir = $subdir . '/' . $1;
207 0 PurpleWiki::Database::CreateDir($subdir);
208 }
209 }
210  
211 # Determines the directory where this KeptRevisions is
212 # saved.
213 0 ▶sub getKeepDirectory {
Subroutine getKeepDirectory: not called CC=2 SCAR=17.9
Pod undocumented
214 0 my $self = shift;
215  
216 0 my $directory = 'other';
217  
218 0 ▶ if ($self->getID() =~ /^([a-zA-Z])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getID =~ /^([a-zA-Z])/) 0 0 0
219 0 $directory = uc($1);
220 }
221  
222 0 return $directory;
223 }
224
225 # Serializes the list of Sections to a string that can
226 # be written to disk.
227 0 ▶sub serialize {
Subroutine serialize: not called CC=2 SCAR=17.9
Pod undocumented
228 0 my $self = shift;
229  
230 0 my $data;
231 my $section;
232 0 foreach $section ($self->getSections()) {
233 0 $data .= $self->{config}->FS1;
234 0 $data .= $section->serialize();
235 }
236  
237 0 return $data;
238 }
239  
240 1 1;
241  
« lib/PurpleWiki/View/rawtext.pm ↑ Summary lib/PurpleWiki/Search/Google.pm »