Subversion Repositories Applications.papyrus

Rev

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

Rev 2103 Rev 2105
Line 18... Line 18...
18
'poids' => 0,
18
'poids' => 0,
19
'url' => '',
19
'url' => '',
20
'titre' => '',
20
'titre' => '',
21
'hreflang' => '',
21
'hreflang' => '',
22
'accesskey' => '',
22
'accesskey' => '',
23
'title' => '',
23
'title' => '', // balise 'title'
24
'date_creation' => '',
24
'date_creation' => '',
25
'description' => ''
25
'description' => ''
26
*/
26
*/
Line -... Line 27...
-
 
27
 
-
 
28
// pour strftime()
-
 
29
date_default_timezone_set('Europe/Paris');
-
 
30
setlocale(LC_TIME, 'fr_FR');
27
 
31
 
28
class MoteurRecherche_SPIP {
32
class MoteurRecherche_SPIP {
-
 
33
	public function get($ids, $q = NULL) {
29
	public function get($ids) {
34
		if(!$ids) return array();
-
 
35
		$db = DB::connect($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['bdd_dsn']);
30
		$db = DB::connect($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['bdd_dsn']);
36
		$req = $db->query(sprintf(<<<EOF
31
		$req = $db->query(sprintf('SELECT id_article AS id, titre, texte, date, lang as hreflang' .
37
SELECT id_article AS id, titre, texte, date AS date_creation, lang as hreflang
32
								  ' FROM spip_articles' .
38
FROM spip_articles
33
								  ' WHERE statut = "%s"' .
39
WHERE statut = "%s"
34
								  ' AND id_article IN (%s)',
40
AND id_article IN (%s)
-
 
41
EOF
35
 
42
								  ,
36
								  "publie",
43
								  "publie",
-
 
44
								  implode(',', $ids)));
-
 
45
 
37
								  implode(',', $ids)));
46
		(DB::isError($req)) ? die($req->getMessage()) : '';
38
		$content = array();
47
		$content = array();
39
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
48
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
40
			$rec['url_simple'] = sprintf("%s/article/%d.html",
49
			$rec['url_simple'] = sprintf("%s/article%d.html",
41
										 trim($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['url'], '/'),
50
										 trim($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['url'], '/'),
42
										 $rec['id']);
51
										 $rec['id']);
43
			// TODO: passer traiterMotif() en static
52
			$rec['url'] = sprintf("%s?var_recherche=%s",
44
			// $rec['url'] = $rec['url_simple'] . traiterMotif()
-
 
45
 
53
								  $rec['url_simple'],
46
			// TODO: passer couperTexte() en static
54
								  More_Recherche::s_traiterMotif($q, 'url'));
47
			$rec['description'] = substr(strip_tags($rec['texte']), 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
55
			$rec['description'] = More_Recherche::couperTexte($rec['texte'], MORE_RESULTAT_TAILLE_DESCRIPTION);
48
			unset($rec['texte']);
56
			unset($rec['texte']);
Line 49... Line 57...
49
			$content[$rec['id']] = $rec;
57
			$content[$rec['id']] = $rec;
50
 
58
 
51
		}
59
		}
52
		return $content;
60
		return $content;
Line 53... Line 61...
53
	}
61
	}
54
}
62
}
-
 
63
 
55
 
