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