Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
QueryCirrusDoc | |
0.00% |
0 / 30 |
|
0.00% |
0 / 6 |
132 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
addByPageId | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
generateSourceFiltering | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
getAllowedParams | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace CirrusSearch\Api; |
4 | |
5 | use MediaWiki\Api\ApiQuery; |
6 | use MediaWiki\Api\ApiQueryBase; |
7 | use MediaWiki\Page\PageIdentity; |
8 | use 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 | * This program is free software; you can redistribute it and/or modify |
24 | * it under the terms of the GNU General Public License as published by |
25 | * the Free Software Foundation; either version 2 of the License, or |
26 | * (at your option) any later version. |
27 | * |
28 | * This program is distributed in the hope that it will be useful, |
29 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
30 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
31 | * GNU General Public License for more details. |
32 | * |
33 | * You should have received a copy of the GNU General Public License along |
34 | * with this program; if not, write to the Free Software Foundation, Inc., |
35 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
36 | * http://www.gnu.org/copyleft/gpl.html |
37 | */ |
38 | class QueryCirrusDoc extends ApiQueryBase { |
39 | use ApiTrait; |
40 | |
41 | public function __construct( ApiQuery $query, string $moduleName ) { |
42 | parent::__construct( $query, $moduleName, 'cd' ); |
43 | } |
44 | |
45 | public function execute() { |
46 | $sourceFiltering = $this->generateSourceFiltering(); |
47 | foreach ( $this->getPageSet()->getGoodPages() as $origPageId => $title ) { |
48 | $this->addByPageId( $origPageId, $title, $sourceFiltering ); |
49 | } |
50 | |
51 | // Not 100% sure we need deletedhistory, but better safe than sorry |
52 | if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) { |
53 | foreach ( $this->getPageSet()->getMissingPages() as $resultPageId => $title ) { |
54 | $this->addByPageId( $resultPageId, $title, $sourceFiltering ); |
55 | } |
56 | } |
57 | } |
58 | |
59 | /** |
60 | * @param int $resultPageId The page id as represented in the api result. |
61 | * This may be negative for missing pages. If those pages were recently |
62 | * deleted they could still be in the elastic index. |
63 | * @param PageIdentity $title The requested title |
64 | * @param string[]|bool $sourceFiltering source filtering to apply |
65 | */ |
66 | private function addByPageId( $resultPageId, PageIdentity $title, $sourceFiltering ) { |
67 | $this->getResult()->addValue( |
68 | [ 'query', 'pages', $resultPageId ], |
69 | 'cirrusdoc', $this->loadDocuments( $title, $sourceFiltering ) |
70 | ); |
71 | } |
72 | |
73 | /** |
74 | * @return array|bool |
75 | */ |
76 | private function generateSourceFiltering() { |
77 | $params = $this->extractRequestParams(); |
78 | $sourceFiltering = (array)$params['includes']; |
79 | $includeAll = in_array( 'all', $sourceFiltering ); |
80 | |
81 | if ( !$sourceFiltering || $includeAll ) { |
82 | return true; |
83 | } else { |
84 | return $sourceFiltering; |
85 | } |
86 | } |
87 | |
88 | public function getAllowedParams() { |
89 | return [ |
90 | 'includes' => [ |
91 | ParamValidator::PARAM_TYPE => 'string', |
92 | ParamValidator::PARAM_DEFAULT => 'all', |
93 | ParamValidator::PARAM_ISMULTI => true, |
94 | ], |
95 | ]; |
96 | } |
97 | |
98 | /** |
99 | * @see ApiBase::getExamplesMessages |
100 | * @return array |
101 | */ |
102 | protected function getExamplesMessages() { |
103 | return [ |
104 | 'action=query&prop=cirrusdoc&titles=Main_Page' => |
105 | 'apihelp-query+cirrusdoc-example', |
106 | 'action=query&prop=cirrusdoc&titles=Main_Page&cdincludes=category' => |
107 | 'apihelp-query+cirrusdoc-example-2' |
108 | ]; |
109 | } |
110 | |
111 | } |