Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UpdateDYMIndexTemplates
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 2
90
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
 execute
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2
3namespace CirrusSearch\Maintenance;
4
5use CirrusSearch\Profile\SearchProfileService;
6
7/**
8 * Update the search configuration on the search backend.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26$IP = getenv( 'MW_INSTALL_PATH' );
27if ( $IP === false ) {
28    $IP = __DIR__ . '/../../..';
29}
30require_once "$IP/maintenance/Maintenance.php";
31require_once __DIR__ . '/../includes/Maintenance/Maintenance.php';
32
33/**
34 * Update the elasticsearch configuration for this index.
35 */
36class UpdateDYMIndexTemplates extends Maintenance {
37    public function __construct() {
38        parent::__construct();
39        $this->addDescription( "Update the template for index lookup DYM fallback profiles." .
40                               "operates on a single cluster." );
41        $this->addOption( 'profile', 'Extract the template from this profile.', false, true );
42    }
43
44    public function execute() {
45        $this->disablePoolCountersAndLogging();
46        $profiles = [];
47        $profileService = $this->getSearchConfig()->getProfileService();
48        if ( $this->hasOption( 'profile' ) ) {
49            $profileName = $this->getOption( 'profile' );
50            $profiles[] = $profileService->loadProfileByName( SearchProfileService::INDEX_LOOKUP_FALLBACK,
51                $profileName );
52        } else {
53            $profiles = $profileService->listExposedProfiles( SearchProfileService::INDEX_LOOKUP_FALLBACK );
54        }
55
56        /** @var IndexTemplateBuilder[] $templateBuilders */
57        $templateBuilders = [];
58        $configUtils = new ConfigUtils( $this->getConnection()->getClient(), $this );
59        $availablePlugins = $this->unwrap(
60            $configUtils->scanAvailablePlugins( $this->getConfig()->get( 'CirrusSearchBannedPlugins' ) ) );
61        foreach ( $profiles as $profileName => $profile ) {
62            if ( !isset( $profile['index_template'] ) ) {
63                $this->output( "Skipping template for [$profileName], no template definition found." );
64                continue;
65            }
66
67            try {
68                 $templateBuilders[] = IndexTemplateBuilder::build(
69                    $this->getConnection(),
70                    $profile['index_template'],
71                    $availablePlugins
72                 );
73            } catch ( \InvalidArgumentException $iae ) {
74                $this->fatalError( "Cannot load profile [$profileName]: {$iae->getMessage()}" );
75            }
76        }
77        $namesSeen = [];
78        foreach ( $templateBuilders as $profileName => $builder ) {
79            $seen = $namesSeen[$builder->getTemplateName()] ?? null;
80            if ( $seen !== null ) {
81                $this->fatalError( "Found duplicate template name [{$builder->getTemplateName()}" .
82                    "in profile [$seen] and [$profileName]" );
83            }
84        }
85        foreach ( $templateBuilders as $builder ) {
86            $this->output( "Creating template [{$builder->getTemplateName()}]...\n" );
87            $builder->execute();
88        }
89    }
90}
91$maintClass = UpdateDYMIndexTemplates::class;
92require_once RUN_MAINTENANCE_IF_MAIN;