PageRenderTime 79ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/sly/Controller/Mercurial.php

https://bitbucket.org/mediastuttgart/mercurial
PHP | 639 lines | 408 code | 99 blank | 132 comment | 46 complexity | 2f7c67f291543079d32385f68b1e60cc MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright (c) 2012, MEDIASTUTTGART, http://mediastuttgart.de
  4. *
  5. * This file is released under the terms of the MIT license.
  6. * You can find the complete text in the attached LICENSE
  7. * file or online at:
  8. *
  9. * http://www.opensource.org/licenses/mit-license.php
  10. */
  11. /**
  12. * Mercurial controller
  13. */
  14. class sly_Controller_Mercurial extends sly_Controller_Backend implements sly_Controller_Interface {
  15. private $init;
  16. /**
  17. * Init controller
  18. */
  19. protected function init() {
  20. if ($this->init) return;
  21. $this->init = true;
  22. $this->renderLayout();
  23. }
  24. /**
  25. * Controller index view
  26. */
  27. public function indexAction() {
  28. $this->init();
  29. $this->getLayout()->setBodyAttr('class', 'mercurial-index-view');
  30. $mercurial = Hg::getService();
  31. $repositories = Mercurial_Provider::getRepositories();
  32. $this->setView('index.phtml', compact(array('repositories', 'mercurial')));
  33. }
  34. /**
  35. * Controller clone view
  36. */
  37. public function createAction() {
  38. $this->init();
  39. $this->setView('clone.phtml');
  40. }
  41. /**
  42. * Controller clone action
  43. *
  44. * @throws <Hg_Exception> Thrown Exception when unable to clone repository
  45. */
  46. public function do_createAction() {
  47. $this->init();
  48. if (sly_request('cancel', 'string', '')) {
  49. $this->info(t('mercurial_cancel_message'));
  50. $this->indexAction();
  51. }
  52. else {
  53. $title = sly_request('title', 'string', '');
  54. $path = sly_request('path', 'string', '');
  55. $directory = sly_request('directory', 'string', '');
  56. $source = sly_request('source', 'string', '');
  57. $options = sly_request('options', 'array', array());
  58. $directory = substr($path, strlen(SLY_BASE), strlen($path)) . DIRECTORY_SEPARATOR . $directory;
  59. try {
  60. if (empty($title)) throw new Hg_Exception(t('mercurial_repository_clone_error_message_description'));
  61. if (empty($path)) throw new Hg_Exception(t('mercurial_repository_clone_error_message_path'));
  62. if (empty($directory)) throw new Hg_Exception(t('mercurial_repository_clone_error_message_directory'));
  63. if (empty($source)) throw new Hg_Exception(t('mercurial_repository_clone_error_message_source'));
  64. $data = array(
  65. 'title' => $title,
  66. 'directory' => $directory,
  67. 'source' => $source,
  68. 'options' => $options
  69. );
  70. $container = new Mercurial_Model_Repository_Container($data);
  71. $service = Mercurial_Service::getService('hg');
  72. $command = $service->setClone($container);
  73. $output = $command->getOutput('external', 'paragraph');
  74. $container->transform();
  75. $this->info(t('mercurial_repository_clone_success_message'));
  76. $this->setView('console.phtml', compact(array('command', 'output', 'options')));
  77. }
  78. catch (Hg_Exception $exception) {
  79. $this->warn($exception->getMessage());
  80. $this->createAction();
  81. }
  82. }
  83. }
  84. /**
  85. * Controller add view
  86. */
  87. public function addAction() {
  88. $this->init();
  89. $this->setView('add.phtml');
  90. }
  91. /**
  92. * Controller add action
  93. *
  94. * @throws <Hg_Exception> Thrown Exception when unable to add repository
  95. */
  96. public function do_addAction() {
  97. $this->init();
  98. if (sly_request('cancel', 'string', '')) {
  99. $this->info(t('mercurial_cancel_message'));
  100. $this->indexAction();
  101. }
  102. else {
  103. $title = sly_request('title', 'string', '');
  104. $directory = sly_request('directory', 'string', '');
  105. $options = sly_request('options', 'array', array());
  106. $directory = substr($directory, strlen(SLY_BASE), strlen($directory));
  107. try {
  108. if (empty($title)) throw new Hg_Exception(t('mercurial_repository_add_error_message_description'));
  109. if (empty($directory)) throw new Hg_Exception(t('mercurial_repository_add_error_message_directory'));
  110. $data = array(
  111. 'title' => $title,
  112. 'directory' => $directory,
  113. 'options' => $options
  114. );
  115. $container = new Mercurial_Model_Repository_Container($data);
  116. $service = Mercurial_Service::getService('hg', $container->getDirectory());
  117. $command = $service->setAdd($container);
  118. $container->transform();
  119. $this->info(t('mercurial_repository_add_success_message'));
  120. $this->indexAction();
  121. }
  122. catch (Hg_Exception $exception) {
  123. $this->warn($exception->getMessage());
  124. $this->addAction();
  125. }
  126. }
  127. }
  128. /**
  129. * Oneclick update view
  130. */
  131. public function oneclickAction() {
  132. $this->init();
  133. if (sly_request('cancel', 'string', '')) {
  134. $this->info(t('mercurial_cancel_message'));
  135. $this->indexAction();
  136. }
  137. else {
  138. $repositories = array();
  139. foreach (Mercurial_Provider::getRepositories() as $repository) {
  140. $repositories[$repository->getID()] = $repository->getTitle();
  141. }
  142. $this->setView('oneclick.phtml', compact(array('repositories')));
  143. }
  144. }
  145. /**
  146. * Oneclick update action
  147. */
  148. public function do_oneclickAction() {
  149. $this->init();
  150. if (sly_request('cancel', 'string', '')) {
  151. $this->info(t('mercurial_cancel_message'));
  152. $this->indexAction();
  153. }
  154. else {
  155. $repos = sly_request('repositories', 'array', array());
  156. $options = sly_request('options', 'array', array());
  157. if (empty($repos)) {
  158. $this->warn('Bitte wählen Sie mindestens ein Repository aus.');
  159. } else {
  160. $repositories = array();
  161. foreach ($repos as $repositoryID) {
  162. $repositories[] = Mercurial_Provider::getRepository($repositoryID);
  163. }
  164. }
  165. $this->setView('queue.phtml', compact(array('repositories', 'options')));
  166. }
  167. }
  168. /**
  169. * Controller pull view
  170. */
  171. public function pullAction() {
  172. $this->init();
  173. $repositoryID = sly_request('id', 'int', 0);
  174. $repository = Mercurial_Provider::getRepository($repositoryID);
  175. $this->setView('pull.phtml', compact(array('repository')));
  176. }
  177. /**
  178. * Controller pull action
  179. *
  180. * @throws <Hg_Exception> Thrown Exception when unable to edit repository
  181. */
  182. public function do_pullAction() {
  183. $this->init();
  184. if (sly_request('cancel', 'string', '')) {
  185. $this->info(t('mercurial_cancel_message'));
  186. $this->indexAction();
  187. }
  188. else {
  189. $source = sly_request('source', 'string', '');
  190. $options = sly_request('options', 'array', array());
  191. $repositoryID = sly_request('id', 'int', 0);
  192. $repository = Mercurial_Provider::getRepository($repositoryID);
  193. $cache = Mercurial_Cache::getCache();
  194. try {
  195. if (empty($source)) throw new Hg_Exception(t('mercurial_repository_pull_error_message_source'));
  196. $command = $repository->setPull($options, $source);
  197. $output = $command->getOutput('external', 'paragraph');
  198. $cache->delete('dashboard.info', 'repository.' . $repositoryID);
  199. $this->info(t('mercurial_repository_pull_success_message'));
  200. $this->setView('console.phtml', compact(array('command', 'output', 'options')));
  201. }
  202. catch (Hg_Exception $exception) {
  203. $this->warn($exception->getMessage());
  204. $this->pullAction();
  205. }
  206. }
  207. }
  208. /**
  209. * Controller update view
  210. */
  211. public function updateAction() {
  212. $this->init();
  213. $repositoryID = sly_request('id', 'int', 0);
  214. $repository = Mercurial_Provider::getRepository($repositoryID);
  215. $changesets = $repository->getChangesets();
  216. $this->setView('update.phtml', compact(array('changesets', 'repository')));
  217. }
  218. /**
  219. * Controller update action
  220. *
  221. * @throws <Hg_Exception> Thrown Exception when unable to edit repository
  222. */
  223. public function do_updateAction() {
  224. $this->init();
  225. if (sly_request('cancel', 'string', '')) {
  226. $this->info(t('mercurial_cancel_message'));
  227. $this->indexAction();
  228. }
  229. else {
  230. $options = sly_request('options', 'array', array());
  231. $repositoryID = sly_request('id', 'int', 0);
  232. $changeset = sly_request('changeset', 'string', '');
  233. $repository = Mercurial_Provider::getRepository($repositoryID);
  234. $cache = Mercurial_Cache::getCache();
  235. try {
  236. if ($changeset) {
  237. $options['rev'] = $changeset;
  238. }
  239. elseif (empty($changeset)) {
  240. throw new Hg_Exception(t('mercurial_repository_update_error_message_changeset'));
  241. }
  242. $command = $repository->setUpdate($options);
  243. $output = $command->getOutput('external', 'paragraph');
  244. $cache->delete('dashboard.info', 'repository.' . $repositoryID);
  245. $this->info(t('mercurial_repository_update_success_message'));
  246. $this->setView('console.phtml', compact(array('command', 'output', 'options')));
  247. }
  248. catch (Hg_Exception $exception) {
  249. $this->warn($exception->getMessage());
  250. $this->updateAction();
  251. }
  252. }
  253. }
  254. /**
  255. * Controller incoming view
  256. */
  257. public function incomingAction() {
  258. $this->init();
  259. $repositoryID = sly_request('id', 'int', 0);
  260. $repository = Mercurial_Provider::getRepository($repositoryID);
  261. $this->setView('incoming.phtml', compact(array('repository')));
  262. }
  263. /**
  264. * Controller incoming action
  265. *
  266. * @throws <Hg_Exception> Thrown Exception when unable to edit repository
  267. */
  268. public function do_incomingAction() {
  269. $this->init();
  270. if (sly_request('cancel', 'string', '')) {
  271. $this->info(t('mercurial_cancel_message'));
  272. $this->indexAction();
  273. }
  274. else {
  275. $source = sly_request('source', 'string', '');
  276. $options = sly_request('options', 'array', array());
  277. $repositoryID = sly_request('id', 'int', 0);
  278. $repository = Mercurial_Provider::getRepository($repositoryID);
  279. try {
  280. if (empty($source)) throw new Hg_Exception(t('mercurial_repository_incoming_error_message_source'));
  281. $command = $repository->getIncoming($options, $source);
  282. $output = $command->getOutput('external', 'paragraph');
  283. $this->info(t('mercurial_repository_incoming_success_message'));
  284. $this->setView('console.phtml', compact(array('command', 'output', 'options')));
  285. }
  286. catch (Hg_Exception $exception) {
  287. $this->warn($exception->getMessage());
  288. $this->incomingAction();
  289. }
  290. }
  291. }
  292. /**
  293. * Controller outgoing view
  294. */
  295. public function outgoingAction() {
  296. $this->init();
  297. $repositoryID = sly_request('id', 'int', 0);
  298. $repository = Mercurial_Provider::getRepository($repositoryID);
  299. $this->setView('outgoing.phtml', compact(array('repository')));
  300. }
  301. /**
  302. * Controller outgoing action
  303. *
  304. * @throws <Hg_Exception> Thrown Exception when unable to edit repository
  305. */
  306. public function do_outgoingAction() {
  307. $this->init();
  308. if (sly_request('cancel', 'string', '')) {
  309. $this->info(t('mercurial_cancel_message'));
  310. $this->indexAction();
  311. }
  312. else {
  313. $options = sly_request('options', 'array', array());
  314. $repositoryID = sly_request('id', 'int', 0);
  315. $repository = Mercurial_Provider::getRepository($repositoryID);
  316. try {
  317. $command = $repository->getOutgoing($options);
  318. $output = $command->getOutput('external', 'paragraph');
  319. $this->info(t('mercurial_repository_outgoing_success_message'));
  320. $this->setView('console.phtml', compact(array('command', 'output', 'options')));
  321. }
  322. catch (Hg_Exception $exception) {
  323. $this->warn($exception->getMessage());
  324. $this->outgoingAction();
  325. }
  326. }
  327. }
  328. /**
  329. * Controller status view
  330. */
  331. public function statusAction() {
  332. $this->init();
  333. $repositoryID = sly_request('id', 'int', 0);
  334. $repository = Mercurial_Provider::getRepository($repositoryID);
  335. $changesets = $repository->getChangesets();
  336. $this->setView('status.phtml', compact(array('changesets', 'repository')));
  337. }
  338. /**
  339. * Controller status action
  340. *
  341. * @throws <Hg_Exception> Thrown Exception when unable to edit repository
  342. */
  343. public function do_statusAction() {
  344. $this->init();
  345. if (sly_request('cancel', 'string', '')) {
  346. $this->info(t('mercurial_cancel_message'));
  347. $this->indexAction();
  348. }
  349. else {
  350. $repositoryID = sly_request('id', 'int', 0);
  351. $options = sly_request('options', 'array', array());
  352. $changeset = sly_request('changeset', 'string', '');
  353. $repository = Mercurial_Provider::getRepository($repositoryID);
  354. try {
  355. if ($changeset) {
  356. $options['rev'] = $changeset;
  357. }
  358. $command = $repository->getStatus($options);
  359. $output = $command->getOutput('internal');
  360. $this->info(t('mercurial_repository_status_success_message'));
  361. $this->setView('console.phtml', compact(array('command', 'output', 'options')));
  362. }
  363. catch (Hg_Exception $exception) {
  364. $this->warn($exception->getMessage());
  365. $this->statusAction();
  366. }
  367. }
  368. }
  369. /**
  370. * Controller edit view
  371. */
  372. public function editAction() {
  373. $this->init();
  374. $repositoryID = sly_request('id', 'int', 0);
  375. $repository = Mercurial_Provider::getRepository($repositoryID);
  376. $this->setView('edit.phtml', compact(array('repository')));
  377. }
  378. /**
  379. * Controller edit action
  380. *
  381. * @throws <Hg_Exception> Thrown Exception when unable to edit repository
  382. */
  383. public function do_editAction() {
  384. $this->init();
  385. if (sly_request('cancel', 'string', '')) {
  386. $this->info(t('mercurial_cancel_message'));
  387. $this->indexAction();
  388. }
  389. else {
  390. $title = sly_request('title', 'string', '');
  391. $config = sly_request('config', 'string', '');
  392. $safeguard = sly_request('safeguard', 'int', 0);
  393. $repositoryID = sly_request('id', 'int', 0);
  394. $repository = Mercurial_Provider::getRepository($repositoryID);
  395. try {
  396. if (empty($title)) throw new Hg_Exception(t('mercurial_repository_edit_error_message_title'));
  397. $data = array(
  398. 'title' => $title
  399. );
  400. $repository->update($data);
  401. if ((boolean) $safeguard) {
  402. $repository->setConfig($config);
  403. }
  404. $this->info(t('mercurial_repository_edit_success_message'));
  405. $this->setView('edit.phtml', compact(array('repository')));
  406. } catch (Hg_Exception $exception) {
  407. $this->warn($exception->getMessage());
  408. $this->editAction();
  409. }
  410. }
  411. }
  412. /**
  413. * Controller delete view
  414. */
  415. public function deleteAction() {
  416. $this->init();
  417. $repositoryID = sly_request('id', 'int', 0);
  418. $repository = Mercurial_Provider::getRepository($repositoryID);
  419. $this->setView('delete.phtml', compact(array('repository')));
  420. }
  421. /**
  422. * Controller delete action
  423. *
  424. * @throws <Hg_Exception> Thrown exception when unable to delete repository
  425. */
  426. public function do_deleteAction() {
  427. $this->init();
  428. if (sly_request('cancel', 'string', '')) {
  429. $this->info(t('mercurial_cancel_message'));
  430. $this->indexAction();
  431. }
  432. else {
  433. $repositoryID = sly_request('id', 'int', 0);
  434. $deleteFolder = sly_request('deleteFolder', 'int', 0);
  435. $repository = Mercurial_Provider::getRepository($repositoryID);
  436. try {
  437. if ((boolean) $deleteFolder) {
  438. $directory = $repository->getDirectory();
  439. $folders = explode(DIRECTORY_SEPARATOR, $directory);
  440. $identifier = array_pop($folders);
  441. $addonService = sly_Service_Factory::getAddOnService();
  442. $pluginService = sly_Service_Factory::getPluginService();
  443. if ($directory === SLY_BASE) {
  444. throw new Hg_Exception(t('mercurial_repository_delete_error_message_core_deletion_forbidden'));
  445. }
  446. if ($addonService->isInstalled($identifier)) {
  447. $deleteFolder = false;
  448. }
  449. else {
  450. foreach ($addonService->getRegisteredAddons() as $addon) {
  451. foreach ($pluginService->getInstalledPlugins($addon) as $plugin) {
  452. if ($plugin === $identifier) {
  453. $deleteFolder = false;
  454. }
  455. }
  456. }
  457. }
  458. if ($deleteFolder) {
  459. $repository->delete($deleteFolder);
  460. }
  461. else {
  462. throw new Hg_Exception(t('mercurial_repository_delete_error_message_active_or_installed'));
  463. }
  464. }
  465. else {
  466. $repository->delete();
  467. }
  468. $this->info(t('mercurial_repository_delete_success_message'));
  469. $this->indexAction();
  470. }
  471. catch (Hg_Exception $exception) {
  472. $this->warn($exception->getMessage());
  473. $this->deleteAction();
  474. }
  475. }
  476. }
  477. /**
  478. * Check permission
  479. *
  480. * @param <string> $action Action parameter
  481. *
  482. * @return <boolean> State if user has permission to access this controller
  483. */
  484. public function checkPermission($action) {
  485. $user = sly_Util_User::getCurrentUser();
  486. return $user->isAdmin() || $user->hasRight('mercurial[]');
  487. }
  488. /**
  489. * Get view folder
  490. *
  491. * @return <string> Path to view folder
  492. */
  493. protected function getViewFolder() {
  494. return MERCURIAL_PATH . 'views/';
  495. }
  496. /**
  497. * Render layout
  498. */
  499. protected function renderLayout() {
  500. $layout = $this->getLayout();
  501. $layout->addCSSFile('../data/dyn/public/mercurial/css/mercurial.css');
  502. $layout->addJavaScriptFile('../data/dyn/public/mercurial/js/jquery-ui.min.js');
  503. $layout->addJavaScriptFile('../data/dyn/public/mercurial/js/jquery.jstree.min.js');
  504. $layout->addJavaScriptFile('../data/dyn/public/mercurial/js/mercurial.min.js');
  505. $layout->pageHeader(t('mercurial_title'));
  506. }
  507. /**
  508. * Get backend layout
  509. *
  510. * @return <sly_Layout_Backend> Returns the backend layout
  511. */
  512. protected function getLayout() {
  513. return sly_Core::getLayout();
  514. }
  515. /**
  516. * Set view
  517. *
  518. * @param <string> $view View to render
  519. * @param <array> $compacts Array of variables in compact
  520. */
  521. protected function setView($view, array $compacts = array()) {
  522. print $this->render($view, $compacts);
  523. }
  524. /**
  525. * Print info messages
  526. *
  527. * @param <string> $message Printable info message
  528. */
  529. protected function info($message) {
  530. print sly_Helper_Message::info($message);
  531. }
  532. /**
  533. * Print warning messages
  534. *
  535. * @param <string> $message Printable warning message
  536. */
  537. protected function warn($message) {
  538. print sly_Helper_Message::warn($message);
  539. }
  540. }