From d61fc8c8a3e968c1e91297dee6907de6480275ee Mon Sep 17 00:00:00 2001 From: Daniel Asher Resnick Date: Tue, 27 Jun 2023 19:34:04 -0500 Subject: [PATCH] Revamp to minimize requests --- wiki-tile2.pl | 113 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 41 deletions(-) diff --git a/wiki-tile2.pl b/wiki-tile2.pl index 9f91958..ad7f11a 100644 --- a/wiki-tile2.pl +++ b/wiki-tile2.pl @@ -7,7 +7,7 @@ use MWTemplate; use Carp; use Data::Dumper; -$Data::Dumper::Indent = 1; +# $Data::Dumper::Indent = 1; use feature "signatures"; no warnings "experimental::signatures"; @@ -18,7 +18,7 @@ my $site_template_name = "MapSite"; my $border_width = 1; my $border_colour = 'black'; -my $show_coords = 1; +my $show_coords = 0; my $grid = HexGrid->new(defaults => { style => { 'stroke-width' => $border_width, stroke => $border_colour }, @@ -26,24 +26,81 @@ my $grid = HexGrid->new(defaults => { my $mw = MediaWiki::API->new(); $mw->{config}->{api_url} = $api_url; -my $region_pages = $mw->list ( { + + +say STDERR "Getting Region pages"; +my $region_query_results = $mw->api +( { action => 'query', - list => 'categorymembers', - cmtitle => 'Category:Regions', - cmlimit => 'max' } ) - || croak $mw->{error}->{code} . ': ' . $mw->{error}->{details}; + 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}}) +{ + 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}; + + push @tile_pages, "$page->{title}/Tiles"; + push @{$background_pages{"File:$parsed_template->{named_params}{background}"}}, $region; +} + + + +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}; -say STDERR "Regions found: " . join(", ", map { $_->{title} } @$region_pages); +foreach my $page (values %{$background_query_results->{query}{pages}}) +{ + # say STDERR Dumper($page); + if($page->{imageinfo}) + { + # say STDERR Dumper($page->{title}); + 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}; -foreach my $region_page_ref (@$region_pages) +foreach my $page (values %{$tile_query_results->{query}{pages}}) { - my $region_name = $region_page_ref->{title}; - say STDERR "Processing Region: $region_name"; - my $region_page = $mw->get_page({ title => $region_name }); - my $tile_page = $mw->get_page({ title => "$region_name/Tiles" }); - process_region($grid, $region_name, $region_page->{'*'}, $tile_page->{'*'}); + 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); + } } + + +say STDERR "Getting Site pages"; my $site_query_results = $mw->api ( { action => 'query', @@ -56,10 +113,8 @@ my $site_query_results = $mw->api } ) || croak $mw->{error}->{code} . ': ' . $mw->{error}->{details}; # say STDERR "Sites found: " . join(" ,", map { $_->{title} } @$site_pages); -# say STDERR Dumper($site_query_results); foreach my $site_page_ref (values %{$site_query_results->{query}{pages}}) { - say STDERR Dumper($site_page_ref); my $site_name = $site_page_ref->{title}; say STDERR "Processing Site $site_name"; my $site_url = $site_page_ref->{canonicalurl}; @@ -83,36 +138,12 @@ foreach my $site_page_ref (values %{$site_query_results->{query}{pages}}) link => $site_url, description => $parsed_template->{named_params}{abstract} ); - # say STDERR Dumper($pin); $grid->get_tile_at($nw, $sw)->pin($pin); } -say wrap_in_html($grid); -sub process_region($grid, $region_name, $region_spec, $tiles) -{ - my $region = $grid->make_region($region_name); - my $parsed_template = MWTemplate::Parse($region_spec, $region_template_name); - $region->{defaults}{colour} = $parsed_template->{named_params}{colour}; - my $image_info = $mw->api({ action => 'query', - prop => 'imageinfo', - titles => "File:$parsed_template->{named_params}{background}", - iiprop => 'url' - }) || carp $mw->{error}->{code} . ': ' . $mw->{error}->{details}; - $region->{defaults}{image} = []; - foreach my $page (values %{$image_info->{query}{pages}}) - { - push @{$region->{defaults}{image}}, $page->{imageinfo}[0]{url}; - } - foreach my $coords (split /;/, $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*$/; - $region->make_tile_at($1,$2); - } -} +say wrap_in_html($grid); sub wrap_in_html($grid) {