]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/relations_controller.rb
Move changeset XML generation to a view
[rails.git] / app / controllers / api / relations_controller.rb
1 module Api
2   class RelationsController < ApplicationController
3     require "xml/libxml"
4
5     skip_before_action :verify_authenticity_token
6     before_action :authorize, :only => [:create, :update, :delete]
7     before_action :api_deny_access_handler
8
9     authorize_resource
10
11     before_action :require_public_data, :only => [:create, :update, :delete]
12     before_action :check_api_writable, :only => [:create, :update, :delete]
13     before_action :check_api_readable, :except => [:create, :update, :delete]
14     around_action :api_call_handle_error, :api_call_timeout
15
16     def create
17       assert_method :put
18
19       relation = Relation.from_xml(request.raw_post, true)
20
21       # Assume that Relation.from_xml has thrown an exception if there is an error parsing the xml
22       relation.create_with_history current_user
23       render :plain => relation.id.to_s
24     end
25
26     def show
27       relation = Relation.find(params[:id])
28       response.last_modified = relation.timestamp
29       if relation.visible
30         render :xml => relation.to_xml.to_s
31       else
32         head :gone
33       end
34     end
35
36     def update
37       logger.debug request.raw_post
38
39       relation = Relation.find(params[:id])
40       new_relation = Relation.from_xml(request.raw_post)
41
42       raise OSM::APIBadUserInput, "The id in the url (#{relation.id}) is not the same as provided in the xml (#{new_relation.id})" unless new_relation && new_relation.id == relation.id
43
44       relation.update_from new_relation, current_user
45       render :plain => relation.version.to_s
46     end
47
48     def delete
49       relation = Relation.find(params[:id])
50       new_relation = Relation.from_xml(request.raw_post)
51       if new_relation && new_relation.id == relation.id
52         relation.delete_with_history!(new_relation, current_user)
53         render :plain => relation.version.to_s
54       else
55         head :bad_request
56       end
57     end
58
59     # -----------------------------------------------------------------
60     # full
61     #
62     # input parameters: id
63     #
64     # returns XML representation of one relation object plus all its
65     # members, plus all nodes part of member ways
66     # -----------------------------------------------------------------
67     def full
68       relation = Relation.find(params[:id])
69
70       if relation.visible
71
72         # first find the ids of nodes, ways and relations referenced by this
73         # relation - note that we exclude this relation just in case.
74
75         node_ids = relation.members.select { |m| m[0] == "Node" }.map { |m| m[1] }
76         way_ids = relation.members.select { |m| m[0] == "Way" }.map { |m| m[1] }
77         relation_ids = relation.members.select { |m| m[0] == "Relation" && m[1] != relation.id }.map { |m| m[1] }
78
79         # next load the relations and the ways.
80
81         relations = Relation.where(:id => relation_ids).includes(:relation_tags)
82         ways = Way.where(:id => way_ids).includes(:way_nodes, :way_tags)
83
84         # now additionally collect nodes referenced by ways. Note how we
85         # recursively evaluate ways but NOT relations.
86
87         way_node_ids = ways.collect do |way|
88           way.way_nodes.collect(&:node_id)
89         end
90         node_ids += way_node_ids.flatten
91         nodes = Node.where(:id => node_ids.uniq).includes(:node_tags)
92
93         # create XML.
94         doc = OSM::API.new.get_xml_doc
95         visible_nodes = {}
96         changeset_cache = {}
97         user_display_name_cache = {}
98
99         nodes.each do |node|
100           next unless node.visible? # should be unnecessary if data is consistent.
101
102           doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
103           visible_nodes[node.id] = node
104         end
105
106         ways.each do |way|
107           next unless way.visible? # should be unnecessary if data is consistent.
108
109           doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
110         end
111
112         relations.each do |rel|
113           next unless rel.visible? # should be unnecessary if data is consistent.
114
115           doc.root << rel.to_xml_node(changeset_cache, user_display_name_cache)
116         end
117
118         # finally add self and output
119         doc.root << relation.to_xml_node(changeset_cache, user_display_name_cache)
120         render :xml => doc.to_s
121
122       else
123         head :gone
124       end
125     end
126
127     def index
128       raise OSM::APIBadUserInput, "The parameter relations is required, and must be of the form relations=id[,id[,id...]]" unless params["relations"]
129
130       ids = params["relations"].split(",").collect(&:to_i)
131
132       raise OSM::APIBadUserInput, "No relations were given to search for" if ids.empty?
133
134       doc = OSM::API.new.get_xml_doc
135
136       Relation.find(ids).each do |relation|
137         doc.root << relation.to_xml_node
138       end
139
140       render :xml => doc.to_s
141     end
142
143     def relations_for_way
144       relations_for_object("Way")
145     end
146
147     def relations_for_node
148       relations_for_object("Node")
149     end
150
151     def relations_for_relation
152       relations_for_object("Relation")
153     end
154
155     private
156
157     def relations_for_object(objtype)
158       relationids = RelationMember.where(:member_type => objtype, :member_id => params[:id]).collect(&:relation_id).uniq
159
160       doc = OSM::API.new.get_xml_doc
161
162       Relation.find(relationids).each do |relation|
163         doc.root << relation.to_xml_node if relation.visible
164       end
165
166       render :xml => doc.to_s
167     end
168   end
169 end