File Coverage

blib/lib/Kwiki/Edit/AdvisoryLock.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Kwiki::Edit::AdvisoryLock;
2              
3 1     1   23905 use warnings;
  1         3  
  1         43  
4 1     1   6 use strict;
  1         2  
  1         37  
5 1     1   398 use Kwiki::Plugin '-Base';
  0            
  0            
6             use mixin 'Kwiki::Installer';
7             use URI::Escape;
8             our $VERSION = '0.01';
9              
10             const class_title => 'Advisory Lock';
11             const class_id => 'advisory_lock';
12              
13             sub register {
14             my $registry = shift;
15             $registry->add(action => 'unlock');
16             $registry->add(hook => 'edit:edit',
17             pre => 'lock_page',
18             post => 'rewrite_links',
19             );
20             $registry->add(hook => 'edit:edit',
21             post => 'add_warning'
22             );
23             $registry->add(hook => 'edit:edit',
24             post => 'unlock_after_save',
25             );
26             }
27              
28             sub unlock {
29             my $page_name = CGI::param('page_name');
30             my $redirect = CGI::param('redirect');
31              
32             io->catfile($self->plugin_base_directory, 'edit', 'lock',
33             $page_name)->unlink;
34             $self->redirect($redirect);
35             }
36              
37             sub unlock_after_save {
38             if( $self->cgi->button eq $self->config->edit_save_button_text ) {
39             io->catfile($self->plugin_directory, 'lock',
40             $self->pages->current->title)->unlink;
41             }
42             return $_[-1]->returned;
43             }
44              
45             sub lock_page {
46             my $user = $self->hub->users->current->name;
47             return if $self->cgi->button ne '' ||
48             !$self->pages->current->is_writable ||
49             $user eq $self->config->user_default_name;
50              
51             my $path = io->catdir($self->plugin_directory, 'lock')->mkpath;
52             $path = io->catfile($path, $self->pages->current->title);
53             $user > $path unless $path->exists;
54             }
55              
56             sub rewrite_links {
57             my $hook = pop;
58             my $page_name = $self->pages->current->title;
59             my $ret = $hook->returned;
60             return $ret
61             unless Kwiki::Edit::AdvisoryLock::own_lock($self, $page_name);
62             $ret =~ s/(\]*\>)/"$1?action=unlock;page_name=$page_name;redirect=".uri_escape($2).$3/gei;
63             return $ret;
64             }
65              
66             sub own_lock {
67             my $page_name = shift;
68             my $lock = io->catfile($self->plugin_base_directory, 'edit', 'lock',
69             $page_name);
70             $lock->exists ? $self->hub->users->current->name eq $lock->slurp : 0;
71             }
72              
73             sub add_warning {
74             my $hook = pop;
75             my $page_name = $self->pages->current->title;
76             my $ret = $hook->returned;
77             return $ret if Kwiki::Edit::AdvisoryLock::own_lock($self, $page_name);
78             return $ret unless io->catfile($self->plugin_base_directory, 'edit',
79             'lock', $page_name)->exists;
80             my $warning = Kwiki::Edit::AdvisoryLock::warning($self, $page_name);
81             $ret =~ s/\