1 # The ChangesetController is the RESTful interface to Changeset objects
4 class ChangesetsController < ApiController
7 before_action :check_api_writable, :only => [:create, :update, :upload, :subscribe, :unsubscribe]
8 before_action :check_api_readable, :except => [:create, :update, :upload, :download, :query, :subscribe, :unsubscribe]
9 before_action :authorize, :only => [:create, :update, :upload, :close, :subscribe, :unsubscribe]
13 before_action :require_public_data, :only => [:create, :update, :upload, :close, :subscribe, :unsubscribe]
14 before_action :set_request_formats, :except => [:create, :close, :upload]
16 around_action :api_call_handle_error
17 around_action :api_call_timeout, :except => [:upload]
19 # Helper methods for checking consistency
20 include ConsistencyValidations
22 DEFAULT_QUERY_LIMIT = 100
26 # Return XML giving the basic info about the changeset. Does not
27 # return anything about the nodes, ways and relations in the changeset.
29 @changeset = Changeset.find(params[:id])
30 @include_discussion = params[:include_discussion].presence
33 respond_to do |format|
39 # Create a changeset from XML.
43 cs = Changeset.from_xml(request.raw_post, :create => true)
45 # Assume that Changeset.from_xml has thrown an exception if there is an error parsing the xml
46 cs.user = current_user
49 # Subscribe user to changeset comments
50 cs.subscribers << current_user
52 render :plain => cs.id.to_s
56 # marks a changeset as closed. this may be called multiple times
57 # on the same changeset, so is idempotent.
61 changeset = Changeset.find(params[:id])
62 check_changeset_consistency(changeset, current_user)
64 # to close the changeset, we'll just set its closed_at time to
65 # now. this might not be enough if there are concurrency issues,
66 # but we'll have to wait and see.
67 changeset.set_closed_time_now
74 # Upload a diff in a single transaction.
76 # This means that each change within the diff must succeed, i.e: that
77 # each version number mentioned is still current. Otherwise the entire
78 # transaction *must* be rolled back.
80 # Furthermore, each element in the diff can only reference the current
83 # Returns: a diffResult document, as described in
84 # http://wiki.openstreetmap.org/wiki/OSM_Protocol_Version_0.6
86 # only allow POST requests, as the upload method is most definitely
87 # not idempotent, as several uploads with placeholder IDs will have
88 # different side-effects.
89 # see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.2
92 changeset = Changeset.find(params[:id])
93 check_changeset_consistency(changeset, current_user)
95 diff_reader = DiffReader.new(request.raw_post, changeset)
96 Changeset.transaction do
97 result = diff_reader.commit
98 render :xml => result.to_s
103 # download the changeset as an osmChange document.
105 # to make it easier to revert diffs it would be better if the osmChange
106 # format were reversible, i.e: contained both old and new versions of
107 # modified elements. but it doesn't at the moment...
109 # this method cannot order the database changes fully (i.e: timestamp and
110 # version number may be too coarse) so the resulting diff may not apply
111 # to a different database. however since changesets are not atomic this
112 # behaviour cannot be guaranteed anyway and is the result of a design
115 changeset = Changeset.find(params[:id])
117 # get all the elements in the changeset which haven't been redacted
118 # and stick them in a big array.
119 elements = [changeset.old_nodes.unredacted,
120 changeset.old_ways.unredacted,
121 changeset.old_relations.unredacted].flatten
123 # sort the elements by timestamp and version number, as this is the
124 # almost sensible ordering available. this would be much nicer if
125 # global (SVN-style) versioning were used - then that would be
127 elements.sort! do |a, b|
128 if a.timestamp == b.timestamp
129 a.version <=> b.version
131 a.timestamp <=> b.timestamp
135 # generate an output element for each operation. note: we avoid looking
136 # at the history because it is simpler - but it would be more correct to
137 # check these assertions.
142 elements.each do |elt|
144 # first version, so it must be newly-created.
150 # if the element isn't visible then it must have been deleted
155 respond_to do |format|
161 # query changesets by bounding box, time, user or open/closed status.
163 # find any bounding box
164 bbox = BoundingBox.from_bbox_params(params) if params["bbox"]
166 # create the conditions that the user asked for. some or all of
168 changesets = Changeset.all
169 changesets = conditions_bbox(changesets, bbox)
170 changesets = conditions_user(changesets, params["user"], params["display_name"])
171 changesets = conditions_time(changesets, params["time"])
172 changesets = conditions_open(changesets, params["open"])
173 changesets = conditions_closed(changesets, params["closed"])
174 changesets = conditions_ids(changesets, params["changesets"])
176 # sort and limit the changesets
177 changesets = changesets.order("created_at DESC").limit(result_limit)
179 # preload users, tags and comments, and render result
180 @changesets = changesets.preload(:user, :changeset_tags, :comments)
183 respond_to do |format|
190 # updates a changeset's tags. none of the changeset's attributes are
191 # user-modifiable, so they will be ignored.
193 # changesets are not (yet?) versioned, so we don't have to deal with
194 # history tables here. changesets are locked to a single user, however.
196 # after succesful update, returns the XML of the changeset.
198 # request *must* be a PUT.
201 @changeset = Changeset.find(params[:id])
202 new_changeset = Changeset.from_xml(request.raw_post)
204 check_changeset_consistency(@changeset, current_user)
205 @changeset.update_from(new_changeset, current_user)
208 respond_to do |format|
215 # Adds a subscriber to the changeset
217 # Check the arguments are sane
218 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
220 # Extract the arguments
221 id = params[:id].to_i
223 # Find the changeset and check it is valid
224 changeset = Changeset.find(id)
225 raise OSM::APIChangesetAlreadySubscribedError, changeset if changeset.subscribers.exists?(current_user.id)
228 changeset.subscribers << current_user
230 # Return a copy of the updated changeset
231 @changeset = changeset
234 respond_to do |format|
241 # Removes a subscriber from the changeset
243 # Check the arguments are sane
244 raise OSM::APIBadUserInput, "No id was given" unless params[:id]
246 # Extract the arguments
247 id = params[:id].to_i
249 # Find the changeset and check it is valid
250 changeset = Changeset.find(id)
251 raise OSM::APIChangesetNotSubscribedError, changeset unless changeset.subscribers.exists?(current_user.id)
253 # Remove the subscriber
254 changeset.subscribers.delete(current_user)
256 # Return a copy of the updated changeset
257 @changeset = changeset
260 respond_to do |format|
268 #------------------------------------------------------------
269 # utility functions below.
270 #------------------------------------------------------------
273 # if a bounding box was specified do some sanity checks.
274 # restrict changesets to those enclosed by a bounding box
275 # we need to return both the changesets and the bounding box
276 def conditions_bbox(changesets, bbox)
278 bbox.check_boundaries
279 bbox = bbox.to_scaled
281 changesets.where("min_lon < ? and max_lon > ? and min_lat < ? and max_lat > ?",
282 bbox.max_lon.to_i, bbox.min_lon.to_i,
283 bbox.max_lat.to_i, bbox.min_lat.to_i)
290 # restrict changesets to those by a particular user
291 def conditions_user(changesets, user, name)
292 if user.nil? && name.nil?
295 # shouldn't provide both name and UID
296 raise OSM::APIBadUserInput, "provide either the user ID or display name, but not both" if user && name
298 # use either the name or the UID to find the user which we're selecting on.
300 # user input checking, we don't have any UIDs < 1
301 raise OSM::APIBadUserInput, "invalid user ID" if user.to_i < 1
303 u = User.find(user.to_i)
305 u = User.find_by(:display_name => name)
308 # make sure we found a user
309 raise OSM::APINotFoundError if u.nil?
311 # should be able to get changesets of public users only, or
312 # our own changesets regardless of public-ness.
313 unless u.data_public?
314 # get optional user auth stuff so that users can see their own
315 # changesets if they're non-public
318 raise OSM::APINotFoundError if current_user.nil? || current_user != u
321 changesets.where(:user_id => u.id)
326 # restrict changes to those closed during a particular time period
327 def conditions_time(changesets, time)
330 elsif time.count(",") == 1
331 # if there is a range, i.e: comma separated, then the first is
332 # low, second is high - same as with bounding boxes.
334 # check that we actually have 2 elements in the array
335 times = time.split(",")
336 raise OSM::APIBadUserInput, "bad time range" if times.size != 2
338 from, to = times.collect { |t| Time.parse(t).utc }
339 changesets.where("closed_at >= ? and created_at <= ?", from, to)
341 # if there is no comma, assume its a lower limit on time
342 changesets.where("closed_at >= ?", Time.parse(time).utc)
344 # stupid Time seems to throw both of these for bad parsing, so
345 # we have to catch both and ensure the correct code path is taken.
346 rescue ArgumentError, RuntimeError => e
347 raise OSM::APIBadUserInput, e.message.to_s
351 # return changesets which are open (haven't been closed yet)
352 # we do this by seeing if the 'closed at' time is in the future. Also if we've
353 # hit the maximum number of changes then it counts as no longer open.
354 # if parameter 'open' is nill then open and closed changesets are returned
355 def conditions_open(changesets, open)
359 changesets.where("closed_at >= ? and num_changes <= ?",
360 Time.now.utc, Changeset::MAX_ELEMENTS)
365 # query changesets which are closed
366 # ('closed at' time has passed or changes limit is hit)
367 def conditions_closed(changesets, closed)
371 changesets.where("closed_at < ? or num_changes > ?",
372 Time.now.utc, Changeset::MAX_ELEMENTS)
377 # query changesets by a list of ids
378 # (either specified as array or comma-separated string)
379 def conditions_ids(changesets, ids)
383 raise OSM::APIBadUserInput, "No changesets were given to search for"
385 ids = ids.split(",").collect(&:to_i)
386 changesets.where(:id => ids)
391 # Get the maximum number of results to return
394 if params[:limit].to_i.positive? && params[:limit].to_i <= MAX_QUERY_LIMIT
397 raise OSM::APIBadUserInput, "Changeset limit must be between 1 and #{MAX_QUERY_LIMIT}"