1 class Changeset < ActiveRecord::Base
4 belongs_to :user, :counter_cache => true
6 has_many :changeset_tags
13 has_many :old_relations
15 validates_presence_of :id, :on => :update
16 validates_presence_of :user_id, :created_at, :closed_at, :num_changes
17 validates_uniqueness_of :id
18 validates_numericality_of :id, :on => :update, :integer_only => true
19 validates_numericality_of :min_lat, :max_lat, :min_lon, :max_lat, :allow_nil => true, :integer_only => true
20 validates_numericality_of :user_id, :integer_only => true
21 validates_numericality_of :num_changes, :integer_only => true, :greater_than_or_equal_to => 0
23 before_save :update_closed_at
25 # over-expansion factor to use when updating the bounding box
28 # maximum number of elements allowed in a changeset
31 # maximum time a changeset is allowed to be open for.
34 # idle timeout increment, one hour seems reasonable.
37 # Use a method like this, so that we can easily change how we
38 # determine whether a changeset is open, without breaking code in at
41 # a changeset is open (that is, it will accept further changes) when
42 # it has not yet run out of time and its capacity is small enough.
43 # note that this may not be a hard limit - due to timing changes and
44 # concurrency it is possible that some changesets may be slightly
45 # longer than strictly allowed or have slightly more changes in them.
46 return ((closed_at > Time.now.getutc) and (num_changes <= MAX_ELEMENTS))
49 def set_closed_time_now
51 self.closed_at = Time.now.getutc
55 def self.from_xml(xml, create=false)
57 p = XML::Parser.string(xml, :options => XML::Parser::Options::NOERROR)
60 doc.find('//osm/changeset').each do |pt|
61 return Changeset.from_xml_node(pt, create)
63 raise OSM::APIBadXMLError.new("changeset", xml, "XML doesn't contain an osm/changeset element.")
64 rescue LibXML::XML::Error, ArgumentError => ex
65 raise OSM::APIBadXMLError.new("changeset", xml, ex.message)
69 def self.from_xml_node(pt, create=false)
72 cs.created_at = Time.now.getutc
73 # initial close time is 1h ahead, but will be increased on each
75 cs.closed_at = cs.created_at + IDLE_TIMEOUT
76 # initially we have no changes in a changeset
80 pt.find('tag').each do |tag|
81 raise OSM::APIBadXMLError.new("changeset", pt, "tag is missing key") if tag['k'].nil?
82 raise OSM::APIBadXMLError.new("changeset", pt, "tag is missing value") if tag['v'].nil?
83 cs.add_tag_keyval(tag['k'], tag['v'])
90 # returns the bounding box of the changeset. it is possible that some
91 # or all of the values will be nil, indicating that they are undefined.
93 @bbox ||= BoundingBox.new(min_lon, min_lat, max_lon, max_lat)
101 # expand the bounding box to include the given bounding box. also,
102 # expand a little bit more in the direction of the expansion, so that
103 # further expansions may be unnecessary. this is an optimisation
104 # suggested on the wiki page by kleptog.
105 def update_bbox!(bbox_update)
106 bbox.expand!(bbox_update, EXPAND)
108 # update active record. rails 2.1's dirty handling should take care of
109 # whether this object needs saving or not.
110 self.min_lon, self.min_lat, self.max_lon, self.max_lat = @bbox.to_a if bbox.complete?
114 # the number of elements is also passed in so that we can ensure that
115 # a single changeset doesn't contain too many elements. this, of course,
116 # destroys the optimisation described in the bbox method above.
117 def add_changes!(elements)
118 self.num_changes += elements
128 self.changeset_tags.each do |tag|
139 def add_tag_keyval(k, v)
140 @tags = Hash.new unless @tags
142 # duplicate tags are now forbidden, so we can't allow values
143 # in the hash to be overwritten.
144 raise OSM::APIDuplicateTagsError.new("changeset", self.id, k) if @tags.include? k
150 # do the changeset update and the changeset tags update in the
151 # same transaction to ensure consistency.
152 Changeset.transaction do
156 ChangesetTag.delete_all(:changeset_id => self.id)
159 tag = ChangesetTag.new
160 tag.changeset_id = self.id
169 # set the auto-close time to be one hour in the future unless
170 # that would make it more than 24h long, in which case clip to
171 # 24h, as this has been decided is a reasonable time limit.
174 if (closed_at - created_at) > (MAX_TIME_OPEN - IDLE_TIMEOUT)
175 self.closed_at = created_at + MAX_TIME_OPEN
177 self.closed_at = Time.now.getutc + IDLE_TIMEOUT
183 doc = OSM::API.new.get_xml_doc
184 doc.root << to_xml_node()
188 def to_xml_node(user_display_name_cache = nil)
189 el1 = XML::Node.new 'changeset'
190 el1['id'] = self.id.to_s
192 user_display_name_cache = {} if user_display_name_cache.nil?
194 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
195 # use the cache if available
196 elsif self.user.data_public?
197 user_display_name_cache[self.user_id] = self.user.display_name
199 user_display_name_cache[self.user_id] = nil
202 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
203 el1['uid'] = self.user_id.to_s if self.user.data_public?
205 self.tags.each do |k,v|
206 el2 = XML::Node.new('tag')
212 el1['created_at'] = self.created_at.xmlschema
213 el1['closed_at'] = self.closed_at.xmlschema unless is_open?
214 el1['open'] = is_open?.to_s
217 bbox.to_unscaled.add_bounds_to(el1, '_')
220 # NOTE: changesets don't include the XML of the changes within them,
221 # they are just structures for tagging. to get the osmChange of a
222 # changeset, see the download method of the controller.
228 # update this instance from another instance given and the user who is
229 # doing the updating. note that this method is not for updating the
230 # bounding box, only the tags of the changeset.
231 def update_from(other, user)
232 # ensure that only the user who opened the changeset may modify it.
233 unless user.id == self.user_id
234 raise OSM::APIUserChangesetMismatchError.new
237 # can't change a closed changeset
239 raise OSM::APIChangesetAlreadyClosedError.new(self)
242 # copy the other's tags
243 self.tags = other.tags