]>
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; | |
10 | ||
b489b02c DM |
11 | use JSON; |
12 | ||
13 | my $data_str = ""; | |
14 | while (<main::DATA>) { $data_str .= $_; } | |
15 | ||
16 | my $fileinfo = decode_json($data_str); | |
17 | ||
cfabc2e9 DM |
18 | my $config_fn = "/root/.pve-docs"; # format 'username:pw' |
19 | ||
c5bf6350 | 20 | my $fh = IO::File->new("$config_fn") || |
cfabc2e9 DM |
21 | die "Please configure the mediawiki user/passswd in '$config_fn'\n"; |
22 | ||
a45b098f | 23 | my $api_url = "http://localhost/mediawiki/api.php"; |
cfabc2e9 DM |
24 | |
25 | my $config = <$fh>; | |
26 | chomp $config; | |
27 | ||
28 | my ($username, $passwd) = split(':', $config, 2); | |
29 | ||
30 | my $mw = MediaWiki::API->new(); | |
31 | $mw->{config}->{api_url} = $api_url; | |
32 | ||
33 | # log in to the wiki | |
34 | $mw->login({ lgname => $username, lgpassword => $passwd }) | |
35 | || die $mw->{error}->{code} . ': ' . $mw->{error}->{details}; | |
36 | ||
37 | sub update_page { | |
38 | my ($pagename, $include, $category) = @_; | |
39 | ||
40 | print "update mediawiki page: $pagename\n"; | |
41 | ||
42 | my $ref = $mw->get_page( { title => $pagename } ); | |
43 | my $page = $ref->{'*'} || ''; | |
44 | ||
45 | if ($page !~ m/^\{\{#pvedocs:.*\}\}\s*$/m) { | |
46 | $page = "{{#pvedocs:$include}}\n$page"; | |
47 | } else { | |
48 | $page =~ s/^\{\{#pvedocs:.*\}\}\s*$/\{\{#pvedocs:$include\}\}\n/m; | |
49 | } | |
50 | ||
51 | if ($category) { | |
52 | my $catstr = "Category:$category"; | |
53 | ||
54 | if ($page !~ m/^\[\[$catstr\]\]\s*$/m) { | |
55 | $page .= "\n[[$catstr]]\n"; | |
56 | } | |
57 | } | |
c5bf6350 | 58 | |
cfabc2e9 DM |
59 | my $timestamp = $ref->{timestamp}; |
60 | my $wcmd = { | |
61 | action => 'edit', | |
62 | title => $pagename, | |
63 | basetimestamp => $timestamp, # to avoid edit conflicts | |
64 | text => $page, | |
65 | }; | |
66 | ||
c5bf6350 | 67 | $mw->edit($wcmd) || |
cfabc2e9 DM |
68 | die $mw->{error}->{code} . ': ' . $mw->{error}->{details}; |
69 | } | |
70 | ||
71 | my $cat_refdoc = "Reference Documentation"; | |
72 | ||
b489b02c DM |
73 | |
74 | my $docs = {}; | |
75 | foreach my $source (sort keys %{$fileinfo->{toplevel}->{wiki}}) { | |
76 | my $title = $fileinfo->{titles}->{wiki}->{$source}; | |
40688099 DM |
77 | my $filename = $fileinfo->{outfile}->{wiki}->{$source} || |
78 | die "found no file name mapping for '$source'"; | |
cfabc2e9 | 79 | |
cfabc2e9 DM |
80 | my $path = "/usr/share/pve-docs/$filename"; |
81 | die "no such file '$path'" if ! -f $path; | |
c5bf6350 DM |
82 | |
83 | update_page($title, $filename, $cat_refdoc); | |
cfabc2e9 | 84 | } |
11e7e157 DM |
85 | |
86 | # also update 'Get support' page, because this is used since a long | |
87 | # time and is referenced from outside | |
b489b02c DM |
88 | update_page("Get support", 'getting-help-plain.html', 'HOWTO'); |
89 | ||
90 | __END__ |