From 8a57904398175d9143cec737282f5066793bdbc3 Mon Sep 17 00:00:00 2001 From: Matt Amos Date: Mon, 27 Oct 2008 17:50:28 +0000 Subject: [PATCH] Implemented osmChange diff downloads for changesets and a couple of tests. --- app/controllers/changeset_controller.rb | 73 ++++++++++++++ config/routes.rb | 1 + test/functional/changeset_controller_test.rb | 100 +++++++++++++++++++ 3 files changed, 174 insertions(+) diff --git a/app/controllers/changeset_controller.rb b/app/controllers/changeset_controller.rb index bb8c3cef2..7ac4eb91a 100644 --- a/app/controllers/changeset_controller.rb +++ b/app/controllers/changeset_controller.rb @@ -106,4 +106,77 @@ class ChangesetController < ApplicationController rescue OSM::APIError => ex render ex.render_opts end + + ## + # download the changeset as an osmChange document. + # + # to make it easier to revert diffs it would be better if the osmChange + # format were reversible, i.e: contained both old and new versions of + # modified elements. but it doesn't at the moment... + # + # this method cannot order the database changes fully (i.e: timestamp and + # version number may be too coarse) so the resulting diff may not apply + # to a different database. however since changesets are not atomic this + # behaviour cannot be guaranteed anyway and is the result of a design + # choice. + def download + changeset = Changeset.find(params[:id]) + + # get all the elements in the changeset and stick them in a big array. + elements = [changeset.old_nodes, + changeset.old_ways, + changeset.old_relations].flatten + + # sort the elements by timestamp and version number, as this is the + # almost sensible ordering available. this would be much nicer if + # global (SVN-style) versioning were used - then that would be + # unambiguous. + elements.sort! do |a, b| + if (a.timestamp == b.timestamp) + a.version <=> b.version + else + a.timestamp <=> b.timestamp + end + end + + # create an osmChange document for the output + result = OSM::API.new.get_xml_doc + result.root.name = "osmChange" + + # generate an output element for each operation. note: we avoid looking + # at the history because it is simpler - but it would be more correct to + # check these assertions. + elements.each do |elt| + result.root << + if (elt.version == 1) + # first version, so it must be newly-created. + created = XML::Node.new "create" + created << elt.to_xml_node + else + # get the previous version from the element history + prev_elt = elt.class.find(:first, :conditions => + ['id = ? and version = ?', + elt.id, elt.version]) + unless elt.visible + # if the element isn't visible then it must have been deleted, so + # output the *previous* XML + deleted = XML::Node.new "delete" + deleted << prev_elt.to_xml_node + else + # must be a modify, for which we don't need the previous version + # yet... + modified = XML::Node.new "modify" + modified << elt.to_xml_node + end + end + end + + render :text => result.to_s, :content_type => "text/xml" + + rescue ActiveRecord::RecordNotFound + render :nothing => true, :status => :not_found + rescue OSM::APIError => ex + render ex.render_opts + end + end diff --git a/config/routes.rb b/config/routes.rb index 39e2a1e74..139611bc6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,7 @@ ActionController::Routing::Routes.draw do |map| map.connect "api/#{API_VERSION}/changeset/create", :controller => 'changeset', :action => 'create' map.connect "api/#{API_VERSION}/changeset/:id/upload", :controller => 'changeset', :action => 'upload', :id => /\d+/ + map.connect "api/#{API_VERSION}/changeset/:id/download", :controller => 'changeset', :action => 'download', :id => /\d+/ map.connect "api/#{API_VERSION}/changeset/:id", :controller => 'changeset', :action => 'read', :id => /\d+/ map.connect "api/#{API_VERSION}/changeset/:id/close", :controller => 'changeset', :action => 'close', :id =>/\d+/ diff --git a/test/functional/changeset_controller_test.rb b/test/functional/changeset_controller_test.rb index 946d139d8..848b6d5f5 100644 --- a/test/functional/changeset_controller_test.rb +++ b/test/functional/changeset_controller_test.rb @@ -381,4 +381,104 @@ EOF "shouldn't be able to upload an element without version: #{@response.body}" end + ## + # when we make some simple changes we get the same changes back from the + # diff download. + def test_diff_download_simple + basic_authorization(users(:normal_user).email, "test") + + # create a temporary changeset + content "" + + "" + + "" + put :create + assert_response :success + changeset_id = @response.body.to_i + + # add a diff to it + diff = < + + + + + + + + + + + +EOF + + # upload it + content diff + post :upload, :id => changeset_id + assert_response :success, + "can't upload multiple versions of an element in a diff: #{@response.body}" + + get :download, :id => changeset_id + assert_response :success + + assert_select "osmChange", 1 + assert_select "osmChange>modify", 8 + assert_select "osmChange>modify>node", 8 + end + + ## + # when we make some complex changes we get the same changes back from the + # diff download. + def test_diff_download_complex + basic_authorization(users(:normal_user).email, "test") + + # create a temporary changeset + content "" + + "" + + "" + put :create + assert_response :success + changeset_id = @response.body.to_i + + # add a diff to it + diff = < + + + + + + + + + + + + + + + + + + +EOF + + # upload it + content diff + post :upload, :id => changeset_id + assert_response :success, + "can't upload multiple versions of an element in a diff: #{@response.body}" + + get :download, :id => changeset_id + assert_response :success + + assert_select "osmChange", 1 + assert_select "osmChange>create", 3 + assert_select "osmChange>delete", 1 + assert_select "osmChange>modify", 2 + assert_select "osmChange>create>node", 3 + assert_select "osmChange>delete>node", 1 + assert_select "osmChange>modify>node", 1 + assert_select "osmChange>modify>way", 1 + end + end -- 2.39.5