]> git.openstreetmap.org Git - rails.git/blob - app/controllers/search_controller.rb
Move contextmenu callbacks to separate file
[rails.git] / app / controllers / search_controller.rb
1 class SearchController < ApplicationController
2   # Support searching for nodes, ways, or all
3   # Can search by tag k, v, or both (type->k,value->v)
4   # Can search by name (k=name,v=....)
5   skip_before_action :verify_authenticity_token
6
7   def search_all
8     do_search(true, true, true)
9   end
10
11   def search_ways
12     do_search(true, false, false)
13   end
14
15   def search_nodes
16     do_search(false, true, false)
17   end
18
19   def search_relations
20     do_search(false, false, true)
21   end
22
23   def do_search(do_ways, do_nodes, do_relations)
24     type = params["type"]
25     value = params["value"]
26     unless type || value
27       name = params["name"]
28       if name
29         type = "name"
30         value = name
31       end
32     end
33
34     if do_nodes
35       response.headers["Error"] = "Searching of nodes is currently unavailable"
36       render :text => "", :status => :service_unavailable
37       return false
38     end
39
40     unless value
41       response.headers["Error"] = "Searching for a key without value is currently unavailable"
42       render :text => "", :status => :service_unavailable
43       return false
44     end
45
46     # Matching for node tags table
47     if do_nodes
48       nodes = Node.joins(:node_tags)
49       nodes = nodes.where(:current_node_tags => { :k => type }) if type
50       nodes = nodes.where(:current_node_tags => { :v => value }) if value
51       nodes = nodes.limit(100)
52     else
53       nodes = []
54     end
55
56     # Matching for way tags table
57     if do_ways
58       ways = Way.joins(:way_tags)
59       ways = ways.where(:current_way_tags => { :k => type }) if type
60       ways = ways.where(:current_way_tags => { :v => value }) if value
61       ways = ways.limit(100)
62     else
63       ways = []
64     end
65
66     # Matching for relation tags table
67     if do_relations
68       relations = Relation.joins(:relation_tags)
69       relations = relations.where(:current_relation_tags => { :k => type }) if type
70       relations = relations.where(:current_relation_tags => { :v => value }) if value
71       relations = relations.limit(2000)
72     else
73       relations = []
74     end
75
76     # Fetch any node needed for our ways (only have matching nodes so far)
77     nodes += Node.find(ways.collect(&:nds).uniq)
78
79     # Print
80     visible_nodes = {}
81     changeset_cache = {}
82     user_display_name_cache = {}
83     doc = OSM::API.new.get_xml_doc
84     nodes.each do |node|
85       doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
86       visible_nodes[node.id] = node
87     end
88
89     ways.each do |way|
90       doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
91     end
92
93     relations.each do |rel|
94       doc.root << rel.to_xml_node(nil, changeset_cache, user_display_name_cache)
95     end
96
97     render :text => doc.to_s, :content_type => "text/xml"
98   end
99 end