]> git.openstreetmap.org Git - rails.git/blob - app/controllers/changeset_controller.rb
5e538c721c09527737d9578b3b33feea5990ab23
[rails.git] / app / controllers / changeset_controller.rb
1 # The ChangesetController is the RESTful interface to Changeset objects
2
3 class ChangesetController < ApplicationController
4   require 'xml/libxml'
5
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
10
11   # Help methods for checking boundary sanity and area size
12   include MapBoundary
13
14   # Create a changeset from XML.
15   def create
16     if request.put?
17       cs = Changeset.from_xml(request.raw_post, true)
18
19       if cs
20         cs.user_id = @user.id
21         cs.save_with_tags!
22         render :text => cs.id.to_s, :content_type => "text/plain"
23       else
24         render :nothing => true, :status => :bad_request
25       end
26     else
27       render :nothing => true, :status => :method_not_allowed
28     end
29   end
30
31   def read
32     begin
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
37     end
38   end
39   
40   ##
41   # marks a changeset as closed. this may be called multiple times
42   # on the same changeset, so is idempotent.
43   def close 
44     unless request.put?
45       render :nothing => true, :status => :method_not_allowed
46       return
47     end
48     
49     changeset = Changeset.find(params[:id])
50     
51     unless @user.id == changeset.user_id 
52       raise OSM::APIUserChangesetMismatchError 
53     end
54     
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
59
60     changeset.save!
61     render :nothing => true
62   rescue ActiveRecord::RecordNotFound
63     render :nothing => true, :status => :not_found
64   rescue OSM::APIError => ex
65     render ex.render_opts
66   end
67
68   ##
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.
73   def include
74     # only allow POST requests, because although this method is
75     # idempotent, there is no "document" to PUT really...
76     if request.post?
77       cs = Changeset.find(params[:id])
78
79       # check user credentials - only the user who opened a changeset
80       # may alter it.
81       unless @user.id == cs.user_id 
82         raise OSM::APIUserChangesetMismatchError 
83       end
84
85       # keep an array of lons and lats
86       lon = Array.new
87       lat = Array.new
88
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
95       end
96
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?
102
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
106
107       # save the larger bounding box and return the changeset, which
108       # will include the bigger bounding box.
109       cs.save!
110       render :text => cs.to_xml.to_s, :content_type => "text/xml"
111
112     else
113       render :nothing => true, :status => :method_not_allowed
114     end
115
116   rescue ActiveRecord::RecordNotFound
117     render :nothing => true, :status => :not_found
118   rescue OSM::APIError => ex
119     render ex.render_opts
120   end
121
122   ##
123   # Upload a diff in a single transaction.
124   #
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.
128   #
129   # Furthermore, each element in the diff can only reference the current
130   # changeset.
131   #
132   # Returns: a diffResult document, as described in 
133   # http://wiki.openstreetmap.org/index.php/OSM_Protocol_Version_0.6
134   def upload
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
139     unless request.post?
140       render :nothing => true, :status => :method_not_allowed
141       return
142     end
143
144     changeset = Changeset.find(params[:id])
145
146     # access control - only the user who created a changeset may
147     # upload to it.
148     unless @user.id == changeset.user_id 
149       raise OSM::APIUserChangesetMismatchError 
150     end
151     
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"
156     end
157     
158   rescue ActiveRecord::RecordNotFound
159     render :nothing => true, :status => :not_found
160   rescue OSM::APIError => ex
161     render ex.render_opts
162   end
163
164   ##
165   # download the changeset as an osmChange document.
166   #
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...
170   #
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
175   # choice.
176   def download
177     changeset = Changeset.find(params[:id])
178     
179     # get all the elements in the changeset and stick them in a big array.
180     elements = [changeset.old_nodes, 
181                 changeset.old_ways, 
182                 changeset.old_relations].flatten
183     
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 
187     # unambiguous.
188     elements.sort! do |a, b| 
189       if (a.timestamp == b.timestamp)
190         a.version <=> b.version
191       else
192         a.timestamp <=> b.timestamp 
193       end
194     end
195     
196     # create an osmChange document for the output
197     result = OSM::API.new.get_xml_doc
198     result.root.name = "osmChange"
199
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|
204       result.root <<
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
209         else
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])
214           unless elt.visible
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
219           else
220             # must be a modify, for which we don't need the previous version
221             # yet...
222             modified = XML::Node.new "modify"
223             modified << elt.to_xml_node
224           end
225         end
226     end
227
228     render :text => result.to_s, :content_type => "text/xml"
229             
230   rescue ActiveRecord::RecordNotFound
231     render :nothing => true, :status => :not_found
232   rescue OSM::APIError => ex
233     render ex.render_opts
234   end
235
236   ##
237   # query changesets by bounding box, time, user or open/closed status.
238   def query
239     # create the conditions that the user asked for. some or all of
240     # these may be nil.
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'])
245
246     # create the results document
247     results = OSM::API.new.get_xml_doc
248
249     # add all matching changesets to the XML results document
250     Changeset.find(:all, 
251                    :conditions => conditions, 
252                    :limit => 100,
253                    :order => 'created_at desc').each do |cs|
254       results.root << cs.to_xml_node
255     end
256
257     render :text => results.to_s, :content_type => "text/xml"
258
259   rescue ActiveRecord::RecordNotFound
260     render :nothing => true, :status => :not_found
261   rescue OSM::APIError => ex
262     render ex.render_opts
263   end
264   
265   ##
266   # updates a changeset's tags. none of the changeset's attributes are
267   # user-modifiable, so they will be ignored.
268   #
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.
271   #
272   # after succesful update, returns the XML of the changeset.
273   def update
274     # request *must* be a PUT.
275     unless request.put?
276       render :nothing => true, :status => :method_not_allowed
277       return
278     end
279     
280     changeset = Changeset.find(params[:id])
281     new_changeset = Changeset.from_xml(request.raw_post)
282
283     unless new_changeset.nil?
284       changeset.update_from(new_changeset, @user)
285       render :text => changeset.to_xml, :mime_type => "text/xml"
286     else
287       
288       render :nothing => true, :status => :bad_request
289     end
290       
291   rescue ActiveRecord::RecordNotFound
292     render :nothing => true, :status => :not_found
293   rescue OSM::APIError => ex
294     render ex.render_opts
295   end
296
297   #------------------------------------------------------------
298   # utility functions below.
299   #------------------------------------------------------------  
300
301   ##
302   # merge two conditions
303   def cond_merge(a, b)
304     if a and b
305       a_str = a.shift
306       b_str = b.shift
307       return [ a_str + " and " + b_str ] + a + b
308     elsif a 
309       return a
310     else b
311       return b
312     end
313   end
314
315   ##
316   # if a bounding box was specified then parse it and do some sanity 
317   # checks. this is mostly the same as the map call, but without the 
318   # area restriction.
319   def conditions_bbox(bbox)
320     unless bbox.nil?
321       raise OSM::APIBadUserInput.new("Bounding box should be min_lon,min_lat,max_lon,max_lat") unless bbox.count(',') == 3
322       bbox = sanitise_boundaries(bbox.split(/,/))
323       raise OSM::APIBadUserInput.new("Minimum longitude should be less than maximum.") unless bbox[0] <= bbox[2]
324       raise OSM::APIBadUserInput.new("Minimum latitude should be less than maximum.") unless bbox[1] <= bbox[3]
325       return ['min_lon < ? and max_lon > ? and min_lat < ? and max_lat > ?',
326               bbox[2] * GeoRecord::SCALE, bbox[0] * GeoRecord::SCALE, bbox[3]* GeoRecord::SCALE, bbox[1] * GeoRecord::SCALE]
327     else
328       return nil
329     end
330   end
331
332   ##
333   # restrict changesets to those by a particular user
334   def conditions_user(user)
335     unless user.nil?
336       # user input checking, we don't have any UIDs < 1
337       raise OSM::APIBadUserInput.new("invalid user ID") if user.to_i < 1
338
339       u = User.find(user.to_i)
340       # should be able to get changesets of public users only, or 
341       # our own changesets regardless of public-ness.
342       unless u.data_public?
343         # get optional user auth stuff so that users can see their own
344         # changesets if they're non-public
345         setup_user_auth
346         
347         raise OSM::APINotFoundError if @user.nil? or @user.id != u.id
348       end
349       return ['user_id = ?', u.id]
350     else
351       return nil
352     end
353   end
354
355   ##
356   # restrict changes to those during a particular time period
357   def conditions_time(time) 
358     unless time.nil?
359       # if there is a range, i.e: comma separated, then the first is 
360       # low, second is high - same as with bounding boxes.
361       if time.count(',') == 1
362         # check that we actually have 2 elements in the array
363         times = time.split(/,/)
364         raise OSM::APIBadUserInput.new("bad time range") if times.size != 2 
365
366         from, to = times.collect { |t| DateTime.parse(t) }
367         return ['closed_at >= ? and created_at <= ?', from, to]
368       else
369         # if there is no comma, assume its a lower limit on time
370         return ['closed_at >= ?', DateTime.parse(time)]
371       end
372     else
373       return nil
374     end
375     # stupid DateTime seems to throw both of these for bad parsing, so
376     # we have to catch both and ensure the correct code path is taken.
377   rescue ArgumentError => ex
378     raise OSM::APIBadUserInput.new(ex.message.to_s)
379   rescue RuntimeError => ex
380     raise OSM::APIBadUserInput.new(ex.message.to_s)
381   end
382
383   ##
384   # restrict changes to those which are open
385   def conditions_open(open)
386     return open.nil? ? nil : ['closed_at >= ?', DateTime.now]
387   end
388
389 end