~helene-verhaeghe27/cairo-dock-core/bugfix

« back to all changes in this revision

Viewing changes to doc/html/search.php

  • Committer: fabounet
  • Date: 2008-11-14 01:51:17 UTC
  • Revision ID: vcs-imports@canonical.com-20081114015117-854dznkw3lfva52x
The commit of the year \!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
2
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
 
3
<title>Recherche</title>
 
4
<link href="doxygen.css" rel="stylesheet" type="text/css">
 
5
<link href="tabs.css" rel="stylesheet" type="text/css">
 
6
</head><body>
 
7
<!-- Généré par Doxygen 1.5.3 -->
 
8
<div class="tabs">
 
9
  <ul>
 
10
    <li><a href="index.html"><span>Page&nbsp;principale</span></a></li>
 
11
    <li><a href="annotated.html"><span>Structures&nbsp;de&nbsp;données</span></a></li>
 
12
    <li><a href="files.html"><span>Fichiers</span></a></li>
 
13
    <li>
 
14
      <form action="search.php" method="get">
 
15
        <table cellspacing="0" cellpadding="0" border="0">
 
16
          <tr>
 
17
            <td><label>&nbsp;Rechercher&nbsp;</label></td>
 
18
 
 
19
<?php
 
20
 
 
21
function search_results()
 
22
{
 
23
  return "Résultats de la recherche";
 
24
}
 
25
 
 
26
function matches_text($num)
 
27
{
 
28
  if ($num==0)
 
29
  {
 
30
    return "Désolé aucun document ne correspond à votre requête.";
 
31
  }
 
32
  else if ($num==1)
 
33
  {
 
34
    return "Trouvé <b>1</b> document correspondant à votre requête.";
 
35
  }
 
36
  else // $num>1
 
37
  {
 
38
    return "Trouvé  <b>$num</b> documents correspondant à votre requête. Classé par ordre de pertinence décroissant.";
 
39
  }
 
40
}
 
41
 
 
42
function report_matches()
 
43
{
 
44
  return "Correspondances : ";
 
45
}
 
46
function end_form($value)
 
47
{
 
48
  echo "            <td><input type=\"text\" name=\"query\" value=\"$value\" size=\"20\" accesskey=\"s\"/></td>\n          </tr>\n        </table>\n      </form>\n    </li>\n  </ul>\n</div>\n";
 
49
}
 
50
 
 
51
function readInt($file)
 
52
{
 
53
  $b1 = ord(fgetc($file)); $b2 = ord(fgetc($file));
 
54
  $b3 = ord(fgetc($file)); $b4 = ord(fgetc($file));
 
55
  return ($b1<<24)|($b2<<16)|($b3<<8)|$b4;
 
56
}
 
57
 
 
58
function readString($file)
 
59
{
 
60
  $result="";
 
61
  while (ord($c=fgetc($file))) $result.=$c;
 
62
  return $result;
 
63
}
 
64
 
 
65
function readHeader($file)
 
66
{
 
67
  $header =fgetc($file); $header.=fgetc($file);
 
68
  $header.=fgetc($file); $header.=fgetc($file);
 
69
  return $header;
 
70
}
 
71
 
 
72
function computeIndex($word)
 
73
{
 
74
  // Fast string hashing
 
75
  //$lword = strtolower($word);
 
76
  //$l = strlen($lword);
 
77
  //for ($i=0;$i<$l;$i++)
 
78
  //{
 
79
  //  $c = ord($lword{$i});
 
80
  //  $v = (($v & 0xfc00) ^ ($v << 6) ^ $c) & 0xffff;
 
81
  //}
 
82
  //return $v;
 
83
 
 
84
  // Simple hashing that allows for substring search
 
85
  if (strlen($word)<2) return -1;
 
86
  // high char of the index
 
87
  $hi = ord($word{0});
 
88
  if ($hi==0) return -1;
 
89
  // low char of the index
 
90
  $lo = ord($word{1});
 
91
  if ($lo==0) return -1;
 
92
  // return index
 
93
  return $hi*256+$lo;
 
94
}
 
95
 
 
96
function search($file,$word,&$statsList)
 
