Use DataMwError

Depends-On: I63f48887690ea916c4e54b4a1a703f301dd64b6a
Depends-On: I63ca14ad4d5b2cdbce2fd61c4d6eb544b634bc8d
Followup-To: I14930937c1beca039484cffd19e486cd06cbc124
Change-Id: Ic8ee0a7c9ba50e19baf06e21251f8f7710110051
This commit is contained in:
C. Scott Ananian 2024-09-20 14:30:34 -04:00
parent 3a9ba45a7d
commit 87d747a54d
2 changed files with 42 additions and 29 deletions

View file

@ -19,6 +19,7 @@ use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
use Wikimedia\Parsoid\Ext\PHPUtils; use Wikimedia\Parsoid\Ext\PHPUtils;
use Wikimedia\Parsoid\Ext\WTUtils; use Wikimedia\Parsoid\Ext\WTUtils;
use Wikimedia\Parsoid\NodeData\DataMw; use Wikimedia\Parsoid\NodeData\DataMw;
use Wikimedia\Parsoid\NodeData\DataMwError;
use Wikimedia\Parsoid\NodeData\DataParsoid; use Wikimedia\Parsoid\NodeData\DataParsoid;
use Wikimedia\Parsoid\Utils\DOMCompat; use Wikimedia\Parsoid\Utils\DOMCompat;
@ -140,8 +141,10 @@ class References extends ExtensionTagHandler {
$refsData->inReferencesContent() && $refsData->inReferencesContent() &&
$groupName !== $refsData->referencesGroup $groupName !== $refsData->referencesGroup
) { ) {
$errs[] = (object)[ 'key' => 'cite_error_references_group_mismatch', $errs[] = new DataMwError(
'params' => [ $refDmw->attrs->group ] ]; 'cite_error_references_group_mismatch',
[ $refDmw->attrs->group ]
);
} }
// NOTE: This will have been trimmed in Utils::getExtArgInfo()'s call // NOTE: This will have been trimmed in Utils::getExtArgInfo()'s call
@ -179,7 +182,7 @@ class References extends ExtensionTagHandler {
if ( $hasRefName ) { if ( $hasRefName ) {
if ( $hasFollow ) { if ( $hasFollow ) {
// Presumably, "name" has higher precedence // Presumably, "name" has higher precedence
$errs[] = (object)[ 'key' => 'cite_error_ref_follow_conflicts' ]; $errs[] = new DataMwError( 'cite_error_ref_follow_conflicts' );
} }
if ( isset( $group->indexByName[$refName] ) ) { if ( isset( $group->indexByName[$refName] ) ) {
$ref = $group->indexByName[$refName]; $ref = $group->indexByName[$refName];
@ -201,10 +204,10 @@ class References extends ExtensionTagHandler {
} }
} else { } else {
if ( $refsData->inReferencesContent() ) { if ( $refsData->inReferencesContent() ) {
$errs[] = (object)[ $errs[] = new DataMwError(
'key' => 'cite_error_references_missing_key', 'cite_error_references_missing_key',
'params' => [ $refDmw->attrs->name ] [ $refDmw->attrs->name ]
]; );
} }
} }
} else { } else {
@ -220,11 +223,13 @@ class References extends ExtensionTagHandler {
// section and it's the $followName we care about, but the // section and it's the $followName we care about, but the
// extension to the legacy parser doesn't have an // extension to the legacy parser doesn't have an
// equivalent key and just outputs something wacky. // equivalent key and just outputs something wacky.
$errs[] = (object)[ 'key' => 'cite_error_references_missing_key', $errs[] = new DataMwError(
'params' => [ $refDmw->attrs->follow ] ]; 'cite_error_references_missing_key',
[ $refDmw->attrs->follow ]
);
} }
} elseif ( $refsData->inReferencesContent() ) { } elseif ( $refsData->inReferencesContent() ) {
$errs[] = (object)[ 'key' => 'cite_error_references_no_key' ]; $errs[] = new DataMwError( 'cite_error_references_no_key' );
} }
} }
@ -283,9 +288,9 @@ class References extends ExtensionTagHandler {
if ( isset( $refDmw->attrs->dir ) ) { if ( isset( $refDmw->attrs->dir ) ) {
if ( $refDir !== 'rtl' && $refDir !== 'ltr' ) { if ( $refDir !== 'rtl' && $refDir !== 'ltr' ) {
$errs[] = (object)[ 'key' => 'cite_error_ref_invalid_dir', 'params' => [ $refDir ] ]; $errs[] = new DataMwError( 'cite_error_ref_invalid_dir', [ $refDir ] );
} elseif ( $ref->dir !== '' && $ref->dir !== $refDir ) { } elseif ( $ref->dir !== '' && $ref->dir !== $refDir ) {
$errs[] = (object)[ 'key' => 'cite_error_ref_conflicting_dir', 'params' => [ $ref->name ] ]; $errs[] = new DataMwError( 'cite_error_ref_conflicting_dir', [ $ref->name ] );
} }
} }
@ -293,7 +298,7 @@ class References extends ExtensionTagHandler {
// supports numerals as a name without it being an actual error, but core Cite does not. // supports numerals as a name without it being an actual error, but core Cite does not.
// Follow refs do not duplicate the error which can be correlated with the original ref. // Follow refs do not duplicate the error which can be correlated with the original ref.
if ( ctype_digit( $refName ) ) { if ( ctype_digit( $refName ) ) {
$errs[] = (object)[ 'key' => 'cite_error_ref_numeric_key' ]; $errs[] = new DataMwError( 'cite_error_ref_numeric_key' );
} }
// Check for missing content, added ?? '' to fix T259676 crasher // Check for missing content, added ?? '' to fix T259676 crasher
@ -307,13 +312,15 @@ class References extends ExtensionTagHandler {
// is an error. It's possible that no name is present (!hasRefName), which also // is an error. It's possible that no name is present (!hasRefName), which also
// gets the error "cite_error_references_no_key" above, so protect against that. // gets the error "cite_error_references_no_key" above, so protect against that.
if ( $refsData->inReferencesContent() ) { if ( $refsData->inReferencesContent() ) {
$errs[] = (object)[ 'key' => 'cite_error_empty_references_define', $errs[] = new DataMwError(
'params' => [ $refDmw->attrs->name ?? '', $refDmw->attrs->group ?? '' ] ]; 'cite_error_empty_references_define',
[ $refDmw->attrs->name ?? '', $refDmw->attrs->group ?? '' ]
);
} elseif ( !$hasRefName ) { } elseif ( !$hasRefName ) {
if ( !empty( $cDp->selfClose ) ) { if ( !empty( $cDp->selfClose ) ) {
$errs[] = (object)[ 'key' => 'cite_error_ref_no_key' ]; $errs[] = new DataMwError( 'cite_error_ref_no_key' );
} else { } else {
$errs[] = (object)[ 'key' => 'cite_error_ref_no_input' ]; $errs[] = new DataMwError( 'cite_error_ref_no_input' );
} }
} }
@ -334,10 +341,10 @@ class References extends ExtensionTagHandler {
if ( $contentDiffers ) { if ( $contentDiffers ) {
// TODO: Since this error is being placed on the ref, the // TODO: Since this error is being placed on the ref, the
// key should arguably be "cite_error_ref_duplicate_key" // key should arguably be "cite_error_ref_duplicate_key"
$errs[] = (object)[ $errs[] = new DataMwError(
'key' => 'cite_error_references_duplicate_key', 'cite_error_references_duplicate_key',
'params' => [ $refDmw->attrs->name ] [ $refDmw->attrs->name ]
]; );
$refDmw->body = (object)[ 'html' => $html ]; $refDmw->body = (object)[ 'html' => $html ];
} else { } else {
$refDmw->body = (object)[ 'id' => 'mw-reference-text-' . $ref->target ]; $refDmw->body = (object)[ 'id' => 'mw-reference-text-' . $ref->target ];
@ -455,6 +462,10 @@ class References extends ExtensionTagHandler {
$dataParsoid->dsr = new DomSourceRange( $offset, $offset, null, null ); $dataParsoid->dsr = new DomSourceRange( $offset, $offset, null, null );
} }
/**
* @param Element $node
* @param list<DataMwError> $errs
*/
private static function addErrorsToNode( Element $node, array $errs ): void { private static function addErrorsToNode( Element $node, array $errs ): void {
DOMUtils::addTypeOf( $node, 'mw:Error' ); DOMUtils::addTypeOf( $node, 'mw:Error' );
$dmw = DOMDataUtils::getDataMw( $node ); $dmw = DOMDataUtils::getDataMw( $node );
@ -482,17 +493,19 @@ class References extends ExtensionTagHandler {
$errs = []; $errs = [];
// Mark all refs that are part of a group that is autogenerated // Mark all refs that are part of a group that is autogenerated
if ( $autoGeneratedWithGroup ) { if ( $autoGeneratedWithGroup ) {
$errs[] = (object)[ 'key' => 'cite_error_group_refs_without_references', $errs[] = new DataMwError(
'params' => [ $group ] ]; 'cite_error_group_refs_without_references',
[ $group ]
);
} }
// Mark all refs that are named without content // Mark all refs that are named without content
if ( ( $ref->name !== '' ) && $ref->contentId === null ) { if ( ( $ref->name !== '' ) && $ref->contentId === null ) {
// TODO: Since this error is being placed on the ref, // TODO: Since this error is being placed on the ref,
// the key should arguably be "cite_error_ref_no_text" // the key should arguably be "cite_error_ref_no_text"
$errs[] = (object)[ $errs[] = new DataMwError(
'key' => 'cite_error_references_no_text', 'cite_error_references_no_text',
'params' => [ $ref->name ] [ $ref->name ]
]; );
} }
if ( $errs ) { if ( $errs ) {
foreach ( $ref->nodes as $node ) { foreach ( $ref->nodes as $node ) {

View file

@ -3,9 +3,9 @@ declare( strict_types = 1 );
namespace Cite\Parsoid; namespace Cite\Parsoid;
use stdClass;
use Wikimedia\Parsoid\Core\Sanitizer; use Wikimedia\Parsoid\Core\Sanitizer;
use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI; use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
use Wikimedia\Parsoid\NodeData\DataMwError;
/** /**
* @license GPL-2.0-or-later * @license GPL-2.0-or-later
@ -15,7 +15,7 @@ class ReferencesData {
private int $index = 0; private int $index = 0;
/** @var array<string,RefGroup> indexed by group name */ /** @var array<string,RefGroup> indexed by group name */
private array $refGroups = []; private array $refGroups = [];
/** @var array<string,stdClass[]> */ /** @var array<string,list<DataMwError>> */
public array $embeddedErrors = []; public array $embeddedErrors = [];
/** @var string[] */ /** @var string[] */
private array $inEmbeddedContent = []; private array $inEmbeddedContent = [];