Add a console tool
Adds a console tool for phpMyAdmin using the symfony/console component and moves the advisor2po script to the the console tool. Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
This commit is contained in:
parent
ddb298bb58
commit
adea61314e
@ -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": {
|
||||
|
||||
112
libraries/classes/Command/AdvisoryRulesCommand.php
Normal file
112
libraries/classes/Command/AdvisoryRulesCommand.php
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,77 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Script to parse advisor rules and output them as Gettext POT formatted
|
||||
* strings for translation.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
if (! defined('ROOT_PATH')) {
|
||||
define('ROOT_PATH', dirname(__DIR__) . DIRECTORY_SEPARATOR);
|
||||
}
|
||||
|
||||
use PhpMyAdmin\Advisor;
|
||||
|
||||
$messages = array();
|
||||
$locations = array();
|
||||
|
||||
/**
|
||||
* Processes single advisor message and stores data in global array.
|
||||
*/
|
||||
function add_message($file, $rules, $idx, $type) {
|
||||
global $messages, $locations;
|
||||
// Get message text
|
||||
if ($type == 'justification') {
|
||||
$msgs = Advisor::splitJustification($rules['rules'][$idx]);
|
||||
$msg = $msgs[0];
|
||||
} else {
|
||||
$msg = $rules['rules'][$idx][$type];
|
||||
}
|
||||
$line = $file . ':' . $rules['lines'][$idx][$type];
|
||||
// Avoid duplicate mesages
|
||||
$pos = array_search($msg, $messages);
|
||||
if ($pos === false) {
|
||||
$messages[] = $msg;
|
||||
$locations[] = array($line);
|
||||
} else {
|
||||
$locations[$pos][] = $line;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints message at given location as Gettext string for translation.
|
||||
*/
|
||||
function print_message($idx) {
|
||||
global $messages, $locations;
|
||||
echo "\n";
|
||||
echo '#: ' . implode(' ', $locations[$idx]);
|
||||
echo "\n";
|
||||
if (strstr($messages[$idx], '%') !== false) {
|
||||
echo '#, php-format';
|
||||
echo "\n";
|
||||
}
|
||||
echo 'msgid "' . addcslashes(Advisor::escapePercent($messages[$idx]), '"\\') . '"';
|
||||
echo "\n";
|
||||
echo 'msgstr ""';
|
||||
echo "\n";
|
||||
}
|
||||
|
||||
define('PHPMYADMIN', 1);
|
||||
require_once 'libraries/vendor_config.php';
|
||||
require_once AUTOLOAD_FILE;
|
||||
|
||||
$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) {
|
||||
add_message($file, $rules, $idx, 'name');
|
||||
add_message($file, $rules, $idx, 'issue');
|
||||
add_message($file, $rules, $idx, 'recommendation');
|
||||
add_message($file, $rules, $idx, 'justification');
|
||||
}
|
||||
}
|
||||
|
||||
foreach($messages as $idx => $rule) {
|
||||
print_message($idx);
|
||||
}
|
||||
19
scripts/console
Executable file
19
scripts/console
Executable file
@ -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();
|
||||
@ -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`
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user