Subversion Repositories Applications.papyrus

Rev

Rev 1032 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
846 jp_milcent 1
<?php
2
/*vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +------------------------------------------------------------------------------------------------------+
4
// | PHP version 4.1                                                                                      |
5
// +------------------------------------------------------------------------------------------------------+
6
// | Copyright (C) 2004 Tela Botanica (accueil@tela-botanica.org)                                         |
7
// +------------------------------------------------------------------------------------------------------+
8
// | This file is part of Papyrus.                                                                        |
9
// |                                                                                                      |
10
// | Foobar is free software; you can redistribute it and/or modify                                       |
11
// | it under the terms of the GNU General Public License as published by                                 |
12
// | the Free Software Foundation; either version 2 of the License, or                                    |
13
// | (at your option) any later version.                                                                  |
14
// |                                                                                                      |
15
// | Foobar is distributed in the hope that it will be useful,                                            |
16
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                                       |
17
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                        |
18
// | GNU General Public License for more details.                                                         |
19
// |                                                                                                      |
20
// | You should have received a copy of the GNU General Public License                                    |
21
// | along with Foobar; if not, write to the Free Software                                                |
22
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                            |
23
// +------------------------------------------------------------------------------------------------------+
24
// CVS : $Id$
25
/**
26
* Classe permettant d'effectuer des recherches sur les informations des articles de Spip.
27
*
28
* Permet de rechercher et classer les articles en fonction d'une chaine.
29
* Utilisation des bibliothèques inclue par Papyrus :
30
* - Papyrus pap_meta.fonct.php
31
* - Papyrus pap_menu.fonct.php
32
* - Papyrus pap_url.class.php
33
*
34
*@package Applette
35
*@subpackage Moteur_Recherche
36
//Auteur original :
37
*@author        Jean-Pascal MILCENT <jpm@tela-botanica.org>
38
//Autres auteurs :
39
*@author        aucun
40
*@copyright     Tela-Botanica 2000-2006
41
*@version       $Revision$ $Date$
42
// +------------------------------------------------------------------------------------------------------+
43
*/
44
 
45
// +------------------------------------------------------------------------------------------------------+
46
// |                                            ENTETE du PROGRAMME                                       |
47
// +------------------------------------------------------------------------------------------------------+
48
///** Inclusion du fichier contenant les fonctions de manipulations des menus de Papyrus.*/
49
//require_once GEN_CHEMIN_BIBLIO.'pap_menu.fonct.php';
50
 
51
// +------------------------------------------------------------------------------------------------------+
52
// |                                            CORPS du PROGRAMME                                        |
53
// +------------------------------------------------------------------------------------------------------+
54
 
