2 /** FusionForge Bazaar plugin
4 * Copyright 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
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 SCMPlugin {
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) {
203 $result = db_query_params ('
204 SELECT SUM(commits) AS commits, SUM(adds) AS adds
208 $commit_num = db_result($result,0,0);
209 $add_num = db_result($result,0,1);
216 return array($commit_num, $add_num);
219 function getDetailedStats ($params) {
221 $group_id = $params['group_id'] ;
224 SELECT u.realname, u.user_name, u.user_id, sum(commits) as commits, sum(adds) as adds, sum(adds+commits) as combined
225 FROM stats_cvs_user s, users u
226 WHERE group_id=\''.$group_id.'\' AND s.user_id=u.user_id AND (commits>0 OR adds >0)
227 GROUP BY group_id, realname, user_name, u.user_id
228 ORDER BY combined DESC, realname;
231 if (db_numrows($result) > 0) {
232 $tableHeaders = array(
237 echo $HTML->listTableTop($tableHeaders);
240 $total = array('adds' => 0, 'commits' => 0);
242 while($data = db_fetch_array($result)) {
243 echo '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
244 echo '<td width="50%">' ;
245 echo util_make_link_u ($data['user_name'], $data['user_id'], $data['realname']) ;
246 echo '</td><td width="25%" align="right">'.$data['adds']. '</td>'.
247 '<td width="25%" align="right">'.$data['commits'].'</td></tr>';
248 $total['adds'] += $data['adds'];
249 $total['commits'] += $data['commits'];
252 list($commit_num, $add_num) = $this->getTotalStats($group_id);
253 if ($commit_num > $total['commits'] ||
254 $add_num > $total['adds']) {
255 echo '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
256 echo '<td width="50%">' .
258 '</td><td width="25%" align="right">'.
259 ($add_num - $total['adds']) . '</td>'.
260 '<td width="25%" align="right">'.
261 ($commit_num - $total['commits']) .
265 echo '<tr '. $HTML->boxGetAltRowStyle($i) .'>';
266 echo '<td width="50%"><strong>'._('Total').':</strong></td>'.
267 '<td width="25%" align="right"><strong>'.$add_num. '</strong></td>'.
268 '<td width="25%" align="right"><strong>'.$commit_num.'</strong></td>';
270 echo $HTML->listTableBottom();
271 echo '<hr size="1" />';
275 function createOrUpdateRepo ($params) {
276 $group_id = $params['group_id'] ;
278 $project =& group_get_object($group_id);
279 if (!$project || !is_object($project)) {
281 } elseif ($project->isError()) {
285 if (! $project->usesPlugin ($this->name)) {
289 $repo = $this->bzr_root . '/' . $project->getUnixName() ;
290 $unix_group = 'scm_' . $project->getUnixName() ;
292 $repo_exists = false ;
293 if (is_dir ($repo)) {
294 $pipe = popen ("bzr info $repo 2>/dev/null", "r") ;
295 $line = fgets ($pipe) ;
298 if (preg_match ("/^Shared repository/", $line) != 0) {
299 $repo_exists = true ;
304 system ("bzr init-repo --no-trees $repo") ;
307 system ("chgrp -R $unix_group $repo") ;
308 if ($project->enableAnonSCM()) {
309 system ("chmod -R g+wXs,o+rX-w $repo") ;
311 system ("chmod -R g+wXs,o-rwx $repo") ;
318 // c-file-style: "bsd"