forked from cakephp/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate_search_index.php
executable file
·184 lines (156 loc) · 4.53 KB
/
populate_search_index.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
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
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env php
<?php
/**
* Utility script to populate the elastic search indexes
*
* Gets called by the Make file.
*/
// Elastic search config
define('ES_DEFAULT_HOST', 'https://ci.cakephp.org:9200');
define('ES_INDEX', 'cake-docs-40');
// file exclusion patterns
const FILE_EXCLUSIONS = [
'/404\.rst$/',
];
/**
* The main function
*
* Populates the search index for the given language.
*
* @param array $argv The array of CLI arguments, 1: language, 2. Elastic search host.
* @return void
*/
function main()
{
$options = getopt('', ['host::', 'lang:']);
if (empty($options['lang'])) {
echo "A language to scan is required.\n";
exit(1);
}
$lang = $options['lang'];
if (!empty($options['host'])) {
define('ES_HOST', $options['host']);
} else {
define('ES_HOST', ES_DEFAULT_HOST);
}
$directory = new RecursiveDirectoryIterator($lang);
$recurser = new RecursiveIteratorIterator($directory);
$matcher = new RegexIterator($recurser, '/\.rst/');
setMapping($lang);
foreach ($matcher as $file) {
$skip = false;
foreach (FILE_EXCLUSIONS as $exclusion) {
if (preg_match($exclusion, $file) === 1) {
echo "\nSkipping $file\n";
$skip = true;
break;
}
}
if (!$skip) {
updateIndex($lang, $file);
}
}
echo "\nIndex update complete\n";
}
function setMapping($lang)
{
echo "Creating index.\n";
$url = implode('/', array(ES_HOST, ES_INDEX . '-' . $lang));
doRequest($url, CURLOPT_PUT);
$mapping = [
"properties" => [
"contents" => ["type" => "text"],
"title" => ["type" => "keyword"],
"url" => [
"type" => "keyword",
"index" => false,
],
],
];
$data = json_encode(['mappings' => ['_doc' => $mapping]]);
echo "Updating mapping.\n";
$url = implode('/', array(ES_HOST, ES_INDEX . '-' . $lang, '_mapping', '_doc'));
doRequest($url, CURLOPT_PUT, $data);
}
/**
* Update the index for a given language
*
* @param string $lang The language to update, e.g. "en".
* @param RecursiveDirectoryIterator $file The file to load data from.
* @return void
*/
function updateIndex($lang, $file)
{
$fileData = readFileData($file);
$filename = $file->getPathName();
list($filename) = explode('.', $filename);
$path = $filename . '.html';
$id = str_replace($lang . '/', '', $filename);
$id = str_replace('/', '-', $id);
$id = trim($id, '-');
$url = implode('/', array(ES_HOST, ES_INDEX . '-' . $lang, '_doc', $id));
$data = json_encode([
'contents' => $fileData['contents'],
'title' => $fileData['title'],
'url' => $path,
]);
echo "Sending request:\n\tfile: $file\n\turl: $url\n";
doRequest($url, CURLOPT_PUT, $data);
echo "Sent $file\n";
}
/**
* Read data from file
*
* @param string $file The file to read.
* @return array The read data.
*/
function readFileData($file)
{
$contents = file_get_contents($file);
// Extract the title and guess that things underlined with # or == and first in the file
// are the title.
preg_match('/^(.*)\n[=#]+\n/', $contents, $matches);
$title = $matches[1];
// Remove the title from the indexed text.
$contents = str_replace($matches[0], '', $contents);
// Remove title markers from the text.
$contents = preg_replace('/\n[-=~]+\n/', '', $contents);
return compact('contents', 'title');
}
/**
* Send a request with curl. If the request fails the process will die.
*
* @param string $url
* @param int $method curl opt value for the method.
* @param string | null $body The body to send if necessary.
*/
function doRequest($url, $method, $body = null)
{
$ch = curl_init($url);
curl_setopt($ch, $method, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
]);
$fh = null;
if ($body) {
$size = strlen($body);
$fh = fopen('php://memory', 'rw');
fwrite($fh, $body);
rewind($fh);
curl_setopt($ch, CURLOPT_INFILE, $fh);
curl_setopt($ch, CURLOPT_INFILESIZE, $size);
}
$response = curl_exec($ch);
$metadata = curl_getinfo($ch);
if ($metadata['http_code'] > 400 || !$metadata['http_code']) {
echo "[ERROR] Failed to complete request.\n";
var_dump($response);
exit(2);
}
curl_close($ch);
if ($fh !== null) {
fclose($fh);
}
}
main();