diff --git a/TracToGitlabPlugin.php b/TracToGitlabPlugin.php index 29b9a61e17dc02a71e379a2e78faebf4ba70bbc3..d01ba4972e6a71b5b9aceadc690a4247c08b7182 100644 --- a/TracToGitlabPlugin.php +++ b/TracToGitlabPlugin.php @@ -111,6 +111,16 @@ final class TracToGitlabPlugin extends StudIPPlugin implements StandardPlugin, S { $navigation = new Navigation(_('Dashboard'), PluginEngine::getURL($this, [], 'dashboard')); $navigation->setImage(Icon::create($this->getPluginURL() . '/assets/cardiogram.svg', Icon::ROLE_NAVIGATION)); + + $navigation->addSubnavigation( + 'dashboard', + new Navigation(_('Dashboard'), PluginEngine::getURL($this, [], 'dashboard')) + ); + $navigation->addSubnavigation( + 'translations', + new Navigation(_('Übersetzungen'), PluginEngine::getURL($this, [], 'translations')) + ); + Navigation::addItem('/gitlab-dashboard', $navigation); } } diff --git a/controllers/dashboard.php b/controllers/dashboard.php index f9344f112906171f252b01800f922feceab3d139..a5d7ade8fe2956454813590f20fc171e062fb977 100644 --- a/controllers/dashboard.php +++ b/controllers/dashboard.php @@ -15,6 +15,7 @@ final class DashboardController extends TracToGitlab\Controller { parent::before_filter($action, $args); + Navigation::activateItem('/gitlab-dashboard/dashboard'); PageLayout::setTitle(_('Übersicht der Issues in gitlab')); $this->setupSidebar(); diff --git a/controllers/translations.php b/controllers/translations.php new file mode 100644 index 0000000000000000000000000000000000000000..b37e6a72e412b766058469248578f0a2d9979394 --- /dev/null +++ b/controllers/translations.php @@ -0,0 +1,82 @@ +<?php +final class TranslationsController extends \TracToGitlab\Controller +{ + public function before_filter(&$action, &$args) + { + parent::before_filter($action, $args); + + Navigation::activateItem('/gitlab-dashboard/translations'); + PageLayout::setTitle(_('Status der Übersetzungen')); + } + + public function index_action() + { + $temp = $this->fetch('resources', [ + 'filter' => [ + 'project' => 'o:' . Config::get()->TRAC2GITLAB_TRANSIFEX_ORGANISATION . ':p:' . Config::get()->TRAC2GITLAB_TRANSIFEX_PROJECT + ] + ]); + $resources = []; + foreach ($temp as $r) { + $resources[$r['id']] = $r['attributes']['name']; + } + + $temp = $this->fetch('resource_language_stats', [ + 'filter' => [ + 'project' => 'o:' . Config::get()->TRAC2GITLAB_TRANSIFEX_ORGANISATION . ':p:' . Config::get()->TRAC2GITLAB_TRANSIFEX_PROJECT, + 'language' => 'l:en', + ] + ]); + $stats = []; + foreach ($temp as $s) { + $id = preg_replace('/:l:.*$/', '', $s['id']); + $name = $resources[$id]; + $stats[$name] = $s['attributes']['translated_strings'] / $s['attributes']['total_strings']; + } + + $this->stats = array_reverse($stats); + } + + private function fetch(...$args): array + { + $parameters = []; + if (is_array(end($args))) { + $parameters = array_pop($args); + } + + $url = Config::get()->TRAC2GITLAB_TRANSIFEX_ENDPOINT . implode('/', $args); + if ($parameters) { + $url .= '?' . http_build_query($parameters); + } + + $context = [ + 'http' => [ + 'method' => 'GET', + 'header' => 'Authorization: Bearer ' . Config::get()->TRAC2GITLAB_TRANSIFEX_TOKEN . "\r\n", + ] + ]; + + $response = file_get_contents($url, false, stream_context_create($context)); + if ($response === false) { + throw new Exception('Request failed'); + } + + $temp = $http_response_header[0]; + if (!preg_match('/^HTTP.*?\s(\d+)\s/', $temp, $match)) { + throw new Exception('Could not parse http status'); + } + $status = (int)$match[1]; + + if ($status !== 200) { + throw new Exception('Not implemented'); + } + + $data = json_decode($response, true); + if ($data === null) { + throw new Exception('Could not parse data'); + } + + return $data['data']; + } + +} diff --git a/migrations/20210720_setup_plugin.php b/migrations/1_setup_plugin.php similarity index 82% rename from migrations/20210720_setup_plugin.php rename to migrations/1_setup_plugin.php index 5ee37258b6ab212cc9c2127ff308058194205903..06cdb648c0107a703e340760af249d4bdcfe5f05 100644 --- a/migrations/20210720_setup_plugin.php +++ b/migrations/1_setup_plugin.php @@ -16,28 +16,28 @@ final class SetupPlugin extends Migration )"; DBManager::get()->exec($query); - Config::get()->create('TRAC2GITLAB_TRAC_URL', [ + $this->safelyCreateConfigEntry('TRAC2GITLAB_TRAC_URL', [ 'value' => '', 'type' => 'string', 'range' => 'global', 'section' => 'Trac2Gitlab', 'description' => 'URL zur Trac-Instanz', ]); - Config::get()->create('TRAC2GITLAB_GITLAB_URL', [ + $this->safelyCreateConfigEntry('TRAC2GITLAB_GITLAB_URL', [ 'value' => '', 'type' => 'string', 'range' => 'global', 'section' => 'Trac2Gitlab', 'description' => 'URL zur gitlab-Instanz', ]); - Config::get()->create('TRAC2GITLAB_GITLAB_TOKEN', [ + $this->safelyCreateConfigEntry('TRAC2GITLAB_GITLAB_TOKEN', [ 'value' => '', 'type' => 'string', 'range' => 'global', 'section' => 'Trac2Gitlab', 'description' => 'Token für die gitlab-Instanz', ]); - Config::get()->create('TRAC2GITLAB_GITLAB_PROJECT_ID', [ + $this->safelyCreateConfigEntry('TRAC2GITLAB_GITLAB_PROJECT_ID', [ 'value' => '', 'type' => 'integer', 'range' => 'global', @@ -56,4 +56,13 @@ final class SetupPlugin extends Migration $query = "DROP TABLE IF EXISTS `trac_to_gitlab`"; DBManager::get()->exec($query); } + + private function safelyCreateConfigEntry(string $name, array $data) + { + try { + Config::get()->create($name, $data); + } catch (Exception $e) { + + } + } } diff --git a/migrations/2_setup_transifex.php b/migrations/2_setup_transifex.php new file mode 100644 index 0000000000000000000000000000000000000000..1939142b7a778f6a43bbf9143b3fa71c81f16498 --- /dev/null +++ b/migrations/2_setup_transifex.php @@ -0,0 +1,43 @@ +<?php +final class SetupTransifex extends Migration +{ + public function up() + { + Config::get()->create('TRAC2GITLAB_TRANSIFEX_TOKEN', [ + 'value' => '', + 'type' => 'string', + 'range' => 'global', + 'section' => 'Trac2Gitlab', + 'description' => 'Token für transifex', + ]); + Config::get()->create('TRAC2GITLAB_TRANSIFEX_ENDPOINT', [ + 'value' => 'https://rest.api.transifex.com/', + 'type' => 'string', + 'range' => 'global', + 'section' => 'Trac2Gitlab', + 'description' => 'Endpunkt für transifex', + ]); + Config::get()->create('TRAC2GITLAB_TRANSIFEX_ORGANISATION', [ + 'value' => 'studip', + 'type' => 'string', + 'range' => 'global', + 'section' => 'Trac2Gitlab', + 'description' => 'Organisation für transifex', + ]); + Config::get()->create('TRAC2GITLAB_TRANSIFEX_PROJECT', [ + 'value' => 'studip', + 'type' => 'string', + 'range' => 'global', + 'section' => 'Trac2Gitlab', + 'description' => 'Project für transifex', + ]); + } + + public function down() + { + Config::get()->delete('TRAC2GITLAB_TRANSIFEX_TOKEN'); + Config::get()->delete('TRAC2GITLAB_TRANSIFEX_ENDPOINT'); + Config::get()->delete('TRAC2GITLAB_TRANSIFEX_ORGANISATION'); + Config::get()->delete('TRAC2GITLAB_TRANSIFEX_PROJECT'); + } +} diff --git a/views/translations/index.php b/views/translations/index.php new file mode 100644 index 0000000000000000000000000000000000000000..925c9323a2c1fd251338a9f95103f490d39854ae --- /dev/null +++ b/views/translations/index.php @@ -0,0 +1,16 @@ +<table class="default"> + <thead> + <tr> + <th><?= _('Version') ?></th> + <th><?= _('Status') ?></th> + </tr> + </thead> + <tbody> + <? foreach ($stats as $version => $status): ?> + <tr> + <td><?= htmlReady($version) ?></td> + <td><?= number_format($status * 100, 2, ',', '.') ?>%</td> + </tr> + <? endforeach; ?> + </tbody> +</table>