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 # FIXME: this should really be done without loading the whole XML file
82 p.string = request.raw_post
85 node_ids, way_ids, rel_ids = {}, {}, {}
86 ids = {"node"=>node_ids, "way"=>way_ids, "relation"=>rel_ids}
88 res = XML::Document.new
89 res.encoding = 'UTF-8'
90 root = XML::Node.new 'osm'
91 root['version'] = '0.6'
92 root['creator'] = 'OpenStreetMap.org'
95 root << XML::Node.new_comment(" Warning: this is a 0.6 result document, " +
96 "not a normal OSM file. ")
98 Changeset.transaction do
99 doc.find('//osm/create/node').each do |nd|
100 elem = XML::Node.new 'node'
101 node = Node.from_xml_node(nd, true)
102 elem['old_id'] = nd['id']
103 create_prim node_ids, node, nd
104 elem['new_id'] = node.id.to_s
105 elem['new_version'] = node.version.to_s
108 doc.find('//osm/create/way').each do |nd|
109 elem = XML::Node.new 'way'
110 way = Way.from_xml_node(nd, true)
111 elem['old_id'] = nd['id']
112 fix_way(way, node_ids)
113 raise OSM::APIPreconditionFailedError.new if !way.preconditions_ok?
114 create_prim way_ids, way, nd
115 elem['new_id'] = way.id.to_s
116 elem['new_version'] = way.version.to_s
119 doc.find('//osm/create/relation').each do |nd|
120 elem = XML::Node.new 'relation'
121 relation = Relation.from_xml_node(nd, true)
122 elem['old_id'] = nd['id']
123 fix_rel(relation, ids)
124 raise OSM::APIPreconditionFailedError.new if !relation.preconditions_ok?
125 create_prim rel_ids, relation, nd
126 elem['new_id'] = relation.id.to_s
127 elem['new_version'] = relation.version.to_s
131 doc.find('//osm/modify/relation').each do |nd|
132 elem = XML::Node.new 'relation'
133 new_relation = Relation.from_xml_node(nd)
134 relation = Relation.find(new_relation.id)
135 relation.update_from new_relation, @user
136 elem['old_id'] = elem['new_id'] = relation.id.to_s
137 elem['new_version'] = relation.version.to_s
140 doc.find('//osm/modify/way').each do |nd|
141 elem = XML::Node.new 'way'
142 new_way = Way.from_xml_node(nd)
143 way = Way.find(new_way.id)
144 way.update_from new_way, @user
145 elem['old_id'] = elem['new_id'] = way.id.to_s
146 elem['new_version'] = way.version.to_s
149 doc.find('//osm/modify/node').each do |nd|
150 elem = XML::Node.new 'node'
151 new_node = Node.from_xml_node(nd)
152 node = Node.find(new_node.id)
153 node.update_from new_node, @user
154 elem['old_id'] = elem['new_id'] = node.id.to_s
155 elem['new_version'] = node.version.to_s
159 doc.find('//osm/delete/relation').each do |nd|
160 elem = XML::Node.new 'relation'
161 relation = Relation.find(nd['id'])
162 relation.delete_with_history(@user)
163 elem['old_id'] = elem['new_id'] = relation.id.to_s
164 elem['new_version'] = relation.version.to_s
167 doc.find('//osm/delete/way').each do |nd|
168 elem = XML::Node.new 'way'
169 way = Way.find(nd['id'])
170 way.delete_with_history(@user)
171 elem['old_id'] = elem['new_id'] = way.id.to_s
172 elem['new_version'] = way.version.to_s
175 doc.find('//osm/delete/node').each do |nd|
176 elem = XML::Node.new 'node'
177 new_node = Node.from_xml_node(nd)
178 node = Node.find(nd['id'])
179 node.delete_with_history(@user)
180 elem['old_id'] = elem['new_id'] = node.id.to_s
181 elem['new_version'] = node.version.to_s
186 render :text => res.to_s, :content_type => "text/xml"