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