2 /** FusionForge Bazaar plugin
4 * Copyright 2009, Roland Mas <lolando@debian.org>
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
19 * along with FusionForge; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 class BzrPlugin extends SCM {
25 function BzrPlugin () {
28 $this->name = 'scmbzr';
29 $this->text = 'Bazaar';
30 $this->hooks[] = 'scm_page';
31 $this->hooks[] = 'scm_admin_update';
32 $this->hooks[] = 'scm_admin_page';
33 $this->hooks[] = 'scm_stats';
34 $this->hooks[] = 'scm_plugin';
35 $this->hooks[] = 'scm_createrepo';
37 require_once $gfconfig.'plugins/scmbzr/config.php' ;
39 $this->default_bzr_server = $default_bzr_server ;
40 $this->enabled_by_default = $enabled_by_default ;
41 $this->bzr_root = $bzr_root;
46 function getDefaultServer() {
47 return $this->default_bzr_server ;
50 function CallHook ($hookname, $params) {
55 $group_id = $params['group_id'] ;
56 $this->getPage ($group_id) ;
58 case 'scm_admin_update':
59 $this->AdminUpdate ($params) ;
61 case 'scm_admin_page':
62 $this->getAdminPage ($params) ;
65 $this->getStats ($params) ;
67 case 'scm_createrepo':
68 $this->createOrUpdateRepo ($params) ;
71 $scm_plugins=& $params['scm_plugins'];
72 $scm_plugins[]=$this->name;
79 function getPage ($group_id) {
80 global $HTML, $sys_scm_snapshots_path;
82 $project =& group_get_object($group_id);
83 if (!$project || !is_object($project)) {
85 } elseif ($project->isError()) {
89 if ($project->usesPlugin ($this->name)) {
90 // Bazaar browser links must be displayed if
91 // project enables anonymous Bazaar
92 $displayBzrBrowser = $project->enableAnonSCM();
94 // Table for summary info
95 print ('<table width="100%">
98 print _('<p>Documentation for Bazaar (sometimes referred to as "bzr") is available <a href="http://doc.bazaar-vcs.org/latest/en/user-guide/index.html">here</a>.</p>');
100 // Anonymous Bazaar Instructions
101 if ($project->enableAnonSCM()) {
102 print _("<p><b>Anonymous Bazaar Access</b></p><p>This project's Bazaar repository can be accessed anonymously through HTTP.</p>");
104 print _("A list of available branches can be obtained with the following command:") ;
106 print '<tt>bzr branches http://' . $project->getSCMBox(). '/' . $this->svn_root .'/'. $project->getUnixName() .'</tt>';
109 print _("To check out one of these branches:") ;
111 print '<tt>bzr checkout http://' . $project->getSCMBox(). '/' . $this->svn_root .'/'. $project->getUnixName() .'<i>'._('branchname').'</i></tt>' ;
116 echo _('<p><b>Developer Bazaar Access via SSH</b></p><p>Only project developers can access the Bazaar branches 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>');
117 print '<p><tt>bzr checkout bzr+ssh://<i>'._('developername').'</i>@' . $project->getSCMBox() . '/'. $this->svn_root .'/'. $project->getUnixName().'/'._('branchname').'</tt></p>' ;
120 if ($displaySvnBrowser) {
121 $filename=$project->getUnixName().'-scm-latest.tar.gz';
122 if (file_exists($sys_scm_snapshots_path.'/'.$filename)) {
124 print util_make_link ("/snapshots.php?group_id=$group_id",
125 _('Download The Nightly Bazaar Tree Snapshot')
131 print '</td><td width="35%" valign="top">' ;
134 echo $HTML->boxTop(_('Repository History'));
135 echo _('Not implemented yet');
136 /* echo $this->getDetailedStats(array('group_id'=>$group_id)).'<p>';
137 if ($displaySvnBrowser) {
138 echo _('<b>Browse the Bazaar Tree</b><p>Browsing the Bazaar tree gives you a great view into the current status of this project\'s code. You may also view the complete histories of any file in the repository.</p>');
140 echo util_make_link ("/scm/viewvc.php/?root=".$project->getUnixName(),
141 _('Browse Bazaar Repository')
146 echo $HTML->boxBottom();
147 print '</td></tr></table>' ;
151 function AdminUpdate ($params) {
152 $group =& group_get_object($params['group_id']);
153 if (!$group || !is_object($group)) {
155 } elseif ($group->isError()) {
159 if ( $group->usesPlugin ( $this->name ) ) {
160 if ($params['scmbzr_enable_anon_bzr']) {
161 $group->SetUsesAnonSCM(true);
163 $group->SetUsesAnonSCM(false);
168 // This function is used to render checkboxes below
171 return 'checked="checked"';
177 function getAdminPage ($params) {
178 $group =& group_get_object($params['group_id']);
179 if ( $group->usesPlugin ( $this->name ) && $group->isPublic()) {
181 <p><input type="checkbox" name="scmbzr_enable_anon_bzr" value="1" <?php echo $this->c($group->enableAnonSCM()); ?> /><strong><?php echo _('Enable Anonymous Access') ?></strong></p>
186 function getStats ($params) {
187 $group_id = $params['group_id'] ;
188 $project =& group_get_object($group_id);
189 if (!$project || !is_object($project)) {
191 } elseif ($project->isError()) {
195 if ($project->usesPlugin ($this->name)) {
196 list($commit_num, $add_num) = $this->getTotalStats($group_id);
197 echo ' (Bazaar: '.sprintf(_('<strong>%1$s</strong> updates, <strong>%2$s</strong> adds'), number_format($commit_num, 0), number_format($add_num, 0)).')';
201 // Get the total stats for a group
202 function getTotalStats($group_id) {
204 SELECT SUM(commits) AS commits, SUM(adds) AS adds
206 WHERE group_id='$group_id'");
207 $commit_num = db_result($result,0,0);
208 $add_num = db_result($result,0,1);
215 return array($commit_num, $add_num);
218 function getDetailedStats ($params) {
220 $group_id = $params['group_id'] ;
223 SELECT u.realname, u.user_name, u.user_id, sum(commits) as commits, sum(adds) as adds, sum(adds+commits) as combined
224 FROM stats_cvs_user s, users u
225 WHERE group_id=\''.$group_id.'\' AND s.user_id=u.user_id AND (commits>0 OR adds >0)
226 GROUP BY group_id, realname, user_name
227 ORDER BY combined DESC, realname;
230 if (db_numrows($result) > 0) {
231 $tableHeaders = array(
236 echo $HTML->listTableTop($tableHeaders);
239 $total = array('adds' => 0, 'commits' => 0);
241 while($data = db_fetch_array($result)) {
242 echo '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
243 echo '<td width="50%">' ;
244 echo util_make_link_u ($data['user_name'], $data['user_id'], $data['realname']) ;
245 echo '</td><td width="25%" align="right">'.$data['adds']. '</td>'.
246 '<td width="25%" align="right">'.$data['commits'].'</td></tr>';
247 $total['adds'] += $data['adds'];
248 $total['commits'] += $data['commits'];
251 list($commit_num, $add_num) = $this->getTotalStats($group_id);
252 if ($commit_num > $total['commits'] ||
253 $add_num > $total['adds']) {
254 echo '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
255 echo '<td width="50%">' .
257 '</td><td width="25%" align="right">'.
258 ($add_num - $total['adds']) . '</td>'.
259 '<td width="25%" align="right">'.
260 ($commit_num - $total['commits']) .
264 echo '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
265 echo '<td width="50%"><strong>'._('Total').':</strong></td>'.
266 '<td width="25%" align="right"><strong>'.$add_num. '</strong></td>'.
267 '<td width="25%" align="right"><strong>'.$commit_num.'</strong></td>';
269 echo $HTML->listTableBottom();
270 echo '<hr size="1" />';
274 function createOrUpdateRepo ($params) {
275 $group_id = $params['group_id'] ;
277 $project =& group_get_object($group_id);
278 if (!$project || !is_object($project)) {
280 } elseif ($project->isError()) {
284 if (! $project->usesPlugin ($this->name)) {
288 $repo = $this->bzr_root . '/' . $project->getUnixName() ;
289 $unix_group = 'scm_' . $project->getUnixName() ;
291 $repo_exists = false ;
292 if (is_dir ($repo)) {
293 $pipe = popen ("bzr info $repo 2>/dev/null", "r") ;
294 $line = fgets ($pipe) ;
297 if (preg_match ("/^Shared repository/", $line) != 0) {
298 $repo_exists = true ;
303 system ("bzr init-repo --no-trees $repo") ;
306 system ("chgrp -R $unix_group $repo") ;
307 if ($project->enableAnonSCM()) {
308 system ("chmod -R g+wXs,o+rX-w $repo") ;
310 system ("chmod -R g+wXs,o-rwx $repo") ;
317 // c-file-style: "bsd"