Subversion Repositories Applications.papyrus

Rev

Rev 2110 | Rev 2117 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2103 drzraf 1
<?php
2
/*
3
 * Moteur de recherche SPHINX
4
 * @author        Raphaël Droz <raphael@tela-botanica.org
5
 * @copyright     Tela-Botanica 2013
6
 */
7
 
8
/*
9
  sudo urpmi lib64sphinxclient-devel
10
  sudo pecl install sphinx
11
 
12
  see also: http://www.ibm.com/developerworks/library/os-sphinx/
13
  see also: http://sphinxsearch.com/docs/manual-2.0.7.html#extended-syntax
14
  TODO: http://sphinxsearch.com/blog/2010/08/17/how-sphinx-relevance-ranking-works/
2113 drzraf 15
  TODO: attention, projet,bazaar,spip et papyrus sont latin9, ainsi en est-il de l'input attendu du <form>
16
  		mais pour coste... c'est full utf-8
2103 drzraf 17
*/
18
 
19
/* returned struct:
20
'poids' => 0,
21
'url' => '',
22
'titre' => '',
23
'hreflang' => '',
24
'accesskey' => '',
2105 drzraf 25
'title' => '', // balise 'title'
2103 drzraf 26
'date_creation' => '',
27
'description' => ''
28
*/
29
 
2113 drzraf 30
define('SPHINX_DSN', '127.0.0.1:9306');
31
 
2105 drzraf 32
// pour strftime()
33
date_default_timezone_set('Europe/Paris');
34
setlocale(LC_TIME, 'fr_FR');
35
 
2103 drzraf 36
class MoteurRecherche_SPIP {
2105 drzraf 37
	public function get($ids, $q = NULL) {
38
		if(!$ids) return array();
2103 drzraf 39
		$db = DB::connect($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['bdd_dsn']);
2105 drzraf 40
		$req = $db->query(sprintf(<<<EOF
41
SELECT id_article AS id, titre, texte, date AS date_creation, lang as hreflang
42
FROM spip_articles
43
WHERE statut = "%s"
44
AND id_article IN (%s)
45
EOF
46
								  ,
2103 drzraf 47
								  "publie",
48
								  implode(',', $ids)));
2105 drzraf 49
 
50
		(DB::isError($req)) ? die($req->getMessage()) : '';
2103 drzraf 51
		$content = array();
52
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
2105 drzraf 53
			$rec['url_simple'] = sprintf("%s/article%d.html",
2103 drzraf 54
										 trim($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['url'], '/'),
55
										 $rec['id']);
2105 drzraf 56
			$rec['url'] = sprintf("%s?var_recherche=%s",
57
								  $rec['url_simple'],
2106 drzraf 58
								  More_Recherche::traiterMotif($q, 'url'));
2105 drzraf 59
			$rec['description'] = More_Recherche::couperTexte($rec['texte'], MORE_RESULTAT_TAILLE_DESCRIPTION);
2103 drzraf 60
			unset($rec['texte']);
61
			$content[$rec['id']] = $rec;
62
 
63
		}
64
		return $content;
65
	}
66
}
67
 
68
class MoteurRecherche_BAZAR {
2105 drzraf 69
	public function get($ids, $q = NULL) {
70
		if(!$ids) return array();
2103 drzraf 71
		$db = DB::connect($GLOBALS['_MOTEUR_RECHERCHE_']['bazar'][0]['bdd_dsn']);
2105 drzraf 72
		$req = $db->query(sprintf(<<<EOF
73
SELECT bf_id_fiche AS id,
74
	   bf_description AS texte,
75
	   bf_titre AS titre,
76
	   bf_date_debut_evenement AS date_creation
77
FROM bazar_fiche
78
WHERE bf_id_fiche IN (%s)
79
EOF
80
								  ,
81
								  implode(',', $ids)));
2103 drzraf 82
 
2105 drzraf 83
		(DB::isError($req)) ? die($req->getMessage()) : '';
2103 drzraf 84
		$content = array();
85
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
2105 drzraf 86
			$rec['url_simple'] = $rec['url'] = sprintf(trim($GLOBALS['_MOTEUR_RECHERCHE_']['bazar'][0]['url'], '/'), $rec['id']);
87
			$rec['description'] = More_Recherche::couperTexte($rec['texte'], MORE_RESULTAT_TAILLE_DESCRIPTION);
2103 drzraf 88
			unset($rec['texte']);
89
			$content[$rec['id']] = $rec;
90
		}
91
		return $content;
92
	}
93
}
94
 
