pve-docs/pve-docs-mediawiki-import.in

91 lines
2.1 KiB
Perl
Executable File

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use IO::File;
use File::Basename;
use MediaWiki::API;
use JSON;
my $data_str = "";
while (<main::DATA>) { $data_str .= $_; }
my $fileinfo = decode_json($data_str);
my $config_fn = "/root/.pve-docs"; # format 'username:pw'
my $fh = IO::File->new("$config_fn") ||
die "Please configure the mediawiki user/passswd in '$config_fn'\n";
my $api_url = "http://localhost/mediawiki/api.php";
my $config = <$fh>;
chomp $config;
my ($username, $passwd) = split(':', $config, 2);
my $mw = MediaWiki::API->new();
$mw->{config}->{api_url} = $api_url;
# log in to the wiki
$mw->login({ lgname => $username, lgpassword => $passwd })
|| die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
sub update_page {
my ($pagename, $include, $category) = @_;
print "update mediawiki page: $pagename\n";
my $ref = $mw->get_page( { title => $pagename } );
my $page = $ref->{'*'} || '';
if ($page !~ m/^\{\{#pvedocs:.*\}\}\s*$/m) {
$page = "{{#pvedocs:$include}}\n$page";
} else {
$page =~ s/^\{\{#pvedocs:.*\}\}\s*$/\{\{#pvedocs:$include\}\}\n/m;
}
if ($category) {
my $catstr = "Category:$category";
if ($page !~ m/^\[\[$catstr\]\]\s*$/m) {
$page .= "\n[[$catstr]]\n";
}
}
my $timestamp = $ref->{timestamp};
my $wcmd = {
action => 'edit',
title => $pagename,
basetimestamp => $timestamp, # to avoid edit conflicts
text => $page,
};
$mw->edit($wcmd) ||
die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
}
my $cat_refdoc = "Reference Documentation";
my $docs = {};
foreach my $source (sort keys %{$fileinfo->{toplevel}->{wiki}}) {
my $title = $fileinfo->{titles}->{wiki}->{$source};
my $filename = $fileinfo->{outfile}->{wiki}->{$source} ||
die "found no file name mapping for '$source'";
my $path = "/usr/share/pve-docs/$filename";
die "no such file '$path'" if ! -f $path;
update_page($title, $filename, $cat_refdoc);
}
# also update 'Get support' page, because this is used since a long
# time and is referenced from outside
update_page("Get support", 'getting-help-plain.html', 'HOWTO');
__END__