Subversion Repositories Applications.papyrus

Rev

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

Rev 2113 Rev 2117
Line 26... Line 26...
26
'date_creation' => '',
26
'date_creation' => '',
27
'description' => ''
27
'description' => ''
28
*/
28
*/
Line 29... Line 29...
29
 
29
 
-
 
30
define('SPHINX_DSN', '127.0.0.1:9306');
Line 30... Line 31...
30
define('SPHINX_DSN', '127.0.0.1:9306');
31
define('_MRS_SPHINX_BASEHOST', $_SERVER['HTTP_HOST']);
31
 
32
 
32
// pour strftime()
33
// pour strftime()
Line 95... Line 96...
95
class MoteurRecherche_PROJET {
96
class MoteurRecherche_PROJET {
96
	public function get($ids, $q = NULL) {
97
	public function get($ids, $q = NULL) {
97
		if(!$ids) return array();
98
		if(!$ids) return array();
98
		$db = $GLOBALS['_MOTEUR_RECHERCHE_']['bd']['papyrus'];
99
		$db = $GLOBALS['_MOTEUR_RECHERCHE_']['bd']['papyrus'];
99
		$req = $db->query(sprintf(<<<EOF
100
		$req = $db->query(sprintf(<<<EOF
100
SELECT p_id AS id, p_titre, p_description, p_date_creation AS date_creation
101
SELECT p_id AS id, p_titre AS titre, p_description, p_date_creation AS date_creation
101
FROM projet WHERE p_id IN (%s)
102
FROM projet WHERE p_id IN (%s)
102
EOF
103
EOF
103
								  ,
104
								  ,
104
								  implode(',', $ids)));
105
								  implode(',', $ids)));
Line 151... Line 152...
151
		}
152
		}
152
		return $content;		
153
		return $content;		
153
	}
154
	}
154
}
155
}
Line -... Line 156...
-
 
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)
-
 
167
EOF
-
 
168
								  ,
-
 
169
								  implode(',', $ids)));
-
 
170
 
-
 
171
		(DB::isError($req)) ? die($req->getMessage()) : '';
-
 
172
		$content = array();
-
 
173
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
-
 
174
			$rec['url_simple'] = $rec['url'] = sprintf("http://%s/bdtfx-nn-%d", _MRS_SPHINX_BASEHOST, $rec['id']);
-
 
175
			// TODO: interpret wikini
-
 
176
			$rec['description'] = substr($rec['description'], 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
-
 
177
			$content[$rec['id']] = $rec;
-
 
178
		}
-
 
179
		return $content;		
-
 
180
	}
-
 
181
}
Line 155... Line 182...
155
 
182
 
156
 
183
 
157
// http://stackoverflow.com/questions/348410/sort-an-array-based-on-another-array
184
// http://stackoverflow.com/questions/348410/sort-an-array-based-on-another-array
158
function _sortArrayByArray($array, $orderArray) {
185
function _sortArrayByArray($array, $orderArray) {
Line 167... Line 194...
167
}
194
}
Line 168... Line 195...
168
 
195
 
169
// adaption aux templates existants: [score] => [weight]
196
// adaption aux templates existants: [score] => [weight]
170
function _weight2score(&$item, $key, $max) {
197
function _weight2score(&$item, $key, $max) {
171
	$item['score'] = intval($item['weight'] / $max * 100);
198
	$item['score'] = intval($item['weight'] / $max * 100);
172
	$item['date_creation'] = strftime("%d %B %Y", strtotime($item['date_creation']));
199
	$item['date_creation'] = isset($item['date_creation']) ? strftime("%d %B %Y", strtotime($item['date_creation'])) : '';
173
	unset($item['weight']);
200
	unset($item['weight']);
Line 174... Line 201...
174
}
201
}
175
 
202
 
