Subversion Repositories Applications.papyrus

Rev

Rev 2105 | Go to most recent revision | Details | 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/
15
*/
16
 
17
/* returned struct:
18
'poids' => 0,
19
'url' => '',
20
'titre' => '',
21
'hreflang' => '',
22
'accesskey' => '',
23
'title' => '',
24
'date_creation' => '',
25
'description' => ''
26
*/
27
 
28
class MoteurRecherche_SPIP {
29
	public function get($ids) {
30
		$db = DB::connect($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['bdd_dsn']);
31
		$req = $db->query(sprintf('SELECT id_article AS id, titre, texte, date, lang as hreflang' .
32
								  ' FROM spip_articles' .
33
								  ' WHERE statut = "%s"' .
34
								  ' AND id_article IN (%s)',
35
 
36
								  "publie",
37
								  implode(',', $ids)));
38
		$content = array();
39
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
40
			$rec['url_simple'] = sprintf("%s/article/%d.html",
41
										 trim($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['url'], '/'),
42
										 $rec['id']);
43
			// TODO: passer traiterMotif() en static
44
			// $rec['url'] = $rec['url_simple'] . traiterMotif()
45
 
46
			// TODO: passer couperTexte() en static
47
			$rec['description'] = substr(strip_tags($rec['texte']), 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
48
			unset($rec['texte']);
49
			$content[$rec['id']] = $rec;
50
 
51
		}
52
		return $content;
53
	}
54
}
55
 
56
class MoteurRecherche_BAZAR {
57
	public function get($ids) {
58
		$db = DB::connect($GLOBALS['_MOTEUR_RECHERCHE_']['bazar'][0]['bdd_dsn']);
59
		$req = $db->query(sprintf('SELECT bf_id_fiche AS id, bf_description AS texte, bf_titre AS titre, bf_date_debut_evenement' .
60
								  ' FROM bazar_fiche' .
61
								  ' WHERE bf_id_fiche IN (%s)',
62
 
63
								  implode(',', $ids)));
64
		$content = array();
65
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
66
			$rec['url_simple'] = $rec['url_simple'] =
67
				sprintf(trim($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['url'], '/'),
68
						$rec['id']);
69
			$rec['description'] = substr(strip_tags($rec['bf_description']), 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
70
			unset($rec['texte']);
71
			$content[$rec['id']] = $rec;
72
		}
73
		return $content;
74
	}
75
}
76
 
77
class MoteurRecherche_PROJET {
78
	public function get($ids) {
79
		$db = $GLOBALS['_MOTEUR_RECHERCHE_']['bd']['papyrus'];
80
		$req = $db->query(sprintf('SELECT p_id AS id, p_titre, p_description, p_date_creation AS date_creation' .
81
								  ' FROM projet WHERE p_id IN (%s)',
82
								  implode(',', $ids)));
83
		(DB::isError($req)) ? die($req->getMessage()) : '';
84
		$content = array();
85
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
86
			$rec['url_simple'] = $rec['url_simple'] = sprintf("%s?id_projet=%d",
87
															  trim($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['url'], '/'),
88
															  $rec['id']);
89
			$rec['description'] = substr(strip_tags($rec['p_description']), 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
90
			unset($rec['p_description']);
91
			$content[$rec['id']] = $rec;
92
		}
93
		return $content;
94
	}
95
}
96
 
97
class MoteurRecherche_PAPYRUS {
98
	public function get($ids) {
99
	}
100
}
101
 
102
// http://stackoverflow.com/questions/348410/sort-an-array-based-on-another-array
103
function _sortArrayByArray($array, $orderArray) {
104
	$ordered = array();
105
	foreach($orderArray as $key) {
106
		if(array_key_exists($key, $array)) {
107
			$ordered[$key] = $array[$key];
108
			unset($array[$key]);
109
		}
110
	}
111
	return $ordered + $array;
112
}
113
 
114
$q = $_SESSION['_MOTEUR_RECHERCHE_']['rechercher']['more_motif'];
115
$db = mysql_connect('127.0.0.1:9306', NULL, NULL, TRUE);
116
$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);
117
 
118
$res = array();
119
$ids_par_poids = array();
120
while($rec = mysql_fetch_array($req, MYSQL_ASSOC)) {
121
	$res[$rec['group_id']][$rec['id']] = $rec;
122
	$ids_par_poids[] = $rec['main_id'];
123
}
124
 
125
$docs = array();
126
 
127
// spip
128
$spip = new MoteurRecherche_SPIP();
129
foreach($spip->get(array_filter(array_keys($res['spip']), 'intval')) as $v) {
130
	unset($res['spip'][$v['id']]['group_id'],
131
		  $res['spip'][$v['id']]['main_id'],
132
		  $res['spip'][$v['id']]['id']);
133
	// left: weight
134
	$docs['spip-' . $v['id']] = array_merge($v,	$res['spip'][$v['id']]);
135
}
136
 
137
// bazar
138
$bazar = new MoteurRecherche_BAZAR();
139
foreach($bazar->get(array_filter(array_keys($res['bazar']), 'intval')) as $v) {
140
	unset($res['bazar'][$v['id']]['group_id'],
141
		  $res['bazar'][$v['id']]['main_id'],
142
		  $res['bazar'][$v['id']]['id']);
143
	// left: weight
144
	$docs['bazar-' . $v['id']] = array_merge($v, $res['bazar'][$v['id']]);
145
}
146
 
147
// projet
148
$projet = new MoteurRecherche_PROJET();
149
foreach($projet->get(array_filter(array_keys($res['projet']), 'intval')) as $v) {
150
	unset($res['projet'][$v['id']]['group_id'],
151
		  $res['projet'][$v['id']]['main_id'],
152
		  $res['projet'][$v['id']]['id']);
153
	// left: weight
154
	$docs['projet-' . $v['id']] = array_merge($v, $res['projet'][$v['id']]);
155
}
156
 
157
 
158
// sort
159
/* var_dump(array_keys(_sortArrayByArray($docs, $ids_par_poids))); die;
160
// should be equal to
161
// SELECT main_id, group_id FROM i_projet, i_spip, i_papyrus, i_bazar WHERE MATCH('test') LIMIT 50; */
162
return "BLAH";
163
 
164
 
165
 
166
 
167
/*
168
// http://www.php.net/manual/fr/sphinx.examples.php
169
$s = new SphinxClient;
170
$s->setServer("localhost", 9306);
171
$s->setMatchMode(SPH_MATCH_ANY);
172
$s->setMaxQueryTime(3);
173
var_dump($s->query("test"));
174
*/
175