]> git.proxmox.com Git - pve-docs.git/blame - pve-docs-mediawiki-import.in
backup: clarify that CLI means FS-level and highlight retention-note
[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;
3f4666b4 10use HTML::Parser;
cfabc2e9 11
b489b02c
DM
12use JSON;
13
14my $data_str = "";
15while (<main::DATA>) { $data_str .= $_; }
16
17my $fileinfo = decode_json($data_str);
18
cfabc2e9
DM
19my $config_fn = "/root/.pve-docs"; # format 'username:pw'
20
c5bf6350 21my $fh = IO::File->new("$config_fn") ||
cfabc2e9
DM
22 die "Please configure the mediawiki user/passswd in '$config_fn'\n";
23
3d5bb7ac 24my $api_url = "https://pve.proxmox.com/mediawiki/api.php";
cfabc2e9
DM
25
26my $config = <$fh>;
27chomp $config;
28
29my ($username, $passwd) = split(':', $config, 2);
30
31my $mw = MediaWiki::API->new();
32$mw->{config}->{api_url} = $api_url;
33
34# log in to the wiki
35$mw->login({ lgname => $username, lgpassword => $passwd })
36 || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
37
38sub update_page {
3f4666b4 39 my ($pagename, $filename, $category) = @_;
cfabc2e9
DM
40
41 print "update mediawiki page: $pagename\n";
42
43 my $ref = $mw->get_page( { title => $pagename } );
44 my $page = $ref->{'*'} || '';
45
3f4666b4
DM
46 my $pve_content = "<!-- Do not edit - this is autogenerated content -->\n";
47
48 $pve_content .= "{{#pvedocs:$filename}}\n";
49 $pve_content .= "[[Category:$category]]\n" if $category;
50
51 my $starttag = '<!--PVE_IMPORT_START_MARKER-->';
52 my $endtag = '<!--PVE_IMPORT_END_MARKER-->';
cfabc2e9 53
3f4666b4 54 $pve_content .= "<pvehide>\n";
cfabc2e9 55
3f4666b4
DM
56 my $parser_opts = {
57 api_version => 3,
4aa7516d 58 text_h => [ sub { $pve_content .= shift }, "text" ],
3f4666b4
DM
59 };
60 my $parser = HTML::Parser->new(%$parser_opts);
4aa7516d 61 $parser->ignore_elements(qw(script style));
3f4666b4
DM
62
63 my $fh = IO::File->new("/usr/share/pve-docs/$filename", "r") or
64 die "unable to open file '$filename' - $!\n";
65 while (defined(my $line = <$fh>)) {
66 $parser->parse($line);
67 }
68 $pve_content .= "</pvehide>\n";
69
70 $pve_content =~ s/\s+$//gm;
71
72 chomp $pve_content;
73
0d02df33 74 if ($page =~ m/^(.*)$starttag\n.*\n$endtag\n?(.*)$/s) {
3f4666b4 75 my ($top_content, $bottom_content) = ($1, $2);
3f4666b4
DM
76 $page = $top_content;
77 $page .= "$starttag\n";
78 $page .= $pve_content;
79 $page .= "\n$endtag\n";
80 $page .= $bottom_content;
81 } elsif ($page =~ m/(.*)\{\{#pvedocs:.*?\}\}(.*)$/) {
82 # old style
83 my ($top_content, $bottom_content) = ($1, $2);
84 chomp $top_content;
85 chomp $bottom_content;
86 $page = $top_content;
87 $page .= "$starttag\n";
88 $page .= $pve_content;
89 $page .= "\n$endtag\n";
90 $page .= $bottom_content;
91 } else {
92 $page = "$starttag\n$pve_content\n$endtag\n$page";
cfabc2e9 93 }
c5bf6350 94
cfabc2e9
DM
95 my $timestamp = $ref->{timestamp};
96 my $wcmd = {
97 action => 'edit',
98 title => $pagename,
99 basetimestamp => $timestamp, # to avoid edit conflicts
100 text => $page,
101 };
102
c5bf6350 103 $mw->edit($wcmd) ||
cfabc2e9
DM
104 die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
105}
106
107my $cat_refdoc = "Reference Documentation";
108
b489b02c
DM
109
110my $docs = {};
111foreach my $source (sort keys %{$fileinfo->{toplevel}->{wiki}}) {
112 my $title = $fileinfo->{titles}->{wiki}->{$source};
40688099
DM
113 my $filename = $fileinfo->{outfile}->{wiki}->{$source} ||
114 die "found no file name mapping for '$source'";
cfabc2e9 115
cfabc2e9
DM
116 my $path = "/usr/share/pve-docs/$filename";
117 die "no such file '$path'" if ! -f $path;
c5bf6350
DM
118
119 update_page($title, $filename, $cat_refdoc);
cfabc2e9 120}
11e7e157
DM
121
122# also update 'Get support' page, because this is used since a long
123# time and is referenced from outside
b489b02c
DM
124update_page("Get support", 'getting-help-plain.html', 'HOWTO');
125
126__END__