Subversion Repositories Applications.papyrus

Compare Revisions

Ignore whitespace Rev 2104 → Rev 2105

/trunk/papyrus/applettes/moteur_recherche/moteur_recherche_sphinx.php
20,31 → 20,39
'titre' => '',
'hreflang' => '',
'accesskey' => '',
'title' => '',
'title' => '', // balise 'title'
'date_creation' => '',
'description' => ''
*/
 
// pour strftime()
date_default_timezone_set('Europe/Paris');
setlocale(LC_TIME, 'fr_FR');
 
class MoteurRecherche_SPIP {
public function get($ids) {
public function get($ids, $q = NULL) {
if(!$ids) return array();
$db = DB::connect($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['bdd_dsn']);
$req = $db->query(sprintf('SELECT id_article AS id, titre, texte, date, lang as hreflang' .
' FROM spip_articles' .
' WHERE statut = "%s"' .
' AND id_article IN (%s)',
 
$req = $db->query(sprintf(<<<EOF
SELECT id_article AS id, titre, texte, date AS date_creation, lang as hreflang
FROM spip_articles
WHERE statut = "%s"
AND id_article IN (%s)
EOF
,
"publie",
implode(',', $ids)));
 
(DB::isError($req)) ? die($req->getMessage()) : '';
$content = array();
while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
$rec['url_simple'] = sprintf("%s/article/%d.html",
$rec['url_simple'] = sprintf("%s/article%d.html",
trim($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['url'], '/'),
$rec['id']);
// TODO: passer traiterMotif() en static
// $rec['url'] = $rec['url_simple'] . traiterMotif()
 
// TODO: passer couperTexte() en static
$rec['description'] = substr(strip_tags($rec['texte']), 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
$rec['url'] = sprintf("%s?var_recherche=%s",
$rec['url_simple'],
More_Recherche::s_traiterMotif($q, 'url'));
$rec['description'] = More_Recherche::couperTexte($rec['texte'], MORE_RESULTAT_TAILLE_DESCRIPTION);
unset($rec['texte']);
$content[$rec['id']] = $rec;
 
54,19 → 62,25
}
 
class MoteurRecherche_BAZAR {
public function get($ids) {
public function get($ids, $q = NULL) {
if(!$ids) return array();
$db = DB::connect($GLOBALS['_MOTEUR_RECHERCHE_']['bazar'][0]['bdd_dsn']);
$req = $db->query(sprintf('SELECT bf_id_fiche AS id, bf_description AS texte, bf_titre AS titre, bf_date_debut_evenement' .
' FROM bazar_fiche' .
' WHERE bf_id_fiche IN (%s)',
$req = $db->query(sprintf(<<<EOF
SELECT bf_id_fiche AS id,
bf_description AS texte,
bf_titre AS titre,
bf_date_debut_evenement AS date_creation
FROM bazar_fiche
WHERE bf_id_fiche IN (%s)
EOF
,
implode(',', $ids)));
 
implode(',', $ids)));
(DB::isError($req)) ? die($req->getMessage()) : '';
$content = array();
while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
$rec['url_simple'] = $rec['url_simple'] =
sprintf(trim($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['url'], '/'),
$rec['id']);
$rec['description'] = substr(strip_tags($rec['bf_description']), 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
$rec['url_simple'] = $rec['url'] = sprintf(trim($GLOBALS['_MOTEUR_RECHERCHE_']['bazar'][0]['url'], '/'), $rec['id']);
$rec['description'] = More_Recherche::couperTexte($rec['texte'], MORE_RESULTAT_TAILLE_DESCRIPTION);
unset($rec['texte']);
$content[$rec['id']] = $rec;
}
75,17 → 89,22
}
 
class MoteurRecherche_PROJET {
public function get($ids) {
public function get($ids, $q = NULL) {
if(!$ids) return array();
$db = $GLOBALS['_MOTEUR_RECHERCHE_']['bd']['papyrus'];
$req = $db->query(sprintf('SELECT p_id AS id, p_titre, p_description, p_date_creation AS date_creation' .
' FROM projet WHERE p_id IN (%s)',
$req = $db->query(sprintf(<<<EOF
SELECT p_id AS id, p_titre, p_description, p_date_creation AS date_creation
FROM projet WHERE p_id IN (%s)
EOF
,
implode(',', $ids)));
 
(DB::isError($req)) ? die($req->getMessage()) : '';
$content = array();
while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
$rec['url_simple'] = $rec['url_simple'] = sprintf("%s?id_projet=%d",
trim($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][0]['url'], '/'),
$rec['id']);
$rec['url_simple'] = $rec['url'] = sprintf("%s?id_projet=%d",
trim($GLOBALS['_MOTEUR_RECHERCHE_']['projet'][0]['url'], '/'),
$rec['id']);
$rec['description'] = substr(strip_tags($rec['p_description']), 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
unset($rec['p_description']);
$content[$rec['id']] = $rec;
95,10 → 114,42
}
 
class MoteurRecherche_PAPYRUS {
public function get($ids) {
public function get($ids, $q = NULL) {
if(!$ids) return array();
$db = $GLOBALS['_MOTEUR_RECHERCHE_']['bd']['papyrus'];
$req = $db->query(sprintf(<<<EOF
SELECT mc.gmc_ce_menu AS id,
IF(gm_nom != '', gm_nom, IF(gm_titre != '', gm_titre, gm_titre_alternatif)) AS titre,
gmc_contenu AS texte,
gm_description_libre, gm_description_resume,
gm_mots_cles,gm_source, gm_auteur, gm_contributeur, gm_editeur, gm_categorie, gm_date_creation AS date_creation
FROM gen_menu m
LEFT JOIN gen_menu_contenu mc ON mc.gmc_ce_menu = m.gm_id_menu AND mc.gmc_bool_dernier = 1
WHERE mc.gmc_ce_menu IN (%s)
EOF
,
implode(',', $ids)));
 
(DB::isError($req)) ? die($req->getMessage()) : '';
$content = array();
while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
// Création de l'url
// TODO : utiliser comme pour spip un fichier de config spécifique pour virer PAP_URL d'ici
$une_url = new Pap_URL(PAP_URL);
$une_url->setId($rec['id']);
$rec['url_simple'] = $une_url->getURL();
$une_url->addQueryString('var_recherche', More_Recherche::s_traiterMotif($q, 'url'), true);
$rec['url'] = $une_url->getURL();
 
$rec['description'] = htmlentities($rec['gm_description_resume']);
unset($rec['gm_description_resume']);
$content[$rec['id']] = $rec;
}
return $content;
}
}
 
 
// http://stackoverflow.com/questions/348410/sort-an-array-based-on-another-array
function _sortArrayByArray($array, $orderArray) {
$ordered = array();
109,61 → 160,99
}
}
return $ordered + $array;
}
}
 
$q = $_SESSION['_MOTEUR_RECHERCHE_']['rechercher']['more_motif'];
$db = mysql_connect('127.0.0.1:9306', NULL, NULL, TRUE);
$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);
 
$res = array();
$ids_par_poids = array();
while($rec = mysql_fetch_array($req, MYSQL_ASSOC)) {
$res[$rec['group_id']][$rec['id']] = $rec;
$ids_par_poids[] = $rec['main_id'];
// adaption aux templates existants: [score] => [weight]
function _weight2score(&$item, $key, $max) {
$item['score'] = intval($item['weight'] / $max * 100);
$item['date_creation'] = strftime("%d %B %Y", strtotime($item['date_creation']));
unset($item['weight']);
}
 
$docs = array();
 
// spip
$spip = new MoteurRecherche_SPIP();
foreach($spip->get(array_filter(array_keys($res['spip']), 'intval')) as $v) {
unset($res['spip'][$v['id']]['group_id'],
$res['spip'][$v['id']]['main_id'],
$res['spip'][$v['id']]['id']);
// left: weight
$docs['spip-' . $v['id']] = array_merge($v, $res['spip'][$v['id']]);
}
// ce fichier/cette fonction peut-être réclamé plusieurs fois
// car le motif du template '{{MoteurRecherche}}' est inclu récursivement,
// (la première substitution fait réapparaître '{{MoteurRecherche}}')
function sphinx_search($q = NULL, $page = 1) {
if(!$q) return array();
 
// bazar
$bazar = new MoteurRecherche_BAZAR();
foreach($bazar->get(array_filter(array_keys($res['bazar']), 'intval')) as $v) {
unset($res['bazar'][$v['id']]['group_id'],
$res['bazar'][$v['id']]['main_id'],
$res['bazar'][$v['id']]['id']);
// left: weight
$docs['bazar-' . $v['id']] = array_merge($v, $res['bazar'][$v['id']]);
}
$db = mysql_connect('127.0.0.1:9306', NULL, NULL, TRUE);
$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);
 
// projet
$projet = new MoteurRecherche_PROJET();
foreach($projet->get(array_filter(array_keys($res['projet']), 'intval')) as $v) {
unset($res['projet'][$v['id']]['group_id'],
$res['projet'][$v['id']]['main_id'],
$res['projet'][$v['id']]['id']);
// left: weight
$docs['projet-' . $v['id']] = array_merge($v, $res['projet'][$v['id']]);
}
$res = array('spip' => array(), 'bazar' => array(), 'projet' => array(), 'papyrus' => array());
$ids_par_poids = array();
while($rec = mysql_fetch_array($req, MYSQL_ASSOC)) {
$res[$rec['group_id']][$rec['id']] = $rec;
$ids_par_poids[] = $rec['main_id'];
}
 
$docs = array();
 
// sort
/* var_dump(array_keys(_sortArrayByArray($docs, $ids_par_poids))); die;
// should be equal to
// SELECT main_id, group_id FROM i_projet, i_spip, i_papyrus, i_bazar WHERE MATCH('test') LIMIT 50; */
return "BLAH";
// spip
$spip = new MoteurRecherche_SPIP();
foreach($spip->get(array_filter(array_keys($res['spip']), 'intval'), $q) as $v) {
unset($res['spip'][$v['id']]['group_id'],
$res['spip'][$v['id']]['main_id'],
$res['spip'][$v['id']]['id']);
// left: weight
$docs['spip-' . $v['id']] = array_merge($v, $res['spip'][$v['id']]);
}
 
// bazar
$bazar = new MoteurRecherche_BAZAR();
foreach($bazar->get(array_filter(array_keys($res['bazar']), 'intval'), $q) as $v) {
unset($res['bazar'][$v['id']]['group_id'],
$res['bazar'][$v['id']]['main_id'],
$res['bazar'][$v['id']]['id']);
// left: weight
$docs['bazar-' . $v['id']] = array_merge($v, $res['bazar'][$v['id']]);
}
 
// projet
$projet = new MoteurRecherche_PROJET();
foreach($projet->get(array_filter(array_keys($res['projet']), 'intval'), $q) as $v) {
unset($res['projet'][$v['id']]['group_id'],
$res['projet'][$v['id']]['main_id'],
$res['projet'][$v['id']]['id']);
// left: weight
$docs['projet-' . $v['id']] = array_merge($v, $res['projet'][$v['id']]);
}
 
// papyrus
$papyrus = new MoteurRecherche_PAPYRUS();
foreach($papyrus->get(array_filter(array_keys($res['papyrus']), 'intval'), $q) as $v) {
unset($res['papyrus'][$v['id']]['group_id'],
$res['papyrus'][$v['id']]['main_id'],
$res['papyrus'][$v['id']]['id']);
// left: weight
$docs['papyrus-' . $v['id']] = array_merge($v, $res['papyrus'][$v['id']]);
}
 
$sorted = _sortArrayByArray($docs, $ids_par_poids);
 
// sort
/*
// uncomment this:
print_r(array_keys($sorted); die;
 
// then:
ddiff
<(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) \
<(POST http://localhost/site:reseau<<<"more_motif=test&"|awk -F'=>' '{print $2}'|sed -e 's/ //g' -e '/^$/d')
 
// both should be equal.
// [ SELECT main_id, group_id FROM i_projet, i_spip, i_papyrus, i_bazar WHERE MATCH('test') LIMIT 50; ]
*/
 
$max = current($sorted);
$max = $max['weight'];
array_walk($sorted, '_weight2score', $max);
 
//var_dump($sorted);die;
return $sorted;
}
 
 
/*
// http://www.php.net/manual/fr/sphinx.examples.php
$s = new SphinxClient;
172,4 → 261,3
$s->setMaxQueryTime(3);
var_dump($s->query("test"));
*/