Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
79.25% covered (warning)
79.25%
42 / 53
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
MigrateConfigToCommunity
79.25% covered (warning)
79.25%
42 / 53
75.00% covered (warning)
75.00%
3 / 4
10.89
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 initServices
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 doDBUpdates
70.27% covered (warning)
70.27%
26 / 37
0.00% covered (danger)
0.00%
0 / 1
8.29
 getUpdateKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Babel\Maintenance;
4
5use MediaWiki\Context\RequestContext;
6use MediaWiki\Extension\CommunityConfiguration\CommunityConfigurationServices;
7use MediaWiki\Extension\CommunityConfiguration\Provider\ConfigurationProviderFactory;
8use MediaWiki\Json\FormatJson;
9use MediaWiki\Maintenance\LoggedUpdateMaintenance;
10use MediaWiki\Maintenance\LoggedUpdateOutcome;
11use MediaWiki\Permissions\UltimateAuthority;
12use MediaWiki\Status\StatusFormatter;
13use MediaWiki\User\User;
14
15// @codeCoverageIgnoreStart
16$IP = getenv( 'MW_INSTALL_PATH' );
17if ( $IP === false ) {
18    $IP = __DIR__ . '/../../..';
19}
20require_once "$IP/maintenance/Maintenance.php";
21// @codeCoverageIgnoreEnd
22
23class MigrateConfigToCommunity extends LoggedUpdateMaintenance {
24
25    private StatusFormatter $statusFormatter;
26    private ConfigurationProviderFactory $providerFactory;
27
28    public function __construct() {
29        parent::__construct();
30
31        $this->requireExtension( 'Babel' );
32        $this->requireExtension( 'CommunityConfiguration' );
33
34        $this->addOption( 'dry-run', 'Print the migration summary.' );
35        $this->addOption(
36            'summary-note',
37            'A note to be included in the edit summary (for example, a link to an announcement ' .
38            'page or a bug tracker; use wikitext syntax for internal links)',
39            false, true
40        );
41    }
42
43    private function initServices(): void {
44        $services = $this->getServiceContainer();
45        $this->statusFormatter = $services->getFormatterFactory()
46            ->getStatusFormatter( RequestContext::getMain() );
47
48        $ccServices = CommunityConfigurationServices::wrap( $this->getServiceContainer() );
49        $this->providerFactory = $ccServices->getConfigurationProviderFactory();
50    }
51
52    /** @inheritDoc */
53    protected function doDBUpdates(): LoggedUpdateOutcome {
54        $this->initServices();
55
56        if ( !$this->providerFactory->isProviderSupported( 'Babel' ) ) {
57            $this->fatalError(
58                'The `Babel` CommunityConfiguration provider is not supported; ' .
59                'maybe BabelUseCommunityConfiguration is not set to true?'
60            );
61        }
62
63        $provider = $this->providerFactory->newProvider( 'Babel' );
64        $dryRun = $this->hasOption( 'dry-run' );
65
66        $config = $this->getConfig();
67        $newConfig = (object)[
68            'BabelCategoryNames' => (object)array_map(
69                // Community configuration does not support multi-typed configs
70                // An empty string is interpreted as "no category" (same as false). See T384941.
71                static fn ( $categoryName ) => $categoryName !== false ? $categoryName : '',
72                $config->get( 'BabelCategoryNames' )
73            ),
74            // Community configuration does not support multi-typed configs
75            // An empty string is interpreted as "no category" (same as false). See T384941.
76            'BabelMainCategory' => $config->get( 'BabelMainCategory' ) !== false ?
77                $config->get( 'BabelMainCategory' ) : '',
78            'BabelUseUserLanguage' => $config->get( 'BabelUseUserLanguage' ),
79            'BabelAutoCreate' => $config->get( 'BabelAutoCreate' ),
80        ];
81
82        if ( $dryRun ) {
83            $this->output( FormatJson::encode( $newConfig, true ) . PHP_EOL );
84            return LoggedUpdateOutcome::SIMULATED;
85        }
86
87        $summary = 'Migrating server configuration to an on-wiki JSON file';
88        if ( $this->hasOption( 'summary-note' ) ) {
89            $summary .= ' (' . $this->getOption( 'summary-note' ) . ')';
90        }
91        $status = $provider->storeValidConfiguration(
92            $newConfig,
93            new UltimateAuthority( User::newSystemUser( User::MAINTENANCE_SCRIPT_USER ) ),
94            $summary
95        );
96        if ( $status->isOK() ) {
97            $this->output( 'Done!' . PHP_EOL );
98            return LoggedUpdateOutcome::COMPLETE;
99        } else {
100            $this->error( 'Error when saving the new configuration' );
101            $this->error( '== Error details' );
102            $this->error( $this->statusFormatter->getWikiText( $status ) );
103            return LoggedUpdateOutcome::FAILED;
104        }
105    }
106
107    /**
108     * @inheritDoc
109     */
110    protected function getUpdateKey() {
111        return 'BabelMigrateConfigToCommunity';
112    }
113}
114
115// @codeCoverageIgnoreStart
116$maintClass = MigrateConfigToCommunity::class;
117require_once RUN_MAINTENANCE_IF_MAIN;
118// @codeCoverageIgnoreEnd