]> git.openstreetmap.org Git - rails.git/commitdiff
Merge remote-tracking branch 'upstream/pull/2864'
authorTom Hughes <tom@compton.nu>
Thu, 1 Oct 2020 07:57:36 +0000 (08:57 +0100)
committerTom Hughes <tom@compton.nu>
Thu, 1 Oct 2020 07:57:36 +0000 (08:57 +0100)
16 files changed:
app/controllers/api/users_controller.rb
app/views/api/users/_user.json.jbuilder [new file with mode: 0644]
app/views/api/users/_user.xml.builder [moved from app/views/api/users/_user.builder with 100% similarity]
app/views/api/users/index.json.jbuilder [new file with mode: 0644]
app/views/api/users/index.xml.builder [moved from app/views/api/users/index.builder with 100% similarity]
app/views/api/users/show.json.jbuilder [new file with mode: 0644]
app/views/api/users/show.xml.builder [moved from app/views/api/users/show.builder with 100% similarity]
app/views/browse/_common_details.html.erb
app/views/browse/changeset.html.erb
app/views/changesets/_changeset.html.erb
app/views/users/_contact.html.erb
script/locale/diff [deleted file]
script/locale/merge-from-translatewiki [deleted file]
script/locale/po2yaml [deleted file]
script/locale/yaml2po [deleted file]
test/controllers/api/users_controller_test.rb

index d97e8774ecd7a5641f8cc94e301c9fef0ad3386d..b4a2efc7c3043fd06d9aee6b7deaaa76dab6f59a 100644 (file)
@@ -9,9 +9,15 @@ module Api
     around_action :api_call_handle_error
     before_action :lookup_user_by_id, :only => [:show]
 
+    before_action :set_request_formats, :except => [:gpx_files]
+
     def show
       if @user.visible?
-        render :content_type => "text/xml"
+        # Render the result
+        respond_to do |format|
+          format.xml
+          format.json
+        end
       else
         head :gone
       end
@@ -19,7 +25,11 @@ module Api
 
     def details
       @user = current_user
-      render :action => :show, :content_type => "text/xml"
+      # Render the result
+      respond_to do |format|
+        format.xml { render :show }
+        format.json { render :show }
+      end
     end
 
     def index
@@ -31,7 +41,11 @@ module Api
 
       @users = User.visible.find(ids)
 
-      render :content_type => "text/xml"
+      # Render the result
+      respond_to do |format|
+        format.xml
+        format.json
+      end
     end
 
     def gpx_files
diff --git a/app/views/api/users/_user.json.jbuilder b/app/views/api/users/_user.json.jbuilder
new file mode 100644 (file)
index 0000000..d89b42b
--- /dev/null
@@ -0,0 +1,69 @@
+json.user do
+  json.id user.id
+  json.display_name user.display_name
+  json.account_created user.creation_time.xmlschema
+  json.description user.description if user.description
+
+  if current_user && current_user == user
+    json.contributor_terms do
+      json.agreed user.terms_agreed.present?
+      json.pd user.consider_pd
+    end
+  else
+    json.contributor_terms do
+      json.agreed user.terms_agreed.present?
+    end
+  end
+
+  json.img do
+    json.href user_image_url(user) if user.avatar.attached? || user.image_use_gravatar
+  end
+
+  json.roles do
+    json.array! user.roles.map(&:role)
+  end
+
+  json.changesets do
+    json.count user.changesets.size
+  end
+
+  json.traces do
+    json.count user.traces.size
+  end
+
+  json.blocks do
+    json.received do
+      json.count user.blocks.size
+      json.active user.blocks.active.size
+    end
+
+    if user.moderator?
+      json.issued do
+        json.count user.blocks_created.size
+        json.active user.blocks_created.active.size
+      end
+    end
+  end
+
+  if current_user && current_user == user
+    if user.home_lat && user.home_lon
+      json.home do
+        json.lat user.home_lat
+        json.lon user.home_lon
+        json.zoom user.home_zoom
+      end
+    end
+
+    json.languages user.languages if user.languages?
+
+    json.messages do
+      json.received do
+        json.count user.messages.size
+        json.unread user.new_messages.size
+      end
+      json.sent do
+        json.count user.sent_messages.size
+      end
+    end
+  end
+end
diff --git a/app/views/api/users/index.json.jbuilder b/app/views/api/users/index.json.jbuilder
new file mode 100644 (file)
index 0000000..efc905c
--- /dev/null
@@ -0,0 +1,3 @@
+json.users(@users) do |user|
+  json.partial! user
+end
diff --git a/app/views/api/users/show.json.jbuilder b/app/views/api/users/show.json.jbuilder
new file mode 100644 (file)
index 0000000..51a8538
--- /dev/null
@@ -0,0 +1 @@
+json.partial! @user
index 351ec4c6a1edb73c43fd2c87167f5bab72b5afa0..3748f2f6587409b0c21166b1c24df2cb077cbd67 100644 (file)
@@ -1,21 +1,21 @@
-<h4>
+<h4 class="details">
+  <%= t "browse.version" %>
+  #<%= common_details.version %>
+</h4>
+
+<p class="font-italic">
   <% if common_details.changeset.tags['comment'].present? %>
     <%= linkify(common_details.changeset.tags["comment"]) %>
   <% else %>
     <%= t "browse.no_comment" %>
   <% end %>
