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