58ee4ab260ff3d805a0c530730e3533bfdb46854
[platform/upstream/doxygen.git] / templates / html / search_opensearch.php
1 <script language="PHP">
2 require "search_functions.php";
3
4 $mode = array_key_exists('v', $_GET)?$_GET['v']:"";
5 $query = array_key_exists('query', $_GET)?$_GET['query']:"";
6 $query = preg_replace("/[^a-zA-Z0-9\-\_\.\x80-\xFF]/i", " ", $query );
7
8 $query_results = run_query($query);
9
10 switch ($mode)
11 {
12   case "opensearch.xml":
13     opensearch_description();
14     break;
15   case "json":
16     opensearch_json_results($query, $query_results);
17     break;
18   case "xml":
19     opensearch_xml_results($query, $query_results);
20     break;
21   default:
22     invalid_format($query, $query_results);
23     break;
24 }
25
26 function opensearch_description()
27 {
28   global $config;
29   global $translator;
30
31   $shortname = $translator['search']." ".$config['PROJECT_NAME'];
32   $link = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']);
33   header("Content-Type: application/xml");
34   echo <<<END_OPENSEARCH
35 <?xml version="1.0" encoding="UTF-8"?>
36 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
37 <ShortName>$shortname</ShortName>
38 <Description>Doxygen Search</Description>
39 <InputEncoding>UTF-8</InputEncoding>
40 <!--
41 <Image height="16" width="16" type="image/x-icon">
42 http://dev.squello.com/doc/html/favicon.ico</Image>
43 -->
44 <Url type="text/html" method="GET"
45 template="$link/search.php?query={searchTerms}" />
46 <Url type="application/x-suggestions+json" method="GET"
47 template="$link/search_opensearch.php?v=json&amp;query={searchTerms}" />
48 <Url type="application/x-suggestions+xml" method="GET"
49 template="$link/search_opensearch.php?v=xml&amp;query={searchTerms}" />
50 </OpenSearchDescription>
51 END_OPENSEARCH;
52 }
53
54 function opensearch_xml_results($query, array $results)
55 {
56   // Much as I hate copy'n'paste code re-use, this is for testing;
57   // I expect a richer version to come soon.
58   // Although I hate that IE does this richer than FF more...
59   $qs_results = array();
60   foreach ($results as $i => $val)
61   {
62     foreach ($val['words'] as $j => $word)
63     {
64       if (array_key_exists($word, $qs_results))
65         $qs_results[$word['match']]++;
66       else
67         $qs_results[$word['match']] = 1;
68     }
69   }
70   $result = <<<END_FRAG
71 <?xml version="1.0"?>
72 <SearchSuggestion xmlns="http://schemas.microsoft.com/Search/2008/suggestions">
73 <Query>$query</Query>
74 <Section>
75 END_FRAG;
76   foreach ($qs_results as $word => $count)
77   {
78     $result .= <<<END_FRAG
79 <Item>
80 <Text>$word</Text>
81 <Description>$count results</Description>
82 </Item>
83 END_FRAG;
84   }
85   $result .= <<<END_FRAG
86 </Section>
87 </SearchSuggestion>
88 END_FRAG;
89   echo $result;
90 }
91
92 function opensearch_json_results($query, array $results)
93 {
94   $qs_results = array();
95   foreach ($results as $i => $val)
96   {
97     foreach ($val['words'] as $j => $word)
98     {
99       if (array_key_exists($word, $qs_results))
100         $qs_results[$word['match']]++;
101       else
102         $qs_results[$word['match']] = 1;
103     }
104   }
105   $result = '["'.$query.'", [';
106   $json_words = "";
107   $json_descriptions = "";
108   $i = 0;
109   foreach ($qs_results as $word => $count)
110   {
111     if ($i != 0)
112     {
113       $json_words .= ", ";
114       $json_descriptions .= ", ";
115     }
116     $json_words .= '"'.$word.'"';
117     $json_descriptions .= '"'.$count.' result'.($count==1?'':'s').'"';
118     $i++;
119   }
120   print "[\"$query\", [$json_words],[$json_descriptions]]";
121 }
122
123 function invalid_format($query, array $results)
124 {
125   print "Search results for '$query':\n\n";
126   print_r($results);
127 }
128 </script>