Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CleanupTranslationProgressStatsMaintenanceScript
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Statistics;
5
6use MediaWiki\Extension\Translate\MessageGroupProcessing\MessageGroups;
7use MediaWiki\Extension\Translate\Utilities\Utilities;
8use MediaWiki\Language\RawMessage;
9use MediaWiki\Maintenance\Maintenance;
10use MediaWiki\MediaWikiServices;
11use const DB_PRIMARY;
12
13/**
14 * @since 2021.03
15 * @license GPL-2.0-or-later
16 * @author Niklas Laxström
17 */
18class CleanupTranslationProgressStatsMaintenanceScript extends Maintenance {
19    public function __construct() {
20        parent::__construct();
21        $this->addDescription( 'Remove obsolete entries from translate_groupstats table' );
22        $this->requireExtension( 'Translate' );
23    }
24
25    public function execute() {
26        $services = MediaWikiServices::getInstance();
27        $db = $services->getDBLoadBalancer()->getConnection( DB_PRIMARY );
28
29        $dbGroupIds = $db->newSelectQueryBuilder()
30            ->select( 'tgs_group' )
31            ->distinct()
32            ->from( 'translate_groupstats' )
33            ->caller( __METHOD__ )
34            ->fetchFieldValues();
35        $knownGroupIds = array_map(
36            [ MessageGroupStats::class, 'getDatabaseIdForGroupId' ],
37            array_keys( MessageGroups::singleton()->getGroups() )
38        );
39        $unknownGroupIds = array_diff( $dbGroupIds, $knownGroupIds );
40
41        if ( $unknownGroupIds !== [] ) {
42            $msg = ( new RawMessage( "Removing rows for $1 unknown group{{PLURAL:$1||s}}:\n" ) )
43                ->numParams( count( $unknownGroupIds ) )
44                ->inLanguage( 'en' )
45                ->text();
46            $this->output( $msg );
47            foreach ( $unknownGroupIds as $id ) {
48                $this->output( "$id\n" );
49            }
50            $db->newDeleteQueryBuilder()
51                ->deleteFrom( 'translate_groupstats' )
52                ->where( [ 'tgs_group' => $unknownGroupIds ] )
53                ->caller( __METHOD__ )
54                ->execute();
55        }
56
57        $dbLanguages = $db->newSelectQueryBuilder()
58            ->select( 'tgs_lang' )
59            ->distinct()
60            ->from( 'translate_groupstats' )
61            ->caller( __METHOD__ )
62            ->fetchFieldValues();
63        $knownLanguages = array_keys( Utilities::getLanguageNames( 'en' ) );
64        $unknownLanguages = array_diff( $dbLanguages, $knownLanguages );
65
66        if ( $unknownLanguages !== [] ) {
67            $msg = ( new RawMessage( "Removing rows for $1 unknown language{{PLURAL:$1||s}}:\n" ) )
68                ->numParams( count( $unknownLanguages ) )
69                ->inLanguage( 'en' )
70                ->text();
71            $this->output( $msg );
72            foreach ( $unknownLanguages as $languageCode ) {
73                $this->output( "$languageCode\n" );
74            }
75            $db->newDeleteQueryBuilder()
76                ->deleteFrom( 'translate_groupstats' )
77                ->where( [ 'tgs_lang' => $unknownLanguages ] )
78                ->caller( __METHOD__ )
79                ->execute();
80        }
81
82        $this->output( "Done.\n" );
83    }
84}