| 1 | package Kwiki::Edit::AdvisoryLock; | ||
| 2 | |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use Kwiki::Plugin '-Base'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 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/(\<a.*?href\s*=\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/\<textarea/\<div class\=\"warning\"\>$warning\<\/div\>\<textarea/i; | ||
| 82 | return $ret; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub warning { | ||
| 86 | my $page_name = shift; | ||
| 87 | my $lock_file = io->catfile($self->plugin_base_directory, 'edit', | ||
| 88 | 'lock', $page_name); | ||
| 89 | my $user = $lock_file->slurp; | ||
| 90 | my $locktime = $self->hub->have_plugin('time_zone') | ||
| 91 | ? $self->hub->time_zone->format($lock_file->mtime) | ||
| 92 | : $self->format_time($lock_file->mtime); | ||
| 93 | |||
| 94 | return <<WARNING; | ||
| 95 | <h1>Another user may be editing this file!</h1> | ||
| 96 | |||
| 97 | <p>User $user started editing this page on $locktime. $user has yet to save | ||
| 98 | his/her edits. You can continue editing this page if you wish but a conflict | ||
| 99 | may occur if $user is still editing the page and plans to submit his/her | ||
| 100 | changes.</p> | ||
| 101 | WARNING | ||
| 102 | } | ||
| 103 | |||
| 104 | sub format_time { | ||
| 105 | my $unix_time = shift; | ||
| 106 | my $formatted = scalar gmtime $unix_time; | ||
| 107 | $formatted .= ' GMT' | ||
| 108 | unless $formatted =~ /GMT$/; | ||
| 109 | return $formatted; | ||
| 110 | } | ||
| 111 | |||
| 112 | 1; # End of Kwiki::Edit::AdvisoryLock | ||
| 113 | __DATA__ | ||