]>
Commit | Line | Data |
---|---|---|
cfabc2e9 DM |
1 | #!/usr/bin/perl |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use Data::Dumper; | |
6 | ||
7 | use IO::File; | |
8 | use File::Basename; | |
9 | use MediaWiki::API; | |
3f4666b4 | 10 | use HTML::Parser; |
cfabc2e9 | 11 | |
b489b02c DM |
12 | use JSON; |
13 | ||
14 | my $data_str = ""; | |
15 | while (<main::DATA>) { $data_str .= $_; } | |
16 | ||
17 | my $fileinfo = decode_json($data_str); | |
18 | ||
cfabc2e9 DM |
19 | my $config_fn = "/root/.pve-docs"; # format 'username:pw' |
20 | ||
c5bf6350 | 21 | my $fh = IO::File->new("$config_fn") || |
cfabc2e9 DM |
22 | die "Please configure the mediawiki user/passswd in '$config_fn'\n"; |
23 | ||
a45b098f | 24 | my $api_url = "http://localhost/mediawiki/api.php"; |
cfabc2e9 DM |
25 | |
26 | my $config = <$fh>; | |
27 | chomp $config; | |
28 | ||
29 | my ($username, $passwd) = split(':', $config, 2); | |
30 | ||
31 | my $mw = MediaWiki::API->new(); | |
32 | $mw->{config}->{api_url} = $api_url; | |
33 | ||
34 | # log in to the wiki | |
35 | $mw->login({ lgname => $username, lgpassword => $passwd }) | |
36 | || die $mw->{error}->{code} . ': ' . $mw->{error}->{details}; | |
37 | ||
38 | sub update_page { | |
3f4666b4 | 39 | my ($pagename, $filename, $category) = @_; |
cfabc2e9 DM |
40 | |
41 | print "update mediawiki page: $pagename\n"; | |
42 | ||
43 | my $ref = $mw->get_page( { title => $pagename } ); | |
44 | my $page = $ref->{'*'} || ''; | |
45 | ||
3f4666b4 DM |
46 | my $pve_content = "<!-- Do not edit - this is autogenerated content -->\n"; |
47 | ||
48 | $pve_content .= "{{#pvedocs:$filename}}\n"; | |
49 | $pve_content .= "[[Category:$category]]\n" if $category; | |
50 | ||
51 | my $starttag = '<!--PVE_IMPORT_START_MARKER-->'; | |
52 | my $endtag = '<!--PVE_IMPORT_END_MARKER-->'; | |
cfabc2e9 | 53 | |
3f4666b4 | 54 | $pve_content .= "<pvehide>\n"; |
cfabc2e9 | 55 | |
3f4666b4 DM |
56 | my $parser_opts = { |
57 | api_version => 3, | |
58 | text_h => [ sub { $pve_content .= shift }, "dtext" ], | |
59 | }; | |
60 | my $parser = HTML::Parser->new(%$parser_opts); | |
61 | ||
62 | my $fh = IO::File->new("/usr/share/pve-docs/$filename", "r") or | |
63 | die "unable to open file '$filename' - $!\n"; | |
64 | while (defined(my $line = <$fh>)) { | |
65 | $parser->parse($line); | |
66 | } | |
67 | $pve_content .= "</pvehide>\n"; | |
68 | ||
69 | $pve_content =~ s/\s+$//gm; | |
70 | ||
71 | chomp $pve_content; | |
72 | ||
73 | if ($page =~ m/^(.*)$starttag\n.*\n$endtag(.*)$/s) { | |
74 | my ($top_content, $bottom_content) = ($1, $2); | |
75 | chomp $top_content; | |
76 | chomp $bottom_content; | |
77 | $page = $top_content; | |
78 | $page .= "$starttag\n"; | |
79 | $page .= $pve_content; | |
80 | $page .= "\n$endtag\n"; | |
81 | $page .= $bottom_content; | |
82 | } elsif ($page =~ m/(.*)\{\{#pvedocs:.*?\}\}(.*)$/) { | |
83 | # old style | |
84 | my ($top_content, $bottom_content) = ($1, $2); | |
85 | chomp $top_content; | |
86 | chomp $bottom_content; | |
87 | $page = $top_content; | |
88 | $page .= "$starttag\n"; | |
89 | $page .= $pve_content; | |
90 | $page .= "\n$endtag\n"; | |
91 | $page .= $bottom_content; | |
92 | } else { | |
93 | $page = "$starttag\n$pve_content\n$endtag\n$page"; | |
cfabc2e9 | 94 | } |
c5bf6350 | 95 | |
cfabc2e9 DM |
96 | my $timestamp = $ref->{timestamp}; |
97 | my $wcmd = { | |
98 | action => 'edit', | |
99 | title => $pagename, | |
100 | basetimestamp => $timestamp, # to avoid edit conflicts | |
101 | text => $page, | |
102 | }; | |
103 | ||
c5bf6350 | 104 | $mw->edit($wcmd) || |
cfabc2e9 DM |
105 | die $mw->{error}->{code} . ': ' . $mw->{error}->{details}; |
106 | } | |
107 | ||
108 | my $cat_refdoc = "Reference Documentation"; | |
109 | ||
b489b02c DM |
110 | |
111 | my $docs = {}; | |
112 | foreach my $source (sort keys %{$fileinfo->{toplevel}->{wiki}}) { | |
113 | my $title = $fileinfo->{titles}->{wiki}->{$source}; | |
40688099 DM |
114 | my $filename = $fileinfo->{outfile}->{wiki}->{$source} || |
115 | die "found no file name mapping for '$source'"; | |
cfabc2e9 | 116 | |
cfabc2e9 DM |
117 | my $path = "/usr/share/pve-docs/$filename"; |
118 | die "no such file '$path'" if ! -f $path; | |
c5bf6350 DM |
119 | |
120 | update_page($title, $filename, $cat_refdoc); | |
cfabc2e9 | 121 | } |
11e7e157 DM |
122 | |
123 | # also update 'Get support' page, because this is used since a long | |
124 | # time and is referenced from outside | |
b489b02c DM |
125 | update_page("Get support", 'getting-help-plain.html', 'HOWTO'); |
126 | ||
127 | __END__ |