[Fusionforge-commits] r10688 - in trunk/src: common/include cronjobs

Roland Mas lolando at libremir.placard.fr.eu.org
Sat Sep 25 17:17:55 CEST 2010


Author: lolando
Date: 2010-09-25 17:17:54 +0200 (Sat, 25 Sep 2010)
New Revision: 10688

Modified:
   trunk/src/common/include/Group.class.php
   trunk/src/common/include/User.class.php
   trunk/src/cronjobs/auth_unix.php
   trunk/src/cronjobs/calculate_user_metric.php
   trunk/src/cronjobs/daily_task_email.php
   trunk/src/cronjobs/homedirs.php
   trunk/src/cronjobs/massmail.php
   trunk/src/cronjobs/site_stats.php
Log:
Less SQL (and fewer references to the user_group table), more code reuse

Modified: trunk/src/common/include/Group.class.php
===================================================================
--- trunk/src/common/include/Group.class.php	2010-09-25 14:08:11 UTC (rev 10687)
+++ trunk/src/common/include/Group.class.php	2010-09-25 15:17:54 UTC (rev 10688)
@@ -140,6 +140,12 @@
 	return $return;
 }
 
+function &group_get_active_projects() {
+	$res=db_query_params ('SELECT group_id FROM groups WHERE status=$1',
+			      array ('A')) ;
+	return group_get_objects (util_result_column_to_array($res,0)) ;
+}
+
 function &group_get_object_by_name($groupname) {
 	$res=db_query_params('SELECT * FROM groups WHERE unix_group_name=$1', array ($groupname)) ;
 	return group_get_object(db_result($res,0,'group_id'),$res);
@@ -2806,6 +2812,9 @@
 			/* If several projects share a same public name */
 			return strcoll ($a->getUnixName(), $b->getUnixName()) ;
 			break ;
+		case 'unixname':
+			return strcmp ($a->getUnixName(), $b->getUnixName()) ;
+			break ;
 		case 'id':
 			$aid = $a->getID() ;
 			$bid = $b->getID() ;

Modified: trunk/src/common/include/User.class.php
===================================================================
--- trunk/src/common/include/User.class.php	2010-09-25 14:08:11 UTC (rev 10687)
+++ trunk/src/common/include/User.class.php	2010-09-25 15:17:54 UTC (rev 10688)
@@ -135,6 +135,12 @@
 	return user_get_objects($arr);
 }
 
+function &user_get_active_users() {
+	$res=db_query_params ('SELECT user_id FROM users WHERE status=$1',
+			      array ('A')) ;
+	return user_get_objects (util_result_column_to_array($res,0)) ;
+}
+
 class GFUser extends Error {
 	/** 
 	 * Associative array of data from db.
@@ -1646,6 +1652,42 @@
 	}
 }
 
+class UserComparator {
+	var $criterion = 'name' ;
+
+	function Compare ($a, $b) {
+		switch ($this->criterion) {
+		case 'name':
+		default:
+			$namecmp = strcoll ($a->getRealName(), $b->getRealName()) ;
+			if ($namecmp != 0) {
+				return $namecmp ;
+			}
+			/* If several projects share a same real name */
+			return strcoll ($a->getUnixName(), $b->getUnixName()) ;
+			break ;
+		case 'unixname':
+			return strcmp ($a->getUnixName(), $b->getUnixName()) ;
+			break ;
+		case 'id':
+			$aid = $a->getID() ;
+			$bid = $b->getID() ;
+			if ($a == $b) {
+				return 0;
+			}
+			return ($a < $b) ? -1 : 1;
+			break ;
+		}
+	}
+}
+
+function sortUserList (&$list, $criterion='name') {
+	$cmp = new UserComparator () ;
+	$cmp->criterion = $criterion ;
+
+	return usort ($list, array ($cmp, 'Compare')) ;
+}
+
 // Local Variables:
 // mode: php
 // c-file-style: "bsd"

