blib/lib/Kwiki/Edit/BackgroundSave.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::BackgroundSave; | ||||||
2 | |||||||
3 | 1 | 1 | 22476 | use warnings; | |||
1 | 3 | ||||||
1 | 35 | ||||||
4 | 1 | 1 | 7 | use strict; | |||
1 | 2 | ||||||
1 | 39 | ||||||
5 | 1 | 1 | 466 | use Kwiki::Plugin '-Base'; | |||
0 | |||||||
0 | |||||||
6 | use mixin 'Kwiki::Installer'; | ||||||
7 | our $VERSION = '0.02'; | ||||||
8 | |||||||
9 | const class_title => 'Background Save'; | ||||||
10 | const class_id => 'background_save'; | ||||||
11 | const javascript_file => 'background_save.js'; | ||||||
12 | const config_file => 'background_save.yaml'; | ||||||
13 | |||||||
14 | sub register { | ||||||
15 | my $registry = shift; | ||||||
16 | |||||||
17 | $registry->add(prerequisite => 'prototype'); | ||||||
18 | $registry->add(preload => $self->class_id); | ||||||
19 | $registry->add(action => 'background_save'); | ||||||
20 | $registry->add(hook => 'edit:edit', | ||||||
21 | post => 'add_button' | ||||||
22 | ); | ||||||
23 | $registry->add(hook => 'headers:value', | ||||||
24 | post => 'add_x_json_header' | ||||||
25 | ); | ||||||
26 | } | ||||||
27 | |||||||
28 | sub background_save { | ||||||
29 | my $page = $self->hub->pages->current; | ||||||
30 | if( $page->modified_time != CGI::param('page_time') ) { | ||||||
31 | my %info = ( | ||||||
32 | problem => 'contention', | ||||||
33 | user => $page->metadata->edit_by || 'UnknownUser', | ||||||
34 | edittime => $page->edit_time, | ||||||
35 | ); | ||||||
36 | $self->hub->headers->json(%info); | ||||||
37 | return $page->modified_time; | ||||||
38 | } | ||||||
39 | $page->content(CGI::param('content')); | ||||||
40 | $page->update->store; | ||||||
41 | return $page->modified_time; | ||||||
42 | } | ||||||
43 | |||||||
44 | sub add_button { | ||||||
45 | my $hook = pop; | ||||||
46 | |||||||
47 | my $background_save = $self->config->edit_save_background_button_text; | ||||||
48 | my $page_name = $self->pages->current->title; | ||||||
49 | $background_save = < | ||||||
50 | |||||||
51 | onClick="do_background_save('$page_name')"/> | ||||||
52 | BUTTON | ||||||
53 | my $ret = $hook->returned; | ||||||
54 | $ret =~ s/(\ | ||||||
55 | $ret =~ s/(name\=\"page_content\")/$1 id\=\"page_content\"/i; | ||||||
56 | $ret =~ s/(name\=\"page_time\")/$1 id\=\"page_time\"/i; | ||||||
57 | return $ret; | ||||||
58 | } | ||||||
59 | |||||||
60 | sub add_x_json_header { | ||||||
61 | return defined $self->json | ||||||
62 | ? ('-X_JSON', '('.$self->json.')', $_[-1]->returned) | ||||||
63 | : $_[-1]->returned; | ||||||
64 | } | ||||||
65 | |||||||
66 | { | ||||||
67 | no warnings 'redefine'; | ||||||
68 | no strict 'refs'; | ||||||
69 | use JSON; | ||||||
70 | my $_json; | ||||||
71 | *Spoon::Headers::json = sub { | ||||||
72 | my ( $class, %args ) = @_; | ||||||
73 | $_json = objToJson(keys(%args) > 1 ? \%args : $_[1]) | ||||||
74 | if keys(%args) > 0; | ||||||
75 | return $_json; | ||||||
76 | } | ||||||
77 | } | ||||||
78 | |||||||
79 | package Kwiki::Edit::BackgroundSave; | ||||||
80 | 1; # End of Kwiki::Edit::BackgroundSave | ||||||
81 | __DATA__ |