Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Dump
0.00% covered (danger)
0.00%
0 / 28
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 / 25
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 FormlessAction;
6use MediaWiki\MediaWikiServices;
7
8/**
9 * action=cirrusDump handler.  Dumps contents of Elasticsearch indexes for the
10 * page.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 */
27class Dump extends FormlessAction {
28    public function onView() {
29        // Disable regular results
30        $this->getOutput()->disable();
31
32        $response = $this->getRequest()->response();
33        $response->header( 'Content-type: application/json; charset=UTF-8' );
34
35        $config = MediaWikiServices::getInstance()
36            ->getConfigFactory()
37            ->makeConfig( 'CirrusSearch' );
38        /** @phan-suppress-next-line PhanTypeMismatchArgumentSuperType $config is actually a SearchConfig */
39        $conn = new Connection( $config );
40        /** @phan-suppress-next-line PhanTypeMismatchArgumentSuperType $config is actually a SearchConfig */
41        $searcher = new Searcher( $conn, 0, 0, $config, [], $this->getUser() );
42
43        /** @phan-suppress-next-line PhanUndeclaredMethod Phan doesn't know $config is a SearchConfig */
44        $docId = $config->makeId( $this->getTitle()->getArticleID() );
45        $esSources = $searcher->get( [ $docId ], true );
46        if ( !$esSources->isOK() ) {
47            // echo for consistency with below
48            echo '{"error": "exception has been logged"}';
49            return null;
50        }
51        $esSources = $esSources->getValue();
52
53        $result = [];
54        foreach ( $esSources as $esSource ) {
55            $result[] = [
56                '_index' => $esSource->getIndex(),
57                '_type' => $esSource->getType(),
58                '_id' => $esSource->getId(),
59                '_version' => $esSource->getVersion(),
60                '_source' => $esSource->getData(),
61            ];
62        }
63        // Echoing raw json to avoid any mangling that would prevent providing
64        // the resulting structures to elasticsearch.
65        echo json_encode( $result );
66        return null;
67    }
68
69    /**
70     * @return string
71     */
72    public function getName() {
73        return 'cirrusdump';
74    }
75
76    /**
77     * @return bool
78     */
79    public function requiresWrite() {
80        return false;
81    }
82
83    /**
84     * @return bool
85     */
86    public function requiresUnblock() {
87        return false;
88    }
89}