1 # == Schema Information
3 # Table name: changesets
5 # id :integer not null, primary key
6 # user_id :integer not null
7 # created_at :datetime not null
12 # closed_at :datetime not null
13 # num_changes :integer default(0), not null
17 # changesets_bbox_idx (min_lat,max_lat,min_lon,max_lon)
18 # changesets_closed_at_idx (closed_at)
19 # changesets_created_at_idx (created_at)
20 # changesets_user_id_created_at_idx (user_id,created_at)
21 # changesets_user_id_id_idx (user_id,id)
25 # changesets_user_id_fkey (user_id => users.id)
28 class Changeset < ActiveRecord::Base
31 belongs_to :user, :counter_cache => true
33 has_many :changeset_tags
40 has_many :old_relations
42 has_many :comments, -> { where(:visible => true).order(:created_at) }, :class_name => "ChangesetComment"
43 has_and_belongs_to_many :subscribers, :class_name => "User", :join_table => "changesets_subscribers", :association_foreign_key => "subscriber_id"
45 validates :id, :uniqueness => true, :presence => { :on => :update },
46 :numericality => { :on => :update, :integer_only => true }
47 validates :user_id, :presence => true,
48 :numericality => { :integer_only => true }
49 validates :num_changes, :presence => true,
50 :numericality => { :integer_only => true,
51 :greater_than_or_equal_to => 0 }
52 validates :created_at, :closed_at, :presence => true
53 validates :min_lat, :max_lat, :min_lon, :max_lat, :allow_nil => true,
54 :numericality => { :integer_only => true }
56 before_save :update_closed_at
58 # maximum number of elements allowed in a changeset
61 # maximum time a changeset is allowed to be open for.
64 # idle timeout increment, one hour seems reasonable.
67 # Use a method like this, so that we can easily change how we
68 # determine whether a changeset is open, without breaking code in at
71 # a changeset is open (that is, it will accept further changes) when
72 # it has not yet run out of time and its capacity is small enough.
73 # note that this may not be a hard limit - due to timing changes and
74 # concurrency it is possible that some changesets may be slightly
75 # longer than strictly allowed or have slightly more changes in them.
76 ((closed_at > Time.now.getutc) && (num_changes <= MAX_ELEMENTS))
79 def set_closed_time_now
80 self.closed_at = Time.now.getutc if is_open?
83 def self.from_xml(xml, create = false)
84 p = XML::Parser.string(xml, :options => XML::Parser::Options::NOERROR)
87 doc.find("//osm/changeset").each do |pt|
88 return Changeset.from_xml_node(pt, create)
90 raise OSM::APIBadXMLError.new("changeset", xml, "XML doesn't contain an osm/changeset element.")
91 rescue LibXML::XML::Error, ArgumentError => ex
92 raise OSM::APIBadXMLError.new("changeset", xml, ex.message)
95 def self.from_xml_node(pt, create = false)
98 cs.created_at = Time.now.getutc
99 # initial close time is 1h ahead, but will be increased on each
101 cs.closed_at = cs.created_at + IDLE_TIMEOUT
102 # initially we have no changes in a changeset
106 pt.find("tag").each do |tag|
107 raise OSM::APIBadXMLError.new("changeset", pt, "tag is missing key") if tag["k"].nil?
108 raise OSM::APIBadXMLError.new("changeset", pt, "tag is missing value") if tag["v"].nil?
109 cs.add_tag_keyval(tag["k"], tag["v"])
116 # returns the bounding box of the changeset. it is possible that some
117 # or all of the values will be nil, indicating that they are undefined.
119 @bbox ||= BoundingBox.new(min_lon, min_lat, max_lon, max_lat)
127 # expand the bounding box to include the given bounding box.
128 def update_bbox!(bbox_update)
129 bbox.expand!(bbox_update)
131 # update active record. rails 2.1's dirty handling should take care of
132 # whether this object needs saving or not.
133 self.min_lon, self.min_lat, self.max_lon, self.max_lat = @bbox.to_a if bbox.complete?
137 # the number of elements is also passed in so that we can ensure that
138 # a single changeset doesn't contain too many elements.
139 def add_changes!(elements)
140 self.num_changes += elements
146 changeset_tags.each do |tag|
155 def add_tag_keyval(k, v)
158 # duplicate tags are now forbidden, so we can't allow values
159 # in the hash to be overwritten.
160 raise OSM::APIDuplicateTagsError.new("changeset", id, k) if @tags.include? k
166 # do the changeset update and the changeset tags update in the
167 # same transaction to ensure consistency.
168 Changeset.transaction do
172 ChangesetTag.where(:changeset_id => id).delete_all
175 tag = ChangesetTag.new
176 tag.changeset_id = id
185 # set the auto-close time to be one hour in the future unless
186 # that would make it more than 24h long, in which case clip to
187 # 24h, as this has been decided is a reasonable time limit.
190 self.closed_at = if (closed_at - created_at) > (MAX_TIME_OPEN - IDLE_TIMEOUT)
191 created_at + MAX_TIME_OPEN
193 Time.now.getutc + IDLE_TIMEOUT
198 def to_xml(include_discussion = false)
199 doc = OSM::API.new.get_xml_doc
200 doc.root << to_xml_node(nil, include_discussion)
204 def to_xml_node(user_display_name_cache = nil, include_discussion = false)
205 el1 = XML::Node.new "changeset"
208 user_display_name_cache = {} if user_display_name_cache.nil?
210 if user_display_name_cache && user_display_name_cache.key?(user_id)
211 # use the cache if available
212 elsif user.data_public?
213 user_display_name_cache[user_id] = user.display_name
215 user_display_name_cache[user_id] = nil
218 el1["user"] = user_display_name_cache[user_id] unless user_display_name_cache[user_id].nil?
219 el1["uid"] = user_id.to_s if user.data_public?
222 el2 = XML::Node.new("tag")
228 el1["created_at"] = created_at.xmlschema
229 el1["closed_at"] = closed_at.xmlschema unless is_open?
230 el1["open"] = is_open?.to_s
232 bbox.to_unscaled.add_bounds_to(el1, "_") if bbox.complete?
234 el1["comments_count"] = comments.length.to_s
236 if include_discussion
237 el2 = XML::Node.new("discussion")
238 comments.includes(:author).each do |comment|
239 el3 = XML::Node.new("comment")
240 el3["date"] = comment.created_at.xmlschema
241 el3["uid"] = comment.author.id.to_s if comment.author.data_public?
242 el3["user"] = comment.author.display_name.to_s if comment.author.data_public?
243 el4 = XML::Node.new("text")
244 el4.content = comment.body.to_s
251 # NOTE: changesets don't include the XML of the changes within them,
252 # they are just structures for tagging. to get the osmChange of a
253 # changeset, see the download method of the controller.
259 # update this instance from another instance given and the user who is
260 # doing the updating. note that this method is not for updating the
261 # bounding box, only the tags of the changeset.
262 def update_from(other, user)
263 # ensure that only the user who opened the changeset may modify it.
264 raise OSM::APIUserChangesetMismatchError unless user.id == user_id
266 # can't change a closed changeset
267 raise OSM::APIChangesetAlreadyClosedError, self unless is_open?
269 # copy the other's tags
270 self.tags = other.tags