]> git.openstreetmap.org Git - rails.git/blob - script/misc/update-wiki-pages
f5f0ccb5ebda5843955b473bcde472a397129444
[rails.git] / script / misc / update-wiki-pages
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4
5 use Pod::Usage ();
6 use Getopt::Long ();
7
8 BEGIN {
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";
12         exit 1;
13     };
14 }
15
16 use MediaWiki::API;
17 use YAML::XS qw(Dump);
18 use Test::More 'no_plan';
19
20 =head1 NAME
21
22 update-wiki-pages - Scrape the wiki for key/value wiki description pages
23
24 =head1 SYNOPSIS
25
26     perl script/misc/update-wiki-pages config/wiki_pages.yml 
27
28 =cut
29
30 # Get the command-line options
31 Getopt::Long::Parser->new(
32     config => [ qw< bundling no_ignore_case no_require_order pass_through > ],
33 )->getoptions(
34     'h|help' => \my $help,
35 ) or help();
36
37 # On --help
38 help() if $help;
39
40 help() unless $ARGV[0];
41
42 # Get a API interface
43 my $mw = MediaWiki::API->new();
44 ok($mw, "Got a MediaWiki API");
45 $mw->{config}->{api_url} = 'http://wiki.openstreetmap.org/w/api.php';
46
47 # All our goodies
48 my (%feature, %count);
49
50 # This is what you get on:
51 ## http://wiki.openstreetmap.org/w/index.php?search=Template:KeyDescription&fulltext=Search&fulltext=Search
52 for my $lang ('', map { "${_}:" } qw[ Pt Fi De It HU Cz Fr RU Pl ]) {
53     ok(1, "  Templates for language '$lang'");
54
55     # Key pages
56     ok(1, "    Getting key pages");
57     my $cnt = stick_content_in_hash("key", "Template:${lang}KeyDescription", \%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, ">", $ARGV[0] or die "Can't open file '$ARGV[0]' 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     get_embeddedin(
93         $title,
94         sub {
95             my ($links) = @_;
96             my (@links) = @$links;
97             ok(1, "    ... got " . scalar(@links) . " more links");
98             for my $link (@links) {
99                 $count++;
100                 my $title = $link->{title};
101
102                 if ($title =~ /^$ukey:(?<key_name>.*?)$/) {
103                     # English by default
104                     $hash->{en}->{$key}->{ $space_to_underscore->($+{key_name}) } = $title;
105                 } elsif ($title =~ /^(?<lang>[^:]+):$ukey:(?<key_name>.*?)$/) {
106                     $hash->{lc $+{lang}}->{$key}->{ $space_to_underscore->($+{key_name}) } = $title;
107                 }
108             }
109         }
110     );
111
112     return $count;
113 }
114
115 sub get_embeddedin
116 {
117     my ($title, $callback) = @_;
118     my $articles = $mw->list(
119         {
120             action => 'query',
121             list => 'embeddedin',
122             eititle => $title,
123             eifilterredir => 'nonredirects',
124             # Doesn't work for De:* and anything non-en. Odd.
125             # einamespace => '0|8',
126             eilimit => '200',
127         },
128         {
129             max => '0',
130             hook => $callback,
131             skip_encoding => 1,
132         }
133     ) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
134 }
135
136 sub help
137 {
138     my %arg = @_;
139
140     Pod::Usage::pod2usage(
141         -verbose => $arg{ verbose },
142         -exitval => $arg{ exitval } || 0,
143     );
144 }