1 class Changeset < ActiveRecord::Base
4 belongs_to :user, :counter_cache => true
6 has_many :changeset_tags
13 has_many :old_relations
15 has_many :comments, -> { where(:visible => true).order(:created_at) }, :class_name => "ChangesetComment"
16 has_and_belongs_to_many :subscribers, :class_name => 'User', :join_table => 'changesets_subscribers', :association_foreign_key => 'subscriber_id'
18 validates_presence_of :id, :on => :update
19 validates_presence_of :user_id, :created_at, :closed_at, :num_changes
20 validates_uniqueness_of :id
21 validates_numericality_of :id, :on => :update, :integer_only => true
22 validates_numericality_of :min_lat, :max_lat, :min_lon, :max_lat, :allow_nil => true, :integer_only => true
23 validates_numericality_of :user_id, :integer_only => true
24 validates_numericality_of :num_changes, :integer_only => true, :greater_than_or_equal_to => 0
26 before_save :update_closed_at
28 # over-expansion factor to use when updating the bounding box
31 # maximum number of elements allowed in a changeset
34 # maximum time a changeset is allowed to be open for.
37 # idle timeout increment, one hour seems reasonable.
40 # Use a method like this, so that we can easily change how we
41 # determine whether a changeset is open, without breaking code in at
44 # a changeset is open (that is, it will accept further changes) when
45 # it has not yet run out of time and its capacity is small enough.
46 # note that this may not be a hard limit - due to timing changes and
47 # concurrency it is possible that some changesets may be slightly
48 # longer than strictly allowed or have slightly more changes in them.
49 ((closed_at > Time.now.getutc) && (num_changes <= MAX_ELEMENTS))
52 def set_closed_time_now
54 self.closed_at = Time.now.getutc
58 def self.from_xml(xml, create = false)
59 p = XML::Parser.string(xml, :options => XML::Parser::Options::NOERROR)
62 doc.find('//osm/changeset').each do |pt|
63 return Changeset.from_xml_node(pt, create)
65 fail OSM::APIBadXMLError.new("changeset", xml, "XML doesn't contain an osm/changeset element.")
66 rescue LibXML::XML::Error, ArgumentError => ex
67 raise OSM::APIBadXMLError.new("changeset", xml, ex.message)
70 def self.from_xml_node(pt, create = false)
73 cs.created_at = Time.now.getutc
74 # initial close time is 1h ahead, but will be increased on each
76 cs.closed_at = cs.created_at + IDLE_TIMEOUT
77 # initially we have no changes in a changeset
81 pt.find('tag').each do |tag|
82 fail OSM::APIBadXMLError.new("changeset", pt, "tag is missing key") if tag['k'].nil?
83 fail OSM::APIBadXMLError.new("changeset", pt, "tag is missing value") if tag['v'].nil?
84 cs.add_tag_keyval(tag['k'], tag['v'])
91 # returns the bounding box of the changeset. it is possible that some
92 # or all of the values will be nil, indicating that they are undefined.
94 @bbox ||= BoundingBox.new(min_lon, min_lat, max_lon, max_lat)
102 # expand the bounding box to include the given bounding box. also,
103 # expand a little bit more in the direction of the expansion, so that
104 # further expansions may be unnecessary. this is an optimisation
105 # suggested on the wiki page by kleptog.
106 def update_bbox!(bbox_update)
107 bbox.expand!(bbox_update, EXPAND)
109 # update active record. rails 2.1's dirty handling should take care of
110 # whether this object needs saving or not.
111 self.min_lon, self.min_lat, self.max_lon, self.max_lat = @bbox.to_a if bbox.complete?
115 # the number of elements is also passed in so that we can ensure that
116 # a single changeset doesn't contain too many elements. this, of course,
117 # destroys the optimisation described in the bbox method above.
118 def add_changes!(elements)
119 self.num_changes += elements
129 changeset_tags.each do |tag|
138 def add_tag_keyval(k, v)
139 @tags = {} unless @tags
141 # duplicate tags are now forbidden, so we can't allow values
142 # in the hash to be overwritten.
143 fail OSM::APIDuplicateTagsError.new("changeset", id, k) if @tags.include? k
149 # do the changeset update and the changeset tags update in the
150 # same transaction to ensure consistency.
151 Changeset.transaction do
155 ChangesetTag.delete_all(:changeset_id => id)
158 tag = ChangesetTag.new
159 tag.changeset_id = id
168 # set the auto-close time to be one hour in the future unless
169 # that would make it more than 24h long, in which case clip to
170 # 24h, as this has been decided is a reasonable time limit.
173 if (closed_at - created_at) > (MAX_TIME_OPEN - IDLE_TIMEOUT)
174 self.closed_at = created_at + MAX_TIME_OPEN
176 self.closed_at = Time.now.getutc + IDLE_TIMEOUT
181 def to_xml(include_discussion = false)
182 doc = OSM::API.new.get_xml_doc
183 doc.root << to_xml_node(nil, include_discussion)
187 def to_xml_node(user_display_name_cache = nil, include_discussion = false)
188 el1 = XML::Node.new 'changeset'
191 user_display_name_cache = {} if user_display_name_cache.nil?
193 if user_display_name_cache && user_display_name_cache.key?(user_id)
194 # use the cache if available
195 elsif user.data_public?
196 user_display_name_cache[user_id] = user.display_name
198 user_display_name_cache[user_id] = nil
201 el1['user'] = user_display_name_cache[user_id] unless user_display_name_cache[user_id].nil?
202 el1['uid'] = user_id.to_s if user.data_public?
205 el2 = XML::Node.new('tag')
211 el1['created_at'] = created_at.xmlschema
212 el1['closed_at'] = closed_at.xmlschema unless is_open?
213 el1['open'] = is_open?.to_s
216 bbox.to_unscaled.add_bounds_to(el1, '_')
219 el1['comments_count'] = comments.count.to_s
221 if include_discussion
222 el2 = XML::Node.new('discussion')
223 comments.includes(:author).each do |comment|
224 el3 = XML::Node.new('comment')
225 el3['date'] = comment.created_at.xmlschema
226 el3['uid'] = comment.author.id.to_s if comment.author.data_public?
227 el3['user'] = comment.author.display_name.to_s if comment.author.data_public?
228 el4 = XML::Node.new('text')
229 el4.content = comment.body.to_s
236 # NOTE: changesets don't include the XML of the changes within them,
237 # they are just structures for tagging. to get the osmChange of a
238 # changeset, see the download method of the controller.
244 # update this instance from another instance given and the user who is
245 # doing the updating. note that this method is not for updating the
246 # bounding box, only the tags of the changeset.
247 def update_from(other, user)
248 # ensure that only the user who opened the changeset may modify it.
249 unless user.id == user_id
250 fail OSM::APIUserChangesetMismatchError.new
253 # can't change a closed changeset
255 fail OSM::APIChangesetAlreadyClosedError.new(self)
258 # copy the other's tags
259 self.tags = other.tags