Subversion Repositories Applications.papyrus

Rev

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

Rev Author Line No. Line
2103 drzraf 1
<?php
2140 jp_milcent 2
/*vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +------------------------------------------------------------------------------------------------------+
4
// | PHP version 4.1                                                                                      |
5
// +------------------------------------------------------------------------------------------------------+
6
// | Copyright (C) 2004 Tela Botanica (accueil@tela-botanica.org)                                         |
7
// +------------------------------------------------------------------------------------------------------+
8
// | This file is part of Papyrus.                                                                        |
9
// |                                                                                                      |
10
// | Foobar is free software; you can redistribute it and/or modify                                       |
11
// | it under the terms of the GNU General Public License as published by                                 |
12
// | the Free Software Foundation; either version 2 of the License, or                                    |
13
// | (at your option) any later version.                                                                  |
14
// |                                                                                                      |
15
// | Foobar is distributed in the hope that it will be useful,                                            |
16
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                                       |
17
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                        |
18
// | GNU General Public License for more details.                                                         |
19
// |                                                                                                      |
20
// | You should have received a copy of the GNU General Public License                                    |
21
// | along with Foobar; if not, write to the Free Software                                                |
22
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                            |
23
// +------------------------------------------------------------------------------------------------------+
24
/**
2103 drzraf 25
 * Moteur de recherche SPHINX
2140 jp_milcent 26
 *
27
 * Installation de sphinx :
28
 * sudo urpmi lib64sphinxclient-devel
29
 * sudo pecl install sphinx
30
 *
31
 * See also:
32
 *  - http://www.ibm.com/developerworks/library/os-sphinx/
33
 *  - http://sphinxsearch.com/docs/manual-2.0.7.html#extended-syntax
34
 *
35
 * TODO: http://sphinxsearch.com/blog/2010/08/17/how-sphinx-relevance-ranking-works/
36
 * TODO: attention, projet,bazaar,spip et papyrus sont latin9, ainsi en est-il de l'input attendu du <form>
37
 * mais pour coste... c'est full utf-8
38
 * Structure retournée :
39
 * 	'poids' => 0,
40
 * 	'url' => '',
41
 * 	'titre' => '',
42
 * 	'hreflang' => '',
43
 * 	'accesskey' => '',
44
 * 	'title' => '', // balise 'title'
45
 * 	'date_creation' => '',
46
 * 	'description' => ''
47
 *
48
 *
49
 * // http://www.php.net/manual/fr/sphinx.examples.php
50
 * $s = new SphinxClient;
51
 * $s->setServer("localhost", 9306);
52
 * $s->setMatchMode(SPH_MATCH_ANY);
53
 * $s->setMaxQueryTime(3);
54
 * var_dump($s->query("test"));
55
 *
56
 * Note: conversion côté client SQL:
57
 * mysql -h0 -P 9306 < <(iconv -f utf8 -t latin1 <<<"SELECT * FROM i_projet, i_spip, i_papyrus, i_bazar WHERE MATCH('journée');")
58
 *
59
 *
60
 * Test :
61
 * ddiff
62
 * <(mysql -h0 -P 9306 <<<"SELECT main_id FROM i_projet, i_spip, i_papyrus, i_bazar, i_coste, i_nvjfl WHERE MATCH('test') LIMIT 50;"|awk '{print $3}'|sed 1d) \
63
 * <(POST http://localhost/site:reseau<<<"more_motif=test&"|awk -F'=>' '{print $2}'|sed -e 's/ //g' -e '/^$/d')
64
 *
65
 * both should be equal.
66
 * [ SELECT main_id, group_id FROM i_projet, i_spip, i_papyrus, i_bazar WHERE MATCH('test') LIMIT 50; ]
67
 *
68
 *
69
 *@package Applette
70
 *@subpackage Moteur_recherche
71
 //Auteur original :
72
 * @author		Raphaël Droz <raphael@tela-botanica.org
73
 //Autres auteurs :
74
 *@author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
75
 *@copyright	Tela-Botanica 2000-2013
76
 *@version		$Revision$
77
 // +------------------------------------------------------------------------------------------------------+
2103 drzraf 78
 */
79
 