-</h4>
+</p>
 
 <div class="details">
   <%= t "browse.#{common_details.visible? ? :edited : :deleted}_by_html",
         :time => time_ago_in_words(common_details.timestamp, :scope => :'datetime.distance_in_words_ago'),
         :user => changeset_user_link(common_details.changeset),
         :title => l(common_details.timestamp) %>
-</div>
-
-<div class="details">
-  <%= t "browse.version" %>
-  #<%= common_details.version %>
   &middot;
   <%= t "browse.in_changeset" %>
   #<%= link_to common_details.changeset_id, :action => :changeset, :id => common_details.changeset_id %>
index c15e1f8c61b932735f51a3288016e4af6e8ba334..1217c03be87d261951937d18d6a5453c69ffb562 100644 (file)
@@ -6,7 +6,9 @@
 </h2>
 
 <div class="browse-section">
-  <h6><%= linkify(@changeset.tags["comment"].to_s.presence || t("browse.no_comment")) %></h6>
+  <p class="font-italic">
+    <%= linkify(@changeset.tags["comment"].to_s.presence || t("browse.no_comment")) %>
+  </p>
   <div class="details"><%= changeset_details(@changeset) %></div>
 
   <%= render :partial => "tag_details", :object => @changeset.tags.except("comment") %>
index e3febb1c5ed0a96d48c4163f25d6e298fc263e42..267bdb5556b261dd4195cdb8c4b810c1ca4a1e75 100644 (file)
    end %>
 
 <%= tag.li :id => "changeset_#{changeset.id}", :data => { :changeset => changeset_data }, :class => "list-group-item" do %>
-  <h6>
+  <p class="font-italic">
     <a class="changeset_id text-dark" href="<%= changeset_path(changeset) %>">
       <%= changeset.tags["comment"].to_s.presence || t("browse.no_comment") %>
     </a>
-  </h6>
+  </p>
   <div class="comments comments-<%= changeset.comments.length %>">
     <%= changeset.comments.length %>
     <span class="icon note grey"></span>
index 4446c519d4c45012b85d4721b8a5014228092b5f..d46a619843f02d95f0c5d878025cac6df3fd7cbe 100644 (file)
@@ -23,9 +23,9 @@
       <% if changeset %>
         <%= t("users.show.latest edit", :ago => time_ago_in_words(changeset.created_at, :scope => :'datetime.distance_in_words_ago')) %>
         <% comment = changeset.tags["comment"].to_s != "" ? changeset.tags["comment"] : t("browse.no_comment") %>
-        "<%= link_to(comment,
-                     { :controller => "browse", :action => "changeset", :id => changeset.id },
-                     { :title => t("changesets.changeset.view_changeset_details") }) %>"
+        <q><%= link_to(comment,
+                       { :controller => "browse", :action => "changeset", :id => changeset.id },
+                       { :title => t("changesets.changeset.view_changeset_details") }) %></q>
       <% else %>
        <%= t "changesets.changeset.no_edits" %>
       <% end %>
