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