95
class MoteurRecherche_PROJET {
2105 drzraf 96
	public function get($ids, $q = NULL) {
97
		if(!$ids) return array();
2103 drzraf 98
		$db = $GLOBALS['_MOTEUR_RECHERCHE_']['bd']['papyrus'];
2105 drzraf 99
		$req = $db->query(sprintf(<<<EOF
100
SELECT p_id AS id, p_titre, p_description, p_date_creation AS date_creation
101
FROM projet WHERE p_id IN (%s)
102
EOF
103
								  ,
2103 drzraf 104
								  implode(',', $ids)));
2105 drzraf 105
 
2103 drzraf 106
		(DB::isError($req)) ? die($req->getMessage()) : '';
107
		$content = array();
108
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
2105 drzraf 109
			$rec['url_simple'] = $rec['url'] = sprintf("%s?id_projet=%d",
110
													   trim($GLOBALS['_MOTEUR_RECHERCHE_']['projet'][0]['url'], '/'),
111
													   $rec['id']);
2103 drzraf 112
			$rec['description'] = substr(strip_tags($rec['p_description']), 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
113
			unset($rec['p_description']);
114
			$content[$rec['id']] = $rec;
115
		}
116
		return $content;
117
	}
118
}
119
 
120
class MoteurRecherche_PAPYRUS {
2105 drzraf 121
	public function get($ids, $q = NULL) {
122
		if(!$ids) return array();
123
        $db = $GLOBALS['_MOTEUR_RECHERCHE_']['bd']['papyrus'];
124
		$req = $db->query(sprintf(<<<EOF
125
SELECT mc.gmc_ce_menu AS id,
126
	   IF(gm_nom != '', gm_nom, IF(gm_titre != '', gm_titre, gm_titre_alternatif)) AS titre,
127
	   gmc_contenu AS texte,
128
	   gm_description_libre, gm_description_resume,
129
	   gm_mots_cles,gm_source, gm_auteur, gm_contributeur, gm_editeur, gm_categorie, gm_date_creation AS date_creation
130
FROM gen_menu m
131
LEFT JOIN gen_menu_contenu mc ON mc.gmc_ce_menu = m.gm_id_menu AND mc.gmc_bool_dernier = 1
132
WHERE mc.gmc_ce_menu IN (%s)
133
EOF
134
								  ,
135
								  implode(',', $ids)));
136
 
137
		(DB::isError($req)) ? die($req->getMessage()) : '';
138
		$content = array();
139
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
140
			// Création de l'url
141
			// TODO : utiliser comme pour spip un fichier de config spécifique pour virer PAP_URL d'ici
142
			$une_url = new Pap_URL(PAP_URL);
143
			$une_url->setId($rec['id']);
144
			$rec['url_simple'] = $une_url->getURL();
2106 drzraf 145
			$une_url->addQueryString('var_recherche', More_Recherche::traiterMotif($q, 'url'), true);
2105 drzraf 146
			$rec['url'] = $une_url->getURL();
147
 
148
			$rec['description'] =  htmlentities($rec['gm_description_resume']);
149
			unset($rec['gm_description_resume']);
150
			$content[$rec['id']] = $rec;
151
		}
152
		return $content;
2103 drzraf 153
	}
154
}
155
 
2105 drzraf 156
 
2103 drzraf 157
// http://stackoverflow.com/questions/348410/sort-an-array-based-on-another-array
158
function _sortArrayByArray($array, $orderArray) {
159
	$ordered = array();
160
	foreach($orderArray as $key) {
161
		if(array_key_exists($key, $array)) {
162
			$ordered[$key] = $array[$key];
163
			unset($array[$key]);
164
		}
165
	}
166
	return $ordered + $array;
2105 drzraf 167
}
2103 drzraf 168
 
2105 drzraf 169
// adaption aux templates existants: [score] => [weight]
170
function _weight2score(&$item, $key, $max) {
171
	$item['score'] = intval($item['weight'] / $max * 100);
172
	$item['date_creation'] = strftime("%d %B %Y", strtotime($item['date_creation']));
173
	unset($item['weight']);
2103 drzraf 174
}
175
 
