]> git.openstreetmap.org Git - rails.git/blob - app/controllers/search_controller.rb
Make terms page work during OpenID based signup
[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     way_ids = Array.new
45     ways = Array.new
46     nodes = Array.new
47     relations = Array.new
48
49     # Matching for node tags table
50     cond_node = Array.new
51     sql = '1=1'
52     if type
53       sql += ' AND current_node_tags.k=?'
54       cond_node += [type]
55     end
56     if value
57       sql += ' AND current_node_tags.v=?'
58       cond_node += [value]
59     end
60     cond_node = [sql] + cond_node
61
62     # Matching for way tags table
63     cond_way = Array.new
64     sql = '1=1'
65     if type
66       sql += ' AND current_way_tags.k=?'
67       cond_way += [type]
68     end
69     if value
70       sql += ' AND current_way_tags.v=?'
71       cond_way += [value]
72     end
73     cond_way = [sql] + cond_way
74
75     # Matching for relation tags table
76     cond_rel = Array.new
77     sql = '1=1'
78     if type
79       sql += ' AND current_relation_tags.k=?'
80       cond_rel += [type]
81     end
82     if value
83       sql += ' AND current_relation_tags.v=?'
84       cond_rel += [value]
85     end
86     cond_rel = [sql] + cond_rel
87
88     # First up, look for the relations we want
89     if do_relations
90       relations = Relation.find(:all,
91                                 :joins => "INNER JOIN current_relation_tags ON current_relation_tags.id = current_relations.id",
92                                 :conditions => cond_rel, :limit => 100)
93     end
94
95     # then ways
96     if do_ways
97       ways = Way.find(:all,
98                       :joins => "INNER JOIN current_way_tags ON current_way_tags.id = current_ways.id",
99                       :conditions => cond_way, :limit => 100)
100     end
101
102     # Now, nodes
103     if do_nodes
104       nodes = Node.find(:all,
105                         :joins => "INNER JOIN current_node_tags ON current_node_tags.id = current_nodes.id",
106                         :conditions => cond_node, :limit => 2000)
107     end
108
109     # Fetch any node needed for our ways (only have matching nodes so far)
110     nodes += Node.find(ways.collect { |w| w.nds }.uniq)
111
112     # Print
113     visible_nodes = {}
114     changeset_cache = {}
115     user_display_name_cache = {}
116     doc = OSM::API.new.get_xml_doc
117     nodes.each do |node|
118       doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
119       visible_nodes[node.id] = node
120     end
121
122     ways.each do |way|
123       doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
124     end 
125
126     relations.each do |rel|
127       doc.root << rel.to_xml_node(nil, changeset_cache, user_display_name_cache)
128     end 
129     render :text => doc.to_s, :content_type => "text/xml"
130   end
131 end