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