Line 176... Line 203...
176
function sphinx_search($q = NULL, $page = 1) {
203
function sphinx_search($q = NULL, $page = 1) {
177
	if(!$q) return array();
204
	if(!$q) return array();
Line 178... Line 205...
178
 
205
 
179
	$db = mysql_connect(SPHINX_DSN, NULL, NULL, TRUE);
206
	$db = mysql_connect(SPHINX_DSN, NULL, NULL, TRUE);
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);
207
	$req = mysql_query(sprintf("SELECT group_id, main_id FROM i_projet, i_spip, i_papyrus, i_bazar, i_coste WHERE MATCH('%s') LIMIT 50", $q), $db);
181
 
208
 
182
	$res = array('spip' => array(), 'bazar' => array(), 'projet' => array(), 'papyrus' => array());
209
	$res = array('spip' => array(), 'bazar' => array(), 'projet' => array(), 'papyrus' => array(), 'coste' => array());
183
	$ids_par_poids = array();
210
	$ids_par_poids = array();
184
	while($rec = mysql_fetch_array($req, MYSQL_ASSOC)) {
-
 
185
		$res[$rec['group_id']][$rec['id']] = $rec;
211
	while($rec = mysql_fetch_array($req, MYSQL_ASSOC)) {
Line 186... Line 212...
186
		$ids_par_poids[] = $rec['main_id'];
212
		$res[$rec['group_id']][$rec['id']] = $rec;
187
	}
213
		$ids_par_poids[] = $rec['main_id'];
188
 
214
	}
Line 226... Line 252...
226
			  $res['papyrus'][$v['id']]['id']);
252
			  $res['papyrus'][$v['id']]['id']);
227
		// left: weight
253
		// left: weight
228
		$docs['papyrus-' . $v['id']] = array_merge($v, $res['papyrus'][$v['id']]);
254
		$docs['papyrus-' . $v['id']] = array_merge($v, $res['papyrus'][$v['id']]);
229
	}
255
	}
Line -... Line 256...
-
 
256
 
-
 
257
	// coste
-
 
258
	$coste = new MoteurRecherche_COSTE();
-
 
259
	foreach($coste->get(array_filter(array_keys($res['coste']), 'intval'), $q) as $v) {
-
 
260
		unset($res['coste'][$v['id']]['group_id'],
-
 
261
			  $res['coste'][$v['id']]['main_id'],
-
 
262
			  $res['coste'][$v['id']]['id']);
-
 
263
		// left: weight
-
 
264
		$docs['coste-' . $v['id']] = array_merge($v, $res['coste'][$v['id']]);
-
 
265
	}
230
 
266
 
Line 231... Line 267...
231
	$sorted = _sortArrayByArray($docs, $ids_par_poids);
267
	$sorted = _sortArrayByArray($docs, $ids_par_poids);
232
 
268
 
233
	// sort
269
	// sort
234
	/*
270
	/*
Line 235... Line 271...
235
	// uncomment this:
271
	// uncomment this:
236
	print_r(array_keys($sorted); die;
272
	print_r(array_keys($sorted); die;
237
 
273
 
238
	// then:
274
	// then:
Line 239... Line 275...
239
	ddiff
275
	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) \
276
	<(mysql -h0 -P 9306 <<<"SELECT main_id FROM i_projet, i_spip, i_papyrus, i_bazar, i_coste 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')
277
	<(POST http://localhost/site:reseau<<<"more_motif=test&"|awk -F'=>' '{print $2}'|sed -e 's/ //g' -e '/^$/d')
Line 242... Line 278...
242
 
278
 
243
	// both should be equal.
279
	// both should be equal.
244
	// [ SELECT main_id, group_id FROM i_projet, i_spip, i_papyrus, i_bazar WHERE MATCH('test') LIMIT 50; ]
280
	// [ SELECT main_id, group_id FROM i_projet, i_spip, i_papyrus, i_bazar WHERE MATCH('test') LIMIT 50; ]
Line 245... Line 281...
245
	*/
281
	*/
246
 
282
 
247
	$max = current($sorted);
283
	$max = current($sorted);
Line 248... Line 284...
248
	$max = $max['weight'];
284
	$max = $max['weight'];
Line 265... Line 301...
265
 
301
 
266
/*
302
/*
267
  Note: conversion côté client SQL:
303
  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');")
304
  mysql -h0 -P 9306 < <(iconv -f utf8 -t latin1 <<<"SELECT * FROM i_projet, i_spip, i_papyrus, i_bazar WHERE MATCH('journée');")
269
*/
-