]> git.openstreetmap.org Git - rails.git/blob - app/controllers/changeset_controller.rb
api06: Preliminary support for diff uploading. This will not return anything
[rails.git] / app / controllers / changeset_controller.rb
1 # The ChangesetController is the RESTful interface to Changeset objects
2
3 class ChangesetController < ApplicationController
4   require 'xml/libxml'
5
6   before_filter :authorize, :only => [:create, :update, :delete, :upload]
7   before_filter :check_write_availability, :only => [:create, :update, :delete, :upload]
8   before_filter :check_read_availability, :except => [:create, :update, :delete, :upload]
9   after_filter :compress_output
10
11   # Create a changeset from XML.
12   def create
13     if request.put?
14       cs = Changeset.from_xml(request.raw_post, true)
15
16       if cs
17         cs.user_id = @user.id
18         cs.save_with_tags!
19         render :text => cs.id.to_s, :content_type => "text/plain"
20       else
21         render :nothing => true, :status => :bad_request
22       end
23     else
24       render :nothing => true, :status => :method_not_allowed
25     end
26   end
27
28   def create_prim(ids, prim, nd)
29     prim.version = 0
30     prim.user_id = @user.id
31     prim.visible = true
32     prim.save_with_history!
33
34     ids[nd['id'].to_i] = prim.id
35   end
36
37   def fix_way(w, node_ids)
38     w.nds.each { |nd|
39       new_id = node_ids[nd.node_id]
40       nd.node_id = new_id unless new_id.nil?
41     }
42   end
43
44   def fix_rel(r, ids)
45     r.members.each { |memb|
46       new_id = ids[memb.member_type][memb.member_id]
47       nd.member_id = new_id unless new_id.nil?
48     }
49   end
50
51   def upload
52     if not request.put?
53       render :nothing => true, :status => :method_not_allowed
54       return
55     end
56
57     # FIXME: this should really be done without loading the whole XML file
58     # into memory.
59     p = XML::Parser.new
60     p.string  = request.raw_post
61     doc = p.parse
62
63     node_ids, way_ids, rel_ids = {}, {}, {}
64     ids = {"node"=>node_ids, "way"=>way_ids, "relation"=>rel_ids}
65
66     Changeset.transaction do
67       doc.find('//osm/create/node').each do |nd|
68         create_prim node_ids, Node.from_xml_node(nd, true), nd
69       end
70       doc.find('//osm/create/way').each do |nd|
71         way = Way.from_xml_node(nd, true)
72         raise OSM::APIPreconditionFailedError.new if !way.preconditions_ok?
73         create_prim way_ids, fix_way(way, node_ids), nd
74       end
75       doc.find('//osm/create/relation').each do |nd|
76         relation = Relation.from_xml_node(nd, true)
77         raise OSM::APIPreconditionFailedError.new if !way.preconditions_ok?
78         create_prim relation_ids, fix_rel(relation, ids), nd
79       end
80
81       doc.find('//osm/modify/node').each do |nd|
82         unless NodeController.update_internal nil, Node.from_xml_node(nd)
83           raise OSM::APIPreconditionFailedError.new
84         end
85       end
86       doc.find('//osm/modify/way').each do |nd|
87         unless WayController.update_internal nil, fix_way(Way.from_xml_node(nd), node_ids)
88           raise OSM::APIPreconditionFailedError.new
89         end
90       end
91       doc.find('//osm/modify/relation').each do |nd|
92         unless RelationController.update_internal nil, fix_rel(Relation.from_xml_node(nd), ids)
93           raise OSM::APIPreconditionFailedError.new
94         end
95       end
96
97       doc.find('//osm/delete/node').each do |nd|
98         unless NodeController.delete_internal nil, Node.from_xml_node(n)
99           raise OSM::APIPreconditionFailedError.new
100         end
101       end
102       doc.find('//osm/delete/way').each do |nd|
103         Way.from_xml_node(nd).delete_with_relations_and_history(@user)
104       end
105       doc.find('//osm/delete/relation').each do |nd|
106         unless RelationController.delete_internal nil, fix_rel(Relation.from_xml_node(nd), ids)
107           raise OSM::APIPreconditionFailedError.new
108         end
109       end
110     end
111
112     render :text => "Ok, Fine. Upload worked without errors.\n", :status => 200
113   end
114 end