-
Notifications
You must be signed in to change notification settings - Fork 16
/
pve-docs-mediawiki-import.in
executable file
·126 lines (93 loc) · 3.2 KB
/
pve-docs-mediawiki-import.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use IO::File;
use File::Basename;
use MediaWiki::API;
use HTML::Parser;
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 = "https://pve.proxmox.com/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, $filename, $category) = @_;
print "update mediawiki page: $pagename\n";
my $ref = $mw->get_page( { title => $pagename } );
my $page = $ref->{'*'} || '';
my $pve_content = "<!-- Do not edit - this is autogenerated content -->\n";
$pve_content .= "{{#pvedocs:$filename}}\n";
$pve_content .= "[[Category:$category]]\n" if $category;
my $starttag = '<!--PVE_IMPORT_START_MARKER-->';
my $endtag = '<!--PVE_IMPORT_END_MARKER-->';
$pve_content .= "<pvehide>\n";
my $parser_opts = {
api_version => 3,
text_h => [ sub { $pve_content .= shift }, "text" ],
};
my $parser = HTML::Parser->new(%$parser_opts);
$parser->ignore_elements(qw(script style));
my $fh = IO::File->new("/usr/share/pve-docs/$filename", "r") or
die "unable to open file '$filename' - $!\n";
while (defined(my $line = <$fh>)) {
$parser->parse($line);
}
$pve_content .= "</pvehide>\n";
$pve_content =~ s/\s+$//gm;
chomp $pve_content;
if ($page =~ m/^(.*)$starttag\n.*\n$endtag\n?(.*)$/s) {
my ($top_content, $bottom_content) = ($1, $2);
$page = $top_content;
$page .= "$starttag\n";
$page .= $pve_content;
$page .= "\n$endtag\n";
$page .= $bottom_content;
} elsif ($page =~ m/(.*)\{\{#pvedocs:.*?\}\}(.*)$/) {
# old style
my ($top_content, $bottom_content) = ($1, $2);
chomp $top_content;
chomp $bottom_content;
$page = $top_content;
$page .= "$starttag\n";
$page .= $pve_content;
$page .= "\n$endtag\n";
$page .= $bottom_content;
} else {
$page = "$starttag\n$pve_content\n$endtag\n$page";
}
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__