Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
UpdateSearchIndexConfig
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 3
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
 execute
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
12
 clustersToWriteTo
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace CirrusSearch\Maintenance;
4
5use CirrusSearch\Connection;
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 UpdateSearchIndexConfig extends Maintenance {
37    public function __construct() {
38        parent::__construct();
39        $this->addDescription( "Update the configuration or contents of all search indices. This always operates on a single cluster." );
40        // Directly require this script so we can include its parameters as maintenance scripts can't use the autoloader
41        // in __construct.  Lame.
42        require_once __DIR__ . '/UpdateOneSearchIndexConfig.php';
43        UpdateOneSearchIndexConfig::addSharedOptions( $this );
44    }
45
46    /**
47     * @return bool|null
48     * @suppress PhanUndeclaredMethod runChild technically returns a
49     *  \Maintenance instance but only \CirrusSearch\Maintenance\Maintenance
50     *  classes have the done method. Just allow it since we know what type of
51     *  maint class is being created
52     */
53    public function execute() {
54        // Use the default connection, rather than the one specified
55        // by --cluster, as we are collecting cluster independent metadata.
56        // Also our script specific `all` cluster fails self::getConnection.
57        $conn = Connection::getPool( $this->getSearchConfig() );
58
59        foreach ( $this->clustersToWriteTo() as $cluster ) {
60            $this->outputIndented( "Updating cluster $cluster...\n" );
61
62            $this->outputIndented( "indexing namespaces...\n" );
63            $child = $this->runChild( IndexNamespaces::class );
64            $child->done();
65            $child->loadParamsAndArgs(
66                null,
67                array_merge( $this->parameters->getOptions(), [
68                    'cluster' => $cluster,
69                ] ),
70                $this->parameters->getArgs()
71            );
72            $child->execute();
73            $child->done();
74
75            foreach ( $conn->getAllIndexSuffixes( null ) as $indexSuffix ) {
76                $this->outputIndented( "$indexSuffix index...\n" );
77                $child = $this->runChild( UpdateOneSearchIndexConfig::class );
78                $child->done();
79                $child->loadParamsAndArgs(
80                    null,
81                    array_merge( $this->parameters->getOptions(), [
82                        'cluster' => $cluster,
83                        'indexSuffix' => $indexSuffix,
84                    ] ),
85                    $this->parameters->getArgs()
86                );
87                $child->execute();
88                $child->done();
89            }
90        }
91
92        return true;
93    }
94
95    /**
96     * Convenience method to interperet the 'all' cluster
97     * as a request to run against each of the known clusters.
98     *
99     * @return string[]
100     */
101    protected function clustersToWriteTo() {
102        $cluster = $this->getOption( 'cluster', null );
103        if ( $cluster === 'all' ) {
104            return $this->getSearchConfig()
105                ->getClusterAssignment()
106                ->getAllKnownClusters();
107        } else {
108            // single specified cluster. May be null, which
109            // indirectly selects the default search cluster.
110            return [ $cluster ];
111        }
112    }
113
114}
115
116$maintClass = UpdateSearchIndexConfig::class;
117require_once RUN_MAINTENANCE_IF_MAIN;