Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
QueryCirrusDoc
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 7
156
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 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 addByPageId
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 generateSourceFiltering
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 isInternal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace CirrusSearch\Api;
4
5use MediaWiki\Api\ApiQuery;
6use MediaWiki\Api\ApiQueryBase;
7use MediaWiki\Page\PageIdentity;
8use Wikimedia\ParamValidator\ParamValidator;
9
10/**
11 * Dump stored CirrusSearch document for page.
12 *
13 * This was primarily written for the integration tests, but may be useful
14 * elsewhere. This is functionally similar to web action=cirrusdump but
15 * available and discoverable over the API. Compared to cirrusdump this
16 * also takes pain to try and ensure if there is a related elastic document,
17 * even if its not in-sync with the sql database, we return it. Similarly
18 * if a document in elasticsearch should, but does not, match the requested
19 * page (perhaps a redirect has been created but not indexed yet) it will
20 * not be returned. In this way this tries to faithfully return the document
21 * in elasticsearch that represents the requested page.
22 *
23 * @license GPL-2.0-or-later
24 */
25class QueryCirrusDoc extends ApiQueryBase {
26    use ApiTrait;
27
28    public function __construct( ApiQuery $query, string $moduleName ) {
29        parent::__construct( $query, $moduleName, 'cd' );
30    }
31
32    public function execute() {
33        $sourceFiltering = $this->generateSourceFiltering();
34        foreach ( $this->getPageSet()->getGoodPages() as $origPageId => $title ) {
35            $this->addByPageId( $origPageId, $title, $sourceFiltering );
36        }
37
38        // Not 100% sure we need deletedhistory, but better safe than sorry
39        if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
40            foreach ( $this->getPageSet()->getMissingPages() as $resultPageId => $title ) {
41                $this->addByPageId( $resultPageId, $title, $sourceFiltering );
42            }
43        }
44    }
45
46    /**
47     * @param int $resultPageId The page id as represented in the api result.
48     *  This may be negative for missing pages. If those pages were recently
49     *  deleted they could still be in the elastic index.
50     * @param PageIdentity $title The requested title
51     * @param string[]|bool $sourceFiltering source filtering to apply
52     */
53    private function addByPageId( $resultPageId, PageIdentity $title, $sourceFiltering ) {
54        $this->getResult()->addValue(
55            [ 'query', 'pages', $resultPageId ],
56            'cirrusdoc', $this->loadDocuments( $title, $sourceFiltering )
57        );
58        $this->getResult()->addValue(
59            [ 'query', 'pages', $resultPageId ],
60            'cirrusdoc_comment',
61            'The CirrusDoc format is meant for internal use by CirrusSearch for debugging or queries, '
62            . 'it might change at any time without notice'
63        );
64    }
65
66    /**
67     * @return array|bool
68     */
69    private function generateSourceFiltering() {
70        $params = $this->extractRequestParams();
71        $sourceFiltering = (array)$params['includes'];
72        $includeAll = in_array( 'all', $sourceFiltering );
73
74        if ( !$sourceFiltering || $includeAll ) {
75            return true;
76        } else {
77            return $sourceFiltering;
78        }
79    }
80
81    /** @inheritDoc */
82    public function getAllowedParams() {
83        return [
84            'includes' => [
85                ParamValidator::PARAM_TYPE => 'string',
86                ParamValidator::PARAM_DEFAULT => 'all',
87                ParamValidator::PARAM_ISMULTI => true,
88            ],
89        ];
90    }
91
92    /**
93     * Mark as internal. This isn't meant to be used by normal api users
94     * @return bool
95     */
96    public function isInternal() {
97        return true;
98    }
99
100    /**
101     * @see ApiBase::getExamplesMessages
102     * @return array
103     */
104    protected function getExamplesMessages() {
105        return [
106            'action=query&prop=cirrusdoc&titles=Main_Page' =>
107                'apihelp-query+cirrusdoc-example',
108            'action=query&prop=cirrusdoc&titles=Main_Page&cdincludes=category' =>
109                'apihelp-query+cirrusdoc-example-2'
110        ];
111    }
112
113}