64
class MoteurRecherche_BAZAR {
-
 
65
	public function get($ids, $q = NULL) {
-
 
66
		if(!$ids) return array();
-
 
67
		$db = DB::connect($GLOBALS['_MOTEUR_RECHERCHE_']['bazar'][0]['bdd_dsn']);
-
 
68
		$req = $db->query(sprintf(<<<EOF
56
class MoteurRecherche_BAZAR {
69
SELECT bf_id_fiche AS id,
57
	public function get($ids) {
70
	   bf_description AS texte,
58
		$db = DB::connect($GLOBALS['_MOTEUR_RECHERCHE_']['bazar'][0]['bdd_dsn']);
71
	   bf_titre AS titre,
59
		$req = $db->query(sprintf('SELECT bf_id_fiche AS id, bf_description AS texte, bf_titre AS titre, bf_date_debut_evenement' .
72
	   bf_date_debut_evenement AS date_creation
-
 
73
FROM bazar_fiche
60
								  ' FROM bazar_fiche' .
74
WHERE bf_id_fiche IN (%s)
-
 
75
EOF
-
 
76
								  ,
61
								  ' WHERE bf_id_fiche IN (%s)',
77
								  implode(',', $ids)));
62
 
78
 
63
								  implode(',', $ids)));
-
 
64
		$content = array();
79
		(DB::isError($req)) ? die($req->getMessage()) : '';
65
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
-
 
66
			$rec['url_simple'] = $rec['url_simple'] =
80
		$content = array();
67
				sprintf(trim($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['url'], '/'),
81
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
68
						$rec['id']);
82
			$rec['url_simple'] = $rec['url'] = sprintf(trim($GLOBALS['_MOTEUR_RECHERCHE_']['bazar'][0]['url'], '/'), $rec['id']);
69
			$rec['description'] = substr(strip_tags($rec['bf_description']), 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
83
			$rec['description'] = More_Recherche::couperTexte($rec['texte'], MORE_RESULTAT_TAILLE_DESCRIPTION);
70
			unset($rec['texte']);
84
			unset($rec['texte']);
71
			$content[$rec['id']] = $rec;
85
			$content[$rec['id']] = $rec;
72
		}
86
		}
Line 73... Line 87...
73
		return $content;		
87
		return $content;		
74
	}
88
	}
-
 
89
}
75
}
90
 
-
 
91
class MoteurRecherche_PROJET {
76
 
92
	public function get($ids, $q = NULL) {
77
class MoteurRecherche_PROJET {
93
		if(!$ids) return array();
-
 
94
		$db = $GLOBALS['_MOTEUR_RECHERCHE_']['bd']['papyrus'];
-
 
95
		$req = $db->query(sprintf(<<<EOF
78
	public function get($ids) {
96
SELECT p_id AS id, p_titre, p_description, p_date_creation AS date_creation
-
 
97
FROM projet WHERE p_id IN (%s)
79
		$db = $GLOBALS['_MOTEUR_RECHERCHE_']['bd']['papyrus'];
98
EOF
80
		$req = $db->query(sprintf('SELECT p_id AS id, p_titre, p_description, p_date_creation AS date_creation' .
99
								  ,
81
								  ' FROM projet WHERE p_id IN (%s)',
100
								  implode(',', $ids)));
82
								  implode(',', $ids)));
101
 
83
		(DB::isError($req)) ? die($req->getMessage()) : '';
102
		(DB::isError($req)) ? die($req->getMessage()) : '';
84
		$content = array();
103
		$content = array();
85
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
104
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
86
			$rec['url_simple'] = $rec['url_simple'] = sprintf("%s?id_projet=%d",
105
			$rec['url_simple'] = $rec['url'] = sprintf("%s?id_projet=%d",
87
															  trim($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['url'], '/'),
106
													   trim($GLOBALS['_MOTEUR_RECHERCHE_']['projet'][0]['url'], '/'),
88
															  $rec['id']);
107
													   $rec['id']);
89
			$rec['description'] = substr(strip_tags($rec['p_description']), 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
108
			$rec['description'] = substr(strip_tags($rec['p_description']), 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
90
			unset($rec['p_description']);
109
			unset($rec['p_description']);
91
			$content[$rec['id']] = $rec;
110
			$content[$rec['id']] = $rec;
Line 92... Line 111...
92
		}
111
		}
93
		return $content;		
112
		return $content;		
-
 
113
	}
-
 
114
}
-
 
115
 
-
 
116
class MoteurRecherche_PAPYRUS {
-
 
117
	public function get($ids, $q = NULL) {
-
 
118
		if(!$ids) return array();
-
 
119
        $db = $GLOBALS['_MOTEUR_RECHERCHE_']['bd']['papyrus'];
-
 
120
		$req = $db->query(sprintf(<<<EOF
-
 
121
SELECT mc.gmc_ce_menu AS id,
-
 
122
	   IF(gm_nom != '', gm_nom, IF(gm_titre != '', gm_titre, gm_titre_alternatif)) AS titre,
-
 
123
	   gmc_contenu AS texte,
-
 
124
	   gm_description_libre, gm_description_resume,
-
 
125
	   gm_mots_cles,gm_source, gm_auteur, gm_contributeur, gm_editeur, gm_categorie, gm_date_creation AS date_creation
-
 
126
FROM gen_menu m
-
 
127
LEFT JOIN gen_menu_contenu mc ON mc.gmc_ce_menu = m.gm_id_menu AND mc.gmc_bool_dernier = 1
-
 
128
WHERE mc.gmc_ce_menu IN (%s)
-
 
129
EOF
-
 
130
								  ,
-
 
131
								  implode(',', $ids)));
-
 
132
 
-
 
133
		(DB::isError($req)) ? die($req->getMessage()) : '';
-
 
134
		$content = array();
-
 
135
		while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
-
 
136
			// Création de l'url
-
 
137
			// TODO : utiliser comme pour spip un fichier de config spécifique pour virer PAP_URL d'ici
-
 
138
			$une_url = new Pap_URL(PAP_URL);
-
 
139
			$une_url->setId($rec['id']);
-
 
140
			$rec['url_simple'] = $une_url->getURL();
-
 
141
			$une_url->addQueryString('var_recherche', More_Recherche::s_traiterMotif($q, 'url'), true);
-
 
142
			$rec['url'] = $une_url->getURL();
-
 
143
 
94
	}
144
			$rec['description'] =  htmlentities($rec['gm_description_resume']);
95
}
145
			unset($rec['gm_description_resume']);
Line -... Line 146...
-
 
146
			$content[$rec['id']] = $rec;
96
 
147
		}
97
class MoteurRecherche_PAPYRUS {
148
		return $content;		
98
	public function get($ids) {
149
	}
99
	}
150
}
100
}
151
 
