]> git.openstreetmap.org Git - rails.git/blob - app/controllers/search_controller.rb
Use SecureRandom instead of the deprecated ActiveSupport::SecureRandom
[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   after_filter :compress_output
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   def search_nodes
15     do_search(false,true,false)
16   end
17   def search_relations
18     do_search(false,false,true)
19   end
20
21   def do_search(do_ways,do_nodes,do_relations)
22     type = params['type']
23     value = params['value']
24     unless type or value
25       name = params['name']
26       if name
27         type = 'name'
28         value = name
29       end
30     end
31
32     if do_nodes
33       response.headers['Error'] = "Searching of nodes is currently unavailable"
34       render :nothing => true, :status => :service_unavailable
35       return false
36     end
37
38     unless value
39       response.headers['Error'] = "Searching for a key without value is currently unavailable"
40       render :nothing => true, :status => :service_unavailable
41       return false
42     end
43
44     # Matching for node tags table
45     if do_nodes
46       nodes = Node.joins(:node_tags)
47       nodes = nodes.where(:current_node_tags => { :k => type }) if type
48       nodes = nodes.where(:current_node_tags => { :v => value }) if value
49       nodes = nodes.limit(100)
50     end
51
52     # Matching for way tags table
53     if do_ways
54       ways = Way.joins(:way_tags)
55       ways = ways.where(:current_way_tags => { :k => type }) if type
56       ways = ways.where(:current_way_tags => { :v => value }) if value
57       ways = ways.limit(100)
58     end
59
60     # Matching for relation tags table
61     if do_relations
62       relations = Relation.joins(:relation_tags)
63       relations = relations.where(:current_relation_tags => { :k => type }) if type
64       relations = relations.where(:current_relation_tags => { :v => value }) if value
65       relations = relations.limit(2000)
66     end
67
68     # Fetch any node needed for our ways (only have matching nodes so far)
69     nodes += Node.find(ways.collect { |w| w.nds }.uniq)
70
71     # Print
72     visible_nodes = {}
73     changeset_cache = {}
74     user_display_name_cache = {}
75     doc = OSM::API.new.get_xml_doc
76     nodes.each do |node|
77       doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
78       visible_nodes[node.id] = node
79     end
80
81     ways.each do |way|
82       doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
83     end
84
85     relations.each do |rel|
86       doc.root << rel.to_xml_node(nil, changeset_cache, user_display_name_cache)
87     end
88
89     render :text => doc.to_s, :content_type => "text/xml"
90   end
91 end