Subversion Repositories Applications.papyrus

Rev

Rev 2133 | Rev 2141 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2133 Rev 2140
Line 1... Line 1...
1
<?php
1
<?php
-
 
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
// +------------------------------------------------------------------------------------------------------+
2
/*
24
/**
3
 * Moteur de recherche SPHINX
25
 * Moteur de recherche SPHINX
-
 
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 :
4
 * @author        Raphaël Droz <raphael@tela-botanica.org
72
 * @author		Raphaël Droz <raphael@tela-botanica.org
-
 
73
 //Autres auteurs :
-
 
74
 *@author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
5
 * @copyright     Tela-Botanica 2013
75
 *@copyright	Tela-Botanica 2000-2013
-
 
76
 *@version		$Revision$
-
 
77
 // +------------------------------------------------------------------------------------------------------+
6
 */
78
 */
Line 7... Line -...
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/
-
 
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
-
 
17
*/
-
 
18
 
-
 
19
/* returned struct:
-
 
20
'poids' => 0,
-
 
21
'url' => '',
-
 
22
'titre' => '',
-
 
23
'hreflang' => '',
-
 
24
'accesskey' => '',
-
 
25
'title' => '', // balise 'title'
-
 
26
'date_creation' => '',
-
 
27
'description' => ''
-
 
28
*/
-
 
29
 
79
 
30
define('SPHINX_DSN', '127.0.0.1:9306');
80
define('SPHINX_DSN', '193.54.123.216:9306');
31
define('_MRS_SPHINX_BASEHOST', $_SERVER['HTTP_HOST']);
-
 
32
 
81
define('_MRS_SPHINX_BASEHOST', $_SERVER['HTTP_HOST']);
33
// pour strftime()
82
// pour strftime()
34
date_default_timezone_set('Europe/Paris');
83
date_default_timezone_set('Europe/Paris');
Line -... Line 84...
-
 
84
setlocale(LC_TIME, 'fr_FR');
-
 
85
 
-
 
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;
35
setlocale(LC_TIME, 'fr_FR');
188
}
36
 
189
 
37
class MoteurRecherche_SPIP {
-
 
38
	public function get($ids, $q = NULL) {
-
 
39
		if(!$ids) return array();
-
 
40
		$db = DB::connect($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['bdd_dsn']);
-
 
41
		$req = $db->query(sprintf(<<<EOF
-
 
42
SELECT id_article AS id, titre, texte, date AS date_creation, lang as hreflang
-
 
43
FROM spip_articles
-
 
44
WHERE statut = "%s"
-
 
45
AND id_article IN (%s)
-
 
46
EOF
-
 
47
								  ,
-
 
48
								  "publie",
-
 
49
								  implode(',', $ids)));
-
 
50
 
190
class MoteurRecherche_SPIP {
-
 
191
	public function get($ids, $q = NULL) {
-
 
192
		$content = array();
-
 
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)));
51
		(DB::isError($req)) ? die($req->getMessage()) : '';
200
			(DB::isError($requete)) ? die($requete->getMessage()) : '';
52
		$content = array();
201
 
53
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
202
			while ($rec = $requete->fetchRow(DB_FETCHMODE_ASSOC)) {
54
			$rec['url_simple'] = sprintf("%s/article%d.html",
203
				$rec['url_simple'] = sprintf("%s/article%d.html",
55
										 trim($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['url'], '/'),
204
					 trim($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['url'], '/'),
56
										 $rec['id']);
205
					 $rec['id']);
57
			$rec['url'] = sprintf("%s?var_recherche=%s",
206
				$rec['url'] = sprintf("%s?var_recherche=%s",
58
								  $rec['url_simple'],
207
						$rec['url_simple'],
59
								  More_Recherche::traiterMotif($q, 'url'));
208
						More_Recherche::traiterMotif($q, 'url'));
60
			$rec['description'] = More_Recherche::couperTexte($rec['texte'], MORE_RESULTAT_TAILLE_DESCRIPTION);
209
				$rec['description'] = More_Recherche::couperTexte($rec['texte'], MORE_RESULTAT_TAILLE_DESCRIPTION);
61
			unset($rec['texte']);
210
				unset($rec['texte']);
62
			$content[$rec['id']] = $rec;
211
				$content[$rec['id']] = $rec;
63
 
212
			}
64
		}
213
		}
65
		return $content;
214
		return $content;
Line 66... Line 215...
66
	}
