redmine1.1.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. <?php
  2. /**
  3. * The model file of redmine 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 Yangyang Shi <shiyangyang@cnezsoft.com>
  8. * @package convert
  9. * @version $Id $
  10. * @link https://www.zentao.net
  11. */
  12. class redmine11ConvertModel extends redmineConvertModel
  13. {
  14. static $convertGroupCount = 0;
  15. static $convertUserCount = 0;
  16. static $convertProductCount = 0;
  17. static $convertExecutionCount = 0;
  18. static $convertStoryCount = 0;
  19. static $convertTaskCount = 0;
  20. static $convertBugCount = 0;
  21. static $convertProductPlanCount = 0;
  22. static $convertTeamCount = 0;
  23. static $convertReleaseCount = 0;
  24. static $convertBuildCount = 0;
  25. static $convertDocLibCount = 0;
  26. static $convertDocCount = 0;
  27. static $convertFileCount = 0;
  28. public $issueType;
  29. public function __construct($issueType)
  30. {
  31. parent::__construct();
  32. $this->issueType = $issueType;
  33. }
  34. /**
  35. * Execute the converter.
  36. *
  37. * @access public
  38. * @return array
  39. */
  40. public function execute($version)
  41. {
  42. $this->clear();
  43. $this->setTable();
  44. $this->convertGroup();
  45. $this->convertUser();
  46. $this->convertUserGroup();
  47. $this->convertProduct();
  48. $this->convertExecution();
  49. $this->convertBuildAndRelease();
  50. $this->convertProductPlan();
  51. $this->convertExecutionProduct();
  52. $this->convertTeam();
  53. $this->convertDocLib();
  54. $this->convertDoc();
  55. $this->convertNews();
  56. $this->convertIssue();
  57. $this->convertFile();
  58. $this->dao->dbh($this->dbh);
  59. $this->loadModel('tree')->fixModulePath();
  60. $result['groups'] = self::$convertGroupCount;
  61. $result['users'] = self::$convertUserCount ;
  62. $result['products'] = self::$convertProductCount ;
  63. $result['executions'] = self::$convertExecutionCount ;
  64. $result['stories'] = self::$convertStoryCount;
  65. $result['tasks'] = self::$convertTaskCount ;
  66. $result['bugs'] = self::$convertBugCount ;
  67. $result['productPlans'] = self::$convertProductPlanCount;
  68. $result['teams'] = self::$convertTeamCount;
  69. $result['releases'] = self::$convertReleaseCount;
  70. $result['builds'] = self::$convertBuildCount;
  71. $result['docLibs'] = self::$convertDocLibCount ;
  72. $result['docs'] = self::$convertDocCount;
  73. $result['files'] = self::$convertFileCount;
  74. return $result;
  75. }
  76. /**
  77. * Set table names.
  78. *
  79. * @access public
  80. * @return void
  81. */
  82. public function setTable()
  83. {
  84. //$dbPrefix = $this->post->dbPrefix;
  85. $dbPrefix = '';
  86. define('REDMINE_TABLE_ATTACHMENTS', $dbPrefix . 'attachments');
  87. define('REDMINE_TABLE_AUTH_SOURCES', $dbPrefix . 'auth_sources');
  88. define('REDMINE_TABLE_BOARDS', $dbPrefix . 'boards');
  89. define('REDMINE_TABLE_CHANGES', $dbPrefix . 'changes');
  90. define('REDMINE_TABLE_CHANGESETS', $dbPrefix . 'changesets');
  91. define('REDMINE_TABLE_CHANGESETS_ISSUES', $dbPrefix . 'changesets_issues');
  92. define('REDMINE_TABLE_COMMENTS', $dbPrefix . 'comments');
  93. define('REDMINE_TABLE_CUSTOM_FIELDS', $dbPrefix . 'custom_fields');
  94. define('REDMINE_TABLE_CUSTOM_FIELDS_EXECUTIONS', $dbPrefix . 'custom_fields_executions');
  95. define('REDMINE_TABLE_CUSTOM_FIELDS_TRACKERS', $dbPrefix . 'custom_fields_trackers');
  96. define('REDMINE_TABLE_CUSTOM_VALUES', $dbPrefix . 'custom_values');
  97. define('REDMINE_TABLE_DOCUMENTS', $dbPrefix . 'documents');
  98. define('REDMINE_TABLE_ENABLED_MODULES', $dbPrefix . 'enabled_modules');
  99. define('REDMINE_TABLE_ENUMERATIONS', $dbPrefix . 'enumerations');
  100. define('REDMINE_TABLE_GROUPS_USERS', $dbPrefix . 'groups_users');
  101. define('REDMINE_TABLE_ISSUES', $dbPrefix . 'issues');
  102. define('REDMINE_TABLE_ISSUE_CATEGORIES', $dbPrefix . 'issue_categories');
  103. define('REDMINE_TABLE_ISSUE_RELATIONS', $dbPrefix . 'issue_relations');
  104. define('REDMINE_TABLE_ISSUE_STATUSES', $dbPrefix . 'issue_statuses');
  105. define('REDMINE_TABLE_JOURNALS', $dbPrefix . 'journals');
  106. define('REDMINE_TABLE_JOURNAL_DETAILS', $dbPrefix . 'journal_details');
  107. define('REDMINE_TABLE_MEMBERS', $dbPrefix . 'members');
  108. define('REDMINE_TABLE_MEMBER_ROLES', $dbPrefix . 'member_roles');
  109. define('REDMINE_TABLE_MESSAGES', $dbPrefix . 'messages');
  110. define('REDMINE_TABLE_NEWS', $dbPrefix . 'news');
  111. define('REDMINE_TABLE_OPEN_ID_AUTHENTICATION_ASSOCIATIONS', $dbPrefix . 'open_id_authentication_associations');
  112. define('REDMINE_TABLE_OPEN_ID_AUTHENTICATION_NONCES', $dbPrefix . 'open_id_authentication_nonces');
  113. define('REDMINE_TABLE_EXECUTIONS', $dbPrefix . 'executions');
  114. define('REDMINE_TABLE_EXECUTIONS_TRACKERS', $dbPrefix . 'executions_trackers');
  115. define('REDMINE_TABLE_QUERIES', $dbPrefix . 'queries');
  116. define('REDMINE_TABLE_REPOSITORIES', $dbPrefix . 'repositories');
  117. define('REDMINE_TABLE_ROLES', $dbPrefix . 'roles');
  118. define('REDMINE_TABLE_SCHEMA_MIGRATIONS', $dbPrefix . 'schema_migrations');
  119. define('REDMINE_TABLE_SETTINGS', $dbPrefix . 'settings');
  120. define('REDMINE_TABLE_TIME_ENTRIES', $dbPrefix . 'time_entries');
  121. define('REDMINE_TABLE_TOKENS', $dbPrefix . 'tokens');
  122. define('REDMINE_TABLE_TRACKERS', $dbPrefix . 'trackers');
  123. define('REDMINE_TABLE_USERS', $dbPrefix . 'users');
  124. define('REDMINE_TABLE_USER_PREFERENCES', $dbPrefix . 'user_preferences');
  125. define('REDMINE_TABLE_VERSIONS', $dbPrefix . 'versions');
  126. define('REDMINE_TABLE_WATCHERS', $dbPrefix . 'watchers');
  127. define('REDMINE_TABLE_WIKIS', $dbPrefix . 'wikis');
  128. define('REDMINE_TABLE_WIKI_CONTENTS', $dbPrefix . 'wiki_contents');
  129. define('REDMINE_TABLE_WIKI_CONTENT_VERSIONS', $dbPrefix . 'wiki_content_versions');
  130. define('REDMINE_TABLE_WIKI_PAGES', $dbPrefix . 'wiki_pages');
  131. define('REDMINE_TABLE_WIKI_REDIRECTS', $dbPrefix . 'wiki_redirects');
  132. define('REDMINE_TABLE_WORKFLOWS', $dbPrefix . 'workflows');
  133. }
  134. /**
  135. * Convert groups.
  136. *
  137. * @access public
  138. * @return void
  139. */
  140. public function convertGroup()
  141. {
  142. /* Get group list */
  143. $groups = $this->dao->dbh($this->sourceDBH)
  144. ->select("id, lastName AS name")
  145. ->from(REDMINE_TABLE_USERS)
  146. ->where('type')->eq('Group')
  147. ->fetchAll('id');
  148. $zentaoGroupNames = $this->dao->dbh($this->dbh)->select('id, name')->from(TABLE_GROUP)->fetchPairs();
  149. $zentaoGroupIDs = $this->dao->dbh($this->dbh)->select('name, id')->from(TABLE_GROUP)->fetchPairs();
  150. /* Insert into zentao */
  151. $convertCount = 0;
  152. foreach($groups as $groupID =>$group)
  153. {
  154. unset($group->id);
  155. if(in_array("$group->name", $zentaoGroupNames))
  156. {
  157. self::$info['groups'][] = sprintf($this->lang->convert->errorGroupExists, $group->name);
  158. $this->map['groups'][$groupID] = $zentaoGroupIDs[$group->name];
  159. }
  160. else
  161. {
  162. $this->dao->dbh($this->dbh)->insert(TABLE_GROUP)
  163. ->data($group)->exec();
  164. $this->map['groups'][$groupID] = $this->dao->lastInsertID();
  165. $convertCount ++;
  166. }
  167. }
  168. self::$convertGroupCount += $convertCount;
  169. }
  170. /**
  171. * Convert users.
  172. *
  173. * @access public
  174. * @return void
  175. */
  176. public function convertUser()
  177. {
  178. /* Get user list. */
  179. $users = $this->dao->dbh($this->sourceDBH)
  180. ->select("id, login AS account, firstname, lastname, mail as email")
  181. ->from(REDMINE_TABLE_USERS)
  182. ->where('type')->eq('User')
  183. ->fetchAll('id');
  184. $zentaoUserNames = $this->dao->dbh($this->dbh)->select('id, account')->from(TABLE_USER)->fetchPairs();
  185. $zentaoUserIDs = $this->dao->dbh($this->dbh)->select('account, id')->from(TABLE_USER)->fetchPairs();
  186. /* Insert into zentao. */
  187. $convertCount = 0;
  188. foreach($users as $id => $user)
  189. {
  190. if(in_array("$user->account", $zentaoUserNames))
  191. {
  192. self::$info['users'][] = sprintf($this->lang->convert->errorUserExists, $user->account);
  193. $this->map['users'][$id] = $zentaoUserIDs[$user->account];
  194. }
  195. else
  196. {
  197. $user->password = md5('123456');
  198. $user->realname = $user->lastname . $user->firstname;
  199. unset($user->id);
  200. unset($user->lastname);
  201. unset($user->firstname);
  202. $this->dao->dbh($this->dbh)->insert(TABLE_USER)->data($user)->exec();
  203. $this->map['users'][$id] = $this->dao->lastInsertID();
  204. $convertCount ++;
  205. }
  206. }
  207. self::$convertUserCount += $convertCount;
  208. }
  209. /**
  210. * convert relationship between user and group.
  211. *
  212. * @access public
  213. * @return void
  214. */
  215. public function convertUserGroup()
  216. {
  217. $this->map['groups'][0] = 0;
  218. /* Get relation between user and group list. */
  219. $userGroups = $this->dao->dbh($this->sourceDBH)
  220. ->select("t1.group_id, t2.login as account")
  221. ->from(REDMINE_TABLE_GROUPS_USERS)->alias('t1')
  222. ->leftJoin(REDMINE_TABLE_USERS)->alias('t2')->on('t1.user_id = t2.id')
  223. ->fetchAll();
  224. $zentaoUserGroups = $this->dao->dbh($this->dbh)->select('*')->from(TABLE_USERGROUP)->fetchAll();
  225. /* Insert into zentao. */
  226. $userGroupExist = false;
  227. foreach($userGroups as $userGroup)
  228. {
  229. $userGroup->group = $this->map['groups'][$userGroup->group_id];
  230. unset($userGroup->group_id);
  231. foreach($zentaoUserGroups as $zentaoUserGroup)
  232. {
  233. if($userGroup->group == $zentaoUserGroup->group and $userGroup->account == $zentaoUserGroup->account)
  234. {
  235. $userGroupExist = true;
  236. }
  237. }
  238. if($userGroupExist == false)
  239. {
  240. $this->dao->dbh($this->dbh)->insert(TABLE_USERGROUP)->data($userGroup)->exec();
  241. }
  242. }
  243. }
  244. /**
  245. * convert products.
  246. *
  247. * @access public
  248. * @return void
  249. */
  250. public function convertProduct()
  251. {
  252. /* Get product list */
  253. $products = $this->dao->dbh($this->sourceDBH)
  254. ->select("id, name, description as `desc`, created_on as createdDate")
  255. ->from(REDMINE_TABLE_EXECUTIONS)
  256. ->fetchAll('id');
  257. /* Insert into zentao */
  258. foreach($products as $productID => $product)
  259. {
  260. unset($product->id);
  261. $this->dao->dbh($this->dbh)->insert(TABLE_PRODUCT)->data($product)->exec();
  262. $this->map['products'][$productID] = $this->dao->lastInsertID();
  263. }
  264. self::$convertProductCount += count($products);
  265. }
  266. /**
  267. * Convert executions.
  268. *
  269. * @access public
  270. * @return void
  271. */
  272. public function convertExecution()
  273. {
  274. /* Get execution list */
  275. $executions = $this->dao->dbh($this->sourceDBH)
  276. ->select("id, name, execution_id, description as `desc`, effective_date AS end")
  277. ->from(REDMINE_TABLE_VERSIONS)
  278. ->fetchAll('id');
  279. /* Insert into zentao */
  280. foreach($executions as $executionID => $execution)
  281. {
  282. $productID = $execution->execution_id;
  283. unset($execution->id);
  284. unset($execution->execution_id);
  285. $this->dao->dbh($this->dbh)->insert(TABLE_execution)->data($execution)->exec();
  286. $this->map['executions'][$productID][$executionID] = $this->dao->lastInsertID();
  287. $this->map['execution'][$executionID] = $this->map['executions'][$productID][$executionID];
  288. }
  289. /* Create a same name execution with product */
  290. foreach($this->map['products'] as $productID)
  291. {
  292. $execution = $this->dao->dbh($this->dbh)->select('name')->from(TABLE_PRODUCT)->where('id')->eq($productID)->fetch();
  293. $this->dao->dbh($this->dbh)->insert(TABLE_execution)->data($execution)->exec();
  294. $this->map['executionOfProduct'][$productID] = $this->dao->lastinsertID();
  295. }
  296. $convertCount = count($executions) + count($this->map['executionOfProduct']);
  297. self::$convertexecutionCount += $convertCount;
  298. }
  299. /**
  300. * convert builds and releases
  301. *
  302. * @access public
  303. * @return void
  304. */
  305. public function convertBuildAndRelease()
  306. {
  307. /* Get build list */
  308. $buildAndReleases = $this->dao->dbh($this->sourceDBH)
  309. ->select("id, name, execution_id, description as `desc`, effective_date AS date")
  310. ->from(REDMINE_TABLE_VERSIONS)
  311. ->fetchAll('id');
  312. /* Insert into zentao */
  313. $convertBuildsCount = 0;
  314. $convertReleasesCount = 0;
  315. $zentaoBuildNames = $this->dao->dbh($this->dbh)->select('id, name')->from(TABLE_BUILD)->fetchPairs();
  316. $zentaoBuildIDs = $this->dao->dbh($this->dbh)->select('name, id')->from(TABLE_BUILD)->fetchPairs();
  317. $zentaoReleaseNames = $this->dao->dbh($this->dbh)->select('id, name')->from(TABLE_RELEASE)->fetchPairs();
  318. $zentaoReleaseIDs = $this->dao->dbh($this->dbh)->select('name, id')->from(TABLE_RELEASE)->fetchPairs();
  319. foreach($buildAndReleases as $id => $buildAndRelease)
  320. {
  321. $buildAndRelease->execution = $this->map['execution'][$id];
  322. $buildAndRelease->product = $this->map['products'][$buildAndRelease->execution_id];
  323. unset($buildAndRelease->id);
  324. unset($buildAndRelease->execution_id);
  325. if(in_array($buildAndRelease->name, $zentaoBuildNames))
  326. {
  327. self::$info['builds'][] = sprintf($this->lang->convert->errorBuildExists, $buildAndRelease->name);
  328. $buildAndRelease->build = $zentaoBuildIDs[$buildAndRelease->name];
  329. }
  330. else
  331. {
  332. $this->dao->dbh($this->dbh)->insert(TABLE_BUILD)->data($buildAndRelease)->exec();
  333. $buildAndRelease->build = $this->dao->lastInsertID();
  334. $convertBuildsCount ++;
  335. }
  336. unset($buildAndRelease->execution);
  337. if(in_array($buildAndRelease->name, $zentaoBuildNames))
  338. {
  339. self::$info['releases'][] = sprintf($this->lang->convert->errorReleaseExists, $buildAndRelease->name);
  340. }
  341. else
  342. {
  343. $this->dao->dbh($this->dbh)->insert(TABLE_RELEASE)->data($buildAndRelease)->exec();
  344. $convertReleasesCount ++;
  345. }
  346. }
  347. self::$convertBuildCount += $convertBuildsCount;
  348. self::$convertReleaseCount += $convertReleasesCount;
  349. }
  350. /**
  351. * convert productPlans
  352. *
  353. * @access public
  354. * @return void
  355. */
  356. public function convertProductPlan()
  357. {
  358. /* Get productPlan list */
  359. $productPlans = $this->dao->dbh($this->sourceDBH)
  360. ->select("id, name as title, execution_id, description as `desc`, effective_date as end, created_on AS begin")
  361. ->from(REDMINE_TABLE_VERSIONS)
  362. ->fetchAll('id');
  363. /* Insert into zentao */
  364. foreach($productPlans as $id => $productPlan)
  365. {
  366. $productPlan->product = $this->map['products'][$productPlan->execution_id];
  367. $productPlan->createdBy = $this->app->user->account;
  368. $productPlan->createdDate = helper::now();
  369. unset($productPlan->id);
  370. unset($productPlan->execution_id);
  371. $this->dao->dbh($this->dbh)->insert(TABLE_PRODUCTPLAN)->data($productPlan)->exec();
  372. }
  373. /* Create a same plan with product */
  374. foreach($this->map['products'] as $productID)
  375. {
  376. $productPlan = $this->dao->dbh($this->dbh)->select("name as title, createdDate as begin")->from(TABLE_PRODUCT)->where('id')->eq($productID)->fetch();
  377. $productPlan->product = $productID;
  378. $productPlan->createdBy = $this->app->user->account;
  379. $productPlan->createdDate = helper::now();
  380. $this->dao->dbh($this->dbh)->insert(TABLE_PRODUCTPLAN)->data($productPlan)->exec();
  381. $this->map['planOfProduct'][$productID] = $this->dao->lastinsertID();
  382. }
  383. $convertCount = count($this->map['products']) + count($productPlans);
  384. self::$convertProductPlanCount += $convertCount;
  385. }
  386. /**
  387. * convert relationship between execution and product.
  388. *
  389. * @access public
  390. * @return void
  391. */
  392. public function convertExecutionProduct()
  393. {
  394. foreach($this->map['executions'] as $productID => $executions)
  395. {
  396. foreach($executions as $executionID => $execution)
  397. {
  398. $this->dao->dbh($this->dbh)->insert(TABLE_PROJECTPRODUCT)
  399. ->set('project')->eq($execution)
  400. ->set('product')->eq($this->map['products'][$productID])
  401. ->exec();
  402. }
  403. }
  404. }
  405. /**
  406. * convert teams.
  407. *
  408. * @access public
  409. * @return void
  410. */
  411. public function convertTeam()
  412. {
  413. /* Get team list */
  414. $teams = $this->dao->dbh($this->sourceDBH)
  415. ->select("t2.login as account, t1.execution_id, t1.created_on AS joinDate")
  416. ->from(REDMINE_TABLE_MEMBERS)->alias('t1')
  417. ->leftJoin(REDMINE_TABLE_USERS)->alias('t2')->on('t1.user_id = t2.id')
  418. ->where('t2.type')->eq('User')
  419. ->fetchAll();
  420. /* Insert into zentao */
  421. foreach($teams as $team)
  422. {
  423. $productID = $team->execution_id;
  424. unset($team->execution_id);
  425. foreach($this->map['executions'][$productID] as $executionID)
  426. {
  427. $team->execution = $executionID;
  428. $this->dao->dbh($this->dbh)->insert(TABLE_TEAM)->data($team)->exec();
  429. }
  430. }
  431. self::$convertTeamCount += count($teams);
  432. }
  433. /**
  434. * convert docLibs.
  435. *
  436. * @access public
  437. * @return void
  438. */
  439. public function convertDocLib()
  440. {
  441. /* Get docLib list */
  442. $docLibs = $this->dao->dbh($this->sourceDBH)
  443. ->select("id, name")->from(REDMINE_TABLE_ENUMERATIONS)
  444. ->where('type')->eq('DocumentCategory')
  445. ->fetchAll('id');
  446. /* Insert into zentao */
  447. foreach($docLibs as $docLibID => $docLib)
  448. {
  449. unset($docLib->id);
  450. $this->dao->dbh($this->dbh)->insert(TABLE_DOCLIB)->data($docLib)->exec();
  451. $this->map['docLibs'][$docLibID] = $this->dao->lastInsertID();
  452. }
  453. self::$convertDocLibCount += count($docLibs);
  454. }
  455. /**
  456. * convert docs.
  457. *
  458. * @access public
  459. * @return void
  460. */
  461. public function convertDoc()
  462. {
  463. /* Get all docs */
  464. $docs = $this->dao->dbh($this->sourceDBH)
  465. ->select("t1.id, t1.execution_id AS product, t2.id AS lib, t1.title, t1.description AS content, t1.created_on AS addedDate")
  466. ->from(REDMINE_TABLE_DOCUMENTS)->alias('t1')
  467. ->leftJoin(REDMINE_TABLE_ENUMERATIONS)->alias('t2')->on('t1.category_id = t2.id')
  468. ->fetchAll('id');
  469. /* Insert into zentao */
  470. foreach($docs as $docID => $doc)
  471. {
  472. unset($doc->id);
  473. $doc->type = 'text';
  474. $doc->execution = 0;
  475. $doc->product = $this->map['products'][$doc->product];
  476. $doc->lib = $this->map['docLibs'][$doc->lib];
  477. $this->dao->dbh($this->dbh)->insert(TABLE_DOC)->data($doc)->exec();
  478. $this->map['docs'][$docID] = $this->dao->lastInsertID();
  479. }
  480. self::$convertDocCount += count($docs);
  481. }
  482. /**
  483. * convert news.
  484. *
  485. * @access public
  486. * @return void
  487. */
  488. public function convertNews()
  489. {
  490. /* Get news from redmine */
  491. $news = $this->dao->dbh($this->sourceDBH)
  492. ->select("t1.execution_id as product, t1.title, t1.summary as digest, t1.description as content, t2.login as addedBy, t1.created_on as addedDate")
  493. ->from(REDMINE_TABLE_NEWS)->alias('t1')
  494. ->leftJoin(REDMINE_TABLE_USERS)->alias('t2')->on('t1.author_id = t2.id')
  495. ->fetchAll();
  496. /* Create a news docLib */
  497. $newLib->name = 'news';
  498. $this->dao->dbh($this->dbh)->insert(TABLE_DOCLIB)->data($newLib)->exec();
  499. $this->map['news'] = $this->dao->lastInsertID();
  500. self::$convertDocLibCount += 1;
  501. /* Insert into zentao */
  502. foreach($news as $new)
  503. {
  504. $new->product = $this->map['products'][$new->product];
  505. $new->execution = 0;
  506. $new->lib = $this->map['news'];
  507. $new->type = 'text';
  508. $this->dao->dbh($this->dbh)->insert(TABLE_DOC)->data($new)->exec();
  509. }
  510. self::$convertDocCount += count($news);
  511. }
  512. /**
  513. * convert issue
  514. *
  515. * @param array $aimTypes //aimTypes['issueTypeID'] = aimtype eg. aimTypes[1] = 'bug';
  516. * @param array $statusTypes //statusTypes['task']['statusTypeID'] = statusType eg. statusTypes['task'][1] = 'wait';
  517. * //statusTypes['bug']['statusTypeID'] = statusType eg. statusTypes['bug'][1] = 'active';
  518. * @param array $priTypes //priTypes['task']['priTypeID'] = priType; eg. priTypes['task'][1] = 1;
  519. * @access public
  520. * @return void
  521. */
  522. public function convertIssue()
  523. {
  524. $aimTypes = $this->issueType->aimTypes;
  525. $statusTypes = $this->issueType->statusTypes;
  526. $priTypes = $this->issueType->priTypes;
  527. foreach($aimTypes as $issueType => $aimType)
  528. {
  529. if('story' == $aimType)
  530. {
  531. $this->convertStory($issueType, $statusTypes, $priTypes);
  532. }
  533. elseif('task' == $aimType)
  534. {
  535. $this->convertTask($issueType, $statusTypes, $priTypes);
  536. }
  537. else
  538. {
  539. $this->convertBug($issueType, $statusTypes, $priTypes);
  540. }
  541. }
  542. }
  543. /**
  544. * convert story
  545. *
  546. * @param array $issueType
  547. * @param array $statusTypes
  548. * @param array $priTypes
  549. * @access public
  550. * @return void
  551. */
  552. public function convertStory($issueType, $statusTypes, $priTypes)
  553. {
  554. /* Get story list*/
  555. $stories = $this->dao->dbh($this->sourceDBH)
  556. ->select("t1.id, t1.execution_id as product, t1.subject as title, t1.description as spec, t1.status_id as status, t2.login as assignedTo, t1.priority_id as pri, t3.login as openedBy, t1.created_on as openedDate, t1.estimated_hours as estimate, t1.updated_on as lastEditedDate")
  557. ->from(REDMINE_TABLE_ISSUES)->alias('t1')
  558. ->leftJoin(REDMINE_TABLE_USERS)->alias('t2')->on('t1.assigned_to_id = t2.id')
  559. ->leftJoin(REDMINE_TABLE_USERS)->alias('t3')->on('t1.author_id = t3.id')
  560. ->where('t1.tracker_id')->eq($issueType)
  561. ->fetchAll('id');
  562. /* Insert into zentao */
  563. foreach($stories as $issueID => $story)
  564. {
  565. $storySpec->title = $story->title;
  566. $storySpec->spec = $story->spec;
  567. unset($story->id);
  568. unset($story->spec);
  569. /* Insert story into table story */
  570. $story->product = $this->map['products'][$story->product];
  571. $story->module = 0;
  572. $story->plan = $this->map['planOfProduct'][$story->product];
  573. $story->fromBug = 0;
  574. $story->pri = $priTypes['story'][$story->pri];
  575. $story->status = $statusTypes['story'][$story->status];
  576. $story->toBug = 0;
  577. $story->duplicateStory = 0;
  578. $this->dao->dbh($this->dbh)->insert(TABLE_STORY)->data($story)->exec();
  579. $this->map['issueID'][$issueID] = $this->dao->lastInsertID();
  580. $this->map['issueType'][$issueID] = 'story';
  581. /* Insert data into table storySpec */
  582. $storySpec->story = $this->map['issueID'][$issueID];
  583. $storySpec->version = 1;
  584. $this->dao->dbh($this->dbh)->insert(TABLE_STORYSPEC)->data($storySpec)->exec();
  585. }
  586. self::$convertStoryCount += count($stories);
  587. }
  588. /**
  589. * convert task
  590. *
  591. * @param array $issueType
  592. * @param array $statusTypes
  593. * @param array $priTypes
  594. * @access public
  595. * @return void
  596. */
  597. public function convertTask($issueType, $statusTypes, $priTypes)
  598. {
  599. /* Get task list */
  600. $tasks = $this->dao->dbh($this->sourceDBH)
  601. ->select("t1.id, t1.execution_id as product, t1.fixed_version_id as execution, t1.subject as name, t1.description as `desc`, t1.due_date as deadline, t1.status_id as status, t2.login as assignedTo, t1.priority_id as pri, t3.login as openedBy, t1.created_on as openedDate, t1.estimated_hours as estimate, t1.updated_on as lastEditedDate")
  602. ->from(REDMINE_TABLE_ISSUES)->alias('t1')
  603. ->leftJoin(REDMINE_TABLE_USERS)->alias('t2')->on('t1.assigned_to_id = t2.id')
  604. ->leftJoin(REDMINE_TABLE_USERS)->alias('t3')->on('t1.author_id = t3.id')
  605. ->where('t1.tracker_id')->eq($issueType)
  606. ->fetchAll('id');
  607. /* Insert into zentao */
  608. foreach($tasks as $issueID => $task)
  609. {
  610. $task->story = 0;
  611. $task->storyVersion = 0;
  612. $task->type = 'misc';
  613. $task->pri = $priTypes['task'][$task->pri];
  614. $task->status = $statusTypes['task'][$task->status];
  615. if($task->execution == 0)
  616. {
  617. $task->execution = $this->map['executionOfProduct'][$task->product];
  618. }
  619. else
  620. {
  621. $task->execution = $this->map['execution'][$task->execution];
  622. }
  623. unset($task->id);
  624. unset($task->product);
  625. $this->dao->dbh($this->dbh)->insert(TABLE_TASK)->data($task)->exec();
  626. $this->map['issueID'][$issueID] = $this->dao->lastInsertID();
  627. $this->map['issueType'][$issueID] = 'task';
  628. }
  629. self::$convertTaskCount += count($tasks);
  630. }
  631. /**
  632. * convert bug
  633. *
  634. * @param array $issueType
  635. * @param array $statusTypes
  636. * @param array $priTypes
  637. * @access public
  638. * @return void
  639. */
  640. public function convertBug($issueType, $statusTypes, $priTypes)
  641. {
  642. /* Get bug list */
  643. $bugs = $this->dao->dbh($this->sourceDBH)
  644. ->select("t1.id, t1.execution_id as product, t1.fixed_version_id execution, t1.subject as title, t1.description as steps, t1.status_id as status, t2.login as assignedTo, t1.priority_id as pri, t3.login as openedBy, t1.created_on as openedDate, t1.updated_on as lastEditedDate")
  645. ->from(REDMINE_TABLE_ISSUES)->alias('t1')
  646. ->leftJoin(REDMINE_TABLE_USERS)->alias('t2')->on('t1.assigned_to_id = t2.id')
  647. ->leftJoin(REDMINE_TABLE_USERS)->alias('t3')->on('t1.author_id = t3.id')
  648. ->where('t1.tracker_id')->eq($issueType)
  649. ->fetchAll('id');
  650. /* Insert into zentao */
  651. foreach($bugs as $issueID => $bug)
  652. {
  653. $bug->product = $this->map['products'][$bug->product];
  654. $bug->module = 0;
  655. $bug->story = 0;
  656. $bug->storyVersion = 1;
  657. $bug->task = 0;
  658. $bug->severity = 3;
  659. $bug->type = 'others';
  660. $bug->status = $statusTypes['bug'][$bug->status];
  661. $bug->openedBuild = 'trunk';
  662. $bug->duplicateBug = 0;
  663. $bug->case = 0;
  664. $bug->caseVersion = 1;
  665. $bug->result = 0;
  666. if($bug->execution == 0)
  667. {
  668. $bug->execution = $this->map['executionOfProduct'][$bug->product];
  669. }
  670. else
  671. {
  672. $bug->execution = $this->map['execution'][$bug->execution];
  673. }
  674. unset($bug->id);
  675. $this->dao->dbh($this->dbh)->insert(TABLE_BUG)->data($bug)->exec();
  676. $this->map['issueID'][$issueID] = $this->dao->lastInsertID();
  677. $this->map['issueType'][$issueID] = 'bug';
  678. }
  679. self::$convertBugCount += count($bugs);
  680. }
  681. /**
  682. * Convert attachments.
  683. *
  684. * @access public
  685. * @return void
  686. */
  687. public function convertFile()
  688. {
  689. $this->setPath();
  690. /* Get file list */
  691. $files = $this->dao->dbh($this->sourceDBH)
  692. ->select('t1.id, t1.container_id as objectID, t1.container_type as objectType, t1.filename as title, t1.disk_filename as pathname, t1.filesize as size, t2.login as addedBy, t1.created_on as addedDate, description')
  693. ->from(REDMINE_TABLE_ATTACHMENTS)->alias('t1')
  694. ->leftJoin(REDMINE_TABLE_USERS)->alias('t2')->on('t1.author_id = t2.id')
  695. ->fetchAll('id');
  696. /* Insert into zentao */
  697. foreach($files as $fileID => $file)
  698. {
  699. if($file->description != '')
  700. {
  701. $file->title = $file->description;
  702. unset($file->description);
  703. }
  704. else
  705. {
  706. unset($file->description);
  707. }
  708. /* Transform objectType and objectID */
  709. if($file->objectType == 'Issue')
  710. {
  711. $file->objectType = $this->map['issueType'][$file->objectID];
  712. $file->objectID = $this->map['issueID'][$file->objectID];
  713. }
  714. elseif($file->objectType == 'Document')
  715. {
  716. $file->objectType = 'doc' ;
  717. $file->objectID = $this->map['docs'][$file->objectID];
  718. }
  719. elseif($file->objectType == 'WikiPage')
  720. {
  721. continue;
  722. }
  723. elseif($file->objectType == 'Version')
  724. {
  725. $doc->execution = $this->map['execution'][$file->objectID];
  726. $doc = $this->dao->dbh($this->dbh)->select('product')->from(TABLE_PROJECTPRODUCT)->where('project')->eq($doc->execution)->fetch();
  727. $doc->lib = 'execution';
  728. $doc->module = 0;
  729. $doc->title = $file->title;
  730. $doc->type = 'file';
  731. $doc->addedBy = $file->addedBy;
  732. $doc->addedDate = $file->addedDate;
  733. $this->dao->dbh($this->dbh)->insert(TABLE_DOC)->data($doc)->exec();
  734. self::$convertDocCount += 1;
  735. $file->objectType = 'doc';
  736. $file->objectID = $this->dao->lastInsertID();
  737. }
  738. $pathname = pathinfo($file->pathname);
  739. $file->extension = $pathname["extension"];
  740. unset($file->id);
  741. /* Insert into database. */
  742. $this->dao->dbh($this->dbh)->insert(TABLE_FILE)->data($file)->exec();
  743. /* Copy file. */
  744. $soureFile = $this->filePath . $file->pathname;
  745. if(!file_exists($soureFile))
  746. {
  747. self::$info['files'][] = sprintf($this->lang->convert->errorFileNotExits, $soureFile);
  748. continue;
  749. }
  750. $targetFile = $this->app->getAppRoot() . "www/data/upload/{$this->app->company->id}/" . $file->pathname;
  751. $targetPath = dirname($targetFile);
  752. if(!is_dir($targetPath)) mkdir($targetPath, 0777, true);
  753. if(!copy($soureFile, $targetFile))
  754. {
  755. self::$info['files'][] = sprintf($this->lang->convert->errorCopyFailed, $targetFile);
  756. }
  757. }
  758. self::$convertFileCount += count($files);
  759. }
  760. /**
  761. * Clear the converted records.
  762. *
  763. * @access public
  764. * @return void
  765. */
  766. public function clear()
  767. {
  768. foreach($this->session->state as $table => $maxID)
  769. {
  770. $this->dao->dbh($this->dbh)->delete()->from($table)->where('id')->gt($maxID)->exec();
  771. }
  772. }
  773. }