Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
QueryCompSuggestBuildDoc | |
0.00% |
0 / 27 |
|
0.00% |
0 / 5 |
72 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
getAllowedParams | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
isInternal | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addExplanation | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace CirrusSearch\Api; |
4 | |
5 | use CirrusSearch\BuildDocument\Completion\SuggestBuilder; |
6 | use Elastica\Document; |
7 | use InvalidArgumentException; |
8 | use MediaWiki\Api\ApiQuery; |
9 | use MediaWiki\Api\ApiQueryBase; |
10 | use Wikimedia\ParamValidator\ParamValidator; |
11 | |
12 | class QueryCompSuggestBuildDoc extends ApiQueryBase { |
13 | use ApiTrait; |
14 | |
15 | public function __construct( ApiQuery $query, string $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 | /** |
48 | * Mark as internal. This isn't meant to be used by normal api users |
49 | * @return bool |
50 | */ |
51 | public function isInternal() { |
52 | return true; |
53 | } |
54 | |
55 | private function addExplanation( SuggestBuilder $builder, int $pageId, array $docs ) { |
56 | $docs = array_map( |
57 | static function ( Document $d ) { |
58 | return [ $d->getId() => $d->getData() ]; |
59 | }, $builder->build( $docs, true ) |
60 | ); |
61 | |
62 | foreach ( $docs as $doc ) { |
63 | $this->getResult()->addValue( |
64 | [ 'query', 'pages', $pageId ], |
65 | 'cirruscompsuggestbuilddoc', |
66 | $doc |
67 | ); |
68 | } |
69 | } |
70 | } |