Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
73.88% |
99 / 134 |
|
28.57% |
2 / 7 |
CRAP | |
0.00% |
0 / 1 |
ApiTemplateData | |
73.88% |
99 / 134 |
|
28.57% |
2 / 7 |
56.83 | |
0.00% |
0 / 1 |
getCustomPrinter | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
getPageSet | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
76.67% |
69 / 90 |
|
0.00% |
0 / 1 |
29.72 | |||
getRawParams | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
5 | |||
getAllowedParams | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
2.00 | |||
getExamplesMessages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\TemplateData\Api; |
4 | |
5 | use MediaWiki\Api\ApiBase; |
6 | use MediaWiki\Api\ApiContinuationManager; |
7 | use MediaWiki\Api\ApiFormatBase; |
8 | use MediaWiki\Api\ApiPageSet; |
9 | use MediaWiki\Api\ApiResult; |
10 | use MediaWiki\Content\TextContent; |
11 | use MediaWiki\Extension\TemplateData\TemplateDataBlob; |
12 | use MediaWiki\MediaWikiServices; |
13 | use Wikimedia\ParamValidator\ParamValidator; |
14 | |
15 | /** |
16 | * Implement the 'templatedata' query module in the API. |
17 | * Format JSON only. |
18 | * @license GPL-2.0-or-later |
19 | * @ingroup API |
20 | * @emits error.code templatedata-corrupt |
21 | * @todo Support continuation (see I1a6e51cd) |
22 | */ |
23 | class ApiTemplateData extends ApiBase { |
24 | |
25 | private ?ApiPageSet $mPageSet = null; |
26 | |
27 | /** |
28 | * For backwards compatibility, this module needs to output format=json when |
29 | * no format is specified. |
30 | * @return ApiFormatBase|null |
31 | */ |
32 | public function getCustomPrinter() { |
33 | if ( $this->getMain()->getVal( 'format' ) === null ) { |
34 | $this->addDeprecation( |
35 | 'apiwarn-templatedata-deprecation-format', 'action=templatedata&!format' |
36 | ); |
37 | return $this->getMain()->createPrinterByName( 'json' ); |
38 | } |
39 | return null; |
40 | } |
41 | |
42 | private function getPageSet(): ApiPageSet { |
43 | $this->mPageSet ??= new ApiPageSet( $this ); |
44 | return $this->mPageSet; |
45 | } |
46 | |
47 | /** |
48 | * @inheritDoc |
49 | */ |
50 | public function execute() { |
51 | $services = MediaWikiServices::getInstance(); |
52 | $params = $this->extractRequestParams(); |
53 | $result = $this->getResult(); |
54 | |
55 | $continuationManager = new ApiContinuationManager( $this, [], [] ); |
56 | $this->setContinuationManager( $continuationManager ); |
57 | |
58 | if ( $params['lang'] === null ) { |
59 | $langCode = false; |
60 | } elseif ( !$services->getLanguageNameUtils()->isValidCode( $params['lang'] ) ) { |
61 | $this->dieWithError( [ 'apierror-invalidlang', 'lang' ] ); |
62 | } else { |
63 | $langCode = $params['lang']; |
64 | } |
65 | |
66 | $pageSet = $this->getPageSet(); |
67 | $pageSet->execute(); |
68 | $titles = $pageSet->getGoodPages(); |
69 | $missingTitles = $pageSet->getMissingPages(); |
70 | |
71 | $includeMissingTitles = $this->getParameter( 'doNotIgnoreMissingTitles' ) ?: |
72 | $this->getParameter( 'includeMissingTitles' ); |
73 | |
74 | if ( !$titles && ( !$includeMissingTitles || !$missingTitles ) ) { |
75 | $result->addValue( null, 'pages', (object)[] ); |
76 | $this->setContinuationManager(); |
77 | $continuationManager->setContinuationIntoResult( $this->getResult() ); |
78 | return; |
79 | } |
80 | |
81 | $resp = []; |
82 | |
83 | if ( $includeMissingTitles ) { |
84 | foreach ( $missingTitles as $missingTitleId => $missingTitle ) { |
85 | $resp[ $missingTitleId ] = [ 'title' => $missingTitle, 'missing' => true ]; |
86 | } |
87 | |
88 | foreach ( $titles as $titleId => $title ) { |
89 | $resp[ $titleId ] = [ |
90 | 'title' => $title, |
91 | 'notemplatedata' => true, |
92 | 'ns' => $title->getNamespace() |
93 | ]; |
94 | } |
95 | } |
96 | |
97 | if ( $titles ) { |
98 | $db = $this->getDB(); |
99 | $res = $db->newSelectQueryBuilder() |
100 | ->from( 'page_props' ) |
101 | ->fields( [ 'pp_page', 'pp_value' ] ) |
102 | ->where( [ |
103 | 'pp_page' => array_keys( $titles ), |
104 | 'pp_propname' => 'templatedata' |
105 | ] ) |
106 | ->orderBy( 'pp_page' ) |
107 | ->caller( __METHOD__ ) |
108 | ->fetchResultSet(); |
109 | |
110 | foreach ( $res as $row ) { |
111 | $rawData = $row->pp_value; |
112 | $tdb = TemplateDataBlob::newFromDatabase( $db, $rawData ); |
113 | $status = $tdb->getStatus(); |
114 | |
115 | if ( !$status->isOK() ) { |
116 | $this->dieWithError( |
117 | [ 'apierror-templatedata-corrupt', intval( $row->pp_page ), $status->getMessage() ] |
118 | ); |
119 | } |
120 | |
121 | if ( $langCode !== false ) { |
122 | $data = $tdb->getDataInLanguage( $langCode ); |
123 | } else { |
124 | $data = $tdb->getData(); |
125 | } |
126 | |
127 | // HACK: don't let ApiResult's formatversion=1 compatibility layer mangle our booleans |
128 | // to empty strings / absent properties |
129 | foreach ( $data->params as $param ) { |
130 | $param->{ApiResult::META_BC_BOOLS} = [ 'required', 'suggested', 'deprecated' ]; |
131 | } |
132 | |
133 | $data->params->{ApiResult::META_TYPE} = 'kvp'; |
134 | $data->params->{ApiResult::META_KVP_KEY_NAME} = 'key'; |
135 | $data->params->{ApiResult::META_INDEXED_TAG_NAME} = 'param'; |
136 | if ( isset( $data->paramOrder ) ) { |
137 | ApiResult::setIndexedTagName( $data->paramOrder, 'p' ); |
138 | } |
139 | |
140 | if ( $includeMissingTitles ) { |
141 | unset( $resp[$row->pp_page]['notemplatedata'] ); |
142 | } else { |
143 | $resp[ $row->pp_page ] = [ |
144 | 'title' => $titles[ $row->pp_page ], |
145 | 'ns' => $titles[ $row->pp_page ]->getNamespace() |
146 | ]; |
147 | } |
148 | $resp[$row->pp_page] += (array)$data; |
149 | } |
150 | } |
151 | |
152 | $wikiPageFactory = $services->getWikiPageFactory(); |
153 | |
154 | // Now go through all the titles again, and attempt to extract parameter names from the |
155 | // wikitext for templates with no templatedata. |
156 | if ( $includeMissingTitles ) { |
157 | foreach ( $resp as $pageId => $pageInfo ) { |
158 | if ( !isset( $pageInfo['notemplatedata'] ) ) { |
159 | // Ignore pages that already have templatedata or that don't exist. |
160 | continue; |
161 | } |
162 | |
163 | $content = $wikiPageFactory->newFromTitle( $pageInfo['title'] )->getContent(); |
164 | $text = $content instanceof TextContent |
165 | ? $content->getText() |
166 | : $content->getTextForSearchIndex(); |
167 | $resp[$pageId]['params'] = $this->getRawParams( $text ); |
168 | } |
169 | } |
170 | |
171 | $pageSet->populateGeneratorData( $resp ); |
172 | ApiResult::setArrayType( $resp, 'kvp', 'id' ); |
173 | ApiResult::setIndexedTagName( $resp, 'page' ); |
174 | |
175 | // Set top level element |
176 | $result->addValue( null, 'pages', (object)$resp ); |
177 | |
178 | $values = $pageSet->getNormalizedTitlesAsResult(); |
179 | if ( $values ) { |
180 | $result->addValue( null, 'normalized', $values ); |
181 | } |
182 | $redirects = $pageSet->getRedirectTitlesAsResult(); |
183 | if ( $redirects ) { |
184 | $result->addValue( null, 'redirects', $redirects ); |
185 | } |
186 | |
187 | $this->setContinuationManager(); |
188 | $continuationManager->setContinuationIntoResult( $this->getResult() ); |
189 | } |
190 | |
191 | /** |
192 | * Get parameter descriptions from raw wikitext (used for templates that have no templatedata). |
193 | * @param string $wikitext The text to extract parameters from. |
194 | * @return array[] Parameter info in the same format as the templatedata 'params' key. |
195 | */ |
196 | private function getRawParams( string $wikitext ): array { |
197 | // Ignore non-wikitext content in comments and wikitext-escaping tags |
198 | $wikitext = preg_replace( '/<!--.*?-->/s', '', $wikitext ); |
199 | $wikitext = preg_replace( '/<nowiki\s*>.*?<\/nowiki\s*>/s', '', $wikitext ); |
200 | $wikitext = preg_replace( '/<pre\s*>.*?<\/pre\s*>/s', '', $wikitext ); |
201 | |
202 | // This regex matches the one in ext.TemplateDataGenerator.sourceHandler.js |
203 | if ( !preg_match_all( '/{{{+([^\n#={|}]*?)([<|]|}}})/m', $wikitext, $rawParams ) ) { |
204 | return []; |
205 | } |
206 | |
207 | $params = []; |
208 | $normalizedParams = []; |
209 | foreach ( $rawParams[1] as $rawParam ) { |
210 | // This normalization process is repeated in JS in ext.TemplateDataGenerator.sourceHandler.js |
211 | $normalizedParam = strtolower( trim( preg_replace( '/[-_ ]+/', ' ', $rawParam ) ) ); |
212 | if ( !$normalizedParam || in_array( $normalizedParam, $normalizedParams ) ) { |
213 | // This or a similarly-named parameter has already been found. |
214 | continue; |
215 | } |
216 | $normalizedParams[] = $normalizedParam; |
217 | $params[ trim( $rawParam ) ] = []; |
218 | } |
219 | return $params; |
220 | } |
221 | |
222 | /** |
223 | * @inheritDoc |
224 | */ |
225 | public function getAllowedParams( $flags = 0 ) { |
226 | $result = [ |
227 | 'includeMissingTitles' => [ |
228 | ParamValidator::PARAM_TYPE => 'boolean', |
229 | ], |
230 | 'doNotIgnoreMissingTitles' => [ |
231 | ParamValidator::PARAM_TYPE => 'boolean', |
232 | ParamValidator::PARAM_DEPRECATED => true, |
233 | ], |
234 | 'lang' => [ |
235 | ParamValidator::PARAM_TYPE => 'string', |
236 | ], |
237 | ]; |
238 | if ( $flags ) { |
239 | $result += $this->getPageSet()->getFinalParams( $flags ); |
240 | } |
241 | return $result; |
242 | } |
243 | |
244 | /** |
245 | * @inheritDoc |
246 | */ |
247 | protected function getExamplesMessages() { |
248 | return [ |
249 | 'action=templatedata&titles=Template:Foobar&includeMissingTitles=1' |
250 | => 'apihelp-templatedata-example-1', |
251 | 'action=templatedata&titles=Template:Phabricator' |
252 | => 'apihelp-templatedata-example-2', |
253 | ]; |
254 | } |
255 | |
256 | /** |
257 | * @inheritDoc |
258 | */ |
259 | public function getHelpUrls() { |
260 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:TemplateData'; |
261 | } |
262 | |
263 | } |