2 /** FusionForge Subversion plugin
4 * Copyright 2003-2009, Roland Mas
5 * Copyright 2004, GForge, LLC
7 * This file is part of FusionForge.
9 * FusionForge is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published
11 * by the Free Software Foundation; either version 2 of the License,
12 * or (at your option) any later version.
14 * FusionForge is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with FusionForge; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 class SVNPlugin extends SCMPlugin {
26 function SVNPlugin () {
29 $this->name = 'scmsvn';
31 $this->hooks[] = 'scm_browser_page';
32 $this->hooks[] = 'scm_generate_snapshots' ;
33 $this->hooks[] = 'scm_gather_stats' ;
35 require_once $gfconfig.'plugins/scmsvn/config.php' ;
37 $this->default_svn_server = $default_svn_server ;
38 $this->use_ssh = $use_ssh;
39 $this->use_dav = $use_dav;
40 $this->use_ssl = $use_ssl;
41 $this->svn_root = $svn_root;
46 function getDefaultServer() {
47 return $this->default_svn_server ;
50 function printShortStats ($params) {
51 $project = $this->checkParams ($params) ;
56 if ($project->usesPlugin($this->name)) {
57 $result = db_query_params('SELECT sum(commits) AS commits, sum(adds) AS adds FROM stats_cvs_group WHERE group_id=$1',
58 array ($project->getID())) ;
59 $commit_num = db_result($result,0,'commits');
60 $add_num = db_result($result,0,'adds');
67 echo ' (SVN: '.sprintf(_('<strong>%1$s</strong> commits, <strong>%2$s</strong> adds'), number_format($commit_num, 0), number_format($add_num, 0)).")";
71 function getBlurb () {
72 return _('<p>Documentation for Subversion (sometimes referred to as "SVN") is available <a href="http://svnbook.red-bean.com/">here</a>.</p>') ;
75 function getInstructionsForAnon ($project) {
76 $b = _('<p><b>Anonymous Subversion Access</b></p><p>This project\'s SVN repository can be checked out through anonymous access with the following command(s).</p>');
79 $b .= '<tt>svn checkout svn://'.$project->getSCMBox().'/'.$this->svn_root.'/'.$project->getUnixName().'</tt><br />';
82 $b .= '<tt>svn checkout --username anonsvn http'.(($this->use_ssl) ? 's' : '').'://' . $project->getSCMBox(). '/' . $this->svn_root .'/'. $project->getUnixName() .'</tt><br/><br/>';
83 $b .= _('The password is \'anonsvn\'').'<br/>';
89 function getInstructionsForRW ($project) {
92 $b .= _('<p><b>Developer Subversion Access via SSH</b></p><p>Only project developers can access the SVN tree via this method. SSH must be installed on your client machine. Substitute <i>developername</i> with the proper values. Enter your site password when prompted.</p>');
93 $b .= '<p><tt>svn checkout svn+ssh://<i>'._('developername').'</i>@' . $project->getSCMBox() . '/'. $this->svn_root .'/'. $project->getUnixName().'</tt></p>' ;
96 $b .= _('<p><b>Developer Subversion Access via DAV</b></p><p>Only project developers can access the SVN tree via this method. Substitute <i>developername</i> with the proper values. Enter your site password when prompted.</p>');
97 $b .= '<p><tt>svn checkout --username <i>'._('developername').'</i> http'.(($this->use_ssl) ? 's' : '').'://'. $project->getSCMBox() .'/'. $this->svn_root .'/'.$project->getUnixName().'</tt></p>' ;
103 function getSnapshotPara ($project) {
107 function getBrowserLinkBlock ($project) {
109 $b = $HTML->boxMiddle(_('Subversion Repository Browser'));
110 $b .= _('<p>Browsing the Subversion 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.</p>');
112 $b .= util_make_link ("/scm/browser.php?group_id=".$project->getID(),
113 _('Browse Subversion Repository')
119 function getStatsBlock ($project) {
123 $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',
124 array ($project->getID()));
126 if (db_numrows($result) > 0) {
127 $b .= $HTML->boxMiddle(_('Repository Statistics'));
129 $tableHeaders = array(
134 $b .= $HTML->listTableTop($tableHeaders);
137 $total = array('adds' => 0, 'commits' => 0);
139 while($data = db_fetch_array($result)) {
140 $b .= '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
141 $b .= '<td width="50%">' ;
142 $b .= util_make_link_u ($data['user_name'], $data['user_id'], $data['realname']) ;
143 $b .= '</td><td width="25%" align="right">'.$data['adds']. '</td>'.
144 '<td width="25%" align="right">'.$data['commits'].'</td></tr>';
145 $total['adds'] += $data['adds'];
146 $total['commits'] += $data['commits'];
149 $b .= '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
150 $b .= '<td width="50%"><strong>'._('Total').':</strong></td>'.
151 '<td width="25%" align="right"><strong>'.$total['adds']. '</strong></td>'.
152 '<td width="25%" align="right"><strong>'.$total['commits'].'</strong></td>';
154 $b .= $HTML->listTableBottom();
155 $b .= '<hr size="1" />';
161 function printBrowserPage ($params) {
164 $project = $this->checkParams ($params) ;
169 if ($project->usesPlugin ($this->name)) {
170 if ($this->browserDisplayable ($project)) {
171 print '<iframe src="'.util_make_url ("/scm/viewvc.php/?root=".$project->getUnixName()).'" frameborder="no" width=100% height=700></iframe>' ;
176 function createOrUpdateRepo ($params) {
177 $project = $this->checkParams ($params) ;
182 if (! $project->usesPlugin ($this->name)) {
186 $repo = $this->svn_root . '/' . $project->getUnixName() ;
187 $unix_group = 'scm_' . $project->getUnixName() ;
189 if (!is_dir ($repo) || !is_file ("$repo/format")) {
190 system ("svnadmin create --fs-type fsfs $repo") ;
193 system ("chgrp -R $unix_group $repo") ;
194 if ($project->enableAnonSCM()) {
195 system ("chmod -R g+wXs,o+rX-w $repo") ;
197 system ("chmod -R g+wXs,o-rwx $repo") ;
201 function gatherStats ($params) {
202 global $last_user, $last_time, $last_tag, $time_ok, $start_time, $end_time,
203 $adds, $deletes, $updates, $commits, $date_key,
204 $usr_adds, $usr_deletes, $usr_updates;
206 $project = $this->checkParams ($params) ;
211 if (! $project->usesPlugin ($this->name)) {
215 if ($params['mode'] == 'day') {
218 $year = $params ['year'] ;
219 $month = $params ['month'] ;
220 $day = $params ['day'] ;
221 $month_string = sprintf( "%04d%02d", $year, $month );
222 $start_time = gmmktime( 0, 0, 0, $month, $day, $year);
223 $end_time = $start_time + 86400;
227 $usr_adds = array () ;
228 $usr_updates = array () ;
230 $repo = $this->svn_root . '/' . $project->getUnixName() ;
231 if (!is_dir ($repo) || !is_file ("$repo/format")) {
232 echo "No repository\n" ;
237 $pipe = popen ("svn log file://$repo --xml -v -q", 'r' ) ;
239 // cleaning stats_cvs_* table for the current day
240 $res = db_query_params ('DELETE FROM stats_cvs_group WHERE month=$1 AND day=$2 AND group_id=$3',
241 array ($month_string,
243 $project->getID())) ;
245 echo "Error while cleaning stats_cvs_group\n" ;
250 $res = db_query_params ('DELETE FROM stats_cvs_user WHERE month=$1 AND day=$2 AND group_id=$3',
251 array ($month_string,
253 $project->getID())) ;
255 echo "Error while cleaning stats_cvs_user\n" ;
260 $xml_parser = xml_parser_create();
261 xml_set_element_handler($xml_parser, "SVNPluginStartElement", "SVNPluginEndElement");
262 xml_set_character_data_handler($xml_parser, "SVNPluginCharData");
264 // Analyzing history stream
265 while (!feof($pipe) &&
266 $data = fgets ($pipe, 4096)) {
268 if (!xml_parse ($xml_parser, $data, feof ($pipe))) {
269 debug("Unable to parse XML with error " .
270 xml_error_string(xml_get_error_code($xml_parser)) .
272 xml_get_current_line_number($xml_parser));
278 if (!$time_ok && $last_time && $last_time < $start_time) {
283 xml_parser_free ($xml_parser);
285 // inserting group results in stats_cvs_groups
286 if (!db_query_params ('INSERT INTO stats_cvs_group (month,day,group_id,checkouts,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
287 array ($month_string,
293 echo "Error while inserting into stats_cvs_group\n" ;
298 // building the user list
299 $user_list = array_unique( array_merge( array_keys( $usr_adds ), array_keys( $usr_updates ) ) );
301 foreach ( $user_list as $user ) {
302 // trying to get user id from user name
303 $u = &user_get_object_by_name ($user) ;
305 $user_id = $u->getID();
310 if (!db_query_params ('INSERT INTO stats_cvs_user (month,day,group_id,user_id,commits,adds) VALUES ($1,$2,$3,$4,$5,$6)',
311 array ($month_string,
315 $usr_updates[$user] ? $usr_updates[$user] : 0,
316 $usr_adds[$user] ? $usr_adds[$user] : 0))) {
317 echo "Error while inserting into stats_cvs_user\n" ;
326 function generateSnapshots ($params) {
327 global $sys_scm_snapshots_path ;
328 global $sys_scm_tarballs_path ;
330 $project = $this->checkParams ($params) ;
335 $group_name = $project->getUnixName() ;
337 $snapshot = $sys_scm_snapshots_path.'/'.$group_name.'-scm-latest.tar.gz';
338 $tarball = $sys_scm_tarballs_path.'/'.$group_name.'-scmroot.tar.gz';
340 if (! $project->usesPlugin ($this->name)) {
344 if (! $project->enableAnonSCM()) {
350 $toprepo = $this->svn_root ;
351 $repo = $toprepo . '/' . $project->getUnixName() ;
353 if (!is_dir ($repo) || !is_file ("$repo/format")) {
359 $tmp = trim (`mktemp -d`) ;
363 $today = date ('Y-m-d') ;
364 $dir = $project->getUnixName ()."-$today" ;
365 system ("mkdir -p $tmp") ;
367 system ("svn ls file://$repo/trunk", $code) ;
369 system ("cd $tmp ; svn checkout file://$repo/trunk $dir > /dev/null 2>&1") ;
370 system ("tar czCf $tmp $tmp/snapshot.tar.gz $dir") ;
371 chmod ("$tmp/snapshot.tar.gz", 0644) ;
372 copy ("$tmp/snapshot.tar.gz", $snapshot) ;
373 unlink ("$tmp/snapshot.tar.gz") ;
374 system ("rm -rf $tmp/$dir") ;
379 system ("tar czCf $toprepo $tmp/tarball.tar.gz " . $project->getUnixName()) ;
380 chmod ("$tmp/tarball.tar.gz", 0644) ;
381 copy ("$tmp/tarball.tar.gz", $tarball) ;
382 unlink ("$tmp/tarball.tar.gz") ;
383 system ("rm -rf $tmp") ;
387 // End of class, helper functions now
389 function SVNPluginCharData ($parser, $chars) {
390 global $last_tag, $last_user, $last_time, $start_time, $end_time,
391 $time_ok, $user_list;
394 $last_user = ereg_replace ('[^a-z0-9_-]', '',
395 strtolower (trim ($chars))) ;
398 $chars = preg_replace('/T(\d\d:\d\d:\d\d)\.\d+Z?$/', ' ${1}', $chars);
399 $last_time = strtotime($chars);
400 if ($start_time <= $last_time && $last_time < $end_time) {
409 function SVNPluginStartElement($parser, $name, $attrs) {
410 global $last_user, $last_time, $last_tag, $time_ok,
411 $adds, $updates, $usr_adds, $usr_updates;
415 // Make sure we clean up before doing a new log entry
421 if ($attrs['ACTION'] == "M") {
424 $usr_updates[$last_user]++;
426 } elseif ($attrs['ACTION'] == "A") {
429 $usr_adds[$last_user]++;
437 function SVNPluginEndElement ($parser, $name) {
438 global $time_ok, $last_tag, $commits;
439 if ($name == "LOGENTRY" && $time_ok) {
447 // c-file-style: "bsd"