Modified: trunk/src/cronjobs/auth_unix.php
===================================================================
--- trunk/src/cronjobs/auth_unix.php	2010-09-25 14:08:11 UTC (rev 10687)
+++ trunk/src/cronjobs/auth_unix.php	2010-09-25 15:17:54 UTC (rev 10688)
@@ -189,15 +189,9 @@
 		else
 		{
 			$line = $group_names [$i] . ":x:" . ($group_ids [$i] + $gid_add) . ":";
-			$resusers = db_query_params ('SELECT user_name FROM user_group,users,groups WHERE users.user_id=user_group.user_id AND groups.group_id=user_group.group_id AND groups.status=$1::bpchar AND groups.use_scm=1 AND groups.unix_group_name=$2 AND users.status=$3 AND users.unix_status=$4',
-						     array('A',
-							   $group_names [$i],
-							   'A',
-							   'A'));
-			$members = &util_result_column_to_array ($resusers, "user_name");
-			if (count ($members) > 0)
-			{
-				$line .= implode (",", $gmembers) . ",";
+			$users = RBACEngine::getInstance()->getUsersByAllowedAction ('scm',$group_ids[$i],'write') ;
+			foreach ($users as $u) {
+				$line .= $u->getUnixName()."," ;
 			}
 			$line .= forge_get_config('apache_user') . "\n";
 			fwrite ($h6, $line);

Modified: trunk/src/cronjobs/calculate_user_metric.php
===================================================================
--- trunk/src/cronjobs/calculate_user_metric.php	2010-09-25 14:08:11 UTC (rev 10687)
+++ trunk/src/cronjobs/calculate_user_metric.php	2010-09-25 15:17:54 UTC (rev 10688)
@@ -58,7 +58,7 @@
 		what we want - a list of the top users on the site.
 
 */
-require dirname(__FILE__).'/../www/env.inc.php';
+require (dirname(__FILE__).'/../www/env.inc.php');
 require_once $gfcommon.'include/pre.php';
 require $gfcommon.'include/cron_utils.php';
 
@@ -75,19 +75,12 @@
 		 array('user_metric0_pk_seq')) ;
 $err .= db_error();
 
-db_query_params ('INSERT INTO user_metric0 
-(user_id,times_ranked,avg_raters_importance,avg_rating,metric,percentile,importance_factor)
-SELECT user_id,5,1.25,3,0,0,1.25
-FROM user_group
-WHERE
-user_group.group_id=$1
-AND user_group.admin_flags=$2',
-			array (forge_get_config('peer_rating_group'),
-			       'A')) ;
+foreach(RBACEngine::getInstance()->getUsersByAllowedAction ('project_admin',forge_get_config('peer_rating_group')) as $u) {
+	db_query_params ('INSERT INTO user_metric0 (user_id,times_ranked,avg_raters_importance,avg_rating,metric,percentile,importance_factor) VALUES ($1,5,1.25,3,0,0,1.25)',
+			array (forge_get_config('peer_rating_group'))) ;
+	$err .= db_error();
+}
 
-
-$err .= db_error();
-
 db_query_params ('UPDATE user_metric0 SET ranking=ranking-1',
 			array()) ;
 

Modified: trunk/src/cronjobs/daily_task_email.php
===================================================================
--- trunk/src/cronjobs/daily_task_email.php	2010-09-25 14:08:11 UTC (rev 10687)
+++ trunk/src/cronjobs/daily_task_email.php	2010-09-25 15:17:54 UTC (rev 10688)
@@ -29,24 +29,8 @@
 
 $err = '';
 
-//
-//  Set up this script to run as the site admin
-//
-$res = db_query_params ('SELECT user_id FROM user_group WHERE admin_flags=$1 AND group_id=$2',
-			array('A',
-			'1')) ;
+session_set_admin() ;
 
-
-if (!$res) {
-    echo db_error();
-    exit(1);
-}
-if (db_numrows($res) == 0) {
-    exit(0);
-}
-$id=db_result($res,0,0);
-session_set_new($id);
-
 // Get user id's from users who have open tasks
 $res = db_query_params ('SELECT DISTINCT u.user_id, u.realname, u.email FROM users u, project_assigned_to pat, project_task_vw ptv 
 		WHERE u.user_id > 100 AND u.user_id=pat.assigned_to_id AND pat.project_task_id=ptv.project_task_id 

Modified: trunk/src/cronjobs/homedirs.php
===================================================================
--- trunk/src/cronjobs/homedirs.php	2010-09-25 14:08:11 UTC (rev 10687)
+++ trunk/src/cronjobs/homedirs.php	2010-09-25 15:17:54 UTC (rev 10688)
@@ -76,63 +76,43 @@
 	@mkdir(forge_get_config('homedir_prefix'),0755,true);
 }
 
-$res = db_query_params ('SELECT distinct users.user_name,users.unix_pw,users.user_id
-	FROM users,user_group,groups
-	WHERE users.user_id=user_group.user_id
-	AND user_group.group_id=groups.group_id
-	AND groups.status=$1
-	AND user_group.cvs_flags IN (0,1)
-	AND users.status=$2
-	ORDER BY user_id ASC',
-			array('A',
-			      'A'));
-$err .= db_error();
-$users    =& util_result_column_to_array($res,'user_name');
-
-$group_res = db_query_params ('SELECT unix_group_name, (is_public=1 AND enable_anonscm=1 AND type_id=1) AS enable_pserver FROM groups WHERE status=$1 AND type_id=1',
-			      array('A'));
-$err .= db_error();
-$groups = util_result_column_to_array($group_res,'unix_group_name');
-
-
-//
-//	this is where we give a user a home
-//
-foreach($users as $user) {
-	if (is_dir(forge_get_config('homedir_prefix')."/".$user)) {
+$active_projects = group_get_active_projects() ;
+$unames = array () ;
+foreach ($active_projects as $project) {
+	foreach ($project->getUsers() as $u) {
+		$unames[] = $u->getUnixName() ;
+	}
+}
+$unames = array_unique ($unames) ;
+foreach($unames as $uname) {
+	if (is_dir(forge_get_config('homedir_prefix')."/".$uname)) {
 		
 	} else {
-		@mkdir(forge_get_config('homedir_prefix')."/".$user);
+		@mkdir(forge_get_config('homedir_prefix')."/".$uname);
 	}
-	system("chown $user:".USER_DEFAULT_GROUP." ".forge_get_config('homedir_prefix')."/".$user);
+	system("chown $uname:".USER_DEFAULT_GROUP." ".forge_get_config('homedir_prefix')."/".$uname);
 }
 
+//test if the FTP upload dir exists and create it if not
+if (!is_dir(forge_get_config('ftp_upload_dir'))) {
+	@mkdir(forge_get_config('ftp_upload_dir'),0755,true);
+}
 
-//
-//	Create home dir for groups
-//
-foreach($groups as $group) {
-
-	//test if the FTP upload dir exists and create it if not
-	if (!is_dir(forge_get_config('ftp_upload_dir'))) {
-		@mkdir(forge_get_config('ftp_upload_dir'),0755,true);
-	}
-	
+foreach($active_projects as $project) {
+	$groupname = $project->getUnixName() ;
 	//create an FTP upload dir for this project
 	if (forge_get_config('use_ftp_uploads')) { 
-		if (!is_dir(forge_get_config('ftp_upload_dir').'/'.$group)) {
-			@mkdir(forge_get_config('ftp_upload_dir').'/'.$group); 
+		if (!is_dir(forge_get_config('ftp_upload_dir').'/'.$groupname)) {
+			@mkdir(forge_get_config('ftp_upload_dir').'/'.$groupname); 
 		}
 	}
 
-	if (is_dir(forge_get_config('groupdir_prefix')."/".$group)) {
+	if (is_dir(forge_get_config('groupdir_prefix')."/".$groupname)) {
 
 	} else {
-		@mkdir(forge_get_config('groupdir_prefix')."/".$group);
-		@mkdir(forge_get_config('groupdir_prefix')."/".$group."/htdocs");
-		@mkdir(forge_get_config('groupdir_prefix')."/".$group."/cgi-bin");
-		$g =& group_get_object_by_name($group);
-		
+		@mkdir(forge_get_config('groupdir_prefix')."/".$groupname);
+		@mkdir(forge_get_config('groupdir_prefix')."/".$groupname."/htdocs");
+		@mkdir(forge_get_config('groupdir_prefix')."/".$groupname."/cgi-bin");
 
 		//
 		//	Read in the template file
@@ -150,37 +130,31 @@
 		//
 		//	Change some defaults in the template file
 		//
-		//$contents=str_replace('<domain>',forge_get_config('web_host'),$contents);
-		//$contents=str_replace('<project_description>',$g->getDescription(),$contents);
-		//$contents=str_replace('<project_name>',$g->getPublicName(),$contents);
-		//$contents=str_replace('<group_id>',$g->getID(),$contents);
-		//$contents=str_replace('<group_name>',$g->getUnixName(),$contents);
-
 		$contents=str_replace('##comment##', _('Default Web Page for groups that haven\'t setup their page yet'), $contents);
 		$contents=str_replace('##purpose##', _('Please replace this file with your own website'), $contents);
-		$contents=str_replace('##welcome_to##', sprintf(_('Welcome to %s'), $g->getPublicName()), $contents);
+		$contents=str_replace('##welcome_to##', sprintf(_('Welcome to %s'), $project->getPublicName()), $contents);
 		$contents=str_replace('##body##',
 			sprintf(
 				_("We're Sorry but this Project hasn't yet uploaded their personal webpage yet. <br /> Please check back soon for updates or visit <a href=\"%s\">the project page</a>."),
-				"http://".forge_get_config('web_host').'/projects/'.$g->getUnixName()),
-			$contents);
+				util_make_url ('/projects/'.$project->getUnixName())),
+				      $contents);
 		//
 		//	Write the file back out to the project home dir
 		//
-		$fw=fopen(forge_get_config('groupdir_prefix')."/".$group."/htdocs/index.html",'w');
+		$fw=fopen(forge_get_config('groupdir_prefix')."/".$groupname."/htdocs/index.html",'w');
 		fwrite($fw,$contents);
 		fclose($fw);
 	}
 
 	if (forge_get_config('use_manual_uploads')) { 
-		$incoming = forge_get_config('groupdir_prefix')/$group."/incoming" ;
+		$incoming = forge_get_config('groupdir_prefix')/$groupname."/incoming" ;
 		if (!is_dir($incoming))
 		{
 			@mkdir($incoming); 
 		}
 	}
 
-	system("chown -R ".forge_get_config('apache_user').":".forge_get_config('apache_group')." forge_get_config('groupdir_prefix')/$group");
+	system("chown -R ".forge_get_config('apache_user').":".forge_get_config('apache_group')." forge_get_config('groupdir_prefix')/$groupname");
 
 }
 

Modified: trunk/src/cronjobs/massmail.php
===================================================================
--- trunk/src/cronjobs/massmail.php	2010-09-25 14:08:11 UTC (rev 10687)
+++ trunk/src/cronjobs/massmail.php	2010-09-25 15:17:54 UTC (rev 10688)
@@ -10,21 +10,22 @@
  *
  * Copyright 1999-2001 (c) VA Linux Systems
  * Copyright 2003 (c) GForge, LLC
+ * Copyright 2010, Roland Mas
  *
- * This file is part of GForge.
+ * This file is part of FusionForge.
  *
- * GForge is free software; you can redistribute it and/or modify
+ * FusionForge is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
  *
- * GForge is distributed in the hope that it will be useful,
+ * FusionForge is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with GForge; if not, write to the Free Software
+ * along with FusionForge; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
  */
 
@@ -37,7 +38,7 @@
 if (!cron_create_lock(__FILE__)) {
 	$err = "Massmail already running...exiting";
 		if (!cron_entry(6,$err)) {
-			# rely on crond to report the error
+			// rely on crond to report the error
 			echo "cron_entry error: ".db_error()."\n";
 		}
 	exit();
@@ -46,27 +47,9 @@
 // Pause between messages, sec
 $SLEEP = 1;
 
-// This tables maps mailing types to tables which required to perform it
-$table_mapping = array(
-	'ALL'		=> "users",
-	'SITE'	=> "users",
-	'COMMNTY' => "users",
-	'DVLPR'   => "users,user_group",
-	'ADMIN'   => "users,user_group,groups",
-	'SFDVLPR' => "users,user_group",
-);
+$all_users = user_get_active_users () ;
+sortUserList ($all_users, 'id') ;
 
-// This tables maps mailing types to WHERE subclauses which select 
-// appropriate users
-$cond_mapping = array(
-	'ALL'		=> "",
-	'SITE'	=> "AND mail_siteupdates=1",
-	'COMMNTY' => "AND mail_va=1",
-	'DVLPR'   => "AND users.user_id=user_group.user_id",
-	'ADMIN'   => "AND users.user_id=user_group.user_id AND user_group.admin_flags='A' AND groups.status='A' AND groups.group_id=user_group.group_id",
-	'SFDVLPR' => "AND users.user_id=user_group.user_id AND user_group.group_id=1"
-);
-
 $mail_res = db_query_params ('SELECT *
 	FROM massmail_queue
 	WHERE finished_date=0
@@ -111,66 +94,44 @@
 $body =  db_result($mail_res, 0, 'message');
 //$err .= "Got mail to send: ".$subj."\n";
 
-$qpa = db_construct_qpa (false, 'SELECT DISTINCT users.user_id,users.user_name,users.realname,users.email,users.confirm_hash') ;
-switch ($type) {
-case 'ALL':
-case 'SITE':
-case 'COMMNTY':
-	$qpa = db_construct_qpa ($qpa, ' FROM users') ;
-	break ;
-case 'DVLPR':
-case 'SFDVLPR':
-	$qpa = db_construct_qpa ($qpa, ' FROM users, user_group') ;
-	break ;
-case 'ADMIN':
-	$qpa = db_construct_qpa ($qpa, ' FROM users, user_group, groups') ;
-	break ;
-}
+$filtered_users = array () ;
 
-$qpa = db_construct_qpa ($qpa, ' WHERE users.user_id > $1 AND users.status=$2',
-			 array (db_result($mail_res, 0, 'last_userid'),
-				'A')) ;
-
-$cond_mapping = array(
-	'ALL'		=> "",
-	'SITE'	=> "",
-	'COMMNTY' => "",
-	'DVLPR'   => "",
-	'ADMIN'   => "",
-	'SFDVLPR' => ""
-);
-
-
-switch ($type) {
-case 'ALL':
-	break ;
-case 'SITE':
-	$qpa = db_construct_qpa ($qpa, ' AND mail_siteupdates=1') ;
-	break ;
-case 'COMMNTY':
-	$qpa = db_construct_qpa ($qpa, ' AND mail_va=1') ;
-	break ;
-case 'DVLPR':
-	$qpa = db_construct_qpa ($qpa, ' AND users.user_id=user_group.user_id') ;
-	break ;
-case 'SFDVLPR':
-	$qpa = db_construct_qpa ($qpa, ' AND users.user_id=user_group.user_id AND user_group.group_id=1') ;
-	break ;
-case 'ADMIN':
-	$qpa = db_construct_qpa ($qpa, ' AND users.user_id=user_group.user_id AND user_group.admin_flags=$1 AND groups.status=$2 AND groups.group_id=user_group.group_id',
-				 array ('A', 'A')) ;
-	break ;
+foreach ($all_users as $user) {
+	$process = false ;
+	switch ($type) {
+	case 'ALL':
+		$process = true ;
+		break;
+	case 'SITE':
+		$process = $user->getMailingPrefs('site') ;
+		break;
+	case 'COMMNTY':
+		$process = $user->getMailingPrefs('va') ;
+		break;
+	case 'DVLPR':
+		$process = count ($user->getGroups()) ;
+		break;
+	case 'ADMIN':
+		foreach ($user->getGroups(false) as $g) {
+			if (forge_check_perm_for_user ($user,'project_admin',$g->getID())) {
+				$process = true ;
+				break ;
+			}
+		}
+		break;
+	case 'SFDLVPR':
+		$process = forge_check_global_perm_for_user ($user,'forge_admin') ;
+		break;
+	}
+	if ($process) {
+		$filtered_users[] = $user ;
+	}
 }
 
-$qpa = db_construct_qpa ($qpa, ' ORDER BY users.user_id') ;
+$err .= "Mailing ".count($filtered_users)." users.\n";
 
-// Get next chunk of users to mail
-$users_res = db_query_qpa ($qpa);
-
-$err .= "Mailing ".db_numrows($users_res)." users.\n";
-
 // If no more users left, we've finished with this mailing
-if ($users_res && db_numrows($users_res)==0) {
+if (count ($filtered_users)==0) {
 	db_query_params ('UPDATE massmail_queue SET failed_date=0,finished_date=$1 WHERE id=$2',
 			 array(time(),
 			       $mail_id));
@@ -179,7 +140,7 @@
 
 // Actual mailing loop
 $compt = 0;
-while ($row =& db_fetch_array($users_res)) {
+foreach ($filtered_users as $user) {
 	$compt++;
 	if ($type=='SITE' || $type=='COMMNTY') {
 		$tail = "\r\n==================================================================\r\n" ;
@@ -192,12 +153,12 @@
 '), 
 				  forge_get_config ('forge_name'), 
 				  util_make_url('/account/'),
-				  util_make_url('/account/unsubscribe.php?ch=_'.$row['confirm_hash'])) ;
+				  util_make_url('/account/unsubscribe.php?ch=_'.$user->getConfirmHash())) ;
 	} else {
 		$tail = "" ;
 	}
-	util_send_message($row['email'],$subj, $body."\r\n".$tail,'noreply@'.forge_get_config('web_host'));
-	$last_userid = $row['user_id'];
+	util_send_message($user->getEmail(),$subj, $body."\r\n".$tail,'noreply@'.forge_get_config('web_host'));
+	$last_userid = $user->getID();
 
 	sleep($SLEEP);
 }
@@ -220,7 +181,7 @@
 		$err .= "Could not remove lock\n";
 	}
 	if (!cron_entry(6,$mess.$err)) {
-		# rely on crond to report the error
+		// rely on crond to report the error
 		echo "cron_entry error: ".db_error()."\n";
 	}
 	exit;

Modified: trunk/src/cronjobs/site_stats.php
===================================================================
--- trunk/src/cronjobs/site_stats.php	2010-09-25 14:08:11 UTC (rev 10687)
+++ trunk/src/cronjobs/site_stats.php	2010-09-25 15:17:54 UTC (rev 10688)
@@ -109,9 +109,16 @@
 			array ("$year$month",
 				$day));
 $err .= db_error();
+if (PFO_USE_RBAC) {
+$rel = db_query_params ('INSERT INTO stats_project_developers (month,day,group_id,developers)
+SELECT  $1::int AS month, $2::int, pfo_role.home_group_id AS group_id, COUNT(DISTINCT(pfo_user_role.user_id)) AS developers FROM pfo_user_role, pfo_role WHERE pfo_user_role.role_id = pfo_role.role_id AND pfo_role.home_group_id IS NOT NULL GROUP BY pfo_role.home_group_id',
+			array ("$year$month",
+			       $day));
+} else {
 $rel = db_query_params ('INSERT INTO stats_project_developers (month,day,group_id,developers) SELECT $1::int AS month,$2::int AS day,group_id,count(*) AS count FROM user_group GROUP BY month,day,group_id',
 			array ("$year$month",
 			       $day));
+}
 $err .= db_error();
 
 db_commit();




More information about the Fusionforge-commits mailing list