File Coverage

blib/lib/Quiki/Pages.pm
Criterion Covered Total %
statement 15 63 23.8
branch 0 28 0.0
condition 0 4 0.0
subroutine 5 12 41.6
pod 7 7 100.0
total 27 114 23.6


line stmt bran cond sub pod time code
1             package Quiki::Pages;
2              
3 1     1   6 use warnings;
  1         2  
  1         33  
4 1     1   5 use strict;
  1         2  
  1         31  
5              
6 1     1   981 use File::Slurp 'slurp';
  1         16772  
  1         75  
7 1     1   896 use Text::Patch;
  1         2397  
  1         53  
8 1     1   867 use Text::Diff;
  1         10580  
  1         833  
9              
10             sub unlock {
11 0     0 1   my ($class, $node) = @_;
12 0 0         unlink "data/locks/$node" if -f "data/locks/$node";
13             }
14              
15             sub locked {
16 0     0 1   my ($class, $node, $user) = @_;
17 0 0         if (-f "data/locks/$node") {
18 0 0         if (-M "data/locks/$node" < 0.01) {
19 0 0         if ($user) {
20 0           return (slurp("data/locks/$node") ne $user);
21             }
22             else {
23 0           return 1;
24             }
25             } else {
26 0           return 0;
27             }
28             } else {
29 0           return 0;
30             }
31             }
32              
33              
34             sub locked_for_user {
35 0     0 1   my ($class, $node, $user) = @_;
36 0 0         return 0 unless $user;
37 0 0         return 0 unless -f "data/locks/$node";
38 0 0         return 0 unless -M "data/locks/$node" < 0.01;
39 0           return (slurp("data/locks/$node") eq $user);
40             }
41              
42             sub lock {
43 0     0 1   my ($class, $node, $user) = @_;
44              
45 0 0         if ($user) {
46 0 0         open LOCK, "> data/locks/$node" or die;
47 0           print LOCK $user;
48 0           close LOCK;
49             }
50             }
51              
52             sub check_in {
53 0     0 1   my ($class, $Quiki, $node, $contents) = @_;
54              
55             # XXX nasty check, needed for diff
56 0 0         $contents .= "\n" unless ($contents =~ m/\n$/);
57              
58 0   0       my $rev = $Quiki->{meta}{rev} || 0;
59              
60 0 0         if ($rev > 0) {
61 0           my $current = slurp "data/content/$node";
62 0           my $diff = diff(\$contents, \$current, { STYLE=>'Unified' });
63              
64 0 0         open F, ">data/revs/$node.$rev" or die $!;
65 0           print F $diff;
66 0           close F;
67             }
68              
69 0           my $file = "data/content/$node";
70              
71             #if (defined($contents)) {
72 0 0         unless ($contents =~ m/^Edit me!/) {
73 0           $Quiki->{meta}{revs}{$Quiki->{meta}{rev}}{last_update_by} = $Quiki->{meta}{last_update_by};
74 0           $Quiki->{meta}{revs}{$Quiki->{meta}{rev}}{last_updated_in} = $Quiki->{meta}{last_updated_in};
75 0           $Quiki->{meta}{rev}++ ;
76             }
77              
78              
79 0 0         open O, "> $file" or die $!;
80 0           print O $contents;
81 0           close O;
82             #}
83             #else {
84             # unlink "data/contents/$node"
85             #}
86             }
87              
88             sub check_out {
89 0     0 1   my ($class, $Quiki, $node, $rev) = @_;
90              
91 0           my $cur_rev = $Quiki->{meta}{rev};
92 0           my $content = slurp "data/content/$node";
93              
94 0   0       while (($rev || 0) < $cur_rev--) {
95 0           my $patch = slurp "data/revs/$node.$cur_rev";
96              
97 0           $content = patch($content, $patch, {STYLE=>'Unified'});
98             }
99              
100 0           return $content;
101             }
102              
103             sub calc_diff {
104 0     0 1   my ($class, $Quiki, $node, $rev, $target) = @_;
105              
106 0           my $one = Quiki::Pages->check_out($Quiki, $node, $rev);
107 0           my $two = Quiki::Pages->check_out($Quiki, $node, $target);
108              
109 0           diff(\$one, \$two, { STYLE=>'Unified' });
110             }
111              
112             '\o/';
113              
114             =encoding UTF-8
115              
116             =head1 NAME
117              
118             Quiki::Users - Quiki pages manager
119              
120             =head1 SYNOPSIS
121              
122             use Quiki::Pages;
123              
124             # lock a node
125             Quiki::Pages->lock($node, $self->{sid});
126              
127             # unlock a node
128             Quiki::Pages->unlock($node);
129              
130             # verify lock status
131             $locked = Quiki::Pages->locked($node, $self->{sid}))
132              
133             # check in new content
134             Quiki::Pages->check_in($self, $node, $content);
135              
136             # retrieve content
137             $content = Quiki::Pages->check_out($self,$node,$rev);
138              
139             =head1 DESCRIPTION
140              
141             This module is handles the needed operations to maintain the pages
142             information. It is used to gain and free locks to edit pages, and
143             implements a simple revision system for page's content.
144              
145             =head2 lock
146              
147             This method is used to gain a lock to edit a given page.
148              
149             =head2 unlock
150              
151             This method is used to free a lock to edit a given page.
152              
153             =head2 locked
154              
155             This method is used to verify if exists a lock to a given page. It
156             returns false (page not locked) if there isn't a lock or, if a user is
157             supplied and that user owns the lock.
158              
159             =head2 locked_for_user
160              
161             This method is similar to C<< locked >> and needs that a user is supplied.
162             It returns true only if the page is locked by the supplied user.
163              
164             =head2 check_in
165              
166             This method is used to update new content to a page. It creates
167             a diff file and increments the revision number.
168              
169             =head2 check_out
170              
171             This method returns the content for a given page and revision
172             number.
173              
174             =head2 calc_diff
175              
176             This method calculates the diff between any two given revisions for
177             a page.
178              
179             =head1 SEE ALSO
180              
181             Quiki, perl(1)
182              
183             =head1 AUTHOR
184              
185             Alberto Simões, Eambs@cpan.orgE
186             Nuno Carvalho, Esmash@cpan.orgE
187              
188             =head1 COPYRIGHT & LICENSE
189              
190             Copyright 2009-2010 Alberto Simoes and Nuno Carvalho.
191              
192             This program is free software; you can redistribute it and/or modify it
193             under the terms of either: the GNU General Public License as published
194             by the Free Software Foundation; or the Artistic License.
195              
196             See http://dev.perl.org/licenses/ for more information.
197              
198             =cut
199