215
	}
67
}
216
}
68
 
-
 
69
class MoteurRecherche_BAZAR {
-
 
70
	public function get($ids, $q = NULL) {
-
 
71
		if(!$ids) return array();
-
 
72
		$db = DB::connect($GLOBALS['_MOTEUR_RECHERCHE_']['bazar'][0]['bdd_dsn']);
-
 
73
		$req = $db->query(sprintf(<<<EOF
-
 
74
SELECT bf_id_fiche AS id,
-
 
75
	   bf_description AS texte,
-
 
76
	   bf_titre AS titre,
-
 
77
	   bf_date_debut_evenement AS date_creation
-
 
78
FROM bazar_fiche
-
 
79
WHERE bf_id_fiche IN (%s)
-
 
80
EOF
-
 
81
								  ,
-
 
82
								  implode(',', $ids)));
217
 
-
 
218
class MoteurRecherche_BAZAR {
-
 
219
	public function get($ids, $q = NULL) {
-
 
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) ';
83
 
229
			$requete = $db->query(sprintf($requeteTpl, implode(',', $ids)));
84
		(DB::isError($req)) ? die($req->getMessage()) : '';
230
			(DB::isError($requete)) ? die($requete->getMessage()) : '';
85
		$content = array();
231
 
86
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
232
			while ($rec = $requete->fetchRow(DB_FETCHMODE_ASSOC)) {
87
			$rec['url_simple'] = $rec['url'] = sprintf(trim($GLOBALS['_MOTEUR_RECHERCHE_']['bazar'][0]['url'], '/'), $rec['id']);
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);
88
			$rec['description'] = More_Recherche::couperTexte($rec['texte'], MORE_RESULTAT_TAILLE_DESCRIPTION);
235
				unset($rec['texte']);
89
			unset($rec['texte']);
236
				$content[$rec['id']] = $rec;
90
			$content[$rec['id']] = $rec;
237
			}
91
		}
238
		}
Line 92... Line 239...
92
		return $content;		
239
		return $content;
93
	}
240
	}
94
}
-
 
95
 
-
 
