You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wiki-map/wiki-tile.pl

312 lines
8.8 KiB

use v5.36;
1 year ago
use HexGrid;
use HexGrid::Pin;
use MWTemplate;
use MediaWiki::API;
use Getopt::Long;
1 year ago
use Carp;
use Data::Dumper;
# $Data::Dumper::Indent = 1;
1 year ago
use feature "signatures";
no warnings "experimental::signatures";
my $api_url = "https://wiki.tensor.green/w/api.php";
my $region_template_name = "MapRegion";
1 year ago
my $subregion_template_name = "MapSubregion";
my $location_template_name = "MapLocation";
1 year ago
my $site_template_name = "MapSite";
my $border_width = 1;
my $border_colour = 'black';
my $show_coords = 0;
1 year ago
my $html_document = 1;
my $outfile = '-';
my $regiondir;
GetOptions
(
'api-url=s' => \$api_url,
'region-template-name=s' => \$region_template_name,
'subregion-template-name=s' => \$subregion_template_name,
'location-template-name=s' => \$location_template_name,
'site-template-name=s' => \$site_template_name,
'border-width|bw=f' => \$border_width,
'border-colour|border-color|bc=s' => \$border_colour,
'show-coords|coords!' => \$show_coords,
'html-document!' => \$html_document,
'outfile=s' => \$outfile,
'regiondir=s' => \$regiondir
);
1 year ago
my $grid = HexGrid->new(defaults => {
style => { 'stroke-width' => $border_width, stroke => $border_colour },
show_coords => $show_coords });
1 year ago
my %region_grids;
1 year ago
my $mw = MediaWiki::API->new();
$mw->{config}->{api_url} = $api_url;
say STDERR "Getting Region pages";
my $region_query_results = $mw->api
( {
1 year ago
action => 'query',
generator => 'categorymembers',
gcmtitle => 'Category:Regions',
gcmlimit => 'max',
prop => 'info|revisions',
rvprop => 'content',
inprop => 'url',
} ) || croak $mw->{error}->{code} . ': ' . $mw->{error}->{details};
my (@tile_pages, %background_pages);
foreach my $page (values %{$region_query_results->{query}{pages}})
{
next if $page->{title} =~ /^Category:/;
my $region = $grid->make_region($page->{title});
my $parsed_template = MWTemplate::Parse($page->{revisions}[0]{'*'}, $region_template_name);
$region->{defaults}{colour} = $parsed_template->{named_params}{colour};
if($regiondir)
{
$region_grids{$page->{title}} = HexGrid->new
(
defaults =>
{
style => { 'stroke-width' => $border_width, stroke => $border_colour },
show_coords => $show_coords
},
height => 300,
width => 300
);
$region_grids{$page->{title}}->add_region($region);
}
push @tile_pages, "$page->{title}/Tiles";
push @{$background_pages{"File:$parsed_template->{named_params}{background}"}}, $region;
}
1 year ago
say STDERR "Getting Subregion pages";
my $subregion_query_results = $mw->api
( {
action => 'query',
generator => 'categorymembers',
gcmtitle => 'Category:Subregions',
gcmlimit => 'max',
prop => 'info|revisions',
rvprop => 'content',
inprop => 'url',
} ) || croak $mw->{error}->{code} . ': ' . $mw->{error}->{details};
foreach my $page (values %{$subregion_query_results->{query}{pages}})
{
next if $page->{title} =~ /^Category:/;
1 year ago
my $parsed_template = MWTemplate::Parse($page->{revisions}[0]{'*'}, $subregion_template_name);
my $subregion = $grid->make_region($page->{title});
$subregion->{defaults}{colour} = $parsed_template->{named_params}{colour};
if($regiondir)
{
my $region_name = $parsed_template->{positional_params}[0];
$region_grids{$region_name}->add_region($subregion);
}
1 year ago
push @tile_pages, "$page->{title}/Tiles";
push @{$background_pages{"File:$parsed_template->{named_params}{background}"}}, $subregion;
}
say STDERR "Getting Background image pages";
my $background_query_results = $mw->api({ action => 'query',
prop => 'imageinfo',
titles => join('|', keys %background_pages),
iiprop => 'url'
}) || carp $mw->{error}->{code} . ': ' . $mw->{error}->{details};
1 year ago
foreach my $page (values %{$background_query_results->{query}{pages}})
{
if($page->{imageinfo})
{
foreach my $region (@{$background_pages{$page->{title}}})
{
$region->{defaults}{image} = $page->{imageinfo}[0]{url};
}
}
}
say STDERR "Getting Tile pages";
my $tile_query_results = $mw->api
( {
action => 'query',
titles => join('|', @tile_pages),
prop => 'revisions',
rvprop => 'content',
} ) || croak $mw->{error}->{code} . ': ' . $mw->{error}->{details};
1 year ago
foreach my $page (values %{$tile_query_results->{query}{pages}})
1 year ago
{
my $content = $page->{revisions}[0]{'*'};
my ($region_name) = $page->{title} =~ /(.*)\/Tiles/;
my $region = $grid->{regions}{$region_name};
foreach my $coords (split /;/, $content)
{
# The below regex is a whitespace forgiving version of /^(-?\d+),(-?\d+)/, an int pair
do { carp "Skipping bad spec: $coords"; next; }
unless $coords =~ /^\s*(-?\s*\d+)\s*,\s*(-?\s*\d+)\s*$/;
$region->make_tile_at($1,$2);
}
1 year ago
}
1 year ago
say STDERR "Getting Location pages";
my $location_query_results = $mw->api
( {
action => 'query',
generator => 'categorymembers',
gcmtitle => 'Category:Locations',
gcmlimit => 'max',
prop => 'info|revisions',
rvprop => 'content',
inprop => 'url',
} ) || croak $mw->{error}->{code} . ': ' . $mw->{error}->{details};
foreach my $page (values %{$location_query_results->{query}{pages}})
{
next if $page->{title} =~ /^Category:/;
1 year ago
my $parsed_template = MWTemplate::Parse($page->{revisions}[0]{'*'}, $location_template_name);
my $location = $grid->make_region($page->{title});
my $location_with_context;
if($regiondir)
{
$region_grids{$location->{name}} = HexGrid->new
(
defaults =>
{
style => { 'stroke-width' => $border_width, stroke => $border_colour },
show_coords => $show_coords
},
height => 300,
width => 300
);
$location_with_context = $region_grids{$location->{name}}->make_region($location->{name});
$location_with_context->{defaults}{colour} = $location->{defaults}{colour} = $parsed_template->{named_params}{colour};
}
1 year ago
# The below regex is a whitespace forgiving version of /^(-?\d+),(-?\d+)/, an int pair
if($parsed_template->{positional_params}[0] =~ /^\s*(-?\s*\d+)\s*,\s*(-?\s*\d+)\s*$/)
{
$location->make_tile_at($1,$2);
if($regiondir)
1 year ago
{
$location_with_context->make_tile_at($1,$2);
foreach my $coords (split /;/, $parsed_template->{named_params}{context_tiles})
{
# The below regex is a whitespace forgiving version of /^(-?\d+),(-?\d+)/, an int pair
do { carp "Skipping bad spec: $coords"; next; }
unless $coords =~ /^\s*(-?\s*\d+)\s*,\s*(-?\s*\d+)\s*$/;
$location_with_context->add_tile($grid->get_tile_at($1, $2));
}
1 year ago
}
}
if($regiondir)
{
my $region_name = $parsed_template->{positional_params}[1];
$region_grids{$region_name}->add_region($location);
}
1 year ago
push @{$background_pages{"File:$parsed_template->{named_params}{background}"}}, $location;
}
say STDERR "Getting Site pages";
my $site_query_results = $mw->api
1 year ago
( {
action => 'query',
generator => 'categorymembers',
prop => 'info|revisions',
gcmtitle => 'Category:Sites',
gcmlimit => 'max',
rvprop => 'content',
inprop => 'url',
1 year ago
} ) || croak $mw->{error}->{code} . ': ' . $mw->{error}->{details};
# say STDERR "Sites found: " . join(" ,", map { $_->{title} } @$site_pages);
foreach my $site_page_ref (values %{$site_query_results->{query}{pages}})
{
next if $site_page_ref->{title} =~ /^Category:/;
1 year ago
my $site_name = $site_page_ref->{title};
say STDERR "Processing Site $site_name";
my $site_url = $site_page_ref->{canonicalurl};
my $site_content = $site_page_ref->{revisions}[0]{'*'};
my $parsed_template = MWTemplate::Parse($site_content, $site_template_name);
my ($nw,$sw) = split /,/, $parsed_template->{named_params}{coords};
my $imageinfo_query_results = $mw->api({ action => 'query',
prop => 'imageinfo',
titles => "File:$parsed_template->{named_params}{icon}",
iiprop => 'url'
}) || carp $mw->{error}->{code} . ': ' . $mw->{error}->{details};
my %image_pages = %{$imageinfo_query_results->{query}{pages}};
my $image_url = (values %image_pages)[0]{imageinfo}[0]{url};
my $pin = HexGrid::Pin->new
(
name => $site_name,
id => "${site_name}_pin",
icon => $image_url,
link => $site_url,
description => $parsed_template->{named_params}{abstract}
);
$grid->get_tile_at($nw, $sw)->pin($pin);
}
open (my $fh, "> $outfile") or croak "Couldn't open $outfile for writing: $!";
say $fh ($html_document ? wrap_in_html($grid) : $grid->render);
close $fh;
if($regiondir)
{
chdir $regiondir || croak "Couldn't chdir to $regiondir: $!";
my $extension = $html_document ? 'html' : 'svg';
while(my ($region, $region_grid) = each %region_grids)
{
open (my $region_fh, "> $region.$extension")
or croak "Couldn't open $region.extension for writing: $!";
say $region_fh ($html_document ? wrap_in_html($region_grid) : $region_grid->render);
close $region_fh;
}
}
1 year ago
sub wrap_in_html($grid)
{
my $html_builder = "<!DOCTYPE html>";
$html_builder .= "\n<html>\n<body>";
$html_builder .= "\n" . <<EOS;
<script>
function clickPin(pinId, containerId) {
let popup = document.getElementById(pinId + '-popup');
popup.style.visibility = popup.style.visibility == 'visible' ? 'hidden' : 'visible';
}
</script>
EOS
$html_builder .= "\n" . $grid->render;
$html_builder .= "\n</body>\n</html>";
return $html_builder;
}