1 # The ChangesetController is the RESTful interface to Changeset objects
3 class ChangesetController < ApplicationController
6 before_filter :authorize, :only => [:create, :update, :delete, :upload]
7 before_filter :check_write_availability, :only => [:create, :update, :delete, :upload]
8 before_filter :check_read_availability, :except => [:create, :update, :delete, :upload]
9 after_filter :compress_output
11 # Create a changeset from XML.
14 cs = Changeset.from_xml(request.raw_post, true)
19 render :text => cs.id.to_s, :content_type => "text/plain"
21 render :nothing => true, :status => :bad_request
24 render :nothing => true, :status => :method_not_allowed
28 def create_prim(ids, prim, nd)
30 prim.user_id = @user.id
32 prim.save_with_history!
34 ids[nd['id'].to_i] = prim.id
37 def fix_way(w, node_ids)
38 w.nds = w.instance_eval { @nds }.
39 map { |nd| node_ids[nd] || nd }
44 r.members = r.instance_eval { @members }.
45 map { |memb| [memb[0], ids[memb[0]][memb[1].to_i] || memb[1], memb[2]] }
51 changeset = Changeset.find(params[:id])
52 render :text => changeset.to_xml.to_s, :content_type => "text/xml"
53 rescue ActiveRecord::RecordNotFound
54 render :nothing => true, :status => :not_found
61 render :nothing => true, :status => :method_not_allowed
64 changeset = Changeset.find(params[:id])
65 changeset.open = false
67 render :nothing => true
68 rescue ActiveRecord::RecordNotFound
69 render :nothing => true, :status => :not_found
75 render :nothing => true, :status => :method_not_allowed
79 p = XML::Reader.new request.raw_post
81 node_ids, way_ids, rel_ids = {}, {}, {}
82 ids = {"node"=>node_ids, "way"=>way_ids, "relation"=>rel_ids}
84 models = {"node"=>Node, "way"=>Way, "relation"=>Relation}
86 # FIXME shouldn't this be done through the
87 # res = OSM::API.new.get_xml_doc
88 # as everything else is?
89 res = XML::Document.new
90 res.encoding = 'UTF-8'
91 root = XML::Node.new 'osm'
92 root['version'] = API_VERSION
93 root['creator'] = 'OpenStreetMap.org'
96 root << XML::Node.new_comment(" Warning: this is a 0.6 result document, " +
97 "not a normal OSM file. ")
99 Changeset.transaction do
101 break if p.node_type == 15 # end element
102 next unless p.node_type == 1 # element
107 break if p.node_type == 15 # end element
108 next unless p.node_type == 1 # element
110 model = models[p.name]
113 elem = XML::Node.new p.name
114 nd = p.expand; p.next
115 osm = model.from_xml_node(nd, true)
116 elem['old_id'] = nd['id']
120 fix_way(osm, node_ids)
121 raise OSM::APIPreconditionFailedError.new if !osm.preconditions_ok?
124 raise OSM::APIPreconditionFailedError.new if !osm.preconditions_ok?
127 create_prim ids[nd.name], osm, nd
128 elem['new_id'] = osm.id.to_s
129 elem['new_version'] = osm.version.to_s
134 break if p.node_type == 15 # end element
135 next unless p.node_type == 1 # element
137 model = models[p.name]
140 elem = XML::Node.new p.name
141 new_osm = model.from_xml_node(p.expand); p.next
142 osm = model.find(new_osm.id)
143 osm.update_from new_osm, @user
144 elem['old_id'] = elem['new_id'] = osm.id.to_s
145 elem['new_version'] = osm.version.to_s
150 break if p.node_type == 15 # end element
151 next unless p.node_type == 1 # element
153 model = models[p.name]
156 elem = XML::Node.new p.name
157 osm = model.find(p.expand['id']); p.next
158 osm.delete_with_history(@user)
159 elem['old_id'] = elem['new_id'] = osm.id.to_s
160 elem['new_version'] = osm.version.to_s
167 render :text => res.to_s, :content_type => "text/xml"
169 rescue OSM::APIError => ex
170 render ex.render_opts