Skip to content

Commit 38325f1

Browse files
committed
Merge remote-tracking branch 'mark/elastic-search'
2 parents ea2a329 + 44adfc0 commit 38325f1

File tree

8 files changed

+9314
-236
lines changed

8 files changed

+9314
-236
lines changed

scripts/populate_search_index.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env php
2+
<?php
3+
/**
4+
* Utility script to populate the elastic search indexes
5+
*
6+
*/
7+
8+
// Elastic search config
9+
define('ES_URL', 'http://localhost:9200');
10+
define('ES_INDEX', 'documentation');
11+
12+
13+
function main($argv) {
14+
if (empty($argv[1])) {
15+
echo "A language to scan is required.\n";
16+
exit(1);
17+
}
18+
$lang = $argv[1];
19+
20+
$directory = new RecursiveDirectoryIterator($lang);
21+
$recurser = new RecursiveIteratorIterator($directory);
22+
$matcher = new RegexIterator($recurser, '/\.rst/');
23+
24+
foreach ($matcher as $file) {
25+
updateIndex($lang, $file);
26+
}
27+
echo "\nIndex update complete\n";
28+
}
29+
30+
function updateIndex($lang, $file) {
31+
$fileData = readFileData($file);
32+
$filename = $file->getPathName();
33+
list($filename) = explode('.', $filename);
34+
35+
$path = $filename . '.html';
36+
$id = str_replace($lang . '/', '', $filename);
37+
$id = str_replace('/', '-', $id);
38+
$id = trim($id, '-');
39+
40+
$url = implode('/', array(ES_URL, ES_INDEX, $lang, $id));
41+
42+
$data = array(
43+
'contents' => $fileData['contents'],
44+
'title' => $fileData['title'],
45+
'url' => $path,
46+
);
47+
48+
$data = json_encode($data);
49+
$size = strlen($data);
50+
51+
$fh = fopen('php://memory', 'rw');
52+
fwrite($fh, $data);
53+
rewind($fh);
54+
55+
echo "Sending request:\n\tfile: $file\n\turl: $url\n";
56+
57+
$ch = curl_init($url);
58+
curl_setopt($ch, CURLOPT_PUT, true);
59+
curl_setopt($ch, CURLOPT_INFILE, $fh);
60+
curl_setopt($ch, CURLOPT_INFILESIZE, $size);
61+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
62+
63+
$response = curl_exec($ch);
64+
$metadata = curl_getinfo($ch);
65+
66+
if ($metadata['http_code'] > 400) {
67+
echo "[ERROR] Failed to complete request.\n";
68+
var_dump($response);
69+
exit(2);
70+
}
71+
72+
curl_close($ch);
73+
fclose($fh);
74+
75+
echo "Sent $file\n";
76+
}
77+
78+
function readFileData($file) {
79+
$contents = file_get_contents($file);
80+
81+
// extract the title and guess that things underlined with # or == and first in the file
82+
// are the title.
83+
preg_match('/^(.*)\n[=#]+\n/', $contents, $matches);
84+
$title = $matches[1];
85+
86+
// Remove the title from the indexed text.
87+
$contents = str_replace($matches[0], '', $contents);
88+
89+
// Remove title markers from the text.
90+
$contents = preg_replace('/\n[-=~]+\n/', '', $contents);
91+
92+
return compact('contents', 'title');
93+
}
94+
95+
main($argv);

themes/cakephp/layout.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
{% block extrahead %}
77
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
8+
<script type="text/javascript">
9+
window.lang = "{{ language }}";
10+
</script>
811
{% endblock %}
912

1013
{%- set script_files = script_files + ["_static/app.js", "_static/modernizr.foundation.js", "_static/foundation.js"] -%}

themes/cakephp/search.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{% extends "layout.html" %}
2+
3+
{% set title = _('Search') %}
4+
{% set script_files = script_files + ['_static/search.js'] %}
5+
6+
{% block body %}
7+
<h1 id="search-documentation">{{ _('Search') }}</h1>
8+
<div id="fallback" class="admonition warning">
9+
<script type="text/javascript">$('#fallback').hide();</script>
10+
<p>
11+
{% trans %}Please activate JavaScript to enable the search
12+
functionality.{% endtrans %}
13+
</p>
14+
</div>
15+
16+
<p>
17+
{% trans %}From here you can search these documents. Enter your search
18+
words into the box below and click "search". Note that the search
19+
function will automatically search for all of the words. Pages
20+
containing fewer words won't appear in the result list.{% endtrans %}
21+
</p>
22+
23+
<form action="" method="get" class="standalone-search">
24+
<input type="search" autocomplete="off" class="search-input" name="q" value="" />
25+
<input type="hidden" name="lang" value="{{ language }}" />
26+
<input type="submit" class="search-submit search-big button red" value="{{ _('search') }}" />
27+
</form>
28+
<div id="search-results">
29+
<ul>
30+
</ul>
31+
</div>
32+
<div id="pagination"></div>
33+
{% endblock %}

themes/cakephp/searchbox.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<div id="searchbox" class="columns nine phone-three">
1111

1212
<form id="searchform" class="search" action="{{ pathto('search') }}" method="get">
13-
<input class="search-input" type="search" name="q" size="18" />
13+
<input class="search-input" autocomplete="off" type="search" name="q" size="18" />
1414
<input class="search-submit search-big button red" type="submit" value="{{ _('Search') }}" />
1515
<input type="hidden" name="check_keywords" value="yes" />
1616
<input type="hidden" name="area" value="default" />

0 commit comments

Comments
 (0)