55
class Recherche_Spip_Article extends Recherche {
56
 
57
    // Constructeur
58
    function Recherche_Menu_Meta($motif) {
59
        $this->setMotif($motif);
60
    }
61
 
62
    // Accesseurs
63
    function getMotif() {
64
        return $this->motif;
65
    }
66
    function setMotif($motif) {
67
        $this->motif = $motif;
68
    }
69
 
70
    // Méthodes
71
    function rechercherMotif($motif) {
72
    	for ($i = 0; $i < count($GLOBALS['_MOTEUR_RECHERCHE_']['spip']); $i++ ) {
73
	        $db = DB::connect($GLOBALS['_MOTEUR_RECHERCHE_']['spip'][$i]['bdd_dsn']);
74
	        if (DB::isError($db)) {
75
	    		$msg_erreur_connection = 'Impossible de se connecter à la base de données Spip.';
76
	    		die(BOG_afficherErreurSql(__FILE__, __LINE__, $db->getMessage(), 'connexion à la base de données',$msg_erreur_connection));
77
			}
78
	        $prefixe = $GLOBALS['_MOTEUR_RECHERCHE_']['spip'][$i]['table_prefixe'];
79
	        $url_base = $GLOBALS['_MOTEUR_RECHERCHE_']['spip'][$i]['url'];
80
	        $tab_articles = $this->retournerArticles($db, $prefixe);
81
	        foreach ($tab_articles as $article_id => $Article) {
82
	            // Initialisation du tableau de résultat vide
83
	            $aso_resultat = array(  'poids' => 0, 'url' => '', 'titre' => '',
84
	                                    'hreflang' => '', 'accesskey' => '', 'title' => '',
85
	                                    'date_creation' => '', 'description' => '');
86
 
87
	            // Création de l'url
88
	            $var_recherche = str_replace(' ', '+', trim($motif));
89
	            $aso_resultat['url'] = $url_base.'article'.$article_id.'.html?var_recherche='.$var_recherche;
90
 
91
	            // Récupération du titre de la page
92
	            if (trim($Article->titre) != '') {
93
	                $aso_resultat['titre'] = htmlentities($Article->titre);
94
	            } else if (trim($Article->surtitre) != '') {
95
	                $aso_resultat['titre'] = htmlentities($Article->surtitre);
96
	            } else if (trim($Article->soustitre) != '') {
97
	                $aso_resultat['titre'] = htmlentities($Article->soustitre);
98
	            }
99
	            $aso_resultat['hreflang'] = htmlentities($Article->lang);
100
 
101
	            $aso_resultat['description'] = $this->couper($Article->texte, 450);
102
 
103
	            if (($jour = date('d', strtotime($Article->date)) ) != 0 ) {
104
	                $aso_resultat['date_creation'] .= '<span class="page_modification_jour"> '.$jour.'</span>'."\n";
105
	            }
106
	            if (($mois = $this->traduireMois(date('m', strtotime($Article->date))) ) != '' ) {
107
	                $aso_resultat['date_creation'] .= '<span class="page_modification_mois"> '.$mois.'</span>'."\n";
108
	            }
109
	            if (($annee = date('Y', strtotime($Article->date)) ) != 0 ) {
110
	                $aso_resultat['date_creation'] .= '<span class="page_modification_annee"> '.$annee.'</span>'."\n";
111
	            }
112
 
113
	            // Analyse du poids de cette page vis à vis des données
114
	            $tab_champs_a_visiter = array(  'surtitre', 'titre', 'soustitre', 'descriptif', 'chapo', 'texte', 'ps');
115
	            foreach ($tab_champs_a_visiter as $val) {
116
	                $aso_resultat['poids'] += $this->retournerOccurenceMotif($motif, $Article->$val);
117
	            }
118
 
119
	            if ($aso_resultat['poids'] > 0) {
120
	                $this->setResultat($aso_resultat);
121
	            }
122
	        }
123
    	}
124
        return $this->getResultats();
125
    }
126
 
127
    /** Renvoie un tableau contenant les infos sur les articles
128
	*
129
	* @param  mixed		une instance de la classse Pear DB.
130
	* @param  string	le préfixe pour les tables spip.
131
	* @return array		tableau contenant les articles.
132
	*/
133
	function retournerArticles(&$db, $prefixe = '')
134
	{
135
	    //----------------------------------------------------------------------------
136
	    // Recherche des informations sur le menu
137
	    $requete =  'SELECT * '.
138
	                'FROM '.$prefixe.'spip_articles '.
139
	                'WHERE statut = "publie"';
140
 
141
	    $resultat = $db->query($requete);
142
	    (DB::isError($resultat)) ? die(BOG_afficherErreurSql(__FILE__, __LINE__, $resultat->getMessage(), $requete)) : '';
143
 
144
	    //----------------------------------------------------------------------------
145
	    // Récupération des infos
146
	    $tab_retour = array();
147
	    while ($info_article = $resultat->fetchRow(DB_FETCHMODE_OBJECT)) {
148
	        $tab_retour[$info_article->id_article] = $info_article;
149
	    }
150
	    $resultat->free();
151
 
152
	    return $tab_retour;
153
	}
154
 
155
	// Fichier : inc_texte.php3
156
	function couper($texte, $taille = 50)
157
	{
158
		$texte = substr($texte, 0, 400 + 2*$taille); /* eviter de travailler sur 10ko pour extraire 150 caracteres */
159
 
160
		// on utilise les \r pour passer entre les gouttes
161
		$texte = str_replace("\r\n", "\n", $texte);
162
		$texte = str_replace("\r", "\n", $texte);
163
 
164
		// sauts de ligne et paragraphes
165
		$texte = ereg_replace("\n\n+", "\r", $texte);
166
		$texte = ereg_replace("<(p|br)( [^>]*)?".">", "\r", $texte);
167
 
168
		// supprimer les traits, lignes etc
169
		$texte = ereg_replace("(^|\r|\n)(-[-#\*]*|_ )", "\r", $texte);
170
 
171
		// supprimer les tags
172
		$texte = $this->supprimer_tags($texte);
173
		$texte = trim(str_replace("\n"," ", $texte));
174
		$texte .= "\n";	// marquer la fin
175
 
176
		// travailler en accents charset
177
		// On supprime dans Papyrus car cela tire trop de fonctions...
178
		//$texte = $this->filtrer_entites($texte);
179
 
180
		// supprimer les liens
181
		$texte = ereg_replace("\[->([^]]*)\]","\\1", $texte); // liens sans texte
182
		$texte = ereg_replace("\[([^\[]*)->([^]]*)\]","\\1", $texte);
183
 
184
		// supprimer les notes
185
		$texte = ereg_replace("\[\[([^]]|\][^]])*\]\]", "", $texte);
186
 
187
		// supprimer les codes typos
188
		$texte = ereg_replace("[}{]", "", $texte);
189
 
190
		// supprimer les tableaux
191
		$texte = ereg_replace("(^|\r)\|.*\|\r", "\r", $texte);
192
 
193
		// couper au mot precedent
194
		$long = $this->spip_substr($texte, 0, max($taille-4,1));
195
		$court = ereg_replace("([^[:space:]][[:space:]]+)[^[:space:]]*\n?$", "\\1", $long);
196
		$points = '&nbsp;(...)';
197
 
198
		// trop court ? ne pas faire de (...)
199
		if (strlen($court) < max(0.75 * $taille,2)) {
200
			$points = '';
201
			$long = $this->spip_substr($texte, 0, $taille);
202
			$texte = ereg_replace("([^[:space:]][[:space:]]+)[^[:space:]]*$", "\\1", $long);
203
			// encore trop court ? couper au caractere
204
			if (strlen($texte) < 0.75 * $taille)
205
				$texte = $long;
206
		} else
207
			$texte = $court;
208
 
209
		if (strpos($texte, "\n"))	// la fin est encore la : c'est qu'on n'a pas de texte de suite
210
			$points = '';
211
 
212
		// remettre les paragraphes
213
		$texte = ereg_replace("\r+", "\n\n", $texte);
214
 
215
		// supprimer l'eventuelle entite finale mal coupee
216
		$texte = preg_replace('/&#?[a-z0-9]*$/', '', $texte);
217
 
218
		return trim($texte).$points;
219
	}
220
 
221
	// Gerer les outils mb_string
222
	// Fichier : inc_texte.php3
223
	function spip_substr($c, $start=0, $end='')
224
	{
225
		// methode substr normale
226
		if ($end) {
227
			return substr($c, $start, $end);
228
		} else {
229
			return substr($c, $start);
230
		}
231
	}
232
 
233
	// Suppression basique et brutale de tous les <...>
234
	// Fichier : inc_filtres.php3
235
	function supprimer_tags($texte, $rempl = "")
236
	{
237
		$texte = preg_replace(",<[^>]*>,U", $rempl, $texte);
238
		// ne pas oublier un < final non ferme
239
		$texte = str_replace('<', ' ', $texte);
240
		return $texte;
241
	}
242
 
243
}
244
 
245
/* +--Fin du code ----------------------------------------------------------------------------------------+
246
*
247
* $Log$
248
*
249
* +-- Fin du code ----------------------------------------------------------------------------------------+
250
*/
251
?>