]> git.openstreetmap.org Git - rails.git/blob - script/misc/update-wiki-pages
c8fe83fb2da7df1dd53bd116926abcb092db0920
[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 Or with prove(1):
29
30     prove -e 'perl script/misc/update-wiki-pages' config/wiki_pages.yml
31
32 =cut
33
34 # Get the command-line options
35 Getopt::Long::Parser->new(
36     config => [ qw< bundling no_ignore_case no_require_order pass_through > ],
37 )->getoptions(
38     'h|help' => \my $help,
39 ) or help();
40
41 # On --help
42 help() if $help;
43
44 help() unless $ARGV[0];
45
46 # Get a API interface
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';
50
51 # All our goodies
52 my (%feature, %count);
53
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'");
58
59     # Key pages
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");
63     $count{key} += $cnt;
64
65     # Value 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;
70 }
71
72 ok(1, "Got a total of $count{$_} ${_}s") for qw[ key value ];
73
74 # Dump to .yml file
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";
78 say $out "";
79 say $out Dump(\%feature);
80 close $out;
81
82 exit 0;
83
84 sub stick_content_in_hash
85 {
86     my ($key, $title, $hash) = @_;
87     my $ukey = ucfirst $key;
88
89     my $space_to_underscore = sub {
90         my $txt = shift;
91         $txt =~ s/ /_/g;
92         $txt;
93     };
94
95     my $count = 0;
96     get_embeddedin(
97         $title,
98         sub {
99             my ($links) = @_;
100             my (@links) = @$links;
101             ok(1, "    ... got " . scalar(@links) . " more links");
102             for my $link (@links) {
103                 $count++;
104                 my $title = $link->{title};
105
106                 if ($title =~ /^$ukey:(?<key_name>.*?)$/) {
107                     # English by default
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;
111                 }
112             }
113         }
114     );
115
116     return $count;
117 }
118
119 sub get_embeddedin
120 {
121     my ($title, $callback) = @_;
122     my $articles = $mw->list(
123         {
124             action => 'query',
125             list => 'embeddedin',
126             eititle => $title,
127             eifilterredir => 'nonredirects',
128             # Doesn't work for De:* and anything non-en. Odd.
129             # einamespace => '0|8',
130             eilimit => '200',
131         },
132         {
133             max => '0',
134             hook => $callback,
135             skip_encoding => 1,
136         }
137     ) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
138 }
139
140 sub help
141 {
142     my %arg = @_;
143
144     Pod::Usage::pod2usage(
145         -verbose => $arg{ verbose },
146         -exitval => $arg{ exitval } || 0,
147     );
148 }