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[]=_('Filename');
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);
169 * Function to add cvstracker lines to a loginfo file
171 * @param string $path The filename of loginfo
174 function addCvsTrackerToFile($group, $path) {
175 global $cvs_binary_version;
177 $FOut = fopen($path, "a");
179 fwrite($FOut, "# BEGIN added by gforge-plugin-cvstracker\n");
180 if ( $cvs_binary_version == "1.12" ) {
181 $Line = "ALL ( php -q -d include_path=".ini_get('include_path').
182 " ".forge_get_config('plugins_path')."/cvstracker/bin/post.php
185 if ( $cvs_binary_version == "1.11") {
186 $Line = "ALL ( php -q -d include_path=".ini_get('include_path').
187 " ".forge_get_config('plugins_path')."/cvstracker/bin/post.php
188 ".$group->getUnixName()." %{sVv} )\n";
191 fwrite($FOut, "# END added by gforge-plugin-cvstracker\n");
197 * Function to get cvstracker loginfo lines
200 * return array with the loginfo lines.
202 function getCvsTrackerLogInfoLines() {
203 global $cvs_binary_version;
205 $array[]="# BEGIN added by gforge-plugin-cvstracker\n";
206 if ( $cvs_binary_version == "1.11" ) {
207 $array[] = "ALL ( php -q -d include_path=".ini_get('include_path').
208 " ".forge_get_config('plugins_path')."/cvstracker/bin/post.php
209 ".$group->getUnixName()." %{sVv} )\n";
210 }else { //it's version 1.12
211 $array[] = "ALL ( php -q -d include_path=".ini_get('include_path').
212 " ".forge_get_config('plugins_path')."/cvstracker/bin/post.php
215 $array[]= "# END added by gforge-plugin-cvstracker\n";
221 * Retrieve a file into a temporary directory from a CVS server
223 * @param String $repos Repository Name
224 * @param String $file File Name
226 * return String the FileName in the working repository
228 function getCvsFile($repos,$file) {
229 $actual_dir = getcwd();
230 $tempdirname = tempnam("/tmp","cvstracker");
233 if (!unlink($tempdirname))
236 // Create the temporary directory and returns its name.
237 if (!mkdir($tempdirname))
240 if (!chdir($tempdirname))
242 system("cvs -d ".$repos." co ".$file);
245 return $tempdirname.$file;
249 * putCvsFile commit a file to the repository
251 * @param String $repos Repository
252 * @param String $file to commit
253 * @param String $message to commit
255 function putCvsFile($repos,$file,$message="Automatic updated by cvstracker") {
256 system("cvs -d ".$repos." ci -m \"".$message."\" ".$file);
261 * The function to be called for a Hook
263 * @param String $hookname The name of the hookname that has been happened
264 * @param String $params The params of the Hook
267 function CallHook ($hookname, &$params) {
268 global $group_id, $G_SESSION, $HTML, $aid ;
270 if ($hookname == "artifact_extra_detail") {
271 $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',
273 $this->getCommitEntries($DBResult, $group_id);
274 } elseif ($hookname == "task_extra_detail") {
275 $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',
276 array ($params['task_id'])) ;
277 $this->getCommitEntries($DBResult, $group_id);
278 } elseif ($hookname == "update_cvs_repository") {
279 $Group = group_get_object($params["group_id"]);
280 if ($Group->usesPlugin("cvstracker")) {
282 $FIn = fopen(getCvsFile( $params["file_name"],
283 "CVSROOT/loginfo"),"r");
285 while (!feof($FIn)) {
286 $Line = fgets ($FIn);
287 if(!preg_match("/^#/", $Line) &&
288 preg_match("/cvstracker/",$Line)) {
294 if($LineFound==FALSE) {
295 $newfile=getCvsFile($params["file_name"],
297 $this->addCvsTrackerToFile($group, $newfile);
298 $this->putCvsFile($params["file_name"],
302 } elseif ($hookname == "get_cvs_loginfo_lines") {
303 $group = group_get_object($group_id);
304 $GLOBALS['loginfo_lines']=$this->getCvsTrackerLogInfoLines($group);
311 // c-file-style: "bsd"