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
MigrateServerConfig
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 4
56
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 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 doDBUpdates
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
20
 getUpdateKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace GrowthExperiments\Maintenance;
4
5use GrowthExperiments\Config\GrowthExperimentsMultiConfig;
6use GrowthExperiments\Config\WikiPageConfigWriter;
7use GrowthExperiments\Config\WikiPageConfigWriterFactory;
8use GrowthExperiments\GrowthExperimentsServices;
9use LoggedUpdateMaintenance;
10use MediaWiki\MediaWikiServices;
11use MediaWiki\Title\TitleFactory;
12
13$IP = getenv( 'MW_INSTALL_PATH' );
14if ( $IP === false ) {
15    $IP = __DIR__ . '/../../..';
16}
17require_once "$IP/maintenance/Maintenance.php";
18
19/**
20 * Maintenance script for migrating server config to on-wiki config files
21 */
22class MigrateServerConfig extends LoggedUpdateMaintenance {
23    /** @var TitleFactory */
24    private $titleFactory;
25
26    /** @var WikiPageConfigWriterFactory */
27    private $wikiPageConfigWriterFactory;
28
29    /** @var WikiPageConfigWriter */
30    private $wikiPageConfigWriter;
31
32    public function __construct() {
33        parent::__construct();
34        $this->requireExtension( 'GrowthExperiments' );
35        $this->addDescription( 'Migrate configuration to on-wiki config files from server config' );
36
37        $this->addOption( 'dry-run', 'Print the configuration that would be saved on-wiki.' );
38    }
39
40    private function initServices() {
41        $services = MediaWikiServices::getInstance();
42
43        $this->wikiPageConfigWriterFactory = GrowthExperimentsServices::wrap( $services )
44            ->getWikiPageConfigWriterFactory();
45        $this->titleFactory = $services->getTitleFactory();
46    }
47
48    /**
49     * @inheritDoc
50     */
51    protected function doDBUpdates() {
52        $this->initServices();
53        $dryRun = $this->hasOption( 'dry-run' );
54
55        $title = $this->titleFactory->newFromText(
56            $this->getConfig()->get( 'GEWikiConfigPageTitle' )
57        );
58        if ( $title === null ) {
59            $this->fatalError( "Invalid GEWikiConfigPageTitle!\n" );
60        }
61        $this->wikiPageConfigWriter = $this->wikiPageConfigWriterFactory
62            ->newWikiPageConfigWriter(
63                $title
64            );
65
66        $variables = [];
67        foreach ( GrowthExperimentsMultiConfig::ALLOW_LIST as $variable ) {
68            $variables[$variable] = $this->getConfig()->get( $variable );
69        }
70        if ( !$dryRun ) {
71            $this->wikiPageConfigWriter->pruneConfig();
72            $this->wikiPageConfigWriter->setVariables( $variables );
73            $this->wikiPageConfigWriter->save(
74                'Migrating server configuration to an on-wiki JSON file ([[phab:T275795]])'
75            );
76            $this->output( "Done!\n" );
77            return true;
78        } else {
79            $this->output( json_encode( $variables, JSON_PRETTY_PRINT ) . "\n" );
80            return false;
81        }
82    }
83
84    /**
85     * @inheritDoc
86     */
87    protected function getUpdateKey() {
88        return 'GrowthExperimentsMigrateServerConfig';
89    }
90}
91
92$maintClass = MigrateServerConfig::class;
93require_once RUN_MAINTENANCE_IF_MAIN;