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 ;
214 if (forge_get_config('use_shell')) {
215 $unix_group = 'scm_' . $project->getUnixName() ;
217 $unix_group = forge_get_config ('apache_user') ;
221 if (!@mkdir($repo, 0700)) {
225 system ("cvs -d $repo init", $ret) ;
229 system ("mkdir -p $locks_dir") ;
230 system ("chgrp $unix_group $locks_dir") ;
231 system ("chmod 3777 $locks_dir") ;
233 if (forge_get_config('use_shell')) {
234 util_create_file_with_contents ("$repo/CVSROOT/config", "SystemAuth=no\nLockDir=$locks_dir\nUseNewInfoFmtStrings=yes\n");
235 if ($project->enableAnonSCM()) {
236 util_create_file_with_contents ("$repo/CVSROOT/readers", "anonymous\n");
237 util_create_file_with_contents ("$repo/CVSROOT/passwd", "anonymous:\n");
238 system ("chmod -R g+wXs,o+rX-w $repo") ;
240 util_create_file_with_contents ("$repo/CVSROOT/readers", "\n");
241 util_create_file_with_contents ("$repo/CVSROOT/passwd", "\n");
242 system ("chmod -R g+wXs,o-rwx $repo") ;
244 system ("chgrp -R $unix_group $repo") ;
246 $unix_user = forge_get_config ('apache_user') ;
247 system ("chmod -R g-rwx,o-rwx $repo") ;
248 system ("chown -R $unix_user:$unix_group $repo") ;
252 if (forge_get_config('use_shell')) {
253 if ($project->enableAnonSCM()) {
254 util_create_file_with_contents ("$repo/CVSROOT/readers", "anonymous\n");
255 util_create_file_with_contents ("$repo/CVSROOT/passwd", "anonymous:\n");
256 system ("chmod g+wXs,o+rX-w $repo") ;
258 util_create_file_with_contents ("$repo/CVSROOT/readers", "\n");
259 util_create_file_with_contents ("$repo/CVSROOT/passwd", "\n");
260 system ("chmod g+wXs,o-rwx $repo") ;
265 function gatherStats ($params) {
266 $project = $this->checkParams ($params) ;
271 if (! $project->usesPlugin ($this->name)) {
275 if ($params['mode'] == 'day') {
278 $year = $params ['year'] ;
279 $month = $params ['month'] ;
280 $day = $params ['day'] ;
281 $month_string = sprintf( "%04d%02d", $year, $month );
282 $day_begin = gmmktime( 0, 0, 0, $month, $day, $year);
283 $day_end = $day_begin + 86400;
285 $repo = forge_get_config('repos_path', 'scmcvs') . '/' . $project->getUnixName() ;
286 if (!is_dir ($repo) || !is_dir ("$repo/CVSROOT")) {
287 echo "No repository\n" ;
295 $usr_commit = array();
298 $hist_file_path = $repo.'/CVSROOT/history';
299 if (!file_exists($hist_file_path)
300 || !is_readable($hist_file_path)
301 || filesize($hist_file_path) == 0) {
302 // echo "No history file\n" ;
307 $hist_file = fopen( $hist_file_path, 'r' );
308 if ( ! $hist_file ) {
309 echo "Unreadable history\n" ;
314 // cleaning stats_cvs_* table for the current day
315 $res = db_query_params ('DELETE FROM stats_cvs_group WHERE month=$1 AND day=$2 AND group_id=$3',
316 array ($month_string,
318 $project->getID())) ;
320 echo "Error while cleaning stats_cvs_group\n" ;
325 $res = db_query_params ('DELETE FROM stats_cvs_user WHERE month=$1 AND day=$2 AND group_id=$3',
326 array ($month_string,
328 $project->getID())) ;
330 echo "Error while cleaning stats_cvs_user\n" ;
336 // analyzing history file
337 while (!feof($hist_file)) {
338 $hist_line = fgets($hist_file, 1024);
339 if ( preg_match( '/^\s*$/', $hist_line ) ) {
342 list( $cvstime,$user,$curdir,$module,$rev,$file ) = explode( '|', $hist_line );
344 $type = substr($cvstime, 0, 1);
345 $time_parsed = hexdec( substr($cvstime, 1, 8) );
347 if ( ($time_parsed > $day_begin) && ($time_parsed < $day_end) ) {
348 if ( $type == 'M' ) {
350 if(!isset($usr_commit[$user])) $usr_commit[$user] = 0;
351 $usr_commit[$user]++;
352 } elseif ( $type == 'A' ) {
354 if(!isset($usr_add[$user])) $usr_add[$user] = 0;
356 } elseif ( $type == 'O' || $type == 'E' ) {
358 // ignoring checkouts on a per-user
360 } elseif ( $time_parsed > $day_end ) {
364 fclose( $hist_file );
366 // inserting group results in stats_cvs_groups
367 if (!db_query_params ('INSERT INTO stats_cvs_group (month,day,group_id,checkouts,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
368 array ($month_string,
374 echo "Error while inserting into stats_cvs_group\n" ;
379 // building the user list
380 $user_list = array_unique( array_merge( array_keys( $usr_add ), array_keys( $usr_commit ) ) );
382 foreach ( $user_list as $user ) {
383 // trying to get user id from user name
384 $u = &user_get_object_by_name ($user) ;
386 $user_id = $u->getID();
392 if (!db_query_params ('INSERT INTO stats_cvs_user (month,day,group_id,user_id,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
393 array ($month_string,
397 isset ($usr_commit[$user]) ? $usr_commit[$user] : 0,
398 isset ($usr_add[$user]) ? $usr_add[$user] : 0))) {
399 echo "Error while inserting into stats_cvs_user\n" ;
408 function generateSnapshots ($params) {
412 $project = $this->checkParams ($params) ;
417 $group_name = $project->getUnixName() ;
419 $snapshot = forge_get_config('scm_snapshots_path').'/'.$group_name.'-scm-latest.tar'.util_get_compressed_file_extension();
420 $tarball = forge_get_config('scm_tarballs_path').'/'.$group_name.'-scmroot.tar'.util_get_compressed_file_extension();
422 if (! $project->usesPlugin ($this->name)) {
426 if (! $project->enableAnonSCM()) {
427 if (file_exists($snapshot)) unlink ($snapshot) ;
428 if (file_exists($tarball)) unlink ($tarball) ;
432 $toprepo = forge_get_config('repos_path', 'scmcvs') ;
433 $repo = $toprepo . '/' . $project->getUnixName() ;
435 $repo_exists = false ;
436 if (is_dir ($repo) && is_dir ("$repo/CVSROOT")) {
437 $repo_exists = true ;
441 if (file_exists($snapshot)) unlink ($snapshot) ;
442 if (file_exists($tarball)) unlink ($tarball) ;
446 $tmp = trim (`mktemp -d`) ;
450 $today = date ('Y-m-d') ;
451 $dir = $project->getUnixName ()."-$today" ;
452 system ("mkdir -p $tmp/$dir") ;
453 system ("cd $tmp/$dir ; cvs -d $repo export -D now . > /dev/null 2>&1") ;
454 system ("tar cCf $tmp - $dir |".forge_get_config('compression_method')."> $tmp/snapshot") ;
455 chmod ("$tmp/snapshot", 0644) ;
456 copy ("$tmp/snapshot", $snapshot) ;
457 unlink ("$tmp/snapshot") ;
458 system ("rm -rf $tmp/$dir") ;
460 system ("tar cCf $toprepo - ".$project->getUnixName() ."|".forge_get_config('compression_method')."> $tmp/tarball") ;
461 chmod ("$tmp/tarball", 0644) ;
462 copy ("$tmp/tarball", $tarball) ;
463 unlink ("$tmp/tarball") ;
464 system ("rm -rf $tmp") ;
470 // c-file-style: "bsd"