lib/PurpleWiki/Database.pm

filter: Statementstmt 22.6% 39 / 172 Branchbran 5.4% 4 / 74 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 50.0% 10 / 20 Podpod 0.0% 0 / 13 totaltotal 17.2% 53 / 307 CC 48 SCAR 47.1
CC
48
Cov
16%
recentChanges
273.1
AllPagesList
182.0
UpdateDiffs
42.0
CRAP 111.0
« lib/PurpleWiki/View/xhtml.pm ↑ Summary lib/PurpleWiki/Transclusion.pm »
1 # PurpleWiki::Database.pm
2 # vi:sw=4:ts=4:ai:sm:et:tw=0
3 #
4 # $Id: Database.pm 448 2004-08-06 11:25:09Z eekim $
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;
32  
33 # PurpleWiki Page Data Access
34  
35 # $Id: Database.pm 448 2004-08-06 11:25:09Z eekim $
36  
37 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶use PurpleWiki::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 2 our $VERSION;
41 2 $VERSION = sprintf("%d", q$Id: Database.pm 448 2004-08-06 11:25:09Z eekim $ =~ /\s(\d+)\s/);
42  
43 # Reads a string from a given filename and returns the data.
44 # If it cannot open the file, it dies with an error.
45 # Public
46 0 ▶sub ReadFileOrDie {
Subroutine ReadFileOrDie: not called CC=2 SCAR=17.9
Pod undocumented
47 0 my $fileName = shift;
48 0 my ($status, $data);
49  
50 0 ($status, $data) = ReadFile($fileName);
51 0 ▶ if (!$status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status) 0 0 0
52 0 die("Can not open $fileName: $!");
53 }
54 0 return $data;
55 }
56  
57 # Reads a string from a given filename and returns a
58 # status value and the string. 1 for success, 0 for
59 # failure.
60 # Public
61 1 ▶sub ReadFile {
Subroutine ReadFile: called CC=2 SCAR=7.1
Pod undocumented
62 1 my $fileName = shift;
63 1 my ($data);
64 1 local $/ = undef; # Read complete files
65  
66 1 ▶ if (open(IN, "<$fileName")) {
Branch: 1 branch
BranchTrueFalseTotal
if (open IN, "<$fileName") 1 0 1
67 1 $data=<IN>;
68 1 close IN;
69 1 return (1, $data);
70 }
71 0 return (0, "");
72 }
73  
74 # Creates a directory if it doesn't already exist.
75 # FIXME: there should be some error checking here.
76 # Public
77 2 ▶sub CreateDir {
Subroutine CreateDir: called CC=2 SCAR=7.2
Pod undocumented
78 2 my $newdir = shift;
79  
80 2 ▶ mkdir($newdir, 0775) if (!(-d $newdir));
Branch: 1 branch
BranchTrueFalseTotal
if not -d $newdir 2 0 2
81 }
82  
83 # Creates a diff using Text::Diff
84 # We require it in here rather than at the top in
85 # case we never need it in the current running
86 # process.
87 # Private
88 0 ▶sub _GetDiff {
Subroutine _GetDiff: not called CC=1 SCAR=6.9
89 0 require Text::Diff;
90 0 my ($old, $new, $lock) = @_;
91  
92 0 my $diff_out = Text::Diff::diff(\$old, \$new, {STYLE => "OldStyle"});
93 0 return $diff_out;
94 }
95  
96 # Creates a directory that acts as a general locking
97 # mechanism for the system.
98 # FIXME: ForceReleaseLock (below) is not immediately accessible
99 # to mortals.
100 # Private.
101 1 ▶sub _RequestLockDir {
Subroutine _RequestLockDir: called CC=5 SCAR=22.7
102 1 my ($name, $tries, $wait, $errorDie) = @_;
103 1 my ($lockName, $n);
104 1 my $config = PurpleWiki::Config->instance();
105  
106 1 CreateDir($config->TempDir);
107 1 $lockName = $config->LockDir . $name;
108 1 $n = 0;
109 1 ▶ while (mkdir($lockName, 0555) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (mkdir($lockName, 365) == 0) 0 1 1
110 0 ▶ if ($! != 17) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! != 17) 0 0 0
111 0 ▶ die("can not make $lockName: $!\n") if $errorDie;
Branch: 1 branch
BranchTrueFalseTotal
if $errorDie 0 0 0
112 0 return 0;
113 }
114 0 ▶ return 0 if ($n++ >= $tries);
Branch: 1 branch
BranchTrueFalseTotal
if $n++ >= $tries 0 0 0
115 0 sleep($wait);
116 }
117 1 return 1;
118 }
119  
120 # Removes the locking directory, destroying the lock
121 # Private
122 1 ▶sub _ReleaseLockDir {
Subroutine _ReleaseLockDir: called CC=1 SCAR=0.0
123 1 my ($name) = @_;
124 1 my $config = PurpleWiki::Config->instance();
125 1 rmdir($config->LockDir . $name);
126 }
127  
128 # Requests a general editing lock for the system.
129 # Public
130 1 ▶sub RequestLock {
Subroutine RequestLock: called CC=1 SCAR=0.0
Pod undocumented
131 # 10 tries, 3 second wait, die on error
132 1 return _RequestLockDir("main", 10, 3, 1);
133 }
134  
135 # Releases the general editing lock
136 # Public
137 1 ▶sub ReleaseLock {
Subroutine ReleaseLock: called CC=1 SCAR=0.0
Pod undocumented
138 1 _ReleaseLockDir('main');
139 }
140  
141 # Forces the lock to be released
142 # Public
143 0 ▶sub ForceReleaseLock {
Subroutine ForceReleaseLock: not called CC=1 SCAR=6.9
Pod undocumented
144 0 my ($name) = @_;
145 0 my $forced;
146  
147 # First try to obtain lock (in case of normal edit lock)
148 # 5 tries, 3 second wait, do not die on error
149 0 $forced = !_RequestLockDir($name, 5, 3, 0);
150 0 _ReleaseLockDir($name); # Release the lock, even if we didn't get it.
151 0 return $forced;
152 }
153  
154 # Writes the given string to the given file. Dies
155 # if it can't write.
156 # Public
157 1 ▶sub WriteStringToFile {
Subroutine WriteStringToFile: called CC=2 SCAR=7.0
Pod undocumented
158 1 my $file = shift;
159 1 my $string = shift;
160  
161 1 ▶ open (OUT, ">$file") or die("can't write $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">$file" or die "can't write ${file}: $!" 1 0 1
162 1 print OUT $string;
163 1 close(OUT);
164 }
165  
166 # Not used?
167 0 ▶sub AppendStringToFile {
Subroutine AppendStringToFile: not called CC=2 SCAR=17.9
Pod undocumented
168 0 my ($file, $string) = @_;
169  
170 0 ▶ open (OUT, ">>$file") or die("can't write $file $!");
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">>$file" or die "can't write $file $!" 0 0 0
171 0 print OUT $string;
172 0 close(OUT);
173 }
174  
175 # Creates and returns an array containing a list of all the
176 # wiki pages in the database.
177 # Public
178 0 ▶sub AllPagesList {
Subroutine AllPagesList: not called CC=13 SCAR=52.0
Pod undocumented
179 0 my $config = PurpleWiki::Config->instance();
180 0 my (@pages, @dirs, $id, $dir, @pageFiles, @subpageFiles, $subId);
181  
182 0 @pages = ();
183 # The following was inspired by the FastGlob code by Marc W. Mengel.
184 # Thanks to Bob Showalter for pointing out the improvement.
185 0 opendir(PAGELIST, $config->PageDir);
186 0 @dirs = readdir(PAGELIST);
187 0 closedir(PAGELIST);
188 0 @dirs = sort(@dirs);
189 0 foreach $dir (@dirs) {
190 0 ▶ next if (($dir eq '.') || ($dir eq '..'));
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "." or $dir eq ".." 0 0 0
Condition0%0/3
$dir eq "." or $dir eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$dir eq "." or $dir eq ".."
$dir eq "." $dir eq ".."
Truth table0%0/3
A or B
A= $dir eq "."B= $dir eq ".."
ABresult
1X1
011
000
191 0 my $directory = $config->PageDir . "/$dir";
192 0 opendir(PAGELIST, $directory);
193 0 @pageFiles = readdir(PAGELIST);
194 0 closedir(PAGELIST);
195 0 foreach $id (@pageFiles) {
196 0 ▶ next if (($id eq '.') || ($id eq '..'));
Branch: 1 branch
BranchTrueFalseTotal
if $id eq "." or $id eq ".." 0 0 0
Condition0%0/3
$id eq "." or $id eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$id eq "." or $id eq ".."
$id eq "." $id eq ".."
Truth table0%0/3
A or B
A= $id eq "."B= $id eq ".."
ABresult
1X1
011
000
197 0 ▶ if (substr($id, -3) eq '.db') {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($id, -3) eq '.db') { } 0 0 0
elsif (substr($id, -4) ne '.lck') { } 0 0 0
198 0 my $pageName = substr($id, 0, -3);
199 0 ▶ $pageName =~ s/_/ /g if ($config->FreeLinks);
Branch: 1 branch
BranchTrueFalseTotal
if $config->FreeLinks 0 0 0
200 0 push(@pages, {
201 'id' => substr($id, 0, -3),
202 'pageName' => $pageName,
203 });
204 } elsif (substr($id, -4) ne '.lck') {
205 0 opendir(PAGELIST, "$directory/$id");
206 0 @subpageFiles = readdir(PAGELIST);
207 0 closedir(PAGELIST);
208 0 foreach $subId (@subpageFiles) {
209 0 ▶ if (substr($subId, -3) eq '.db') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($subId, -3) eq ".db") 0 0 0
210 0 my $pageName = "$id/" . substr($subId, 0, -3);
211 0 ▶ $pageName =~ s/_/ /g if ($config->FreeLinks);
Branch: 1 branch
BranchTrueFalseTotal
if $config->FreeLinks 0 0 0
212 0 push(@pages, {
213 'id' => "$id/" . substr($subId, 0, -3),
214 'pageName' => $pageName,
215 });
216 }
217 }
218 }
219 }
220 }
221 0 return sort { $a->{id} cmp $b->{id} } @pages;
222 }
223  
224 # Updates the diffs keps for a page.
225 # Public
226 0 ▶sub UpdateDiffs {
Subroutine UpdateDiffs: not called CC=6 SCAR=37.4
Pod undocumented
227 0 my $page = shift;
228 0 my $keptRevision = shift;
229 0 my ($id, $editTime, $old, $new, $isEdit, $newAuthor) = @_;
230 0 my ($editDiff, $oldMajor, $oldAuthor);
231 0 my $config = PurpleWiki::Config->instance();
232  
233 0 $editDiff = _GetDiff($old, $new, 0); # 0 = already in lock
234 0 $oldMajor = $page->getPageCache('oldmajor');
235 0 $oldAuthor = $page->getPageCache('oldauthor');
236 0 ▶ if ($config->UseDiffLog) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->UseDiffLog) 0 0 0
237 0 _WriteDiff($id, $editTime, $editDiff);
238 }
239 0 $page->setPageCache('diff_default_minor', $editDiff);
240  
241 0 ▶ if (!$isEdit) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $isEdit) { } 0 0 0
242 0 $page->setPageCache('diff_default_major', "1");
243 } else {
244 0 $page->setPageCache('diff_default_major',
245 GetKeptDiff($keptRevision, $new, $oldMajor, 0));
246 }
247  
248 0 ▶ if ($newAuthor) {
Branch: 3 branches
BranchTrueFalseTotal
if ($newAuthor) { } 0 0 0
elsif ($oldMajor == $oldAuthor) { } 0 0 0
elsif ($oldMajor == $oldAuthor) { } 0 0 0
249 0 $page->setPageCache('diff_default_author', "1");
250 } elsif ($oldMajor == $oldAuthor) {
251 0 $page->setPageCache('diff_default_author', "2");
252 } elsif ($oldMajor == $oldAuthor) {
253 0 $page->setPageCache('diff_default_author', "2");
254 } else {
255 0 $page->setPageCache('diff_default_author',
256 GetKeptDiff($keptRevision, $new, $oldAuthor, 0));
257 }
258 }
259  
260 # Retrieves a cached diff for a page.
261 # Public
262 0 ▶sub GetCacheDiff {
Subroutine GetCacheDiff: not called CC=3 SCAR=24.8
Pod undocumented
263 0 my ($page, $type) = @_;
264 0 my ($diffText);
265  
266 0 $diffText = $page->getPageCache("diff_default_$type");
267 0 ▶ $diffText = GetCacheDiff($page, 'minor') if ($diffText eq "1");
Branch: 1 branch
BranchTrueFalseTotal
if $diffText eq 1 0 0 0
268 0 ▶ $diffText = GetCacheDiff($page, 'major') if ($diffText eq "2");
Branch: 1 branch
BranchTrueFalseTotal
if $diffText eq 2 0 0 0
269 0 return $diffText;
270 }
271  
272 # Retrieves the diff of an old kept revision
273 # Public
274 0 ▶sub GetKeptDiff {
Subroutine GetKeptDiff: not called CC=3 SCAR=24.8
Pod undocumented
275 0 my $keptRevision = shift;
276 0 my ($newText, $oldRevision, $lock) = @_;
277  
278 0 my $section = $keptRevision->getRevision($oldRevision);
279 0 ▶ return "" if (!defined $section); # there is no old revision
Branch: 1 branch
BranchTrueFalseTotal
unless defined $section 0 0 0
280 0 my $oldText = $section->getText()->getText();
281  
282 0 ▶ return "" if ($oldText eq ""); # Old revision not found
Branch: 1 branch
BranchTrueFalseTotal
if $oldText eq "" 0 0 0
283 0 return _GetDiff($oldText, $newText, $lock);
284 }
285  
286 # Writes out a diff to the diff log.
287 # Private
288 0 ▶sub _WriteDiff {
Subroutine _WriteDiff: not called CC=2 SCAR=17.9
289 0 my ($id, $editTime, $diffString) = @_;
290 0 my $config = PurpleWiki::Config->instance();
291  
292 0 my $directory = $config->DataDir;
293 0 ▶ open (OUT, ">>$directory/diff_log") or die('can not write diff_log');
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">>$directory/diff_log" or die "can not write diff_log" 0 0 0
294 0 print OUT "------\n" . $id . "|" . $editTime . "\n";
295 0 print OUT $diffString;
296 0 close(OUT);
297 }
298  
299 # Populates a hash reference with recent changes.
300 # Data structure:
301 # $recentChanges = [
302 # { timeStamp => , # time stamp
303 # name => , # page name
304 # numChanges => , # number of times changed
305 # summary => , # change summary
306 # userName => , # username
307 # userId => , # user ID
308 # host => , # hostname
309 # },
310 # ...
311 # ]
312 0 ▶sub recentChanges {
Subroutine recentChanges: not called CC=17 SCAR=56.1
Pod undocumented
313 0 my ($config, $timeStamp) = @_;
314 0 my @recentChanges;
315 my %pages;
316  
317 # Default to showing all changes.
318 0 ▶ $timeStamp = 0 if not defined $timeStamp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeStamp 0 0 0
319  
320 # Convert timeStamp to seconds since the epoch if it's not already in
321 # that form.
322 0 ▶ if (not $timeStamp =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($timeStamp =~ /^\d+$/) 0 0 0
323 2 ▶ use Date::Manip;
Subroutine BEGIN: called CC=1 SCAR=0.0
324 0 ▶ $timeStamp = abs(UnixDate($timeStamp, "%o")) || 0;
Condition0%0/2
abs UnixDate($timeStamp, '%o') || 0
l!l
00
MC/DC0%0/1
abs UnixDate($timeStamp, '%o') || 0
abs UnixDate($timeStamp, '%o')
Truth table0%0/2
A
A= abs UnixDate($timeStamp, '%o')
Aresult
11
00
325 }
326  
327 ### FIXME: There's also an OldRcFile. Should we read this also?
328 ### What is it for, anyway?
329 0 ▶ if (open(IN, $config->RcFile)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open IN, $config->RcFile) 0 0 0
330 # parse logfile into pages hash
331 0 ▶ while (my $logEntry = <IN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $logEntry = readline IN)) 0 0 0
332 0 chomp $logEntry;
333 0 my $fsexp = $config->FS3;
334 0 my @entries = split /$fsexp/, $logEntry;
335 0 ▶ if (scalar @entries >= 6 && $entries[0] >= $timeStamp) { # Check timestamp
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @entries >= 6 and $entries[0] >= $timeStamp) 0 0 0
Condition0%0/3
scalar @entries >= 6 and $entries[0] >= $timeStamp
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @entries >= 6 and $entries[0] >= $timeStamp
scalar @entries >= 6 $entries[0] >= $timeStamp
Truth table0%0/3
A and B
A= scalar @entries >= 6B= $entries[0] >= $timeStamp
ABresult
0X0
100
111
336 0 my $name = $entries[1];
337 0 my $pageName = $name;
338  
339 0 ▶ if ($config->FreeLinks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->FreeLinks) 0 0 0
340 0 $pageName =~ s/_/ /g;
341 }
342 0 ▶ if ( $pages{$name} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($pages{$name} and $pages{$name}{'timeStamp'} > $entries[0]) { } 0 0 0
Condition0%0/3
$pages{$name} and $pages{$name}{"timeStamp"} > $entries[0]
!ll&&!rl&&r
000
MC/DC0%0/2
$pages{$name} and $pages{$name}{"timeStamp"} > $entries[0]
$pages{$name} $pages{$name}{"timeStamp"} > $entries[0]
Truth table0%0/3
A and B
A= $pages{$name}B= $pages{$name}{"timeStamp"} > $entries[0]
ABresult
0X0
100
111
343 ($pages{$name}->{timeStamp} > $entries[0]) ) {
344 0 $pages{$name}->{numChanges}++;
345 }
346 else {
347 0 ▶ if ($pages{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pages{$name}) { } 0 0 0
348 0 $pages{$name}->{numChanges}++;
349 }
350 else {
351 0 $pages{$name}->{numChanges} = 1;
352 0 $pages{$name}->{pageName} = $pageName;
353 }
354 0 $pages{$name}->{timeStamp} = $entries[0];
355 0 ▶ if ($entries[2] ne '' && $entries[2] ne '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($entries[2] ne '' and $entries[2] ne '*') { } 0 0 0
Condition0%0/3
$entries[2] ne "" and $entries[2] ne "*"
!ll&&!rl&&r
000
MC/DC0%0/2
$entries[2] ne "" and $entries[2] ne "*"
$entries[2] ne "" $entries[2] ne "*"
Truth table0%0/3
A and B
A= $entries[2] ne ""B= $entries[2] ne "*"
ABresult
0X0
100
111
356 0 $pages{$name}->{summary} = $entries[2];
357 }
358 else {
359 0 $pages{$name}->{summary} = '';
360 }
361 0 $pages{$name}->{minorEdit} = $entries[3];
362 0 $pages{$name}->{host} = $entries[4];
363  
364 # $entries[5] is garbage and so we ignore it...
365  
366 # Get extra info
367 0 my $fsexp = $config->FS2;
368 0 my %userInfo = split /$fsexp/, $entries[6];
369 0 ▶ if ($userInfo{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($userInfo{'id'}) { } 0 0 0
370 0 $pages{$name}->{userId} = $userInfo{id};
371 }
372 else {
373 0 $pages{$name}->{userId} = '';
374 }
375 0 ▶ if ($userInfo{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($userInfo{'name'}) { } 0 0 0
376 0 $pages{$name}->{userName} = $userInfo{name};
377 }
378 else {
379 0 $pages{$name}->{userName} = '';
380 }
381 }
382 }
383 }
384 0 close(IN);
385  
386 }
387 # now parse pages hash into final data structure and return
388 0 foreach my $name (sort { $pages{$b}->{timeStamp} <=> $pages{$a}->{timeStamp} } keys %pages) {
389 push @recentChanges, { timeStamp => $pages{$name}->{timeStamp},
390 id => $name,
391 pageName => $pages{$name}->{pageName},
392 numChanges => $pages{$name}->{numChanges},
393 summary => $pages{$name}->{summary},
394 userName => $pages{$name}->{userName},
395 userId => $pages{$name}->{userId},
396 0 host => $pages{$name}->{host} };
397 }
398 0 return \@recentChanges;
399 }
400  
401 2 1;
« lib/PurpleWiki/View/xhtml.pm ↑ Summary lib/PurpleWiki/Transclusion.pm »