23 private Language $language;
25 public function __construct() {
26 parent::__construct();
27 $this->addDescription(
'Refresh cached message group stats to fix any outdated entries' );
31 'Which groups to scan. Comma-separated list with wildcard (*) accepted.',
37 'Which languages to scan. Comma-separated list with wildcard (*) accepted.',
44 'Use job queue (asynchronous).',
49 $this->requireExtension(
'Translate' );
54 $this->language = $this->getServiceContainer()->getContentLanguage();
56 $groupInput = $this->getOption(
'group',
'*' );
58 $groupIds = MessageGroups::expandWildcards( $groupIdPattern );
61 $languageInput = strtolower( $this->getOption(
'language',
'*' ) );
63 $languages = $this->getLanguages( $languagePattern );
65 $useJobQueue = $this->hasOption(
'use-job-queue' );
66 $jobQueueGroup = $this->getServiceContainer()->getJobQueueGroup();
67 $jobCount = count( $groupIds ) * count( $languages );
70 $startTime = microtime(
true );
72 foreach ( $groupIds as $groupId ) {
74 foreach ( $languages as $language ) {
75 $jobs[] = $job = RebuildMessageGroupStatsJob::newJob( [
76 RebuildMessageGroupStatsJob::REFRESH =>
true,
77 RebuildMessageGroupStatsJob::GROUP_ID => $groupId,
78 RebuildMessageGroupStatsJob::LANGUAGE_CODE => $language
81 if ( !$useJobQueue ) {
84 $elapsed = microtime(
true ) - $startTime;
85 $this->output(
"\033[0K\r" . $this->cliProgressBar( $jobCount, ++$counter, $elapsed ) );
89 $jobQueueGroup->push( $jobs );
95 $message =
new RawMessage(
"\nQueued {{PLURAL:$1|$1 job|$1 jobs}}.", [ $jobCount ] );
96 $this->output( $message->text() );
98 $this->output(
"\n" );
101 private function getLanguages( array $patterns ): array {
102 $allLanguages = array_keys(
Utilities::getLanguageNames( null ) );
104 if ( in_array(
'*', $patterns,
true ) ) {
105 return $allLanguages;
108 $matchedLanguages = array_intersect( $patterns, $allLanguages );
109 $patterns = array_diff( $patterns, $matchedLanguages );
110 $remainingLanguages = array_diff( $allLanguages, $matchedLanguages );
112 if ( $patterns === [] ) {
113 return $matchedLanguages;
117 $matcher =
new StringMatcher(
'', $patterns );
118 foreach ( $remainingLanguages as $id ) {
119 if ( $matcher->matches( $id ) ) {
120 $matchedLanguages[] = $id;
124 return $matchedLanguages;
127 private function cliProgressBar(
int $total,
int $current,
float $elapsed ): string {
129 $progress = $current / $total;
130 $filledLength = (int)round( $barLength * $progress );
131 $bar = str_repeat(
'#', $filledLength ) . str_repeat(
'-', ( $barLength - $filledLength ) );
132 $percent = $current / $total * 100;
133 $estimatedRemaining = $this->language->formatDuration( (
int)( $elapsed / $progress - $elapsed ) );
135 return sprintf(
'Progress: [%s] %.2f%% | %s remaining', $bar, $percent, $estimatedRemaining );