Subversion Repositories Applications.papyrus

Rev

Rev 2141 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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