From 288b9cc54503b52f4e3b7c9e29fe1fa3d43ae4c2 Mon Sep 17 00:00:00 2001 From: Daniel Asher Resnick Date: Mon, 17 Jul 2023 18:12:14 -0500 Subject: [PATCH] Fix location background bug (#1) Splits location processing into pre and post background image fetching. --- wiki-map.pl | 124 +++++++++++++++++++++++++--------------------------- 1 file changed, 60 insertions(+), 64 deletions(-) diff --git a/wiki-map.pl b/wiki-map.pl index c4bb46d..c6f2c03 100644 --- a/wiki-map.pl +++ b/wiki-map.pl @@ -134,6 +134,63 @@ foreach my $page (values %{$subregion_query_results->{query}{pages}}) +my @location_continuations; +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:/; + my $parsed_template = MWTemplate::Parse($page->{revisions}[0]{'*'}, $location_template_name); + next unless $parsed_template; + say STDERR "Processing location: $page->{title}"; + my $location = $grid->make_region($page->{title}); + $location->{defaults}{colour} = $parsed_template->{named_params}{colour}; + push @{$background_pages{"File:$parsed_template->{named_params}{background}"}}, $location; + push @location_continuations, sub + { + return unless $parsed_template->{positional_params}[0] =~ $coords_regex; + my ($nw, $sw) = ($1, $2); + $location->make_tile_at($nw, $sw); + my $region_name = $parsed_template->{positional_params}[1]; + $region_grids{$region_name}->add_region($location); + if($regiondir) + { + my $region_name = $parsed_template->{positional_params}[1]; + $region_grids{$region_name}->add_region($location); + + $region_grids{$location->{name}} = HexGrid->new + ( + defaults => + { + style => { 'stroke-width' => $border_width, stroke => $border_colour }, + show_coords => $show_coords + }, + height => 300, + width => 300 + ); + my $location_with_context = $region_grids{$location->{name}}->make_region($location->{name}); + $location_with_context->add_tile($grid->get_tile_at($nw, $sw)); + foreach my $coords (split /;/, $parsed_template->{named_params}{context_tiles}) + { + do { carp "Skipping bad spec: $coords"; next; } unless $coords =~ $coords_regex; + $location_with_context->add_tile($grid->get_tile_at($1, $2)); + } + } + }; +} + + + say STDERR "Getting Background image pages"; my $background_query_results = $mw->api({ action => 'query', prop => 'imageinfo', @@ -172,76 +229,15 @@ foreach my $page (values %{$tile_query_results->{query}{pages}}) my $region = $grid->{regions}{$region_name}; foreach my $coords (split /;/, $content) { - do { carp "Skipping bad spec: $coords"; next; } - unless $coords =~ $coords_regex; + do { carp "Skipping bad spec: $coords"; next; } unless $coords =~ $coords_regex; $region->make_tile_at($1,$2); } } -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:/; - my $parsed_template = MWTemplate::Parse($page->{revisions}[0]{'*'}, $location_template_name); - next unless $parsed_template; - say STDERR "Processing location: $page->{title}"; - my $location = $grid->make_region($page->{title}); - $location->{defaults}{colour} = $parsed_template->{named_params}{colour}; - - 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}; - } - - if($parsed_template->{positional_params}[0] =~ $coords_regex) - { - $location->make_tile_at($1,$2); - if($regiondir) - { - $location_with_context->make_tile_at($1,$2); - foreach my $coords (split /;/, $parsed_template->{named_params}{context_tiles}) - { - do { carp "Skipping bad spec: $coords"; next; } - unless $coords =~ $coords_regex; - $location_with_context->add_tile($grid->get_tile_at($1, $2)); - } - } - } - - if($regiondir) - { - my $region_name = $parsed_template->{positional_params}[1]; - $region_grids{$region_name}->add_region($location); - } - - push @{$background_pages{"File:$parsed_template->{named_params}{background}"}}, $location; -} +say STDERR "Continuing Location processing"; +$_->() for @location_continuations;