Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
84.21% |
32 / 38 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
ApiQueryZFunctionReference | |
84.21% |
32 / 38 |
|
33.33% |
1 / 3 |
13.67 | |
0.00% |
0 / 1 |
__construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
execute | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
executeGenerator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
run | |
85.29% |
29 / 34 |
|
0.00% |
0 / 1 |
8.20 | |||
getAllowedParams | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
getExamplesMessages | n/a |
0 / 0 |
n/a |
0 / 0 |
1 |
1 | <?php |
2 | /** |
3 | * WikiLambda ZFunction reference helper for the query API |
4 | * |
5 | * @file |
6 | * @ingroup Extensions |
7 | * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt |
8 | * @license MIT |
9 | */ |
10 | |
11 | namespace MediaWiki\Extension\WikiLambda\ActionAPI; |
12 | |
13 | use ApiBase; |
14 | use ApiPageSet; |
15 | use ApiQueryGeneratorBase; |
16 | use MediaWiki\Extension\WikiLambda\ZObjectStore; |
17 | use MediaWiki\Title\Title; |
18 | use Wikimedia\ParamValidator\ParamValidator; |
19 | use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
20 | |
21 | class ApiQueryZFunctionReference extends ApiQueryGeneratorBase { |
22 | |
23 | private ZObjectStore $zObjectStore; |
24 | |
25 | /** |
26 | * @inheritDoc |
27 | * @codeCoverageIgnore |
28 | */ |
29 | public function __construct( |
30 | $query, |
31 | $moduleName, |
32 | ZObjectStore $zObjectStore |
33 | ) { |
34 | parent::__construct( $query, $moduleName, 'wikilambdafn_' ); |
35 | |
36 | $this->zObjectStore = $zObjectStore; |
37 | } |
38 | |
39 | /** |
40 | * @inheritDoc |
41 | */ |
42 | public function execute() { |
43 | // (T362271) Emit appropriate cache headers for a 24 hour TTL |
44 | // NOTE (T362273): MediaWiki out-guesses us and assumes we don't know what we're doing; to fix so it works |
45 | $this->getMain()->setCacheMode( 'public' ); |
46 | $this->getMain()->setCacheMaxAge( 60 * 60 * 24 ); |
47 | |
48 | $this->run(); |
49 | } |
50 | |
51 | /** |
52 | * @inheritDoc |
53 | */ |
54 | public function executeGenerator( $resultPageSet ) { |
55 | $this->run( $resultPageSet ); |
56 | } |
57 | |
58 | /** |
59 | * @param ApiPageSet|null $resultPageSet |
60 | */ |
61 | private function run( $resultPageSet = null ) { |
62 | [ |
63 | 'zfunction_id' => $zFunctionId, |
64 | 'type' => $type, |
65 | 'limit' => $limit, |
66 | 'continue' => $continue, |
67 | ] = $this->extractRequestParams(); |
68 | $result = $this->getResult(); |
69 | $res = $this->zObjectStore->findReferencedZObjectsByZFunctionId( $zFunctionId, $type, $continue, $limit + 1 ); |
70 | |
71 | // If $res has no rows, then return false to indicate that no results were found. |
72 | // This is handled in the UI as an empty list. |
73 | if ( $res->numRows() === 0 ) { |
74 | $result->addValue( [ 'query', $this->getModuleName() ], null, false ); |
75 | } |
76 | |
77 | $zids = []; |
78 | $i = 0; |
79 | $lastId = 0; |
80 | foreach ( $res as $row ) { |
81 | if ( $i >= $limit ) { |
82 | break; |
83 | } |
84 | $zids[] = [ |
85 | 'page_namespace' => NS_MAIN, |
86 | 'zid' => $row->wlzf_ref_zid |
87 | ]; |
88 | $lastId = $row->wlzf_id; |
89 | $i++; |
90 | } |
91 | unset( $i ); |
92 | |
93 | if ( $res->numRows() > $limit ) { |
94 | $this->setContinueEnumParameter( 'continue', strval( $lastId + 1 ) ); |
95 | } |
96 | |
97 | if ( $resultPageSet ) { |
98 | foreach ( $zids as $index => $entry ) { |
99 | $resultPageSet->setGeneratorData( |
100 | Title::makeTitle( $entry['page_namespace'], $entry['zid'] ), |
101 | [ 'index' => $index + $continue + 1 ] |
102 | ); |
103 | } |
104 | } else { |
105 | $result = $this->getResult(); |
106 | foreach ( $zids as $entry ) { |
107 | $result->addValue( [ 'query', $this->getModuleName() ], null, $entry ); |
108 | } |
109 | } |
110 | } |
111 | |
112 | /** |
113 | * @inheritDoc |
114 | * @codeCoverageIgnore |
115 | */ |
116 | protected function getAllowedParams(): array { |
117 | return [ |
118 | 'zfunction_id' => [ |
119 | ParamValidator::PARAM_TYPE => 'string', |
120 | ParamValidator::PARAM_DEFAULT => '' |
121 | ], |
122 | 'type' => [ |
123 | ParamValidator::PARAM_TYPE => 'string', |
124 | ParamValidator::PARAM_DEFAULT => '' |
125 | ], |
126 | 'limit' => [ |
127 | ParamValidator::PARAM_TYPE => 'limit', |
128 | ParamValidator::PARAM_DEFAULT => 10, |
129 | IntegerDef::PARAM_MIN => 1, |
130 | IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
131 | IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2, |
132 | ], |
133 | 'continue' => [ |
134 | ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
135 | ], |
136 | ]; |
137 | } |
138 | |
139 | /** |
140 | * @see ApiBase::getExamplesMessages() |
141 | * @return array |
142 | * @codeCoverageIgnore |
143 | */ |
144 | protected function getExamplesMessages() { |
145 | $exampleZid = $this->zObjectStore->findFirstZImplementationFunction(); |
146 | |
147 | return [ |
148 | "action=query&list=wikilambdafn_search&wikilambdafn_zfunction_id=$exampleZid&wikilambdafn_type=Z14" |
149 | => 'apihelp-query+wikilambdafn_search-example-simple' |
150 | ]; |
151 | } |
152 | } |