97
{
 
98
  $index = computeIndex($word);
 
99
  if ($index!=-1) // found a valid index
 
100
  {
 
101
    fseek($file,$index*4+4); // 4 bytes per entry, skip header
 
102
    $index = readInt($file);
 
103
    if ($index) // found words matching the hash key
 
104
    {
 
105
      $start=sizeof($statsList);
 
106
      $count=$start;
 
107
      fseek($file,$index);
 
108
      $w = readString($file);
 
109
      while ($w)
 
110
      {
 
111
        $statIdx = readInt($file);
 
112
        if ($word==substr($w,0,strlen($word)))
 
113
        { // found word that matches (as substring)
 
114
          $statsList[$count++]=array(
 
115
              "word"=>$word,
 
116
              "match"=>$w,
 
117
              "index"=>$statIdx,
 
118
              "full"=>strlen($w)==strlen($word),
 
119
              "docs"=>array()
 
120
              );
 
121
        }
 
122
        $w = readString($file);
 
123
      }
 
124
      $totalHi=0;
 
125
      $totalFreqHi=0;
 
126
      $totalFreqLo=0;
 
127
      for ($count=$start;$count<sizeof($statsList);$count++)
 
128
      {
 
129
        $statInfo = &$statsList[$count];
 
130
        $multiplier = 1;
 
131
        // whole word matches have a double weight
 
132
        if ($statInfo["full"]) $multiplier=2;
 
133
        fseek($file,$statInfo["index"]); 
 
134
        $numDocs = readInt($file);
 
135
        $docInfo = array();
 
136
        // read docs info + occurrence frequency of the word
 
137
        for ($i=0;$i<$numDocs;$i++)
 
138
        {
 
139
          $idx=readInt($file); 
 
140
          $freq=readInt($file); 
 
141
          $docInfo[$i]=array("idx"  => $idx,
 
142
                             "freq" => $freq>>1,
 
143
                             "rank" => 0.0,
 
144
                             "hi"   => $freq&1
 
145
                            );
 
146
          if ($freq&1) // word occurs in high priority doc
 
147
          {
 
148
            $totalHi++;
 
149
            $totalFreqHi+=$freq*$multiplier;
 
150
          }
 
151
          else // word occurs in low priority doc
 
152
          {
 
153
            $totalFreqLo+=$freq*$multiplier;
 
154
          }
 
155
        }
 
156
        // read name and url info for the doc
 
157
        for ($i=0;$i<$numDocs;$i++)
 
158
        {
 
159
          fseek($file,$docInfo[$i]["idx"]);
 
160
          $docInfo[$i]["name"]=readString($file);
 
161
          $docInfo[$i]["url"]=readString($file);
 
162
        }
 
163
        $statInfo["docs"]=$docInfo;
 
164
      }
 
165
      $totalFreq=($totalHi+1)*$totalFreqLo + $totalFreqHi;
 
166
      for ($count=$start;$count<sizeof($statsList);$count++)
 
167
      {
 
168
        $statInfo = &$statsList[$count];
 
169
        $multiplier = 1;
 
170
        // whole word matches have a double weight
 
171
        if ($statInfo["full"]) $multiplier=2;
 
172
        for ($i=0;$i<sizeof($statInfo["docs"]);$i++)
 
173
        {
 
174
          $docInfo = &$statInfo["docs"];
 
175
          // compute frequency rank of the word in each doc
 
176
          $freq=$docInfo[$i]["freq"];
 
177
          if ($docInfo[$i]["hi"])
 
178
          {
 
179
            $statInfo["docs"][$i]["rank"]=
 
180
              (float)($freq*$multiplier+$totalFreqLo)/$totalFreq;
 
181
          }
 
182
          else
 
183
          {
 
184
            $statInfo["docs"][$i]["rank"]=
 
185
              (float)($freq*$multiplier)/$totalFreq;
 
186
          }
 
187
        }
 
188
      }
 
189
    }
 
190
  }
 
191
  return $statsList;
 
192
}
 
193
 
 
194
function combine_results($results,&$docs)
 
195
{
 
196
  foreach ($results as $wordInfo)
 
197
  {
 
198
    $docsList = &$wordInfo["docs"];
 
199
    foreach ($docsList as $di)
 
200
    {
 
201
      $key=$di["url"];
 
202
      $rank=$di["rank"];
 
203
      if (in_array($key, array_keys($docs)))
 
204
      {
 
205
        $docs[$key]["rank"]+=$rank;
 
206
      }
 
207
      else
 
208
      {
 
209
        $docs[$key] = array("url"=>$key,
 
210
            "name"=>$di["name"],
 
211
            "rank"=>$rank
 
212
            );
 
213
      }
 
214
      $docs[$key]["words"][] = array(
 
215
               "word"=>$wordInfo["word"],
 
216
               "match"=>$wordInfo["match"],
 
217
               "freq"=>$di["freq"]
 
218
               );
 
219
    }
 
220
  }
 
221
  return $docs;
 
222
}
 
223
 
 
224
function filter_results($docs,&$requiredWords,&$forbiddenWords)
 
225
{
 
226
  $filteredDocs=array();
 
227
  while (list ($key, $val) = each ($docs)) 
 
228
  {
 
229
    $words = &$docs[$key]["words"];
 
230
    $copy=1; // copy entry by default
 
231
    if (sizeof($requiredWords)>0)
 
232
    {
 
233
      foreach ($requiredWords as $reqWord)
 
234
      {
 
235
        $found=0;
 
236
        foreach ($words as $wordInfo)
 
237
        { 
 
238
          $found = $wordInfo["word"]==$reqWord;
 
239
          if ($found) break;
 
240
        }
 
241
        if (!$found) 
 
242
        {
 
243
          $copy=0; // document contains none of the required words
 
244
          break;
 
245
        }
 
246
      }
 
247
    }
 
248
    if (sizeof($forbiddenWords)>0)
 
249
    {
 
250
      foreach ($words as $wordInfo)
 
251
      {
 
252
        if (in_array($wordInfo["word"],$forbiddenWords))
 
253
        {
 
254
          $copy=0; // document contains a forbidden word
 
255
          break;
 
256
        }
 
257
      }
 
258
    }
 
259
    if ($copy) $filteredDocs[$key]=$docs[$key];
 
260
  }
 
261
  return $filteredDocs;
 
262
}
 
