Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
32 / 36
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProfilesDump
88.89% covered (warning)
88.89%
32 / 36
75.00% covered (warning)
75.00%
3 / 4
12.20
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 execute
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
8
 getAllowedParams
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace CirrusSearch\Api;
4
5use ApiMain;
6use CirrusSearch\Profile\SearchProfileOverride;
7use CirrusSearch\Profile\SearchProfileService;
8use Wikimedia\ParamValidator\ParamValidator;
9
10/**
11 * Dumps CirrusSearch profiles for easy viewing.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 * http://www.gnu.org/copyleft/gpl.html
27 */
28class ProfilesDump extends \ApiBase {
29    use ApiTrait;
30
31    /** @var SearchProfileService */
32    private $service;
33
34    /**
35     * @param ApiMain $mainModule
36     * @param string $moduleName
37     * @param string $modulePrefix
38     * @param SearchProfileService|null $service
39     */
40    public function __construct( ApiMain $mainModule, $moduleName, $modulePrefix = '', SearchProfileService $service = null ) {
41        parent::__construct( $mainModule, $moduleName, $modulePrefix );
42        $this->service = $service ?: $this->getSearchConfig()->getProfileService();
43    }
44
45    public function execute() {
46        $service = $this->service;
47        $verbose = $this->getParameter( 'verbose' );
48        $contexts = [];
49        foreach ( $service->listProfileTypes() as $type ) {
50            foreach ( $service->listProfileRepositories( $type ) as $repository ) {
51                $this->getResult()->addValue( [ 'profiles', $repository->repositoryType(), 'repositories' ], $repository->repositoryName(),
52                    $verbose ? $repository->listExposedProfiles() : array_keys( $repository->listExposedProfiles() ) );
53            }
54            foreach ( $service->listProfileContexts( $type ) as $context => $default ) {
55                $this->getResult()->addValue( [ 'profiles', $type, 'contexts', $context ], 'code_default', $default );
56                $this->getResult()->addValue( [ 'profiles', $type, 'contexts', $context ], 'actual_default',
57                    $service->getProfileName( $type, $context, [] ) );
58                $overriders = $service->listProfileOverrides( $type, $context );
59                usort( $overriders, static function ( SearchProfileOverride $a, SearchProfileOverride $b ) {
60                    return $a->priority() <=> $b->priority();
61                } );
62                $overriders = array_map( static function ( SearchProfileOverride $o ) {
63                    return $o->explain();
64                }, $overriders );
65                $this->getResult()->addValue( [ 'profiles', $type, 'contexts', $context ], 'overriders', $overriders );
66                if ( !isset( $contexts[$context] ) ) {
67                    $contexts[$context] = [];
68                }
69                $contexts[$context][] = $type;
70            }
71        }
72
73        foreach ( $contexts as $context => $types ) {
74            foreach ( $types as $type ) {
75                $this->getResult()->addValue( [ 'contexts', $context ], $type, $service->getProfileName( $type, $context, [] ) );
76            }
77        }
78    }
79
80    public function getAllowedParams() {
81        return [
82            'verbose' => [
83                ParamValidator::PARAM_DEFAULT => false,
84            ],
85        ];
86    }
87
88    /**
89     * @see ApiBase::getExamplesMessages
90     * @return array
91     */
92    protected function getExamplesMessages() {
93        return [
94            'action=cirrus-profiles-dump' =>
95                'apihelp-cirrus-profiles-dump-example'
96        ];
97    }
98
99}