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 | $lang = "zh_CN" if $lang eq "zh-hans";
|
---|
49 | $lang = "zh_TW" if $lang eq "zh-hant";
|
---|
50 | $datan{$lang} = $val;
|
---|
51 | }
|
---|
52 | while($main =~ /<a href="(\/wiki\/([a-zA-Z-_]):.*?)".*? • /g)
|
---|
53 | {
|
---|
54 | my $lang = lc($1);
|
---|
55 | my $val = uri_unescape($2);
|
---|
56 | $lang = "$1_".uc($2) if($lang =~ /^(..)[_-](..)$/);
|
---|
57 | $datan{$lang} = $val;
|
---|
58 | }
|
---|
59 | foreach my $l (sort keys %datan)
|
---|
60 | {
|
---|
61 | $v .= "\n" . (" " x 18) . "$l.href=\"http://wiki.openstreetmap.org$datan{$l}\"";
|
---|
62 | }
|
---|
63 | print "$v\n";
|
---|
64 | print "Replace failed for $data{href}.\n" if !($file =~ s/(<link )href="\Q$data{href}\E.*?( ?\/>)/$1$v$2/s);
|
---|
65 |
|
---|
66 | $skip++;
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 | open FILE,">","data/defaultpresets.xml" or die;
|
---|
71 | print FILE $file;
|
---|
72 | close FILE;
|
---|