2140 jp_milcent 80
define('SPHINX_DSN', '193.54.123.216:9306');
2117 drzraf 81
define('_MRS_SPHINX_BASEHOST', $_SERVER['HTTP_HOST']);
2105 drzraf 82
// pour strftime()
83
date_default_timezone_set('Europe/Paris');
84
setlocale(LC_TIME, 'fr_FR');
85
 
2140 jp_milcent 86
function sphinx_search($q = NULL, $page = 1) {
87
	if(!$q) return array();
88
 
89
	// quelques aliases pour faciliter l'usage sans passer par le full sphinxQL
90
	// $q = preg_replace('/\<actu\>(.*)/', '\1 @group_id i_spip', $q);
91
	// $q = preg_replace('/\<eflore\>(.*)/', '\1 @group_id i_bazar|i_coste', $q);
92
 
93
	$db = mysql_connect(SPHINX_DSN, NULL, NULL, TRUE);
94
	// AFAICT, pas de réel risque de SQL-injection du côté de sphinx (au pire, $req = FALSE)
95
	// et il serait dommage de devoir limiter la puissante syntaxe offerte à  l'utilisation
96
	//$requeteTpl = "SELECT group_id, main_id FROM i_projet, i_spip, i_papyrus, i_bazar, i_coste, i_nvjfl WHERE MATCH('%s') LIMIT 50";
97
	$requeteTpl = 'SELECT group_id, main_id, id '.
98
		'FROM i_projet, i_spip, i_papyrus, i_bazar '.
99
		"WHERE MATCH('%s') ".
100
		'LIMIT 50 ';
101
	$requete = mysql_query(sprintf($requeteTpl, $q), $db);
102
 
103
	$retour = array();
104
	if ($requete) {
105
		$res = array('spip' => array(), 'bazar' => array(), 'projet' => array(), 'papyrus' => array(), 'coste' => array(), 'nvjfl' => array());
106
		$ids_par_poids = array();
107
		while($rec = mysql_fetch_array($requete, MYSQL_ASSOC)) {
108
			$res[$rec['group_id']][$rec['id']] = $rec;
109
			$ids_par_poids[] = $rec['main_id'];
110
		}
111
 
112
		$docs = array();
113
		// spip
114
		$spip = new MoteurRecherche_SPIP();
115
		foreach($spip->get(array_filter(array_keys($res['spip']), 'intval'), $q) as $v) {
116
			unset($res['spip'][$v['id']]['group_id'],
117
				  $res['spip'][$v['id']]['main_id'],
118
				  $res['spip'][$v['id']]['id']);
119
			// left: weight
120
			$docs['spip-' . $v['id']] = array_merge($v,	$res['spip'][$v['id']]);
121
		}
122
 
123
		// bazar
124
		$bazar = new MoteurRecherche_BAZAR();
125
		foreach($bazar->get(array_filter(array_keys($res['bazar']), 'intval'), $q) as $v) {
126
			unset($res['bazar'][$v['id']]['group_id'],
127
				  $res['bazar'][$v['id']]['main_id'],
128
				  $res['bazar'][$v['id']]['id']);
129
			// left: weight
130
			$docs['bazar-' . $v['id']] = array_merge($v, $res['bazar'][$v['id']]);
131
		}
132
 
133
		// projet
134
		$projet = new MoteurRecherche_PROJET();
135
		foreach($projet->get(array_filter(array_keys($res['projet']), 'intval'), $q) as $v) {
136
			unset($res['projet'][$v['id']]['group_id'],
137
				  $res['projet'][$v['id']]['main_id'],
138
				  $res['projet'][$v['id']]['id']);
139
			// left: weight
140
			$docs['projet-' . $v['id']] = array_merge($v, $res['projet'][$v['id']]);
141
		}
142
 
143
		// papyrus
144
		$papyrus = new MoteurRecherche_PAPYRUS();
145
		foreach($papyrus->get(array_filter(array_keys($res['papyrus']), 'intval'), $q) as $v) {
146
			unset($res['papyrus'][$v['id']]['group_id'],
147
				  $res['papyrus'][$v['id']]['main_id'],
148
				  $res['papyrus'][$v['id']]['id']);
149
			// left: weight
150
			$docs['papyrus-' . $v['id']] = array_merge($v, $res['papyrus'][$v['id']]);
151
		}
152
 
153
		// coste
154
		$coste = new MoteurRecherche_COSTE();
155
		foreach($coste->get(array_filter(array_keys($res['coste']), 'intval'), $q) as $v) {
156
			unset($res['coste'][$v['id']]['group_id'],
157
				  $res['coste'][$v['id']]['main_id'],
158
				  $res['coste'][$v['id']]['id']);
159
			// left: weight
160
			$docs['coste-' . $v['id']] = array_merge($v, $res['coste'][$v['id']]);
161
		}
162
 
163
		// nvjfl
164
		$nvjfl = new MoteurRecherche_NVJFL();
165
		foreach($nvjfl->get(array_filter(array_keys($res['nvjfl']), 'intval'), $q) as $v) {
166
			unset($res['nvjfl'][$v['id']]['group_id'],
167
				  $res['nvjfl'][$v['id']]['main_id'],
168
				  $res['nvjfl'][$v['id']]['id']);
169
			// left: weight
170
			$docs['nvjfl-' . $v['id']] = array_merge($v, $res['nvjfl'][$v['id']]);
171
		}
172
 
173
		// sort
174
		$sorted = _sortArrayByArray($docs, $ids_par_poids);
175
		$max = current($sorted);
176
		$max = $max['weight'];
177
 
178
		if (isset($_GET['tri']) && $_GET['tri'] == 'date') {
179
			usort($sorted, '_actuNewerFirst');
180
		}
181
		// transforme les clefs pour s'adapter aux templates existants
182
		array_walk($sorted, '_weight2score', $max);
183
 
184
		// var_dump($sorted);die;
185
		$retour = $sorted;
186
	}
187
	return $retour;
188
}
189
 
