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 Title; |
6 | use Wikimedia\ParamValidator\ParamValidator; |
7 | |
8 | /** |
9 | * Dump stored CirrusSearch document for page. |
10 | * |
11 | * This was primarily written for the integration tests, but may be useful |
12 | * elsewhere. This is functionally similar to web action=cirrusdump but |
13 | * available and discoverable over the API. Compared to cirrusdump this |
14 | * also takes pain to try and ensure if there is a related elastic document, |
15 | * even if its not in-sync with the sql database, we return it. Similarly |
16 | * if a document in elasticsearch should, but does not, match the requested |
17 | * page (perhaps a redirect has been created but not indexed yet) it will |
18 | * not be returned. In this way this tries to faithfully return the document |
19 | * in elasticsearch that represents the requested page. |
20 | * |
21 | * This program is free software; you can redistribute it and/or modify |
22 | * it under the terms of the GNU General Public License as published by |
23 | * the Free Software Foundation; either version 2 of the License, or |
24 | * (at your option) any later version. |
25 | * |
26 | * This program is distributed in the hope that it will be useful, |
27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
29 | * GNU General Public License for more details. |
30 | * |
31 | * You should have received a copy of the GNU General Public License along |
32 | * with this program; if not, write to the Free Software Foundation, Inc., |
33 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
34 | * http://www.gnu.org/copyleft/gpl.html |
35 | */ |
36 | class QueryCirrusDoc extends \ApiQueryBase { |
37 | use ApiTrait; |
38 | |
39 | public function __construct( \ApiQuery $query, $moduleName ) { |
40 | parent::__construct( $query, $moduleName, 'cd' ); |
41 | } |
42 | |
43 | public function execute() { |
44 | $sourceFiltering = $this->generateSourceFiltering(); |
45 | foreach ( $this->getPageSet()->getGoodTitles() as $origPageId => $title ) { |
46 | $this->addByPageId( $origPageId, $title, $sourceFiltering ); |
47 | } |
48 | |
49 | // Not 100% sure we need deletedhistory, but better safe than sorry |
50 | if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) { |
51 | foreach ( $this->getPageSet()->getMissingTitles() as $resultPageId => $title ) { |
52 | $this->addByPageId( $resultPageId, $title, $sourceFiltering ); |
53 | } |
54 | } |
55 | } |
56 | |
57 | /** |
58 | * @param int $resultPageId The page id as represented in the api result. |
59 | * This may be negative for missing pages. If those pages were recently |
60 | * deleted they could still be in the elastic index. |
61 | * @param Title $title The requested title |
62 | * @param string[]|bool $sourceFiltering source filtering to apply |
63 | */ |
64 | private function addByPageId( $resultPageId, Title $title, $sourceFiltering ) { |
65 | $this->getResult()->addValue( |
66 | [ 'query', 'pages', $resultPageId ], |
67 | 'cirrusdoc', $this->loadDocuments( $title, $sourceFiltering ) |
68 | ); |
69 | } |
70 | |
71 | /** |
72 | * @return array|bool |
73 | */ |
74 | private function generateSourceFiltering() { |
75 | $params = $this->extractRequestParams(); |
76 | $sourceFiltering = (array)$params['includes']; |
77 | $includeAll = in_array( 'all', $sourceFiltering ); |
78 | |
79 | if ( empty( $sourceFiltering ) || $includeAll ) { |
80 | return true; |
81 | } else { |
82 | return $sourceFiltering; |
83 | } |
84 | } |
85 | |
86 | public function getAllowedParams() { |
87 | return [ |
88 | 'includes' => [ |
89 | ParamValidator::PARAM_TYPE => 'string', |
90 | ParamValidator::PARAM_DEFAULT => 'all', |
91 | ParamValidator::PARAM_ISMULTI => true, |
92 | ], |
93 | ]; |
94 | } |
95 | |
96 | /** |
97 | * @see ApiBase::getExamplesMessages |
98 | * @return array |
99 | */ |
100 | protected function getExamplesMessages() { |
101 | return [ |
102 | 'action=query&prop=cirrusdoc&titles=Main_Page' => |
103 | 'apihelp-query+cirrusdoc-example', |
104 | 'action=query&prop=cirrusdoc&titles=Main_Page&cdincludes=category' => |
105 | 'apihelp-query+cirrusdoc-example-2' |
106 | ]; |
107 | } |
108 | |
109 | } |