PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/bugtracking/int_bugtracking.php

https://github.com/viglesiasce/testlink
PHP | 387 lines | 166 code | 34 blank | 187 comment | 20 complexity | 1f4d3a12571f7b5788f454ec5fd5bd32 MD5 | raw file
  1. <?php
  2. /**
  3. * TestLink Open Source Project - http://testlink.sourceforge.net/
  4. *
  5. * Filename $RCSfile: int_bugtracking.php,v $
  6. *
  7. * @version $Revision: 1.45 $
  8. * @modified $Date: 2010/08/23 18:02:39 $ $Author: franciscom $
  9. *
  10. * @author Andreas Morsing
  11. *
  12. * Baseclass for connection to additional bug tracking interfaces
  13. * TestLink uses bugzilla to check if displayed bugs resolved, verified,
  14. * and closed bugs. If they are it will strike through them
  15. *
  16. * For supporting a bug tracking system this class has to be extended
  17. * All bug tracking customization should be done in a sub class of this
  18. * class . for an example look at the bugzilla.cfg.php and mantis.cfg.php
  19. *
  20. *
  21. * rev:
  22. * 20100823 - franciscom - BUGID 3699
  23. * 20100814 - franciscom - BUGID 3681 - new BTS youtrack (www.jetbrains.com)
  24. * 20100616 - eloff - Show error message if bts config is broken
  25. * 20100311 - Julian - BUGID 3256, BUGID 3098
  26. * function checkBugID_existence() has to return true
  27. * in this parent class to be able to add bugs if
  28. * function has not been overloaded in child classes
  29. *
  30. * 20081217 - franciscom - BUGID 1939
  31. * removed global coupling, usign config_get()
  32. * 20081102 - franciscom - refactored to ease configuration
  33. * 20080207 - needles - added notation for Seapine's TestTrackPro
  34. * 20070505 - franciscom - TL_INTERFACE_BUGS -> $g_interface_bugs
  35. * 20070304 - franciscom - added new method checkBugID_existence()
  36. *
  37. *
  38. **/
  39. require_once(TL_ABS_PATH. "/lib/functions/database.class.php");
  40. // Add new bugtracking interfaces here
  41. // If user configures an interface not declared here, pages trying to use bts
  42. // will give error message
  43. $btslist = array('BUGZILLA','MANTIS','JIRA', 'JIRASOAP', 'TRACKPLUS',
  44. 'EVENTUM','TRAC','SEAPINE','REDMINE','GFORGE','FOGBUGZ','YOUTRACK');
  45. $bts = array_flip($btslist);
  46. //Set the bug tracking system Interface
  47. class bugtrackingInterface
  48. {
  49. //members to store the bugtracking information, these values are
  50. //set in the actual subclasses of this class
  51. var $dbHost = null;
  52. var $dbName = null;
  53. var $dbUser = null;
  54. var $dbPass = null;
  55. var $dbType = null;
  56. var $showBugURL = null;
  57. var $enterBugURL = null;
  58. var $dbCharSet = null;
  59. var $tlCharSet = null;
  60. //private vars don't touch
  61. var $dbConnection = null;
  62. var $Connected = false;
  63. /*
  64. *
  65. * FUNCTIONS NOT CALLED BY TestLink (helpers):
  66. *
  67. **/
  68. /**
  69. * Constructor of bugtrackingInterface
  70. * put special initialization in here
  71. *
  72. * @version 1.0
  73. * @author Andreas Morsing
  74. * @since 22.04.2005, 21:03:32
  75. **/
  76. function bugtrackingInterface()
  77. {
  78. $this->tlCharSet = config_get('charset');
  79. if (defined('BUG_TRACK_DB_CHARSET'))
  80. {
  81. $this->dbCharSet = BUG_TRACK_DB_CHARSET;
  82. }
  83. else
  84. {
  85. $this->dbCharSet = $this->tlCharSet;
  86. }
  87. }
  88. /**
  89. * this function establishes the database connection to the
  90. * bugtracking system
  91. *
  92. * @return bool returns true if the db connection was established and the
  93. * db could be selected, false else
  94. *
  95. * @version 1.0
  96. * @author Francisco Mancardi
  97. * @since 14.09.2006
  98. *
  99. * @version 1.0
  100. * @author Andreas Morsing
  101. * @since 22.04.2005, 21:05:25
  102. **/
  103. function connect()
  104. {
  105. if (is_null($this->dbHost) || is_null($this->dbUser))
  106. {
  107. return false;
  108. }
  109. $this->dbConnection = new database($this->dbType);
  110. $result = $this->dbConnection->connect(false, $this->dbHost,$this->dbUser,$this->dbPass, $this->dbName);
  111. if (!$result['status'])
  112. {
  113. $this->dbConnection = null;
  114. $bts_type = config_get('interface_bugs');
  115. $connection_args = "(interface: $bts_type - Host:$this->dbHost - DBName: $this->dbName - User: $this->dbUser) ";
  116. $msg = sprintf(lang_get('BTS_connect_to_database_fails'),$connection_args);
  117. tLog($msg . $result['dbms_msg'], 'ERROR');
  118. }
  119. elseif (BUG_TRACK_DB_TYPE == 'mysql')
  120. {
  121. if ($this->dbCharSet == 'UTF-8')
  122. {
  123. $r = $this->dbConnection->exec_query("SET CHARACTER SET utf8");
  124. $r = $this->dbConnection->exec_query("SET NAMES utf8");
  125. $r = $this->dbConnection->exec_query("SET collation_connection = 'utf8_general_ci'");
  126. }
  127. else
  128. {
  129. $r = $this->dbConnection->exec_query("SET CHARACTER SET ".$this->dbCharSet);
  130. $r = $this->dbConnection->exec_query("SET NAMES ".$this->dbCharSet);
  131. }
  132. }
  133. $this->Connected = $result['status'] ? 1 : 0;
  134. return $this->Connected;
  135. }
  136. /**
  137. * this function simply returns the state of the db connection
  138. *
  139. * @return bool returns true if the db connection is established, false else
  140. *
  141. * @version 1.0
  142. * @author Andreas Morsing
  143. * @since 22.04.2005, 21:05:25
  144. **/
  145. function isConnected()
  146. {
  147. return ($this->Connected && is_object($this->dbConnection)) ? 1 : 0;
  148. }
  149. /**
  150. * this function closes the db connection (if any)
  151. *
  152. * @version 1.0
  153. * @author Andreas Morsing
  154. * @since 22.04.2005, 21:05:25
  155. **/
  156. function disconnect()
  157. {
  158. if (isConnected())
  159. {
  160. $this->dbConnection->close();
  161. }
  162. $this->Connected = false;
  163. $this->dbConnection = null;
  164. }
  165. /**
  166. * overload this to return the URL to the bugtracking page for viewing
  167. * the bug with the given id. This function is not directly called by
  168. * TestLink at the moment
  169. *
  170. * @param int id the bug id
  171. *
  172. * @return string returns a complete URL to view the given bug, or false if the bug
  173. * wasnt found
  174. *
  175. * @version 1.0
  176. * @author Andreas Morsing
  177. * @since 22.04.2005, 21:05:25
  178. **/
  179. function buildViewBugURL($id)
  180. {
  181. return false;
  182. }
  183. /**
  184. * overload this to return the status of the bug with the given id
  185. * this function is not directly called by TestLink.
  186. *
  187. * @param int id the bug id
  188. *
  189. * @return any returns the status of the given bug, or false if the bug
  190. * was not found
  191. * @version 1.0
  192. * @author Andreas Morsing
  193. * @since 22.04.2005, 21:05:25
  194. **/
  195. function getBugStatus($id)
  196. {
  197. return false;
  198. }
  199. /**
  200. * overload this to return the status in a readable form for the bug with the given id
  201. * This function is not directly called by TestLink
  202. *
  203. * @param int id the bug id
  204. *
  205. * @return any returns the status (in a readable form) of the given bug, or false
  206. * if the bug is not found
  207. *
  208. * @version 1.0
  209. * @author Andreas Morsing
  210. * @since 22.04.2005, 21:05:25
  211. **/
  212. function getBugStatusString($id)
  213. {
  214. return false;
  215. }
  216. /*
  217. *
  218. * FUNCTIONS CALLED BY TestLink:
  219. *
  220. **/
  221. /**
  222. * default implementation for fetching the bug summary from the
  223. * bugtracking system
  224. *
  225. * @param int id the bug id
  226. *
  227. * @return string returns the bug summary (if bug is found), or false
  228. *
  229. * @version 1.0
  230. * @author Andreas Morsing
  231. * @since 22.04.2005, 21:05:25
  232. **/
  233. function getBugSummaryString($id)
  234. {
  235. return false;
  236. }
  237. /**
  238. * simply returns the URL which should be displayed for entering bugs
  239. *
  240. * @return string returns a complete URL
  241. *
  242. * @version 1.0
  243. * @author Andreas Morsing
  244. * @since 25.08.2005, 21:05:25
  245. **/
  246. function getEnterBugURL()
  247. {
  248. return $this->enterBugURL;
  249. }
  250. /**
  251. * checks a bug id for validity, that means numeric only
  252. *
  253. * @return bool returns true if the bugid has the right format, false else
  254. **/
  255. function checkBugID($id)
  256. {
  257. $valid = true;
  258. $forbidden_chars = '/\D/i';
  259. if (preg_match($forbidden_chars, $id))
  260. {
  261. $valid = false;
  262. }
  263. else
  264. {
  265. $valid = (intval($id) > 0);
  266. }
  267. return $valid;
  268. }
  269. /**
  270. * return the maximum length in chars of a bug id
  271. * @return int the maximum length of a bugID
  272. */
  273. function getBugIDMaxLength()
  274. {
  275. return 16;
  276. }
  277. /**
  278. * default implementation for generating a link to the bugtracking page for viewing
  279. * the bug with the given id in a new page
  280. *
  281. * @param int id the bug id
  282. *
  283. * @return string returns a complete URL to view the bug (if found in db)
  284. *
  285. * @version 1.1
  286. * @author Andreas Morsing
  287. * @author Raphael Bosshard
  288. * @author Arjen van Summeren
  289. * @since 28.09.2005, 16:02:25
  290. **/
  291. function buildViewBugLink($bugID,$bWithSummary = false)
  292. {
  293. $link = "<a href='" .$this->buildViewBugURL($bugID) . "' target='_blank'>";
  294. $status = $this->getBugStatusString($bugID);
  295. if (!is_null($status))
  296. {
  297. $status = iconv($this->dbCharSet,$this->tlCharSet,$status);
  298. $link .= $status;
  299. }
  300. else
  301. $link .= $bugID;
  302. if ($bWithSummary)
  303. {
  304. $summary = $this->getBugSummaryString($bugID);
  305. if (!is_null($summary))
  306. {
  307. $summary = iconv($this->dbCharSet,$this->tlCharSet,$summary);
  308. $link .= " : " . $summary;
  309. }
  310. }
  311. $link .= "</a>";
  312. return $link;
  313. }
  314. /**
  315. * checks if bug id is present on BTS
  316. * Function has to be overloaded on child classes
  317. *
  318. * @return bool
  319. **/
  320. function checkBugID_existence($id)
  321. {
  322. // BUGID 3256, BUGID 3098
  323. return true;
  324. }
  325. }
  326. // -----------------------------------------------------------------------------------
  327. // DONT TOUCH ANYTHING BELOW THIS NOTICE!
  328. // -----------------------------------------------------------------------------------
  329. $g_bugInterfaceOn = false;
  330. $g_bugInterface = null;
  331. $bts_type = config_get('interface_bugs');
  332. if (isset($bts[$bts_type]))
  333. {
  334. $btsname = strtolower($bts_type);
  335. $configPHP = $btsname . '.cfg.php';
  336. $interfacePHP = 'int_' . $btsname . '.php';
  337. require_once(TL_ABS_PATH . 'cfg/'. $configPHP);
  338. require_once(TL_ABS_PATH . 'lib/bugtracking/'. $interfacePHP);
  339. $g_bugInterfaceName = BUG_INTERFACE_CLASSNAME;
  340. $g_bugInterface = new $g_bugInterfaceName();
  341. if ($g_bugInterface)
  342. {
  343. $g_bugInterface->connect();
  344. }
  345. // Important: connect() do log if something fails
  346. $g_bugInterfaceOn = ($g_bugInterface && $g_bugInterface->isConnected());
  347. }
  348. else if ($bts_type != 'NO') {
  349. $errorMsg = sprintf(lang_get('BTS_integration_failure'),$bts_type);
  350. tLog($errorMsg, 'ERROR');
  351. die($errorMsg);
  352. }
  353. ?>