2103 drzraf 190
class MoteurRecherche_SPIP {
2105 drzraf 191
	public function get($ids, $q = NULL) {
2103 drzraf 192
		$content = array();
2140 jp_milcent 193
		if (count($ids) > 0) {
194
			$db = DB::connect($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['bdd_dsn']);
195
			$requeteTpl = 'SELECT id_article AS id, titre, texte, date AS date_creation, lang as hreflang '.
196
					'FROM spip_articles '.
197
					'WHERE statut = "%s" '.
198
					'AND id_article IN (%s) ';
199
			$requete = $db->query(sprintf($requeteTpl, 'publie',implode(',', $ids)));
200
			(DB::isError($requete)) ? die($requete->getMessage()) : '';
2103 drzraf 201
 
2140 jp_milcent 202
			while ($rec = $requete->fetchRow(DB_FETCHMODE_ASSOC)) {
203
				$rec['url_simple'] = sprintf("%s/article%d.html",
204
					 trim($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['url'], '/'),
205
					 $rec['id']);
206
				$rec['url'] = sprintf("%s?var_recherche=%s",
207
						$rec['url_simple'],
208
						More_Recherche::traiterMotif($q, 'url'));
209
				$rec['description'] = More_Recherche::couperTexte($rec['texte'], MORE_RESULTAT_TAILLE_DESCRIPTION);
210
				unset($rec['texte']);
211
				$content[$rec['id']] = $rec;
212
			}
2103 drzraf 213
		}
214
		return $content;
215
	}
216
}
217
 
218
class MoteurRecherche_BAZAR {
2105 drzraf 219
	public function get($ids, $q = NULL) {
2140 jp_milcent 220
		$content = array();
221
		if (count($ids) > 0) {
222
			$db = DB::connect($GLOBALS['_MOTEUR_RECHERCHE_']['bazar'][0]['bdd_dsn']);
223
			$requeteTpl = 'SELECT bf_id_fiche AS id, '.
224
					'bf_description AS texte, '.
225
					'bf_titre AS titre, '.
226
					'bf_date_debut_evenement AS date_creation '.
227
					'FROM bazar_fiche '.
228
					'WHERE bf_id_fiche IN (%s) ';
229
			$requete = $db->query(sprintf($requeteTpl, implode(',', $ids)));
230
			(DB::isError($requete)) ? die($requete->getMessage()) : '';
2103 drzraf 231
 
2140 jp_milcent 232
			while ($rec = $requete->fetchRow(DB_FETCHMODE_ASSOC)) {
233
				$rec['url_simple'] = $rec['url'] = sprintf(trim($GLOBALS['_MOTEUR_RECHERCHE_']['bazar'][0]['url'], '/'), $rec['id']);
234
				$rec['description'] = More_Recherche::couperTexte($rec['texte'], MORE_RESULTAT_TAILLE_DESCRIPTION);
235
				unset($rec['texte']);
236
				$content[$rec['id']] = $rec;
237
			}
2103 drzraf 238
		}
2140 jp_milcent 239
		return $content;
2103 drzraf 240
	}
241
}
242
 
