9 eval "require MediaWiki::API; require YAML::XS;" or do {
10 print "You have to install some modules via CPAN to run this:\n";
11 print " sudo cpanp MediaWiki::API YAML::XS\n";
17 use YAML::XS qw(Dump);
18 use Test::More 'no_plan';
22 update-wiki-pages - Scrape the wiki for key/value wiki description pages
26 perl script/misc/update-wiki-pages config/wiki_pages.yml
30 prove -e 'perl script/misc/update-wiki-pages' config/wiki_pages.yml
34 # Get the command-line options
35 Getopt::Long::Parser->new(
36 config => [ qw< bundling no_ignore_case no_require_order pass_through > ],
38 'h|help' => \my $help,
44 help() unless $ARGV[0];
47 my $mw = MediaWiki::API->new();
48 ok($mw, "Got a MediaWiki API");
49 $mw->{config}->{api_url} = 'http://wiki.openstreetmap.org/w/api.php';
52 my (%feature, %count);
54 # This is what you get on:
55 ## http://wiki.openstreetmap.org/w/index.php?search=Template:KeyDescription&fulltext=Search&fulltext=Search
56 for my $lang ('', map { "${_}:" } qw[ Pt Fi De It HU Cz Fr RU Pl ]) {
57 ok(1, " Templates for language '$lang'");
60 ok(1, " Getting key pages");
61 my $cnt = stick_content_in_hash("key", "Template:${lang}KeyDescription", \%feature);
62 ok(1, " Got $cnt key pages");
66 ok(1, " Getting value pages");
67 $cnt = stick_content_in_hash("tag", "Template:${lang}ValueDescription", \%feature);
68 ok(1, " Got $cnt value pages");
69 $count{value} += $cnt;
72 ok(1, "Got a total of $count{$_} ${_}s") for qw[ key value ];
75 open my $out, ">", $ARGV[0] or die "Can't open file '$ARGV[0]' supplied on the command line";
76 say $out "# THIS FILE IS AUTOGENERATED WITH THE script/misc/update-wiki-pages";
77 say $out "# PROGRAM DO NOT MANUALLY EDIT IT";
79 say $out Dump(\%feature);
84 sub stick_content_in_hash
86 my ($key, $title, $hash) = @_;
87 my $ukey = ucfirst $key;
89 my $space_to_underscore = sub {
100 my (@links) = @$links;
101 ok(1, " ... got " . scalar(@links) . " more links");
102 for my $link (@links) {
104 my $title = $link->{title};
106 if ($title =~ /^$ukey:(?<key_name>.*?)$/) {
108 $hash->{en}->{$key}->{ $space_to_underscore->($+{key_name}) } = $title;
109 } elsif ($title =~ /^(?<lang>[^:]+):$ukey:(?<key_name>.*?)$/) {
110 $hash->{lc $+{lang}}->{$key}->{ $space_to_underscore->($+{key_name}) } = $title;
121 my ($title, $callback) = @_;
122 my $articles = $mw->list(
125 list => 'embeddedin',
127 eifilterredir => 'nonredirects',
128 # Doesn't work for De:* and anything non-en. Odd.
129 # einamespace => '0|8',
137 ) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
144 Pod::Usage::pod2usage(
145 -verbose => $arg{ verbose },
146 -exitval => $arg{ exitval } || 0,