Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
MigrateConfigToCommunity
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 initServices
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 doDBUpdates
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
12
 getUpdateKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Babel\Maintenance;
4
5use MediaWiki\Context\RequestContext;
6use MediaWiki\Extension\CommunityConfiguration\CommunityConfigurationServices;
7use MediaWiki\Extension\CommunityConfiguration\Provider\IConfigurationProvider;
8use MediaWiki\Json\FormatJson;
9use MediaWiki\Maintenance\LoggedUpdateMaintenance;
10use MediaWiki\Permissions\UltimateAuthority;
11use MediaWiki\Status\StatusFormatter;
12use MediaWiki\User\User;
13
14$IP = getenv( 'MW_INSTALL_PATH' );
15if ( $IP === false ) {
16    $IP = __DIR__ . '/../../..';
17}
18require_once "$IP/maintenance/Maintenance.php";
19
20class MigrateConfigToCommunity extends LoggedUpdateMaintenance {
21
22    private StatusFormatter $statusFormatter;
23    private IConfigurationProvider $provider;
24
25    public function __construct() {
26        parent::__construct();
27
28        $this->requireExtension( 'Babel' );
29        $this->requireExtension( 'CommunityConfiguration' );
30
31        $this->addOption( 'dry-run', 'Print the migration summary.' );
32    }
33
34    private function initServices(): void {
35        $services = $this->getServiceContainer();
36        $this->statusFormatter = $services->getFormatterFactory()
37            ->getStatusFormatter( RequestContext::getMain() );
38
39        $ccServices = CommunityConfigurationServices::wrap( $this->getServiceContainer() );
40        $this->provider = $ccServices->getConfigurationProviderFactory()->newProvider( 'Babel' );
41    }
42
43    protected function doDBUpdates() {
44        $this->initServices();
45        $dryRun = $this->hasOption( 'dry-run' );
46
47        $config = $this->getConfig();
48        $newConfig = (object)[
49            'BabelCategoryNames' => (object)$config->get( 'BabelCategoryNames' ),
50            'BabelMainCategory' => $config->get( 'BabelMainCategory' ),
51            'BabelUseUserLanguage' => $config->get( 'BabelUseUserLanguage' ),
52            'BabelAutoCreate' => $config->get( 'BabelAutoCreate' ),
53            'BabelCategorizeNamespaces' => $config->get( 'BabelCategorizeNamespaces' ),
54        ];
55
56        if ( $dryRun ) {
57            $this->output( FormatJson::encode( $newConfig, true ) . PHP_EOL );
58        } else {
59            $status = $this->provider->storeValidConfiguration(
60                $newConfig,
61                new UltimateAuthority( User::newSystemUser( User::MAINTENANCE_SCRIPT_USER ) )
62            );
63            if ( $status->isOK() ) {
64                $this->output( 'Done!' . PHP_EOL );
65            } else {
66                $this->error( 'Error when saving the new configuration' );
67                $this->error( '== Error details' );
68                $this->error( $this->statusFormatter->getWikiText( $status ) );
69                return false;
70            }
71        }
72
73        return !$dryRun;
74    }
75
76    /**
77     * @inheritDoc
78     */
79    protected function getUpdateKey() {
80        return 'BabelMigrateConfigToCommunity';
81    }
82}
83
84$maintClass = MigrateConfigToCommunity::class;
85require_once RUN_MAINTENANCE_IF_MAIN;