From b33838be655b1c86d1b389eeba9506048076dc13 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Willms <tleilax+github@gmail.com> Date: Fri, 14 Jan 2022 14:33:59 +0100 Subject: [PATCH] fixes #21 --- TracToGitlabPlugin.php | 10 +++ controllers/dashboard.php | 1 + controllers/translations.php | 82 +++++++++++++++++++ ...20_setup_plugin.php => 1_setup_plugin.php} | 17 +++- migrations/2_setup_transifex.php | 43 ++++++++++ views/translations/index.php | 16 ++++ 6 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 controllers/translations.php rename migrations/{20210720_setup_plugin.php => 1_setup_plugin.php} (82%) create mode 100644 migrations/2_setup_transifex.php create mode 100644 views/translations/index.php diff --git a/TracToGitlabPlugin.php b/TracToGitlabPlugin.php index 29b9a61..d01ba49 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 f9344f1..a5d7ade 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 0000000..b37e6a7 --- /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 5ee3725..06cdb64 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 0000000..1939142 --- /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 0000000..925c932 --- /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> -- GitLab