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