]> git.openstreetmap.org Git - rails.git/blob - app/models/changeset.rb
Improve error handling for routing engines
[rails.git] / app / models / changeset.rb
1 class Changeset < ActiveRecord::Base
2   require "xml/libxml"
3
4   belongs_to :user, :counter_cache => true
5
6   has_many :changeset_tags
7
8   has_many :nodes
9   has_many :ways
10   has_many :relations
11   has_many :old_nodes
12   has_many :old_ways
13   has_many :old_relations
14
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"
17
18   validates :id, :uniqueness => true, :presence => { :on => :update },
19                  :numericality => { :on => :update, :integer_only => true }
20   validates :user_id, :presence => true,
21                       :numericality => { :integer_only => true }
22   validates :num_changes, :presence => true,
23                           :numericality => { :integer_only => true,
24                                              :greater_than_or_equal_to => 0 }
25   validates :created_at, :closed_at, :presence => true
26   validates :min_lat, :max_lat, :min_lon, :max_lat, :allow_nil => true,
27                                                     :numericality => { :integer_only => true }
28
29   before_save :update_closed_at
30
31   # over-expansion factor to use when updating the bounding box
32   EXPAND = 0.1
33
34   # maximum number of elements allowed in a changeset
35   MAX_ELEMENTS = 50000
36
37   # maximum time a changeset is allowed to be open for.
38   MAX_TIME_OPEN = 1.day
39
40   # idle timeout increment, one hour seems reasonable.
41   IDLE_TIMEOUT = 1.hour
42
43   # Use a method like this, so that we can easily change how we
44   # determine whether a changeset is open, without breaking code in at
45   # least 6 controllers
46   def is_open?
47     # a changeset is open (that is, it will accept further changes) when
48     # it has not yet run out of time and its capacity is small enough.
49     # note that this may not be a hard limit - due to timing changes and
50     # concurrency it is possible that some changesets may be slightly
51     # longer than strictly allowed or have slightly more changes in them.
52     ((closed_at > Time.now.getutc) && (num_changes <= MAX_ELEMENTS))
53   end
54
55   def set_closed_time_now
56     self.closed_at = Time.now.getutc if is_open?
57   end
58
59   def self.from_xml(xml, create = false)
60     p = XML::Parser.string(xml, :options => XML::Parser::Options::NOERROR)
61     doc = p.parse
62
63     doc.find("//osm/changeset").each do |pt|
64       return Changeset.from_xml_node(pt, create)
65     end
66     fail OSM::APIBadXMLError.new("changeset", xml, "XML doesn't contain an osm/changeset element.")
67   rescue LibXML::XML::Error, ArgumentError => ex
68     raise OSM::APIBadXMLError.new("changeset", xml, ex.message)
69   end
70
71   def self.from_xml_node(pt, create = false)
72     cs = Changeset.new
73     if create
74       cs.created_at = Time.now.getutc
75       # initial close time is 1h ahead, but will be increased on each
76       # modification.
77       cs.closed_at = cs.created_at + IDLE_TIMEOUT
78       # initially we have no changes in a changeset
79       cs.num_changes = 0
80     end
81
82     pt.find("tag").each do |tag|
83       fail OSM::APIBadXMLError.new("changeset", pt, "tag is missing key") if tag["k"].nil?
84       fail OSM::APIBadXMLError.new("changeset", pt, "tag is missing value") if tag["v"].nil?
85       cs.add_tag_keyval(tag["k"], tag["v"])
86     end
87
88     cs
89   end
90
91   ##
92   # returns the bounding box of the changeset. it is possible that some
93   # or all of the values will be nil, indicating that they are undefined.
94   def bbox
95     @bbox ||= BoundingBox.new(min_lon, min_lat, max_lon, max_lat)
96   end
97
98   def has_valid_bbox?
99     bbox.complete?
100   end
101
102   ##
103   # expand the bounding box to include the given bounding box. also,
104   # expand a little bit more in the direction of the expansion, so that
105   # further expansions may be unnecessary. this is an optimisation
106   # suggested on the wiki page by kleptog.
107   def update_bbox!(bbox_update)
108     bbox.expand!(bbox_update, EXPAND)
109
110     # update active record. rails 2.1's dirty handling should take care of
111     # whether this object needs saving or not.
112     self.min_lon, self.min_lat, self.max_lon, self.max_lat = @bbox.to_a if bbox.complete?
113   end
114
115   ##
116   # the number of elements is also passed in so that we can ensure that
117   # a single changeset doesn't contain too many elements. this, of course,
118   # destroys the optimisation described in the bbox method above.
119   def add_changes!(elements)
120     self.num_changes += elements
121   end
122
123   def tags
124     unless @tags
125       @tags = {}
126       changeset_tags.each do |tag|
127         @tags[tag.k] = tag.v
128       end
129     end
130     @tags
131   end
132
133   attr_writer :tags
134
135   def add_tag_keyval(k, v)
136     @tags = {} unless @tags
137
138     # duplicate tags are now forbidden, so we can't allow values
139     # in the hash to be overwritten.
140     fail OSM::APIDuplicateTagsError.new("changeset", id, k) if @tags.include? k
141
142     @tags[k] = v
143   end
144
145   def save_with_tags!
146     # do the changeset update and the changeset tags update in the
147     # same transaction to ensure consistency.
148     Changeset.transaction do
149       self.save!
150
151       tags = self.tags
152       ChangesetTag.delete_all(:changeset_id => id)
153
154       tags.each do |k, v|
155         tag = ChangesetTag.new
156         tag.changeset_id = id
157         tag.k = k
158         tag.v = v
159         tag.save!
160       end
161     end
162   end
163
164   ##
165   # set the auto-close time to be one hour in the future unless
166   # that would make it more than 24h long, in which case clip to
167   # 24h, as this has been decided is a reasonable time limit.
168   def update_closed_at
169     if self.is_open?
170       if (closed_at - created_at) > (MAX_TIME_OPEN - IDLE_TIMEOUT)
171         self.closed_at = created_at + MAX_TIME_OPEN
172       else
173         self.closed_at = Time.now.getutc + IDLE_TIMEOUT
174       end
175     end
176   end
177
178   def to_xml(include_discussion = false)
179     doc = OSM::API.new.get_xml_doc
180     doc.root << to_xml_node(nil, include_discussion)
181     doc
182   end
183
184   def to_xml_node(user_display_name_cache = nil, include_discussion = false)
185     el1 = XML::Node.new "changeset"
186     el1["id"] = id.to_s
187
188     user_display_name_cache = {} if user_display_name_cache.nil?
189
190     if user_display_name_cache && user_display_name_cache.key?(user_id)
191       # use the cache if available
192     elsif user.data_public?
193       user_display_name_cache[user_id] = user.display_name
194     else
195       user_display_name_cache[user_id] = nil
196     end
197
198     el1["user"] = user_display_name_cache[user_id] unless user_display_name_cache[user_id].nil?
199     el1["uid"] = user_id.to_s if user.data_public?
200
201     tags.each do |k, v|
202       el2 = XML::Node.new("tag")
203       el2["k"] = k.to_s
204       el2["v"] = v.to_s
205       el1 << el2
206     end
207
208     el1["created_at"] = created_at.xmlschema
209     el1["closed_at"] = closed_at.xmlschema unless is_open?
210     el1["open"] = is_open?.to_s
211
212     bbox.to_unscaled.add_bounds_to(el1, "_") if bbox.complete?
213
214     el1["comments_count"] = comments.count.to_s
215
216     if include_discussion
217       el2 = XML::Node.new("discussion")
218       comments.includes(:author).each do |comment|
219         el3 = XML::Node.new("comment")
220         el3["date"] = comment.created_at.xmlschema
221         el3["uid"] = comment.author.id.to_s if comment.author.data_public?
222         el3["user"] = comment.author.display_name.to_s if comment.author.data_public?
223         el4 = XML::Node.new("text")
224         el4.content = comment.body.to_s
225         el3 << el4
226         el2 << el3
227       end
228       el1 << el2
229     end
230
231     # NOTE: changesets don't include the XML of the changes within them,
232     # they are just structures for tagging. to get the osmChange of a
233     # changeset, see the download method of the controller.
234
235     el1
236   end
237
238   ##
239   # update this instance from another instance given and the user who is
240   # doing the updating. note that this method is not for updating the
241   # bounding box, only the tags of the changeset.
242   def update_from(other, user)
243     # ensure that only the user who opened the changeset may modify it.
244     fail OSM::APIUserChangesetMismatchError.new unless user.id == user_id
245
246     # can't change a closed changeset
247     fail OSM::APIChangesetAlreadyClosedError.new(self) unless is_open?
248
249     # copy the other's tags
250     self.tags = other.tags
251
252     save_with_tags!
253   end
254 end