]> git.proxmox.com Git - pve-docs.git/blob - pve-docs-mediawiki-import.in
scan-adoc-refs: sort make targets
[pve-docs.git] / pve-docs-mediawiki-import.in
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
11 use JSON;
12
13 my $data_str = "";
14 while (<main::DATA>) { $data_str .= $_; }
15
16 my $fileinfo = decode_json($data_str);
17
18 my $config_fn = "/root/.pve-docs"; # format 'username:pw'
19
20 my $fh = IO::File->new("$config_fn") ||
21 die "Please configure the mediawiki user/passswd in '$config_fn'\n";
22
23 my $api_url = "http://localhost/mediawiki/api.php";
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 }
58
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
67 $mw->edit($wcmd) ||
68 die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
69 }
70
71 my $cat_refdoc = "Reference Documentation";
72
73
74 my $docs = {};
75 foreach my $source (sort keys %{$fileinfo->{toplevel}->{wiki}}) {
76 my $title = $fileinfo->{titles}->{wiki}->{$source};
77 my $filename = $fileinfo->{outfile}->{wiki}->{$source} ||
78 die "found no file name mapping for '$source'";
79
80 my $path = "/usr/share/pve-docs/$filename";
81 die "no such file '$path'" if ! -f $path;
82
83 update_page($title, $filename, $cat_refdoc);
84 }
85
86 # also update 'Get support' page, because this is used since a long
87 # time and is referenced from outside
88 update_page("Get support", 'getting-help-plain.html', 'HOWTO');
89
90 __END__