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