mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2024-11-23 22:45:20 +00:00
Add strictly typed RefGroupItem class to Parsoid code
This does the exact same as the previously used generic stdClass object, just strictly typed. Turned out to be surprisingly straightforward, as proven by the small size of this patch. I'm intentionally not adding anything new in this patch. For example, the new class is perfect to write longer documentation for every field. But this is for a later patch. Change-Id: Ibf696f6b5ef1bfdbe846b571fb7e9ded96693351
This commit is contained in:
parent
52df9bd54f
commit
9e1c4645be
|
@ -3,7 +3,6 @@ declare( strict_types = 1 );
|
|||
|
||||
namespace Cite\Parsoid;
|
||||
|
||||
use stdClass;
|
||||
use Wikimedia\Parsoid\DOM\Document;
|
||||
use Wikimedia\Parsoid\DOM\Element;
|
||||
use Wikimedia\Parsoid\Ext\DOMUtils;
|
||||
|
@ -22,12 +21,12 @@ class RefGroup {
|
|||
public $name;
|
||||
|
||||
/**
|
||||
* @var stdClass[]
|
||||
* @var RefGroupItem[]
|
||||
*/
|
||||
public array $refs = [];
|
||||
|
||||
/**
|
||||
* @var stdClass[]
|
||||
* @var RefGroupItem[]
|
||||
*/
|
||||
public array $indexByName = [];
|
||||
|
||||
|
@ -62,7 +61,7 @@ class RefGroup {
|
|||
}
|
||||
|
||||
public function renderLine(
|
||||
ParsoidExtensionAPI $extApi, Element $refsList, stdClass $ref
|
||||
ParsoidExtensionAPI $extApi, Element $refsList, RefGroupItem $ref
|
||||
): void {
|
||||
$ownerDoc = $refsList->ownerDocument;
|
||||
|
||||
|
|
44
src/Parsoid/RefGroupItem.php
Normal file
44
src/Parsoid/RefGroupItem.php
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
declare( strict_types = 1 );
|
||||
|
||||
namespace Cite\Parsoid;
|
||||
|
||||
use Wikimedia\Parsoid\DOM\Element;
|
||||
|
||||
/**
|
||||
* Individual item in a {@see RefGroup}.
|
||||
*
|
||||
* @license GPL-2.0-or-later
|
||||
*/
|
||||
class RefGroupItem {
|
||||
|
||||
/**
|
||||
* Pointer to the contents of the ref, accessible with the
|
||||
* {@see ParsoidExtensionAPI::getContentDOM}, to be used when serializing the references group.
|
||||
* It gets set when extracting the ref from a node and not $missingContent. Note that that
|
||||
* might not be the first one for named refs. Also, for named refs, it's used to detect
|
||||
* multiple conflicting definitions.
|
||||
*/
|
||||
public ?string $contentId = null;
|
||||
|
||||
/** Just used for comparison when we have multiples */
|
||||
public ?string $cachedHtml = null;
|
||||
|
||||
public string $dir = '';
|
||||
public string $group = '';
|
||||
public int $groupIndex = 1;
|
||||
public int $index = 0;
|
||||
public string $key;
|
||||
public string $id;
|
||||
/** @var array<int,string> */
|
||||
public array $linkbacks = [];
|
||||
public string $name = '';
|
||||
public string $target;
|
||||
|
||||
/** @var Element[] */
|
||||
public array $nodes = [];
|
||||
|
||||
/** @var string[] */
|
||||
public array $embeddedNodes = [];
|
||||
|
||||
}
|
|
@ -190,6 +190,7 @@ class References extends ExtensionTagHandler {
|
|||
// HTML inline for all of them.
|
||||
if ( $ref->contentId ) {
|
||||
if ( $ref->cachedHtml === null ) {
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
|
||||
$refContent = $extApi->getContentDOM( $ref->contentId )->firstChild;
|
||||
$ref->cachedHtml = $extApi->domToHtml( $refContent, true, false );
|
||||
}
|
||||
|
@ -253,6 +254,7 @@ class References extends ExtensionTagHandler {
|
|||
if ( $validFollow ) {
|
||||
// Migrate content from the follow to the ref
|
||||
if ( $ref->contentId ) {
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
|
||||
$refContent = $extApi->getContentDOM( $ref->contentId )->firstChild;
|
||||
DOMUtils::migrateChildren( $c, $refContent );
|
||||
} else {
|
||||
|
@ -495,7 +497,6 @@ class References extends ExtensionTagHandler {
|
|||
self::addErrorsToNode( $node, $errs );
|
||||
}
|
||||
foreach ( $ref->embeddedNodes as $about ) {
|
||||
'@phan-var string $about'; // $about is non-null
|
||||
$refsData->embeddedErrors[$about] = $errs;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ declare( strict_types = 1 );
|
|||
|
||||
namespace Cite\Parsoid;
|
||||
|
||||
use stdClass;
|
||||
use Wikimedia\Parsoid\Core\Sanitizer;
|
||||
use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
|
||||
|
||||
|
@ -79,7 +78,7 @@ class ReferencesData {
|
|||
|
||||
public function add(
|
||||
ParsoidExtensionAPI $extApi, string $groupName, string $refName, string $refDir
|
||||
): stdClass {
|
||||
): RefGroupItem {
|
||||
$group = $this->getRefGroup( $groupName, true );
|
||||
$hasRefName = strlen( $refName ) > 0;
|
||||
|
||||
|
@ -99,28 +98,15 @@ class ReferencesData {
|
|||
// bump index
|
||||
$this->index += 1;
|
||||
|
||||
$ref = (object)[
|
||||
// Pointer to the contents of the ref, accessible with the
|
||||
// $extApi->getContentDOM(), to be used when serializing the
|
||||
// references group. It gets set when extracting the ref from a
|
||||
// node and not $missingContent. Note that that might not
|
||||
// be the first one for named refs. Also, for named refs, it's
|
||||
// used to detect multiple conflicting definitions.
|
||||
'contentId' => null,
|
||||
// Just used for comparison when we have multiples
|
||||
'cachedHtml' => null,
|
||||
'dir' => $refDir,
|
||||
'group' => $group->name,
|
||||
'groupIndex' => count( $group->refs ) + 1,
|
||||
'index' => $n,
|
||||
'key' => $refIdBase,
|
||||
'id' => $hasRefName ? $refIdBase . '-0' : $refIdBase,
|
||||
'linkbacks' => [],
|
||||
'name' => $refName,
|
||||
'target' => $noteId,
|
||||
'nodes' => [],
|
||||
'embeddedNodes' => [],
|
||||
];
|
||||
$ref = new RefGroupItem();
|
||||
$ref->dir = $refDir;
|
||||
$ref->group = $group->name;
|
||||
$ref->groupIndex = count( $group->refs ) + 1;
|
||||
$ref->index = $n;
|
||||
$ref->key = $refIdBase;
|
||||
$ref->id = $hasRefName ? $refIdBase . '-0' : $refIdBase;
|
||||
$ref->name = $refName;
|
||||
$ref->target = $noteId;
|
||||
|
||||
$group->refs[] = $ref;
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ namespace Cite\Tests\Unit;
|
|||
|
||||
use Cite\Cite;
|
||||
use Cite\Parsoid\ReferencesData;
|
||||
use Cite\Parsoid\RefGroupItem;
|
||||
use MediaWikiUnitTestCase;
|
||||
use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
|
||||
|
||||
|
@ -58,25 +59,14 @@ class ReferencesDataTest extends MediaWikiUnitTestCase {
|
|||
''
|
||||
);
|
||||
|
||||
$expected = [
|
||||
'contentId' => null,
|
||||
'cachedHtml' => null,
|
||||
'dir' => '',
|
||||
'group' => '',
|
||||
'groupIndex' => 1,
|
||||
'index' => 0,
|
||||
'key' => 'cite_ref-1',
|
||||
'id' => 'cite_ref-1',
|
||||
'linkbacks' => [],
|
||||
'name' => '',
|
||||
'target' => 'cite_note-1',
|
||||
'nodes' => [],
|
||||
'embeddedNodes' => [],
|
||||
];
|
||||
$this->assertSame( $expected, (array)$ref );
|
||||
$expected = new RefGroupItem();
|
||||
$expected->key = 'cite_ref-1';
|
||||
$expected->id = 'cite_ref-1';
|
||||
$expected->target = 'cite_note-1';
|
||||
$this->assertEquals( $expected, $ref );
|
||||
|
||||
$group = $data->getRefGroup( Cite::DEFAULT_GROUP );
|
||||
$this->assertEquals( [ (object)$expected ], $group->refs );
|
||||
$this->assertEquals( [ $expected ], $group->refs );
|
||||
$this->assertSame( [], $group->indexByName );
|
||||
}
|
||||
|
||||
|
@ -89,26 +79,18 @@ class ReferencesDataTest extends MediaWikiUnitTestCase {
|
|||
'rtl'
|
||||
);
|
||||
|
||||
$expected = [
|
||||
'contentId' => null,
|
||||
'cachedHtml' => null,
|
||||
'dir' => 'rtl',
|
||||
'group' => 'note',
|
||||
'groupIndex' => 1,
|
||||
'index' => 0,
|
||||
'key' => 'cite_ref-wales_1',
|
||||
'id' => 'cite_ref-wales_1-0',
|
||||
'linkbacks' => [],
|
||||
'name' => 'wales',
|
||||
'target' => 'cite_note-wales-1',
|
||||
'nodes' => [],
|
||||
'embeddedNodes' => [],
|
||||
];
|
||||
$this->assertSame( $expected, (array)$ref );
|
||||
$expected = new RefGroupItem();
|
||||
$expected->dir = 'rtl';
|
||||
$expected->group = 'note';
|
||||
$expected->key = 'cite_ref-wales_1';
|
||||
$expected->id = 'cite_ref-wales_1-0';
|
||||
$expected->name = 'wales';
|
||||
$expected->target = 'cite_note-wales-1';
|
||||
$this->assertEquals( $expected, $ref );
|
||||
|
||||
$group = $data->getRefGroup( 'note' );
|
||||
$this->assertEquals( [ (object)$expected ], $group->refs );
|
||||
$this->assertEquals( [ 'wales' => (object)$expected ], $group->indexByName );
|
||||
$this->assertEquals( [ $expected ], $group->refs );
|
||||
$this->assertEquals( [ 'wales' => $expected ], $group->indexByName );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue