lib/PurpleWiki/Database/Page.pm

filter: Statementstmt 15.8% 19 / 120 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 16.6% 5 / 30 Podpod 0.0% 0 / 21 totaltotal 12.5% 24 / 191 CC 10 SCAR 14.1
CC
10
Cov
14%
openPage
12.0
new
6.0
getText
6.0
CRAP 4.1
« lib/PurpleWiki/Database/Text.pm ↑ Summary lib/PurpleWiki/Database/Section.pm »
1 # PurpleWiki::Database::Page
2 # vi:sw=4:ts=4:ai:sm:et:tw=0
3 #
4 # $Id: Page.pm 428 2004-07-26 00:46:41Z 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::Page;
32  
33 # PurpleWiki Page Data Access
34  
35 # $Id: Page.pm 428 2004-07-26 00:46:41Z 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 1 ▶use PurpleWiki::Database::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 1 our $VERSION;
44 1 $VERSION = sprintf("%d", q$Id: Page.pm 428 2004-07-26 00:46:41Z cdent $ =~ /\s(\d+)\s/);
45  
46 # defaults for Text Based data structure
47 1 my $DATA_VERSION = 3; # the data format version
48  
49 # Creates a new page reference, may be a
50 # a new one or an existing one. Expects args of
51 # at least 'id', will also take 'now' for the time of
52 # the current CGI request and 'userID' and 'username' to
53 # be passed to Section for the creation of new Text.
54 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
55 0 my $proto = shift;
56 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
57 0 my %args = @_;
58 0 my $self = { %args };
59 0 $self->{config} = PurpleWiki::Config->instance();
60 0 bless ($self, $class);
61 0 return $self;
62 }
63  
64 # A shim to facillitate other callers
65 0 ▶sub pageExists {
Subroutine pageExists: not called CC=1 SCAR=6.9
Pod undocumented
66 0 my $self = shift;
67 0 return $self->pageFileExists();
68 }
69  
70 # Returns true if the page file associated with this
71 # page exists.
72 0 ▶sub pageFileExists {
Subroutine pageFileExists: not called CC=1 SCAR=6.9
Pod undocumented
73 0 my $self = shift;
74  
75 0 my $filename = $self->getPageFile();
76  
77 0 return (-f $filename);
78 }
79  
80 # Returns the revision of this Page.
81 0 ▶sub getRevision {
Subroutine getRevision: not called CC=1 SCAR=6.9
Pod undocumented
82 0 my $self = shift;
83 0 return $self->{revision};
84 }
85  
86 # Sets the revision of this Page.
87 0 ▶sub setRevision {
Subroutine setRevision: not called CC=1 SCAR=6.9
Pod undocumented
88 0 my $self = shift;
89 0 my $revision = shift;
90 0 $self->{revision} = $revision;
91 }
92  
93 # Gets the timestamp of this Page.
94 0 ▶sub getTS {
Subroutine getTS: not called CC=1 SCAR=6.9
Pod undocumented
95 0 my $self = shift;
96 0 return $self->{ts};
97 }
98  
99 # Sets the timestamp of this Page.
100 0 ▶sub setTS {
Subroutine setTS: not called CC=1 SCAR=6.9
Pod undocumented
101 0 my $self = shift;
102 0 my $ts = shift;
103 0 $self->{ts} = $ts;
104 }
105  
106 # Gets one of a few different cache data items for this Page.
107 0 ▶sub getPageCache {
Subroutine getPageCache: not called CC=1 SCAR=6.9
Pod undocumented
108 0 my $self = shift;
109 0 my $cache = shift;
110
111 0 return $self->{"cache_$cache"};
112 }
113  
114 # Sets one of a few different cache data items for this Page
115 # to the provided value.
116 0 ▶sub setPageCache {
Subroutine setPageCache: not called CC=1 SCAR=6.9
Pod undocumented
117 0 my $self = shift;
118 0 my $cache = shift;
119 0 my $revision = shift;
120  
121 0 $self->{"cache_$cache"} = $revision;
122 }
123  
124 # Opens the page file associated with the id of this
125 # Page.
126 0 ▶sub openPage {
Subroutine openPage: not called CC=3 SCAR=24.8
Pod undocumented
127 0 my $self = shift;
128  
129 0 ▶ if ($self->pageFileExists()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pageFileExists) { } 0 0 0
130 0 my $filename = $self->getPageFile();
131 # FIXME: there should be a utility class of some kind
132 0 my $data = PurpleWiki::Database::ReadFileOrDie($filename);
133 0 $self->_parseData($data);
134 } else {
135 0 $self->_openNewPage();
136 }
137  
138 0 ▶ if ($self->getVersion() != $DATA_VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getVersion != $DATA_VERSION) 0 0 0
139 0 $self->_updatePageVersion();
140 }
141 }
142  
143 # Retrieves the default text data by getting the
144 # Section and then the text in that Section.
145 # Or creates a new one.
146 0 ▶sub getText {
Subroutine getText: not called CC=2 SCAR=17.9
Pod undocumented
147 0 my $self = shift;
148  
149 0 ▶ if (!defined($self->{text_default})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'text_default'}) { } 0 0 0
150 0 return $self->createNewText();
151 } else {
152 0 my $section = $self->getSection();
153 0 return $section->getText();
154 }
155 }
156  
157 # Retrieves the Section if it already
158 # exists. If not a new one is created
159 # and returned.
160 0 ▶sub getSection {
Subroutine getSection: not called CC=2 SCAR=17.9
Pod undocumented
161 0 my $self = shift;
162  
163 0 ▶ if (ref($self->{text_default})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'text_default'}) { } 0 0 0
164 0 return $self->{text_default};
165 } else {
166 $self->{text_default} =
167 new PurpleWiki::Database::Section('data' => $self->{text_default},
168 'now' => $self->getNow(),
169 'userID' => $self->{userID},
170 0 'username' => $self->{username});
171 0 return $self->{text_default};
172 }
173 }
174  
175 # Creates an empty new Text and Section
176 0 ▶sub createNewText {
Subroutine createNewText: not called CC=1 SCAR=6.9
Pod undocumented
177 0 my $self = shift;
178 0 my $section = $self->getSection();
179 0 return $section->getText();
180 }
181  
182 # Retrives the version of this page.
183 0 ▶sub getVersion {
Subroutine getVersion: not called CC=1 SCAR=6.9
Pod undocumented
184 0 my $self = shift;
185 0 return $self->{version};
186 }
187  
188 # Retrieves the id of this page.
189 0 ▶sub getID {
Subroutine getID: not called CC=1 SCAR=6.9
Pod undocumented
190 0 my $self = shift;
191 0 return $self->{id};
192 }
193  
194 # Retrieves the now of when this page was asked for.
195 0 ▶sub getNow {
Subroutine getNow: not called CC=1 SCAR=6.9
Pod undocumented
196 0 my $self = shift;
197 0 return $self->{now};
198 }
199  
200 # Determines the filename of the page with this id.
201 0 ▶sub getPageFile {
Subroutine getPageFile: not called CC=1 SCAR=6.9
Pod undocumented
202 0 my $self = shift;
203  
204 0 return $self->{config}->PageDir . '/' . $self->getPageDirectory() . '/' .
205 $self->getID() . '.db';
206 }
207  
208 # Determines the directory of this Page.
209 0 ▶sub getPageDirectory {
Subroutine getPageDirectory: not called CC=2 SCAR=17.9
Pod undocumented
210 0 my $self = shift;
211  
212 0 my $directory = 'other';
213  
214 0 ▶ if ($self->getID() =~ /^([a-zA-Z])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getID =~ /^([a-zA-Z])/) 0 0 0
215 0 $directory = uc($1);
216 }
217  
218 0 return $directory;
219 }
220  
221 # Causes an error because the data in the
222 # Page file is out of date.
223 0 ▶sub _updatePageVersion {
Subroutine _updatePageVersion: not called CC=1 SCAR=6.9
224 0 my $self = shift;
225  
226 # FIXME: ugly, but quick
227 0 die('Bad page version (or corrupt page)');
228 }
229  
230 # Parses the data read in from a page file.
231 # FIXME: the profiler considers this sub
232 # somewhat more expensive than some others. Can
233 # the multi step name be collapsed?
234 0 ▶sub _parseData {
Subroutine _parseData: not called CC=2 SCAR=17.9
235 0 my $self = shift;
236 0 my $data = shift;
237  
238 0 my $regexp = $self->{config}->FS1;
239 0 my %tempHash = split(/$regexp/, $data, -1);
240
241 0 foreach my $key (keys(%tempHash)) {
242 0 $self->{$key} = $tempHash{$key};
243 }
244  
245 0 $self->{text_default} = $self->getSection();
246 }
247  
248 # Sets the minium data fields necessary for a Page.
249 0 ▶sub _openNewPage {
Subroutine _openNewPage: not called CC=1 SCAR=6.9
250 0 my $self = shift;
251  
252 0 $self->{version} = 3;
253 0 $self->{revision} = 0;
254 0 $self->{ts_create} = $self->getNow();
255 0 $self->{ts} = $self->getNow();
256 }
257  
258 # Saves the Page by serialize it and its constituent parts to
259 # a string and then writing to disk.
260 0 ▶sub save {
Subroutine save: not called CC=1 SCAR=6.9
Pod undocumented
261 0 my $self = shift;
262  
263 0 my $data = $self->serialize();
264  
265 0 $self->_createPageDir();
266 0 PurpleWiki::Database::WriteStringToFile($self->getPageFile(), $data);
267 }
268  
269 # Gets the path and filename for the lock file for
270 # this page.
271 0 ▶sub getLockedPageFile {
Subroutine getLockedPageFile: not called CC=1 SCAR=6.9
Pod undocumented
272 0 my $self = shift;
273 0 my $id = $self->getID();
274 0 return $self->{config}->PageDir . '/' . $self->getPageDirectory() . "/$id.lck";
275 }
276  
277 # Creates the directory where this Page is stored.
278 0 ▶sub _createPageDir {
Subroutine _createPageDir: not called CC=2 SCAR=17.9
279 0 my $self = shift;
280 0 my $id = $self->getID();
281 0 my $dir = $self->{config}->PageDir;
282 0 my $subdir;
283  
284 0 PurpleWiki::Database::CreateDir($dir); # Make sure main page exists
285 0 $subdir = $dir . '/' . $self->getPageDirectory();
286 0 PurpleWiki::Database::CreateDir($subdir);
287  
288 0 ▶ if ($id =~ m|([^/]+)/|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ m[([^/]+)/]) 0 0 0
289 0 $subdir = $subdir . '/' . $1;
290 0 PurpleWiki::Database::CreateDir($subdir);
291 }
292 }
293  
294 # Serializes the data structure to a string. Calls serialize
295 # on Section which in turns calls serialize on Text.
296 0 ▶sub serialize {
Subroutine serialize: not called CC=2 SCAR=17.9
Pod undocumented
297 0 my $self = shift;
298  
299 0 my $sectionData = $self->getSection()->serialize();
300  
301 0 my $separator = $self->{config}->FS1;
302  
303 0 ▶ my $data = join($separator, map {$_ . $separator . ($self->{$_} || '')}
Condition0%0/2
$self->{$_} || ''
l!l
00
MC/DC0%0/1
$self->{$_} || ''
$self->{$_}
Truth table0%0/2
A
A= $self->{$_}
Aresult
11
00
304 ('version', 'revision', 'cache_oldmajor', 'cache_oldauthor',
305 'cache_diff_default_major', 'cache_diff_default_minor',
306 'ts_create', 'ts'));
307  
308 0 $data .= $separator . 'text_default' . $separator . $sectionData;
309  
310 0 return $data;
311 }
312  
313 1 1;
« lib/PurpleWiki/Database/Text.pm ↑ Summary lib/PurpleWiki/Database/Section.pm »