bugfree2.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. <?php
  2. /**
  3. * The model file of bugfree2 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: bugfree2.php 5028 2013-07-06 02:59:41Z wyd621@gmail.com $
  10. * @link https://www.zentao.net
  11. */
  12. class bugfree2ConvertModel extends bugfreeConvertModel
  13. {
  14. /**
  15. * Execute the converter.
  16. *
  17. * @access public
  18. * @return array
  19. */
  20. public function execute()
  21. {
  22. $this->clear();
  23. $this->setTable();
  24. $this->convertGroup();
  25. $result['users'] = $this->convertUser();
  26. $result['executions'] = $this->convertExecution();
  27. $result['modules'] = $this->convertModule();
  28. $result['bugs'] = $this->convertBug();
  29. $result['cases'] = $this->convertCase();
  30. $result['results'] = $this->convertResult();
  31. $result['actions'] = $this->convertAction();
  32. $result['files'] = $this->convertFile();
  33. $this->dao->dbh($this->dbh);
  34. $this->loadModel('tree')->fixModulePath();
  35. return $result;
  36. }
  37. /**
  38. * Set table names.
  39. *
  40. * @access public
  41. * @return void
  42. */
  43. public function setTable()
  44. {
  45. $dbPrefix = $this->post->dbPrefix;
  46. define('BUGFREE_TABLE_OPTION', $dbPrefix . 'TestOptions');
  47. define('BUGFREE_TABLE_USER', $dbPrefix . 'TestUser');
  48. define('BUGFREE_TABLE_EXECUTION', $dbPrefix . 'TestExecution');
  49. define('BUGFREE_TABLE_MODULE', $dbPrefix . 'TestModule');
  50. define('BUGFREE_TABLE_BUGINFO', $dbPrefix . 'BugInfo');
  51. define('BUGFREE_TABLE_CASEINFO', $dbPrefix . 'CaseInfo');
  52. define('BUGFREE_TABLE_RESULTINFO', $dbPrefix . 'ResultInfo');
  53. define('BUGFREE_TABLE_ACTION', $dbPrefix . 'TestAction');
  54. define('BUGFREE_TABLE_FILE', $dbPrefix . 'TestFile');
  55. define('BUGFREE_TABLE_HISTORY', $dbPrefix . 'TestHistory');
  56. define('BUGFREE_TABLE_GROUP', $dbPrefix . 'TestGroup');
  57. }
  58. /**
  59. * Get the version of bugfree2.x.
  60. *
  61. * @access public
  62. * @return int
  63. */
  64. public function getBugFreeVersion()
  65. {
  66. return $this->dao->dbh($this->sourceDBH)
  67. ->select("optionValue as version")->from(BUGFREE_TABLE_OPTION)
  68. ->where('OptionName')->eq('dbVersion')
  69. ->fetch('version');
  70. }
  71. /**
  72. * Convert user.
  73. *
  74. * @access public
  75. * @return int converted user count
  76. */
  77. public function convertUser()
  78. {
  79. /* Get all user list. */
  80. $users = $this->dao
  81. ->dbh($this->sourceDBH)
  82. ->select("username AS account, userpassword AS password, realname, email, isDroped AS deleted")
  83. ->from(BUGFREE_TABLE_USER)
  84. ->orderBy('userID ASC')
  85. ->fetchAll('account');
  86. /* Insert into zentao. */
  87. $convertCount = 0;
  88. foreach($users as $account => $user)
  89. {
  90. if(!$this->dao->dbh($this->dbh)->findByAccount($account)->from(TABLE_USER)->fetch('account'))
  91. {
  92. $this->dao->dbh($this->dbh)->insert(TABLE_USER)->data($user)->exec();
  93. $convertCount ++;
  94. }
  95. else
  96. {
  97. self::$info['users'][] = sprintf($this->lang->convert->errorUserExists, $account);
  98. }
  99. }
  100. return $convertCount;
  101. }
  102. /**
  103. * Convert groups.
  104. *
  105. * @access public
  106. * @return void converted group count.
  107. */
  108. public function convertGroup()
  109. {
  110. if(!$this->tableExists(BUGFREE_TABLE_GROUP)) return false;
  111. $groups = $this->dao->dbh($this->sourceDBH)
  112. ->select("groupID AS id, groupName AS name, groupUser AS users")
  113. ->from(BUGFREE_TABLE_GROUP)
  114. ->fetchAll('id');
  115. foreach($groups as $groupID => $group)
  116. {
  117. /* Fix the group data. */
  118. if($group->name == '[All Users]') continue;
  119. $groupUsers = explode(',', $group->users);
  120. unset($group->id);
  121. unset($group->users);
  122. /* Insert into zentao's group table. */
  123. $this->dao->dbh($this->dbh)->insert(TABLE_GROUP)->data($group)->exec();
  124. $zentaoGroupID = $this->dao->lastInsertId();
  125. /* Insert into zentao's usergroup table. */
  126. foreach($groupUsers as $account)
  127. {
  128. if(empty($account)) continue;
  129. $this->dao->dbh($this->dbh)->insert(TABLE_USERGROUP)
  130. ->set('`group`')->eq($zentaoGroupID)
  131. ->set('account')->eq($account)
  132. ->exec();
  133. }
  134. }
  135. }
  136. /**
  137. * Convert executions.
  138. *
  139. * @access public
  140. * @return int converted executions count.
  141. */
  142. public function convertExecution()
  143. {
  144. $executions = $this->dao->dbh($this->sourceDBH)
  145. ->select("executionID AS id, executionName AS name, isDroped AS deleted")
  146. ->from(BUGFREE_TABLE_EXECUTION)
  147. ->fetchAll('id');
  148. foreach($executions as $executionID => $execution)
  149. {
  150. unset($execution->id);
  151. $this->dao->dbh($this->dbh)->insert(TABLE_PRODUCT)->data($execution)->exec();
  152. $this->map['product'][$executionID] = $this->dao->lastInsertID();
  153. }
  154. return count($executions);
  155. }
  156. /**
  157. * Convert modules.
  158. *
  159. * @access public
  160. * @return int converted modules count.
  161. */
  162. public function convertModule()
  163. {
  164. $this->map['module'][0] = 0;
  165. $modules = $this->dao
  166. ->dbh($this->sourceDBH)
  167. ->select(
  168. 'moduleID AS id,
  169. moduleType as type,
  170. executionID AS root,
  171. moduleName AS name,
  172. moduleGrade AS grade,
  173. parentID AS parent,
  174. displayOrder AS `order`')
  175. ->from(BUGFREE_TABLE_MODULE)
  176. ->orderBy('id ASC')
  177. ->fetchAll('id');
  178. foreach($modules as $moduleID => $module)
  179. {
  180. $module->root = $this->map['product'][$module->root];
  181. $module->type = strtolower($module->type);
  182. unset($module->id);
  183. $this->dao->dbh($this->dbh)->insert(TABLE_MODULE)->data($module)->exec();
  184. $this->map['module'][$moduleID] = $this->dao->lastInsertID();
  185. }
  186. /* Update parent. */
  187. foreach($modules as $oldModuleID => $module)
  188. {
  189. $newModuleID = $this->map['module'][$oldModuleID];
  190. $newParentID = $this->map['module'][$module->parent];
  191. $this->dao->dbh($this->dbh)->update(TABLE_MODULE)->set('parent')->eq($newParentID)->where('id')->eq($newModuleID)->exec();
  192. }
  193. return count($modules);
  194. }
  195. /**
  196. * Convert bugs.
  197. *
  198. * @access public
  199. * @return int converted bugs count.
  200. */
  201. public function convertBug()
  202. {
  203. $bugs = $this->dao
  204. ->dbh($this->sourceDBH)
  205. ->select('
  206. bugID AS id,
  207. executionID AS product,
  208. moduleID AS module,
  209. bugTitle AS title,
  210. bugSeverity AS severity,
  211. bugPriority AS pri,
  212. bugType AS type,
  213. bugOS AS os,
  214. bugBrowser AS browser,
  215. bugMachine AS hardware,
  216. howFound AS found,
  217. reproSteps AS steps,
  218. bugStatus AS status,
  219. linkID AS relatedBug,
  220. duplicateID AS duplicateBug,
  221. caseID AS `case`,
  222. 1 AS caseVersion,
  223. resultID AS result,
  224. mailto,
  225. openedBy, openedDate, openedBuild,
  226. assignedTo, assignedDate,
  227. resolvedBy, resolution, resolvedBuild, resolvedDate,
  228. closedBy, closedDate,
  229. lastEditedBy, lastEditedDate,
  230. bugKeyword AS keywords
  231. ')
  232. ->from(BUGFREE_TABLE_BUGINFO)
  233. ->where('isDroped')->eq(0)
  234. ->orderBy('bugID')
  235. ->fetchAll('id');
  236. foreach($bugs as $bugID => $bug)
  237. {
  238. /* Fix some fileds of bug. */
  239. $bugID = (int)$bugID;
  240. unset($bug->id);
  241. if($bug->assignedTo == 'Closed') $bug->assignedTo = 'closed';
  242. if($bug->assignedTo == 'Active') $bug->assignedTo = '';
  243. $bug->type = strtolower($bug->type);
  244. $bug->found = strtolower($bug->found);
  245. $bug->status = strtolower($bug->status);
  246. $bug->os = strtolower($bug->os);
  247. $bug->browser= strtolower($bug->browser);
  248. $bug->steps = nl2br($bug->steps);
  249. if($bug->os == 'winvista') $bug->os = 'vista';
  250. if($bug->browser == 'firefox3.0') $bug->browser = 'firefox3';
  251. if($bug->browser == 'firefox2.0') $bug->browser = 'firefox2';
  252. if($bug->openedBuild == 'N/A') $bug->openedBuild = '';
  253. if(!$bug->case) $bug->caseVersion = 0;
  254. $bug->resolution = str_replace(' ', '', strtolower($bug->resolution));
  255. $bug->product = $this->map['product'][$bug->product];
  256. $bug->module = $this->map['module'][$bug->module];
  257. $this->dao->dbh($this->dbh)->insert(TABLE_BUG)->data($bug)->exec();
  258. $this->map['bug'][$bugID] = $this->dao->lastInsertID();
  259. }
  260. /* Update duplicated bugs. */
  261. foreach($this->map['bug'] as $oldBugID => $newBugID)
  262. {
  263. $this->dao->dbh($this->dbh)->update(TABLE_BUG)->set('duplicateBug')->eq($newBugID)->where('duplicateBug')->eq($oldBugID)->exec();
  264. }
  265. return count($bugs);
  266. }
  267. /**
  268. * Convert cases.
  269. *
  270. * @access public
  271. * @return int converted cases count.
  272. */
  273. public function convertCase()
  274. {
  275. $cases = $this->dao
  276. ->dbh($this->sourceDBH)
  277. ->select('
  278. caseID AS id,
  279. executionID AS product,
  280. moduleID AS module,
  281. caseTitle AS title,
  282. caseSteps AS step,
  283. casePriority AS pri,
  284. caseType AS type,
  285. caseStatus AS status,
  286. caseMethod AS howRun,
  287. casePlan AS stage,
  288. openedBy, openedDate,
  289. lastEditedBy, lastEditedDate,
  290. scriptedBy, scriptedDate, scriptStatus, scriptLocation,
  291. linkID AS linkCase,
  292. casekeyword AS keywords,
  293. 1 AS version,
  294. bugID
  295. ')
  296. ->from(BUGFREE_TABLE_CASEINFO)
  297. ->where('isDroped')->eq(0)
  298. ->orderBy('caseID')
  299. ->fetchAll('id');
  300. foreach($cases as $caseID => $case)
  301. {
  302. /* Fix fields of case. */
  303. $caseID = (int)$caseID;
  304. $step = $case->step;
  305. $bugs = explode(',', $case->bugID);
  306. unset($case->id);
  307. unset($case->step);
  308. unset($case->bugID);
  309. $case->type = strtolower($case->type);
  310. $case->status = strtolower($case->status);
  311. $case->howRun = strtolower($case->howRun);
  312. $case->stage = strtolower($case->stage);
  313. if($case->type == 'configuration') $case->type = 'config';
  314. if($case->type == 'setup') $case->type = 'install';
  315. if($case->type == 'functional') $case->type = 'feature';
  316. if($case->status == 'active') $case->status = 'normal';
  317. /* Change product and module by zentao's product and module. */
  318. $case->product = $this->map['product'][$case->product];
  319. $case->module = $this->map['module'][$case->module];
  320. /* Insert into case table. */
  321. $this->dao->dbh($this->dbh)->insert(TABLE_CASE)->data($case)->exec();
  322. $zentaoCaseID = $this->dao->lastInsertID();
  323. $this->map['case'][$caseID] = $zentaoCaseID;
  324. /* Insert into case step table. */
  325. $caseStep->case = $zentaoCaseID;
  326. $caseStep->version = 1;
  327. $caseStep->desc = $step;
  328. $this->dao->dbh($this->dbh)->insert(TABLE_CASESTEP)->data($caseStep)->exec();
  329. /* Update related bugs. */
  330. foreach($bugs as $bugID)
  331. {
  332. if(!isset($this->map['bug'][$bugID])) continue;
  333. $zentaoBugID = $this->map['bug'][$bugID];
  334. $this->dao->dbh($this->dbh)->update(TABLE_BUG)->set('`case`')->eq($zentaoCaseID)->where('id')->eq($zentaoBugID)->limit(1)->exec();
  335. }
  336. }
  337. return count($cases);
  338. }
  339. /**
  340. * Convert results.
  341. *
  342. * @access public
  343. * @return int converted results count.
  344. */
  345. public function convertResult()
  346. {
  347. $results = $this->dao->dbh($this->sourceDBH)
  348. ->select('
  349. resultID AS id,
  350. caseID AS `case`,
  351. resultValue AS caseResult,
  352. 1 AS version,
  353. openedDate as date,
  354. bugID
  355. ')
  356. ->from(BUGFREE_TABLE_RESULTINFO)
  357. ->orderBy('id')
  358. ->fetchAll('id');
  359. foreach($results as $resultID => $result)
  360. {
  361. unset($result->id);
  362. /* The bug id of zentao. */
  363. $bugID = (int)$result->bugID;
  364. $zentaoBugID = $this->map['bug'][$bugID];
  365. unset($result->bugID);
  366. /* Insert into test result table. */
  367. $this->dao->dbh($this->dbh)->insert(TABLE_TESTRESULT)->data($result)->exec();
  368. $zentaoResultID = $this->dao->lastInsertId();
  369. $this->map['result'][$resultID] = $zentaoResultID;
  370. /* Update result table. */
  371. $this->dao->dbh($this->dbh)->update(TABLE_BUG)->set('result')->eq($zentaoResultID)->where('id')->eq($zentaoBugID)->limit(1)->exec();
  372. }
  373. return count($results);
  374. }
  375. /**
  376. * Convert actions.
  377. *
  378. * @access public
  379. * @return int converted actions count.
  380. */
  381. public function convertAction()
  382. {
  383. $actions = $this->dao
  384. ->dbh($this->sourceDBH)
  385. ->select("actionID AS id,
  386. actionTarget AS objectType,
  387. idValue AS objectID,
  388. actionUser AS actor,
  389. actionType AS action,
  390. actionDate AS date,
  391. actionNote AS comment
  392. ")
  393. ->from(BUGFREE_TABLE_ACTION)
  394. ->where('actionTarget' != 'Result')
  395. ->orderBy('actionID')
  396. ->fetchAll('id');
  397. foreach($actions as $actionID => $action)
  398. {
  399. $actionID = (int)$action->id;
  400. unset($action->id);
  401. $action->objectType = strtolower($action->objectType);
  402. $action->action = strtolower($action->action);
  403. $action->objectID = $this->map[$action->objectType][$action->objectID];
  404. $this->dao->dbh($this->dbh)->insert(TABLE_ACTION)->data($action)->exec();
  405. $this->map['action'][$actionID] = $this->dao->lastInsertID();
  406. }
  407. return count($actions);
  408. }
  409. /**
  410. * Convert histories.
  411. *
  412. * @access public
  413. * @return int the converted histories count.
  414. */
  415. public function convertHistory()
  416. {
  417. $histories = $this->dao->dbh($this->sourceDBH)
  418. ->select('actioID, actionField AS field, oldValue AS old, newValue AS new')
  419. ->from(BUGFREE_TABLE_HISTORY)
  420. ->orderBy('historyID')
  421. ->fetchAll();
  422. foreach($histories as $history)
  423. {
  424. $history->actionID = $this->map['action'][$history->actionID];
  425. $this->dao->dbh($this->dbh)->insert(TABLE_HISTORY)->data($history)->exec();
  426. }
  427. }
  428. /**
  429. * Convert attachments.
  430. *
  431. * @access public
  432. * @return int the converted files count.
  433. */
  434. public function convertFile()
  435. {
  436. $this->setPath();
  437. $files = $this->dao->dbh($this->sourceDBH)
  438. ->select("
  439. actionID,
  440. fileName AS pathname,
  441. fileTitle AS title,
  442. fileType AS extension,
  443. fileSize AS size
  444. ")
  445. ->from(BUGFREE_TABLE_FILE)
  446. ->orderBy('fileID')
  447. ->fetchAll();
  448. foreach($files as $file)
  449. {
  450. /* Get the actionID in zentao, to get file info. */
  451. $zentaoActionID = $this->map['action'][$file->actionID];
  452. $zentaoAction = $this->dao->dbh($this->dbh)->findById($zentaoActionID)->from(TABLE_ACTION)->fetch();
  453. $file->objectType = $zentaoAction->objectType;
  454. $file->objectID = $zentaoAction->objectID;
  455. $file->addedBy = $zentaoAction->actor;
  456. $file->addedDate = $zentaoAction->date;
  457. unset($file->actionID);
  458. /* Compute the file size. */
  459. if(strpos($file->size, 'KB')) $file->size = (int)(str_replace('KB', '', $file->size) * 1024);
  460. if(strpos($file->size, 'MB')) $file->size = (int)(str_replace('MB', '', $file->size) * 1024 * 1024);
  461. /* Insert into database. */
  462. $this->dao->dbh($this->dbh)->insert(TABLE_FILE)->data($file)->exec();
  463. /* Copy file. */
  464. $soureFile = $this->filePath . $file->pathname;
  465. if(!file_exists($soureFile))
  466. {
  467. self::$info['files'][] = sprintf($this->lang->convert->errorFileNotExits, $soureFile);
  468. continue;
  469. }
  470. $targetFile = $this->app->getAppRoot() . "www/data/upload/{$this->app->company->id}/" . $file->pathname;
  471. $targetPath = dirname($targetFile);
  472. if(!is_dir($targetPath)) mkdir($targetPath, 0777, true);
  473. if(!copy($soureFile, $targetFile))
  474. {
  475. self::$info['files'][] = sprintf($this->lang->convert->errorCopyFailed, $targetFile);
  476. }
  477. }
  478. return count($files);
  479. }
  480. /**
  481. * Clear the converted records.
  482. *
  483. * @access public
  484. * @return void
  485. */
  486. public function clear()
  487. {
  488. foreach($this->session->state as $table => $maxID)
  489. {
  490. $this->dao->dbh($this->dbh)->delete()->from($table)->where('id')->gt($maxID)->exec();
  491. }
  492. }
  493. }