96
class MoteurRecherche_PROJET {
-
 
97
	public function get($ids, $q = NULL) {
-
 
98
		if(!$ids) return array();
-
 
99
		$db = $GLOBALS['_MOTEUR_RECHERCHE_']['bd']['papyrus'];
-
 
100
		$req = $db->query(sprintf(<<<EOF
-
 
101
SELECT p_id AS id, p_titre AS titre, p_description, p_date_creation AS date_creation
-
 
102
FROM projet WHERE p_id IN (%s)
-
 
103
EOF
-
 
104
								  ,
241
}
-
 
242
 
-
 
243
class MoteurRecherche_PROJET {
-
 
244
	public function get($ids, $q = NULL) {
-
 
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 '.
105
								  implode(',', $ids)));
250
					'WHERE p_id IN (%s)';
106
 
251
			$requete = $db->query(sprintf($requeteTpl, implode(',', $ids)));
107
		(DB::isError($req)) ? die($req->getMessage()) : '';
252
			(DB::isError($requete)) ? die($requete->getMessage()) : '';
108
		$content = array();
253
 
109
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
254
			while ($rec = $requete->fetchRow(DB_FETCHMODE_ASSOC)) {
110
			$rec['url_simple'] = $rec['url'] = sprintf("%s?id_projet=%d",
255
				$rec['url_simple'] = $rec['url'] = sprintf("%s?id_projet=%d",
111
													   trim($GLOBALS['_MOTEUR_RECHERCHE_']['projet']['url'], '/'),
256
					trim($GLOBALS['_MOTEUR_RECHERCHE_']['projet']['url'], '/'),
-
 
257
					$rec['id']);
112
													   $rec['id']);
258
				$rec['description'] = substr(strip_tags($rec['p_description']), 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
113
			$rec['description'] = substr(strip_tags($rec['p_description']), 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
259
				unset($rec['p_description']);
114
			unset($rec['p_description']);
260
				$content[$rec['id']] = $rec;
115
			$content[$rec['id']] = $rec;
261
			}
Line 116... Line 262...
116
		}
262
		}
117
		return $content;		
263
		return $content;
118
	}
-
 
119
}
-
 
120
 
-
 
121
class MoteurRecherche_PAPYRUS {
-
 
122
	public function get($ids, $q = NULL) {
-
 
123
		if(!$ids) return array();
-
 
124
        $db = $GLOBALS['_MOTEUR_RECHERCHE_']['bd']['papyrus'];
-
 
125
		$req = $db->query(sprintf(<<<EOF
-
 
126
SELECT mc.gmc_ce_menu AS id,
-
 
127
	   IF(gm_nom != '', gm_nom, IF(gm_titre != '', gm_titre, gm_titre_alternatif)) AS titre,
-
 
128
	   gmc_contenu AS texte,
-
 
129
	   gm_description_libre, gm_description_resume,
-
 
130
	   gm_mots_cles,gm_source, gm_auteur, gm_contributeur, gm_editeur, gm_categorie, gm_date_creation AS date_creation
-
 
131
FROM gen_menu m
-
 
132
LEFT JOIN gen_menu_contenu mc ON mc.gmc_ce_menu = m.gm_id_menu AND mc.gmc_bool_dernier = 1
-
 
133
WHERE mc.gmc_ce_menu IN (%s)
-
 
134
EOF
264
	}
-
 
265
}
-
 
266
 
-
 
267
class MoteurRecherche_PAPYRUS {
-
 
268
	public function get($ids, $q = NULL) {
-
 
269
		$content = array();
-
 
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 '.
135
								  ,
279
				'	LEFT JOIN gen_menu_contenu AS mc ON mc.gmc_ce_menu = m.gm_id_menu AND mc.gmc_bool_dernier = 1 '.
136
								  implode(',', $ids)));
280
				'WHERE mc.gmc_ce_menu IN (%s) ';
137
 
281
			$requete = $db->query(sprintf($requeteTpl, implode(',', $ids)));
138
		(DB::isError($req)) ? die($req->getMessage()) : '';
282
			(DB::isError($requete)) ? die($requete->getMessage()) : '';
139
		$content = array();
283
 
140
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
284
			while ($rec = $requete->fetchRow(DB_FETCHMODE_ASSOC)) {
141
			// Création de l'url
285
				// Création de l'url
142
			// TODO : utiliser comme pour spip un fichier de config spécifique pour virer PAP_URL d'ici
286
				// TODO : utiliser comme pour spip un fichier de config spécifique pour virer PAP_URL d'ici
143
			$une_url = new Pap_URL(PAP_URL);
287
				$une_url = new Pap_URL(PAP_URL);
144
			$une_url->setId($rec['id']);
288
				$une_url->setId($rec['id']);
145
			$rec['url_simple'] = $une_url->getURL();
289
				$rec['url_simple'] = $une_url->getURL();
146
			$une_url->addQueryString('var_recherche', More_Recherche::traiterMotif($q, 'url'), true);
290
				$une_url->addQueryString('var_recherche', More_Recherche::traiterMotif($q, 'url'), true);
-
 
291
				$rec['url'] = $une_url->getURL();
147
			$rec['url'] = $une_url->getURL();
292
 
148
 
293
				$rec['description'] =  htmlentities($rec['gm_description_resume']);
149
			$rec['description'] =  htmlentities($rec['gm_description_resume']);
294
				unset($rec['gm_description_resume']);
150
			unset($rec['gm_description_resume']);
295
				$content[$rec['id']] = $rec;
Line 151... Line 296...
151
			$content[$rec['id']] = $rec;
296
			}
152
		}
297
		}
153
		return $content;		
-
 
154
	}
-
 
155
}
-
 
156
 
-
 
