-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.class.php
More file actions
70 lines (63 loc) · 2.32 KB
/
Search.class.php
File metadata and controls
70 lines (63 loc) · 2.32 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
<?php
// The Search class takes responsibility for indexing and searching of Post objects.
// Search makes use of plain text indexing in a separate index folder. It takes the
// raw data from the Post object and its Comments and dumps it to a file with the name
// of the id of the Post. It is not possible to index a Post for Search unless it has
// an id, so the Post must be stored in the database.
class Search {
// Create searchable index of all Posts
public function indexAll() {
$posts = Post::getAll();
foreach($posts as $post) {
self::index($post);
}
}
// Create searchable index of given Post object
public function index($post) {
if(isset($post->id)) {
if(!is_dir('index')) mkdir('index');
$index = opendir('index');
// Create the search object
foreach ($post as $name=>$val) {
$content = strip_tags($val);
$content = strtolower($content);
if(isset($searchobject))
$searchobject = array_merge($searchobject, preg_split("/[\s,]+/", $content));
else
$searchobject = preg_split("/[\s,]+/", $content);
}
// Indexing the comments...
foreach ($post->comments() as $comment) {
$content = strip_tags($comment->comment);
$content = strtolower($content);
$searchobject = array_merge($searchobject, preg_split("/[\s,]+/", $content));
$content = strip_tags($comment->title);
$content = strtolower($content);
$searchobject = array_merge($searchobject, preg_split("/[\s,]+/", $content));
}
$pif = fopen("index/$post->id", 'w'); //post-index-file
fwrite($pif, join(" ", $searchobject));
fclose($pif);
closedir($index);
return true;
}
throw new Exception("Can't index a Post without an id.");
}
// Find a post by given keywords
public function find($string="") {
if(!is_dir('index')) throw new Exception("No search index.");
$index = scandir('index');
$hits = array();
foreach ($index as $pif) {
if($pif != '.' and $pif != '..') {
$content = file_get_contents("index/".$pif);
$string = strtolower($string);
if(preg_match("/$string/", $content)) $hits[] = $pif;
}
}
if(empty($hits)) return false;
$hits = Post::getAll($hits);
return $hits;
}
}
?>