3 * FusionForge source control management
5 * Copyright 2004-2009, Roland Mas
6 * Copyright (C) 2011-2012 Alain Peyrat - Alcatel-Lucent
7 * Copyright 2012,2014 Franck Villaume - TrivialDev
9 * This file is part of FusionForge. FusionForge is free software;
10 * you can redistribute it and/or modify it under the terms of the
11 * GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the Licence, or (at your option)
15 * FusionForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 require_once $gfcommon.'include/scm.php';
27 abstract class SCMPlugin extends Plugin {
29 * SCMPlugin() - constructor
32 function SCMPlugin() {
34 $this->_addHook('group_plugin_use');
35 $this->_addHook('scm_plugin');
36 $this->_addHook('scm_page');
37 $this->_addHook('scm_admin_page');
38 $this->_addHook('scm_admin_update');
39 $this->_addHook('scm_stats');
40 $this->_addHook('scm_create_repo');
42 # Other common hooks that can be enabled per plugin:
43 # scm_generate_snapshots
48 $this->provides['scm'] = true;
51 function CallHook($hookname, &$params) {
53 case 'group_plugin_use': {
54 $systasksq = new SysTasksQ();
55 $systasksq->add(SYSTASK_CORE, SYSTASK_SCM_REPO, $params['group_id']);
59 $scm_plugins=& $params['scm_plugins'];
60 $scm_plugins[]=$this->name;
64 $this->printPage($params);
67 case 'scm_browser_page': {
68 $this->printBrowserPage($params);
71 case 'scm_admin_page': {
72 $this->printAdminPage($params);
75 case 'scm_admin_update': {
76 $this->adminUpdate($params);
80 $this->printShortStats($params);
83 case 'scm_create_repo': {
85 $this->createOrUpdateRepo($params);
88 case 'scm_update_repolist': {
90 $this->updateRepositoryList($params);
93 case 'scm_generate_snapshots': { // Optional
95 $this->generateSnapshots($params);
98 case 'scm_gather_stats': { // Optional
100 $this->gatherStats($params);
103 case 'widgets': { // Optional
104 $this->widgets($params);
107 case 'widget_instance': { // Optional
108 $this->myPageBox($params);
111 case 'activity': { //Optional
112 $this->activity($params);
116 // Default is to use method named as the hook
117 $this->$hookname($params);
122 final function register() {
125 $scm_list[] = $this->name;
128 function browserDisplayable($project) {
129 if ($project->usesSCM() && $project->usesPlugin($this->name)) {
130 if ($project->enableAnonSCM() || forge_check_perm('scm', $project->getID(), 'read')) {
137 abstract function createOrUpdateRepo($params);
139 function printShortStats($params) {
140 $project = $this->checkParams($params);
145 if ($project->usesPlugin($this->name)) {
146 echo ' ('.$this->text.')' ;
150 function getBlurb () {
151 return html_e('p', array(), _('Unimplemented SCM plugin.'));
154 function getInstructionsForAnon ($project) {
155 return html_e('p', array(), _('Instructions for anonymous access for unimplemented SCM plugin.'));
158 function getInstructionsForRW ($project) {
159 return html_e('p', array(), _('Instructions for read-write access for unimplemented SCM plugin.'));
162 function getSnapshotPara ($project) {
163 return html_e('p', array(), _('Instructions for snapshot access for unimplemented SCM plugin.'));
166 function getBrowserLinkBlock($project) {
168 $b = $HTML->boxMiddle(_('Repository Browser'));
169 $b .= html_e('p', array(), _('Browsing the SCM tree is not yet implemented for this SCM plugin.'));
170 $b .= html_e('p', array(), '['.util_make_link('/scm/?group_id='.$project->getID(), _('Not implemented yet')).']');
174 function getBrowserBlock($project) {
176 $b = $HTML->boxMiddle(_('Repository Browser'));
177 $b .= html_e('p', array(), _('Browsing the SCM tree is not yet implemented for this SCM plugin.'));
181 function getStatsBlock($project) {
183 $b = $HTML->boxMiddle(_('Repository Statistics'));
184 $b .= html_e('p', array(), _('Not implemented for this SCM plugin yet.'));
188 function printPage($params) {
191 $project = $this->checkParams($params);
196 if ($project->usesPlugin($this->name)) {
198 session_require_perm('scm', $project->getID(), 'read');
199 // Table for summary info
200 echo $HTML->listTableTop();
202 $cellContent = $this->getBlurb();
204 // Instructions for anonymous access
205 if ($project->enableAnonSCM()) {
206 $cellContent .= $this->getInstructionsForAnon($project);
209 // Instructions for developer access
210 $cellContent .= $this->getInstructionsForRW($project);
212 if ($this->browserDisplayable($project)) {
213 $cellContent .= $this->getBrowserLinkBlock($project);
217 if ($this->browserDisplayable($project)) {
218 $cellContent .= $this->getSnapshotPara($project);
220 $cells[] = array($cellContent, 'style' => 'width:65%');
223 $cellContent = $HTML->boxTop(_('Repository History'));
224 $cellContent .= _('Data about current and past states of the repository.');
225 if ($this->browserDisplayable($project)) {
226 $cellContent .= $this->getStatsBlock($project);
229 $cellContent .= $HTML->boxBottom();
230 $cells[] = array($cellContent, 'style' => 'width:35%', 'class' => 'top');
231 echo $HTML->multiTableRow(array('class' => 'top'), $cells);
232 echo $HTML->listTableBottom();
236 function printBrowserPage($params) {
237 $project = $this->checkParams($params);
242 if ($project->usesPlugin ($this->name)) {
243 if ($this->browserDisplayable ($project)) {
244 // print '<iframe src="'.util_make_url('/scm/browser.php?title='.$group->getUnixName()).'" frameborder="0" width=100% height=700></iframe>' ;
249 function printAdminPage($params) {
250 $group = group_get_object($params['group_id']);
251 $ra = RoleAnonymous::getInstance() ;
253 if ( $group->usesPlugin ( $this->name ) && $ra->hasPermission('project_read', $group->getID())) {
254 $inputAttr = array('type' => 'checkbox', 'name' => 'scm_enable_anonymous', 'value' => 1);
255 if ($group->enableAnonSCM()) {
256 $inputAttr['checked'] = 'checked';
258 echo html_e('p', array(), html_e('input', $inputAttr).html_e('strong', array(), _('Enable Anonymous Read Access')));
262 function adminUpdate($params) {
263 $project = $this->checkParams($params);
268 if ($project->usesPlugin($this->name) ) {
269 if (isset($params['scm_enable_anonymous']) && $params['scm_enable_anonymous']) {
270 $project->SetUsesAnonSCM(true);
272 $project->SetUsesAnonSCM(false);
277 function getBoxForProject($project) {
278 $box = $project->getSCMBox();
280 $box = forge_get_config('default_server', $this->name);
283 $box = forge_get_config('web_host');
288 function scm_delete_repo(&$params) {
289 $project = $this->checkParams($params);
293 if (! $project->usesPlugin ($this->name)) {
297 if (!isset($params['repo_name'])) {
301 $result = db_query_params('SELECT count(*) AS count FROM scm_secondary_repos WHERE group_id=$1 AND repo_name = $2 AND plugin_id=$3',
302 array ($params['group_id'],
303 $params['repo_name'],
306 $params['error_msg'] = db_error();
309 if (db_result($result, 0, 'count') == 0) {
310 $params['error_msg'] = sprintf(_('No repository %s exists'), $params['repo_name']);
314 $result = db_query_params ('UPDATE scm_secondary_repos SET next_action = $1 WHERE group_id=$2 AND repo_name=$3 AND plugin_id=$4',
315 array (SCM_EXTRA_REPO_ACTION_DELETE,
317 $params['repo_name'],
320 $params['error_msg'] = db_error();
324 plugin_hook ("scm_admin_update", $params);
328 function checkParams ($params) {
329 $group_id = $params['group_id'] ;
330 $project = group_get_object($group_id);
331 if (!$project || !is_object($project)) {
333 } elseif ($project->isError()) {
343 // c-file-style: "bsd"