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;
9use MediaWiki\Language\RawMessage;
10use MediaWiki\MediaWikiServices;
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()->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->delete(
51 'translate_groupstats',
52 [ 'tgs_group' => $unknownGroupIds ],
53 __METHOD__
54 );
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->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 a list.
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:31