+ end
+
+ # create changeset and user caches
+ changeset_cache = {}
+ user_display_name_cache = {}
+
+ # create an osmChange document for the output
+ result = OSM::API.new.get_xml_doc
+ result.root.name = "osmChange"
+
+ # 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(changeset_cache, user_display_name_cache)
+ else
+ unless elt.visible
+ # if the element isn't visible then it must have been deleted
+ deleted = XML::Node.new "delete"
+ deleted << elt.to_xml_node(changeset_cache, user_display_name_cache)
+ else
+ # must be a modify
+ modified = XML::Node.new "modify"
+ modified << elt.to_xml_node(changeset_cache, user_display_name_cache)
+ end
+ end
+ end
+
+ render :text => result.to_s, :content_type => "text/xml"
+ end
+
+ ##
+ # query changesets by bounding box, time, user or open/closed status.
+ def query
+ # find any bounding box
+ if params['bbox']
+ bbox = BoundingBox.from_bbox_params(params)
+ end
+
+ # create the conditions that the user asked for. some or all of
+ # these may be nil.
+ changesets = Changeset.all
+ changesets = conditions_bbox(changesets, bbox)
+ changesets = conditions_user(changesets, params['user'], params['display_name'])
+ changesets = conditions_time(changesets, params['time'])
+ changesets = conditions_open(changesets, params['open'])
+ changesets = conditions_closed(changesets, params['closed'])
+
+ # create the results document
+ results = OSM::API.new.get_xml_doc
+
+ # add all matching changesets to the XML results document
+ changesets.order("created_at DESC").limit(100).each do |cs|
+ results.root << cs.to_xml_node
+ end
+
+ render :text => results.to_s, :content_type => "text/xml"
+ 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.
+ assert_method :put
+
+ 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 :text => "", :status => :bad_request
+ end
+ end
+
+ ##
+ # list edits (open changesets) in reverse chronological order
+ def list
+ if request.format == :atom and params[:page]
+ redirect_to params.merge({ :page => nil }), :status => :moved_permanently
+ elsif request.format == :html and !params[:bbox]
+ render :action => :history, :layout => map_layout
+ else
+ changesets = conditions_nonempty(Changeset.all)
+
+ if params[:display_name]
+ user = User.find_by_display_name(params[:display_name])
+
+ if user and user.active?
+ if user.data_public? or user == @user
+ changesets = changesets.where(:user_id => user.id)
+ else
+ changesets = changesets.where("false")
+ end
+ else
+ render_unknown_user params[:display_name]
+ return
+ end