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()->getGoodTitles() as $origPageId => $title ) {
32            $docs = $this->loadDocuments( $title );
33            $this->addExplanation( $builder, $origPageId, $docs );
34        }
35    }
36
37    protected function getAllowedParams() {
38        return [
39            'method' => [
40                ParamValidator::PARAM_TYPE => 'string',
41                ParamValidator::PARAM_DEFAULT => $this->getSearchConfig()->get( 'CirrusSearchCompletionDefaultScore' ),
42            ],
43        ];
44    }
45
46    private function addExplanation( SuggestBuilder $builder, $pageId, array $docs ) {
47        $docs = array_map(
48            static function ( Document $d ) {
49                return [ $d->getId() => $d->getData() ];
50            }, $builder->build( $docs, true )
51        );
52
53        foreach ( $docs as $doc ) {
54            $this->getResult()->addValue(
55                [ 'query', 'pages', $pageId ],
56                'cirruscompsuggestbuilddoc',
57                $doc
58            );
59        }
60    }
61}