class ChangesetController < ApplicationController
require 'xml/libxml'
+ require 'diff_reader'
before_filter :authorize, :only => [:create, :update, :delete, :upload]
before_filter :check_write_availability, :only => [:create, :update, :delete, :upload]
end
end
+ ##
+ # Upload a diff in a single transaction.
+ #
+ # This means that each change within the diff must succeed, i.e: that
+ # each version number mentioned is still current. Otherwise the entire
+ # transaction *must* be rolled back.
+ #
+ # Furthermore, each element in the diff can only reference the current
+ # changeset.
+ #
+ # Returns: ??? the new document? updated diffs?
def upload
- unless request.put?
+ # only allow POST requests, as the upload method is most definitely
+ # not idempotent, as several uploads with placeholder IDs will have
+ # different side-effects.
+ # see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.2
+ unless request.post?
render :nothing => true, :status => :method_not_allowed
return
end
- p = XML::Reader.new request.raw_post
-
- node_ids, way_ids, rel_ids = {}, {}, {}
- ids = {"node"=>node_ids, "way"=>way_ids, "relation"=>rel_ids}
-
- models = {"node"=>Node, "way"=>Way, "relation"=>Relation}
-
- # FIXME shouldn't this be done through the
- # res = OSM::API.new.get_xml_doc
- # as everything else is?
- res = XML::Document.new
- res.encoding = 'UTF-8'
- root = XML::Node.new 'osm'
- root['version'] = API_VERSION
- root['creator'] = 'OpenStreetMap.org'
- res.root = root
-
- root << XML::Node.new_comment(" Warning: this is a 0.6 result document, " +
- "not a normal OSM file. ")
-
+ changeset = Changeset.find(params[:id])
+
+ diff_reader = DiffReader.new(request.raw_post, changeset)
Changeset.transaction do
- while p.read == 1
- break if p.node_type == 15 # end element
- next unless p.node_type == 1 # element
-
- case p.name
- when 'create':
- while p.read == 1
- break if p.node_type == 15 # end element
- next unless p.node_type == 1 # element
-
- model = models[p.name]
- next if model.nil?
-
- elem = XML::Node.new p.name
- nd = p.expand; p.next
- osm = model.from_xml_node(nd, true)
- elem['old_id'] = nd['id']
-
- case nd.name
- when 'way':
- fix_way(osm, node_ids)
- raise OSM::APIPreconditionFailedError.new if !osm.preconditions_ok?
- when 'relation':
- fix_rel(osm, ids)
- raise OSM::APIPreconditionFailedError.new if !osm.preconditions_ok?
- end
-
- create_prim ids[nd.name], osm, nd
- elem['new_id'] = osm.id.to_s
- elem['new_version'] = osm.version.to_s
- root << elem
- end
- when 'modify':
- while p.read == 1
- break if p.node_type == 15 # end element
- next unless p.node_type == 1 # element
-
- model = models[p.name]
- next if model.nil?
-
- elem = XML::Node.new p.name
- new_osm = model.from_xml_node(p.expand); p.next
- osm = model.find(new_osm.id)
- osm.update_from new_osm, @user
- elem['old_id'] = elem['new_id'] = osm.id.to_s
- elem['new_version'] = osm.version.to_s
- root << elem
- end
- when 'delete':
- while p.read == 1
- break if p.node_type == 15 # end element
- next unless p.node_type == 1 # element
-
- model = models[p.name]
- next if model.nil?
-
- elem = XML::Node.new p.name
- osm = model.find(p.expand['id']); p.next
- osm.delete_with_history(@user)
- elem['old_id'] = elem['new_id'] = osm.id.to_s
- elem['new_version'] = osm.version.to_s
- root << elem
- end
- end
- end
+ result = diff_reader.commit
+ render :text => result.to_s, :content_type => "text/xml"
end
-
- render :text => res.to_s, :content_type => "text/xml"
-
+
+ rescue ActiveRecord::RecordNotFound
+ render :nothing => true, :status => :not_found
rescue OSM::APIError => ex
render ex.render_opts
end