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>
8 * This file is part of GForge-plugin-cvstracker
10 * GForge-plugin-cvstracker is free software; you can redistribute it
11 * and/or modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * GForge-plugin-cvstracker is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY; without even the implied
17 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 * See the GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with GForge-plugin-cvstracker; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 US
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";
45 * It display a table with commit related to this tracker or task_extra_detail
47 * @param string $Query Query to be executed to get the commit entries.
48 * @param integer $group_id Group_id of the actual Group_id
51 function getCommitEntries($DBResult,$group_id) {
52 $group = &group_get_object($group_id);
54 if (!$group->usesPlugin($this->name)) {
58 $Rows= db_numrows($DBResult);
61 echo '<tr><td colspan="2">';
62 echo '<h4>'._('Links to related CVS commits').':</h4>';
64 $title_arr=$this->getTitleArr();
65 echo $GLOBALS['HTML']->listTableTop ($title_arr);
67 for ($i=0; $i<$Rows; $i++) {
68 $Row = db_fetch_array($DBResult);
69 echo '<tr '. $GLOBALS['HTML']->boxGetAltRowStyle($i) .'>'.
70 '<td>'. $this->getFileLink($group->getUnixName(),
71 $Row['file'],$Row['actual_version']). '</td>'.
72 '<td>'. date(_('Y-m-d'), $Row['cvs_date']).'</td>'.
73 '<td>'. $this->getDiffLink($group->getUnixName(),
76 $Row['actual_version']).'</td>'.
77 '<td>'. $this->getActualVersionLink($group->getUnixName(),
78 $Row['file'], $Row['actual_version']).
80 <td>'. htmlspecialchars($Row['log_text']).'</td>
81 <td>'. util_make_link_u ($Row['author'],
82 user_get_object_by_name ($Row['author'])->getId(),
83 $Row['author']).'</td>
86 echo $GLOBALS['HTML']->listTableBottom();
89 echo '<h4>'._('No commits have been made.').'</h4>';
94 * Return an array with titles of Box to display the entries
96 * @return Array The array containing the titles
99 function getTitleArr() {
101 $title_arr[]=_('Filename');
102 $title_arr[]=_('Date');
103 $title_arr[]=_('Previous Version');
104 $title_arr[]=_('Current Version');
105 $title_arr[]=_('Log Message');
106 $title_arr[]=_('Author');
111 * Return a link to the File in cvsweb
113 * @param String $GroupName is the Name of the project
114 * @param String $FileName is the FileName ( with path )
116 * @return String The string containing a link to the File in the cvsweb
119 function getFileLink($GroupName, $FileName, $LatestRevision) {
120 return util_make_link ('/scm/viewvc.php/'.$FileName .
121 '?root='.$GroupName.'&view=log',
126 * Return a link to the File in cvsweb in the specified Version
128 * @param String $GroupName is the Name of the project
129 * @param String $FileName is the FileName ( with path )
130 * @param String $Version the version to retrieve
132 * @return String The string containing a link to the File in the cvsweb
135 function getActualVersionLink($GroupName, $FileName, $Version) {
136 return util_make_link ('/scm/viewvc.php/'.$FileName .
137 '?root='.$GroupName.'&pathrev='.$Version .
143 * Return a link to the diff between two versions of a File in cvsweb
145 * @param String $GroupName is the Name of the project
146 * @param String $FileName is the FileName ( with path )
147 * @param String $PrevVersion First version to retrieve
148 * @param String $ActualVersion Second version to retrieve
150 * @return String The string containing a link to the File in the cvsweb
153 function getDiffLink($GroupName, $FileName, $PrevVersion, $ActualVersion) {
154 return util_make_link ('/scm/viewvc.php/'.$FileName .
155 '?root='.$GroupName.'&r1='.$PrevVersion .
156 '&r2='.$ActualVersion,
157 _('Diff To').' '.$PrevVersion);
162 * Function to add cvstracker lines to a loginfo file
164 * @param string $path The filename of loginfo
167 function addCvsTrackerToFile($group, $path) {
168 global $cvs_binary_version;
170 $FOut = fopen($path, "a");
172 fwrite($FOut, "# BEGIN added by gforge-plugin-cvstracker\n");
173 if ( $cvs_binary_version == "1.12" ) {
174 $Line = "ALL ( php -q -d include_path=".ini_get('include_path').
175 " ".forge_get_config('plugins_path')."/cvstracker/bin/post.php
178 if ( $cvs_binary_version == "1.11") {
179 $Line = "ALL ( php -q -d include_path=".ini_get('include_path').
180 " ".forge_get_config('plugins_path')."/cvstracker/bin/post.php
181 ".$group->getUnixName()." %{sVv} )\n";
184 fwrite($FOut, "# END added by gforge-plugin-cvstracker\n");
190 * Function to get cvstracker loginfo lines
193 * return array with the loginfo lines.
195 function getCvsTrackerLogInfoLines() {
196 global $cvs_binary_version;
198 $array[]="# BEGIN added by gforge-plugin-cvstracker\n";
199 if ( $cvs_binary_version == "1.11" ) {
200 $array[] = "ALL ( php -q -d include_path=".ini_get('include_path').
201 " ".forge_get_config('plugins_path')."/cvstracker/bin/post.php
202 ".$group->getUnixName()." %{sVv} )\n";
203 }else { //it's version 1.12
204 $array[] = "ALL ( php -q -d include_path=".ini_get('include_path').
205 " ".forge_get_config('plugins_path')."/cvstracker/bin/post.php
208 $array[]= "# END added by gforge-plugin-cvstracker\n";
214 * Retrieve a file into a temporary directory from a CVS server
216 * @param String $repos Repository Name
217 * @param String $file File Name
219 * return String the FileName in the working repository
221 function getCvsFile($repos,$file) {
222 $actual_dir = getcwd();
223 $tempdirname = tempnam("/tmp","cvstracker");
226 if (!unlink($tempdirname))
229 // Create the temporary directory and returns its name.
230 if (!mkdir($tempdirname))
233 if (!chdir($tempdirname))
235 system("cvs -d ".$repos." co ".$file);
238 return $tempdirname.$file;
242 * putCvsFile commit a file to the repository
244 * @param String $repos Repository
245 * @param String $file to commit
246 * @param String $message to commit
248 function putCvsFile($repos,$file,$message="Automatic updated by cvstracker") {
249 system("cvs -d ".$repos." ci -m \"".$message."\" ".$file);
254 * The function to be called for a Hook
256 * @param String $hookname The name of the hookname that has been happened
257 * @param String $params The params of the Hook
260 function CallHook ($hookname, $params) {
261 global $group_id, $G_SESSION, $HTML, $use_cvstrackerplugin,$aid ;
262 $use_cvstrackerplugin = getIntFromRequest('use_cvstrackerplugin');
263 if ($hookname == "groupisactivecheckbox") {
264 //Check if the group is active
265 $group = &group_get_object($group_id);
266 if ($group->usesPlugin('scmcvs')) {
269 echo ' <input type="checkbox" name="use_cvstrackerplugin" value="1" ';
270 // checked or unchecked?
271 if ( $group->usesPlugin ( $this->name ) ) {
277 echo "<strong>Use ".$this->text." Plugin</strong>";
281 } elseif ($hookname == "groupisactivecheckboxpost") {
282 $group = &group_get_object($group_id);
283 if ( $use_cvstrackerplugin == 1 ) {
284 $group->setPluginUse ( $this->name );
286 $group->setPluginUse ( $this->name, false );
288 } elseif ($hookname == "artifact_extra_detail") {
289 $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',
291 $this->getCommitEntries($DBResult, $group_id);
292 } elseif ($hookname == "task_extra_detail") {
293 $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',
294 array ($params['task_id'])) ;
295 $this->getCommitEntries($DBResult, $group_id);
296 } elseif ($hookname == "update_cvs_repository") {
297 $Group = group_get_object($params["group_id"]);
298 if ($Group->usesPlugin("cvstracker")) {
300 $FIn = fopen(getCvsFile( $params["file_name"],
301 "CVSROOT/loginfo"),"r");
303 while (!feof($FIn)) {
304 $Line = fgets ($FIn);
305 if(!preg_match("/^#/", $Line) &&
306 preg_match("/cvstracker/",$Line)) {
312 if($LineFound==FALSE) {
313 $newfile=getCvsFile($params["file_name"],
315 $this->addCvsTrackerToFile($group, $newfile);
316 $this->putCvsFile($params["file_name"],
320 } elseif ($hookname == "get_cvs_loginfo_lines") {
321 $group = &group_get_object($group_id);
322 $GLOBALS['loginfo_lines']=$this->getCvsTrackerLogInfoLines($group);
329 // c-file-style: "bsd"