-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAdminPanel.php
More file actions
230 lines (218 loc) · 7.78 KB
/
Copy pathAdminPanel.php
File metadata and controls
230 lines (218 loc) · 7.78 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
namespace VFou\Search;
use Exception;
use VFou\Search\Query\QueryBuilder;
use VFou\Search\Query\QuerySegment;
class AdminPanel
{
/**
* @var Engine $engine
*/
private $engine;
/**
* Admin constructor.
* @param array $configuration
* @throws Exception
*/
public function __construct($configuration = [])
{
$this->engine = new Engine($configuration);
$GLOBALS['vfou_admin'] = true;
$GLOBALS['vfou_baseurl'] = $_SERVER['SCRIPT_NAME'];
}
/**
* Runs the Admin Panel.
* You can provide an $uri parameter that'll override the default extraction from SERVER variable
* If provided, $baseUrl is the entry point of any generated url. Useful if you wrapped the panel into a framework for example.
* By default, $_SERVER['SCRIPT_NAME'] is used
* @param null $uri
* @return false|string
* @throws Exception
*/
public function run($uri = null, $baseUrl = null)
{
ob_start();
if($uri == null){
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if(strpos($uri, $_SERVER['SCRIPT_NAME']) == 0) $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
}
if($baseUrl != null) {
$GLOBALS['vfou_baseurl'] = $baseUrl;
}
if(empty($uri)) $uri = '/';
include('templates/header.php');
switch($uri){
case '/':
$this->indexAction();
break;
case '/query':
$this->QueryAction();
break;
case '/edit':
$this->editAction();
break;
case '/types':
$this->typesAction();
break;
case '/schemas':
$this->schemasAction();
break;
case '/cache/clear':
$this->engine->getIndex()->clearCache();
header('location: '.$_SERVER['HTTP_REFERER']);
exit();
default:
header('location: '.$GLOBALS['vfou_baseurl'].'/');
exit();
}
include('templates/footer.php');
$output = ob_get_contents();
ob_clean();
ob_end_clean();
return $output;
}
/**
* Route : /
*/
private function indexAction(): void
{
$stats = $this->engine->getIndex()->getStats();
include('templates/index.php');
}
/**
* Route : /query
* Methods : GET
* Parameters :
* 'q' : current query
* 'limit' : maximum document count
* 'offset' : offset the results by this (for pagination purposes)
* 'facets' : Display listed facets (comma separated)
* 'connex' : Enable Connex Search
* @throws Exception
*/
private function QueryAction(): void
{
$q = $_GET['q'] ?? '';
$sw = microtime(true);
$segments = [];
$isFacetSearching = false;
foreach($_GET as $field=>$value){
if(strpos($field,'facet-') === 0){
$isFacetSearching = true;
$facetField = substr($field, 6);
$subSeg = [];
foreach($value as $v){
$subSeg[] = QuerySegment::exactSearch($facetField, $v);
}
$segments[] = QuerySegment::or($subSeg);
}
}
$query = new QueryBuilder(QuerySegment::search($q, QuerySegment::and($segments)));
$query->setLimit($_GET['limit'] ?? 10);
$query->setOffset($_GET['offset'] ?? 0);
if($_GET['connex'] ?? false) $query->enableConnex();
$facets = $_GET['facets'] ?? '';
if(!empty($facets)){
foreach(explode(',', $facets) as $facet){
$query->addFacet($facet);
}
}
if($isFacetSearching){
$results = $this->engine->search($query);
} else {
$results = $this->engine->search($q, $query->getFilters());
}
$sw = (microtime(true) - $sw) * 1000;
include('templates/results.php');
}
/**
* Route : /edit
* Methods : GET, POST
* Parameters :
* 'id' : document to find
* 'prefill' : prefill the content with this
* 'content' : JSON of a document's body
* 'delete' : document id to delete
* @throws Exception
*/
private function editAction()
{
$errors = [];
if(($_SERVER['REQUEST_METHOD'] ?? false) == 'POST'){
if (isset($_POST['delete'])) {
// deletes document
$this->engine->delete($_POST['delete']);
$this->engine->getIndex()->getDocument($_POST['delete']);
} elseif(isset($_POST['prefill'])) {
// we catch this here to prevent 'content' parameter to trigger
// by the way, we disable the 'id' parameter
$_GET['id'] = null;
} else {
// edit or create document
$_POST['content'] = empty($_POST['content'] ?? '') ? '{}' : $_POST['content'];
$content = json_decode($_POST['content'], true);
if($content === null){
$errors[] = "ERROR : Invalid Json";
} else {
// we parse DateTimes back to their Object
array_walk_recursive($content, function (&$elem) {
if (strpos($elem, '@@@DateTime:') === 0) {
$ts = substr($elem, 12);
$elem = new \DateTime();
$elem->setTimestamp($ts);
}
});
if (!isset($content['id'])) {
$errors[] = "ERROR : Document must have an 'id'";
}
if (!isset($content['type'])) {
$errors[] = "ERROR : Document must have a 'type'";
}
}
if (empty($errors)) {
// if everything's okay we create/update the document
try {
$this->engine->update($content);
} catch (\Throwable $exception) {
$errors[] = get_class($exception) . ": " . $exception->getMessage();
}
}
if (isset($content['id'])) $_GET['id'] = $content['id'];
}
}
$id = $_GET['id'] ?? null;
if(isset($id)){
// fetch document and converts Datetime to a string representation
$document = $this->engine->getIndex()->getDocument($id);
array_walk_recursive($document, function(&$elem) {
if(is_a($elem, \DateTime::class)) $elem = "@@@DateTime:".$elem->getTimestamp();
});
}
include('templates/edit.php');
}
/**
* Route : /types
* Methods : GET
* Parameters :
* 'type' : currently selected type
* 'text' : text to debug
*/
private function typesAction()
{
$types = $this->engine->getIndex()->getTypes();
if(!isset($_GET['type'])) $_GET['type'] = '_default';
$debugTokens = [];
if(!empty($_GET['text'])){
$debugTokens = $this->engine->getIndex()->tokenizeQuery($_GET['text'] ?? '', $_GET['type']);
}
include('templates/types.php');
}
/**
* Route : /schemas
*/
private function schemasAction(): void
{
$schemas = $this->engine->getIndex()->getSchemas();
include('templates/schemas.php');
}
}