1 | #! /usr/bin/perl -w
|
---|
2 | # short tool to update language links in presets file
|
---|
3 |
|
---|
4 | use XML::Parser;
|
---|
5 | use Data::Dumper;
|
---|
6 | use URI::Escape;
|
---|
7 |
|
---|
8 | my $skip = 0;
|
---|
9 | my $xml = new XML::Parser(Handlers => {Start => \&handle_start});
|
---|
10 | undef $/;
|
---|
11 | open FILE,"<","data/defaultpresets.xml" or die;
|
---|
12 | my $file = <FILE>;
|
---|
13 | close FILE;
|
---|
14 | $xml->parsefile('data/defaultpresets.xml');
|
---|
15 |
|
---|
16 | sub handle_start
|
---|
17 | {
|
---|
18 | my ($expat, $element, %data) = @_;
|
---|
19 |
|
---|
20 | if($element eq "link" && $skip < 50000)
|
---|
21 | {
|
---|
22 | my %datan;
|
---|
23 | foreach my $k (sort {$az=$a;$bz=$b;$az=~s/.?href//;;$bz=~s/.?href//;$az cmp $bz} keys %data)
|
---|
24 | {
|
---|
25 | my $z = $k;
|
---|
26 | $z =~ s/.?href//;
|
---|
27 | #printf("%-5s %s\n", $z,$data{$k});
|
---|
28 | }
|
---|
29 |
|
---|
30 | if(!$data{href})
|
---|
31 | {
|
---|
32 | print "Missing href: %s\n", join(" ", %data);
|
---|
33 | }
|
---|
34 | else
|
---|
35 | {
|
---|
36 | my $main = `wget '$data{href}' -O - -q`;
|
---|
37 | my $h = uri_unescape($data{href});
|
---|
38 | my $v = "href=\"$h\"";
|
---|
39 | if($h eq "http://wiki.openstreetmap.org/wiki/Proposed_features/House_numbers/Karlsruhe_Schema")
|
---|
40 | {
|
---|
41 | $datan{"de"} = "/wiki/Proposed_features/De:Hausnummern";
|
---|
42 | }
|
---|
43 | while($main =~ /<span lang="([a-z_A-Z-]+)"(?: style="unicode-bidi:embed")?><a href="(\/wiki\/.*?)"/g)
|
---|
44 | {
|
---|
45 | my $lang = lc($1);
|
---|
46 | my $val = uri_unescape($2);
|
---|
47 | $lang = "$1_".uc($2) if($lang =~ /^(..)[_-](..)$/);
|
---|
48 | $datan{$lang} = $val;
|
---|
49 | }
|
---|
50 | while($main =~ /<a href="(\/wiki\/([a-zA-Z-_]):.*?)".*? • /g)
|
---|
51 | {
|
---|
52 | my $lang = lc($1);
|
---|
53 | my $val = uri_unescape($2);
|
---|
54 | $lang = "$1_".uc($2) if($lang =~ /^(..)[_-](..)$/);
|
---|
55 | $datan{$lang} = $val;
|
---|
56 | }
|
---|
57 | foreach my $l (sort keys %datan)
|
---|
58 | {
|
---|
59 | $v .= "\n" . (" " x 18) . "$l.href=\"http://wiki.openstreetmap.org$datan{$l}\"";
|
---|
60 | }
|
---|
61 | print "$v\n";
|
---|
62 | print "Replace failed for $data{href}.\n" if !($file =~ s/(<link )href="\Q$data{href}\E.*?( ?\/>)/$1$v$2/s);
|
---|
63 |
|
---|
64 | $skip++;
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | open FILE,">","data/defaultpresets.xml" or die;
|
---|
69 | print FILE $file;
|
---|
70 | close FILE;
|
---|