10 eval "require MediaWiki::API; require YAML::XS;" or do {
11 print "You have to install some modules via CPAN to run this:\n";
12 print " sudo cpanp MediaWiki::API YAML::XS\n";
18 use YAML::XS qw(Dump);
19 use Test::More 'no_plan';
23 update-wiki-pages - Scrape the wiki for key/value wiki description pages
27 perl script/misc/update-wiki-pages config/wiki_pages.yml
31 prove -e 'perl script/misc/update-wiki-pages' config/wiki_pages.yml
35 # Get the command-line options
36 Getopt::Long::Parser->new(
37 config => [ qw< bundling no_ignore_case no_require_order pass_through > ],
39 'h|help' => \my $help,
45 my $out_file = $ARGV[0];
46 $out_file //= 'config/wiki_pages.yml';
48 help() unless -f $out_file;
51 my $mw = MediaWiki::API->new();
52 ok($mw, "Got a MediaWiki API");
53 $mw->{config}->{api_url} = 'http://wiki.openstreetmap.org/w/api.php';
56 my (%feature, %count);
58 # This is what you get on:
59 ## http://wiki.openstreetmap.org/w/index.php?search=Template:KeyDescription&fulltext=Search&fulltext=Search
60 for my $lang ('', map { "${_}:" } qw[ Pt Fi De It HU Cz Fr RU Pl ]) {
61 ok(1, " Templates for language '$lang'");
64 ok(1, " Getting key pages");
65 my $cnt = stick_content_in_hash("key", "Template:${lang}KeyDescription", \%feature);
66 ok(1, " Got $cnt key pages");
70 ok(1, " Getting value pages");
71 $cnt = stick_content_in_hash("tag", "Template:${lang}ValueDescription", \%feature);
72 ok(1, " Got $cnt value pages");
73 $count{value} += $cnt;
76 ok(1, "Got a total of $count{$_} ${_}s") for qw[ key value ];
79 open my $out, ">", $out_file or die "Can't open file '$out_file' supplied on the command line";
80 say $out "# THIS FILE IS AUTOGENERATED WITH THE script/misc/update-wiki-pages";
81 say $out "# PROGRAM DO NOT MANUALLY EDIT IT";
83 say $out Dump(\%feature);
88 sub stick_content_in_hash
90 my ($key, $title, $hash) = @_;
91 my $ukey = ucfirst $key;
93 my $space_to_underscore = sub {
101 my $process_link = sub {
104 ok(1, " ... got $count links") if $count % 200 == 0;
105 my $title = $link->{title};
108 if ($title =~ /^$ukey:(?<key_name>.*?)$/) {
111 $key_name = $space_to_underscore->($+{key_name});
112 } elsif ($title =~ /^(?<lang>[^:]+):$ukey:(?<key_name>.*?)$/) {
114 $key_name = $space_to_underscore->($+{key_name});
116 if ($lang && !exists($hash->{$lang}->{$key}->{$key_name})) {
117 $hash->{$lang}->{$key}->{$key_name} = $title;
125 $process_link->($link);
130 $process_link->($link) if exists($link->{redirect});
141 my $callback = shift;
143 for my $link (@$links) {
150 my ($title, $callback) = @_;
151 my $articles = $mw->list(
154 list => 'embeddedin',
156 eifilterredir => 'nonredirects',
157 # Doesn't work for De:* and anything non-en. Odd.
158 # einamespace => '0|8',
163 hook => sub { process_list($callback, @_) },
166 ) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
171 my ($title, $callback) = @_;
172 my $articles = $mw->list(
177 blfilterredir => 'redirects',
178 # Doesn't work for De:* and anything non-en. Odd.
179 # einamespace => '0|8',
184 hook => sub { process_list($callback, @_) },
187 ) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
194 Pod::Usage::pod2usage(
195 -verbose => $arg{ verbose },
196 -exitval => $arg{ exitval } || 0,