]> git.openstreetmap.org Git - rails.git/blobdiff - script/misc/update-wiki-pages
Stop wiki update script trying to be "helpful"
[rails.git] / script / misc / update-wiki-pages
index e01e9cae02bb698969dc8a2d1391798ce47a8cd7..3f15c0df0a7f69991c17799d2b947a365c8d4b00 100755 (executable)
@@ -3,20 +3,11 @@ use 5.010;
 use strict;
 use warnings;
 
-use Pod::Usage ();
-use Getopt::Long ();
-
-BEGIN {
-    eval "require MediaWiki::API; require YAML::XS;" or do {
-        print "You have to install some modules via CPAN to run this:\n";
-        print "   sudo cpanp MediaWiki::API YAML::XS\n";
-        exit 1;
-    };
-}
-
+use Getopt::Long;
+use Pod::Usage;
 use MediaWiki::API;
+use Test::More qw(no_plan);
 use YAML::XS qw(Dump);
-use Test::More 'no_plan';
 
 =head1 NAME
 
@@ -50,7 +41,7 @@ help() unless -f $out_file;
 # Get a API interface
 my $mw = MediaWiki::API->new();
 ok($mw, "Got a MediaWiki API");
-$mw->{config}->{api_url} = 'http://wiki.openstreetmap.org/w/api.php';
+$mw->{config}->{api_url} = 'https://wiki.openstreetmap.org/w/api.php';
 
 # All our goodies
 my (%feature, %count);
@@ -63,6 +54,7 @@ for my $lang ('', map { "${_}:" } qw[ Pt Fi De It HU Cz Fr RU Pl ]) {
     # Key pages
     ok(1, "    Getting key pages");
     my $cnt = stick_content_in_hash("key", "Template:${lang}KeyDescription", \%feature);
+    $cnt += stick_content_in_hash("key", "Template:${lang}Feature", \%feature);
     ok(1, "    Got $cnt key pages");
     $count{key} += $cnt;
 
@@ -97,29 +89,54 @@ sub stick_content_in_hash
     };
 
     my $count = 0;
+
+    my $process_link = sub {
+        my $link = shift;
+        $count++;
+        ok(1, "    ... got $count links") if $count % 200 == 0;
+        my $title = $link->{title};
+        my $lang;
+        my $key_name;
+        if ($title =~ /^$ukey:(?<key_name>.*?)$/) {
+            # English by default
+            $lang = "en";
+            $key_name = $space_to_underscore->($+{key_name});
+        } elsif ($title =~ /^(?<lang>[^:]+):$ukey:(?<key_name>.*?)$/) {
+            $lang = lc $+{lang};
+            $key_name = $space_to_underscore->($+{key_name});
+        }
+        if ($lang && !exists($hash->{$lang}->{$key}->{$key_name})) {
+            $hash->{$lang}->{$key}->{$key_name} = $title;
+        }
+    };
+
     get_embeddedin(
         $title,
         sub {
-            my ($links) = @_;
-            my (@links) = @$links;
-            ok(1, "    ... got " . scalar(@links) . " more links");
-            for my $link (@links) {
-                $count++;
-                my $title = $link->{title};
-
-                if ($title =~ /^$ukey:(?<key_name>.*?)$/) {
-                    # English by default
-                    $hash->{en}->{$key}->{ $space_to_underscore->($+{key_name}) } = $title;
-                } elsif ($title =~ /^(?<lang>[^:]+):$ukey:(?<key_name>.*?)$/) {
-                    $hash->{lc $+{lang}}->{$key}->{ $space_to_underscore->($+{key_name}) } = $title;
+            my $link = shift;
+            $process_link->($link);
+            get_redirects(
+                $link->{title},
+                sub {
+                    my $link = shift;
+                    $process_link->($link) if exists($link->{redirect});
                 }
-            }
+            );
         }
     );
 
     return $count;
 }
 
+sub process_list
+{
+    my $callback = shift;
+    my $links = shift;
+    for my $link (@$links) {
+        $callback->($link);
+    }
+}
+
 sub get_embeddedin
 {
     my ($title, $callback) = @_;
@@ -128,14 +145,35 @@ sub get_embeddedin
             action => 'query',
             list => 'embeddedin',
             eititle => $title,
-            eifilterredir => 'all',
+            eifilterredir => 'nonredirects',
             # Doesn't work for De:* and anything non-en. Odd.
             # einamespace => '0|8',
             eilimit => '200',
         },
         {
             max => '0',
-            hook => $callback,
+            hook => sub { process_list($callback, @_) },
+            skip_encoding => 1,
+        }
+    ) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
+}
+
+sub get_redirects
+{
+    my ($title, $callback) = @_;
+    my $articles = $mw->list(
+        {
+            action => 'query',
+            list => 'backlinks',
+            bltitle => $title,
+            blfilterredir => 'redirects',
+            # Doesn't work for De:* and anything non-en. Odd.
+            # einamespace => '0|8',
+            bllimit => '200',
+        },
+        {
+            max => '0',
+            hook => sub { process_list($callback, @_) },
             skip_encoding => 1,
         }
     ) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};