2105 drzraf 176
function sphinx_search($q = NULL, $page = 1) {
177
	if(!$q) return array();
2103 drzraf 178
 
2113 drzraf 179
	$db = mysql_connect(SPHINX_DSN, NULL, NULL, TRUE);
2105 drzraf 180
	$req = mysql_query(sprintf("SELECT group_id, main_id FROM i_projet, i_spip, i_papyrus, i_bazar WHERE MATCH('%s') LIMIT 50", $q), $db);
2103 drzraf 181
 
2105 drzraf 182
	$res = array('spip' => array(), 'bazar' => array(), 'projet' => array(), 'papyrus' => array());
183
	$ids_par_poids = array();
184
	while($rec = mysql_fetch_array($req, MYSQL_ASSOC)) {
185
		$res[$rec['group_id']][$rec['id']] = $rec;
186
		$ids_par_poids[] = $rec['main_id'];
187
	}
2103 drzraf 188
 
2105 drzraf 189
	$docs = array();
2103 drzraf 190
 
2105 drzraf 191
	// spip
192
	$spip = new MoteurRecherche_SPIP();
193
	foreach($spip->get(array_filter(array_keys($res['spip']), 'intval'), $q) as $v) {
194
		unset($res['spip'][$v['id']]['group_id'],
195
			  $res['spip'][$v['id']]['main_id'],
196
			  $res['spip'][$v['id']]['id']);
197
		// left: weight
198
		$docs['spip-' . $v['id']] = array_merge($v,	$res['spip'][$v['id']]);
199
	}
2103 drzraf 200
 
2105 drzraf 201
	// bazar
202
	$bazar = new MoteurRecherche_BAZAR();
203
	foreach($bazar->get(array_filter(array_keys($res['bazar']), 'intval'), $q) as $v) {
204
		unset($res['bazar'][$v['id']]['group_id'],
205
			  $res['bazar'][$v['id']]['main_id'],
206
			  $res['bazar'][$v['id']]['id']);
207
		// left: weight
208
		$docs['bazar-' . $v['id']] = array_merge($v, $res['bazar'][$v['id']]);
209
	}
2103 drzraf 210
 
2105 drzraf 211
	// projet
212
	$projet = new MoteurRecherche_PROJET();
213
	foreach($projet->get(array_filter(array_keys($res['projet']), 'intval'), $q) as $v) {
214
		unset($res['projet'][$v['id']]['group_id'],
215
			  $res['projet'][$v['id']]['main_id'],
216
			  $res['projet'][$v['id']]['id']);
217
		// left: weight
218
		$docs['projet-' . $v['id']] = array_merge($v, $res['projet'][$v['id']]);
219
	}
2103 drzraf 220
 
2105 drzraf 221
	// papyrus
222
	$papyrus = new MoteurRecherche_PAPYRUS();
223
	foreach($papyrus->get(array_filter(array_keys($res['papyrus']), 'intval'), $q) as $v) {
224
		unset($res['papyrus'][$v['id']]['group_id'],
225
			  $res['papyrus'][$v['id']]['main_id'],
226
			  $res['papyrus'][$v['id']]['id']);
227
		// left: weight
228
		$docs['papyrus-' . $v['id']] = array_merge($v, $res['papyrus'][$v['id']]);
229
	}
2103 drzraf 230
 
2105 drzraf 231
	$sorted = _sortArrayByArray($docs, $ids_par_poids);
232
 
233
	// sort
234
	/*
235
	// uncomment this:
236
	print_r(array_keys($sorted); die;
237
 
238
	// then:
239
	ddiff
240
	<(mysql -h0 -P 9306 <<<"SELECT main_id FROM i_projet, i_spip, i_papyrus, i_bazar WHERE MATCH('test') LIMIT 50;"|awk '{print $3}'|sed 1d) \
241
	<(POST http://localhost/site:reseau<<<"more_motif=test&"|awk -F'=>' '{print $2}'|sed -e 's/ //g' -e '/^$/d')
242
 
243
	// both should be equal.
244
	// [ SELECT main_id, group_id FROM i_projet, i_spip, i_papyrus, i_bazar WHERE MATCH('test') LIMIT 50; ]
245
	*/
246
 
247
	$max = current($sorted);
248
	$max = $max['weight'];
249
	array_walk($sorted, '_weight2score', $max);
250
 
251
	//var_dump($sorted);die;
252
	return $sorted;
253
}
254
 
255
 
2103 drzraf 256
/*
257
// http://www.php.net/manual/fr/sphinx.examples.php
258
$s = new SphinxClient;
259
$s->setServer("localhost", 9306);
260
$s->setMatchMode(SPH_MATCH_ANY);
261
$s->setMaxQueryTime(3);
262
var_dump($s->query("test"));
263
*/
2108 drzraf 264
 
265
 
266
/*
267
  Note: conversion côté client SQL:
268
  mysql -h0 -P 9306 < <(iconv -f utf8 -t latin1 <<<"SELECT * FROM i_projet, i_spip, i_papyrus, i_bazar WHERE MATCH('journée');")
269
*/
270