aboutsummaryrefslogtreecommitdiffhomepage
path: root/doc/ref/csharp/html/SearchHelp.inc.php
blob: b905e130cfd995c034a569df15812d682c926c1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?
// Contributed to the Sandcastle Help File Builder project by Thomas Levesque

class Ranking
{
    public $filename;
    public $pageTitle;
    public $rank;

    function __construct($file, $title, $rank)
    {
        $this->filename = $file;
        $this->pageTitle = $title;
        $this->rank = $rank;
    }
}


/// <summary>
/// Split the search text up into keywords
/// </summary>
/// <param name="keywords">The keywords to parse</param>
/// <returns>A list containing the words for which to search</returns>
function ParseKeywords($keywords)
{
    $keywordList = array();
    $words = preg_split("/[^\w]+/", $keywords);

    foreach($words as $word)
    {
        $checkWord = strtolower($word);
        $first = substr($checkWord, 0, 1);
        if(strlen($checkWord) > 2 && !ctype_digit($first) && !in_array($checkWord, $keywordList))
        {
            array_push($keywordList, $checkWord);
        }
    }

    return $keywordList;
}


/// <summary>
/// Search for the specified keywords and return the results as a block of
/// HTML.
/// </summary>
/// <param name="keywords">The keywords for which to search</param>
/// <param name="fileInfo">The file list</param>
/// <param name="wordDictionary">The dictionary used to find the words</param>
/// <param name="sortByTitle">True to sort by title, false to sort by
/// ranking</param>
/// <returns>A block of HTML representing the search results.</returns>
function Search($keywords, $fileInfo, $wordDictionary, $sortByTitle)
{
    $sb = "<ol>";
    $matches = array();
    $matchingFileIndices = array();
    $rankings = array();

    $isFirst = true;

    foreach($keywords as $word)
    {
        if (!array_key_exists($word, $wordDictionary))
        {
            return "<strong>Nothing found</strong>";
        }
        $occurrences = $wordDictionary[$word];

        $matches[$word] = $occurrences;
        $occurrenceIndices = array();

        // Get a list of the file indices for this match
        foreach($occurrences as $entry)
            array_push($occurrenceIndices, ($entry >> 16));

        if($isFirst)
        {
            $isFirst = false;
            foreach($occurrenceIndices as $i)
            {
                array_push($matchingFileIndices, $i);
            }
        }
        else
        {
            // After the first match, remove files that do not appear for
            // all found keywords.
            for($idx = 0; $idx < count($matchingFileIndices); $idx++)
            {
                if (!in_array($matchingFileIndices[$idx], $occurrenceIndices))
                {
                    array_splice($matchingFileIndices, $idx, 1);
                    $idx--;
                }
            }
        }
    }

    if(count($matchingFileIndices) == 0)
    {
        return "<strong>Nothing found</strong>";
    }

    // Rank the files based on the number of times the words occurs
    foreach($matchingFileIndices as $index)
    {
        // Split out the title, filename, and word count
        $fileIndex = explode("\x00", $fileInfo[$index]);

        $title = $fileIndex[0];
        $filename = $fileIndex[1];
        $wordCount = intval($fileIndex[2]);
        $matchCount = 0;

        foreach($keywords as $words)
        {
            $occurrences = $matches[$word];

            foreach($occurrences as $entry)
            {
                if(($entry >> 16) == $index)
                    $matchCount += $entry & 0xFFFF;
            }
        }

        $r = new Ranking($filename, $title, $matchCount * 1000 / $wordCount);
        array_push($rankings, $r);

        if(count($rankings) > 99)
            break;
    }

    // Sort by rank in descending order or by page title in ascending order
    if($sortByTitle)
    {
        usort($rankings, "cmprankbytitle");
    }
    else
    {
        usort($rankings, "cmprank");
    }

    // Format the file list and return the results
    foreach($rankings as $r)
    {
        $f = $r->filename;
        $t = $r->pageTitle;
        $sb .= "<li><a href=\"$f\" target=\"_blank\">$t</a></li>";
    }

    $sb .= "</ol";

    if(count($rankings) < count($matchingFileIndices))
    {
        $c = count(matchingFileIndices) - count(rankings);
        $sb .= "<p>Omitted $c more results</p>";
    }

    return $sb;
}

function cmprank($x, $y)
{
    return $y->rank - $x->rank;
}

function cmprankbytitle($x, $y)
{
    return strcmp($x->pageTitle, $y->pageTitle);
}

?>