]> git.openstreetmap.org Git - rails.git/blob - script/misc/update-wiki-pages
Avoid error when there is no layer with a keyid
[rails.git] / script / misc / update-wiki-pages
1 #!/usr/bin/env perl
2 use 5.010;
3 use strict;
4 use warnings;
5
6 use Getopt::Long;
7 use Pod::Usage;
8 use MediaWiki::API;
9 use Test::More qw(no_plan);
10 use YAML::XS qw(Dump);
11
12 =head1 NAME
13
14 update-wiki-pages - Scrape the wiki for key/value wiki description pages
15
16 =head1 SYNOPSIS
17
18     perl script/misc/update-wiki-pages config/wiki_pages.yml 
19
20 Or with prove(1):
21
22     prove -e 'perl script/misc/update-wiki-pages' config/wiki_pages.yml
23
24 =cut
25
26 # Get the command-line options
27 Getopt::Long::Parser->new(
28     config => [ qw< bundling no_ignore_case no_require_order pass_through > ],
29 )->getoptions(
30     'h|help' => \my $help,
31 ) or help();
32
33 # On --help
34 help() if $help;
35
36 my $out_file = $ARGV[0];
37 $out_file //= 'config/wiki_pages.yml';
38
39 help() unless -f $out_file;
40
41 # Get a API interface
42 my $mw = MediaWiki::API->new();
43 ok($mw, "Got a MediaWiki API");
44 $mw->{config}->{api_url} = 'https://wiki.openstreetmap.org/w/api.php';
45
46 # All our goodies
47 my (%feature, %count);
48
49 # This is what you get on:
50 ## http://wiki.openstreetmap.org/w/index.php?search=Template:KeyDescription&fulltext=Search&fulltext=Search
51 for my $lang ('', map { "${_}:" } qw[ Pt Fi De It HU Cz Fr RU Pl ]) {
52     ok(1, "  Templates for language '$lang'");
53
54     # Key pages
55     ok(1, "    Getting key pages");
56     my $cnt = stick_content_in_hash("key", "Template:${lang}KeyDescription", \%feature);
57     $cnt += stick_content_in_hash("key", "Template:${lang}Feature", \%feature);
58     ok(1, "    Got $cnt key pages");
59     $count{key} += $cnt;
60
61     # Value pages
62     ok(1, "    Getting value pages");
63     $cnt = stick_content_in_hash("tag", "Template:${lang}ValueDescription", \%feature);
64     ok(1, "    Got $cnt value pages");
65     $count{value} += $cnt;
66 }
67
68 ok(1, "Got a total of $count{$_} ${_}s") for qw[ key value ];
69
70 # Dump to .yml file
71 open my $out, ">", $out_file or die "Can't open file '$out_file' supplied on the command line";
72 say $out "# THIS FILE IS AUTOGENERATED WITH THE script/misc/update-wiki-pages";
73 say $out "# PROGRAM DO NOT MANUALLY EDIT IT";
74 say $out "";
75 say $out Dump(\%feature);
76 close $out;
77
78 exit 0;
79
80 sub stick_content_in_hash
81 {
82     my ($key, $title, $hash) = @_;
83     my $ukey = ucfirst $key;
84
85     my $space_to_underscore = sub {
86         my $txt = shift;
87         $txt =~ s/ /_/g;
88         $txt;
89     };
90
91     my $count = 0;
92
93     my $process_link = sub {
94         my $link = shift;
95         $count++;
96         ok(1, "    ... got $count links") if $count % 200 == 0;
97         my $title = $link->{title};
98         my $lang;
99         my $key_name;
100         if ($title =~ /^$ukey:(?<key_name>.*?)$/) {
101             # English by default
102             $lang = "en";
103             $key_name = $space_to_underscore->($+{key_name});
104         } elsif ($title =~ /^(?<lang>[^:]+):$ukey:(?<key_name>.*?)$/) {
105             $lang = lc $+{lang};
106             $key_name = $space_to_underscore->($+{key_name});
107         }
108         if ($lang && !exists($hash->{$lang}->{$key}->{$key_name})) {
109             $hash->{$lang}->{$key}->{$key_name} = $title;
110         }
111     };
112
113     get_embeddedin(
114         $title,
115         sub {
116             my $link = shift;
117             $process_link->($link);
118             get_redirects(
119                 $link->{title},
120                 sub {
121                     my $link = shift;
122                     $process_link->($link) if exists($link->{redirect});
123                 }
124             );
125         }
126     );
127
128     return $count;
129 }
130
131 sub process_list
132 {
133     my $callback = shift;
134     my $links = shift;
135     for my $link (@$links) {
136         $callback->($link);
137     }
138 }
139
140 sub get_embeddedin
141 {
142     my ($title, $callback) = @_;
143     my $articles = $mw->list(
144         {
145             action => 'query',
146             list => 'embeddedin',
147             eititle => $title,
148             eifilterredir => 'nonredirects',
149             # Doesn't work for De:* and anything non-en. Odd.
150             # einamespace => '0|8',
151             eilimit => '200',
152         },
153         {
154             max => '0',
155             hook => sub { process_list($callback, @_) },
156             skip_encoding => 1,
157         }
158     ) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
159 }
160
161 sub get_redirects
162 {
163     my ($title, $callback) = @_;
164     my $articles = $mw->list(
165         {
166             action => 'query',
167             list => 'backlinks',
168             bltitle => $title,
169             blfilterredir => 'redirects',
170             # Doesn't work for De:* and anything non-en. Odd.
171             # einamespace => '0|8',
172             bllimit => '200',
173         },
174         {
175             max => '0',
176             hook => sub { process_list($callback, @_) },
177             skip_encoding => 1,
178         }
179     ) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
180 }
181
182 sub help
183 {
184     my %arg = @_;
185
186     Pod::Usage::pod2usage(
187         -verbose => $arg{ verbose },
188         -exitval => $arg{ exitval } || 0,
189     );
190 }