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