2 /** FusionForge CVS plugin
4 * Copyright 2004-2009, Roland Mas
6 * This file is part of FusionForge.
8 * FusionForge is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; either version 2 of the License,
11 * or (at your option) any later version.
13 * FusionForge is distributed in the hope that it will be useful, but
14 * 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.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 forge_define_config_item ('default_server', 'scmcvs', forge_get_config ('web_host')) ;
24 forge_define_config_item ('repos_path', 'scmcvs', forge_get_config('chroot').'/scmrepos/cvs') ;
26 class CVSPlugin extends SCMPlugin {
27 function CVSPlugin () {
30 global $cvsdir_prefix ;
32 $this->name = 'scmcvs';
34 $this->hooks[] = 'scm_browser_page';
35 $this->hooks[] = 'scm_generate_snapshots' ;
36 $this->hooks[] = 'scm_gather_stats' ;
38 $this->provides['cvs'] = true;
43 function getDefaultServer() {
44 return forge_get_config('default_server', 'scmcvs');
47 function printShortStats ($params) {
48 $project = $this->checkParams ($params) ;
53 if ($project->usesPlugin($this->name)) {
54 $result = db_query_params('SELECT sum(commits) AS commits, sum(adds) AS adds FROM stats_cvs_group WHERE group_id=$1',
55 array ($project->getID())) ;
56 $commit_num = db_result($result,0,'commits');
57 $add_num = db_result($result,0,'adds');
64 echo ' (CVS: '.sprintf(_('<strong>%1$s</strong> commits, <strong>%2$s</strong> adds'), number_format($commit_num, 0), number_format($add_num, 0)).")";
68 function getBlurb () {
69 return '<p>' . _('CVS documentation is available <a href="http://cvsbook.red-bean.com/">here</a>.') . '</p>';
72 function getInstructionsForAnon ($project) {
73 $cvsrootend = $this->getBoxForProject($project).':'.forge_get_config('repos_path', 'scmcvs').'/'.$project->getUnixName();
74 $b = '<h2>' . _('Anonymous CVS Access') . '</h2>';
76 $b .= _('This project\'s CVS repository can be checked out through anonymous (pserver) CVS with the following instruction set. The module you wish to check out must be specified as the <i>modulename</i>. When prompted for a password for <i>anonymous</i>, simply press the Enter key.');
79 <tt>cvs -d :pserver:anonymous@' . $cvsrootend.' login</tt><br/>
80 <tt>cvs -d :pserver:anonymous@' . $cvsrootend.' checkout <em>'._('modulename').'</em></tt>
86 function getInstructionsForRW ($project) {
87 $cvsrootend = $this->getBoxForProject($project).':'.forge_get_config('repos_path', 'scmcvs').'/'.$project->getUnixName();
88 if (session_loggedin()) {
89 $u =& user_get_object(user_getid()) ;
90 $d = $u->getUnixName() ;
91 $b = '<h2>' . _('Developer CVS Access via SSH') . '</h2>';
93 $b .= _('Only project developers can access the CVS tree via this method. SSH must be installed on your client machine. Substitute <i>modulename</i> with the proper values. Enter your site password when prompted.');
96 <tt>export CVS_RSH=ssh</tt><br/>
97 <tt>cvs -d :ext:'.$d.'@'.$cvsrootend.' checkout <em>'._('modulename').'</em></tt>
100 $b = '<h2>' . _('Developer CVS Access via SSH') . '</h2>';
102 $b .= _('Only project developers can access the CVS tree via this method. SSH must be installed on your client machine. Substitute <i>modulename</i> and <i>developername</i> with the proper values. Enter your site password when prompted.');
105 <tt>export CVS_RSH=ssh</tt><br/>
106 <tt>cvs -d :ext:<em>'._('developername').'</em>@'.$cvsrootend.' checkout <em>'._('modulename').'</em></tt>
112 function getSnapshotPara ($project) {
115 $filename = $project->getUnixName().'-scm-latest.tar'.util_get_compressed_file_extension();
116 if (file_exists(forge_get_config('scm_snapshots_path').'/'.$filename)) {
118 $b .= util_make_link ("/snapshots.php?group_id=".$project->getID(),
119 _('Download the nightly snapshot')
126 function getBrowserLinkBlock ($project) {
128 $b = $HTML->boxMiddle(_('CVS Repository Browser'));
130 $b .= _('Browsing the CVS tree gives you a view into the current status of this project\'s code. You may also view the complete histories of any file in the repository.');
133 $b .= util_make_link ("/scm/browser.php?group_id=".$project->getID(),
134 _('Browse CVS Repository')
140 function getStatsBlock ($project) {
144 $result = db_query_params('SELECT u.realname, u.user_name, u.user_id, sum(commits) as commits, sum(adds) as adds, sum(adds+commits) as combined FROM stats_cvs_user s, users u WHERE group_id=$1 AND s.user_id=u.user_id AND (commits>0 OR adds >0) GROUP BY u.user_id, realname, user_name, u.user_id ORDER BY combined DESC, realname',
145 array ($project->getID()));
147 if (db_numrows($result) > 0) {
148 $b .= $HTML->boxMiddle(_('Repository Statistics'));
150 $tableHeaders = array(
155 $b .= $HTML->listTableTop($tableHeaders);
158 $total = array('adds' => 0, 'commits' => 0);
160 while($data = db_fetch_array($result)) {
161 $b .= '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
162 $b .= '<td width="50%">' ;
163 $b .= util_make_link_u ($data['user_name'], $data['user_id'], $data['realname']) ;
164 $b .= '</td><td width="25%" align="right">'.$data['adds']. '</td>'.
165 '<td width="25%" align="right">'.$data['commits'].'</td></tr>';
166 $total['adds'] += $data['adds'];
167 $total['commits'] += $data['commits'];
170 $b .= '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
171 $b .= '<td width="50%"><strong>'._('Total').':</strong></td>'.
172 '<td width="25%" align="right"><strong>'.$total['adds']. '</strong></td>'.
173 '<td width="25%" align="right"><strong>'.$total['commits'].'</strong></td>';
175 $b .= $HTML->listTableBottom();
181 function printBrowserPage ($params) {
184 $project = $this->checkParams ($params) ;
189 if ($project->usesPlugin ($this->name)) {
190 if ($this->browserDisplayable ($project)) {
191 session_redirect("/scm/viewvc.php/?root=".$project->getUnixName());
196 function createOrUpdateRepo ($params) {
197 $project = $this->checkParams ($params) ;
202 if (! $project->usesPlugin ($this->name)) {
206 $repo = forge_get_config('repos_path', 'scmcvs') . '/' . $project->getUnixName() ;
207 $locks_dir = forge_get_config('repos_path', 'scmcvs') . '/cvs-locks/' . $project->getUnixName() ;
209 $repo_exists = false ;
210 if (is_dir ($repo) && is_dir ("$repo/CVSROOT")) {
211 $repo_exists = true ;
215 system ("cvs -d $repo init") ;
216 system ("mkdir -p $locks_dir") ;
219 if (forge_get_config('use_shell')) {
220 $unix_group = 'scm_' . $project->getUnixName() ;
222 system ("chgrp -R $unix_group $repo $locks_dir") ;
223 system ("chmod 3777 $locks_dir") ;
224 system ("echo \"SystemAuth=no\" > $repo/CVSROOT/config");
225 system ("echo \"LockDir=$locks_dir\" >> $repo/CVSROOT/config");
226 system ("echo \"UseNewInfoFmtStrings=yes\" >> $repo/CVSROOT/config");
227 if ($project->enableAnonSCM()) {
228 system ("chmod -R g+wXs,o+rX-w $repo") ;
229 system ("echo \"anonymous\" > $repo/CVSROOT/readers");
230 system ("echo \"anonymous:\" > $repo/CVSROOT/passwd");
232 system ("chmod -R g+wXs,o-rwx $repo") ;
233 system ("echo \"\" > $repo/CVSROOT/readers");
234 system ("echo \"\" > $repo/CVSROOT/passwd");
237 $unix_user = forge_get_config ('apache_user') ;
238 $unix_group = forge_get_config ('apache_user') ;
239 system ("chown -R $unix_user:$unix_group $repo") ;
240 system ("chmod -R g-rwx,o-rwx $repo") ;
244 function gatherStats ($params) {
245 $project = $this->checkParams ($params) ;
250 if (! $project->usesPlugin ($this->name)) {
254 if ($params['mode'] == 'day') {
257 $year = $params ['year'] ;
258 $month = $params ['month'] ;
259 $day = $params ['day'] ;
260 $month_string = sprintf( "%04d%02d", $year, $month );
261 $day_begin = gmmktime( 0, 0, 0, $month, $day, $year);
262 $day_end = $day_begin + 86400;
264 $repo = forge_get_config('repos_path', 'scmcvs') . '/' . $project->getUnixName() ;
265 if (!is_dir ($repo) || !is_dir ("$repo/CVSROOT")) {
266 echo "No repository\n" ;
274 $usr_commit = array();
277 $hist_file_path = $repo.'/CVSROOT/history';
278 if (!file_exists($hist_file_path)
279 || !is_readable($hist_file_path)
280 || filesize($hist_file_path) == 0) {
281 // echo "No history file\n" ;
286 $hist_file =& fopen( $hist_file_path, 'r' );
287 if ( ! $hist_file ) {
288 echo "Unreadable history\n" ;
293 // cleaning stats_cvs_* table for the current day
294 $res = db_query_params ('DELETE FROM stats_cvs_group WHERE month=$1 AND day=$2 AND group_id=$3',
295 array ($month_string,
297 $project->getID())) ;
299 echo "Error while cleaning stats_cvs_group\n" ;
304 $res = db_query_params ('DELETE FROM stats_cvs_user WHERE month=$1 AND day=$2 AND group_id=$3',
305 array ($month_string,
307 $project->getID())) ;
309 echo "Error while cleaning stats_cvs_user\n" ;
315 // analyzing history file
316 while (!feof($hist_file)) {
317 $hist_line = fgets($hist_file, 1024);
318 if ( preg_match( '/^\s*$/', $hist_line ) ) {
321 list( $cvstime,$user,$curdir,$module,$rev,$file ) = explode( '|', $hist_line );
323 $type = substr($cvstime, 0, 1);
324 $time_parsed = hexdec( substr($cvstime, 1, 8) );
326 if ( ($time_parsed > $day_begin) && ($time_parsed < $day_end) ) {
327 if ( $type == 'M' ) {
329 if(!isset($usr_commit[$user])) $usr_commit[$user] = 0;
330 $usr_commit[$user]++;
331 } elseif ( $type == 'A' ) {
333 if(!isset($usr_add[$user])) $usr_add[$user] = 0;
335 } elseif ( $type == 'O' || $type == 'E' ) {
337 // ignoring checkouts on a per-user
339 } elseif ( $time_parsed > $day_end ) {
343 fclose( $hist_file );
345 // inserting group results in stats_cvs_groups
346 if (!db_query_params ('INSERT INTO stats_cvs_group (month,day,group_id,checkouts,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
347 array ($month_string,
353 echo "Error while inserting into stats_cvs_group\n" ;
358 // building the user list
359 $user_list = array_unique( array_merge( array_keys( $usr_add ), array_keys( $usr_commit ) ) );
361 foreach ( $user_list as $user ) {
362 // trying to get user id from user name
363 $u = &user_get_object_by_name ($user) ;
365 $user_id = $u->getID();
370 if (!db_query_params ('INSERT INTO stats_cvs_user (month,day,group_id,user_id,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
371 array ($month_string,
375 $usr_commit[$user] ? $usr_commit[$user] : 0,
376 $usr_add[$user] ? $usr_add[$user] : 0))) {
377 echo "Error while inserting into stats_cvs_user\n" ;
386 function generateSnapshots ($params) {
390 $project = $this->checkParams ($params) ;
395 $group_name = $project->getUnixName() ;
397 $snapshot = forge_get_config('scm_snapshots_path').'/'.$group_name.'-scm-latest.tar'.util_get_compressed_file_extension();
398 $tarball = forge_get_config('scm_tarballs_path').'/'.$group_name.'-scmroot.tar'.util_get_compressed_file_extension();
400 if (! $project->usesPlugin ($this->name)) {
404 if (! $project->enableAnonSCM()) {
405 if (file_exists($snapshot)) unlink ($snapshot) ;
406 if (file_exists($tarball)) unlink ($tarball) ;
410 $toprepo = forge_get_config('repos_path', 'scmcvs') ;
411 $repo = $toprepo . '/' . $project->getUnixName() ;
413 $repo_exists = false ;
414 if (is_dir ($repo) && is_dir ("$repo/CVSROOT")) {
415 $repo_exists = true ;
419 if (file_exists($snapshot)) unlink ($snapshot) ;
420 if (file_exists($tarball)) unlink ($tarball) ;
424 $tmp = trim (`mktemp -d`) ;
428 $today = date ('Y-m-d') ;
429 $dir = $project->getUnixName ()."-$today" ;
430 system ("mkdir -p $tmp/$dir") ;
431 system ("cd $tmp/$dir ; cvs -d $repo export -D now . > /dev/null 2>&1") ;
432 system ("tar cCf $tmp - $dir |".forge_get_config('compression_method')."> $tmp/snapshot") ;
433 chmod ("$tmp/snapshot", 0644) ;
434 copy ("$tmp/snapshot", $snapshot) ;
435 unlink ("$tmp/snapshot") ;
436 system ("rm -rf $tmp/$dir") ;
438 system ("tar cCf $toprepo - ".$project->getUnixName() ."|".forge_get_config('compression_method')."> $tmp/tarball") ;
439 chmod ("$tmp/tarball", 0644) ;
440 copy ("$tmp/tarball", $tarball) ;
441 unlink ("$tmp/tarball") ;
442 system ("rm -rf $tmp") ;
448 // c-file-style: "bsd"