Merge "Move getRawParams helper method to ApiTemplateData class"

This commit is contained in:
jenkins-bot 2022-02-10 05:44:14 +00:00 committed by Gerrit Code Review
commit 67d6b77a84
4 changed files with 38 additions and 33 deletions

View file

@ -170,7 +170,7 @@ class ApiTemplateData extends ApiBase {
$text = $content instanceof TextContent
? $content->getText()
: $content->getTextForSearchIndex();
$resp[ $pageId ][ 'params' ] = TemplateDataBlob::getRawParams( $text );
$resp[$pageId]['params'] = $this->getRawParams( $text );
}
}
@ -210,6 +210,36 @@ class ApiTemplateData extends ApiBase {
$continuationManager->setContinuationIntoResult( $this->getResult() );
}
/**
* Get parameter descriptions from raw wikitext (used for templates that have no templatedata).
* @param string $wikitext The text to extract parameters from.
* @return array[] Parameter info in the same format as the templatedata 'params' key.
*/
private function getRawParams( string $wikitext ): array {
// Ignore wikitext within nowiki tags and comments
$wikitext = preg_replace( '/<!--.*?-->/s', '', $wikitext );
$wikitext = preg_replace( '/<nowiki\s*>.*?<\/nowiki\s*>/s', '', $wikitext );
// This regex matches the one in ext.TemplateDataGenerator.sourceHandler.js
if ( !preg_match_all( '/{{{+([^\n#={|}]*?)([<|]|}}})/m', $wikitext, $rawParams ) ) {
return [];
}
$params = [];
$normalizedParams = [];
foreach ( $rawParams[1] as $rawParam ) {
// This normalization process is repeated in JS in ext.TemplateDataGenerator.sourceHandler.js
$normalizedParam = strtolower( trim( preg_replace( '/[-_ ]+/', ' ', $rawParam ) ) );
if ( !$normalizedParam || in_array( $normalizedParam, $normalizedParams ) ) {
// This or a similarly-named parameter has already been found.
continue;
}
$normalizedParams[] = $normalizedParam;
$params[ trim( $rawParam ) ] = [];
}
return $params;
}
/**
* @inheritDoc
*/

View file

@ -224,35 +224,6 @@ class TemplateDataBlob {
return $this->getJSON();
}
/**
* Get parameter descriptions from raw wikitext (used for templates that have no templatedata).
* @param string $wikitext The text to extract parameters from.
* @return string[] Parameter info in the same format as the templatedata 'params' key.
*/
public static function getRawParams( string $wikitext ): array {
// Ignore wikitext within nowiki tags and comments
$wikitext = preg_replace( '/<!--.*?-->/s', '', $wikitext );
$wikitext = preg_replace( '/<nowiki\s*>.*?<\/nowiki\s*>/s', '', $wikitext );
// This regex matches the one in ext.TemplateDataGenerator.sourceHandler.js
preg_match_all( '/{{3,}([^\n#={|}]*?)([<|]|}{3,})/m', $wikitext, $rawParams );
$params = [];
$normalizedParams = [];
if ( isset( $rawParams[1] ) ) {
foreach ( $rawParams[1] as $rawParam ) {
// This normalization process is repeated in JS in ext.TemplateDataGenerator.sourceHandler.js
$normalizedParam = strtolower( trim( preg_replace( '/[-_ ]+/', ' ', $rawParam ) ) );
if ( !$normalizedParam || in_array( $normalizedParam, $normalizedParams ) ) {
// This or a similarly-named parameter has already been found.
continue;
}
$normalizedParams[] = $normalizedParam;
$params[ trim( $rawParam ) ] = [];
}
}
return $params;
}
/**
* @param mixed $data
*/

View file

@ -171,7 +171,7 @@ SourceHandler.prototype.extractParametersFromTemplateCode = function ( templateC
var paramNames = [],
normalizedParamNames = [],
// This regex matches the one in TemplateDataBlob.php
paramExtractor = /{{3,}([^\n#={|}]*?)([<|]|}{3,})/mg;
paramExtractor = /{{{+([^\n#={|}]*?)([<|]|}}})/mg;
// Strip everything in nowiki tags and HTML comments
templateCode = templateCode.replace( /<!--[\s\S]*?-->/g, '' )

View file

@ -1,5 +1,6 @@
<?php
use MediaWiki\Extension\TemplateData\Api\ApiTemplateData;
use MediaWiki\Extension\TemplateData\TemplateDataBlob;
use MediaWiki\Extension\TemplateData\TemplateDataHtmlFormatter;
use MediaWiki\Extension\TemplateData\TemplateDataValidator;
@ -1356,10 +1357,13 @@ class TemplateDataBlobTest extends MediaWikiIntegrationTestCase {
}
/**
* @covers \MediaWiki\Extension\TemplateData\Api\ApiTemplateData
* @dataProvider provideGetRawParams
*/
public function testGetRawParams( $inputWikitext, $expectedParams ) {
$params = TemplateDataBlob::getRawParams( $inputWikitext );
public function testGetRawParams( string $inputWikitext, array $expectedParams ) {
/** @var ApiTemplateData $api */
$api = TestingAccessWrapper::newFromObject( $this->createMock( ApiTemplateData::class ) );
$params = $api->getRawParams( $inputWikitext );
$this->assertArrayEquals( $expectedParams, $params, true, true );
}