Imported Upstream version 1.2.4
[archive/platform/upstream/libvirt.git] / docs / search.php.code.in
1 <?php
2     $query = $_GET['query'];
3     // We handle only the first argument so far
4     $query = ltrim ($query);
5
6     $scope = $_GET['scope'];
7     if ($scope == NULL)
8         $scope = "any";
9     $scope = ltrim ($scope);
10     if ($scope == "")
11         $scope = "any";
12 ?>
13
14 <form action="<?php echo $_SERVER['PHP_SELF'], "?query=", rawurlencode($query) ?>"
15       enctype="application/x-www-form-urlencoded" method="get">
16   <input name="query" type="text" size="50" value="<?php echo $query?>"/>
17   <select name="scope">
18     <option value="any">Search All</option>
19     <option value="API" <?php if ($scope == 'API') print "selected='selected'"?>>Only the APIs</option>
20     <option value="DOCS" <?php if ($scope == 'DOCS') print "selected='selected'"?>>Only the Documentation</option>
21     <option value="LISTS" <?php if ($scope == 'LISTS') print "selected='selected'"?>>Only the lists archives</option>
22   </select>
23   <input name="submit" type="submit" value="Search ..."/>
24 </form>
25
26 <?php
27     function logQueryWord($word) {
28         $result = mysql_query ("SELECT ID,Count FROM Queries WHERE Value='$word'");
29         if ($result) {
30             $i = mysql_num_rows($result);
31             if ($i == 0) {
32                 mysql_free_result($result);
33                 mysql_query ("INSERT INTO Queries (Value,Count) VALUES ('$word',1)");
34             } else {
35                 $id = mysql_result($result, 0, 0);
36                 $count = mysql_result($result, 0, 1);
37                 $count ++;
38                 mysql_query ("UPDATE Queries SET Count=$count WHERE ID=$id");
39             }
40         } else {
41             mysql_query ("INSERT INTO Queries (Value,Count) VALUES ('$word',1)");
42         }
43     }
44     function queryWord($word) {
45         $result = NULL;
46         $j = 0;
47         if ($word) {
48             $result = mysql_query ("SELECT words.relevance, symbols.name, symbols.type, symbols.module, symbols.descr FROM words, symbols WHERE LCASE(words.name) LIKE LCASE('$word') and words.symbol = symbols.name ORDER BY words.relevance DESC LIMIT 75");
49             if ($result) {
50                 $j = mysql_num_rows($result);
51                 if ($j == 0)
52                     mysql_free_result($result);
53             }
54             logQueryWord($word);
55         }
56         return array($result, $j);
57     }
58     function queryHTMLWord($word) {
59         $result = NULL;
60         $j = 0;
61         if ($word) {
62             $result = mysql_query ("SELECT relevance, name, id, resource, section FROM wordsHTML WHERE LCASE(name) LIKE LCASE('$word') ORDER BY relevance DESC LIMIT 75");
63             if ($result) {
64                 $j = mysql_num_rows($result);
65                 if ($j == 0)
66                     mysql_free_result($result);
67             }
68             logQueryWord($word);
69         }
70         return array($result, $j);
71     }
72     function queryArchiveWord($word) {
73         $result = NULL;
74         $j = 0;
75         if ($word) {
76             $result = mysql_query ("SELECT wordsArchive.relevance, wordsArchive.name, 'libvir-list', archives.resource, archives.title FROM wordsArchive, archives WHERE LCASE(wordsArchive.name) LIKE LCASE('$word') and wordsArchive.ID = archives.ID ORDER BY relevance DESC LIMIT 75");
77             if ($result) {
78                 $j = mysql_num_rows($result);
79                 if ($j == 0)
80                     mysql_free_result($result);
81             }
82             logQueryWord($word);
83         }
84         return array($result, $j);
85     }
86     function resSort ($a, $b) {
87         list($ra,$ta,$ma,$na,$da) = $a;
88         list($rb,$tb,$mb,$nb,$db) = $b;
89         if ($ra == $rb) return 0;
90         return ($ra > $rb) ? -1 : 1;
91     }
92     if (($query) && (strlen($query) <= 50)) {
93         $link = mysql_connect ("localhost", "nobody");
94         if (!$link) {
95             echo "<p> Could not connect to the database: ", mysql_error();
96         } else {
97             mysql_select_db("libvir", $link);
98             $list = explode (" ", $query);
99             $results = array();
100             $number = 0;
101             for ($number = 0;$number < count($list);$number++) {
102
103                 $word = $list[$number];
104                 if (($scope == 'any') || ($scope == 'API')) {
105                     list($result, $j) = queryWord($word);
106                     if ($j > 0) {
107                         for ($i = 0; $i < $j; $i++) {
108                             $relevance = mysql_result($result, $i, 0);
109                             $name = mysql_result($result, $i, 1);
110                             $type = mysql_result($result, $i, 2);
111                             $module = mysql_result($result, $i, 3);
112                             $desc = mysql_result($result, $i, 4);
113                             if (array_key_exists($name, $results)) {
114                                 list($r,$t,$m,$d,$w,$u) = $results[$name];
115                                 $results[$name] = array(($r + $relevance) * 2,
116                                                         $t,$m,$d,$w,$u);
117                             } else {
118                                 $id = $name;
119                                 $m = strtolower($module);
120                                 $url = "html/libvirt-$module.html#$id";
121                                 $results[$name] = array($relevance,$type,
122                                                 $module, $desc, $name, $url);
123                             }
124                         }
125                         mysql_free_result($result);
126                     }
127                 }
128                 if (($scope == 'any') || ($scope == 'DOCS')) {
129                     list($result, $k) = queryHTMLWord($word);
130                     if ($k > 0) {
131                         for ($i = 0; $i < $k; $i++) {
132                             $relevance = mysql_result($result, $i, 0);
133                             $name = mysql_result($result, $i, 1);
134                             $id = mysql_result($result, $i, 2);
135                             $module = mysql_result($result, $i, 3);
136                             $desc = mysql_result($result, $i, 4);
137                             $url = $module;
138                             if ($id != "") {
139                                 $url = $url + "#$id";
140                             }
141                             $results["$name _html_ $number _ $i"] =
142                                           array($relevance, "XML docs",
143                                                 $module, $desc, $name, $url);
144                         }
145                         mysql_free_result($result);
146                     }
147                 }
148                 if (($scope == 'any') || ($scope == 'LISTS')) {
149                     list($result, $j) = queryArchiveWord($word);
150                     if ($j > 0) {
151                         for ($i = 0; $i < $j; $i++) {
152                             $relevance = mysql_result($result, $i, 0);
153                             $name = mysql_result($result, $i, 1);
154                             $type = mysql_result($result, $i, 2);
155                             $url = mysql_result($result, $i, 3);
156                             $desc = mysql_result($result, $i, 4);
157                             if (array_key_exists($url, $results)) {
158                                 list($r,$t,$m,$d,$w,$u) = $results[$url];
159                                 $results[$name] = array(($r + $relevance) * 2,
160                                                         $t,$m,$d,$w,$u);
161                             } else {
162                                 $id = $name;
163                                 $m = strtolower($module);
164                                 $u = str_replace(
165                         "http://www.redhat.com/archives/libvir-list/", "", $url);
166                                 $results[$url] = array($relevance,$type,
167                                                 $u, $desc, $name, $url);
168                             }
169                         }
170                         mysql_free_result($result);
171                     }
172                 }
173             }
174             if ((count($results) == 0) && (count($list) == 1)) {
175                 $word = $list[0];
176                 if (($scope == 'any') || ($scope == 'XMLAPI')) {
177                     list($result, $j) = queryWord("vir$word");
178                     if ($j > 0) {
179                         for ($i = 0; $i < $j; $i++) {
180                             $relevance = mysql_result($result, $i, 0);
181                             $name = mysql_result($result, $i, 1);
182                             $type = mysql_result($result, $i, 2);
183                             $module = mysql_result($result, $i, 3);
184                             $desc = mysql_result($result, $i, 4);
185                             if (array_key_exists($name, $results)) {
186                                 list($r,$t,$m,$d,$w,$u) = $results[$name];
187                                 $results[$name] = array(($r + $relevance) * 2,
188                                                         $t,$m,$d,$w,$u);
189                             } else {
190                                 $id = $name;
191                                 $m = strtolower($module);
192                                 $url = "html/libvirt-$module.html#$id";
193                                 $results[$name] = array($relevance,$type,
194                                                 $module, $desc, $name, $url);
195                             }
196                         }
197                         mysql_free_result($result);
198                     }
199                 }
200             }
201             mysql_close($link);
202             $nb = count($results);
203             echo "<h3 align='center'>Found $nb results for query $query</h3>\n";
204             usort($results, "resSort");
205
206             if ($nb > 0) {
207                 printf("<table><tbody>\n");
208                 printf("<tr><td>Quality</td><td>Symbol</td><td>Type</td><td>module</td><td>Description</td></tr>\n");
209                 $i = 0;
210                 while (list ($name, $val) = each ($results)) {
211                     list($r,$t,$m,$d,$s,$u) = $val;
212                     $m = str_replace("<", "&lt;", $m);
213                     $s = str_replace("<", "&lt;", $s);
214                     $d = str_replace("<", "&lt;", $d);
215                     echo "<tr><td>$r</td><td><a href='$u'>$s</a></td><td>$t</td><td>$m</td><td>$d</td></tr>";
216                     $i = $i + 1;
217                     if ($i > 75)
218                         break;
219                 }
220                 printf("</tbody></table>\n");
221             }
222         }
223     }
224 ?>