]> git.proxmox.com Git - pve-docs.git/blame - pve-docs-mediawiki-import.in
do not set toplevel attribute for default env
[pve-docs.git] / pve-docs-mediawiki-import.in
CommitLineData
cfabc2e9
DM
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use Data::Dumper;
6
7use IO::File;
8use File::Basename;
9use MediaWiki::API;
10
b489b02c
DM
11use JSON;
12
13my $data_str = "";
14while (<main::DATA>) { $data_str .= $_; }
15
16my $fileinfo = decode_json($data_str);
17
cfabc2e9
DM
18my $config_fn = "/root/.pve-docs"; # format 'username:pw'
19
c5bf6350 20my $fh = IO::File->new("$config_fn") ||
cfabc2e9
DM
21 die "Please configure the mediawiki user/passswd in '$config_fn'\n";
22
a45b098f 23my $api_url = "http://localhost/mediawiki/api.php";
cfabc2e9
DM
24
25my $config = <$fh>;
26chomp $config;
27
28my ($username, $passwd) = split(':', $config, 2);
29
30my $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
37sub 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
71my $cat_refdoc = "Reference Documentation";
72
b489b02c
DM
73
74my $docs = {};
75foreach 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
88update_page("Get support", 'getting-help-plain.html', 'HOWTO');
89
90__END__