Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.49% covered (warning)
86.49%
32 / 37
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProfilesDump
86.49% covered (warning)
86.49%
32 / 37
60.00% covered (warning)
60.00%
3 / 5
12.36
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
1
 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
 isInternal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 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 CirrusSearch\Profile\SearchProfileOverride;
6use CirrusSearch\Profile\SearchProfileService;
7use MediaWiki\Api\ApiBase;
8use MediaWiki\Api\ApiMain;
9use Wikimedia\ParamValidator\ParamValidator;
10
11/**
12 * Dumps CirrusSearch profiles for easy viewing.
13 *
14 * @license GPL-2.0-or-later
15 */
16class ProfilesDump extends ApiBase {
17    use ApiTrait;
18
19    private SearchProfileService $service;
20
21    public function __construct( ApiMain $mainModule, string $moduleName, ?SearchProfileService $service = null ) {
22        parent::__construct( $mainModule, $moduleName );
23        $this->service = $service ?? $this->getSearchConfig()->getProfileService();
24    }
25
26    public function execute() {
27        $service = $this->service;
28        $verbose = $this->getParameter( 'verbose' );
29        $contexts = [];
30        foreach ( $service->listProfileTypes() as $type ) {
31            foreach ( $service->listProfileRepositories( $type ) as $repository ) {
32                $this->getResult()->addValue( [ 'profiles', $repository->repositoryType(), 'repositories' ], $repository->repositoryName(),
33                    $verbose ? $repository->listExposedProfiles() : array_keys( $repository->listExposedProfiles() ) );
34            }
35            foreach ( $service->listProfileContexts( $type ) as $context => $default ) {
36                $this->getResult()->addValue( [ 'profiles', $type, 'contexts', $context ], 'code_default', $default );
37                $this->getResult()->addValue( [ 'profiles', $type, 'contexts', $context ], 'actual_default',
38                    $service->getProfileName( $type, $context, [] ) );
39                $overriders = $service->listProfileOverrides( $type, $context );
40                usort( $overriders, static function ( SearchProfileOverride $a, SearchProfileOverride $b ) {
41                    return $a->priority() <=> $b->priority();
42                } );
43                $overriders = array_map( static function ( SearchProfileOverride $o ) {
44                    return $o->explain();
45                }, $overriders );
46                $this->getResult()->addValue( [ 'profiles', $type, 'contexts', $context ], 'overriders', $overriders );
47                if ( !isset( $contexts[$context] ) ) {
48                    $contexts[$context] = [];
49                }
50                $contexts[$context][] = $type;
51            }
52        }
53
54        foreach ( $contexts as $context => $types ) {
55            foreach ( $types as $type ) {
56                $this->getResult()->addValue( [ 'contexts', $context ], $type, $service->getProfileName( $type, $context, [] ) );
57            }
58        }
59    }
60
61    /** @inheritDoc */
62    public function getAllowedParams() {
63        return [
64            'verbose' => [
65                ParamValidator::PARAM_DEFAULT => false,
66            ],
67        ];
68    }
69
70    /**
71     * Mark as internal. This isn't meant to be used by normal api users
72     * @return bool
73     */
74    public function isInternal() {
75        return true;
76    }
77
78    /**
79     * @see ApiBase::getExamplesMessages
80     * @return array
81     */
82    protected function getExamplesMessages() {
83        return [
84            'action=cirrus-profiles-dump' =>
85                'apihelp-cirrus-profiles-dump-example'
86        ];
87    }
88
89}