157
class MoteurRecherche_COSTE {
-
 
158
	public function get($ids, $q = NULL) {
-
 
159
		if(!$ids) return array();
-
 
160
		// DB access is dumb, let's use this one and pray
-
 
161
		$db = $GLOBALS['_MOTEUR_RECHERCHE_']['bd']['papyrus'];
-
 
162
		$req = $db->query(sprintf(<<<EOF
-
 
163
SELECT c.flore_bdtfx_nn AS id, c.nom_sci AS titre, dsc.body AS description
-
 
164
FROM tb_eflore.coste_v2_00 c
-
 
165
LEFT JOIN tela_prod_wikini.florecoste_pages dsc ON c.page_wiki_dsc = dsc.tag AND dsc.latest = 'Y'
-
 
166
WHERE c.flore_bdtfx_nn IN (%s)
298
		return $content;
-
 
299
	}
-
 
300
}
-
 
301
 
-
 
302
class MoteurRecherche_COSTE {
-
 
303
	public function get($ids, $q = NULL) {
-
 
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 '.
167
EOF
309
				'FROM tb_eflore.coste_v2_00 AS c '.
168
								  ,
310
				"	LEFT JOIN tela_prod_wikini.florecoste_pages dsc ON c.page_wiki_dsc = dsc.tag AND dsc.latest = 'Y' ".
169
								  implode(',', $ids)));
311
				'WHERE c.flore_bdtfx_nn IN (%s) ';
170
 
312
			$requete = $db->query(sprintf($requeteTpl, implode(',', $ids)));
171
		(DB::isError($req)) ? die($req->getMessage()) : '';
313
			(DB::isError($requete)) ? die($requete->getMessage()) : '';
-
 
314
 
172
		$content = array();
315
			while ($rec = $requete->fetchRow(DB_FETCHMODE_ASSOC)) {
173
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
316
				$rec['url_simple'] = $rec['url'] = sprintf("http://%s/bdtfx-nn-%d", _MRS_SPHINX_BASEHOST, $rec['id']);
174
			$rec['url_simple'] = $rec['url'] = sprintf("http://%s/bdtfx-nn-%d", _MRS_SPHINX_BASEHOST, $rec['id']);
317
				// TODO: interpret wikini
175
			// TODO: interpret wikini
318
				$rec['description'] = substr($rec['description'], 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
Line 176... Line 319...
176
			$rec['description'] = substr($rec['description'], 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
319
				$content[$rec['id']] = $rec;
177
			$content[$rec['id']] = $rec;
320
			}
178
		}
-
 
179
		return $content;		
-
 
180
	}
-
 
181
}
-
 
182
 
-
 
183
class MoteurRecherche_NVJFL {
-
 
184
	public function get($ids, $q = NULL) {
-
 
185
		if(!$ids) return array();
-
 
186
		// DB access is dumb, let's use this one and pray
-
 
187
		$db = $GLOBALS['_MOTEUR_RECHERCHE_']['bd']['papyrus'];
-
 
188
		$req = $db->query(sprintf(<<<EOF
-
 
189
SELECT b.num_nom AS id, CONCAT(nom_sci, ' (nn: ', b.num_nom, ', nt: ', num_taxonomique, ')') AS titre, GROUP_CONCAT(n.nom_vernaculaire) AS description
-
 
190
FROM tb_eflore.bdtfx_v1_01 b
-
 
191
LEFT JOIN tb_eflore.nvjfl_v2007 n ON n.num_taxon = b.num_taxonomique
-
 
192
WHERE b.num_nom IN (%s)
321
		}
-
 
322
		return $content;
-
 
323
	}
-
 
324
}
-
 
325
 
-
 
326
class MoteurRecherche_NVJFL {
-
 
327
	public function get($ids, $q = NULL) {
-
 
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 '.
193
GROUP BY n.num_taxon
336
			'	LEFT JOIN tb_eflore.nvjfl_v2007 n ON n.num_taxon = b.num_taxonomique '.
194
EOF
337
			'WHERE b.num_nom IN (%s) '.
195
								  ,
338
			'GROUP BY n.num_taxon ';
196
								  implode(',', $ids)));
339
			$requete = $db->query(sprintf($requeteTpl, implode(',', $ids)));
-
 
340
 
197
 
341
			(DB::isError($requete)) ? die($requete->getMessage()) : '';
198
		(DB::isError($req)) ? die($req->getMessage()) : '';
342
 
199
		$content = array();
343
			while ($rec = $requete->fetchRow(DB_FETCHMODE_ASSOC)) {
200
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
344
				$rec['url_simple'] = $rec['url'] = sprintf("http://%s/bdtfx-nn-%d", _MRS_SPHINX_BASEHOST, $rec['id']);
Line 201... Line -...
201
			$rec['url_simple'] = $rec['url'] = sprintf("http://%s/bdtfx-nn-%d", _MRS_SPHINX_BASEHOST, $rec['id']);
-
 
202
			$rec['description'] = substr($rec['description'], 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
345
				$rec['description'] = substr($rec['description'], 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
203
			$content[$rec['id']] = $rec;
346
				$content[$rec['id']] = $rec;
204
		}
347
			}
205
		return $content;		
348
		}
206
	}
349
		return $content;
207
}
350
	}
208
 
351
}
209
 
352
 
