Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
QueryCompSuggestBuildDoc
0.00% covered (danger)
0.00%
0 / 26
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 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 addExplanation
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace CirrusSearch\Api;
4
5use ApiQuery;
6use ApiQueryBase;
7use CirrusSearch\BuildDocument\Completion\SuggestBuilder;
8use Elastica\Document;
9use InvalidArgumentException;
10use Wikimedia\ParamValidator\ParamValidator;
11
12class QueryCompSuggestBuildDoc extends ApiQueryBase {
13    use ApiTrait;
14
15    public function __construct( ApiQuery $query, $moduleName ) {
16        parent::__construct( $query, $moduleName, 'csb' );
17    }
18
19    /**
20     * @inheritDoc
21     */
22    public function execute() {
23        $method = $this->getParameter( 'method' );
24        try {
25            $builder = SuggestBuilder::create( $this->getCirrusConnection(), $method );
26        } catch ( InvalidArgumentException $e ) {
27            $this->addError( 'apierror-compsuggestbuilddoc-bad-method' );
28            return;
29        }
30
31        foreach ( $this->getPageSet()->getGoodPages() as $origPageId => $title ) {
32            $docs = $this->loadDocuments( $title );
33            $this->addExplanation( $builder, $origPageId, $docs );
34        }
35    }
36
37    /** @inheritDoc */
38    protected function getAllowedParams() {
39        return [
40            'method' => [
41                ParamValidator::PARAM_TYPE => 'string',
42                ParamValidator::PARAM_DEFAULT => $this->getSearchConfig()->get( 'CirrusSearchCompletionDefaultScore' ),
43            ],
44        ];
45    }
46
47    private function addExplanation( SuggestBuilder $builder, $pageId, array $docs ) {
48        $docs = array_map(
49            static function ( Document $d ) {
50                return [ $d->getId() => $d->getData() ];
51            }, $builder->build( $docs, true )
52        );
53
54        foreach ( $docs as $doc ) {
55            $this->getResult()->addValue(
56                [ 'query', 'pages', $pageId ],
57                'cirruscompsuggestbuilddoc',
58                $doc
59            );
60        }
61    }
62}