1 # The ChangesetController is the RESTful interface to Changeset objects
4 class ChangesetsController < ApiController
7 before_action :check_api_writable, :only => [:create, :update]
8 before_action :setup_user_auth, :only => [:show]
9 before_action :authorize, :only => [:create, :update]
13 before_action :require_public_data, :only => [:create, :update]
14 before_action :set_request_formats, :except => [:create]
16 # Helper methods for checking consistency
17 include ConsistencyValidations
20 # query changesets by bounding box, time, user or open/closed status.
22 raise OSM::APIBadUserInput, "cannot use order=oldest with time" if params[:time] && params[:order] == "oldest"
24 # find any bounding box
25 bbox = BoundingBox.from_bbox_params(params) if params["bbox"]
27 # create the conditions that the user asked for. some or all of
29 changesets = Changeset.all
30 changesets = conditions_bbox(changesets, bbox)
31 changesets = conditions_user(changesets, params["user"], params["display_name"])
32 changesets = conditions_time(changesets, params["time"])
33 changesets = query_conditions_time(changesets)
34 changesets = conditions_open(changesets, params["open"])
35 changesets = conditions_closed(changesets, params["closed"])
36 changesets = conditions_ids(changesets, params["changesets"])
39 changesets = if params[:order] == "oldest"
40 changesets.order(:created_at => :asc)
42 changesets.order(:created_at => :desc)
46 changesets = query_limit(changesets)
48 # preload users, tags and comments, and render result
49 @changesets = changesets.preload(:user, :changeset_tags, :comments)
51 respond_to do |format|
58 # Return XML giving the basic info about the changeset. Does not
59 # return anything about the nodes, ways and relations in the changeset.
61 @changeset = Changeset.find(params[:id])
62 if params[:include_discussion].presence
63 @comments = @changeset.comments
64 @comments = @comments.unscope(:where => :visible) if params[:show_hidden_comments].presence && can?(:create, :changeset_comment_visibility)
65 @comments = @comments.includes(:author)
68 respond_to do |format|
74 # Create a changeset from XML.
76 cs = Changeset.from_xml(request.raw_post, :create => true)
78 # Assume that Changeset.from_xml has thrown an exception if there is an error parsing the xml
79 cs.user = current_user
82 # Subscribe user to changeset comments
83 cs.subscribers << current_user
85 render :plain => cs.id.to_s
89 # updates a changeset's tags. none of the changeset's attributes are
90 # user-modifiable, so they will be ignored.
92 # changesets are not (yet?) versioned, so we don't have to deal with
93 # history tables here. changesets are locked to a single user, however.
95 # after succesful update, returns the XML of the changeset.
97 @changeset = Changeset.find(params[:id])
98 new_changeset = Changeset.from_xml(request.raw_post)
100 check_changeset_consistency(@changeset, current_user)
101 @changeset.update_from(new_changeset, current_user)
104 respond_to do |format|
112 #------------------------------------------------------------
113 # utility functions below.
114 #------------------------------------------------------------
117 # if a bounding box was specified do some sanity checks.
118 # restrict changesets to those enclosed by a bounding box
119 def conditions_bbox(changesets, bbox)
121 bbox.check_boundaries
122 bbox = bbox.to_scaled
124 changesets.where("min_lon < ? and max_lon > ? and min_lat < ? and max_lat > ?",
125 bbox.max_lon.to_i, bbox.min_lon.to_i,
126 bbox.max_lat.to_i, bbox.min_lat.to_i)
133 # restrict changesets to those by a particular user
134 def conditions_user(changesets, user, name)
135 if user.nil? && name.nil?
138 # shouldn't provide both name and UID
139 raise OSM::APIBadUserInput, "provide either the user ID or display name, but not both" if user && name
141 # use either the name or the UID to find the user which we're selecting on.
143 # user input checking, we don't have any UIDs < 1
144 raise OSM::APIBadUserInput, "invalid user ID" if user.to_i < 1
146 u = User.find_by(:id => user.to_i)
148 u = User.find_by(:display_name => name)
151 # make sure we found a user
152 raise OSM::APINotFoundError if u.nil?
154 # should be able to get changesets of public users only, or
155 # our own changesets regardless of public-ness.
156 unless u.data_public?
157 # get optional user auth stuff so that users can see their own
158 # changesets if they're non-public
161 raise OSM::APINotFoundError if current_user.nil? || current_user != u
164 changesets.where(:user => u)
169 # restrict changesets to those during a particular time period
170 def conditions_time(changesets, time)
173 elsif time.count(",") == 1
174 # if there is a range, i.e: comma separated, then the first is
175 # low, second is high - same as with bounding boxes.
177 # check that we actually have 2 elements in the array
178 times = time.split(",")
179 raise OSM::APIBadUserInput, "bad time range" if times.size != 2
181 from, to = times.collect { |t| Time.parse(t).utc }
182 changesets.where("closed_at >= ? and created_at <= ?", from, to)
184 # if there is no comma, assume its a lower limit on time
185 changesets.where(:closed_at => Time.parse(time).utc..)
187 # stupid Time seems to throw both of these for bad parsing, so
188 # we have to catch both and ensure the correct code path is taken.
189 rescue ArgumentError, RuntimeError => e
190 raise OSM::APIBadUserInput, e.message.to_s
194 # return changesets which are open (haven't been closed yet)
195 # we do this by seeing if the 'closed at' time is in the future. Also if we've
196 # hit the maximum number of changes then it counts as no longer open.
197 # if parameter 'open' is nill then open and closed changesets are returned
198 def conditions_open(changesets, open)
202 changesets.where("closed_at >= ? and num_changes <= ?",
203 Time.now.utc, Changeset::MAX_ELEMENTS)
208 # query changesets which are closed
209 # ('closed at' time has passed or changes limit is hit)
210 def conditions_closed(changesets, closed)
214 changesets.where("closed_at < ? or num_changes > ?",
215 Time.now.utc, Changeset::MAX_ELEMENTS)
220 # query changesets by a list of ids
221 # (either specified as array or comma-separated string)
222 def conditions_ids(changesets, ids)
226 raise OSM::APIBadUserInput, "No changesets were given to search for"
228 ids = ids.split(",").collect(&:to_i)
229 changesets.where(:id => ids)