Subversion Repositories Sites.tela-botanica.org

Rev

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

Rev Author Line No. Line
439 alex 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 library is free software; you can redistribute it and/or                                        |
9
// | modify it under the terms of the GNU General Public                                                  |
10
// | License as published by the Free Software Foundation; either                                         |
11
// | version 2.1 of the License, or (at your option) any later version.                                   |
12
// |                                                                                                      |
13
// | This library is distributed in the hope that it will be useful,                                      |
14
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                                       |
15
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                                    |
16
// | General Public License for more details.                                                             |
17
// |                                                                                                      |
18
// | You should have received a copy of the GNU General Public                                            |
19
// | License along with this library; if not, write to the Free Software                                  |
20
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                            |
21
// +------------------------------------------------------------------------------------------------------+
22
// CVS : $Id:$
23
/**
24
* Application projet
25
*
26
* Script pour calculer les statistiques
27
*
28
*@package projet
29
//Auteur original :
30
*@author        Alexandre Granier <alexandre@tela-botanica.org>
31
//Autres auteurs :
32
*@author        Aucun
33
*@copyright     Tela-Botanica 2000-2005
34
*@version       $Revision:$
35
// +------------------------------------------------------------------------------------------------------+
36
*/
37
 
38
// +------------------------------------------------------------------------------------------------------+
39
// |                                            ENTETE du PROGRAMME                                       |
40
// +------------------------------------------------------------------------------------------------------+
41
 
42
 
43
include_once PROJET_CHEMIN_APPLI.'classes/ezmlmAccessObject.class.php';
44
include_once PROJET_CHEMIN_BIBLIOTHEQUE_API.'pear/XML/RSS.php';
45
define("NB_SEC_INSERT", 7*24*3600);
46
define("NB_SEC_UPDATE", 2*3600);
47
//if(!set_time_limit(100))return 'Impossible de changer le temps max d\execution';
48
//define("URL_WIKINI", "http://www.outils-reseaux.org/wikini/");
49
define("URL_WIKINI", "http://www.tela-botanica.org/wikini/");
50
 
51
function CurlFileGetContents($adresse, $timeout = 30){
52
  $ch = curl_init($adresse);
53
  curl_setopt($ch, CURLOPT_HEADER, 0);
54
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
55
  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
56
  curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
57
  $page    = curl_exec($ch);
58
  $CurlErr = curl_error($ch);
59
  curl_close($ch);
60
  if ($CurlErr){
61
    echo $CurlErr;
62
    return false;
63
  }else{
64
    return $page;
65
  }
66
}
67
 
68
function monFileGetContents($adresse, $timeout = 30){
69
	$url = parse_url($adresse);
70
	$url['port'] = isset($url['port']) ? $url['port'] : '80';
71
	$url['scheme'] = isset($url['scheme']) ? strtoupper($url['scheme']) : 'HTTP';
72
	$fp = fsockopen($url['host'], $url['port'], $errno, $errstr, $timeout);
73
	if (!$fp) {
74
		echo "Erreur de socket: $errno - $errstr<br />\n";
75
		return false;
76
	} else {
77
		$header  = 'GET '.$url['path'].' '.$url['scheme']."/1.1\r\n";
78
 
79
		//$header .= 'Host: '.$url['host']."\r\n";
80
		//$header .= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n";
81
 
82
		$header .= "Host: www.yahoo.com\r\n";
83
		$header .= "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021204\r\n";
84
		$header .= "Referer: http://www.yahoo.com/\r\n";
85
 
86
		$header .= 'Connection: Close\r\n\r\n';
87
 
88
		fputs($fp, $header);
89
		stream_set_timeout($fp, $timeout);
90
		while (!feof($fp)) {
91
			$page .= fread($fp, 262144);
92
		}
93
		$stream  = stream_get_meta_data($fp);
94
		fclose($fp);
95
		if ($stream['timed_out']) {
96
			echo 'Le délai de réponse de la page <b>'.$adresse.'</b> a dépassé le timeout de <b>'.$timeout.'</b> sec.';
97
			return false;
98
		}else{
99
			//séparation du header
100
    		//$page = substr($page, strpos($page, "\r\n\r\n"));
101
    		//$page = trim($page);
102
 
103
			return 1;
104
		}
105
	}
106
}
107
 
