bugfree1.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /**
  3. * The model file of bugfree version 1 convert of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Chunsheng Wang <chunsheng@cnezsoft.com>
  8. * @package convert
  9. * @version $Id: bugfree1.php 5028 2013-07-06 02:59:41Z wyd621@gmail.com $
  10. * @link https://www.zentao.net
  11. */
  12. class bugfree1ConvertModel extends bugfreeConvertModel
  13. {
  14. /**
  15. * Execute the convert.
  16. *
  17. * @access public
  18. * @return array
  19. */
  20. public function execute()
  21. {
  22. $this->clear();
  23. $this->convertGroup();
  24. $result['users'] = $this->convertUser();
  25. $result['executions'] = $this->convertExecution();
  26. $result['modules'] = $this->convertModule();
  27. $result['bugs'] = $this->convertBug();
  28. $result['actions'] = $this->convertAction();
  29. $result['files'] = $this->convertFile();
  30. $this->dao->dbh($this->dbh);
  31. $this->loadModel('tree')->fixModulePath();
  32. return $result;
  33. }
  34. /**
  35. * Convert groups.
  36. *
  37. * @access public
  38. * @return void
  39. */
  40. public function convertGroup()
  41. {
  42. $groups = $this->dao->dbh($this->sourceDBH)
  43. ->select("groupID AS id, groupName AS name, groupUser AS users")
  44. ->from('BugGroup')
  45. ->fetchAll('id');
  46. foreach($groups as $groupID => $group)
  47. {
  48. /* Explode into array. */
  49. $groupUsers = explode(',', $group->users);
  50. unset($group->id);
  51. unset($group->users);
  52. /* Insert the group. */
  53. $this->dao->dbh($this->dbh)->insert(TABLE_GROUP)->data($group)->exec();
  54. $zentaoGroupID = $this->dao->lastInsertId();
  55. /* Insert account. */
  56. foreach($groupUsers as $account)
  57. {
  58. if(empty($account)) continue;
  59. $this->dao->dbh($this->dbh)->insert(TABLE_USERGROUP)->set('`group`')->eq($zentaoGroupID)->set('account')->eq($account)->exec();
  60. }
  61. }
  62. }
  63. /**
  64. * Convert user.
  65. *
  66. * @access public
  67. * @return int converted user count
  68. */
  69. public function convertUser()
  70. {
  71. /* Get users exist in the system. */
  72. $activeUsers = $this->dao
  73. ->dbh($this->sourceDBH)
  74. ->select("username AS account, userpassword AS password, realname, email")
  75. ->from('BugUser')
  76. ->orderBy('userID ASC')
  77. ->fetchAll('account');
  78. /* Get users in histories. */
  79. $allUsers = $this->dao->select("distinct(username) AS account")->from('BugHistory')->fetchPairs();
  80. /* Merge them. */
  81. foreach($allUsers as $key => $account)
  82. {
  83. if(isset($activeUsers[$account]))
  84. {
  85. $allUsers[$key] = $activeUsers[$account];
  86. }
  87. else
  88. {
  89. $allUsers[$key] = array('account' => $account, 'realname' => $account, 'deleted' => '1');
  90. }
  91. }
  92. foreach($activeUsers as $account => $user) if(!isset($allUsers[$account])) $allUsers[$account] = $user;
  93. /* Insert into zentao. */
  94. $convertCount = 0;
  95. foreach($allUsers as $account => $user)
  96. {
  97. if(!$this->dao->dbh($this->dbh)->findByAccount($account)->from(TABLE_USER)->fetch('account'))
  98. {
  99. $this->dao->dbh($this->dbh)->insert(TABLE_USER)->data($user)->exec();
  100. $convertCount ++;
  101. }
  102. else
  103. {
  104. self::$info['users'][] = sprintf($this->lang->convert->errorUserExists, $account);
  105. }
  106. }
  107. return $convertCount;
  108. }
  109. /**
  110. * Convert execution in bugfree to product in zentao.
  111. *
  112. * @access public
  113. * @return int converted execution count
  114. */
  115. public function convertExecution()
  116. {
  117. $executions = $this->dao->dbh($this->sourceDBH)->select("executionID AS id, executionName AS name")->from('Bugexecution')->fetchAll('id');
  118. foreach($executions as $executionID => $execution)
  119. {
  120. unset($execution->id);
  121. $this->dao->dbh($this->dbh)->insert(TABLE_PRODUCT)->data($execution)->exec();
  122. $this->map['product'][$executionID] = $this->dao->lastInsertID();
  123. }
  124. return count($executions);
  125. }
  126. /**
  127. * Convert modules.
  128. *
  129. * @access public
  130. * @return int converted modules count
  131. */
  132. public function convertModule()
  133. {
  134. $this->map['module'][0] = 0;
  135. $modules = $this->dao
  136. ->dbh($this->sourceDBH)
  137. ->select(
  138. 'moduleID AS id,
  139. executionID AS root,
  140. moduleName AS name,
  141. moduleGrade AS grade,
  142. parentID AS parent,
  143. "bug" AS type')
  144. ->from('BugModule')
  145. ->orderBy('id ASC')
  146. ->fetchAll('id');
  147. foreach($modules as $moduleID => $module)
  148. {
  149. $module->root = $this->map['product'][$module->root];
  150. unset($module->id);
  151. $this->dao->dbh($this->dbh)->insert(TABLE_MODULE)->data($module)->exec();
  152. $this->map['module'][$moduleID] = $this->dao->lastInsertID();
  153. }
  154. /* Update parents. */
  155. foreach($modules as $oldModuleID => $module)
  156. {
  157. $newModuleID = $this->map['module'][$oldModuleID];
  158. $newParentID = $this->map['module'][$module->parent];
  159. $this->dao->dbh($this->dbh)->update(TABLE_MODULE)->set('parent')->eq($newParentID)->where('id')->eq($newModuleID)->exec();
  160. }
  161. return count($modules);
  162. }
  163. /**
  164. * Convert bugs.
  165. *
  166. * @access public
  167. * @return int converted bugs count.
  168. */
  169. public function convertBug()
  170. {
  171. $bugs = $this->dao
  172. ->dbh($this->sourceDBH)
  173. ->select('
  174. bugID AS id,
  175. executionID AS product,
  176. moduleID AS module,
  177. bugTitle AS title,
  178. bugSeverity AS severity,
  179. bugType AS type,
  180. bugOS AS os,
  181. bugStatus AS status,
  182. mailto,
  183. openedBy, openedDate, openedBuild,
  184. assignedTo, assignedDate,
  185. resolvedBy, resolution, resolvedBuild, resolvedDate,
  186. closedBy, closedDate,
  187. lastEditedBy, lastEditedDate,
  188. linkID as duplicateBug
  189. ')
  190. ->from('BugInfo')
  191. ->orderBy('bugID')
  192. ->fetchAll('id');
  193. foreach($bugs as $bugID => $bug)
  194. {
  195. /* Adjust some fields of bug. */
  196. $bugID = (int)$bugID;
  197. unset($bug->id);
  198. if($bug->assignedTo == 'Closed') $bug->assignedTo = 'closed';
  199. $bug->type = strtolower($bug->type);
  200. $bug->os = strtolower($bug->os);
  201. $bug->browser = 'all';
  202. $bug->resolution = str_replace(' ','', strtolower($bug->resolution));
  203. $bug->product = $this->map['product'][$bug->product];
  204. $bug->module = $this->map['module'][$bug->module];
  205. $this->dao->dbh($this->dbh)->insert(TABLE_BUG)->data($bug)->exec();
  206. $this->map['bug'][$bugID] = $this->dao->lastInsertID();
  207. }
  208. /* Update duplicated bugs. */
  209. foreach($this->map['bug'] as $oldBugID => $newBugID)
  210. {
  211. $this->dao->dbh($this->dbh)->update(TABLE_BUG)->set('duplicateBug')->eq($newBugID)->where('duplicateBug')->eq($oldBugID)->exec();
  212. }
  213. return count($bugs);
  214. }
  215. /**
  216. * Convert actions.
  217. *
  218. * @access public
  219. * @return int converted actions count.
  220. */
  221. public function convertAction()
  222. {
  223. $actions = $this->dao
  224. ->dbh($this->sourceDBH)
  225. ->select("
  226. 'bug' AS objectType,
  227. bugID AS objectID,
  228. userName AS actor,
  229. action,
  230. fullInfo AS comment,
  231. actionDate AS date")
  232. ->from('BugHistory')
  233. ->orderBy('bugID, historyID')
  234. ->fetchGroup('objectID');
  235. $convertCount = 0;
  236. foreach($actions as $bugID => $bugActions)
  237. {
  238. /* Get the related bugID. */
  239. $bugID = (int)$bugID;
  240. $zentaoBugID = $this->map['bug'][$bugID];
  241. /* Process actions. */
  242. foreach($bugActions as $key => $action)
  243. {
  244. $action->objectID = $zentaoBugID;
  245. if($key == 0)
  246. {
  247. $this->dao->dbh($this->dbh)->update(TABLE_BUG)->set('steps')->eq(nl2br($action->comment))->where('id')->eq($zentaoBugID)->exec();
  248. $action->comment = '';
  249. }
  250. $this->dao->dbh($this->dbh)->insert(TABLE_ACTION)->data($action)->exec();
  251. $convertCount ++;
  252. }
  253. }
  254. return $convertCount;
  255. }
  256. /**
  257. * Convert files.
  258. *
  259. * @access public
  260. * @return int converted files count.
  261. */
  262. public function convertFile()
  263. {
  264. $this->setPath();
  265. $files = $this->dao->dbh($this->sourceDBH)
  266. ->select("
  267. fileName AS pathname,
  268. fileTitle AS title,
  269. fileType AS extension,
  270. fileSize AS size,
  271. 'bug' AS objectType,
  272. bugID AS objectID,
  273. addUser AS addedBy,
  274. addDate AS addedDate
  275. ")
  276. ->from('BugFile')
  277. ->orderBy('fileID')
  278. ->fetchAll();
  279. foreach($files as $file)
  280. {
  281. $file->objectID = $this->map['bug'][(int)$file->objectID];
  282. if(strpos($file->size, 'KB')) $file->size = (int)(str_replace('KB', '', $file->size) * 1024);
  283. if(strpos($file->size, 'MB')) $file->size = (int)(str_replace('MB', '', $file->size) * 1024 * 1024);
  284. $this->dao->dbh($this->dbh)->insert(TABLE_FILE)->data($file)->exec();
  285. /* Copy files. */
  286. $soureFile = $this->filePath . $file->pathname;
  287. if(!file_exists($soureFile))
  288. {
  289. self::$info['files'][] = sprintf($this->lang->convert->errorFileNotExits, $soureFile);
  290. continue;
  291. }
  292. $targetFile = $this->app->getAppRoot() . "www/data/upload/{$this->app->company->id}/" . $file->pathname;
  293. $targetPath = dirname($targetFile);
  294. if(!is_dir($targetPath)) mkdir($targetPath, 0777, true);
  295. if(!copy($soureFile, $targetFile))
  296. {
  297. self::$info['files'][] = sprintf($this->lang->convert->errorCopyFailed, $targetFile);
  298. }
  299. }
  300. return count($files);
  301. }
  302. }