]> git.proxmox.com Git - pve-docs.git/blobdiff - pve-docs-mediawiki-import.in
fixup: s/devies/devices/
[pve-docs.git] / pve-docs-mediawiki-import.in
index 4a2736c366c35f7de83ffd9267f0e235e8fe73b7..14dffa09d9e7556f1c13c72145f303f7497b308f 100755 (executable)
@@ -7,6 +7,7 @@ use Data::Dumper;
 use IO::File;
 use File::Basename;
 use MediaWiki::API;
+use HTML::Parser;
 
 use JSON;
 
@@ -17,10 +18,10 @@ my $fileinfo = decode_json($data_str);
 
 my $config_fn = "/root/.pve-docs"; # format 'username:pw'
 
-my $fh = IO::File->new("$config_fn") || 
+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 $api_url = "https://pve.proxmox.com/mediawiki/api.php";
 
 my $config = <$fh>;
 chomp $config;
@@ -35,27 +36,62 @@ $mw->login({ lgname => $username, lgpassword => $passwd })
     || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
 
 sub update_page {
-    my ($pagename, $include, $category) = @_;
+    my ($pagename, $filename, $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;
-    }
+    my $pve_content = "<!-- Do not edit - this is autogenerated content -->\n";
+
+    $pve_content .= "{{#pvedocs:$filename}}\n";
+    $pve_content .= "[[Category:$category]]\n" if $category;
 
-    if ($category) {
-       my $catstr = "Category:$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));
 
-       if ($page !~ m/^\[\[$catstr\]\]\s*$/m) {
-           $page .= "\n[[$catstr]]\n";
-       }
+    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',
@@ -64,168 +100,23 @@ sub update_page {
        text => $page,
     };
 
-    $mw->edit($wcmd) || 
+    $mw->edit($wcmd) ||
        die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
 }
 
 my $cat_refdoc = "Reference Documentation";
 
-my $docs_old = {
-    'ha-manager-plain.html' => {
-       title => "High Availability",
-       category => $cat_refdoc,
-    },
-    'pve-installation-plain.html' => {
-       title => "Installation",
-       category => $cat_refdoc,
-    },
-    'pve-usbstick-plain.html' => {
-       title => "Install from USB Stick",
-       category => $cat_refdoc,
-    },
-    'pve-system-requirements-plain.html' => {
-       title => "System Requirements",
-       category => $cat_refdoc,
-    },
-    'getting-help-plain.html' => {
-       title => "Getting Help",
-       category => $cat_refdoc,
-    },
-    'pve-network-plain.html' => {
-       title => "Network Configuration",
-       category => $cat_refdoc,
-    },
-    'pve-disk-health-monitoring-plain.html' => {
-       title => "Disk Health Monitoring",
-       category => $cat_refdoc,
-    },
-    'local-lvm-plain.html' => {
-       title => "Logical Volume Manager (LVM)",
-       category => $cat_refdoc,
-    },
-    'pve-package-repos-plain.html' => {
-       title => "Package Repositories",
-       category => $cat_refdoc,
-    },
-    'system-software-updates-plain.html' => {
-       title => "System Software Updates",
-       category => $cat_refdoc,
-    },
-    'sysadmin-plain.html' => {
-       title => "Host System Administration",
-       category => $cat_refdoc,
-    },
-    'pct-plain.html' => {
-       title => "Linux Container",
-       category => $cat_refdoc,
-    },
-    'pmxcfs-plain.html' => {
-       title => "Proxmox Cluster File System (pmxcfs)",
-       category => $cat_refdoc,
-    },
-    'pve-bibliography-plain.html' => {
-       title => "Bibliography",
-       category => $cat_refdoc,
-    },
-    'pvecm-plain.html' => {
-       title => "Cluster Manager",
-       category => $cat_refdoc,
-    },
-    'pve-faq-plain.html' => {
-       title => "FAQ",
-       category => $cat_refdoc,
-    },
-    'pve-firewall-plain.html' => {
-       title => "Firewall",
-       category => $cat_refdoc,
-    },
-    'pvesm-plain.html' => {
-       title => "Storage",
-       category => $cat_refdoc,
-    },
-    'pveum-plain.html' => {
-       title => "User Management",
-       category => $cat_refdoc,
-    },
-    'qm-plain.html' => {
-       title => "Qemu/KVM Virtual Machines",
-       category => $cat_refdoc,
-    },
-    'vzdump-plain.html' => {
-       title => "Backup and Restore",
-       category => $cat_refdoc,
-    },
-    'qm.conf.5-plain.html' => {
-       title => "Manual: vm.conf",
-       category => $cat_refdoc,
-    },
-    'pct.conf.5-plain.html' => {
-       title => "Manual: pct.conf",
-       category => $cat_refdoc,
-    },
-    'datacenter.cfg.5-plain.html' => {
-       title => "Manual: datacenter.cfg",
-       category => $cat_refdoc,
-    },
-    # Storage Plugins
-    'pve-storage-dir-plain.html' => {
-       title => "Storage: Directory",
-       category => $cat_refdoc,
-    },
-    'pve-storage-glusterfs-plain.html' => {
-       title => "Storage: GlusterFS",
-       category => $cat_refdoc,
-    },
-    'pve-storage-iscsidirect-plain.html' => {
-       title => "Storage: User Mode iSCSI",
-       category => $cat_refdoc,
-    },
-    'pve-storage-iscsi-plain.html' => {
-       title => "Storage: iSCSI",
-       category => $cat_refdoc,
-    },
-    'pve-storage-lvm-plain.html' => {
-       title => "Storage: LVM",
-       category => $cat_refdoc,
-    },
-    'pve-storage-lvmthin-plain.html' => {
-       title => "Storage: LVM Thin",
-       category => $cat_refdoc,
-    },
-    'pve-storage-nfs-plain.html' => {
-       title => "Storage: NFS",
-       category => $cat_refdoc,
-    },
-    'pve-storage-rbd-plain.html' => {
-       title => "Storage: RBD",
-       category => $cat_refdoc,
-    },
-    'pve-storage-zfspool-plain.html' => {
-       title => "Storage: ZFS",
-       category => $cat_refdoc,
-    },
-};
-
-my $wiki_man5_pages = {
-    'datacenter.cfg.adoc' => 1,
-    'qm.conf.adoc' => 1,
-    'pct.conf.adoc' => 1,
-};
 
 my $docs = {};
 foreach my $source (sort keys %{$fileinfo->{toplevel}->{wiki}}) {
     my $title = $fileinfo->{titles}->{wiki}->{$source};
-    my $doctype = $fileinfo->{doctype}->{wiki}->{$source};
-
-    my $suffix = $wiki_man5_pages->{$source} ? '.5-plain.html' : '-plain.html';
-    my $filename = $source;
-    $filename =~ s/\.adoc//;
-    $filename .= $suffix;
+    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, category => $cat_refdoc);
+
+    update_page($title, $filename, $cat_refdoc);
 }
 
 # also update 'Get support' page, because this is used since a long