]> git.openstreetmap.org Git - rails.git/blob - app/controllers/changeset_controller.rb
c56e15c0186a74d2f66ea28075da65710c4ca0a5
[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   require 'diff_reader'
6
7   before_filter :authorize, :only => [:create, :update, :delete, :upload, :include]
8   before_filter :check_write_availability, :only => [:create, :update, :delete, :upload, :include]
9   before_filter :check_read_availability, :except => [:create, :update, :delete, :upload, :download]
10   after_filter :compress_output
11
12   # Create a changeset from XML.
13   def create
14     if request.put?
15       cs = Changeset.from_xml(request.raw_post, true)
16
17       if cs
18         cs.user_id = @user.id
19         cs.save_with_tags!
20         render :text => cs.id.to_s, :content_type => "text/plain"
21       else
22         render :nothing => true, :status => :bad_request
23       end
24     else
25       render :nothing => true, :status => :method_not_allowed
26     end
27   end
28
29   def read
30     begin
31       changeset = Changeset.find(params[:id])
32       render :text => changeset.to_xml.to_s, :content_type => "text/xml"
33     rescue ActiveRecord::RecordNotFound
34       render :nothing => true, :status => :not_found
35     end
36   end
37   
38   def close 
39     begin
40       unless request.put?
41         render :nothing => true, :status => :method_not_allowed
42         return
43       end
44       changeset = Changeset.find(params[:id])
45       changeset.open = false
46       changeset.save!
47       render :nothing => true
48     rescue ActiveRecord::RecordNotFound
49       render :nothing => true, :status => :not_found
50     end
51   end
52
53   ##
54   # insert a (set of) points into a changeset bounding box. this can only
55   # increase the size of the bounding box. this is a hint that clients can
56   # set either before uploading a large number of changes, or changes that
57   # the client (but not the server) knows will affect areas further away.
58   def include
59     # only allow POST requests, because although this method is
60     # idempotent, there is no "document" to PUT really...
61     if request.post?
62       cs = Changeset.find(params[:id])
63
64       # keep an array of lons and lats
65       lon = Array.new
66       lat = Array.new
67
68       # the request is in pseudo-osm format... this is kind-of an
69       # abuse, maybe should change to some other format?
70       doc = XML::Parser.string(request.raw_post).parse
71       doc.find("//osm/node").each do |n|
72         lon << n['lon'].to_f * SCALE
73         lat << n['lat'].to_f * SCALE
74       end
75
76       # add the existing bounding box to the lon-lat array
77       lon << cs.min_lon unless cs.min_lon.nil?
78       lat << cs.min_lat unless cs.min_lat.nil?
79       lon << cs.max_lon unless cs.max_lon.nil?
80       lat << cs.max_lat unless cs.max_lat.nil?
81
82       # collapse the arrays to minimum and maximum
83       cs.min_lon, cs.min_lat, cs.max_lon, cs.max_lat = 
84         lon.min, lat.min, lon.max, lat.max
85
86       # save the larger bounding box and return the changeset, which
87       # will include the bigger bounding box.
88       cs.save!
89       render :text => cs.to_xml.to_s, :content_type => "text/xml"
90
91     else
92       render :nothing => true, :status => :method_not_allowed
93     end
94
95   rescue ActiveRecord::RecordNotFound
96     render :nothing => true, :status => :not_found
97   rescue OSM::APIError => ex
98     render ex.render_opts
99   end
100
101   ##
102   # Upload a diff in a single transaction.
103   #
104   # This means that each change within the diff must succeed, i.e: that
105   # each version number mentioned is still current. Otherwise the entire
106   # transaction *must* be rolled back.
107   #
108   # Furthermore, each element in the diff can only reference the current
109   # changeset.
110   #
111   # Returns: a diffResult document, as described in 
112   # http://wiki.openstreetmap.org/index.php/OSM_Protocol_Version_0.6
113   def upload
114     # only allow POST requests, as the upload method is most definitely
115     # not idempotent, as several uploads with placeholder IDs will have
116     # different side-effects.
117     # see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.2
118     unless request.post?
119       render :nothing => true, :status => :method_not_allowed
120       return
121     end
122
123     changeset = Changeset.find(params[:id])
124     
125     diff_reader = DiffReader.new(request.raw_post, changeset)
126     Changeset.transaction do
127       result = diff_reader.commit
128       render :text => result.to_s, :content_type => "text/xml"
129     end
130     
131   rescue ActiveRecord::RecordNotFound
132     render :nothing => true, :status => :not_found
133   rescue OSM::APIError => ex
134     render ex.render_opts
135   end
136
137   ##
138   # download the changeset as an osmChange document.
139   #
140   # to make it easier to revert diffs it would be better if the osmChange
141   # format were reversible, i.e: contained both old and new versions of 
142   # modified elements. but it doesn't at the moment...
143   #
144   # this method cannot order the database changes fully (i.e: timestamp and
145   # version number may be too coarse) so the resulting diff may not apply
146   # to a different database. however since changesets are not atomic this 
147   # behaviour cannot be guaranteed anyway and is the result of a design
148   # choice.
149   def download
150     changeset = Changeset.find(params[:id])
151     
152     # get all the elements in the changeset and stick them in a big array.
153     elements = [changeset.old_nodes, 
154                 changeset.old_ways, 
155                 changeset.old_relations].flatten
156     
157     # sort the elements by timestamp and version number, as this is the 
158     # almost sensible ordering available. this would be much nicer if 
159     # global (SVN-style) versioning were used - then that would be 
160     # unambiguous.
161     elements.sort! do |a, b| 
162       if (a.timestamp == b.timestamp)
163         a.version <=> b.version
164       else
165         a.timestamp <=> b.timestamp 
166       end
167     end
168     
169     # create an osmChange document for the output
170     result = OSM::API.new.get_xml_doc
171     result.root.name = "osmChange"
172
173     # generate an output element for each operation. note: we avoid looking
174     # at the history because it is simpler - but it would be more correct to 
175     # check these assertions.
176     elements.each do |elt|
177       result.root <<
178         if (elt.version == 1) 
179           # first version, so it must be newly-created.
180           created = XML::Node.new "create"
181           created << elt.to_xml_node
182         else
183           # get the previous version from the element history
184           prev_elt = elt.class.find(:first, :conditions => 
185                                     ['id = ? and version = ?',
186                                      elt.id, elt.version])
187           unless elt.visible
188             # if the element isn't visible then it must have been deleted, so
189             # output the *previous* XML
190             deleted = XML::Node.new "delete"
191             deleted << prev_elt.to_xml_node
192           else
193             # must be a modify, for which we don't need the previous version
194             # yet...
195             modified = XML::Node.new "modify"
196             modified << elt.to_xml_node
197           end
198         end
199     end
200
201     render :text => result.to_s, :content_type => "text/xml"
202             
203   rescue ActiveRecord::RecordNotFound
204     render :nothing => true, :status => :not_found
205   rescue OSM::APIError => ex
206     render ex.render_opts
207   end
208
209 end