]> git.openstreetmap.org Git - rails.git/blob - app/controllers/changeset_controller.rb
Implemented osmChange diff downloads for changesets and a couple of tests.
[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]
8   before_filter :check_write_availability, :only => [:create, :update, :delete, :upload]
9   before_filter :check_read_availability, :except => [:create, :update, :delete, :upload]
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 create_prim(ids, prim, nd)
30     prim.version = 0
31     prim.user_id = @user.id
32     prim.visible = true
33     prim.save_with_history!
34
35     ids[nd['id'].to_i] = prim.id
36   end
37
38   def fix_way(w, node_ids)
39     w.nds = w.instance_eval { @nds }.
40       map { |nd| node_ids[nd] || nd }
41     return w
42   end
43
44   def fix_rel(r, ids)
45     r.members = r.instance_eval { @members }.
46       map { |memb| [memb[0], ids[memb[0]][memb[1].to_i] || memb[1], memb[2]] }
47     return r
48   end
49   
50   def read
51     begin
52       changeset = Changeset.find(params[:id])
53       render :text => changeset.to_xml.to_s, :content_type => "text/xml"
54     rescue ActiveRecord::RecordNotFound
55       render :nothing => true, :status => :not_found
56     end
57   end
58   
59   def close 
60     begin
61       unless request.put?
62         render :nothing => true, :status => :method_not_allowed
63         return
64       end
65       changeset = Changeset.find(params[:id])
66       changeset.open = false
67       changeset.save!
68       render :nothing => true
69     rescue ActiveRecord::RecordNotFound
70       render :nothing => true, :status => :not_found
71     end
72   end
73
74   ##
75   # Upload a diff in a single transaction.
76   #
77   # This means that each change within the diff must succeed, i.e: that
78   # each version number mentioned is still current. Otherwise the entire
79   # transaction *must* be rolled back.
80   #
81   # Furthermore, each element in the diff can only reference the current
82   # changeset.
83   #
84   # Returns: a diffResult document, as described in 
85   # http://wiki.openstreetmap.org/index.php/OSM_Protocol_Version_0.6
86   def upload
87     # only allow POST requests, as the upload method is most definitely
88     # not idempotent, as several uploads with placeholder IDs will have
89     # different side-effects.
90     # see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.2
91     unless request.post?
92       render :nothing => true, :status => :method_not_allowed
93       return
94     end
95
96     changeset = Changeset.find(params[:id])
97     
98     diff_reader = DiffReader.new(request.raw_post, changeset)
99     Changeset.transaction do
100       result = diff_reader.commit
101       render :text => result.to_s, :content_type => "text/xml"
102     end
103     
104   rescue ActiveRecord::RecordNotFound
105     render :nothing => true, :status => :not_found
106   rescue OSM::APIError => ex
107     render ex.render_opts
108   end
109
110   ##
111   # download the changeset as an osmChange document.
112   #
113   # to make it easier to revert diffs it would be better if the osmChange
114   # format were reversible, i.e: contained both old and new versions of 
115   # modified elements. but it doesn't at the moment...
116   #
117   # this method cannot order the database changes fully (i.e: timestamp and
118   # version number may be too coarse) so the resulting diff may not apply
119   # to a different database. however since changesets are not atomic this 
120   # behaviour cannot be guaranteed anyway and is the result of a design
121   # choice.
122   def download
123     changeset = Changeset.find(params[:id])
124     
125     # get all the elements in the changeset and stick them in a big array.
126     elements = [changeset.old_nodes, 
127                 changeset.old_ways, 
128                 changeset.old_relations].flatten
129     
130     # sort the elements by timestamp and version number, as this is the 
131     # almost sensible ordering available. this would be much nicer if 
132     # global (SVN-style) versioning were used - then that would be 
133     # unambiguous.
134     elements.sort! do |a, b| 
135       if (a.timestamp == b.timestamp)
136         a.version <=> b.version
137       else
138         a.timestamp <=> b.timestamp 
139       end
140     end
141     
142     # create an osmChange document for the output
143     result = OSM::API.new.get_xml_doc
144     result.root.name = "osmChange"
145
146     # generate an output element for each operation. note: we avoid looking
147     # at the history because it is simpler - but it would be more correct to 
148     # check these assertions.
149     elements.each do |elt|
150       result.root <<
151         if (elt.version == 1) 
152           # first version, so it must be newly-created.
153           created = XML::Node.new "create"
154           created << elt.to_xml_node
155         else
156           # get the previous version from the element history
157           prev_elt = elt.class.find(:first, :conditions => 
158                                     ['id = ? and version = ?',
159                                      elt.id, elt.version])
160           unless elt.visible
161             # if the element isn't visible then it must have been deleted, so
162             # output the *previous* XML
163             deleted = XML::Node.new "delete"
164             deleted << prev_elt.to_xml_node
165           else
166             # must be a modify, for which we don't need the previous version
167             # yet...
168             modified = XML::Node.new "modify"
169             modified << elt.to_xml_node
170           end
171         end
172     end
173
174     render :text => result.to_s, :content_type => "text/xml"
175             
176   rescue ActiveRecord::RecordNotFound
177     render :nothing => true, :status => :not_found
178   rescue OSM::APIError => ex
179     render ex.render_opts
180   end
181
182 end