Subversion Repositories Applications.papyrus

Rev

Rev 2106 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
/*
 * Moteur de recherche SPHINX
 * @author        Raphaƫl Droz <raphael@tela-botanica.org
 * @copyright     Tela-Botanica 2013
 */

/*
  sudo urpmi lib64sphinxclient-devel
  sudo pecl install sphinx

  see also: http://www.ibm.com/developerworks/library/os-sphinx/
  see also: http://sphinxsearch.com/docs/manual-2.0.7.html#extended-syntax
  TODO: http://sphinxsearch.com/blog/2010/08/17/how-sphinx-relevance-ranking-works/
*/

/* returned struct:
'poids' => 0,
'url' => '',
'titre' => '',
'hreflang' => '',
'accesskey' => '',
'title' => '',
'date_creation' => '',
'description' => ''
*/

class MoteurRecherche_SPIP {
        public function get($ids) {
                $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)',

                                                                  "publie",
                                                                  implode(',', $ids)));
                $content = array();
                while($rec = $req->fetchRow(DB_FETCHMODE_ASSOC)) {
                        $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);
                        unset($rec['texte']);
                        $content[$rec['id']] = $rec;

                }
                return $content;
        }
}

class MoteurRecherche_BAZAR {
        public function get($ids) {
                $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)',

                                                                  implode(',', $ids)));
                $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);
                        unset($rec['texte']);
                        $content[$rec['id']] = $rec;
                }
                return $content;                
        }
}

class MoteurRecherche_PROJET {
        public function get($ids) {
                $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)',
                                                                  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['description'] = substr(strip_tags($rec['p_description']), 0, 400 + 2 * MORE_RESULTAT_TAILLE_DESCRIPTION);
                        unset($rec['p_description']);
                        $content[$rec['id']] = $rec;
                }
                return $content;                
        }
}

class MoteurRecherche_PAPYRUS {
        public function get($ids) {
        }
}

// http://stackoverflow.com/questions/348410/sort-an-array-based-on-another-array
function _sortArrayByArray($array, $orderArray) {
        $ordered = array();
        foreach($orderArray as $key) {
                if(array_key_exists($key, $array)) {
                        $ordered[$key] = $array[$key];
                        unset($array[$key]);
                }
        }
        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'];
}

$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']]);
}

// 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']]);
}

// 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']]);
}


// 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";




/*
// http://www.php.net/manual/fr/sphinx.examples.php
$s = new SphinxClient;
$s->setServer("localhost", 9306);
$s->setMatchMode(SPH_MATCH_ANY);
$s->setMaxQueryTime(3);
var_dump($s->query("test"));
*/