Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.41% |
73 / 79 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
QueryBuildDocument | |
92.41% |
73 / 79 |
|
50.00% |
2 / 4 |
16.11 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
96.61% |
57 / 59 |
|
0.00% |
0 / 1 |
13 | |||
getAllowedParams | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 | |||
getExamplesMessages | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace CirrusSearch\Api; |
4 | |
5 | use ApiBase; |
6 | use CirrusSearch\BuildDocument\BuildDocument; |
7 | use CirrusSearch\BuildDocument\DocumentSizeLimiter; |
8 | use CirrusSearch\CirrusSearch; |
9 | use CirrusSearch\Profile\SearchProfileService; |
10 | use CirrusSearch\Search\CirrusIndexField; |
11 | use CirrusSearch\SearchConfig; |
12 | use MediaWiki\MediaWikiServices; |
13 | use 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 | */ |
33 | class 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 | |
45 | $builders = $this->getParameter( 'builders' ); |
46 | $profile = $this->getParameter( 'limiterprofile' ); |
47 | $flags = 0; |
48 | if ( !in_array( 'content', $builders ) ) { |
49 | $flags |= BuildDocument::SKIP_PARSE; |
50 | } |
51 | if ( !in_array( 'links', $builders ) ) { |
52 | $flags |= BuildDocument::SKIP_LINKS; |
53 | } |
54 | |
55 | if ( $engine instanceof CirrusSearch ) { |
56 | $pages = []; |
57 | $wikiPageFactory = $services->getWikiPageFactory(); |
58 | $revisionStore = $services->getRevisionStore(); |
59 | $revisionBased = false; |
60 | if ( $this->getPageSet()->getRevisionIDs() ) { |
61 | $revisionBased = true; |
62 | foreach ( $this->getPageSet()->getRevisionIDs() as $revId => $pageId ) { |
63 | $pages[$revId] = $revisionStore->getRevisionById( $revId ); |
64 | } |
65 | } else { |
66 | foreach ( $this->getPageSet()->getGoodPages() as $pageId => $title ) { |
67 | $pages[$pageId] = $wikiPageFactory->newFromTitle( $title ); |
68 | } |
69 | } |
70 | |
71 | $builder = new BuildDocument( |
72 | $this->getCirrusConnection(), |
73 | $this->getDB(), |
74 | $services->getRevisionStore(), |
75 | $services->getBacklinkCacheFactory(), |
76 | new DocumentSizeLimiter( $engine->getConfig()->getProfileService() |
77 | ->loadProfile( SearchProfileService::DOCUMENT_SIZE_LIMITER, SearchProfileService::CONTEXT_DEFAULT, $profile ) ), |
78 | $services->getTitleFormatter(), |
79 | $services->getWikiPageFactory() |
80 | ); |
81 | $baseMetadata = []; |
82 | $clusterGroup = $engine->getConfig()->getClusterAssignment()->getCrossClusterName(); |
83 | if ( $clusterGroup !== null ) { |
84 | $baseMetadata['cluster_group'] = $clusterGroup; |
85 | } |
86 | $docs = $builder->initialize( $pages, $flags ); |
87 | foreach ( $docs as $pageId => $doc ) { |
88 | $revisionId = $doc->get( 'version' ); |
89 | $revision = $revisionBased ? $pages[$revisionId] : null; |
90 | if ( $builder->finalize( $doc, false, $revision ) ) { |
91 | $result->addValue( |
92 | [ 'query', 'pages', $pageId ], |
93 | 'cirrusbuilddoc', $doc->getData() |
94 | ); |
95 | $hints = CirrusIndexField::getHint( $doc, CirrusIndexField::NOOP_HINT ); |
96 | $metadata = []; |
97 | if ( $hints !== null ) { |
98 | $metadata = $baseMetadata + [ 'noop_hints' => $hints ]; |
99 | } |
100 | $limiterStats = CirrusIndexField::getHint( $doc, DocumentSizeLimiter::HINT_DOC_SIZE_LIMITER_STATS ); |
101 | if ( $limiterStats !== null ) { |
102 | $metadata += [ 'size_limiter_stats' => $limiterStats ]; |
103 | } |
104 | $indexName = $this->getCirrusConnection()->getIndexName( $this->getConfig()->get( SearchConfig::INDEX_BASE_NAME ), |
105 | $this->getCirrusConnection()->getIndexSuffixForNamespace( $doc->get( 'namespace' ) ) ); |
106 | $metadata += [ |
107 | 'index_name' => $indexName |
108 | ]; |
109 | |
110 | $result->addValue( [ 'query', 'pages', $pageId ], |
111 | 'cirrusbuilddoc_metadata', $metadata ); |
112 | } |
113 | } |
114 | } else { |
115 | throw new \RuntimeException( 'Could not create cirrus engine' ); |
116 | } |
117 | } |
118 | |
119 | public function getAllowedParams() { |
120 | return [ |
121 | 'builders' => [ |
122 | ParamValidator::PARAM_DEFAULT => [ 'content', 'links' ], |
123 | ParamValidator::PARAM_ISMULTI => true, |
124 | ParamValidator::PARAM_ALLOW_DUPLICATES => false, |
125 | ParamValidator::PARAM_TYPE => [ |
126 | 'content', |
127 | 'links', |
128 | ], |
129 | ApiBase::PARAM_HELP_MSG => 'apihelp-query+cirrusbuilddoc-param-builders', |
130 | ], |
131 | 'limiterprofile' => [ |
132 | ParamValidator::PARAM_TYPE => 'string' |
133 | ], |
134 | ]; |
135 | } |
136 | |
137 | /** |
138 | * @see ApiBase::getExamplesMessages |
139 | * @return array |
140 | */ |
141 | protected function getExamplesMessages() { |
142 | return [ |
143 | 'action=query&prop=cirrusbuilddoc&titles=Main_Page' => |
144 | 'apihelp-query+cirrusbuilddoc-example' |
145 | ]; |
146 | } |
147 | |
148 | } |