This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphinxql.php
115 lines (89 loc) · 2.71 KB
/
sphinxql.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
<?php
## plugin sphinxql
defined('_SPHINXQL_HOST') || define('_SPHINXQL_HOST', '127.0.0.1');
defined('_SPHINXQL_MODE') || define('_SPHINXQL_MODE', 'sphinxql');
defined('_SPHINXQL_INDEX') || define('_SPHINXQL_INDEX', 'testrt');
defined('SPHINX_SERVER_PORT') || define('SPHINX_SERVER_PORT', 9306);
/**
* Démarrer une connexion sur le serveur sphinxql
*/
function sphinxql_connect() {
static $connection;
if (!isset($connection)) $connection = mysqli_connect(_SPHINXQL_HOST, null, null, null, SPHINX_SERVER_PORT);
return $connection;
}
/**
* Lancer une requête sur le serveur sphinxql
*
* @param string $query
* Chaîne contenant la requête à lancer
*/
function sphinxql_query($query) {
// tester ou etablir la connexion SphinxQL
if (!$connection = sphinxql_connect()) return false;
// envoyer la query
$res = mysqli_query($connection, $query);
if (!$res) {
echo $query;
var_dump( mysqli_error($connection) );
exit;
}
// etablir le succès (renvoie true/false ou une ressource MySQL si SELECT)
return $res;
}
function sphinxql_indexer_document($doc = array()) {
if (_SPHINXQL_MODE == 'sphinxql')
sphinxql_query('REPLACE INTO '._SPHINXQL_INDEX.' … ');
if (_SPHINXQL_MODE == 'xmlpipe')
echo sphinxql_xml($doc);
return true; // succès
}
function sphinxql_documents( $ids = array() ) {
return $documents;
}
function sphinxql_indexer_documents( $ids = array() ) {
$documents = sphinxql_documents($ids);
foreach($documents as $doc) {
sphinxql_indexer_document($doc);
}
}
function sphinxql_indexer_tout() {
// lister les docs
// appeler par batch
}
function sphinxql_allfetsel($select = null, $from=null, $where=null) {
$docs = $meta = array();
if (is_null($from)){
$from = _SPHINXQL_INDEX;
}
// Lancer la requête demandée
$a = sphinxql_query("SELECT $select FROM $from where $where");
// Récupérer les documents trouvés
while($t = mysqli_fetch_array($a, MYSQLI_ASSOC)) {
$docs[] = $t;
}
// Récupérer les méta-informations des résultats
$a = sphinxql_query("SHOW META");
while($t = mysqli_fetch_array($a, MYSQLI_ASSOC)) {
$meta[] = $t;
}
return array('docs' => $docs, 'meta'=>$meta);
}
function sphinxql_escape_query($t) {
if (is_array($t))
return array_map('sphinxql_escape_query', $t);
return "'".mysqli_real_escape_string(sphinxql_connect(),$t)."'";
}
function sphinxql_shorten($text) {
return mb_substr(preg_replace("/\s+/", " ", trim(strip_tags($text))),0,60);
}
function sphinxql_show_results($res) {
array_unshift($res['docs'], array_keys($res['docs'][0]));
foreach ($res['docs'] as $doc) {
echo '| '.join(' | ', array_map('sphinxql_shorten',$doc))." |\n";
}
echo "---------\n";
foreach ($res['meta'] as $meta) {
echo join(':', $meta)."\n";
}
}