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
SuggestDump
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 onView
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
12
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 requiresWrite
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 requiresUnblock
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace CirrusSearch;
4
5use MediaWiki\Actions\FormlessAction;
6use MediaWiki\MediaWikiServices;
7
8/**
9 * action=cirrusSuggestDump handler. Dumps contents of Elasticsearch suggester
10 * index for the page.
11 *
12 * @license GPL-2.0-or-later
13 */
14class SuggestDump extends FormlessAction {
15    /** @inheritDoc */
16    public function onView() {
17        // Disable regular results
18        $this->getOutput()->disable();
19
20        $response = $this->getRequest()->response();
21        $response->header( 'Content-type: application/json; charset=UTF-8' );
22
23        $config = MediaWikiServices::getInstance()
24            ->getConfigFactory()
25            ->makeConfig( CirrusSearch::NAME );
26        /** @phan-suppress-next-line PhanTypeMismatchArgumentSuperType $config is actually a SearchConfig */
27        $conn = new Connection( $config );
28        /** @phan-suppress-next-line PhanTypeMismatchArgumentSuperType $config is actually a SearchConfig */
29        $searcher = new Searcher( $conn, 0, 0, $config, [], $this->getUser() );
30
31        /** @phan-suppress-next-line PhanUndeclaredMethod Phan doesn't know $config is a SearchConfig */
32        $docId = $config->makeId( $this->getTitle()->getArticleID() );
33
34        $esSources = $searcher->getSuggest( [ $docId ] );
35        if ( !$esSources->isOK() ) {
36            // happens when, for example, the completion index doesn't exist.
37            echo '{"error": "exception has been logged"}';
38            return null;
39        }
40        $result = [];
41        foreach ( $esSources->getValue() as $esResult ) {
42            $result[] = [
43                '_index' => $esResult->getIndex(),
44                '_id' => $esResult->getId(),
45                '_version' => $esResult->getVersion(),
46                '_source' => $esResult->getData(),
47            ];
48        }
49
50        // Echoing raw json to avoid any mangling that would prevent providing
51        // the resulting structures to elasticsearch.
52        echo json_encode( $result );
53
54        return null;
55    }
56
57    /**
58     * @return string
59     */
60    public function getName() {
61        return 'cirrussuggestdump';
62    }
63
64    /**
65     * @return bool
66     */
67    public function requiresWrite() {
68        return false;
69    }
70
71    /**
72     * @return bool
73     */
74    public function requiresUnblock() {
75        return false;
76    }
77}