/lib/sly/Controller/Mercurial.php
PHP | 639 lines | 408 code | 99 blank | 132 comment | 46 complexity | 2f7c67f291543079d32385f68b1e60cc MD5 | raw file
- <?php
- /*
- * Copyright (c) 2012, MEDIASTUTTGART, http://mediastuttgart.de
- *
- * This file is released under the terms of the MIT license.
- * You can find the complete text in the attached LICENSE
- * file or online at:
- *
- * http://www.opensource.org/licenses/mit-license.php
- */
- /**
- * Mercurial controller
- */
- class sly_Controller_Mercurial extends sly_Controller_Backend implements sly_Controller_Interface {
- private $init;
- /**
- * Init controller
- */
- protected function init() {
- if ($this->init) return;
- $this->init = true;
- $this->renderLayout();
- }
- /**
- * Controller index view
- */
- public function indexAction() {
- $this->init();
- $this->getLayout()->setBodyAttr('class', 'mercurial-index-view');
- $mercurial = Hg::getService();
- $repositories = Mercurial_Provider::getRepositories();
- $this->setView('index.phtml', compact(array('repositories', 'mercurial')));
- }
- /**
- * Controller clone view
- */
- public function createAction() {
- $this->init();
- $this->setView('clone.phtml');
- }
- /**
- * Controller clone action
- *
- * @throws <Hg_Exception> Thrown Exception when unable to clone repository
- */
- public function do_createAction() {
- $this->init();
- if (sly_request('cancel', 'string', '')) {
- $this->info(t('mercurial_cancel_message'));
- $this->indexAction();
- }
- else {
- $title = sly_request('title', 'string', '');
- $path = sly_request('path', 'string', '');
- $directory = sly_request('directory', 'string', '');
- $source = sly_request('source', 'string', '');
- $options = sly_request('options', 'array', array());
- $directory = substr($path, strlen(SLY_BASE), strlen($path)) . DIRECTORY_SEPARATOR . $directory;
- try {
- if (empty($title)) throw new Hg_Exception(t('mercurial_repository_clone_error_message_description'));
- if (empty($path)) throw new Hg_Exception(t('mercurial_repository_clone_error_message_path'));
- if (empty($directory)) throw new Hg_Exception(t('mercurial_repository_clone_error_message_directory'));
- if (empty($source)) throw new Hg_Exception(t('mercurial_repository_clone_error_message_source'));
- $data = array(
- 'title' => $title,
- 'directory' => $directory,
- 'source' => $source,
- 'options' => $options
- );
- $container = new Mercurial_Model_Repository_Container($data);
- $service = Mercurial_Service::getService('hg');
- $command = $service->setClone($container);
- $output = $command->getOutput('external', 'paragraph');
- $container->transform();
- $this->info(t('mercurial_repository_clone_success_message'));
- $this->setView('console.phtml', compact(array('command', 'output', 'options')));
- }
- catch (Hg_Exception $exception) {
- $this->warn($exception->getMessage());
- $this->createAction();
- }
- }
- }
- /**
- * Controller add view
- */
- public function addAction() {
- $this->init();
- $this->setView('add.phtml');
- }
- /**
- * Controller add action
- *
- * @throws <Hg_Exception> Thrown Exception when unable to add repository
- */
- public function do_addAction() {
- $this->init();
- if (sly_request('cancel', 'string', '')) {
- $this->info(t('mercurial_cancel_message'));
- $this->indexAction();
- }
- else {
- $title = sly_request('title', 'string', '');
- $directory = sly_request('directory', 'string', '');
- $options = sly_request('options', 'array', array());
- $directory = substr($directory, strlen(SLY_BASE), strlen($directory));
- try {
- if (empty($title)) throw new Hg_Exception(t('mercurial_repository_add_error_message_description'));
- if (empty($directory)) throw new Hg_Exception(t('mercurial_repository_add_error_message_directory'));
- $data = array(
- 'title' => $title,
- 'directory' => $directory,
- 'options' => $options
- );
- $container = new Mercurial_Model_Repository_Container($data);
- $service = Mercurial_Service::getService('hg', $container->getDirectory());
- $command = $service->setAdd($container);
- $container->transform();
- $this->info(t('mercurial_repository_add_success_message'));
- $this->indexAction();
- }
- catch (Hg_Exception $exception) {
- $this->warn($exception->getMessage());
- $this->addAction();
- }
- }
- }
- /**
- * Oneclick update view
- */
- public function oneclickAction() {
- $this->init();
- if (sly_request('cancel', 'string', '')) {
- $this->info(t('mercurial_cancel_message'));
- $this->indexAction();
- }
- else {
- $repositories = array();
- foreach (Mercurial_Provider::getRepositories() as $repository) {
- $repositories[$repository->getID()] = $repository->getTitle();
- }
- $this->setView('oneclick.phtml', compact(array('repositories')));
- }
- }
- /**
- * Oneclick update action
- */
- public function do_oneclickAction() {
- $this->init();
- if (sly_request('cancel', 'string', '')) {
- $this->info(t('mercurial_cancel_message'));
- $this->indexAction();
- }
- else {
- $repos = sly_request('repositories', 'array', array());
- $options = sly_request('options', 'array', array());
- if (empty($repos)) {
- $this->warn('Bitte wählen Sie mindestens ein Repository aus.');
- } else {
- $repositories = array();
- foreach ($repos as $repositoryID) {
- $repositories[] = Mercurial_Provider::getRepository($repositoryID);
- }
- }
- $this->setView('queue.phtml', compact(array('repositories', 'options')));
- }
- }
- /**
- * Controller pull view
- */
- public function pullAction() {
- $this->init();
- $repositoryID = sly_request('id', 'int', 0);
- $repository = Mercurial_Provider::getRepository($repositoryID);
- $this->setView('pull.phtml', compact(array('repository')));
- }
- /**
- * Controller pull action
- *
- * @throws <Hg_Exception> Thrown Exception when unable to edit repository
- */
- public function do_pullAction() {
- $this->init();
- if (sly_request('cancel', 'string', '')) {
- $this->info(t('mercurial_cancel_message'));
- $this->indexAction();
- }
- else {
- $source = sly_request('source', 'string', '');
- $options = sly_request('options', 'array', array());
- $repositoryID = sly_request('id', 'int', 0);
- $repository = Mercurial_Provider::getRepository($repositoryID);
- $cache = Mercurial_Cache::getCache();
- try {
- if (empty($source)) throw new Hg_Exception(t('mercurial_repository_pull_error_message_source'));
- $command = $repository->setPull($options, $source);
- $output = $command->getOutput('external', 'paragraph');
- $cache->delete('dashboard.info', 'repository.' . $repositoryID);
- $this->info(t('mercurial_repository_pull_success_message'));
- $this->setView('console.phtml', compact(array('command', 'output', 'options')));
- }
- catch (Hg_Exception $exception) {
- $this->warn($exception->getMessage());
- $this->pullAction();
- }
- }
- }
- /**
- * Controller update view
- */
- public function updateAction() {
- $this->init();
- $repositoryID = sly_request('id', 'int', 0);
- $repository = Mercurial_Provider::getRepository($repositoryID);
- $changesets = $repository->getChangesets();
- $this->setView('update.phtml', compact(array('changesets', 'repository')));
- }
- /**
- * Controller update action
- *
- * @throws <Hg_Exception> Thrown Exception when unable to edit repository
- */
- public function do_updateAction() {
- $this->init();
- if (sly_request('cancel', 'string', '')) {
- $this->info(t('mercurial_cancel_message'));
- $this->indexAction();
- }
- else {
- $options = sly_request('options', 'array', array());
- $repositoryID = sly_request('id', 'int', 0);
- $changeset = sly_request('changeset', 'string', '');
- $repository = Mercurial_Provider::getRepository($repositoryID);
- $cache = Mercurial_Cache::getCache();
- try {
- if ($changeset) {
- $options['rev'] = $changeset;
- }
- elseif (empty($changeset)) {
- throw new Hg_Exception(t('mercurial_repository_update_error_message_changeset'));
- }
- $command = $repository->setUpdate($options);
- $output = $command->getOutput('external', 'paragraph');
- $cache->delete('dashboard.info', 'repository.' . $repositoryID);
- $this->info(t('mercurial_repository_update_success_message'));
- $this->setView('console.phtml', compact(array('command', 'output', 'options')));
- }
- catch (Hg_Exception $exception) {
- $this->warn($exception->getMessage());
- $this->updateAction();
- }
- }
- }
- /**
- * Controller incoming view
- */
- public function incomingAction() {
- $this->init();
- $repositoryID = sly_request('id', 'int', 0);
- $repository = Mercurial_Provider::getRepository($repositoryID);
- $this->setView('incoming.phtml', compact(array('repository')));
- }
- /**
- * Controller incoming action
- *
- * @throws <Hg_Exception> Thrown Exception when unable to edit repository
- */
- public function do_incomingAction() {
- $this->init();
- if (sly_request('cancel', 'string', '')) {
- $this->info(t('mercurial_cancel_message'));
- $this->indexAction();
- }
- else {
- $source = sly_request('source', 'string', '');
- $options = sly_request('options', 'array', array());
- $repositoryID = sly_request('id', 'int', 0);
- $repository = Mercurial_Provider::getRepository($repositoryID);
- try {
- if (empty($source)) throw new Hg_Exception(t('mercurial_repository_incoming_error_message_source'));
- $command = $repository->getIncoming($options, $source);
- $output = $command->getOutput('external', 'paragraph');
- $this->info(t('mercurial_repository_incoming_success_message'));
- $this->setView('console.phtml', compact(array('command', 'output', 'options')));
- }
- catch (Hg_Exception $exception) {
- $this->warn($exception->getMessage());
- $this->incomingAction();
- }
- }
- }
- /**
- * Controller outgoing view
- */
- public function outgoingAction() {
- $this->init();
- $repositoryID = sly_request('id', 'int', 0);
- $repository = Mercurial_Provider::getRepository($repositoryID);
- $this->setView('outgoing.phtml', compact(array('repository')));
- }
- /**
- * Controller outgoing action
- *
- * @throws <Hg_Exception> Thrown Exception when unable to edit repository
- */
- public function do_outgoingAction() {
- $this->init();
- if (sly_request('cancel', 'string', '')) {
- $this->info(t('mercurial_cancel_message'));
- $this->indexAction();
- }
- else {
- $options = sly_request('options', 'array', array());
- $repositoryID = sly_request('id', 'int', 0);
- $repository = Mercurial_Provider::getRepository($repositoryID);
- try {
- $command = $repository->getOutgoing($options);
- $output = $command->getOutput('external', 'paragraph');
- $this->info(t('mercurial_repository_outgoing_success_message'));
- $this->setView('console.phtml', compact(array('command', 'output', 'options')));
- }
- catch (Hg_Exception $exception) {
- $this->warn($exception->getMessage());
- $this->outgoingAction();
- }
- }
- }
- /**
- * Controller status view
- */
- public function statusAction() {
- $this->init();
- $repositoryID = sly_request('id', 'int', 0);
- $repository = Mercurial_Provider::getRepository($repositoryID);
- $changesets = $repository->getChangesets();
- $this->setView('status.phtml', compact(array('changesets', 'repository')));
- }
- /**
- * Controller status action
- *
- * @throws <Hg_Exception> Thrown Exception when unable to edit repository
- */
- public function do_statusAction() {
- $this->init();
- if (sly_request('cancel', 'string', '')) {
- $this->info(t('mercurial_cancel_message'));
- $this->indexAction();
- }
- else {
- $repositoryID = sly_request('id', 'int', 0);
- $options = sly_request('options', 'array', array());
- $changeset = sly_request('changeset', 'string', '');
- $repository = Mercurial_Provider::getRepository($repositoryID);
- try {
- if ($changeset) {
- $options['rev'] = $changeset;
- }
- $command = $repository->getStatus($options);
- $output = $command->getOutput('internal');
- $this->info(t('mercurial_repository_status_success_message'));
- $this->setView('console.phtml', compact(array('command', 'output', 'options')));
- }
- catch (Hg_Exception $exception) {
- $this->warn($exception->getMessage());
- $this->statusAction();
- }
- }
- }
- /**
- * Controller edit view
- */
- public function editAction() {
- $this->init();
- $repositoryID = sly_request('id', 'int', 0);
- $repository = Mercurial_Provider::getRepository($repositoryID);
- $this->setView('edit.phtml', compact(array('repository')));
- }
- /**
- * Controller edit action
- *
- * @throws <Hg_Exception> Thrown Exception when unable to edit repository
- */
- public function do_editAction() {
- $this->init();
- if (sly_request('cancel', 'string', '')) {
- $this->info(t('mercurial_cancel_message'));
- $this->indexAction();
- }
- else {
- $title = sly_request('title', 'string', '');
- $config = sly_request('config', 'string', '');
- $safeguard = sly_request('safeguard', 'int', 0);
- $repositoryID = sly_request('id', 'int', 0);
- $repository = Mercurial_Provider::getRepository($repositoryID);
- try {
- if (empty($title)) throw new Hg_Exception(t('mercurial_repository_edit_error_message_title'));
- $data = array(
- 'title' => $title
- );
- $repository->update($data);
- if ((boolean) $safeguard) {
- $repository->setConfig($config);
- }
- $this->info(t('mercurial_repository_edit_success_message'));
- $this->setView('edit.phtml', compact(array('repository')));
- } catch (Hg_Exception $exception) {
- $this->warn($exception->getMessage());
- $this->editAction();
- }
- }
- }
- /**
- * Controller delete view
- */
- public function deleteAction() {
- $this->init();
- $repositoryID = sly_request('id', 'int', 0);
- $repository = Mercurial_Provider::getRepository($repositoryID);
- $this->setView('delete.phtml', compact(array('repository')));
- }
- /**
- * Controller delete action
- *
- * @throws <Hg_Exception> Thrown exception when unable to delete repository
- */
- public function do_deleteAction() {
- $this->init();
- if (sly_request('cancel', 'string', '')) {
- $this->info(t('mercurial_cancel_message'));
- $this->indexAction();
- }
- else {
- $repositoryID = sly_request('id', 'int', 0);
- $deleteFolder = sly_request('deleteFolder', 'int', 0);
- $repository = Mercurial_Provider::getRepository($repositoryID);
- try {
- if ((boolean) $deleteFolder) {
- $directory = $repository->getDirectory();
- $folders = explode(DIRECTORY_SEPARATOR, $directory);
- $identifier = array_pop($folders);
- $addonService = sly_Service_Factory::getAddOnService();
- $pluginService = sly_Service_Factory::getPluginService();
- if ($directory === SLY_BASE) {
- throw new Hg_Exception(t('mercurial_repository_delete_error_message_core_deletion_forbidden'));
- }
- if ($addonService->isInstalled($identifier)) {
- $deleteFolder = false;
- }
- else {
- foreach ($addonService->getRegisteredAddons() as $addon) {
- foreach ($pluginService->getInstalledPlugins($addon) as $plugin) {
- if ($plugin === $identifier) {
- $deleteFolder = false;
- }
- }
- }
- }
- if ($deleteFolder) {
- $repository->delete($deleteFolder);
- }
- else {
- throw new Hg_Exception(t('mercurial_repository_delete_error_message_active_or_installed'));
- }
- }
- else {
- $repository->delete();
- }
- $this->info(t('mercurial_repository_delete_success_message'));
- $this->indexAction();
- }
- catch (Hg_Exception $exception) {
- $this->warn($exception->getMessage());
- $this->deleteAction();
- }
- }
- }
- /**
- * Check permission
- *
- * @param <string> $action Action parameter
- *
- * @return <boolean> State if user has permission to access this controller
- */
- public function checkPermission($action) {
- $user = sly_Util_User::getCurrentUser();
- return $user->isAdmin() || $user->hasRight('mercurial[]');
- }
- /**
- * Get view folder
- *
- * @return <string> Path to view folder
- */
- protected function getViewFolder() {
- return MERCURIAL_PATH . 'views/';
- }
- /**
- * Render layout
- */
- protected function renderLayout() {
- $layout = $this->getLayout();
- $layout->addCSSFile('../data/dyn/public/mercurial/css/mercurial.css');
- $layout->addJavaScriptFile('../data/dyn/public/mercurial/js/jquery-ui.min.js');
- $layout->addJavaScriptFile('../data/dyn/public/mercurial/js/jquery.jstree.min.js');
- $layout->addJavaScriptFile('../data/dyn/public/mercurial/js/mercurial.min.js');
- $layout->pageHeader(t('mercurial_title'));
- }
- /**
- * Get backend layout
- *
- * @return <sly_Layout_Backend> Returns the backend layout
- */
- protected function getLayout() {
- return sly_Core::getLayout();
- }
- /**
- * Set view
- *
- * @param <string> $view View to render
- * @param <array> $compacts Array of variables in compact
- */
- protected function setView($view, array $compacts = array()) {
- print $this->render($view, $compacts);
- }
- /**
- * Print info messages
- *
- * @param <string> $message Printable info message
- */
- protected function info($message) {
- print sly_Helper_Message::info($message);
- }
- /**
- * Print warning messages
- *
- * @param <string> $message Printable warning message
- */
- protected function warn($message) {
- print sly_Helper_Message::warn($message);
- }
- }