108
function scanner($ligne, &$nombre_contrib, &$nombre_message, &$res) {
109
	$nombre_contrib='0';
110
	$nombre_msg='0';
111
	$r=false;
112
 
113
	// Recuperation du nbre de message
114
	$xml_parser = &new ezmlmAccessObject('nombre_messages', $ligne->pl_domaine, $ligne->pl_nom_liste, $GLOBALS['lang']) ;
115
	$xml_parser->load();
116
 
117
	ob_start();
118
	$xml_parser->parse();
119
	$nombre_message = ob_get_contents();
120
	if(!$nombre_message)$nombre_message='0';
121
	ob_end_clean();
122
 
123
 
124
	// Parse le flux RSS généré par wikini
125
	if($ligne->AGO_A_NOMGRP) {
126
		$url='http://fr.groups.yahoo.com/group/'.$ligne->AGO_A_NOMGRP;
127
		$pattern='>([0-9]+)</span> nouveau';
128
		$contenu=file_get_contents($url);
129
		//$contenu=CurlFileGetContents($url);
130
		//$contenu=monFileGetContents($url);
131
 
132
		$res.=$contenu;
133
 
134
		$r=($contenu?true:false);
135
		ereg($pattern, $contenu, $no);
136
		$nombre_message=$no[1];
137
 
138
		//$rss =& new XML_RSS($url.'/rss');
139
	} else {
140
		$rss =& new XML_RSS(URL_WIKINI.$ligne->p_wikini.'/wakka.php?wiki=DerniersChangementsRSS/xml');
141
		$rss->parse();
142
 
143
		// On va compter le nombre de modification depuis les x derniers jours (voirs constantes)
144
		foreach ($rss->getItems() as $item) {
145
			// Le script doit être compatiable avec plusieurs formats de date
146
			if ($item['pubDate']) {
147
				if (strtotime($item['pubDate'])>time()-NB_SEC_INSERT) $nombre_contrib++;
148
 
149
			} else {
150
				$date = substr($item['title'], strlen($item['title'])-18);
151
				if ($date>date("Y m d - H:i", time()-NB_SEC_INSERT)) $nombre_contrib++;
152
			}
153
		}
154
 
155
		$r=true;
156
 
157
	}
158
 
159
	return $r;
160
}
161
 
162
$start=microtime(true);
163
 
164
 
165
// +------------------------------------------------------------------------------------------------------+
166
// |                                            CORPS du PROGRAMME                                       |
167
// +------------------------------------------------------------------------------------------------------+
168
 
169
 
170
// requete pour recuperer la liste des projets
171
$requete = 'select count(distinct psu_id_utilisateur) as nb_utilisateur, count(distinct pil_id_utilisateur) as nb_utilisateur_liste, count(distinct projet_documents.pd_id) as nb_doc, p_wikini, ' .
172
		'p_id, p_titre, p_resume, projet_lien_liste.pl_id_liste, pl_domaine,pl_nom_liste,AGO_A_NOMGRP ' .
173
		'from projet left join projet_lien_liste on p_id=projet_lien_liste.pl_id_projet ' .
174
		'left join projet_liste on projet_lien_liste.pl_id_liste=projet_liste.pl_id_liste ' .
175
		'left join projet_inscription_liste on (projet_inscription_liste.pil_id_liste=projet_liste.pl_id_liste and pil_date_inscription > date_sub(now(), interval 1 week)) ' .
176
		'left join projet_documents on (p_id=pd_ce_projet and  pd_date_de_mise_a_jour > date_sub(now(), interval 1 week)) ' .
177
		'left join projet_statut_utilisateurs on p_id=psu_id_projet ' .
178
		'left join projet_lien_liste_externe on plle_id_projet=p_id ' .
179
		'left join agora on plle_id_liste=AGO_A_ID ' .
180
		'group by p_id order by p_titre' ;
181
$resultat = $this->_db->query($requete) ;
182
if (DB::isError($resultat)) {
183
	return ("Echec de la requete<br />".$resultat->getMessage()."<br />".$resultat->getDebugInfo()) ;
184
}
185
$res .= '<h1>Statistiques des projets</h1>';
186
 
187
$res .= '<style type="text/css">';
188
$res .= '.stats { border-spacing: 0px; }';
189
$res .= '.stats .stat_nb { text-align: center; }';
190
$res .= '.stats th { font-weight: bold; }';
191
$res .= '.stats td { border-bottom: 1px #ddd solid; }';
192
$res .= '.warning { color: #800; font-weight: bold; }';
193
$res .= '</style>';
194
 
195
$calculs=false;
196
 
197
//*
198
$res .= '<table cellspacing="0" class="stats" style="width: 100%"><tr><th>Nom du projet</th><th>Nom liste</th><th>Inscrits</th><th>Utilisateurs</th><th>Nb msg</th><th>Nb doc</th><th>Nb contrib</th></tr>';
199
 
200
$nombre_contrib = 0;
201
$nombre_message = 0;
202
 
