Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
CleanupTranslationProgressStatsMaintenanceScript.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Statistics;
5
6use Maintenance;
7use MediaWiki\MediaWikiServices;
9use RawMessage;
11use const DB_PRIMARY;
12
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()->getConnectionRef( DB_PRIMARY );
28
29 $dbGroupIds = $db->selectFieldValues(
30 'translate_groupstats',
31 'DISTINCT(tgs_group)',
32 '*',
33 __METHOD__
34 );
35 $knownGroupIds = array_map(
36 'MessageGroupStats::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->delete(
51 'translate_groupstats',
52 [ 'tgs_group' => $unknownGroupIds ],
53 __METHOD__
54 );
55 }
56
57 $dbLanguages = $db->selectFieldValues(
58 'translate_groupstats',
59 'DISTINCT(tgs_lang)',
60 '*',
61 __METHOD__
62 );
63 $knownLanguages = array_keys( TranslateUtils::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->delete(
76 'translate_groupstats',
77 [ 'tgs_lang' => $unknownLanguages ],
78 __METHOD__
79 );
80 }
81
82 $this->output( "Done.\n" );
83 }
84}
Factory class for accessing message groups individually by id or all of them as an list.
Essentially random collection of helper functions, similar to GlobalFunctions.php.