210
// http://stackoverflow.com/questions/348410/sort-an-array-based-on-another-array
353
// http://stackoverflow.com/questions/348410/sort-an-array-based-on-another-array
211
function _sortArrayByArray($array, $orderArray) {
354
function _sortArrayByArray($array, $orderArray) {
212
	$ordered = array();
355
	$ordered = array();
Line -... Line 356...
-
 
356
	foreach ($orderArray as $key) {
-
 
357
		if (array_key_exists($key, $array)) {
-
 
358
			$ordered[$key] = $array[$key];
-
 
359
			unset($array[$key]);
213
	foreach($orderArray as $key) {
360
		}
214
		if(array_key_exists($key, $array)) {
361
	}
215
			$ordered[$key] = $array[$key];
362
	return $ordered + $array;
216
			unset($array[$key]);
363
}
217
		}
364
 
218
	}
365
function _actuNewerFirst($a,$b) {
Line 219... Line -...
219
	return $ordered + $array;
-
 
220
}
-
 
221
 
-
 
222
// adaption aux templates existants: [score] => [weight]
-
 
223
function _weight2score(&$item, $key, $max) {
-
 
224
	$item['score'] = intval($item['weight'] / $max * 100);
-
 
225
	$item['date_creation'] = isset($item['date_creation']) ? strftime("%d %B %Y", strtotime($item['date_creation'])) : '';
-
 
226
	unset($item['weight']);
-
 
227
}
-
 
228
 
-
 
229
function _actuNewerFirst($a,$b) {
-
 
230
	return isset($a['date_creation']) && isset($b['date_creation']) ? strcmp($b['date_creation'], $a['date_creation']) : 0;
-
 
231
}
-
 
232
 
-
 
233
function sphinx_search($q = NULL, $page = 1) {
-
 
234
	if(!$q) return array();
-
 
235
 
-
 
236
	// quelques aliases pour faciliter l'usage sans passer par le full sphinxQL
-
 
237
	// $q = preg_replace('/\<actu\>(.*)/', '\1 @group_id i_spip', $q);
-
 
238
	// $q = preg_replace('/\<eflore\>(.*)/', '\1 @group_id i_bazar|i_coste', $q);
-
 
239
 
-
 
240
	$db = mysql_connect(SPHINX_DSN, NULL, NULL, TRUE);
-
 
241
	// AFAICT, pas de réel risque de SQL-injection du côté de sphinx (au pire, $req = FALSE)
-
 
242
	// et il serait dommage de devoir limiter la puissante syntaxe offerte à l'utilisation
-
 
243
	$req = mysql_query(sprintf("SELECT group_id, main_id FROM i_projet, i_spip, i_papyrus, i_bazar, i_coste, i_nvjfl WHERE MATCH('%s') LIMIT 50", $q), $db);
-
 
244
	if(!$req) return array();
-
 
245
 
-
 
246
	$res = array('spip' => array(), 'bazar' => array(), 'projet' => array(), 'papyrus' => array(), 'coste' => array(), 'nvjfl' => array());
-
 
247
	$ids_par_poids = array();
-
 
248
	while($rec = mysql_fetch_array($req, MYSQL_ASSOC)) {
-
 
249
		$res[$rec['group_id']][$rec['id']] = $rec;
-
 
250
		$ids_par_poids[] = $rec['main_id'];
-
 
251
	}
-
 
252
	$docs = array();
-
 
253
 
-
 
254
	// spip
-
 
255
	$spip = new MoteurRecherche_SPIP();
-
 
256
	foreach($spip->get(array_filter(array_keys($res['spip']), 'intval'), $q) as $v) {
-
 
257
		unset($res['spip'][$v['id']]['group_id'],
-
 
258
			  $res['spip'][$v['id']]['main_id'],
-
 
259
			  $res['spip'][$v['id']]['id']);
-
 
260
		// left: weight
-
 
261
		$docs['spip-' . $v['id']] = array_merge($v,	$res['spip'][$v['id']]);
-
 
262
	}
-
 
263
 
-
 
264
	// bazar
-
 
265
	$bazar = new MoteurRecherche_BAZAR();
-
 
266
	foreach($bazar->get(array_filter(array_keys($res['bazar']), 'intval'), $q) as $v) {
-
 
267
		unset($res['bazar'][$v['id']]['group_id'],
-
 
268
			  $res['bazar'][$v['id']]['main_id'],
-
 
269
			  $res['bazar'][$v['id']]['id']);
-
 
270
		// left: weight
-
 
271
		$docs['bazar-' . $v['id']] = array_merge($v, $res['bazar'][$v['id']]);
-
 
272
	}
-
 
273
 
-
 
274
	// projet
-
 
275
	$projet = new MoteurRecherche_PROJET();
-
 
276
	foreach($projet->get(array_filter(array_keys($res['projet']), 'intval'), $q) as $v) {
-
 
277
		unset($res['projet'][$v['id']]['group_id'],
-
 
278
			  $res['projet'][$v['id']]['main_id'],
-
 
279
			  $res['projet'][$v['id']]['id']);
-
 
280
		// left: weight
-
 
281
		$docs['projet-' . $v['id']] = array_merge($v, $res['projet'][$v['id']]);
-
 
282
	}
-
 
283
 
-
 
284
	// papyrus
-
 
285
	$papyrus = new MoteurRecherche_PAPYRUS();
-
 
286
	foreach($papyrus->get(array_filter(array_keys($res['papyrus']), 'intval'), $q) as $v) {
-
 
287
		unset($res['papyrus'][$v['id']]['group_id'],
-
 
288
			  $res['papyrus'][$v['id']]['main_id'],
-
 
289
			  $res['papyrus'][$v['id']]['id']);
-
 
290
		// left: weight
-
 
291
		$docs['papyrus-' . $v['id']] = array_merge($v, $res['papyrus'][$v['id']]);
-
 
292
	}
-
 
293
 
-
 
294
	// coste
-
 
295
	$coste = new MoteurRecherche_COSTE();
-
 
296
	foreach($coste->get(array_filter(array_keys($res['coste']), 'intval'), $q) as $v) {
-
 
297
		unset($res['coste'][$v['id']]['group_id'],
-
 
298
			  $res['coste'][$v['id']]['main_id'],
-
 
299
			  $res['coste'][$v['id']]['id']);
-
 
300
		// left: weight
-
 
301
		$docs['coste-' . $v['id']] = array_merge($v, $res['coste'][$v['id']]);
-
 
302
	}
-
 
303
 
-
 
304
	// nvjfl
-
 
305
	$nvjfl = new MoteurRecherche_NVJFL();
-
 
306
	foreach($nvjfl->get(array_filter(array_keys($res['nvjfl']), 'intval'), $q) as $v) {
-
 
307
		unset($res['nvjfl'][$v['id']]['group_id'],
-
 
308
			  $res['nvjfl'][$v['id']]['main_id'],
-
 
309
			  $res['nvjfl'][$v['id']]['id']);
-
 
310
		// left: weight
-
 
311
		$docs['nvjfl-' . $v['id']] = array_merge($v, $res['nvjfl'][$v['id']]);
-
 
312
	}
-
 
313
 
-
 
314
	// sort
-
 
315
	$sorted = _sortArrayByArray($docs, $ids_par_poids);
-
 
316
 
-
 
317
	$max = current($sorted);
-
 
318
	$max = $max['weight'];
-
 
319
 
-
 
320
	if(isset($_GET['tri']) && $_GET['tri'] == 'date')
-
 
321
		usort($sorted, '_actuNewerFirst');
-
 
322
 
-
 
323
	// transforme les clefs pour s'adapter aux templates existants
-
 
324
	array_walk($sorted, '_weight2score', $max);
-
 
325
 
-
 
326
	// var_dump($sorted);die;
-
 
327
	return $sorted;
-
 
328
}
-
 
329
 
-
 
330
 
-
 
331
/*
-
 
332
// http://www.php.net/manual/fr/sphinx.examples.php
-
 
333
$s = new SphinxClient;
-
 
334
$s->setServer("localhost", 9306);
-
 
335
$s->setMatchMode(SPH_MATCH_ANY);
-
 
336
$s->setMaxQueryTime(3);
-
 
337
var_dump($s->query("test"));
-
 
338
*/
-
 
339
 
-
 
340
 
-
 
341
/*
-
 
342
  Note: conversion côté client SQL:
-
 
343
  mysql -h0 -P 9306 < <(iconv -f utf8 -t latin1 <<<"SELECT * FROM i_projet, i_spip, i_papyrus, i_bazar WHERE MATCH('journée');")
-
 
344
*/
-
 
345
 
-
 
346
/*
-
 
347
  // test sorting
-
 
348
  // uncomment this:
366
	return isset($a['date_creation']) && isset($b['date_creation']) ? strcmp($b['date_creation'], $a['date_creation']) : 0;