1 # The ChangesetController is the RESTful interface to Changeset objects
3 class ChangesetController < ApplicationController
6 before_filter :authorize, :only => [:create, :update, :delete, :upload, :include, :close]
7 before_filter :check_write_availability, :only => [:create, :update, :delete, :upload, :include]
8 before_filter :check_read_availability, :except => [:create, :update, :delete, :upload, :download, :query]
9 after_filter :compress_output
11 # Help methods for checking boundary sanity and area size
14 # Create a changeset from XML.
17 cs = Changeset.from_xml(request.raw_post, true)
22 render :text => cs.id.to_s, :content_type => "text/plain"
24 render :nothing => true, :status => :bad_request
27 render :nothing => true, :status => :method_not_allowed
33 changeset = Changeset.find(params[:id])
34 render :text => changeset.to_xml.to_s, :content_type => "text/xml"
35 rescue ActiveRecord::RecordNotFound
36 render :nothing => true, :status => :not_found
41 # marks a changeset as closed. this may be called multiple times
42 # on the same changeset, so is idempotent.
45 render :nothing => true, :status => :method_not_allowed
49 changeset = Changeset.find(params[:id])
51 unless @user.id == changeset.user_id
52 raise OSM::APIUserChangesetMismatchError
55 # to close the changeset, we'll just set its closed_at time to
56 # now. this might not be enough if there are concurrency issues,
57 # but we'll have to wait and see.
58 changeset.set_closed_time_now
61 render :nothing => true
62 rescue ActiveRecord::RecordNotFound
63 render :nothing => true, :status => :not_found
64 rescue OSM::APIError => ex
69 # insert a (set of) points into a changeset bounding box. this can only
70 # increase the size of the bounding box. this is a hint that clients can
71 # set either before uploading a large number of changes, or changes that
72 # the client (but not the server) knows will affect areas further away.
74 # only allow POST requests, because although this method is
75 # idempotent, there is no "document" to PUT really...
77 cs = Changeset.find(params[:id])
79 # check user credentials - only the user who opened a changeset
81 unless @user.id == cs.user_id
82 raise OSM::APIUserChangesetMismatchError
85 # keep an array of lons and lats
89 # the request is in pseudo-osm format... this is kind-of an
90 # abuse, maybe should change to some other format?
91 doc = XML::Parser.string(request.raw_post).parse
92 doc.find("//osm/node").each do |n|
93 lon << n['lon'].to_f * GeoRecord::SCALE
94 lat << n['lat'].to_f * GeoRecord::SCALE
97 # add the existing bounding box to the lon-lat array
98 lon << cs.min_lon unless cs.min_lon.nil?
99 lat << cs.min_lat unless cs.min_lat.nil?
100 lon << cs.max_lon unless cs.max_lon.nil?
101 lat << cs.max_lat unless cs.max_lat.nil?
103 # collapse the arrays to minimum and maximum
104 cs.min_lon, cs.min_lat, cs.max_lon, cs.max_lat =
105 lon.min, lat.min, lon.max, lat.max
107 # save the larger bounding box and return the changeset, which
108 # will include the bigger bounding box.
110 render :text => cs.to_xml.to_s, :content_type => "text/xml"
113 render :nothing => true, :status => :method_not_allowed
116 rescue ActiveRecord::RecordNotFound
117 render :nothing => true, :status => :not_found
118 rescue OSM::APIError => ex
119 render ex.render_opts
123 # Upload a diff in a single transaction.
125 # This means that each change within the diff must succeed, i.e: that
126 # each version number mentioned is still current. Otherwise the entire
127 # transaction *must* be rolled back.
129 # Furthermore, each element in the diff can only reference the current
132 # Returns: a diffResult document, as described in
133 # http://wiki.openstreetmap.org/index.php/OSM_Protocol_Version_0.6
135 # only allow POST requests, as the upload method is most definitely
136 # not idempotent, as several uploads with placeholder IDs will have
137 # different side-effects.
138 # see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.2
140 render :nothing => true, :status => :method_not_allowed
144 changeset = Changeset.find(params[:id])
146 # access control - only the user who created a changeset may
148 unless @user.id == changeset.user_id
149 raise OSM::APIUserChangesetMismatchError
152 diff_reader = DiffReader.new(request.raw_post, changeset)
153 Changeset.transaction do
154 result = diff_reader.commit
155 render :text => result.to_s, :content_type => "text/xml"
158 rescue ActiveRecord::RecordNotFound
159 render :nothing => true, :status => :not_found
160 rescue OSM::APIError => ex
161 render ex.render_opts
165 # download the changeset as an osmChange document.
167 # to make it easier to revert diffs it would be better if the osmChange
168 # format were reversible, i.e: contained both old and new versions of
169 # modified elements. but it doesn't at the moment...
171 # this method cannot order the database changes fully (i.e: timestamp and
172 # version number may be too coarse) so the resulting diff may not apply
173 # to a different database. however since changesets are not atomic this
174 # behaviour cannot be guaranteed anyway and is the result of a design
177 changeset = Changeset.find(params[:id])
179 # get all the elements in the changeset and stick them in a big array.
180 elements = [changeset.old_nodes,
182 changeset.old_relations].flatten
184 # sort the elements by timestamp and version number, as this is the
185 # almost sensible ordering available. this would be much nicer if
186 # global (SVN-style) versioning were used - then that would be
188 elements.sort! do |a, b|
189 if (a.timestamp == b.timestamp)
190 a.version <=> b.version
192 a.timestamp <=> b.timestamp
196 # create an osmChange document for the output
197 result = OSM::API.new.get_xml_doc
198 result.root.name = "osmChange"
200 # generate an output element for each operation. note: we avoid looking
201 # at the history because it is simpler - but it would be more correct to
202 # check these assertions.
203 elements.each do |elt|
205 if (elt.version == 1)
206 # first version, so it must be newly-created.
207 created = XML::Node.new "create"
208 created << elt.to_xml_node
210 # get the previous version from the element history
211 prev_elt = elt.class.find(:first, :conditions =>
212 ['id = ? and version = ?',
213 elt.id, elt.version])
215 # if the element isn't visible then it must have been deleted, so
216 # output the *previous* XML
217 deleted = XML::Node.new "delete"
218 deleted << prev_elt.to_xml_node
220 # must be a modify, for which we don't need the previous version
222 modified = XML::Node.new "modify"
223 modified << elt.to_xml_node
228 render :text => result.to_s, :content_type => "text/xml"
230 rescue ActiveRecord::RecordNotFound
231 render :nothing => true, :status => :not_found
232 rescue OSM::APIError => ex
233 render ex.render_opts
237 # query changesets by bounding box, time, user or open/closed status.
239 # create the conditions that the user asked for. some or all of
241 conditions = conditions_bbox(params['bbox'])
242 conditions = cond_merge conditions, conditions_user(params['user'])
243 conditions = cond_merge conditions, conditions_time(params['time'])
244 conditions = cond_merge conditions, conditions_open(params['open'])
246 # create the results document
247 results = OSM::API.new.get_xml_doc
249 # add all matching changesets to the XML results document
251 :conditions => conditions,
253 :order => 'created_at desc').each do |cs|
254 results.root << cs.to_xml_node
257 render :text => results.to_s, :content_type => "text/xml"
259 rescue ActiveRecord::RecordNotFound
260 render :nothing => true, :status => :not_found
261 rescue OSM::APIError => ex
262 render ex.render_opts
266 # updates a changeset's tags. none of the changeset's attributes are
267 # user-modifiable, so they will be ignored.
269 # changesets are not (yet?) versioned, so we don't have to deal with
270 # history tables here. changesets are locked to a single user, however.
272 # after succesful update, returns the XML of the changeset.
274 # request *must* be a PUT.
276 render :nothing => true, :status => :method_not_allowed
280 changeset = Changeset.find(params[:id])
281 new_changeset = Changeset.from_xml(request.raw_post)
283 unless new_changeset.nil?
284 changeset.update_from(new_changeset, @user)
285 render :text => changeset.to_xml, :mime_type => "text/xml"
288 render :nothing => true, :status => :bad_request
291 rescue ActiveRecord::RecordNotFound
292 render :nothing => true, :status => :not_found
293 rescue OSM::APIError => ex
294 render ex.render_opts
298 #------------------------------------------------------------
299 # utility functions below.
300 #------------------------------------------------------------
303 # merge two conditions
308 return [ a_str + " and " + b_str ] + a + b
317 # if a bounding box was specified then parse it and do some sanity
318 # checks. this is mostly the same as the map call, but without the
320 def conditions_bbox(bbox)
322 raise OSM::APIBadUserInput.new("Bounding box should be min_lon,min_lat,max_lon,max_lat") unless bbox.count(',') == 3
323 bbox = sanitise_boundaries(bbox.split(/,/))
324 raise OSM::APIBadUserInput.new("Minimum longitude should be less than maximum.") unless bbox[0] <= bbox[2]
325 raise OSM::APIBadUserInput.new("Minimum latitude should be less than maximum.") unless bbox[1] <= bbox[3]
326 return ['min_lon < ? and max_lon > ? and min_lat < ? and max_lat > ?',
327 bbox[2] * GeoRecord::SCALE, bbox[0] * GeoRecord::SCALE, bbox[3]* GeoRecord::SCALE, bbox[1] * GeoRecord::SCALE]
334 # restrict changesets to those by a particular user
335 def conditions_user(user)
337 # user input checking, we don't have any UIDs < 1
338 raise OSM::APIBadUserInput.new("invalid user ID") if user.to_i < 1
340 u = User.find(user.to_i)
341 # should be able to get changesets of public users only, or
342 # our own changesets regardless of public-ness.
343 unless u.data_public?
344 # get optional user auth stuff so that users can see their own
345 # changesets if they're non-public
348 raise OSM::APINotFoundError if @user.nil? or @user.id != u.id
350 return ['user_id = ?', u.id]
357 # restrict changes to those during a particular time period
358 def conditions_time(time)
360 # if there is a range, i.e: comma separated, then the first is
361 # low, second is high - same as with bounding boxes.
362 if time.count(',') == 1
363 # check that we actually have 2 elements in the array
364 times = time.split(/,/)
365 raise OSM::APIBadUserInput.new("bad time range") if times.size != 2
367 from, to = times.collect { |t| DateTime.parse(t) }
368 return ['closed_at >= ? and created_at <= ?', from, to]
370 # if there is no comma, assume its a lower limit on time
371 return ['closed_at >= ?', DateTime.parse(time)]
376 # stupid DateTime seems to throw both of these for bad parsing, so
377 # we have to catch both and ensure the correct code path is taken.
378 rescue ArgumentError => ex
379 raise OSM::APIBadUserInput.new(ex.message.to_s)
380 rescue RuntimeError => ex
381 raise OSM::APIBadUserInput.new(ex.message.to_s)
385 # restrict changes to those which are open
386 def conditions_open(open)
387 return open.nil? ? nil : ['closed_at >= ?', DateTime.now]