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