]> git.openstreetmap.org Git - rails.git/blob - app/controllers/way_controller.rb
Merge remote-tracking branch 'upstream/pull/2014'
[rails.git] / app / controllers / way_controller.rb
1 class WayController < 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     way = Way.from_xml(request.raw_post, true)
16
17     # Assume that Way.from_xml has thrown an exception if there is an error parsing the xml
18     way.create_with_history current_user
19     render :plain => way.id.to_s
20   end
21
22   def read
23     way = Way.find(params[:id])
24
25     response.last_modified = way.timestamp
26
27     if way.visible
28       render :xml => way.to_xml.to_s
29     else
30       head :gone
31     end
32   end
33
34   def update
35     way = Way.find(params[:id])
36     new_way = Way.from_xml(request.raw_post)
37
38     unless new_way && new_way.id == way.id
39       raise OSM::APIBadUserInput, "The id in the url (#{way.id}) is not the same as provided in the xml (#{new_way.id})"
40     end
41
42     way.update_from(new_way, current_user)
43     render :plain => way.version.to_s
44   end
45
46   # This is the API call to delete a way
47   def delete
48     way = Way.find(params[:id])
49     new_way = Way.from_xml(request.raw_post)
50
51     if new_way && new_way.id == way.id
52       way.delete_with_history!(new_way, current_user)
53       render :plain => way.version.to_s
54     else
55       head :bad_request
56     end
57   end
58
59   def full
60     way = Way.includes(:nodes => :node_tags).find(params[:id])
61
62     if way.visible
63       visible_nodes = {}
64       changeset_cache = {}
65       user_display_name_cache = {}
66
67       doc = OSM::API.new.get_xml_doc
68       way.nodes.uniq.each do |node|
69         if node.visible
70           doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
71           visible_nodes[node.id] = node
72         end
73       end
74       doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
75
76       render :xml => doc.to_s
77     else
78       head :gone
79     end
80   end
81
82   def ways
83     unless params["ways"]
84       raise OSM::APIBadUserInput, "The parameter ways is required, and must be of the form ways=id[,id[,id...]]"
85     end
86
87     ids = params["ways"].split(",").collect(&:to_i)
88
89     raise OSM::APIBadUserInput, "No ways were given to search for" if ids.empty?
90
91     doc = OSM::API.new.get_xml_doc
92
93     Way.find(ids).each do |way|
94       doc.root << way.to_xml_node
95     end
96
97     render :xml => doc.to_s
98   end
99
100   ##
101   # returns all the ways which are currently using the node given in the
102   # :id parameter. note that this used to return deleted ways as well, but
103   # this seemed not to be the expected behaviour, so it was removed.
104   def ways_for_node
105     wayids = WayNode.where(:node_id => params[:id]).collect { |ws| ws.id[0] }.uniq
106
107     doc = OSM::API.new.get_xml_doc
108
109     Way.find(wayids).each do |way|
110       doc.root << way.to_xml_node if way.visible
111     end
112
113     render :xml => doc.to_s
114   end
115 end