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 res = XML::Document.new
87 res.encoding = 'UTF-8'
88 root = XML::Node.new 'osm'
89 root['version'] = '0.6'
90 root['creator'] = 'OpenStreetMap.org'
93 root << XML::Node.new_comment(" Warning: this is a 0.6 result document, " +
94 "not a normal OSM file. ")
96 Changeset.transaction do
98 break if p.node_type == 15 # end element
99 next unless p.node_type == 1 # element
104 break if p.node_type == 15 # end element
105 next unless p.node_type == 1 # element
107 model = models[p.name]
110 elem = XML::Node.new p.name
111 nd = p.expand; p.next
112 osm = model.from_xml_node(nd, true)
113 elem['old_id'] = nd['id']
117 fix_way(osm, node_ids)
118 raise OSM::APIPreconditionFailedError.new if !osm.preconditions_ok?
121 raise OSM::APIPreconditionFailedError.new if !osm.preconditions_ok?
124 create_prim ids[nd.name], osm, nd
125 elem['new_id'] = osm.id.to_s
126 elem['new_version'] = osm.version.to_s
131 break if p.node_type == 15 # end element
132 next unless p.node_type == 1 # element
134 model = models[p.name]
137 elem = XML::Node.new p.name
138 new_osm = model.from_xml_node(p.expand); p.next
139 osm = model.find(new_osm.id)
140 osm.update_from new_osm, @user
141 elem['old_id'] = elem['new_id'] = osm.id.to_s
142 elem['new_version'] = osm.version.to_s
147 break if p.node_type == 15 # end element
148 next unless p.node_type == 1 # element
150 model = models[p.name]
153 elem = XML::Node.new p.name
154 osm = model.find(p.expand['id']); p.next
155 osm.delete_with_history(@user)
156 elem['old_id'] = elem['new_id'] = osm.id.to_s
157 elem['new_version'] = osm.version.to_s
164 render :text => res.to_s, :content_type => "text/xml"
166 rescue OSM::APIError => ex
167 render ex.render_opts