#----------------------------------------------------------------- # Creates pages describing the tagging schemes in use within # OpenStreetmap #----------------------------------------------------------------- # Usage: perl construct.pl # Will create an ./html/ directory and fill it with HTML files # Uses input from http://wiki.openstreetmap.org/index.php/Tagwatch/* #----------------------------------------------------------------- # This file is part of Tagwatch # Tagwatch is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Tagwatch is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Tagwatch. If not, see . #--------------------------------------------------------------- use MediaWiki; use strict; my $Dir = "html"; mkdir $Dir if ! -d $Dir; my $DataDir = "Data"; open(INDEX, ">$Dir/index_en.htm"); open(SAMPLE_REQUESTS, ">sample_requests.txt"); my @Languages = ("en","fr","de"); my $c = MediaWiki->new; $c->setup({'wiki' => { 'host' => 'wiki.openstreetmap.org', 'path' => ''}}); my $wp = MediaWiki->new; $wp->setup({'wiki' => { 'host' => 'en.wikipedia.org', 'path' => 'w'}}); my $Descriptions; foreach my $Lang(@Languages){ $Descriptions->{$Lang} = GetDescriptions($Lang); } print INDEX "

List of all tags found

\n"; AllTags(); print INDEX "

Watchlist tags

\n"; foreach my $Line(split(/\n/, $c->text("Tagwatch/Watchlist"))){ if($Line =~ m{\* (\w+)}){ foreach my $Lang(@Languages){ Watchlist($1, $Lang); } } } close INDEX; sub Watchlist{ my($Tag, $Language) = @_; my $Filename = "${Language}_tag_$Tag.htm"; Index($Filename, "$Language:$Tag"); open(OUT, ">$Dir/$Filename"); print OUT htmlHeader($Tag); print OUT "

Back to index

\n"; print OUT "

$Tag

\n"; print OUT "

Discuss $Tag on the wiki

\n"; my @InterLanguageLinks; foreach my $OtherLanguage(@Languages){ if($OtherLanguage eq $Language){ push(@InterLanguageLinks, "$OtherLanguage"); } else{ my $URL = "${OtherLanguage}_tag_$Tag.htm"; push(@InterLanguageLinks, "$OtherLanguage"); } } print OUT "

In other languages: ", join(", ", @InterLanguageLinks), "

"; my $Values = GetValues($Tag); my $Max = Max($Values); my @Others; print OUT table(); foreach my $Value(sort {$Values->{$b} <=> $Values->{$a}} keys %{$Values}){ if($Values->{$Value} > ($Max / 120)){ my $Image = sprintf("Photos/%s_%s.jpg", $Tag, $Value); my $ImageHtml = '-'; $ImageHtml = "" if(-f "$Dir/$Image"); my $Text = $Descriptions->{$Language}->{$Tag}->{$Value}; $Text = ' ' if(!$Text); my @Links = ( "Wikipedia", "Translate" # TODO ); my $Sample = GetSampleImage($Tag,$Value); my $SampleHtml = ""; printf OUT "%s = %s%s%s%s%s\n", $Tag, $Value, $ImageHtml, $SampleHtml, $Text, "".join(",
", @Links)."
"; } else{ my $Log = 1 + (log($Values->{$Value} / $Max) / log(10)); my $Darkness = $Log * -60; $Darkness = 200 if($Darkness > 200); my $Grey = "#" . sprintf("%02x", $Darkness) x 3; my $Size = 120 + $Log * 10; $Size = 80 if($Size < 80); my $Stuff = sprintf("%s", $Grey, $Size, $Value); push(@Others, $Stuff); } } print OUT "\n"; printf OUT "

Other values used

\n

%s

\n", join(",\n", @Others); print OUT htmlFooter(); close OUT; } sub GetSampleImage{ my ($Tag, $Value) = @_; # Add this tag to a list of requests, so makeSamples.pl will # generate the image later printf SAMPLE_REQUESTS "%s = %s\n", $Tag, $Value; # Return the location where makeSamples.pl will put the PNG image return sprintf("Samples/%s_%s.png", $Tag, $Value); } sub AllTags{ my $Filename = "tags.htm"; Index($Filename, "All tags"); my $Tags = GetTags(); open(OUT, ">$Dir/$Filename"); print OUT table(); print OUT "KeyExamples\n"; foreach my $Tag(sort {$Tags->{$b} <=> $Tags->{$a}} keys %{$Tags}){ my $Values = GetValues($Tag); my $Count = 0; my @Examples; foreach my $Value(sort {$Values->{$b} <=> $Values->{$a}} keys %{$Values}){ my $Text = $Value; my $Text = sprintf("$Value (%d)", $Values->{$Value}) if($Values->{$Value} > 100); push(@Examples, $Text) if($Count++ < 10 && $Text); } printf OUT "%s%s\n", $Tag, join(", ", @Examples); } print OUT "\n"; close OUT; } sub GetTags{ open(IN, "<$DataDir/tags.txt") || return; my %Values; while(my $Line = ){ if($Line =~ m{(\d+) (.*)}){ $Values{$2} = $1 if($1 && $2); } } close IN; return \%Values; } sub GetDescriptions{ my ($Language) = @_; my %D; foreach my $Line(split(/\n/, $c->text("Tagwatch/Descriptions/$Language"))){ if($Line =~ m{\*\s*(\w+)\s*=\s*(\w+)\s*:\s*(.*?)\s*$}){ $D{$1}->{$2} = $3; } } return \%D; } sub Index{ my ($Page, $Title) = @_; print INDEX "

$Title

\n"; } sub Max{ my ($Values) = @_; my $Max = 0; while(my($k,$v) = each(%{$Values})){ $Max = $v if($v > $Max); } return $Max; } sub GetValues{ my ($Tag) = @_; open(IN, "<$DataDir/tag_$Tag.txt") || return; my %Values; while(my $Line = ){ if($Line =~ m{(\d+) (.*)}){ $Values{$2} = $1 if($1 && $2); } } close IN; return \%Values; } sub table{ "\n" } sub htmlHeader{ my $Title = shift(); return("$Title"); } sub htmlFooter{ my $URL = "http://wiki.openstreetmap.org/index.php/Tagwatch"; my $Footer = "

Generated by TagWatch

"; return("

 

\n
\n$Footer\n\n"); } sub translate{ my($Word, $Language) = @_; my $Data = $wp->text($Word); print "Translating $Word into $Language\n"; if($Data =~ m{ \[\[ fr : (.+?) \]\] }x ){ return($1, 1); } return($Word, 0); }