+ # generate an output element for each operation. note: we avoid looking
+ # at the history because it is simpler - but it would be more correct to
+ # check these assertions.
+ elements.each do |elt|
+ result.root <<
+ if (elt.version == 1)
+ # first version, so it must be newly-created.
+ created = XML::Node.new "create"
+ created << elt.to_xml_node
+ else
+ # get the previous version from the element history
+ prev_elt = elt.class.find(:first, :conditions =>
+ ['id = ? and version = ?',
+ elt.id, elt.version])
+ unless elt.visible
+ # if the element isn't visible then it must have been deleted, so
+ # output the *previous* XML
+ deleted = XML::Node.new "delete"
+ deleted << prev_elt.to_xml_node
+ else
+ # must be a modify, for which we don't need the previous version
+ # yet...
+ modified = XML::Node.new "modify"
+ modified << elt.to_xml_node
+ end
+ end
+ end
+
+ render :text => result.to_s, :content_type => "text/xml"
+
+ rescue ActiveRecord::RecordNotFound
+ render :nothing => true, :status => :not_found
+ rescue OSM::APIError => ex
+ render ex.render_opts
+ end
+
+ ##
+ # query changesets by bounding box, time, user or open/closed status.
+ def query
+ # create the conditions that the user asked for. some or all of
+ # these may be nil.
+ conditions = conditions_bbox(params['bbox'])
+ conditions = cond_merge conditions, conditions_user(params['user'])
+ conditions = cond_merge conditions, conditions_time(params['time'])
+ conditions = cond_merge conditions, conditions_open(params['open'])
+
+ # create the results document
+ results = OSM::API.new.get_xml_doc
+
+ # add all matching changesets to the XML results document
+ Changeset.find(:all,
+ :conditions => conditions,
+ :limit => 100,
+ :order => 'created_at desc').each do |cs|
+ results.root << cs.to_xml_node
+ end
+
+ render :text => results.to_s, :content_type => "text/xml"
+
+ rescue ActiveRecord::RecordNotFound
+ render :nothing => true, :status => :not_found
+ rescue OSM::APIError => ex
+ render ex.render_opts
+ end
+
+ ##
+ # updates a changeset's tags. none of the changeset's attributes are
+ # user-modifiable, so they will be ignored.
+ #
+ # changesets are not (yet?) versioned, so we don't have to deal with
+ # history tables here. changesets are locked to a single user, however.
+ #
+ # after succesful update, returns the XML of the changeset.
+ def update
+ # request *must* be a PUT.
+ unless request.put?
+ render :nothing => true, :status => :method_not_allowed
+ return
+ end
+
+ changeset = Changeset.find(params[:id])
+ new_changeset = Changeset.from_xml(request.raw_post)
+
+ unless new_changeset.nil?
+ check_changeset_consistency(changeset, @user)
+ changeset.update_from(new_changeset, @user)
+ render :text => changeset.to_xml, :mime_type => "text/xml"
+ else
+
+ render :nothing => true, :status => :bad_request
+ end
+
+ rescue ActiveRecord::RecordNotFound
+ render :nothing => true, :status => :not_found
+ rescue OSM::APIError => ex
+ render ex.render_opts
+ end
+
+private
+ #------------------------------------------------------------
+ # utility functions below.
+ #------------------------------------------------------------
+
+ ##
+ # merge two conditions
+ def cond_merge(a, b)
+ if a and b
+ a_str = a.shift
+ b_str = b.shift
+ return [ a_str + " and " + b_str ] + a + b
+ elsif a
+ return a
+ else b
+ return b
+ end
+ end
+
+ ##
+ # if a bounding box was specified then parse it and do some sanity
+ # checks. this is mostly the same as the map call, but without the
+ # area restriction.
+ def conditions_bbox(bbox)
+ unless bbox.nil?
+ raise OSM::APIBadUserInput.new("Bounding box should be min_lon,min_lat,max_lon,max_lat") unless bbox.count(',') == 3
+ bbox = sanitise_boundaries(bbox.split(/,/))
+ raise OSM::APIBadUserInput.new("Minimum longitude should be less than maximum.") unless bbox[0] <= bbox[2]
+ raise OSM::APIBadUserInput.new("Minimum latitude should be less than maximum.") unless bbox[1] <= bbox[3]
+ return ['min_lon < ? and max_lon > ? and min_lat < ? and max_lat > ?',
+ bbox[2] * GeoRecord::SCALE, bbox[0] * GeoRecord::SCALE, bbox[3]* GeoRecord::SCALE, bbox[1] * GeoRecord::SCALE]
+ else
+ return nil
+ end
+ end
+
+ ##
+ # restrict changesets to those by a particular user
+ def conditions_user(user)
+ unless user.nil?
+ # user input checking, we don't have any UIDs < 1
+ raise OSM::APIBadUserInput.new("invalid user ID") if user.to_i < 1
+
+ u = User.find(user.to_i)
+ # should be able to get changesets of public users only, or
+ # our own changesets regardless of public-ness.
+ unless u.data_public?
+ # get optional user auth stuff so that users can see their own
+ # changesets if they're non-public
+ setup_user_auth
+
+ raise OSM::APINotFoundError if @user.nil? or @user.id != u.id