Skip to content

Commit

Permalink
Merge pull request #15505 from mauriciofauth/scripts-console
Browse files Browse the repository at this point in the history
Add a console tool
  • Loading branch information
MauricioFauth committed Oct 24, 2019
2 parents ace830d + adea613 commit f0d4122
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 78 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -90,6 +90,7 @@
"pragmarx/google2fa-qrcode": "^1.0.1",
"samyoul/u2f-php-server": "^1.1",
"squizlabs/php_codesniffer": "^3.0",
"symfony/console": "^4.3",
"tecnickcom/tcpdf": "^6.3"
},
"extra": {
Expand Down
112 changes: 112 additions & 0 deletions libraries/classes/Command/AdvisoryRulesCommand.php
@@ -0,0 +1,112 @@
<?php
/**
* Translates advisory rules to Gettext format
*
* @package PhpMyAdmin\Command
*/
declare(strict_types=1);

namespace PhpMyAdmin\Command;

use PhpMyAdmin\Advisor;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Translates advisory rules to Gettext format
*
* @package PhpMyAdmin\Command
*/
class AdvisoryRulesCommand extends Command
{
/** @var string */
protected static $defaultName = 'po:advisory-rules';

/** @var array */
private $messages = [];

/** @var array */
private $locations = [];

/**
* @return void
*/
protected function configure(): void
{
$this->setDescription('Translates advisory rules to Gettext format');
$this->setHelp(
'This command parses advisory rules and output them'
. ' as Gettext POT formatted strings for translation.'
);
}

/**
* @param InputInterface $input input
* @param OutputInterface $output output
*
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output): void
{
$ruleFiles = [];
$ruleFiles[Advisor::GENERIC_RULES_FILE] = Advisor::parseRulesFile(
Advisor::GENERIC_RULES_FILE
);
$ruleFiles[Advisor::BEFORE_MYSQL80003_RULES_FILE] = Advisor::parseRulesFile(
Advisor::BEFORE_MYSQL80003_RULES_FILE
);

foreach ($ruleFiles as $file => $rules) {
foreach ($rules['rules'] as $idx => $rule) {
$this->addMessage($file, $rules, $idx, 'name');
$this->addMessage($file, $rules, $idx, 'issue');
$this->addMessage($file, $rules, $idx, 'recommendation');
$this->addMessage($file, $rules, $idx, 'justification');
}
}

foreach ($this->messages as $index => $message) {
$output->writeln('');
$output->write('#: ');
$output->writeln(implode(' ', $this->locations[$index]));
if (strstr($this->messages[$index], '%') !== false) {
$output->writeln('#, php-format');
}
$output->write('msgid "');
$output->write(addcslashes(
Advisor::escapePercent($this->messages[$index]),
'"\\'
));
$output->writeln('"');
$output->writeln('msgstr ""');
}
}

/**
* @param string $file file name
* @param array $rules rules array
* @param int $index rule index
* @param string $type rule type
*
* @return void
*/
private function addMessage(string $file, array $rules, int $index, string $type): void
{
if ($type == 'justification') {
$messages = Advisor::splitJustification($rules['rules'][$index]);
$message = $messages[0];
} else {
$message = $rules['rules'][$index][$type];
}
$line = $file . ':' . $rules['lines'][$index][$type];

$pos = array_search($message, $this->messages);
if ($pos === false) {
$this->messages[] = $message;
$this->locations[] = [$line];
} else {
$this->locations[$pos][] = $line;
}
}
}
77 changes: 0 additions & 77 deletions scripts/advisor2po

This file was deleted.

19 changes: 19 additions & 0 deletions scripts/console
@@ -0,0 +1,19 @@
#!/usr/bin/env php
<?php

use PhpMyAdmin\Command\AdvisoryRulesCommand;
use Symfony\Component\Console\Application;

if (! defined('ROOT_PATH')) {
define('ROOT_PATH', dirname(__DIR__) . DIRECTORY_SEPARATOR);
}

define('PHPMYADMIN', true);
require_once ROOT_PATH . 'libraries/vendor_config.php';
require_once AUTOLOAD_FILE;

$application = new Application('phpMyAdmin Console Tool');

$application->add(new AdvisoryRulesCommand());

$application->run();
2 changes: 1 addition & 1 deletion scripts/update-po
Expand Up @@ -37,7 +37,7 @@ php scripts/fix-po-twig
rm -rf twig-templates/

# Generate PHP code for advisor rules
php ./scripts/advisor2po >> po/phpmyadmin.pot
php scripts/console po:advisory-rules >> po/phpmyadmin.pot

ver=`sed -n "/PMA_VERSION', '/ s/.*PMA_VERSION', '\(.*\)'.*/\1/p" libraries/classes/Config.php`

Expand Down

0 comments on commit f0d4122

Please sign in to comment.