243
class MoteurRecherche_PROJET {
2105 drzraf 244
	public function get($ids, $q = NULL) {
2140 jp_milcent 245
		$content = array();
246
		if (count($ids) > 0) {
247
			$db = $GLOBALS['_MOTEUR_RECHERCHE_']['bd']['papyrus'];
248
			$requeteTpl = 'SELECT p_id AS id, p_titre AS titre, p_description, p_date_creation AS date_creation '.
249
					'FROM projet '.
250
					'WHERE p_id IN (%s)';
251
			$requete = $db->query(sprintf($requeteTpl, implode(',', $ids)));
252
			(DB::isError($requete)) ? die($requete->getMessage()) : '';
2105 drzraf 253
 
2140 jp_milcent 254
			while ($rec = $requete->fetchRow(DB_FETCHMODE_ASSOC)) {
255
				$rec['url_simple'] = $rec['url'] = sprintf("%s?id_projet=%d",
256
					trim($GLOBALS['_MOTEUR_RECHERCHE_']['projet']['url'], '/'),
257
					$rec['id']);
258
				$rec['description'] = substr(strip_tags($rec['p_description']), 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
259
				unset($rec['p_description']);
260
				$content[$rec['id']] = $rec;
261
			}
2103 drzraf 262
		}
2140 jp_milcent 263
		return $content;
2103 drzraf 264
	}
265
}
266
 
267
class MoteurRecherche_PAPYRUS {
2105 drzraf 268
	public function get($ids, $q = NULL) {
269
		$content = array();
2140 jp_milcent 270
		if (count($ids) > 0) {
271
			$db = $GLOBALS['_MOTEUR_RECHERCHE_']['bd']['papyrus'];
272
			$requeteTpl = 'SELECT mc.gmc_ce_menu AS id, '.
273
				"	IF(gm_nom != '', gm_nom, IF(gm_titre != '', gm_titre, gm_titre_alternatif)) AS titre, ".
274
				'	gmc_contenu AS texte, '.
275
				'	gm_description_libre, gm_description_resume, '.
276
				'	gm_mots_cles,gm_source, gm_auteur, gm_contributeur, gm_editeur, gm_categorie, '.
277
				'	gm_date_creation AS date_creation '.
278
				'FROM gen_menu AS m '.
279
				'	LEFT JOIN gen_menu_contenu AS mc ON mc.gmc_ce_menu = m.gm_id_menu AND mc.gmc_bool_dernier = 1 '.
280
				'WHERE mc.gmc_ce_menu IN (%s) ';
281
			$requete = $db->query(sprintf($requeteTpl, implode(',', $ids)));
282
			(DB::isError($requete)) ? die($requete->getMessage()) : '';
2105 drzraf 283
 
2140 jp_milcent 284
			while ($rec = $requete->fetchRow(DB_FETCHMODE_ASSOC)) {
285
				// Création de l'url
286
				// TODO : utiliser comme pour spip un fichier de config spécifique pour virer PAP_URL d'ici
287
				$une_url = new Pap_URL(PAP_URL);
288
				$une_url->setId($rec['id']);
289
				$rec['url_simple'] = $une_url->getURL();
290
				$une_url->addQueryString('var_recherche', More_Recherche::traiterMotif($q, 'url'), true);
291
				$rec['url'] = $une_url->getURL();
292
 
293
				$rec['description'] =  htmlentities($rec['gm_description_resume']);
294
				unset($rec['gm_description_resume']);
295
				$content[$rec['id']] = $rec;
296
			}
2105 drzraf 297
		}
2140 jp_milcent 298
		return $content;
2103 drzraf 299
	}
300
}
301
 
2117 drzraf 302
class MoteurRecherche_COSTE {
303
	public function get($ids, $q = NULL) {
2140 jp_milcent 304
		$content = array();
305
		if (count($ids) > 0) {
306
			// DB access is dumb, let's use this one and pray
307
			$db = $GLOBALS['_MOTEUR_RECHERCHE_']['bd']['bota'];
308
			$requeteTpl = 'SELECT c.flore_bdtfx_nn AS id, c.nom_sci AS titre, dsc.body AS description '.
309
				'FROM tb_eflore.coste_v2_00 AS c '.
310
				"	LEFT JOIN tela_prod_wikini.florecoste_pages dsc ON c.page_wiki_dsc = dsc.tag AND dsc.latest = 'Y' ".
311
				'WHERE c.flore_bdtfx_nn IN (%s) ';
312
			$requete = $db->query(sprintf($requeteTpl, implode(',', $ids)));
313
			(DB::isError($requete)) ? die($requete->getMessage()) : '';
2105 drzraf 314
 
2140 jp_milcent 315
			while ($rec = $requete->fetchRow(DB_FETCHMODE_ASSOC)) {
316
				$rec['url_simple'] = $rec['url'] = sprintf("http://%s/bdtfx-nn-%d", _MRS_SPHINX_BASEHOST, $rec['id']);
317
				// TODO: interpret wikini
318
				$rec['description'] = substr($rec['description'], 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
319
				$content[$rec['id']] = $rec;
320
			}
2117 drzraf 321
		}
2140 jp_milcent 322
		return $content;
2117 drzraf 323
	}
324
}
325
 
2123 drzraf 326
class MoteurRecherche_NVJFL {
327
	public function get($ids, $q = NULL) {
2140 jp_milcent 328
		$content = array();
329
		if (count($ids) > 0) {
330
			// DB access is dumb, let's use this one and pray
331
			$db = $GLOBALS['_MOTEUR_RECHERCHE_']['bd']['bota'];
332
			$requeteTpl = 'SELECT b.num_nom AS id, '.
333
			"	CONCAT(nom_sci, ' (nn: ', b.num_nom, ', nt: ', num_taxonomique, ')') AS titre, ".
334
			'	GROUP_CONCAT(n.nom_vernaculaire) AS description '.
335
			'FROM tb_eflore.bdtfx_v1_01 AS b '.
336
			'	LEFT JOIN tb_eflore.nvjfl_v2007 n ON n.num_taxon = b.num_taxonomique '.
337
			'WHERE b.num_nom IN (%s) '.
338
			'GROUP BY n.num_taxon ';
339
			$requete = $db->query(sprintf($requeteTpl, implode(',', $ids)));
2117 drzraf 340
 
2140 jp_milcent 341
			(DB::isError($requete)) ? die($requete->getMessage()) : '';
342
 
343
			while ($rec = $requete->fetchRow(DB_FETCHMODE_ASSOC)) {
344
				$rec['url_simple'] = $rec['url'] = sprintf("http://%s/bdtfx-nn-%d", _MRS_SPHINX_BASEHOST, $rec['id']);
345
				$rec['description'] = substr($rec['description'], 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
346
				$content[$rec['id']] = $rec;
347
			}
2123 drzraf 348
		}
2140 jp_milcent 349
		return $content;
2123 drzraf 350
	}
351
}
352
 
2103 drzraf 353
// http://stackoverflow.com/questions/348410/sort-an-array-based-on-another-array
354
function _sortArrayByArray($array, $orderArray) {
355
	$ordered = array();
2140 jp_milcent 356
	foreach ($orderArray as $key) {
357
		if (array_key_exists($key, $array)) {
2103 drzraf 358
			$ordered[$key] = $array[$key];
359
			unset($array[$key]);
360
		}
361
	}
362
	return $ordered + $array;
2105 drzraf 363
}
2103 drzraf 364
 
2140 jp_milcent 365
function _actuNewerFirst($a,$b) {
366
	return isset($a['date_creation']) && isset($b['date_creation']) ? strcmp($b['date_creation'], $a['date_creation']) : 0;
367
}
368
 
2105 drzraf 369
// adaption aux templates existants: [score] => [weight]
370
function _weight2score(&$item, $key, $max) {
371
	$item['score'] = intval($item['weight'] / $max * 100);
2117 drzraf 372
	$item['date_creation'] = isset($item['date_creation']) ? strftime("%d %B %Y", strtotime($item['date_creation'])) : '';
2105 drzraf 373
	unset($item['weight']);
2103 drzraf 374
}
375
 
2140 jp_milcent 376
?>