diff --git a/script/locale/diff b/script/locale/diff
deleted file mode 100755 (executable)
index bfc6369..0000000
+++ /dev/null
@@ -1,382 +0,0 @@
-#!/usr/bin/env perl
-use feature ':5.10';
-use strict;
-use warnings;
-use YAML::Syck qw(Dump LoadFile);
-use Test::Differences;
-use Pod::Usage ();
-use Getopt::Long ();
-
-=head1 NAME
-
-locale-diff - Compare two YAML files and print how their datastructures differ
-
-=head1 SYNOPSIS
-
-    # --keys is the default
-    diff en.yml is.yml
-    diff --keys en.yml is.yml
-
-    # --untranslated-values compares prints keys whose values don't differ
-    diff --untranslated-values en.yml is.yml
-
-    # --untranslated-values-all compares prints keys whose values
-    # don't differ. Ignoring the blacklist which prunes things
-    # unlikley to be translated
-    diff --untranslated-values-all en.yml is.yml
-
-    # Check that interpolated variables ({{var}} and [[var]]) are the same
-    diff --validate-variables en.yml is.yml
-
-=head1 DESCRIPTION
-
-This utility prints the differences between two YAML files using
-L<Test::Differences>. The purpose of it is to diff the files is
-F<config/locales> to find out what keys need to be added to the
-translated files when F<en.yml> changes.
-
-=head1 OPTIONS
-
-=over
-
-=item -h, --help
-
-Print this help message.
-
-=item --keys
-
-Show the hash keys that differ between the two files, useful merging
-new entries from F<en.yml> to a local file.
-
-=item --untranslated-values
-
-Show keys that B<exist in both the compared files> and whose values
-are exactly the same. Use C<--keys> to a list of values that hasn't
-been merged.
-
-The values are pruned according to global and language specific
-blacklists found in the C<__DATA__> section of this script.
-
-This helps to find untranslated values.
-
-=item --untranslated-values-all
-
-Like C<--untranslated-values> but ignores blacklists.
-
-=item --validate-variables
-
-Check that interpolated Ruby i18n variables (C<{{foo}}> and
-C<[[foo]]>) are equivalent in the two provided files.
-
-=item --dump-flat
-
-Dump a flat version of the translation hash in YAML format,
-i.e. "foo.bar" instead of "{foo}->{bar}".
-
-=back
-
-=head1 AUTHOR
-
-E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avarab@gmail.com>
-
-=cut
-
-# Get the command-line options
-Getopt::Long::Parser->new(
-    config => [ qw< bundling no_ignore_case no_require_order pass_through > ],
-)->getoptions(
-    'h|help' => \my $help,
-    'keys' => \my $keys,
-    'dump-flat' => \my $dump_flat,
-    'untranslated-values' => \my $untranslated_values,
-    'untranslated-values-all' => \my $untranslated_values_all,
-    'validate-variables' => \my $validate_variables,
-    'reconstruct' => \my $reconstruct,
-) or help();
-
-# --keys is the default
-$keys = 1 if not $untranslated_values_all and not $untranslated_values and not $validate_variables and not $dump_flat;
-
-# On --help
-help() if $help;
-
-# If we're not given two .yml files
-help() if (@ARGV != 2 or (!-f $ARGV[0] or !-f $ARGV[1])) and not $dump_flat || $reconstruct;
-
-my ($from, $to) = @ARGV;
-
-my $from_data = LoadFile($from);
-my $from_parsed = { iterate($from_data->{basename($from)}) };
-
-if ($dump_flat)
-{
-    mark_utf8($from_parsed);
-
-    print Dump $from_parsed;
-
-    exit 0;
-}
-
-if ($reconstruct) {
-    mark_utf8($from_parsed);
-
-    my %out;
-    while (my ($k, $v) = each %$from_parsed) {
-        insert_string_deep(\%out, $k, $v);
-    }
-
-    print Dump { basename($from) => \%out };
-
-    exit 0;
-}
-
-my $to_data   = LoadFile($to);
-my $to_parsed = { iterate($to_data->{basename($to)}) };
-
-if ($keys)
-{
-    print_key_differences($from_parsed, $to_parsed);
-}
-elsif ($untranslated_values or $untranslated_values_all)
-{
-    my @untranslated = untranslated_keys($from_parsed, $to_parsed);
-
-    # Prune according to blacklist
-    if ($untranslated_values) {
-        @untranslated = prune_untranslated_with_blacklist(basename($to), @untranslated);
-    }
-
-    say for @untranslated;
-} elsif ($validate_variables)
-{
-    print_validate_variables($from_parsed, $to_parsed);
-}
-
-exit 0;
-
-sub print_key_differences
-{
-    my ($f, $t) = @_;
-
-    # Hack around Test::Differences wanting a Test::* module loaded
-    $INC{"Test.pm"} = 1;
-    sub Test::ok { print shift }
-
-    # Diff the tree
-    eq_or_diff([ sort keys %$f ], [ sort keys %$t ]);
-}
-
-sub untranslated_keys
-{
-    my ($from_parsed, $to_parsed) = @_;
-    sort grep { exists $to_parsed->{$_} and $from_parsed->{$_} eq $to_parsed->{$_} } keys %$from_parsed;
-}
-
-sub prune_untranslated_with_blacklist
-{
-    my ($language, @keys) = @_;
-    my %keys;
-    @keys{@keys} = ();
-
-    my $end_yaml = LoadFile(*DATA);
-    my $untranslated_values = $end_yaml->{untranslated_values};
-    my $default = $untranslated_values->{default};
-    my $this_language = $untranslated_values->{$language} || {};
-
-    my %bw_list = (%$default, %$this_language);
-    
-    while (my ($key, $blacklisted) = each %bw_list)
-    {
-        # FIXME: Does syck actually support true/false booleans in yaml?
-        delete $keys{$key} if $blacklisted eq 'true'
-    }
-
-    sort keys %keys;
-}
-
-sub print_validate_variables
-{
-    my ($f, $t) = @_;
-
-    while (my ($key, $val) = each %$f)
-    {
-        next if exists $f->{$key} and not exists $t->{$key};
-
-        my @from_var = parse_variables_from_string($f->{$key});
-        my @to_var   = parse_variables_from_string($t->{$key});
-
-        unless (@from_var ~~ @to_var) {
-            say "$key in $from has (@from_var) and $to has (@to_var)";
-        }
-
-    }
-}
-
-sub parse_variables_from_string
-{
-    my ($string) = @_;
-
-    # This probably matches most of the variables
-    my $var = qr/ [a-z0-9_]+? /xs;
-
-    if (my @var = $string =~ m/ \{\{ ($var) \}\} | \[\[ ($var) \]\] /gsx) {
-        return sort grep { defined } @var;
-    } else {
-        return;
-    }
-}
-
-sub iterate
-{
-    my ($hash, @path) = @_;
-    my @ret;
-        
-    while (my ($k, $v) = each %$hash)
-    {
-        if (ref $v eq 'HASH')
-        {
-             push @ret => iterate($v, @path, $k);
-        }
-        else
-        {
-            push @ret => join(".",@path, $k), $v;
-        }
-    }
-
-    return @ret;
-}
-
-# $s = 'foo.bar.baz.spam.eggs.ham'; $h = \%h; $h = $h->{$_} = {} for split /\./, $s; \%h
-# ==> {foo => {bar => {baz => {spam => {eggs => {ham => {}}}}}}}
-sub insert_string_deep {
-    my ($h, $ks, $v) = @_;
-    my $p = \$h; $p = \$$p->{$_} for split /\./, $ks;
-    $$p = $v;
-}
-
-# sub insert_string_deep
-# {
-#     my ($hash, $key, $value) = @_;
-#
-#     my @key = split /\./, $key;
-#     my $h = $hash;
-#
-#     my $i = 0;
-#     for my $k (@key) {
-#         $i ++;
-#         if ($i == @key) {
-#             $h->{$k} = $value;
-#         } else {
-#             if (ref $h->{$k}) {
-#                 $h = $h->{$k};
-#             } else {
-#                 $h = $h->{$k} = {};
-#             }
-#         }
-#     }
-# }
-
-sub basename
-{
-    my $name = shift;
-    $name =~ s[\..*?$][];
-    $name =~ s[.*/][];
-    $name;
-}
-
-sub mark_utf8
-{
-    my ($hash) = @_;
-
-    # Mark as UTF-8
-    map { if (ref $_ eq 'ARRAY') { map { utf8::decode($_) } @$_ } else {  utf8::decode($_) } } values %$hash;
-}
-
-sub help
-{
-    my %arg = @_;
-
-    Pod::Usage::pod2usage(
-        -verbose => $arg{ verbose },
-        -exitval => $arg{ exitval } || 0,
-    );
-}
-
-__DATA__
-untranslated_values:
-
-  # Default/Per language blacklist/whitelist for the
-  # --untranslated-values switch. "true" as a value indicates that the
-  # key is to be blacklisted, and "false" that it's to be
-  # whitelisted. "false" is only required to whitelist a key
-  # blacklisted by default on a per-language basis.
-
-  default:
-    html.dir: true
-    layouts.intro_3_bytemark: true
-    layouts.intro_3_ucl: true
-    layouts.project_name.h1: true
-    layouts.project_name.title: true
-    site.index.license.project_url: true
-    browse.relation_member.entry: true
-
-    # #{{id}}
-    changeset.changeset.id: true
-
-  de:
-    activerecord.attributes.message.sender: true
-    activerecord.attributes.trace.name: true
-    activerecord.models.changeset: true
-    activerecord.models.relation: true
-    browse.changeset.changeset: true
-    browse.changeset.changesetxml: true
-    browse.changeset.osmchangexml: true
-    browse.changeset.title: true
-    browse.common_details.version: true
-    browse.containing_relation.relation: true
-    browse.relation.relation: true
-    browse.relation.relation_title: true
-    browse.start_rjs.details: true
-    browse.start_rjs.object_list.details: true
-    browse.tag_details.tags: true
-    changeset.changesets.id: true
-    export.start.export_button: true
-    export.start.format: true
-    export.start.output: true
-    export.start.zoom: true
-    export.start_rjs.export: true
-    layouts.export: true
-    layouts.shop: true
-    site.edit.anon_edits: true
-    site.index.license.license_name: true
-    site.index.permalink: true
-    site.key.table.entry.park: true
-    site.search.submit_text: true
-    trace.edit.tags: true
-    trace.trace.in: true
-    trace.trace_form.tags: true
-    trace.trace_optionals.tags: true
-    trace.view.tags: true
-    user.account.public editing.enabled link: true
-
-  is:
-    # ({{link}})
-    site.edit.anon_edits: true
-
-    # Creative Commons Attribution-Share Alike 2.0
-    site.index.license.license_name: true
-
-    # http://creativecommons.org/licenses/by-sa/2.0/
-    site.index.license.license_url: true
-
-    # {{id}}
-    printable_name.with_id: true
-    
-    # {{name}} ({{id}})
-    printable_name.with_name: true
-
-    # {{type}} 
-    geocoder.search_osm_namefinder.prefix: true
-
-    # {{suffix}}, {{parentname}}
-    geocoder.search_osm_namefinder.suffix_suburb: true
diff --git a/script/locale/merge-from-translatewiki b/script/locale/merge-from-translatewiki
deleted file mode 100755 (executable)
index 706ec45..0000000
+++ /dev/null
@@ -1,442 +0,0 @@
-#!/usr/bin/env perl
-use feature ':5.10';
-use strict;
-use warnings;
-use File::Slurp qw(slurp);
-use YAML::Syck qw(Dump Load LoadFile DumpFile);
-BEGIN {
-    $YAML::Syck::Headless = 1;
-    $YAML::Syck::SortKeys = 1;
-}
-use WWW::Mechanize;
-use HTML::TableParser::Grid;
-use Pod::Usage ();
-use Getopt::Long ();
-use Data::Dump 'dump';
-use File::Spec::Functions qw(catfile);
-use Storable;
-use autodie;
-
-=head1 NAME
-
-merge-from-translatewiki - Get new translations from L<http://translatewiki.net> and selectively merge them with ours
-
-=head1 SYNOPSIS
-
-    # Run this normally, hopefully...
-    merge-from-translatewiki --locales-dir=config/locales
-
-    # Diff the existing files:
-    config/locales$ for i in $(ls *yml | grep -v en.yml); do perl ../../script/locale/diff --dump-flat $i > $i.0 ;done
-
-    # Merge and find out what changed:
-    rails_port$ perl script/locale/merge-from-translatewiki --locales-dir config/locales
-
-    # Or, more complexy:
-    rails_port$ for i in $(svn st config/locales/ | egrep '^M|\\?' | awk '{print $2}' | grep 'yml$'); do rm -v $i; done && svn up config/locales && perl script/locale/merge-from-translatewiki --locales-dir config/locales && svn st config/locales
-    # Diff:
-    config/locales$ for i in $(ls *yml | grep -v en.yml); do perl ../../script/locale/diff --dump-flat $i > $i.1 ;done && for i in $(ls *yml | grep -v en.yml); do diff -ru $i.*; done
-
-=head1 DESCRIPTION
-
-Translatewiki's export process L<is
-broken|http://trac.openstreetmap.org/ticket/2305>. This script imports
-new messages from it while tiptoeing around known bugs.
-
-=head1 OPTIONS
-
-=over
-
-=item -h, --help
-
-Print this help message.
-
-=item --locales-dir
-
-The locales dir we'll merge stuff into. F<config/locales> by default.
-
-=item --only-new
-
-Only import translations that don't exists for us yet.
-
-=item --cache
-
-Write a L<Storable> cache for things downloaded from Translatewiki and
-use it if it exists.
-
-=back
-
-=head1 AUTHOR
-
-E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avarab@gmail.com>
-
-=cut
-
-# Get the command-line options
-Getopt::Long::Parser->new(
-    config => [ qw< bundling no_ignore_case no_require_order pass_through > ],
-)->getoptions(
-    'h|help' => \my $help,
-    'locales-dir=s' => \(my $locales_dir = 'config/locales'),
-    'only-new' => \my $only_new,
-    'cache' => \my $cache,
-) or help();
-
-# On --help
-help() if $help;
-
-help() unless $locales_dir and -d $locales_dir;
-
-###
-### Main
-###
-
-### Get Translatewiki data
-my %translatewiki_languages = translatewiki_languages();
-
-# Don't process English from Translatewiki
-delete $translatewiki_languages{en};
-
-#say Dump \%translatewiki_languages;
-
-my @translatewiki_languages_codes = keys %translatewiki_languages;
-my %translatewiki_translations = get_translatewiki_translations(@translatewiki_languages_codes);
-
-#say Dump \%translatewiki_translations;
-
-### Get our existing data
-my %my_translations;
-my @my_yaml_files = glob catfile($locales_dir, '*.yml');
-for my $my_yaml_file (@my_yaml_files) {
-    my $basename = basename($my_yaml_file);
-    my $tw_lang = lc $basename;
-
-    say STDERR "Loading my translation $tw_lang ($my_yaml_file)";
-    $my_translations{$tw_lang} = load_and_flatten_yaml(scalar slurp($my_yaml_file));
-}
-
-say "loaded my translations";
-
-## Write out merged data
-for my $translatewiki_lang (sort @translatewiki_languages_codes) {
-    my $rails_lang = $translatewiki_lang; $rails_lang =~ s/(?<=-)(\w+)/\U$1\E/;
-    my $out_file = catfile($locales_dir, $rails_lang . '.yml');
-
-    unless (-f $out_file) {
-        # No translation like this exists
-        say STDERR "$rails_lang has no existing translation. Importing as-is from Translatewiki to $out_file";
-        my $expanded = expand_hash($translatewiki_translations{$translatewiki_lang});
-        my $out = +{ $rails_lang => $expanded };
-        spit_out($out_file, $out);
-    } elsif (ref $my_translations{$translatewiki_lang} eq 'HASH' and not $only_new) {
-        say STDERR "$rails_lang has existing translations. Merging the old translation with the new Translatewiki one";
-
-        # Get the data
-        my %tw = %{ $translatewiki_translations{$translatewiki_lang} };
-        my %me = %{ $my_translations{$translatewiki_lang} };
-        my %en = %{ $my_translations{en} };
-        # Use %tw to start with
-        my %new = %tw;
-
-        ### Merge stuff
-
-        ## These keys shouldn't be removed but are due to
-        ## Translatewiki fail (they were missing in the original
-        ## import)
-        my @url_keys = qw(
-                             browse.relation_member.entry
-                             changeset.changeset.id
-                             geocoder.search_osm_namefinder.suffix_suburb
-                             html.dir
-                             layouts.intro_3_bytemark
-                             layouts.intro_3_ucl
-                             layouts.project_name.h1
-                             layouts.project_name.title
-                             printable_name.with_version
-                             site.edit.anon_edits
-                             layouts.help_wiki_url
-                             layouts.shop_url
-                             notifier.gpx_notification.failure.import_failures_url
-                             notifier.signup_confirm_plain.the_wiki_url
-                             notifier.signup_confirm_plain.wiki_signup_url
-                             trace.edit.visibility_help_url
-                             trace.trace_form.help_url
-                             trace.trace_form.visibility_help_url
-                     );
-
-        for my $key (@url_keys) {
-            if ( exists $me{$key} and not exists $new{$key} ) {
-                $new{$key} = $me{$key} if $me{$key} ne $en{$key};
-            }
-        }
-
-        ## When foo exists in this file but only foo.one, foo,other
-        ## etc in English or the original file we don't want to throw away what we have
-        my @plural_keys = qw( zero one many few other two );
-
-        while (my ($me_k, $me_v) = each %me) {
-            if (not exists $tw{ $me_k } and 
-                not exists $en{ $me_k } and
-                (
-                    exists $en{ $me_k . '.zero' } or
-                    exists $en{ $me_k . '.one' } or
-                    exists $en{ $me_k . '.many' } or
-                    exists $en{ $me_k . '.few' } or
-                    exists $en{ $me_k . '.other' } or
-                    exists $en{ $me_k . '.two' })) {
-                #say STDERR "Bringing back nuked plural form '$me_k' Setting it to '$me{ $me_k }'";
-                $new{ $me_k } = $me{ $me_k };
-            }
-        }
-
-        # Both arrays and strings are supported in the site key. Avoid removing e.g.:
-        #   -site.key.table.entry.school: 學校;大學
-        # Just because en.yml has site.key.table.entry.school.0 and site.key.table.entry.school.1
-        while (my ($me_k, $me_v) = each %me) {
-            next unless $me_k =~ /^site\.key\.table\.entry/;
-            next if $me_k =~ /\.\d+$/;
-
-            if (ref $en{ $me_k } eq 'ARRAY' and not ref $me{ $me_k }) {
-                $new{ $me_k } = $me{ $me_k };
-            }
-        }
-
-        # There are a bunch of keys on Translatewiki that are
-        # equivalent to English for some reason. Probably because they
-        # were there at import time. Nuke them.
-        while (my ($new_k, $new_v) = each %new) {
-            if (exists $en{ $new_k } and $en{ $new_k } eq $new_v) {
-                #say "Purging dupe in $rails_lang: $new_k=$new_v";
-                delete $new{ $new_k };
-            }
-        }
-
-        my $expanded = expand_hash( \%new );
-        my $out = +{ $rails_lang => $expanded };
-        spit_out($out_file, $out);
-    } elsif (not $only_new) {
-        die "Internal error on $translatewiki_lang";
-    }
-}
-
-sub spit_out
-{
-    my ($file, $data) = @_;
-    my $yaml_out = Dump $data;
-    
-    open my $fh, ">", $file;
-    print $fh $yaml_out;
-    close $fh;
-}
-
-#
-# YAML stuff
-#
-
-sub mark_utf8
-{
-    my ($hash) = @_;
-
-    # Mark as UTF-8
-    map { if (ref $_ eq 'ARRAY') { map { utf8::decode($_) } @$_ } else {  utf8::decode($_) } } values %$hash;
-}
-
-sub iterate
-{
-    my ($hash, @path) = @_;
-    my @ret;
-        
-    while (my ($k, $v) = each %$hash)
-    {
-        if (ref $v eq 'HASH')
-        {
-             push @ret => iterate($v, @path, $k);
-        }
-        else
-        {
-            push @ret => join(".",@path, $k), $v;
-        }
-    }
-
-    return @ret;
-}
-
-sub expand_hash
-{
-    my ($flat_hash) = @_;
-    my %new_hash;
-    while (my ($k, $v) = each %$flat_hash) {
-        #say "Inserting $k=$v";
-        insert_string_deep(\%new_hash, $k, $v);
-    }
-
-    \%new_hash;
-}
-
-# Fails under strict in certain cases:
-## Inserting browse.start_rjs.object_list.history.type.way=Vía [[id]]
-## Inserting activerecord.models.relation_tag=Etiqueta de la relación
-## Inserting browse.changeset_details.has_nodes.one=Tiene el siguiente {{count}} nodo:
-## Can't use string ("Tiene {{count}} nodos:") as a HASH ref while "strict refs" in use at script/locale/merge-from-translatewiki line 234.
-# Line 234 = my $p = \$h; $p = \$$p->{$_} for split /\./, $ks;
-
-# sub insert_string_deep_X {
-#     my ($h, $ks, $v) = @_;
-#     my $p = \$h; $p = \$$p->{$_} for split /\./, $ks;
-#     $$p = $v;
-# }
-
-sub insert_string_deep
-{
-    my ($hash, $key, $value) = @_;
-
-    my @key = split /\./, $key;
-    my $h = $hash;
-
-    my $i = 0;
-    for my $k (@key) {
-        $i ++;
-        if ($i == @key) {
-            $h->{$k} = $value;
-        } else {
-            if (ref $h->{$k}) {
-                $h = $h->{$k};
-            } else {
-                $h = $h->{$k} = {};
-            }
-        }
-    }
-}
-
-#
-# Get language from Translatewiki
-#
-
-sub get_translatewiki_translations
-{
-    my @languages = @_;
-
-    my $cache_file = "/tmp/merge-from-translatewiki.storable";
-    if ($cache) {
-        if (-f $cache_file) {
-            my $c = retrieve($cache_file);
-            return %$c;
-        }
-    }
-
-    my %translatewiki_languages;
-    my $all_count = scalar @languages;
-    say "Translatewiki has $all_count languages I'm about to get";
-    my $count = 0;
-    for my $lang (@languages) {
-        $count ++;
-        say STDERR "Getting language $count/$all_count ($lang) from Translatewiki";
-        my $yaml = get_language_from_translatewiki($lang);
-
-        my $flat_data = load_and_flatten_yaml($yaml);
-
-        $translatewiki_languages{$lang} = $flat_data;
-    }
-
-    if ($cache) {
-        store \%translatewiki_languages, $cache_file;
-    }
-
-    return %translatewiki_languages;
-}
-
-sub get_language_from_translatewiki
-{
-    my ($lang) = @_;
-    my $mech = WWW::Mechanize->new;
-
-    $mech->get("http://translatewiki.net/w/i.php?title=Special%3ATranslate&task=export-to-file&group=out-osm-site&language=$lang");
-
-    die "Couldn't get lang $lang lang from Translatewiki" unless $mech->success;
-
-    return $mech->content;
-}
-
-#
-# from language list
-#
-
-sub translatewiki_languages
-{
-    my $mech = WWW::Mechanize->new;
-
-    $mech->get('http://translatewiki.net/wiki/Translating:OpenStreetMap/stats/trunk/site');
-
-    die "Couldn't get translatewiki table" unless $mech->success;
-
-    my $content = $mech->content;
-    my ($sortable) = $content =~ m[(<table class="sortable.*</table>)]s;
-
-    my @table = parse_language_table($sortable);
-
-    # Just get the codes
-    map { $_->{code} => $_->{language} } @table;
-}
-
-sub parse_language_table
-{
-    my ($table) = @_;
-
-    my $parser = HTML::TableParser::Grid->new($table);
-    my @rows;
-    for my $n (0 .. $parser->num_rows - 1) {
-        my %row;
-        @row{qw(code language done fuzzy)} = $parser->row($n);
-        mark_utf8(\%row);
-
-        push @rows => \%row;
-    }
-
-    @rows;
-}
-
-#
-# Misc
-#
-
-sub basename
-{
-    my $name = shift;
-    $name =~ s[\..*?$][];
-    $name =~ s[.*/][];
-    $name;
-}
-
-sub load_and_flatten_yaml
-{
-    my ($yaml) = @_;
-
-    my $data = Load($yaml);
-
-    # Remove the root $lang => key
-    my @keys = keys %$data;
-    die "YAML data had more than 1 root key" if @keys != 1;
-    $data = $data->{$keys[0]};
-
-    # Flatten it
-    my $flat_data = { iterate($data) };
-
-    mark_utf8($flat_data);
-
-    $flat_data;
-}
-
-#
-# Help
-#
-
-sub help
-{
-    my %arg = @_;
-
-    Pod::Usage::pod2usage(
-        -verbose => $arg{ verbose },
-        -exitval => $arg{ exitval } || 0,
-    );
-}
diff --git a/script/locale/po2yaml b/script/locale/po2yaml
deleted file mode 100755 (executable)
index 69278ea..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-#!/usr/bin/env ruby
-# po2yaml, for converting gettext .po to the RoR translation YAML
-# Use:
-#  - To create a language's yaml from a given po file
-#    po2yaml de.po > de.yml
-
-require "yaml"
-
-def add_translation(hash, keys, value)
-  key = keys.shift
-  if keys.empty?
-    hash[key] = value
-  else
-    hash[key] ||= {}
-
-    add_translation(hash[key], keys, value)
-  end
-  hash
-end
-
-def po2hash(f)
-  trs = {}
-  path = []
-  msgstr = ""
-  f.each_line do |line|
-    line.strip!
-    if line[0..8] == 'msgctxt "'
-      path = line[9..-2].split(":")
-    elsif line[0..7] == 'msgstr "'
-      msgstr = line[8..-2]
-    end
-
-    next if path.empty? || msgstr.empty?
-
-    add_translation(trs, path, msgstr)
-    path = []
-    msgstr = ""
-  end
-  trs
-end
-
-filename = ARGV[0]
-pofile = File.open(filename, "r")
-
-langcode = File.basename(filename, ".po")
-
-tr = { langcode => po2hash(pofile) }
-
-print tr.to_yaml
diff --git a/script/locale/yaml2po b/script/locale/yaml2po
deleted file mode 100755 (executable)
index e1233fc..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-#!/usr/bin/env ruby
-# yaml2po, for converting RoR translation YAML to the standard gettext
-#          for eventual use with a translation site such as Transifex
-# Use:
-#  - To create a 'master' .pot
-#    yaml2po > translations.pot
-#  - To create a language's .po (includes from scratch)
-#    yaml2po de > de.po
-#  - To create all languages' .pos and a .pot (under /config/locales/po)
-#    yaml2po --all
-
-require "yaml"
-require "optparse"
-
-LOCALE_DIR = File.dirname(__FILE__) + "/../../config/locales/"
-EN = YAML.load_file("#{LOCALE_DIR}en.yml")
-
-def iterate(hash, fhash = {}, path = "", outfile = $stdout)
-  hash.each do |key, val|
-    fhash[key] = {} unless fhash.key? key
-    if val.is_a? Hash
-      fhash[key] = {} unless fhash[key].is_a? Hash
-      iterate(val, fhash[key], "#{path}#{key}:#{outfile}")
-    else
-      outfile.puts "msgctxt \"#{path}#{key}\""
-      outfile.puts "msgid \"#{val}\""
-      outfile.puts "msgstr \"#{fhash[key]}\""
-    end
-  end
-end
-
-def lang2po(lang, outfile = $stdout)
-  puts lang
-  infile = "#{LOCALE_DIR}#{lang}.yml"
-  if File.exist? infile
-    oth = YAML.load_file(infile)
-    oth = oth[lang]
-    iterate(EN["en"], oth, "", outfile)
-  else
-    iterate(EN["en"], {}, "", outfile)
-  end
-end
-
-opt = ARGV[0]
-if opt == "--all"
-  # Produce .po files for all langs, and a .pot template
-  po_dir = "#{LOCALE_DIR}po/"
-  Dir.mkdir(po_dir) unless File.directory?(po_dir)
-  Dir.glob("#{LOCALE_DIR}/*.yml") do |filename|
-    lang = File.basename(filename, ".yml")
-    unless lang == "en"
-      outfile = File.new("#{po_dir}#{lang}.po", "w")
-      lang2po(lang, outfile)
-      outfile.close
-    end
-  end
-  outfile = File.new("#{po_dir}rails_port.pot", "w")
-  iterate(EN["en"], {}, "", outfile)
-  outfile.close
-elsif opt
-  lang2po(opt)
-else
-  iterate(EN["en"])
-end
index 7ebe50d7b77213cd9794ea026114e081033c161d..5ce30d6a614f4e7f3d84cd5c2af2aaf675f56ac8 100644 (file)
@@ -9,10 +9,18 @@ module Api
         { :path => "/api/0.6/user/1", :method => :get },
         { :controller => "api/users", :action => "show", :id => "1" }
       )
+      assert_routing(
+        { :path => "/api/0.6/user/1.json", :method => :get },
+        { :controller => "api/users", :action => "show", :id => "1", :format => "json" }
+      )
       assert_routing(
         { :path => "/api/0.6/user/details", :method => :get },
         { :controller => "api/users", :action => "details" }
       )
+      assert_routing(
+        { :path => "/api/0.6/user/details.json", :method => :get },
+        { :controller => "api/users", :action => "details", :format => "json" }
+      )
       assert_routing(
         { :path => "/api/0.6/user/gpx_files", :method => :get },
         { :controller => "api/users", :action => "gpx_files" }
@@ -21,6 +29,10 @@ module Api
         { :path => "/api/0.6/users", :method => :get },
         { :controller => "api/users", :action => "index" }
       )
+      assert_routing(
+        { :path => "/api/0.6/users.json", :method => :get },
+        { :controller => "api/users", :action => "index", :format => "json" }
+      )
     end
 
     def test_show
@@ -28,7 +40,7 @@ module Api
       # check that a visible user is returned properly
       get api_user_path(:id => user.id)
       assert_response :success
-      assert_equal "text/xml", response.media_type
+      assert_equal "application/xml", response.media_type
 
       # check the data that is returned
       assert_select "description", :count => 1, :text => "test"
@@ -69,6 +81,15 @@ module Api
       # check that a non-existent user is not returned
       get api_user_path(:id => 0)
       assert_response :not_found
+
+      # check that a visible user is returned properly in json
+      get api_user_path(:id => user.id, :format => "json")
+      assert_response :success
+      assert_equal "application/json", response.media_type
+
+      js = ActiveSupport::JSON.decode(@response.body)
+      assert_not_nil js
+      assert_equal user.id, js["user"]["id"]
     end
 
     def test_details
@@ -84,7 +105,7 @@ module Api
       auth_header = basic_authorization_header user.email, "test"
       get user_details_path, :headers => auth_header
       assert_response :success
-      assert_equal "text/xml", response.media_type
+      assert_equal "application/xml", response.media_type
 
       # check the data that is returned
       assert_select "description", :count => 1, :text => "test"
@@ -130,16 +151,25 @@ module Api
 
       get api_users_path(:users => user1.id)
       assert_response :success
-      assert_equal "text/xml", response.media_type
+      assert_equal "application/xml", response.media_type
       assert_select "user", :count => 1 do
         assert_select "user[id='#{user1.id}']", :count => 1
         assert_select "user[id='#{user2.id}']", :count => 0
         assert_select "user[id='#{user3.id}']", :count => 0
       end
 
+      # Test json
+      get api_users_path(:users => user1.id, :format => "json")
+      assert_response :success
+      assert_equal "application/json", response.media_type
+
+      js = ActiveSupport::JSON.decode(@response.body)
+      assert_not_nil js
+      assert_equal 1, js["users"].count
+
       get api_users_path(:users => user2.id)
       assert_response :success
-      assert_equal "text/xml", response.media_type
+      assert_equal "application/xml", response.media_type
       assert_select "user", :count => 1 do
         assert_select "user[id='#{user1.id}']", :count => 0
         assert_select "user[id='#{user2.id}']", :count => 1
@@ -148,7 +178,7 @@ module Api
 
       get api_users_path(:users => "#{user1.id},#{user3.id}")
       assert_response :success
-      assert_equal "text/xml", response.media_type
+      assert_equal "application/xml", response.media_type
       assert_select "user", :count => 2 do
         assert_select "user[id='#{user1.id}']", :count => 1
         assert_select "user[id='#{user2.id}']", :count => 0