Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
72.46% |
100 / 138 |
|
28.57% |
2 / 7 |
CRAP | |
0.00% |
0 / 1 |
| ApiTemplateData | |
72.46% |
100 / 138 |
|
28.57% |
2 / 7 |
63.06 | |
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 | |
74.47% |
70 / 94 |
|
0.00% |
0 / 1 |
33.59 | |||
| 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 MediaWiki\Status\Status; |
| 14 | use Wikimedia\ParamValidator\ParamValidator; |
| 15 | |
| 16 | /** |
| 17 | * Implement the 'templatedata' query module in the API. |
| 18 | * Format JSON only. |
| 19 | * @license GPL-2.0-or-later |
| 20 | * @ingroup API |
| 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', |
| 118 | intval( $row->pp_page ), |
| 119 | Status::wrap( $status )->getMessage() |
| 120 | ] ); |
| 121 | } |
| 122 | |
| 123 | if ( $langCode !== false ) { |
| 124 | $data = $tdb->getDataInLanguage( $langCode ); |
| 125 | } else { |
| 126 | $data = $tdb->getData(); |
| 127 | } |
| 128 | |
| 129 | // HACK: don't let ApiResult's formatversion=1 compatibility layer mangle our booleans |
| 130 | // to empty strings / absent properties |
| 131 | foreach ( $data->params as $param ) { |
| 132 | $param->{ApiResult::META_BC_BOOLS} = [ 'required', 'suggested', 'deprecated' ]; |
| 133 | } |
| 134 | |
| 135 | $data->params->{ApiResult::META_TYPE} = 'kvp'; |
| 136 | $data->params->{ApiResult::META_KVP_KEY_NAME} = 'key'; |
| 137 | $data->params->{ApiResult::META_INDEXED_TAG_NAME} = 'param'; |
| 138 | if ( isset( $data->paramOrder ) ) { |
| 139 | ApiResult::setIndexedTagName( $data->paramOrder, 'p' ); |
| 140 | } |
| 141 | |
| 142 | if ( $includeMissingTitles ) { |
| 143 | unset( $resp[$row->pp_page]['notemplatedata'] ); |
| 144 | } else { |
| 145 | $resp[ $row->pp_page ] = [ |
| 146 | 'title' => $titles[ $row->pp_page ], |
| 147 | 'ns' => $titles[ $row->pp_page ]->getNamespace() |
| 148 | ]; |
| 149 | } |
| 150 | $resp[$row->pp_page] += (array)$data; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | $wikiPageFactory = $services->getWikiPageFactory(); |
| 155 | |
| 156 | // Now go through all the titles again, and attempt to extract parameter names from the |
| 157 | // wikitext for templates with no templatedata. |
| 158 | if ( $includeMissingTitles ) { |
| 159 | foreach ( $resp as $pageId => $pageInfo ) { |
| 160 | if ( !isset( $pageInfo['notemplatedata'] ) ) { |
| 161 | // Ignore pages that already have templatedata or that don't exist. |
| 162 | continue; |
| 163 | } |
| 164 | |
| 165 | $content = $wikiPageFactory->newFromTitle( $pageInfo['title'] )->getContent(); |
| 166 | if ( !$content ) { |
| 167 | continue; |
| 168 | } |
| 169 | $text = $content instanceof TextContent |
| 170 | ? $content->getText() |
| 171 | : $content->getTextForSearchIndex(); |
| 172 | $resp[$pageId]['params'] = $this->getRawParams( $text ); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | $pageSet->populateGeneratorData( $resp ); |
| 177 | ApiResult::setArrayType( $resp, 'kvp', 'id' ); |
| 178 | ApiResult::setIndexedTagName( $resp, 'page' ); |
| 179 | |
| 180 | // Set top level element |
| 181 | $result->addValue( null, 'pages', (object)$resp ); |
| 182 | |
| 183 | $values = $pageSet->getNormalizedTitlesAsResult(); |
| 184 | if ( $values ) { |
| 185 | $result->addValue( null, 'normalized', $values ); |
| 186 | } |
| 187 | $redirects = $pageSet->getRedirectTitlesAsResult(); |
| 188 | if ( $redirects ) { |
| 189 | $result->addValue( null, 'redirects', $redirects ); |
| 190 | } |
| 191 | |
| 192 | $this->setContinuationManager(); |
| 193 | $continuationManager->setContinuationIntoResult( $this->getResult() ); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Get parameter descriptions from raw wikitext (used for templates that have no templatedata). |
| 198 | * @param string $wikitext The text to extract parameters from. |
| 199 | * @return array[] Parameter info in the same format as the templatedata 'params' key. |
| 200 | */ |
| 201 | private function getRawParams( string $wikitext ): array { |
| 202 | // Ignore non-wikitext content in comments and wikitext-escaping tags |
| 203 | $wikitext = preg_replace( '/<!--.*?-->/s', '', $wikitext ); |
| 204 | $wikitext = preg_replace( '/<nowiki\s*>.*?<\/nowiki\s*>/s', '', $wikitext ); |
| 205 | $wikitext = preg_replace( '/<pre\s*>.*?<\/pre\s*>/s', '', $wikitext ); |
| 206 | |
| 207 | // This regex matches the one in ext.TemplateDataGenerator.sourceHandler.js |
| 208 | if ( !preg_match_all( '/{{{+([^\n#={|}]*?)([<|]|}}})/m', $wikitext, $rawParams ) ) { |
| 209 | return []; |
| 210 | } |
| 211 | |
| 212 | $params = []; |
| 213 | $normalizedParams = []; |
| 214 | foreach ( $rawParams[1] as $rawParam ) { |
| 215 | // This normalization process is repeated in JS in ext.TemplateDataGenerator.sourceHandler.js |
| 216 | $normalizedParam = strtolower( trim( preg_replace( '/[-_ ]+/', ' ', $rawParam ) ) ); |
| 217 | if ( !$normalizedParam || in_array( $normalizedParam, $normalizedParams ) ) { |
| 218 | // This or a similarly-named parameter has already been found. |
| 219 | continue; |
| 220 | } |
| 221 | $normalizedParams[] = $normalizedParam; |
| 222 | $params[ trim( $rawParam ) ] = []; |
| 223 | } |
| 224 | return $params; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * @inheritDoc |
| 229 | */ |
| 230 | public function getAllowedParams( $flags = 0 ) { |
| 231 | $result = [ |
| 232 | 'includeMissingTitles' => [ |
| 233 | ParamValidator::PARAM_TYPE => 'boolean', |
| 234 | ], |
| 235 | 'doNotIgnoreMissingTitles' => [ |
| 236 | ParamValidator::PARAM_TYPE => 'boolean', |
| 237 | ParamValidator::PARAM_DEPRECATED => true, |
| 238 | ], |
| 239 | 'lang' => [ |
| 240 | ParamValidator::PARAM_TYPE => 'string', |
| 241 | ], |
| 242 | ]; |
| 243 | if ( $flags ) { |
| 244 | $result += $this->getPageSet()->getFinalParams( $flags ); |
| 245 | } |
| 246 | return $result; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * @inheritDoc |
| 251 | */ |
| 252 | protected function getExamplesMessages() { |
| 253 | return [ |
| 254 | 'action=templatedata&titles=Template:Foobar&includeMissingTitles=1' |
| 255 | => 'apihelp-templatedata-example-1', |
| 256 | 'action=templatedata&titles=Template:Phabricator' |
| 257 | => 'apihelp-templatedata-example-2', |
| 258 | ]; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * @inheritDoc |
| 263 | */ |
| 264 | public function getHelpUrls() { |
| 265 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:TemplateData'; |
| 266 | } |
| 267 | |
| 268 | } |