File Coverage

blib/lib/Mojito/Page/Publish.pm
Criterion Covered Total %
statement 12 33 36.3
branch 0 2 0.0
condition n/a
subroutine 4 7 57.1
pod n/a
total 16 42 38.1


line stmt bran cond sub pod time code
1 1     1   217995 use strictures 1;
  1         10  
  1         33  
2              
3             package Mojito::Page::Publish;
4             {
5             $Mojito::Page::Publish::VERSION = '0.24';
6             }
7 1     1   1162 use Moo;
  1         18489  
  1         9  
8 1     1   1992 use WWW::Mechanize;
  1         2  
  1         18  
9 1     1   951 use Data::Dumper::Concise;
  1         9271  
  1         639  
10              
11             =pod
12              
13             Starting with the ability to publish a Mojito page to a MojoMojo wiki.
14              
15             NEED:
16             - MM base_url
17             - MM username/password
18             - MM page name (path)
19             - some content
20              
21             =cut
22              
23             with('Mojito::Role::DB');
24             with('Mojito::Role::Config');
25              
26             has target_base_url => (
27             is => 'rw',
28             lazy => 1,
29             default => sub { $_[0]->config->{MM_base_url} },
30             );
31             has user => (
32             is => 'rw',
33             lazy => 1,
34             default => sub { $_[0]->config->{MM_user} },
35             );
36             has password => (
37             is => 'rw',
38             lazy => 1,
39             default => sub { $_[0]->config->{MM_password} },
40             );
41             has source_page => ( is => 'rw', );
42             has target_page => ( is => 'rw', );
43             has content => (
44             is => 'rw',
45             );
46             has page_id => (
47             is => 'rw',
48             );
49             has publish_form => (
50             is => 'rw',
51             lazy => 1,
52             builder => '_build_publish_form',
53             );
54             has mech => (
55             is => 'ro',
56             lazy => 1,
57             builder => '_build_mech',
58             );
59              
60             sub _build_mech {
61 0     0     my ( $self, ) = @_;
62 0           my $mech = WWW::Mechanize->new(ssl_opts => { verify_hostname => 0 });
63 0           my $base_url = $self->target_base_url;
64 0           my $login_page = $base_url . '.login';
65 0           $mech->get($login_page);
66 0           $mech->submit_form(
67             with_fields => {
68             login => $self->user,
69             pass => $self->password,
70             }
71             );
72 0           return $mech;
73             }
74              
75             =head1 Methods
76              
77             =head2 publish
78              
79             Get, Fillin and Post the Form for a Page
80              
81             =cut
82              
83             sub publish {
84 0     0     my $self = shift;
85              
86 0           my $mech = $self->mech;
87 0           $mech->get( $self->target_base_url . $self->target_page . '.edit' );
88 0           $mech->form_with_fields('body');
89 0           $mech->field( body => $self->content );
90 0           $mech->click_button( value => 'Save' );
91 0           return $mech->success;
92             }
93              
94              
95             sub _build_publish_form {
96 0     0     my $self = shift;
97            
98 0 0         return if not defined $self->target_base_url;
99 0           my $target_base_url = $self->target_base_url;
100 0           my $user = $self->user;
101 0           my $password = $self->password;
102 0           my $form =<<"END_FORM";
103             <div class="demo">
104              
105             <div id="dialog-form" title="Publish this page">
106             <form>
107             <fieldset>
108             <table>
109             <tr>
110             <td><label for="name">Page Name:</label></td>
111             <td><input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all" size="48" required /></td>
112             </tr>
113             <td><label for="target_base_url">Pub Base:</label></td>
114             <td><input type="text" name="target_base_url" id="target_base_url" value="$target_base_url" class="text ui-widget-content ui-corner-all" size="48" required /></td>
115             </tr>
116             <td><label for="user">User:</label>
117             <td><input type="text" name="user" id="user" value="$user" class="text ui-widget-content ui-corner-all" required /></td>
118             </tr>
119             <td><label for="password">Password</label>
120             <td><input type="password" name="password" id="password" value="$password" class="text ui-widget-content ui-corner-all" required /></td>
121             </tr>
122             </table>
123             </fieldset>
124             </form>
125             </div>
126             <button id="publish-page">Publish</button>
127              
128             </div>
129             END_FORM
130            
131 0           return $form;
132             }
133              
134             1