]> git.openstreetmap.org Git - rails.git/blob - app/controllers/way_controller.rb
The current_way_nodes table is already an InnoDB table.
[rails.git] / app / controllers / way_controller.rb
1 class WayController < ApplicationController
2   require 'xml/libxml'
3
4   session :off
5   before_filter :authorize, :only => [:create, :update, :delete]
6   before_filter :check_write_availability, :only => [:create, :update, :delete]
7   before_filter :check_read_availability, :except => [:create, :update, :delete]
8   after_filter :compress_output
9
10   def create
11     if request.put?
12       way = Way.from_xml(request.raw_post, true)
13
14       if way
15         if !way.preconditions_ok?
16           render :text => "", :status => :precondition_failed
17         else
18           way.version = 0
19           way.user_id = @user.id
20           way.save_with_history!
21
22           render :text => way.id.to_s, :content_type => "text/plain"
23         end
24       else
25         render :nothing => true, :status => :bad_request
26       end
27     else
28       render :nothing => true, :status => :method_not_allowed
29     end
30   end
31
32   def read
33     begin
34       way = Way.find(params[:id])
35
36       response.headers['Last-Modified'] = way.timestamp.rfc822
37
38       if way.visible
39         render :text => way.to_xml.to_s, :content_type => "text/xml"
40       else
41         render :text => "", :status => :gone
42       end
43     rescue ActiveRecord::RecordNotFound
44       render :nothing => true, :status => :not_found
45     end
46   end
47
48   def update
49     begin
50       way = Way.find(params[:id])
51       new_way = Way.from_xml(request.raw_post)
52
53       if new_way and new_way.id == way.id
54         unless update_internal(way, new_way)
55           render :text => "", :status => :precondition_failed
56         else
57           render :nothing => true
58         end
59       else
60         render :nothing => true, :status => :bad_request
61       end
62     rescue ActiveRecord::RecordNotFound
63       render :nothing => true, :status => :not_found
64     end
65   end
66
67   def update_internal way, new_way
68     way = Way.find(new_way.id) if way.nil?
69
70     if !new_way.preconditions_ok?
71       return false
72     else
73       way.user_id = @user.id
74       way.tags = new_way.tags
75       way.nds = new_way.nds
76       way.visible = true
77       way.save_with_history!
78
79       return true
80     end
81   end
82
83   # This is the API call to delete a way
84   def delete
85     begin
86       way = Way.find(params[:id])
87       way.delete_with_relations_and_history(@user)
88
89       # if we get here, all is fine, otherwise something will catch below.  
90       render :nothing => true
91     rescue OSM::APIAlreadyDeletedError
92       render :text => "", :status => :gone
93     rescue OSM::APIPreconditionFailedError
94       render :text => "", :status => :precondition_failed
95     rescue ActiveRecord::RecordNotFound
96       render :nothing => true, :status => :not_found
97     end
98   end
99
100   def full
101     begin
102       way = Way.find(params[:id])
103
104       if way.visible
105         nd_ids = way.nds + [-1]
106         nodes = Node.find(:all, :conditions => "visible = 1 AND id IN (#{nd_ids.join(',')})")
107
108         # Render
109         doc = OSM::API.new.get_xml_doc
110         nodes.each do |node|
111           doc.root << node.to_xml_node()
112         end
113         doc.root << way.to_xml_node()
114
115         render :text => doc.to_s, :content_type => "text/xml"
116       else
117         render :text => "", :status => :gone
118       end
119     rescue ActiveRecord::RecordNotFound
120       render :nothing => true, :status => :not_found
121     end
122   end
123
124   def ways
125     begin
126       ids = params['ways'].split(',').collect { |w| w.to_i }
127     rescue
128       ids = []
129     end
130
131     if ids.length > 0
132       doc = OSM::API.new.get_xml_doc
133
134       Way.find(ids).each do |way|
135         doc.root << way.to_xml_node
136       end
137
138       render :text => doc.to_s, :content_type => "text/xml"
139     else
140       render :nothing => true, :status => :bad_request
141     end
142   end
143
144   def ways_for_node
145     wayids = WayNode.find(:all, :conditions => ['node_id = ?', params[:id]]).collect { |ws| ws.id[0] }.uniq
146
147     doc = OSM::API.new.get_xml_doc
148
149     Way.find(wayids).each do |way|
150       doc.root << way.to_xml_node
151     end
152
153     render :text => doc.to_s, :content_type => "text/xml"
154   end
155 end