]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/changesets_controller.rb
Use multiple columns for map controls if they don't fit vertically
[rails.git] / app / controllers / api / changesets_controller.rb
1 # The ChangesetController is the RESTful interface to Changeset objects
2
3 module Api
4   class ChangesetsController < ApiController
5     require "xml/libxml"
6
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]
10
11     authorize_resource
12
13     before_action :require_public_data, :only => [:create, :update, :upload, :close, :subscribe, :unsubscribe]
14     before_action :set_request_formats, :except => [:create, :close, :upload]
15
16     around_action :api_call_handle_error
17     around_action :api_call_timeout, :except => [:upload]
18
19     # Helper methods for checking consistency
20     include ConsistencyValidations
21
22     DEFAULT_QUERY_LIMIT = 100
23     MAX_QUERY_LIMIT = 100
24
25     ##
26     # Return XML giving the basic info about the changeset. Does not
27     # return anything about the nodes, ways and relations in the changeset.
28     def show
29       @changeset = Changeset.find(params[:id])
30       @include_discussion = params[:include_discussion].presence
31       render "changeset"
32
33       respond_to do |format|
34         format.xml
35         format.json
36       end
37     end
38
39     # Create a changeset from XML.
40     def create
41       assert_method :put
42
43       cs = Changeset.from_xml(request.raw_post, :create => true)
44
45       # Assume that Changeset.from_xml has thrown an exception if there is an error parsing the xml
46       cs.user = current_user
47       cs.save_with_tags!
48
49       # Subscribe user to changeset comments
50       cs.subscribers << current_user
51
52       render :plain => cs.id.to_s
53     end
54
55     ##
56     # marks a changeset as closed. this may be called multiple times
57     # on the same changeset, so is idempotent.
58     def close
59       assert_method :put
60
61       changeset = Changeset.find(params[:id])
62       check_changeset_consistency(changeset, current_user)
63
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
68
69       changeset.save!
70       head :ok
71     end
72
73     ##
74     # Upload a diff in a single transaction.
75     #
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.
79     #
80     # Furthermore, each element in the diff can only reference the current
81     # changeset.
82     #
83     # Returns: a diffResult document, as described in
84     # http://wiki.openstreetmap.org/wiki/OSM_Protocol_Version_0.6
85     def upload
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
90       assert_method :post
91
92       changeset = Changeset.find(params[:id])
93       check_changeset_consistency(changeset, current_user)
94
95       diff_reader = DiffReader.new(request.raw_post, changeset)
96       Changeset.transaction do
97         result = diff_reader.commit
98         render :xml => result.to_s
99       end
100     end
101
102     ##
103     # download the changeset as an osmChange document.
104     #
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...
108     #
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
113     # choice.
114     def download
115       changeset = Changeset.find(params[:id])
116
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
122
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
126       # unambiguous.
127       elements.sort! do |a, b|
128         if a.timestamp == b.timestamp
129           a.version <=> b.version
130         else
131           a.timestamp <=> b.timestamp
132         end
133       end
134
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.
138       @created = []
139       @modified = []
140       @deleted = []
141
142       elements.each do |elt|
143         if elt.version == 1
144           # first version, so it must be newly-created.
145           @created << elt
146         elsif elt.visible
147           # must be a modify
148           @modified << elt
149         else
150           # if the element isn't visible then it must have been deleted
151           @deleted << elt
152         end
153       end
154
155       respond_to do |format|
156         format.xml
157       end
158     end
159
160     ##
161     # query changesets by bounding box, time, user or open/closed status.
162     def query
163       # find any bounding box
164       bbox = BoundingBox.from_bbox_params(params) if params["bbox"]
165
166       # create the conditions that the user asked for. some or all of
167       # these may be nil.
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"])
175
176       # sort and limit the changesets
177       changesets = changesets.order("created_at DESC").limit(result_limit)
178
179       # preload users, tags and comments, and render result
180       @changesets = changesets.preload(:user, :changeset_tags, :comments)
181       render "changesets"
182
183       respond_to do |format|
184         format.xml
185         format.json
186       end
187     end
188
189     ##
190     # updates a changeset's tags. none of the changeset's attributes are
191     # user-modifiable, so they will be ignored.
192     #
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.
195     #
196     # after succesful update, returns the XML of the changeset.
197     def update
198       # request *must* be a PUT.
199       assert_method :put
200
201       @changeset = Changeset.find(params[:id])
202       new_changeset = Changeset.from_xml(request.raw_post)
203
204       check_changeset_consistency(@changeset, current_user)
205       @changeset.update_from(new_changeset, current_user)
206       render "changeset"
207
208       respond_to do |format|
209         format.xml
210         format.json
211       end
212     end
213
214     ##
215     # Adds a subscriber to the changeset
216     def subscribe
217       # Check the arguments are sane
218       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
219
220       # Extract the arguments
221       id = params[:id].to_i
222
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)
226
227       # Add the subscriber
228       changeset.subscribers << current_user
229
230       # Return a copy of the updated changeset
231       @changeset = changeset
232       render "changeset"
233
234       respond_to do |format|
235         format.xml
236         format.json
237       end
238     end
239
240     ##
241     # Removes a subscriber from the changeset
242     def unsubscribe
243       # Check the arguments are sane
244       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
245
246       # Extract the arguments
247       id = params[:id].to_i
248
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)
252
253       # Remove the subscriber
254       changeset.subscribers.delete(current_user)
255
256       # Return a copy of the updated changeset
257       @changeset = changeset
258       render "changeset"
259
260       respond_to do |format|
261         format.xml
262         format.json
263       end
264     end
265
266     private
267
268     #------------------------------------------------------------
269     # utility functions below.
270     #------------------------------------------------------------
271
272     ##
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)
277       if bbox
278         bbox.check_boundaries
279         bbox = bbox.to_scaled
280
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)
284       else
285         changesets
286       end
287     end
288
289     ##
290     # restrict changesets to those by a particular user
291     def conditions_user(changesets, user, name)
292       if user.nil? && name.nil?
293         changesets
294       else
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
297
298         # use either the name or the UID to find the user which we're selecting on.
299         u = if name.nil?
300               # user input checking, we don't have any UIDs < 1
301               raise OSM::APIBadUserInput, "invalid user ID" if user.to_i < 1
302
303               u = User.find(user.to_i)
304             else
305               u = User.find_by(:display_name => name)
306             end
307
308         # make sure we found a user
309         raise OSM::APINotFoundError if u.nil?
310
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
316           setup_user_auth
317
318           raise OSM::APINotFoundError if current_user.nil? || current_user != u
319         end
320
321         changesets.where(:user_id => u.id)
322       end
323     end
324
325     ##
326     # restrict changes to those closed during a particular time period
327     def conditions_time(changesets, time)
328       if time.nil?
329         changesets
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.
333
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
337
338         from, to = times.collect { |t| Time.parse(t).utc }
339         changesets.where("closed_at >= ? and created_at <= ?", from, to)
340       else
341         # if there is no comma, assume its a lower limit on time
342         changesets.where("closed_at >= ?", Time.parse(time).utc)
343       end
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
348     end
349
350     ##
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)
356       if open.nil?
357         changesets
358       else
359         changesets.where("closed_at >= ? and num_changes <= ?",
360                          Time.now.utc, Changeset::MAX_ELEMENTS)
361       end
362     end
363
364     ##
365     # query changesets which are closed
366     # ('closed at' time has passed or changes limit is hit)
367     def conditions_closed(changesets, closed)
368       if closed.nil?
369         changesets
370       else
371         changesets.where("closed_at < ? or num_changes > ?",
372                          Time.now.utc, Changeset::MAX_ELEMENTS)
373       end
374     end
375
376     ##
377     # query changesets by a list of ids
378     # (either specified as array or comma-separated string)
379     def conditions_ids(changesets, ids)
380       if ids.nil?
381         changesets
382       elsif ids.empty?
383         raise OSM::APIBadUserInput, "No changesets were given to search for"
384       else
385         ids = ids.split(",").collect(&:to_i)
386         changesets.where(:id => ids)
387       end
388     end
389
390     ##
391     # Get the maximum number of results to return
392     def result_limit
393       if params[:limit]
394         if params[:limit].to_i.positive? && params[:limit].to_i <= MAX_QUERY_LIMIT
395           params[:limit].to_i
396         else
397           raise OSM::APIBadUserInput, "Changeset limit must be between 1 and #{MAX_QUERY_LIMIT}"
398         end
399       else
400         DEFAULT_QUERY_LIMIT
401       end
402     end
403   end
404 end