-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.php
129 lines (108 loc) · 3.46 KB
/
search.php
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
<?php
class TextPressSearch
{
/**
* @brief Output
* Display field and results
*/
public function displayfield()
{
$q = "";
$list = "";
if(isset($_REQUEST["q"])) {
$q = $_REQUEST["q"];
$results = $this->search_in_dir("articles",$q);
$list = '<ul>';
foreach($results as $post)
$list .= "<li><a href=". $post["url"].">". $post['title'] ."</a></li>";
$list .= '</ul>';
}
$html = <<<EOT
<form method=GET >
<input name=q size=15 maxlength=255 value="$q">
<input type=submit value="Search">
</form>
EOT;
$html .= $list;
return $html;
}
/**
* @brief Main Search Function
* @return $results arr (array(title,url))
*/
public function search_in_dir( $dir, $str )
{
$results = array();
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $filename)
$files[] = $filename;
foreach( $files as $k => $file )
{
$source = file_get_contents( $file );
if( stripos( $source, $str ) !== false ) {
$data=$this->extractDataFromSource($source);
$results[]=$data;
} else
unset( $files[$k] );
}
return array_filter($results);
}
/**
* @brief
* Get Url and Title from TextPress Article
* @return arr(title=>,url=>)
*/
public function extractDataFromSource($content)
{
$content = str_replace("\r\n", "\n", $content);
$content = str_replace("\r", "\n", $content);
$content = preg_replace("/\n{2,}/", "\n\n", $content);
$sections = explode("\n\n", $content);
$meta = json_decode(array_shift($sections),true);
$slug = (array_key_exists('slug', $meta) && $meta['slug'] !='')
? $meta['slug']
: $this->slugize($meta['title']);
$article = array(
'title' => $meta['title'],
'url'=>$this->getArticleUrl($meta['date'],$slug)
);
return $article;
}
/**
* @brief Contruct Textpress Article Link
* Could use Textpress class if it's available
*/
public function getArticleUrl($date,$slug)
{
$date = new \DateTime($date);
$date = $date->format('Y-m-d');
$dateSplit = explode('-', $date);
$url = "/" . $dateSplit[0] .
"/" . $dateSplit[1] .
"/" . $dateSplit[2] .
"/" . $slug;
return $url;
}
/**
* @brief Create a Slug
* Could use Textpress class if it's available
*/
public function slugize($str)
{
$str = strtolower(trim($str));
$chars = array("ä", "ö", "ü", "ß");
$replacements = array("ae", "oe", "ue", "ss");
$str = str_replace($chars, $replacements, $str);
$pattern = array("/(é|è|ë|ê)/", "/(ó|ò|ö|ô)/", "/(ú|ù|ü|û)/");
$replacements = array("e", "o", "u");
$str = preg_replace($pattern, $replacements, $str);
$pattern = array(":", "!", "?", ".", "/", "'");
$str = str_replace($pattern, "", $str);
$pattern = array("/[^a-z0-9-]/", "/-+/");
$str = preg_replace($pattern, "-", $str);
return $str;
}
}
$h= new TextPressSearch();
?>
<h3>Search</h3>
<?php echo $h->displayfield() ?>