Merge "Move last remaining HTML formating code out of blob class"

This commit is contained in:
jenkins-bot 2022-02-04 17:06:25 +00:00 committed by Gerrit Code Review
commit 8793641465
4 changed files with 25 additions and 20 deletions

View file

@ -228,7 +228,10 @@ class Hooks {
// FIXME: this hard-codes default skin, but it is needed because
// ::getHtml() will need a theme singleton to be set.
OutputPage::setupOOUI( 'bogus', $userLang->getDir() );
return $ti->getHtml( $userLang );
$localizer = new TemplateDataMessageLocalizer( $userLang );
$formatter = new TemplateDataHtmlFormatter( $localizer, $userLang->getCode() );
return $formatter->getHtml( $ti );
}
/**

View file

@ -6,7 +6,6 @@
namespace MediaWiki\Extension\TemplateData;
use Language;
use MediaWiki\MediaWikiServices;
use Status;
use stdClass;
@ -225,16 +224,6 @@ class TemplateDataBlob {
return $this->getJSON();
}
/**
* @param Language $lang
*
* @return string
*/
public function getHtml( Language $lang ): string {
$formatter = new TemplateDataHtmlFormatter( new TemplateDataMessageLocalizer( $lang ) );
return $formatter->getHtml( $this->getDataInLanguage( $lang->getCode() ) );
}
/**
* Get parameter descriptions from raw wikitext (used for templates that have no templatedata).
* @param string $wikitext The text to extract parameters from.

View file

@ -4,26 +4,32 @@ namespace MediaWiki\Extension\TemplateData;
use Html;
use MessageLocalizer;
use stdClass;
class TemplateDataHtmlFormatter {
/** @var MessageLocalizer */
private $localizer;
/** @var string */
private $languageCode;
/**
* @param MessageLocalizer $localizer
* @param string $languageCode
*/
public function __construct( MessageLocalizer $localizer ) {
public function __construct( MessageLocalizer $localizer, string $languageCode = 'en' ) {
$this->localizer = $localizer;
$this->languageCode = $languageCode;
}
/**
* @param stdClass $data
* @param TemplateDataBlob $templateData
*
* @return string
* @return string HTML
*/
public function getHtml( stdClass $data ): string {
public function getHtml( TemplateDataBlob $templateData ): string {
$data = $templateData->getDataInLanguage( $this->languageCode );
if ( is_string( $data->format ) && isset( TemplateDataValidator::PREDEFINED_FORMATS[$data->format] ) ) {
// The following icon names are used here:
// * template-format-block

View file

@ -1,6 +1,7 @@
<?php
use MediaWiki\Extension\TemplateData\TemplateDataBlob;
use MediaWiki\Extension\TemplateData\TemplateDataHtmlFormatter;
use MediaWiki\Extension\TemplateData\TemplateDataValidator;
use Wikimedia\TestingAccessWrapper;
@ -9,7 +10,6 @@ use Wikimedia\TestingAccessWrapper;
* @group Database
* @covers \MediaWiki\Extension\TemplateData\TemplateDataBlob
* @covers \MediaWiki\Extension\TemplateData\TemplateDataCompressedBlob
* @covers \MediaWiki\Extension\TemplateData\TemplateDataHtmlFormatter
* @covers \MediaWiki\Extension\TemplateData\TemplateDataValidator
*/
class TemplateDataBlobTest extends MediaWikiIntegrationTestCase {
@ -1471,15 +1471,22 @@ HTML
}
/**
* @covers \MediaWiki\Extension\TemplateData\TemplateDataHtmlFormatter
* @dataProvider provideGetHtml
*/
public function testGetHtml( array $data, $expected ) {
$t = TemplateDataBlob::newFromJSON( $this->db, json_encode( $data ) );
$actual = $t->getHtml( Language::factory( 'qqx' ) );
$localizer = new class implements MessageLocalizer {
public function msg( $key, ...$params ) {
return new RawMessage( "($key)" );
}
};
$formatter = new TemplateDataHtmlFormatter( $localizer );
$actual = $formatter->getHtml( $t );
$linedActual = preg_replace( '/>\s*</', ">\n<", $actual );
$linedExpected = preg_replace( '/>\s*</', ">\n<", trim( $expected ) );
$this->assertSame( $linedExpected, $linedActual, 'html' );
$this->assertSame( $linedExpected, $linedActual );
}
}