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