263
 
 
264
function compare_rank($a,$b)
 
265
{
 
266
  if ($a["rank"] == $b["rank"]) 
 
267
  {
 
268
    return 0;
 
269
  }
 
270
  return ($a["rank"]>$b["rank"]) ? -1 : 1; 
 
271
}
 
272
 
 
273
function sort_results($docs,&$sorted)
 
274
{
 
275
  $sorted = $docs;
 
276
  usort($sorted,"compare_rank");
 
277
  return $sorted;
 
278
}
 
279
 
 
280
function report_results(&$docs)
 
281
{
 
282
  echo "<table cellspacing=\"2\">\n";
 
283
  echo "  <tr>\n";
 
284
  echo "    <td colspan=\"2\"><h2>".search_results()."</h2></td>\n";
 
285
  echo "  </tr>\n";
 
286
  $numDocs = sizeof($docs);
 
287
  if ($numDocs==0)
 
288
  {
 
289
    echo "  <tr>\n";
 
290
    echo "    <td colspan=\"2\">".matches_text(0)."</td>\n";
 
291
    echo "  </tr>\n";
 
292
  }
 
293
  else
 
294
  {
 
295
    echo "  <tr>\n";
 
296
    echo "    <td colspan=\"2\">".matches_text($numDocs);
 
297
    echo "\n";
 
298
    echo "    </td>\n";
 
299
    echo "  </tr>\n";
 
300
    $num=1;
 
301
    foreach ($docs as $doc)
 
302
    {
 
303
      echo "  <tr>\n";
 
304
      echo "    <td align=\"right\">$num.</td>";
 
305
      echo     "<td><a class=\"el\" href=\"".$doc["url"]."\">".$doc["name"]."</a></td>\n";
 
306
      echo "  <tr>\n";
 
307
      echo "    <td></td><td class=\"tiny\">".report_matches()." ";
 
308
      foreach ($doc["words"] as $wordInfo)
 
309
      {
 
310
        $word = $wordInfo["word"];
 
311
        $matchRight = substr($wordInfo["match"],strlen($word));
 
312
        echo "<b>$word</b>$matchRight(".$wordInfo["freq"].") ";
 
313
      }
 
314
      echo "    </td>\n";
 
315
      echo "  </tr>\n";
 
316
      $num++;
 
317
    }
 
318
  }
 
319
  echo "</table>\n";
 
320
}
 
321
 
 
322
function main()
 
323
{
 
324
  if(strcmp('4.1.0', phpversion()) > 0) 
 
325
  {
 
326
    die("Error: PHP version 4.1.0 or above required!");
 
327
  }
 
328
  if (!($file=fopen("search.idx","rb"))) 
 
329
  {
 
330
    die("Error: Search index file could NOT be opened!");
 
331
  }
 
332
  if (readHeader($file)!="DOXS")
 
333
  {
 
334
    die("Error: Header of index file is invalid!");
 
335
  }
 
336
  $query="";
 
337
  if (array_key_exists("query", $_GET))
 
338
  {
 
339
    $query=$_GET["query"];
 
340
  }
 
341
  end_form($query);
 
342
  echo "&nbsp;\n<div class=\"searchresults\">\n";
 
343
  $results = array();
 
344
  $requiredWords = array();
 
345
  $forbiddenWords = array();
 
346
  $foundWords = array();
 
347
  $word=strtok($query," ");
 
348
  while ($word) // for each word in the search query
 
349
  {
 
350
    if (($word{0}=='+')) { $word=substr($word,1); $requiredWords[]=$word; }
 
351
    if (($word{0}=='-')) { $word=substr($word,1); $forbiddenWords[]=$word; }
 
352
    if (!in_array($word,$foundWords))
 
353
    {
 
354
      $foundWords[]=$word;
 
355
      search($file,strtolower($word),$results);
 
356
    }
 
357
    $word=strtok(" ");
 
358
  }
 
359
  $docs = array();
 
360
  combine_results($results,$docs);
 
361
  // filter out documents with forbidden word or that do not contain
 
362
  // required words
 
363
  $filteredDocs = filter_results($docs,$requiredWords,$forbiddenWords);
 
364
  // sort the results based on rank
 
365
  $sorted = array();
 
366
  sort_results($filteredDocs,$sorted);
 
367
  // report results to the user
 
368
  report_results($sorted);
 
369
  echo "</div>\n";
 
370
  fclose($file);
 
371
}
 
372
 
 
373
main();
 
374
 
 
375
 
 
376
?>
 
377
<hr size="1"><address style="text-align: right;"><small>Généré le Wed Apr 16 03:27:16 2008 pour Cairo-Dock par&nbsp;
 
378
<a href="http://www.doxygen.org/index.html">
 
379
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.3 </small></address>
 
380
</body>
 
381
</html>