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