- doc.find('//osm/modify/node').each do |nd|
- unless NodeController.new.update_internal nil, Node.from_xml_node(nd)
- raise OSM::APIPreconditionFailedError.new
- end
- end
- doc.find('//osm/modify/way').each do |nd|
- unless WayController.update_internal nil, fix_way(Way.from_xml_node(nd), node_ids)
- raise OSM::APIPreconditionFailedError.new
- end
- end
- doc.find('//osm/modify/relation').each do |nd|
- unless RelationController.update_internal nil, fix_rel(Relation.from_xml_node(nd), ids)
- raise OSM::APIPreconditionFailedError.new
- end
+ ##
+ # download the changeset as an osmChange document.
+ #
+ # to make it easier to revert diffs it would be better if the osmChange
+ # format were reversible, i.e: contained both old and new versions of
+ # modified elements. but it doesn't at the moment...
+ #
+ # this method cannot order the database changes fully (i.e: timestamp and
+ # version number may be too coarse) so the resulting diff may not apply
+ # to a different database. however since changesets are not atomic this
+ # behaviour cannot be guaranteed anyway and is the result of a design
+ # choice.
+ def download
+ changeset = Changeset.find(params[:id])
+
+ # get all the elements in the changeset and stick them in a big array.
+ elements = [changeset.old_nodes,
+ changeset.old_ways,
+ changeset.old_relations].flatten
+
+ # sort the elements by timestamp and version number, as this is the
+ # almost sensible ordering available. this would be much nicer if
+ # global (SVN-style) versioning were used - then that would be
+ # unambiguous.
+ elements.sort! do |a, b|
+ if (a.timestamp == b.timestamp)
+ a.version <=> b.version
+ else
+ a.timestamp <=> b.timestamp