Skip to content

Commit f18b9d6

Browse files
authored
Adding command for deleting obsolete messages (#33)
* Adding command for deleting obsolete messages * code style * Added support for 'default' config * code style
1 parent 7c27749 commit f18b9d6

File tree

4 files changed

+198
-17
lines changed

4 files changed

+198
-17
lines changed

Catalogue/CatalogueManager.php

Lines changed: 99 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,65 @@ public function getMessages($locale, $domain)
8181
return $messages;
8282
}
8383

84+
/**
85+
* @param array $config {
86+
*
87+
* @var string $domain
88+
* @var string $locale
89+
* @var bool $isNew
90+
* @var bool $isObsolete
91+
* }
92+
*
93+
* @return CatalogueMessage[]
94+
*/
95+
public function findMessages(array $config = [])
96+
{
97+
$inputDomain = isset($config['domain']) ? $config['domain'] : null;
98+
$isNew = isset($config['isNew']) ? $config['isNew'] : null;
99+
$isObsolete = isset($config['isObsolete']) ? $config['isObsolete'] : null;
100+
101+
$messages = [];
102+
$catalogues = [];
103+
if (isset($config['locale'])) {
104+
$locale = $config['locale'];
105+
if (isset($this->catalogues[$locale])) {
106+
$catalogues = [$locale => $this->catalogues[$locale]];
107+
}
108+
} else {
109+
$catalogues = $this->catalogues;
110+
}
111+
112+
foreach ($catalogues as $locale => $catalogue) {
113+
$domains = $catalogue->getDomains();
114+
if (null !== $inputDomain) {
115+
$domains = [$inputDomain];
116+
}
117+
foreach ($domains as $domain) {
118+
foreach ($catalogue->all($domain) as $key => $text) {
119+
// Filter on new and obsolete
120+
if (null !== $isNew || null !== $isObsolete) {
121+
$notes = $this->getNotes($domain, $key, $catalogue);
122+
123+
if (null !== $isNew) {
124+
if ($isNew !== $this->hasNoteNew($notes)) {
125+
continue;
126+
}
127+
}
128+
if (null !== $isObsolete) {
129+
if ($isObsolete !== $this->hasNoteObsolete($notes)) {
130+
continue;
131+
}
132+
}
133+
}
134+
135+
$messages[] = new CatalogueMessage($this, $locale, $domain, $key, $text);
136+
}
137+
}
138+
}
139+
140+
return $messages;
141+
}
142+
84143
/**
85144
* @param string $domain
86145
* @param string $key
@@ -128,13 +187,8 @@ public function getSourceLocations($domain, $key)
128187
public function isNew($domain, $key)
129188
{
130189
$notes = $this->getNotes($domain, $key);
131-
foreach ($notes as $note) {
132-
if ($note['content'] === 'status:new') {
133-
return true;
134-
}
135-
}
136190

137-
return false;
191+
return $this->hasNoteNew($notes);
138192
}
139193

140194
/**
@@ -146,13 +200,8 @@ public function isNew($domain, $key)
146200
public function isObsolete($domain, $key)
147201
{
148202
$notes = $this->getNotes($domain, $key);
149-
foreach ($notes as $note) {
150-
if ($note['content'] === 'status:obsolete') {
151-
return true;
152-
}
153-
}
154203

155-
return false;
204+
return $this->hasNoteObsolete($notes);
156205
}
157206

158207
/**
@@ -161,16 +210,50 @@ public function isObsolete($domain, $key)
161210
*
162211
* @return array
163212
*/
164-
private function getNotes($domain, $key)
213+
private function getNotes($domain, $key, MessageCatalogue $catalogue = null)
165214
{
166-
/** @var MessageCatalogue $c */
167-
$c = reset($this->catalogues);
168-
$meta = $c->getMetadata($key, $domain);
215+
if (null === $catalogue) {
216+
/** @var MessageCatalogue $c */
217+
$catalogue = reset($this->catalogues);
218+
}
219+
$meta = $catalogue->getMetadata($key, $domain);
169220

170221
if (!isset($meta['notes'])) {
171222
return [];
172223
}
173224

174225
return $meta['notes'];
175226
}
227+
228+
/**
229+
* @param array $notes
230+
*
231+
* @return bool
232+
*/
233+
private function hasNoteObsolete(array $notes)
234+
{
235+
foreach ($notes as $note) {
236+
if ($note['content'] === 'status:obsolete') {
237+
return true;
238+
}
239+
}
240+
241+
return false;
242+
}
243+
244+
/**
245+
* @param array $notes
246+
*
247+
* @return bool
248+
*/
249+
private function hasNoteNew(array $notes)
250+
{
251+
foreach ($notes as $note) {
252+
if ($note['content'] === 'status:new') {
253+
return true;
254+
}
255+
}
256+
257+
return false;
258+
}
176259
}

