1 # The ChangesetController is the RESTful interface to Changeset objects
3 class ChangesetController < ApplicationController
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
12 # Create a changeset from XML.
15 cs = Changeset.from_xml(request.raw_post, true)
20 render :text => cs.id.to_s, :content_type => "text/plain"
22 render :nothing => true, :status => :bad_request
25 render :nothing => true, :status => :method_not_allowed
29 def create_prim(ids, prim, nd)
31 prim.user_id = @user.id
33 prim.save_with_history!
35 ids[nd['id'].to_i] = prim.id
38 def fix_way(w, node_ids)
39 w.nds = w.instance_eval { @nds }.
40 map { |nd| node_ids[nd] || nd }
45 r.members = r.instance_eval { @members }.
46 map { |memb| [memb[0], ids[memb[0]][memb[1].to_i] || memb[1], memb[2]] }
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
62 render :nothing => true, :status => :method_not_allowed
65 changeset = Changeset.find(params[:id])
66 changeset.open = false
68 render :nothing => true
69 rescue ActiveRecord::RecordNotFound
70 render :nothing => true, :status => :not_found
75 # Upload a diff in a single transaction.
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.
81 # Furthermore, each element in the diff can only reference the current
84 # Returns: a diffResult document, as described in
85 # http://wiki.openstreetmap.org/index.php/OSM_Protocol_Version_0.6
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
92 render :nothing => true, :status => :method_not_allowed
96 changeset = Changeset.find(params[:id])
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"
104 rescue ActiveRecord::RecordNotFound
105 render :nothing => true, :status => :not_found
106 rescue OSM::APIError => ex
107 render ex.render_opts
111 # download the changeset as an osmChange document.
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...
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
123 changeset = Changeset.find(params[:id])
125 # get all the elements in the changeset and stick them in a big array.
126 elements = [changeset.old_nodes,
128 changeset.old_relations].flatten
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
134 elements.sort! do |a, b|
135 if (a.timestamp == b.timestamp)
136 a.version <=> b.version
138 a.timestamp <=> b.timestamp
142 # create an osmChange document for the output
143 result = OSM::API.new.get_xml_doc
144 result.root.name = "osmChange"
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|
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
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])
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
166 # must be a modify, for which we don't need the previous version
168 modified = XML::Node.new "modify"
169 modified << elt.to_xml_node
174 render :text => result.to_s, :content_type => "text/xml"
176 rescue ActiveRecord::RecordNotFound
177 render :nothing => true, :status => :not_found
178 rescue OSM::APIError => ex
179 render ex.render_opts