3 * GForge Plugin CVSTracker Class
5 * Copyright 2004 (c) Francisco Gimeno <kikov @nospam@ kikov.org>
6 * Copyright 2005 (c) Guillaume Smet <guillaume-gforge@smet.org>
7 * Copyright (C) 2010 Alain Peyrat - Alcatel-Lucent
9 * This file is part of GForge-plugin-cvstracker
11 * GForge-plugin-cvstracker is free software; you can redistribute it
12 * and/or modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * GForge-plugin-cvstracker is distributed in the hope that it will
17 * be useful, but WITHOUT ANY WARRANTY; without even the implied
18 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 * See the GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * The cvstrackerPlugin class. It implements the Hooks for the presentation
27 * of table in tracker and task in detailed mode.
31 class cvstrackerPlugin extends Plugin {
32 function cvstrackerPlugin () {
34 $this->name = "cvstracker" ;
35 $this->text = "CVS<->Tracker";
36 $this->hooks[] = "groupisactivecheckbox";
37 $this->hooks[] = "groupisactivecheckboxpost";
38 $this->hooks[] = "artifact_extra_detail";
39 $this->hooks[] = "task_extra_detail";
40 $this->hooks[] = "update_cvs_repository";
41 $this->hooks[] = "get_cvs_loginfo_lines";
44 function groupisactivecheckbox (&$params) {
45 $group = group_get_object($params['group']);
46 if ($group->usesPlugin('scmcvs')) {
47 parent::groupisactivecheckbox($params);
52 * It display a table with commit related to this tracker or task_extra_detail
54 * @param string $Query Query to be executed to get the commit entries.
55 * @param integer $group_id Group_id of the actual Group_id
58 function getCommitEntries($DBResult,$group_id) {
59 $group = group_get_object($group_id);
61 if (!$group->usesPlugin($this->name)) {
65 $Rows= db_numrows($DBResult);
68 echo '<tr><td colspan="2">';
69 echo '<h4>'._('Links to related CVS commits').':</h4>';
71 $title_arr=$this->getTitleArr();
72 echo $GLOBALS['HTML']->listTableTop ($title_arr);
74 for ($i=0; $i<$Rows; $i++) {
75 $Row = db_fetch_array($DBResult);
76 echo '<tr '. $GLOBALS['HTML']->boxGetAltRowStyle($i) .'>'.
77 '<td>'. $this->getFileLink($group->getUnixName(),
78 $Row['file'],$Row['actual_version']). '</td>'.
79 '<td>'. date(_('Y-m-d'), $Row['cvs_date']).'</td>'.
80 '<td>'. $this->getDiffLink($group->getUnixName(),
83 $Row['actual_version']).'</td>'.
84 '<td>'. $this->getActualVersionLink($group->getUnixName(),
85 $Row['file'], $Row['actual_version']).
87 <td>'. htmlspecialchars($Row['log_text']).'</td>
88 <td>'. util_make_link_u ($Row['author'],
89 user_get_object_by_name ($Row['author'])->getId(),
90 $Row['author']).'</td>
93 echo $GLOBALS['HTML']->listTableBottom();
96 echo '<h4>'._('No commits have been made.').'</h4>';
101 * Return an array with titles of Box to display the entries
103 * @return Array The array containing the titles
106 function getTitleArr() {
108 $title_arr[]=_('File Name');
109 $title_arr[]=_('Date');
110 $title_arr[]=_('Previous Version');
111 $title_arr[]=_('Current Version');
112 $title_arr[]=_('Log Message');
113 $title_arr[]=_('Author');
118 * Return a link to the File in cvsweb
120 * @param String $GroupName is the Name of the project
121 * @param String $FileName is the FileName ( with path )
123 * @return String The string containing a link to the File in the cvsweb
126 function getFileLink($GroupName, $FileName, $LatestRevision) {
127 return util_make_link ('/scm/viewvc.php/'.$FileName .
128 '?root='.$GroupName.'&view=log',
133 * Return a link to the File in cvsweb in the specified Version
135 * @param String $GroupName is the Name of the project
136 * @param String $FileName is the FileName ( with path )
137 * @param String $Version the version to retrieve
139 * @return String The string containing a link to the File in the cvsweb
142 function getActualVersionLink($GroupName, $FileName, $Version) {
143 return util_make_link ('/scm/viewvc.php/'.$FileName .
144 '?root='.$GroupName.'&pathrev='.$Version .
150 * Return a link to the diff between two versions of a File in cvsweb
152 * @param String $GroupName is the Name of the project
153 * @param String $FileName is the FileName ( with path )
154 * @param String $PrevVersion First version to retrieve
155 * @param String $ActualVersion Second version to retrieve
157 * @return String The string containing a link to the File in the cvsweb
160 function getDiffLink($GroupName, $FileName, $PrevVersion, $ActualVersion) {
161 return util_make_link ('/scm/viewvc.php/'.$FileName .
162 '?root='.$GroupName.'&r1='.$PrevVersion .
163 '&r2='.$ActualVersion,
164 _('Diff To').' '.$PrevVersion);
168 * Function to add cvstracker lines to a loginfo file
170 * @param string $path The filename of loginfo
173 function addCvsTrackerToFile($group, $path) {
174 global $cvs_binary_version;
176 $FOut = fopen($path, "a");
178 fwrite($FOut, "# BEGIN added by gforge-plugin-cvstracker\n");
179 if ( $cvs_binary_version == "1.12" ) {
180 $Line = "ALL ( php -q -d include_path=".ini_get('include_path').
181 " ".forge_get_config('plugins_path')."/cvstracker/bin/post.php
184 if ( $cvs_binary_version == "1.11") {
185 $Line = "ALL ( php -q -d include_path=".ini_get('include_path').
186 " ".forge_get_config('plugins_path')."/cvstracker/bin/post.php
187 ".$group->getUnixName()." %{sVv} )\n";
190 fwrite($FOut, "# END added by gforge-plugin-cvstracker\n");
196 * Function to get cvstracker loginfo lines
199 * return array with the loginfo lines.
201 function getCvsTrackerLogInfoLines() {
202 global $cvs_binary_version;
204 $array[]="# BEGIN added by gforge-plugin-cvstracker\n";
205 if ( $cvs_binary_version == "1.11" ) {
206 $array[] = "ALL ( php -q -d include_path=".ini_get('include_path').
207 " ".forge_get_config('plugins_path')."/cvstracker/bin/post.php
208 ".$group->getUnixName()." %{sVv} )\n";
209 }else { //it's version 1.12
210 $array[] = "ALL ( php -q -d include_path=".ini_get('include_path').
211 " ".forge_get_config('plugins_path')."/cvstracker/bin/post.php
214 $array[]= "# END added by gforge-plugin-cvstracker\n";
220 * Retrieve a file into a temporary directory from a CVS server
222 * @param String $repos Repository Name
223 * @param String $file File Name
225 * return String the FileName in the working repository
227 function getCvsFile($repos,$file) {
228 $actual_dir = getcwd();
229 $tempdirname = tempnam("/tmp","cvstracker");
232 if (!unlink($tempdirname))
235 // Create the temporary directory and returns its name.
236 if (!mkdir($tempdirname))
239 if (!chdir($tempdirname))
241 system("cvs -d ".$repos." co ".$file);
244 return $tempdirname.$file;
248 * putCvsFile commit a file to the repository
250 * @param String $repos Repository
251 * @param String $file to commit
252 * @param String $message to commit
254 function putCvsFile($repos,$file,$message="Automatic updated by cvstracker") {
255 system("cvs -d ".$repos." ci -m \"".$message."\" ".$file);
260 * The function to be called for a Hook
262 * @param String $hookname The name of the hookname that has been happened
263 * @param String $params The params of the Hook
266 function CallHook ($hookname, &$params) {
267 global $group_id, $G_SESSION, $HTML, $aid ;
269 if ($hookname == "artifact_extra_detail") {
270 $DBResult = db_query_params ('SELECT * FROM plugin_cvstracker_data_master,plugin_cvstracker_data_artifact WHERE plugin_cvstracker_data_artifact.group_artifact_id=$1 AND plugin_cvstracker_data_master.holder_id=plugin_cvstracker_data_artifact.id ORDER BY cvs_date',
272 $this->getCommitEntries($DBResult, $group_id);
273 } elseif ($hookname == "task_extra_detail") {
274 $DBResult = db_query_params ('SELECT * FROM plugin_cvstracker_data_master,plugin_cvstracker_data_artifact WHERE plugin_cvstracker_data_artifact.project_task_id=$1 AND plugin_cvstracker_data_master.holder_id=plugin_cvstracker_data_artifact.id ORDER BY cvs_date',
275 array ($params['task_id'])) ;
276 $this->getCommitEntries($DBResult, $group_id);
277 } elseif ($hookname == "update_cvs_repository") {
278 $Group = group_get_object($params["group_id"]);
279 if ($Group->usesPlugin("cvstracker")) {
281 $FIn = fopen(getCvsFile( $params["file_name"],
282 "CVSROOT/loginfo"),"r");
284 while (!feof($FIn)) {
285 $Line = fgets ($FIn);
286 if(!preg_match("/^#/", $Line) &&
287 preg_match("/cvstracker/",$Line)) {
293 if($LineFound==FALSE) {
294 $newfile=getCvsFile($params["file_name"],
296 $this->addCvsTrackerToFile($group, $newfile);
297 $this->putCvsFile($params["file_name"],
301 } elseif ($hookname == "get_cvs_loginfo_lines") {
302 $group = group_get_object($group_id);
303 $GLOBALS['loginfo_lines']=$this->getCvsTrackerLogInfoLines($group);
310 // c-file-style: "bsd"