203
while ($ligne = $resultat->fetchRow(DB_FETCHMODE_OBJECT)) {
204
	$nombre_contrib = 0;
205
	$nombre_message = 0;
206
 
207
	// On vérifie la date de derniere mise à jour
208
	$requete_verif='SELECT * FROM projet_statistiques WHERE ps_ce_projet="'.$ligne->p_id.'" ORDER BY ps_date DESC LIMIT 1';
209
	$resultat_verif = $this->_db->query($requete_verif);
210
	if (DB::isError($resultat_verif)) {
211
		return $resultat_verif->getMessage().$resultat_verif;
212
	}
213
	$ligne_verif = $resultat_verif->fetchRow(DB_FETCHMODE_OBJECT);
214
 
215
	// Si elle remonte à plus de deux heure, on sauvegarde, et à plus de 7 jours, on archive et créé une nouvelle entrée
216
	if($ligne_verif->ps_date > date("Y-m-d H:i:s", time()-NB_SEC_INSERT)) {
217
		if($ligne_verif->ps_maj < date("Y-m-d H:i:s", time()-NB_SEC_UPDATE)) {
218
 
219
			// Recupere nombre_contrib et nombre_message
220
			$calculs=scanner($ligne_verif, $nombre_contrib, $nombre_message, $res);
221
 
222
			$requete_maj = 'UPDATE projet_statistiques SET ps_msg_derniere_semaine="'.$nombre_message.'", ps_doc_derniere_semaine="'.$ligne->nb_doc.'", ps_nombre_inscrit="'.$ligne->nb_utilisateur.'", ps_nombre_inscrit_liste="'.$ligne->nb_utilisateur_liste.'", ps_modifwiki_derniere_semaine="'.$nombre_contrib.'", ps_maj=NOW() WHERE ps_ce_projet="'.$ligne->p_id.'" AND ps_date="'.$ligne->ps_date.'"';
223
			$resultat_maj = $this->_db->query($requete_maj);
224
			if (DB::isError($resultat_maj)) {
225
				return $resultat_maj->getMessage().$requete_maj;
226
			}
227
		} else {
228
 
229
			$nombre_contrib = $ligne_verif->ps_modifwiki_derniere_semaine;
230
			$nombre_message = $ligne_verif->ps_msg_derniere_semaine;
231
 
232
		}
233
 
234
	} else {
235
 
236
		// Recupere nombre_contrib et nombre_message
237
		$calculs=scanner($ligne, $nombre_contrib, $nombre_message, $res);
238
 
239
		$requete_maj = 'INSERT INTO projet_statistiques (ps_ce_projet, ps_msg_derniere_semaine, ps_doc_derniere_semaine, ps_nombre_inscrit, ps_nombre_inscrit_liste, ps_modifwiki_derniere_semaine, ps_date, ps_maj) '.
240
						'VALUES ("'.$ligne->p_id.'", "'.$nombre_message.'", "'.$ligne->nb_doc.'", "'.$ligne->nb_utilisateur.'", "'.$ligne->nb_utilisateur_liste.'", "'.$nombre_contrib.'", NOW(), NOW())';
241
		$resultat_maj = $this->_db->query($requete_maj);
242
		if (DB::isError($resultat_maj)) {
243
			return $resultat_maj->getMessage().$requete_maj;
244
		}
245
	}
246
 
247
	// Et on affiche les stats courantes
248
	$res .= '<tr><td>'.$ligne->p_titre.'</td><td>'.($ligne->pl_nom_liste?$ligne->pl_nom_liste:$ligne->AGO_A_NOMGRP.' (Yahoo)').'</td><td class="stat_nb">'.$ligne->nb_utilisateur_liste.'</td><td class="stat_nb">'.$ligne->nb_utilisateur.'</td><td class="stat_nb">'.$nombre_message.'</td><td class="stat_nb">'.$ligne->nb_doc.'</td><td class="stat_nb">'.$nombre_contrib.'</td></tr>';
249
}
250
$res .= '</table>';
251
//*/
252
 
253
$sql="update `projet_statistiques` set ps_somme=(ps_msg_derniere_semaine +ps_doc_derniere_semaine+ps_nombre_inscrit_liste+ps_modifwiki_derniere_semaine);";
254
$resultat = $this->_db->query($sql);
255
if (DB::isError($resultat)) {
256
	return $resultat->getMessage().$sql;
257
}
258
 
259
$sql='SELECT MAX( ps_somme ) AS max FROM projet_statistiques WHERE ps_date>=CURDATE()';
260
$resultat = $this->_db->query($sql);
261
if (DB::isError($resultat)) {
262
	return $resultat->getMessage().$sql;
263
}
264
$tresultat = $resultat->fetchRow(DB_FETCHMODE_OBJECT);
265
 
266
$sql="UPDATE `projet_statistiques` SET ps_pourcent=ps_somme / ".($tresultat->max?$tresultat->max:1)." * 100 WHERE ps_date>=CURDATE();";
267
$resultat = $this->_db->query($sql);
268
if (DB::isError($resultat)) {
269
	return $resultat->getMessage().$sql;
270
}
271
 
272
$res .= '<p>';
273
if(!$calculs)$res .= '<span class="warning">Les variables nb_msg et nb_contrib n\'ont pas &eacute;t&eacute; calcul&eacute;es (datent de moins de '.round(NB_SEC_UPDATE/3600).' heures)</span><br />';
274
$res .= 'Execut&eacute; en '.(round((microtime(true)-$start)*1000)/1000).' secondes.</p>';
275
 
276
// $res .= $requete;
277
return $res;
278
 
279
/* +--Fin du code ---------------------------------------------------------------------------------------+
280
* $Log:$
281
* +--Fin du code ----------------------------------------------------------------------------------------+
282
*/
283
?>