Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.66% covered (warning)
88.66%
86 / 97
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
QueryBuildDocument
88.66% covered (warning)
88.66%
86 / 97
40.00% covered (danger)
40.00%
2 / 5
22.71
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
90.91% covered (success)
90.91%
60 / 66
0.00% covered (danger)
0.00%
0 / 1
14.15
 getRevisionIDs
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
5.02
 getAllowedParams
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace CirrusSearch\Api;
4
5use ApiBase;
6use CirrusSearch\BuildDocument\BuildDocument;
7use CirrusSearch\BuildDocument\DocumentSizeLimiter;
8use CirrusSearch\CirrusSearch;
9use CirrusSearch\Profile\SearchProfileService;
10use CirrusSearch\Search\CirrusIndexField;
11use CirrusSearch\SearchConfig;
12use MediaWiki\MediaWikiServices;
13use Wikimedia\ParamValidator\ParamValidator;
14
15/**
16 * Generate CirrusSearch document for page.
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License along
29 * with this program; if not, write to the Free Software Foundation, Inc.,
30 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 * http://www.gnu.org/copyleft/gpl.html
32 */
33class QueryBuildDocument extends \ApiQueryBase {
34    use ApiTrait;
35
36    public function __construct( \ApiQuery $query, $moduleName ) {
37        parent::__construct( $query, $moduleName, 'cb' );
38    }
39
40    public function execute() {
41        $result = $this->getResult();
42        $services = MediaWikiServices::getInstance();
43        $engine = $services->getSearchEngineFactory()->create();
44        if ( !( $engine instanceof CirrusSearch ) ) {
45            throw new \RuntimeException( 'Could not create cirrus engine' );
46        }
47
48        $builders = $this->getParameter( 'builders' );
49        $profile = $this->getParameter( 'limiterprofile' );
50        $flags = 0;
51        if ( !in_array( 'content', $builders ) ) {
52            $flags |= BuildDocument::SKIP_PARSE;
53        }
54        if ( !in_array( 'links', $builders ) ) {
55            $flags |= BuildDocument::SKIP_LINKS;
56        }
57
58        $pages = [];
59        $wikiPageFactory = $services->getWikiPageFactory();
60        $revisionStore = $services->getRevisionStore();
61        $revisionBased = false;
62        if ( $this->getPageSet()->getRevisionIDs() ) {
63            $revisionBased = true;
64            foreach ( $this->getRevisionIDs() as $pageId => $revId ) {
65                $rev = $revisionStore->getRevisionById( $revId );
66                if ( $rev->audienceCan( $rev::DELETED_TEXT, $rev::FOR_PUBLIC ) ) {
67                    $pages[$pageId] = $rev;
68                } else {
69                    // While the user might have permissions, we want to limit
70                    // what could possibly be indexed to that which is public.
71                    // For an anon this would fail deeper in the system
72                    // anyways, this early check mostly avoids blowing up deep
73                    // in the bowels.
74                    $result->addValue(
75                        [ 'query', 'pages', $pageId ],
76                        'texthidden', true
77                    );
78                }
79            }
80        } else {
81            foreach ( $this->getPageSet()->getGoodPages() as $pageId => $title ) {
82                $pages[$pageId] = $wikiPageFactory->newFromTitle( $title );
83            }
84        }
85
86        $searchConfig = $engine->getConfig();
87        $builder = new BuildDocument(
88            $this->getCirrusConnection(),
89            $this->getDB(),
90            $services->getRevisionStore(),
91            $services->getBacklinkCacheFactory(),
92            new DocumentSizeLimiter( $searchConfig->getProfileService()
93                ->loadProfile( SearchProfileService::DOCUMENT_SIZE_LIMITER, SearchProfileService::CONTEXT_DEFAULT, $profile ) ),
94            $services->getTitleFormatter(),
95            $services->getWikiPageFactory()
96        );
97        $baseMetadata = [];
98        $clusterGroup = $searchConfig->getClusterAssignment()->getCrossClusterName();
99        if ( $clusterGroup !== null ) {
100            $baseMetadata['cluster_group'] = $clusterGroup;
101        }
102        $docs = $builder->initialize( $pages, $flags );
103        foreach ( $docs as $pageId => $doc ) {
104            $pageId = $doc->get( 'page_id' );
105            $revision = $revisionBased ? $pages[$pageId] : null;
106            if ( $builder->finalize( $doc, false, $revision ) ) {
107                $result->addValue(
108                    [ 'query', 'pages', $pageId ],
109                    'cirrusbuilddoc', $doc->getData()
110                );
111                $hints = CirrusIndexField::getHint( $doc, CirrusIndexField::NOOP_HINT );
112                $metadata = [];
113                if ( $hints !== null ) {
114                    $metadata = $baseMetadata + [ 'noop_hints' => $hints ];
115                }
116                $limiterStats = CirrusIndexField::getHint( $doc, DocumentSizeLimiter::HINT_DOC_SIZE_LIMITER_STATS );
117                if ( $limiterStats !== null ) {
118                    $metadata += [ 'size_limiter_stats' => $limiterStats ];
119                }
120                $indexName = $this->getCirrusConnection()->getIndexName( $searchConfig->get( SearchConfig::INDEX_BASE_NAME ),
121                    $this->getCirrusConnection()->getIndexSuffixForNamespace( $doc->get( 'namespace' ) ) );
122                $metadata += [
123                    'index_name' => $indexName
124                ];
125
126                $result->addValue( [ 'query', 'pages', $pageId ],
127                    'cirrusbuilddoc_metadata', $metadata );
128            }
129        }
130    }
131
132    private function getRevisionIDs(): array {
133        $result = [];
134        $warning = false;
135        foreach ( $this->getPageSet()->getRevisionIDs() as $revId => $pageId ) {
136            if ( isset( $result[$pageId] ) ) {
137                $warning = true;
138                if ( $result[$pageId] >= $revId ) {
139                    continue;
140                }
141            }
142            $result[$pageId] = $revId;
143        }
144        if ( $warning ) {
145            $this->addWarning( [ 'apiwarn-cirrus-ignore-revisions' ] );
146        }
147        return $result;
148    }
149
150    public function getAllowedParams() {
151        return [
152            'builders' => [
153                ParamValidator::PARAM_DEFAULT => [ 'content', 'links' ],
154                ParamValidator::PARAM_ISMULTI => true,
155                ParamValidator::PARAM_ALLOW_DUPLICATES => false,
156                ParamValidator::PARAM_TYPE => [
157                    'content',
158                    'links',
159                ],
160                ApiBase::PARAM_HELP_MSG => 'apihelp-query+cirrusbuilddoc-param-builders',
161            ],
162            'limiterprofile' => [
163                ParamValidator::PARAM_TYPE => 'string'
164            ],
165        ];
166    }
167
168    /**
169     * @see ApiBase::getExamplesMessages
170     * @return array
171     */
172    protected function getExamplesMessages() {
173        return [
174            'action=query&prop=cirrusbuilddoc&titles=Main_Page' =>
175                'apihelp-query+cirrusbuilddoc-example'
176        ];
177    }
178
179}