added new function replace from Joel Reed. added new test case for above.
[platform/upstream/libxslt.git] / doc / search.php.inc
1 <?php
2     $query = $HTTP_GET_VARS[ "query" ];
3     $scope = $HTTP_GET_VARS[ "scope" ];
4     // We handle only the first argument so far
5     $query = $_GET['query'];
6     $query = ltrim ($query);
7     if (! $query) {
8         echo "<h1 align='center'>Search the documentation on XMLSoft.org</h1>";
9     }
10     if ($scope == NULL)
11         $scope = "any";
12     $scope = ltrim ($scope);
13     if ($scope == "")
14         $scope = "any";
15
16 ?>
17 <p> The search service indexes the libxml2 and libxslt APIs and documentation as well as the xml@gnome.org and xslt@gnome.org mailing-list archives. To use it simply provide a set of keywords:
18 <p>
19 <form action="<?php echo "$PHP_SELF", "?query=", rawurlencode($query) ?>"
20       enctype="application/x-www-form-urlencoded" method="GET">
21   <input name="query" type="TEXT" size="50" value="<?php echo $query?>">
22   <select name="scope">
23     <option value="any">Search All</option>
24     <option value="XML" <?php if ($scope == 'XML') print "selected"?>>XML resources</option>
25     <option value="XSLT" <?php if ($scope == 'XSLT') print "selected"?>>XSLT resources</option>
26     <option value="API" <?php if ($scope == 'API') print "selected"?>>Only the APIs</option>
27     <option value="XMLAPI" <?php if ($scope == 'XMLAPI') print "selected"?>>Only the XML API</option>
28     <option value="XSLTAPI" <?php if ($scope == 'XSLTAPI') print "selected"?>>Only the XSLT API</option>
29     <option value="DOCS" <?php if ($scope == 'DOCS') print "selected"?>>Only the Documentation</option>
30     <option value="XMLDOC" <?php if ($scope == 'XMLDOC') print "selected"?>>Only the XML Documentation</option>
31     <option value="XSLTDOC" <?php if ($scope == 'XSLTDOC') print "selected"?>>Only the XSLT Documentation</option>
32     <option value="LISTS" <?php if ($scope == 'LISTS') print "selected"?>>Only the lists archives</option>
33     <option value="XMLLIST" <?php if ($scope == 'XMLLIST') print "selected"?>>Only the XML list archive</option>
34     <option value="XSLTLIST" <?php if ($scope == 'XSLTLIST') print "selected"?>>Only the XSLT list archive</option>
35   </select>
36   <input name=submit type=submit value="Search ...">
37 </form>
38 <?php
39     function logQueryWord($word) {
40         $result = mysql_query ("SELECT ID,Count FROM Queries WHERE Value='$word'");
41         if ($result) {
42             $i = mysql_num_rows($result);
43             if ($i == 0) {
44                 mysql_free_result($result);
45                 mysql_query ("INSERT INTO Queries (Value,Count) VALUES ('$word',1)");
46             } else {
47                 $id = mysql_result($result, 0, 0);
48                 $count = mysql_result($result, 0, 1);
49                 $count ++;
50                 mysql_query ("UPDATE Queries SET Count=$count WHERE ID=$id");
51             }
52         } else {
53             mysql_query ("INSERT INTO Queries (Value,Count) VALUES ('$word',1)");
54         }
55     }
56     function queryWord($word) {
57         $result = NULL;
58         $j = 0;
59         if ($word) {
60             $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");
61             if ($result) {
62                 $j = mysql_num_rows($result);
63                 if ($j == 0) 
64                     mysql_free_result($result);
65             }
66             logQueryWord($word);
67         }
68         return array($result, $j);
69     }
70     function queryHTMLWord($word) {
71         $result = NULL;
72         $j = 0;
73         if ($word) {
74             $result = mysql_query ("SELECT relevance, name, id, resource, section FROM wordsHTML WHERE LCASE(name) LIKE LCASE('$word') ORDER BY relevance DESC LIMIT 75");
75             if ($result) {
76                 $j = mysql_num_rows($result);
77                 if ($j == 0) 
78                     mysql_free_result($result);
79             }
80             logQueryWord($word);
81         }
82         return array($result, $j);
83     }
84     function queryArchiveWord($word) {
85         $result = NULL;
86         $j = 0;
87         if ($word) {
88             $result = mysql_query ("SELECT wordsArchive.relevance, wordsArchive.name, 'xml-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");
89             if ($result) {
90                 $j = mysql_num_rows($result);
91                 if ($j == 0) 
92                     mysql_free_result($result);
93             }
94             logQueryWord($word);
95         }
96         return array($result, $j);
97     }
98     function XSLTqueryWord($word) {
99         $result = NULL;
100         $j = 0;
101         if ($word) {
102             $result = mysql_query ("SELECT XSLTwords.relevance, XSLTsymbols.name, XSLTsymbols.type, XSLTsymbols.module, XSLTsymbols.descr FROM XSLTwords, XSLTsymbols WHERE LCASE(XSLTwords.name) LIKE LCASE('$word') and XSLTwords.symbol = XSLTsymbols.name ORDER BY XSLTwords.relevance DESC LIMIT 75");
103             if ($result) {
104                 $j = mysql_num_rows($result);
105                 if ($j == 0) 
106                     mysql_free_result($result);
107             }
108             logQueryWord($word);
109         }
110         return array($result, $j);
111     }
112     function XSLTqueryHTMLWord($word) {
113         $result = NULL;
114         $j = 0;
115         if ($word) {
116             $result = mysql_query ("SELECT relevance, name, id, resource, section FROM XSLTwordsHTML WHERE LCASE(name) LIKE LCASE('$word') ORDER BY relevance DESC LIMIT 75");
117             if ($result) {
118                 $j = mysql_num_rows($result);
119                 if ($j == 0) 
120                     mysql_free_result($result);
121             }
122             logQueryWord($word);
123         }
124         return array($result, $j);
125     }
126     function XSLTqueryArchiveWord($word) {
127         $result = NULL;
128         $j = 0;
129         if ($word) {
130             $result = mysql_query ("SELECT XSLTwordsArchive.relevance, XSLTwordsArchive.name, 'xslt-list', archives.resource, archives.title FROM XSLTwordsArchive, archives WHERE LCASE(XSLTwordsArchive.name) LIKE LCASE('$word') and XSLTwordsArchive.ID = archives.ID ORDER BY relevance DESC LIMIT 75");
131             if ($result) {
132                 $j = mysql_num_rows($result);
133                 if ($j == 0) 
134                     mysql_free_result($result);
135             }
136             logQueryWord($word);
137         }
138         return array($result, $j);
139     }
140     function resSort ($a, $b) {
141         list($ra,$ta,$ma,$na,$da) = $a;
142         list($rb,$tb,$mb,$nb,$db) = $b;
143         if ($ra == $rb) return 0;
144         return ($ra > $rb) ? -1 : 1;
145     }
146     if (($query) && (strlen($query) <= 50)) {
147         $link = mysql_connect ("localhost", "nobody");
148         if (!$link) {
149             echo "<p> Could not connect to the database: ", mysql_error();
150         } else {
151             mysql_select_db("xmlsoft", $link);
152             $list = explode (" ", $query);
153             $results = array();
154             $number = 0;
155             for ($number = 0;$number < count($list);$number++) {
156
157                 $word = $list[$number];
158                 if (($scope == 'any') || ($scope == 'XML') ||
159                     ($scope == 'API') || ($scope == 'XMLAPI')) {
160                     list($result, $j) = queryWord($word);
161                     if ($j > 0) {
162                         for ($i = 0; $i < $j; $i++) {
163                             $relevance = mysql_result($result, $i, 0);
164                             $name = mysql_result($result, $i, 1);
165                             $type = mysql_result($result, $i, 2);
166                             $module = mysql_result($result, $i, 3);
167                             $desc = mysql_result($result, $i, 4);
168                             if (array_key_exists($name, $results)) {
169                                 list($r,$t,$m,$d,$w,$u) = $results[$name];
170                                 $results[$name] = array(($r + $relevance) * 2,
171                                                         $t,$m,$d,$w,$u);
172                             } else {
173                                 $id = strtoupper($name);
174 //                              $m = strtolower($module);
175                                 $m = $module;
176                                 $url = "../html/libxml-$m.html#$id";
177                                 $results[$name] = array($relevance,$type,
178                                                 $module, $desc, $name, $url);
179                             }
180                         }
181                         mysql_free_result($result);
182                     }
183                 }
184                 if (($scope == 'any') || ($scope == 'XSLT') ||
185                     ($scope == 'API') || ($scope == 'XSLTAPI')) {
186                     list($result, $j) = XSLTqueryWord($word);
187                     if ($j > 0) {
188                         for ($i = 0; $i < $j; $i++) {
189                             $relevance = mysql_result($result, $i, 0);
190                             $name = mysql_result($result, $i, 1);
191                             $type = mysql_result($result, $i, 2);
192                             $module = mysql_result($result, $i, 3);
193                             $desc = mysql_result($result, $i, 4);
194                             if (array_key_exists($name, $results)) {
195                                 list($r,$t,$m,$d,$w,$u) = $results[$name];
196                                 $results[$name] = array(($r + $relevance) * 2,
197                                                         $t,$m,$d,$w,$u);
198                             } else {
199                                 $id = strtoupper($name);
200 //                              $m = strtolower($module);
201                                 $m = $module;
202                                 $url = "html/libxslt-$m.html#$id";
203                                 $results[$name] = array($relevance,$type,
204                                                 $module, $desc, $name, $url);
205                             }
206                         }
207                         mysql_free_result($result);
208                     }
209                 }
210                 if (($scope == 'any') || ($scope == 'XML') ||
211                     ($scope == 'DOCS') || ($scope == 'XMLDOC')) {
212                     list($result, $k) = queryHTMLWord($word);
213                     if ($k > 0) {
214                         for ($i = 0; $i < $k; $i++) {
215                             $relevance = mysql_result($result, $i, 0);
216                             $name = mysql_result($result, $i, 1);
217                             $id = mysql_result($result, $i, 2);
218                             $module = mysql_result($result, $i, 3);
219                             $desc = mysql_result($result, $i, 4);
220                             $url = $module;
221                             if ($id != "") {
222                                 $url = $url + "#$id";
223                             }
224                             $results["$name _html_ $number _ $i"] =
225                                           array($relevance, "XML docs",
226                                                 $module, $desc, $name, $url);
227                         }
228                         mysql_free_result($result);
229                     }
230                 }
231                 if (($scope == 'any') || ($scope == 'XSLT') ||
232                     ($scope == 'DOCS') || ($scope == 'XSLTDOC')) {
233                     list($result, $k) = XSLTqueryHTMLWord($word);
234                     if ($k > 0) {
235                         for ($i = 0; $i < $k; $i++) {
236                             $relevance = mysql_result($result, $i, 0);
237                             $name = mysql_result($result, $i, 1);
238                             $id = mysql_result($result, $i, 2);
239                             $module = mysql_result($result, $i, 3);
240                             $desc = mysql_result($result, $i, 4);
241                             $url = "$module";
242                             if ($id != "") {
243                                 $url = $url + "#$id";
244                             }
245                             $results["$name xslthtml $number _ $i "] =
246                                           array($relevance, "XSLT docs",
247                                                 $module, $desc, $name, $url);
248                         }
249                         mysql_free_result($result);
250                     }
251                 }
252                 if (($scope == 'any') || ($scope == 'XML') ||
253                     ($scope == 'LISTS') || ($scope == 'XMLLIST')) {
254                     list($result, $j) = queryArchiveWord($word);
255                     if ($j > 0) {
256                         for ($i = 0; $i < $j; $i++) {
257                             $relevance = mysql_result($result, $i, 0);
258                             $name = mysql_result($result, $i, 1);
259                             $type = mysql_result($result, $i, 2);
260                             $url = mysql_result($result, $i, 3);
261                             $desc = mysql_result($result, $i, 4);
262                             if (array_key_exists($url, $results)) {
263                                 list($r,$t,$m,$d,$w,$u) = $results[$url];
264                                 $results[$name] = array(($r + $relevance) * 2,
265                                                         $t,$m,$d,$w,$u);
266                             } else {
267                                 $id = strtoupper($name);
268 //                              $m = strtolower($module);
269                                 $m = $module;
270                                 $u = str_replace(
271                         "http://mail.gnome.org/archives/xml/", "", $url);
272                                 $results[$url] = array($relevance,$type,
273                                                 $u, $desc, $name, $url);
274                             }
275                         }
276                         mysql_free_result($result);
277                     }
278                 }
279                 if (($scope == 'any') || ($scope == 'XSLT') ||
280                     ($scope == 'LISTS') || ($scope == 'XSLTLIST')) {
281                     list($result, $j) = XSLTqueryArchiveWord($word);
282                     if ($j > 0) {
283                         for ($i = 0; $i < $j; $i++) {
284                             $relevance = mysql_result($result, $i, 0);
285                             $name = mysql_result($result, $i, 1);
286                             $type = mysql_result($result, $i, 2);
287                             $url = mysql_result($result, $i, 3);
288                             $desc = mysql_result($result, $i, 4);
289                             if (array_key_exists($url, $results)) {
290                                 list($r,$t,$m,$d,$w,$u) = $results[$url];
291                                 $results[$name] = array(($r + $relevance) * 2,
292                                                         $t,$m,$d,$w,$u);
293                             } else {
294                                 $id = strtoupper($name);
295 //                              $m = strtolower($module);
296                                 $m = $module;
297                                 $u = str_replace(
298                         "http://mail.gnome.org/archives/xslt/", "", $url);
299                                 $results[$url] = array($relevance,$type,
300                                                 $u, $desc, $name, $url);
301                             }
302                         }
303                         mysql_free_result($result);
304                     }
305                 }
306                 if (($j <= 0) && ($k <= 0)) {
307                     echo "<p> No result found for $word\n";
308                 }
309             }
310             mysql_close($link);
311             $nb = count($results);
312             echo "<h3 align='center'>Found $nb results for query $query</h3>\n";
313             usort($results, "resSort");
314
315             if ($nb > 0) {
316                 printf("<table><tbody>\n");
317                 printf("<tr><td>Quality</td><td>Symbol</td><td>Type</td><td>module</td><td>Description</td></tr>\n");
318                 $i = 0;
319                 while (list ($name, $val) = each ($results)) {
320                     list($r,$t,$m,$d,$s,$u) = $val;
321                     $m = str_replace("<", "&lt;", $m);
322                     $s = str_replace("<", "&lt;", $s);
323                     $d = str_replace("<", "&lt;", $d);
324                     echo "<tr><td>$r</td><td><a href='$u'>$s</a></td><td>$t</td><td>$m</td><td>$d</td></tr>";
325                     $i = $i + 1;
326                     if ($i > 75)
327                         break;
328                 }
329                 printf("</tbody></table>\n");
330             }
331         }
332     }
333 ?>