fix dynamic generation skip bug
[scpubgit/System-Introspector-Report.git] / lib / System / Introspector / Report / Publish / MediaWiki / Connection.pm
CommitLineData
499ebcdd 1package System::Introspector::Report::Publish::MediaWiki::Connection;
2use Moo;
0331d9cd 3use MediaWiki::API;
4use URI;
5use aliased 'System::Introspector::Report::Publish::MediaWiki::Page';
6
7a479e54 7has api => (is => 'lazy');
8has api_uri => (is => 'ro', required => 1);
9has username => (is => 'ro', required => 1);
10has password => (is => 'ro', required => 1);
11has allow_create => (is => 'ro', default => sub { 0 });
12has http_auth => (is => 'ro');
13has http_realm => (is => 'ro');
0331d9cd 14
15my $_api_fail = sub {
16 my $api = shift;
17 die sprintf "MediaWiki Error %s: %s\n",
18 $api->{error}{code},
19 $api->{error}{details};
20};
21
22sub _build_api {
23 my ($self) = @_;
24 my $api_uri = URI->new($self->api_uri);
25 my $api = MediaWiki::API->new({ api_url => $self->api_uri });
26 my $passwd = $self->password;
7a479e54 27 if ($ENV{TEST_SI_MEDIAWIKI_GETPW}) {
0331d9cd 28 system "stty -echo";
29 printf "MediaWiki password for '%s': ", $self->username;
30 $passwd = <STDIN>;
31 chomp $passwd;
32 system "stty echo";
33 print "\n";
34 }
35 $api->{ua}->credentials(
36 join(':', $api_uri->host, $api_uri->port),
37 $self->http_realm || 'Wiki',
38 $self->username,
39 $passwd,
40 ) if $self->http_auth;
41 $api->login({
42 lgname => $self->username,
43 lgpassword => $passwd,
44 }) or $api->$_api_fail;
45 return $api;
46}
499ebcdd 47
21e7cc98 48sub get {
49 my ($self, $page_name) = @_;
0331d9cd 50 my $page = $self->api->get_page({ title => $page_name })
51 or $self->api->$_api_fail;
52 if (defined $page->{missing}) {
53 die "MediaWiki page '$page_name' does not exist\n"
7a479e54 54 unless $self->allow_create;
0331d9cd 55 return Page->new(
56 name => $page_name,
57 content => '',
58 );
59 }
60 return Page->new(
61 name => $page_name,
62 timestamp => $page->{timestamp},
63 content => $page->{'*'} || '',
64 );
21e7cc98 65}
499ebcdd 66
21e7cc98 67sub put {
68 my ($self, $page) = @_;
0331d9cd 69 $self->api->edit({
70 action => 'edit',
71 title => $page->name,
72 text => $page->content,
4ce41cee 73 summary => 'System-Introspector-Report Update',
62499a9b 74 bot => 1,
0331d9cd 75 $page->has_timestamp
76 ? (basetimestamp => $page->timestamp)
77 : (),
78 }) or $self->api->$_api_fail;
79 return 1;
21e7cc98 80}
499ebcdd 81
821;