Command/DeleteObsoleteCommand.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Translation\Bundle\Command;
13+
14+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
15+
use Symfony\Component\Console\Helper\ProgressBar;
16+
use Symfony\Component\Console\Input\InputArgument;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Output\OutputInterface;
19+
use Symfony\Component\Console\Question\ConfirmationQuestion;
20+
use Symfony\Component\DependencyInjection\ContainerInterface;
21+
use Symfony\Component\Finder\Finder;
22+
use Translation\Bundle\Service\StorageService;
23+
24+
class DeleteObsoleteCommand extends ContainerAwareCommand
25+
{
26+
/**
27+
* @var ContainerInterface
28+
*/
29+
private $container;
30+
31+
protected function configure()
32+
{
33+
$this
34+
->setName('translation:delete-obsolete')
35+
->setDescription('Delete all translations marked as obsolete.')
36+
->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
37+
->addArgument('locale', InputArgument::OPTIONAL, 'The locale ot use. If omitted, we use all configured locales.', null);
38+
}
39+
40+
protected function execute(InputInterface $input, OutputInterface $output)
41+
{
42+
$configName = $input->getArgument('configuration');
43+
$config = $this->getContainer()->get('php_translation.configuration_manager')->getConfiguration($configName);
44+
if (null !== $inputLocale = $input->getArgument('locale', null)) {
45+
$locales = [$inputLocale];
46+
} else {
47+
$locales = $this->getContainer()->getParameter('php_translation.locales');
48+
}
49+
50+
$transPaths = array_merge($config['external_translations_dirs'], [$config['output_dir']]);
51+
$catalogueManager = $this->getContainer()->get('php_translation.catalogue_manager');
52+
$catalogueManager->load($this->getContainer()->get('php_translation.catalogue_fetcher')->getCatalogues($locales, $transPaths));
53+
54+
/** @var StorageService $storage */
55+
$storage = $this->getContainer()->get('php_translation.storage.'.$configName);
56+
$messages = $catalogueManager->findMessages(['locale' => $inputLocale, 'isObsolete' => true]);
57+
58+
$helper = $this->getHelper('question');
59+
$question = new ConfirmationQuestion(sprintf('You are about to remove %d translations. Do you wish to continue?', count($messages)), false);
60+
if (!$helper->ask($input, $output, $question)) {
61+
return;
62+
}
63+
64+
$progress = new ProgressBar($output, count($messages));
65+
foreach ($messages as $message) {
66+
$storage->delete($message->getLocale(), $message->getDomain(), $message->getKey());
67+
$progress->advance();
68+
}
69+
$progress->finish();
70+
}
71+
72+
/**
73+
* @param array $configuration
74+
*
75+
* @return Finder
76+
*/
77+
private function getConfiguredFinder(array $config)
78+
{
79+
// 'dirs', 'excluded_dirs', 'excluded_names'
80+
81+
$finder = new Finder();
82+
$finder->in($config['dirs']);
83+
84+
foreach ($config['excluded_dirs'] as $exclude) {
85+
$finder->notPath($exclude);
86+
}
87+
88+
foreach ($config['excluded_names'] as $exclude) {
89+
$finder->notName($exclude);
90+
}
91+
92+
return $finder;
93+
}
94+
}

Command/ExtractCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected function configure()
3030
$this
3131
->setName('translation:extract')
3232
->setDescription('Extract translations from source code.')
33-
->addArgument('configuration', InputArgument::REQUIRED, 'The configuration to use')
33+
->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
3434
->addArgument('locale', InputArgument::OPTIONAL, 'The locale ot use. If omitted, we use all configured locales.', false);
3535
}
3636

Service/ConfigurationManager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public function __construct(array $configuration)
3535
public function getConfiguration($name)
3636
{
3737
if (!isset($this->configuration[$name])) {
38+
if ($name === 'default') {
39+
return $this->getConfiguration($this->getFirstName());
40+
}
41+
3842
return [];
3943
}
4044

0 commit comments

Comments
 (0)