map { |memb| [memb[0], ids[memb[0]][memb[1].to_i] || memb[1], memb[2]] }
return r
end
+
+ def read
+ begin
+ changeset = Changeset.find(params[:id])
+ render :text => changeset.to_xml.to_s, :content_type => "text/xml"
+ rescue ActiveRecord::RecordNotFound
+ render :nothing => true, :status => :not_found
+ end
+ end
+
+ def close
+ begin
+ unless request.put?
+ render :nothing => true, :status => :method_not_allowed
+ return
+ end
+ changeset = Changeset.find(params[:id])
+ changeset.open = false
+ changeset.save!
+ render :nothing => true
+ rescue ActiveRecord::RecordNotFound
+ render :nothing => true, :status => :not_found
+ end
+ end
def upload
- if not request.put?
+ unless request.put?
render :nothing => true, :status => :method_not_allowed
return
end
- # FIXME: this should really be done without loading the whole XML file
- # into memory.
- p = XML::Parser.new
- p.string = request.raw_post
- doc = p.parse
+ 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.transaction do
- doc.find('//osm/create/node').each do |nd|
- create_prim node_ids, Node.from_xml_node(nd, true), nd
- end
- doc.find('//osm/create/way').each do |nd|
- way = Way.from_xml_node(nd, true)
- fix_way(way, node_ids)
- raise OSM::APIPreconditionFailedError.new if !way.preconditions_ok?
- create_prim way_ids, way, nd
- end
- doc.find('//osm/create/relation').each do |nd|
- relation = Relation.from_xml_node(nd, true)
- fix_rel(relation, ids)
- raise OSM::APIPreconditionFailedError.new if !relation.preconditions_ok?
- create_prim rel_ids, relation, nd
- end
+ while p.read == 1
+ break if p.node_type == 15 # end element
+ next unless p.node_type == 1 # element
- doc.find('//osm/modify/relation').each do |nd|
- new_relation = Relation.from_xml_node(nd)
- Relation.find(new_relation.id).update_from new_relation, @user
- end
- doc.find('//osm/modify/way').each do |nd|
- new_way = Way.from_xml_node(nd)
- Way.find(new_way.id).update_from new_way, @user
- end
- doc.find('//osm/modify/node').each do |nd|
- new_node = Node.from_xml_node(nd)
- Node.find(new_node.id).update_from new_node, @user
- end
+ case p.name
+ when 'create':
+ while p.read == 1
+ break if p.node_type == 15 # end element
+ next unless p.node_type == 1 # element
- doc.find('//osm/delete/relation').each do |nd|
- Relation.find(nd['id']).delete_with_history(@user)
- end
- doc.find('//osm/delete/way').each do |nd|
- Way.find(nd['id']).delete_with_relations_and_history(@user)
- end
- doc.find('//osm/delete/node').each do |nd|
- Node.find(nd['id']).delete_with_history(@user)
+ 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
end
- render :text => "Ok, Fine. Upload worked without errors.\n", :status => 200
+ render :text => res.to_s, :content_type => "text/xml"
+
+ rescue OSM::APIError => ex
+ render ex.render_opts
end
end