101
 
152
 
102
// http://stackoverflow.com/questions/348410/sort-an-array-based-on-another-array
153
// http://stackoverflow.com/questions/348410/sort-an-array-based-on-another-array
103
function _sortArrayByArray($array, $orderArray) {
154
function _sortArrayByArray($array, $orderArray) {
104
	$ordered = array();
155
	$ordered = array();
105
	foreach($orderArray as $key) {
156
	foreach($orderArray as $key) {
106
		if(array_key_exists($key, $array)) {
157
		if(array_key_exists($key, $array)) {
Line 107... Line 158...
107
			$ordered[$key] = $array[$key];
158
			$ordered[$key] = $array[$key];
108
			unset($array[$key]);
-
 
109
		}
-
 
110
	}
-
 
111
	return $ordered + $array;
-
 
112
}	
-
 
113
 
159
			unset($array[$key]);
114
$q = $_SESSION['_MOTEUR_RECHERCHE_']['rechercher']['more_motif'];
160
		}
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
161
	}
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
 
162
	return $ordered + $array;
147
// projet
-
 
148
$projet = new MoteurRecherche_PROJET();
163
}
Line -... Line 164...
-
 
164
 
-
 
165
// adaption aux templates existants: [score] => [weight]
-
 
166
function _weight2score(&$item, $key, $max) {
-
 
167
	$item['score'] = intval($item['weight'] / $max * 100);
-
 
168
	$item['date_creation'] = strftime("%d %B %Y", strtotime($item['date_creation']));
-
 
169
	unset($item['weight']);
-
 
170
}
-
 
171
 
-
 
172
 
-
 
173
// ce fichier/cette fonction peut-être réclamé plusieurs fois
-
 
174
// car le motif du template '{{MoteurRecherche}}' est inclu récursivement,
-
 
175
// (la première substitution fait réapparaître '{{MoteurRecherche}}')
-
 
