control.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * The control file of svn module 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 Yanyi Cao <caoyanyi@easycorp.com>
  8. * @package svn
  9. * @link https://www.zentao.net
  10. */
  11. class svn extends control
  12. {
  13. /**
  14. * 定时任务,同步SVN.
  15. * Sync svn.
  16. *
  17. * @access public
  18. * @return void
  19. */
  20. public function run()
  21. {
  22. set_time_limit(0);
  23. $this->svn->run();
  24. }
  25. /**
  26. * 对比文件.
  27. * Diff a file.
  28. *
  29. * @param string $url
  30. * @param int $revision
  31. * @access public
  32. * @return void
  33. */
  34. public function diff($url, $revision)
  35. {
  36. if(isset($_GET['repoUrl'])) $url = $this->get->repoUrl;
  37. $url = helper::safe64Decode($url);
  38. if(common::hasPriv('repo', 'diff'))
  39. {
  40. $svnRepos = $this->loadModel('repo')->getListBySCM('Subversion', 'haspriv');
  41. foreach($svnRepos as $repo)
  42. {
  43. if(strpos(strtolower($url), strtolower($repo->path)) === 0)
  44. {
  45. $entry = $this->repo->encodePath(str_ireplace($repo->path, '', $url));
  46. $oldRevision = $revision - 1;
  47. return print($this->fetch('repo', 'diff', "repoID=$repo->id&objectID=0&entry=$entry&oldRevision=$oldRevision&revision=$revision"));
  48. }
  49. }
  50. }
  51. $this->view->url = $url;
  52. $this->view->revision = $revision;
  53. $this->view->diff = $this->svn->diff($url, $revision);
  54. $this->display();
  55. }
  56. /**
  57. * 查看文件.
  58. * Cat a file.
  59. *
  60. * @param string $url
  61. * @param int $revision
  62. * @access public
  63. * @return void
  64. */
  65. public function cat($url, $revision)
  66. {
  67. if(isset($_GET['repoUrl'])) $url = $this->get->repoUrl;
  68. $url = helper::safe64Decode($url);
  69. if(common::hasPriv('repo', 'view'))
  70. {
  71. $repos = $this->loadModel('repo')->getListBySCM('Subversion', 'haspriv');
  72. foreach($repos as $repo)
  73. {
  74. if(strpos(strtolower($url), strtolower($repo->path)) === 0)
  75. {
  76. $entry = $this->repo->encodePath(str_ireplace(strtolower($repo->path), '', $url));
  77. return print($this->fetch('repo', 'view', "repoID=$repo->id&objectID=0&entry=$entry&revision=$revision"));
  78. }
  79. }
  80. }
  81. $this->view->url = $url;
  82. $this->view->revision = $revision;
  83. $this->view->code = $this->svn->cat($url, $revision);
  84. $this->display();
  85. }
  86. /**
  87. * 通过api同步提交信息。
  88. * Sync from the syncer by api.
  89. *
  90. * @access public
  91. * @return void
  92. */
  93. public function apiSync()
  94. {
  95. if(!$this->post->logs) return;
  96. $repoRoot = $this->post->repoRoot;
  97. $logs = stripslashes($this->post->logs);
  98. $logs = simplexml_load_string($logs);
  99. foreach($logs->logentry as $entry)
  100. {
  101. $log = $this->svn->convertLog($entry);
  102. if($log) $parsedLogs[] = $log;
  103. }
  104. $this->loadModel('repo');
  105. $parsedObjects = array('stories' => array(), 'tasks' => array(), 'bugs' => array());
  106. foreach($parsedLogs as $log)
  107. {
  108. $objects = $this->repo->parseComment($log->msg);
  109. if($objects)
  110. {
  111. $this->repo->saveAction2PMS($objects, $log, $repoRoot);
  112. if($objects['stories']) $parsedObjects['stories'] = array_merge($parsedObjects['stories'], $objects['stories']);
  113. if($objects['tasks']) $parsedObjects['tasks' ] = array_merge($parsedObjects['tasks'], $objects['tasks']);
  114. if($objects['bugs']) $parsedObjects['bugs'] = array_merge($parsedObjects['bugs'], $objects['bugs']);
  115. }
  116. }
  117. $parsedObjects['stories'] = array_unique($parsedObjects['stories']);
  118. $parsedObjects['tasks'] = array_unique($parsedObjects['tasks']);
  119. $parsedObjects['bugs'] = array_unique($parsedObjects['bugs']);
  120. $this->view->parsedObjects = $parsedObjects;
  121. return $this->display();
  122. }
  123. /**
  124. * 保存提交日志。
  125. * Ajax save log.
  126. *
  127. * @access public
  128. * @return void
  129. */
  130. public function ajaxSaveLog()
  131. {
  132. $repoUrl = trim($this->post->repoUrl);
  133. $message = trim($this->post->message);
  134. $revision = trim($this->post->revision);
  135. $files = $this->post->files;
  136. /* Ignore git. */
  137. if(strpos($repoUrl, '://') === false) return;
  138. $parsedFiles = array();
  139. $repoDirs = explode('/', trim($repoUrl, '/'));
  140. foreach($files as $file)
  141. {
  142. $file = trim($file);
  143. if(empty($file)) continue;
  144. $action = '';
  145. if(preg_match('/^[\w][ \t]/', $file))
  146. {
  147. $action = $file[0];
  148. $file = trim(substr($file, 2));
  149. }
  150. $fileDirs = explode('/', trim($file, '/'));
  151. $diffDirs = array_diff($fileDirs, $repoDirs);
  152. foreach($fileDirs as $i => $dir)
  153. {
  154. if(isset($diffDirs[$i])) break;
  155. if(!isset($diffDirs[$i])) unset($fileDirs[$i]);
  156. }
  157. $path = '/' . join('/', $fileDirs);
  158. $parsedFiles[$action][] = $path;
  159. }
  160. $objects = $this->loadModel('repo')->parseComment($message);
  161. if($objects)
  162. {
  163. $log = new stdclass();
  164. $log->author = $this->app->user->account;
  165. $log->date = helper::now();
  166. $log->msg = $message;
  167. $log->revision = $revision;
  168. $log->files = $parsedFiles;
  169. $this->repo->saveAction2PMS($objects, $log, $repoUrl);
  170. }
  171. }
  172. /**
  173. * 获取代码库列表。
  174. * Ajax get repos.
  175. *
  176. * @access public
  177. * @return void
  178. */
  179. public function ajaxGetRepos()
  180. {
  181. $repos = $this->svn->getRepos();
  182. echo json_encode($repos);
  183. }
  184. }