Merge "Use dependency injection for References/RefProcessor"

This commit is contained in:
jenkins-bot 2024-09-25 17:18:42 +00:00 committed by Gerrit Code Review
commit 3b197da578
5 changed files with 64 additions and 28 deletions

View file

@ -317,10 +317,15 @@
"value": false
},
"CiteResponsiveReferences": {
"description": "If long <references /> lists with more than 10 references should behave responsive by default and be displayed in two or more columns. This can also be toggled individually with <references responsive /> to enable and <references responsive=\"0\" /> to disable it.",
"description": "If long <references /> lists with more than $wgCiteResponsiveReferencesThreshold references should behave responsive by default and be displayed in two or more columns. This can also be toggled individually with <references responsive /> to enable and <references responsive=\"0\" /> to disable it.",
"public": true,
"value": true
},
"CiteResponsiveReferencesThreshold": {
"description": "The number of references at which CiteResponsiveReferences is enabled.",
"public": true,
"value": 10
},
"CiteReferencePreviews": {
"description": "Feature flag to enable or disable the popups provided by the Popups extension for <ref> tags.",
"value": true
@ -348,7 +353,12 @@
{
"name": "Cite",
"domProcessors": [
"Cite\\Parsoid\\RefProcessor"
{
"class": "Cite\\Parsoid\\RefProcessor",
"services": [
"MainConfig"
]
}
],
"tags": [
{
@ -364,7 +374,12 @@
},
{
"name": "references",
"handler": "Cite\\Parsoid\\References",
"handler": {
"class": "Cite\\Parsoid\\References",
"services": [
"MainConfig"
]
},
"options": {
"wt2html": {
"embedsHTMLInAttributes": true

View file

@ -3,6 +3,7 @@ declare( strict_types = 1 );
namespace Cite\Parsoid;
use MediaWiki\Config\Config;
use Wikimedia\Parsoid\DOM\Element;
use Wikimedia\Parsoid\DOM\Node;
use Wikimedia\Parsoid\Ext\DOMProcessor;
@ -13,6 +14,11 @@ use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
* @license GPL-2.0-or-later
*/
class RefProcessor extends DOMProcessor {
private Config $mainConfig;
public function __construct( Config $mainConfig ) {
$this->mainConfig = $mainConfig;
}
/**
* @inheritDoc
@ -21,10 +27,11 @@ class RefProcessor extends DOMProcessor {
ParsoidExtensionAPI $extApi, Node $node, array $options
): void {
$refsData = new ReferencesData();
References::processRefs( $extApi, $refsData, $node );
References::insertMissingReferencesIntoDOM( $extApi, $refsData, $node );
$references = new References( $this->mainConfig );
$references->processRefs( $extApi, $refsData, $node );
$references->insertMissingReferencesIntoDOM( $extApi, $refsData, $node );
if ( $refsData->embeddedErrors ) {
References::addEmbeddedErrors( $extApi, $refsData, $node );
$references->addEmbeddedErrors( $extApi, $refsData, $node );
}
}

View file

@ -7,6 +7,7 @@ declare( strict_types = 1 );
namespace Cite\Parsoid;
use Closure;
use MediaWiki\Config\Config;
use stdClass;
use Wikimedia\Parsoid\Core\DomSourceRange;
use Wikimedia\Parsoid\DOM\DocumentFragment;
@ -26,6 +27,11 @@ use Wikimedia\Parsoid\Utils\DOMCompat;
* @license GPL-2.0-or-later
*/
class References extends ExtensionTagHandler {
private Config $mainConfig;
public function __construct( Config $mainConfig ) {
$this->mainConfig = $mainConfig;
}
private static function hasRef( Node $node ): bool {
$c = $node->firstChild;
@ -43,7 +49,7 @@ class References extends ExtensionTagHandler {
return false;
}
private static function createReferences(
private function createReferences(
ParsoidExtensionAPI $extApi, DocumentFragment $domFragment,
array $refsOpts, ?callable $modifyDp, bool $autoGenerated = false
): Element {
@ -58,7 +64,7 @@ class References extends ExtensionTagHandler {
if ( $refsOpts['responsive'] !== null ) {
$responsiveWrap = $refsOpts['responsive'] !== '0';
} else {
$responsiveWrap = (bool)$extApi->getSiteConfig()->getMWConfigValue( 'CiteResponsiveReferences' );
$responsiveWrap = (bool)$this->mainConfig->get( 'CiteResponsiveReferences' );
}
if ( $responsiveWrap ) {
@ -105,7 +111,7 @@ class References extends ExtensionTagHandler {
return $frag;
}
private static function extractRefFromNode(
private function extractRefFromNode(
ParsoidExtensionAPI $extApi, Element $node, ReferencesData $refsData
): void {
$doc = $node->ownerDocument;
@ -236,7 +242,7 @@ class References extends ExtensionTagHandler {
if ( $contentDiffers ) {
$refsData->pushEmbeddedContentFlag();
}
self::processRefs( $extApi, $refsData, $c );
$this->processRefs( $extApi, $refsData, $c );
if ( $contentDiffers ) {
$refsData->popEmbeddedContentFlag();
// If we have refs and the content differs, we need to
@ -462,7 +468,7 @@ class References extends ExtensionTagHandler {
array_merge( $dmw->errors, $errs ) : $errs;
}
private static function insertReferencesIntoDOM(
private function insertReferencesIntoDOM(
ParsoidExtensionAPI $extApi, Element $refsNode,
ReferencesData $refsData, bool $autoGenerated = false
): void {
@ -539,7 +545,7 @@ class References extends ExtensionTagHandler {
// NOTE: The default Cite implementation hardcodes this threshold to 10.
// We use a configurable parameter here primarily for test coverage purposes.
// See citeParserTests.txt where we set a threshold of 1 or 2.
$rrThreshold = $extApi->getSiteConfig()->getMWConfigValue( 'CiteResponsiveReferencesThreshold' ) ?? 10;
$rrThreshold = $this->mainConfig->get( 'CiteResponsiveReferencesThreshold' ) ?? 10;
if ( $refGroup && count( $refGroup->refs ) > $rrThreshold ) {
DOMCompat::getClassList( $refsNode )->add( 'mw-references-columns' );
}
@ -573,13 +579,13 @@ class References extends ExtensionTagHandler {
* @param ReferencesData $refsData
* @param Node $node
*/
public static function insertMissingReferencesIntoDOM(
public function insertMissingReferencesIntoDOM(
ParsoidExtensionAPI $extApi, ReferencesData $refsData, Node $node
): void {
$doc = $node->ownerDocument;
foreach ( $refsData->getRefGroups() as $groupName => $refsGroup ) {
$domFragment = $doc->createDocumentFragment();
$frag = self::createReferences(
$frag = $this->createReferences(
$extApi,
$domFragment,
[
@ -605,19 +611,19 @@ class References extends ExtensionTagHandler {
$node->appendChild( $doc->createTextNode( "\n" ) );
$node->appendChild( $frag );
self::insertReferencesIntoDOM( $extApi, $frag, $refsData, true );
$this->insertReferencesIntoDOM( $extApi, $frag, $refsData, true );
}
}
private static function processEmbeddedRefs(
private function processEmbeddedRefs(
ParsoidExtensionAPI $extApi, ReferencesData $refsData, string $str
): string {
$domFragment = $extApi->htmlToDom( $str );
self::processRefs( $extApi, $refsData, $domFragment );
$this->processRefs( $extApi, $refsData, $domFragment );
return $extApi->domToHtml( $domFragment, true, true );
}
public static function processRefs(
public function processRefs(
ParsoidExtensionAPI $extApi, ReferencesData $refsData, Node $node
): void {
$child = $node->firstChild;
@ -625,7 +631,7 @@ class References extends ExtensionTagHandler {
$nextChild = $child->nextSibling;
if ( $child instanceof Element ) {
if ( WTUtils::isSealedFragmentOfType( $child, 'ref' ) ) {
self::extractRefFromNode( $extApi, $child, $refsData );
$this->extractRefFromNode( $extApi, $child, $refsData );
} elseif ( DOMUtils::hasTypeOf( $child, 'mw:Extension/references' ) ) {
if ( !$refsData->inReferencesContent() ) {
$refsData->referencesGroup =
@ -633,24 +639,24 @@ class References extends ExtensionTagHandler {
}
$refsData->pushEmbeddedContentFlag( 'references' );
if ( $child->hasChildNodes() ) {
self::processRefs( $extApi, $refsData, $child );
$this->processRefs( $extApi, $refsData, $child );
}
$refsData->popEmbeddedContentFlag();
if ( !$refsData->inReferencesContent() ) {
$refsData->referencesGroup = '';
self::insertReferencesIntoDOM( $extApi, $child, $refsData, false );
$this->insertReferencesIntoDOM( $extApi, $child, $refsData, false );
}
} else {
$refsData->pushEmbeddedContentFlag();
// Look for <ref>s embedded in data attributes
$extApi->processAttributeEmbeddedHTML( $child,
function ( string $html ) use ( $extApi, $refsData ) {
return self::processEmbeddedRefs( $extApi, $refsData, $html );
return $this->processEmbeddedRefs( $extApi, $refsData, $html );
}
);
$refsData->popEmbeddedContentFlag();
if ( $child->hasChildNodes() ) {
self::processRefs( $extApi, $refsData, $child );
$this->processRefs( $extApi, $refsData, $child );
}
}
}
@ -673,13 +679,13 @@ class References extends ExtensionTagHandler {
* @param ReferencesData $refsData
* @param Node $node
*/
public static function addEmbeddedErrors(
public function addEmbeddedErrors(
ParsoidExtensionAPI $extApi, ReferencesData $refsData, Node $node
): void {
$processEmbeddedErrors = function ( string $html ) use ( $extApi, $refsData ) {
// Similar to processEmbeddedRefs
$domFragment = $extApi->htmlToDom( $html );
self::addEmbeddedErrors( $extApi, $refsData, $domFragment );
$this->addEmbeddedErrors( $extApi, $refsData, $domFragment );
return $extApi->domToHtml( $domFragment, true, true );
};
$processBodyHtml = static function ( Element $n ) use ( $processEmbeddedErrors ) {
@ -710,7 +716,7 @@ class References extends ExtensionTagHandler {
);
}
if ( $child->hasChildNodes() ) {
self::addEmbeddedErrors( $extApi, $refsData, $child );
$this->addEmbeddedErrors( $extApi, $refsData, $child );
}
}
$child = $nextChild;
@ -743,7 +749,7 @@ class References extends ExtensionTagHandler {
}
}
$frag = self::createReferences(
$frag = $this->createReferences(
$extApi,
$domFragment,
$refsOpts,

View file

@ -23,6 +23,13 @@ use Wikimedia\Parsoid\Utils\DOMUtils;
*/
class CiteParsoidTest extends \MediaWikiIntegrationTestCase {
protected function setUp(): void {
// Ensure these tests are independent of LocalSettings
parent::setUp();
$this->overrideConfigValue( 'CiteResponsiveReferences', true );
$this->overrideConfigValue( 'CiteResponsiveReferencesThreshold', 10 );
}
private function getSiteConfig( $options ) {
$siteConfig = new class( $options ) extends MockSiteConfig {
public function getObjectFactory(): ObjectFactory {

View file

@ -3,6 +3,7 @@
namespace Cite\Tests\Unit;
use Cite\Parsoid\References;
use MediaWiki\Config\Config;
use MediaWikiUnitTestCase;
use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
use Wikimedia\Parsoid\NodeData\DataMw;
@ -21,7 +22,7 @@ class ReferencesTest extends MediaWikiUnitTestCase {
$elt = $doc->createElement( 'a' );
DOMDataUtils::setDataMw( $elt, new DataMw( [ 'body' => (object)[ 'html' => 'old' ] ] ) );
$refs = new References();
$refs = new References( $this->createNoOpMock( Config::class ) );
$refs->processAttributeEmbeddedHTML(
$this->createNoOpMock( ParsoidExtensionAPI::class ),
$elt,