176
function sphinx_search($q = NULL, $page = 1) {
-
 
177
	if(!$q) return array();
-
 
178
 
-
 
179
	$db = mysql_connect('127.0.0.1:9306', 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);
-
 
181
 
149
foreach($projet->get(array_filter(array_keys($res['projet']), 'intval')) as $v) {
182
	$res = array('spip' => array(), 'bazar' => array(), 'projet' => array(), 'papyrus' => array());
-
 
183
	$ids_par_poids = array();
150
	unset($res['projet'][$v['id']]['group_id'],
184
	while($rec = mysql_fetch_array($req, MYSQL_ASSOC)) {
-
 
185
		$res[$rec['group_id']][$rec['id']] = $rec;
-
 
186
		$ids_par_poids[] = $rec['main_id'];
-
 
187
	}
151
		  $res['projet'][$v['id']]['main_id'],
188
 
-
 
189
	$docs = array();
-
 
190
 
-
 
191
	// spip
-
 
192
	$spip = new MoteurRecherche_SPIP();
-
 
193
	foreach($spip->get(array_filter(array_keys($res['spip']), 'intval'), $q) as $v) {
152
		  $res['projet'][$v['id']]['id']);
194
		unset($res['spip'][$v['id']]['group_id'],
-
 
195
			  $res['spip'][$v['id']]['main_id'],
-
 
196
			  $res['spip'][$v['id']]['id']);
-
 
197
		// left: weight
-
 
198
		$docs['spip-' . $v['id']] = array_merge($v,	$res['spip'][$v['id']]);
-
 
199
	}
-
 
200
 
-
 
201
	// bazar
-
 
202
	$bazar = new MoteurRecherche_BAZAR();
-
 
203
	foreach($bazar->get(array_filter(array_keys($res['bazar']), 'intval'), $q) as $v) {
-
 
204
		unset($res['bazar'][$v['id']]['group_id'],
-
 
205
			  $res['bazar'][$v['id']]['main_id'],
-
 
206
			  $res['bazar'][$v['id']]['id']);
-
 
207
		// left: weight
-
 
208
		$docs['bazar-' . $v['id']] = array_merge($v, $res['bazar'][$v['id']]);
-
 
209
	}
-
 
210
 
-
 
211
	// projet
153
	// left: weight
212
	$projet = new MoteurRecherche_PROJET();
-
 
213
	foreach($projet->get(array_filter(array_keys($res['projet']), 'intval'), $q) as $v) {
-
 
214
		unset($res['projet'][$v['id']]['group_id'],
-
 
215
			  $res['projet'][$v['id']]['main_id'],
-
 
216
			  $res['projet'][$v['id']]['id']);
-
 
217
		// left: weight
-
 
218
		$docs['projet-' . $v['id']] = array_merge($v, $res['projet'][$v['id']]);
-
 
219
	}
-
 
220
 
-
 
221
	// papyrus
-
 
222
	$papyrus = new MoteurRecherche_PAPYRUS();
Line -... Line 223...
-
 
223
	foreach($papyrus->get(array_filter(array_keys($res['papyrus']), 'intval'), $q) as $v) {
-
 
224
		unset($res['papyrus'][$v['id']]['group_id'],
-
 
225
			  $res['papyrus'][$v['id']]['main_id'],
-
 
226
			  $res['papyrus'][$v['id']]['id']);
-
 
227
		// left: weight
-
 
228
		$docs['papyrus-' . $v['id']] = array_merge($v, $res['papyrus'][$v['id']]);
-
 
229
	}
-
 
230
 
-
 
231
	$sorted = _sortArrayByArray($docs, $ids_par_poids);
-
 
232
 
-
 
233
	// sort
-
 
234
	/*
-
 
235
	// uncomment this:
-
 
236
	print_r(array_keys($sorted); die;
-
 
237
 
-
 
238
	// then:
-
 
239
	ddiff
Line -... Line 240...
-
 
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) \
-
 
241
	<(POST http://localhost/site:reseau<<<"more_motif=test&"|awk -F'=>' '{print $2}'|sed -e 's/ //g' -e '/^$/d')
-
 
242
 
Line 154... Line 243...
154
	$docs['projet-' . $v['id']] = array_merge($v, $res['projet'][$v['id']]);
243
	// both should be equal.
155
}
244
	// [ SELECT main_id, group_id FROM i_projet, i_spip, i_papyrus, i_bazar WHERE MATCH('test') LIMIT 50; ]
156
 
245
	*/
157
 
246
 
158
// sort
247
	$max = current($sorted);
159
/* var_dump(array_keys(_sortArrayByArray($docs, $ids_par_poids))); die;
248
	$max = $max['weight'];
160
// should be equal to
249
	array_walk($sorted, '_weight2score', $max);
161
// SELECT main_id, group_id FROM i_projet, i_spip, i_papyrus, i_bazar WHERE MATCH('test') LIMIT 50; */
250
 
162
return "BLAH";
-