forked from bolt/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.php
More file actions
135 lines (89 loc) · 3.02 KB
/
search.php
File metadata and controls
135 lines (89 loc) · 3.02 KB
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
<?php
require_once './vendor/autoload.php';
use Symfony\Component\Finder\Finder;
$searcher = new search();
echo $searcher->find();
// --------
class Search
{
private $results = array();
private $titles = array();
private $items = array();
public function find()
{
// Set $q
if (isset($_GET['q'])) {
$q = $_GET['q'];
} else {
$q = "";
}
// Split on words.
$q = preg_split("/(?<=\w)\b\s*/", $q);
foreach ($q as $atom) {
$this->searchWord($atom);
}
arsort($this->results);
// \Dumper::dump($this->results);
foreach ($this->results as $page => $score) {
$this->items[] = array('id' => $page, 'text' => $this->titles[$page]);
}
return json_encode(array("items" => $this->items));
}
private function searchWord($q)
{
if (strlen($q) < 3) {
return;
}
$finder = new Finder();
$finder->files()->in(__DIR__."/source")->contains('/' . $q . '/i');
foreach ($finder as $file) {
$filename = str_replace(".md", "", $file->getRelativePathname());
$contents = file_get_contents($file->getRealpath());
$score = $this->weighQueryText($contents, $q, $filename);
// Assume first line is the title.
$title = strtok($contents, "\n");
// Add the 'folder' to the title, perhaps
if (!empty($file->getRelativePath())) {
$title = ucfirst($file->getRelativePath()) . " - " . $title;
}
$this->titles[$filename] = $title;
}
}
/**
* Weight a text part relative to some other part
*
* @param string $subject The subject to search in.
* @param string $complete The complete search term (lowercased).
* @return integer The weight
*/
private function weighQueryText($contents, $q, $filename)
{
$contents = mb_strtolower(trim($contents));
// echo "<br>filename: $filename - ";
$score = $this->findScore($contents, $q);
if (!isset($results[$filename])) {
$this->results[$filename] = 0;
}
$this->results[$filename] += $score;
return $score;
}
private function findScore($contents, $q)
{
$score = 0;
$length = strlen($contents);
while (strpos($contents, $q) !== false) {
$pos = strpos($contents, $q);
$score += pow( (($length - $pos) / $length), 2);
// echo "[x]";
$contents = $this->str_replace_once($q, " ", $contents);
}
return $score;
}
private function str_replace_once($str_pattern, $str_replacement, $string){
if (strpos($string, $str_pattern) !== false){
$occurrence = strpos($string, $str_pattern);
return substr_replace($string, $str_replacement, strpos($string, $str_pattern), strlen($str_pattern));
}
return $string;
}
}