From 87d747a54d7c17ab028d06df00a624f529abece9 Mon Sep 17 00:00:00 2001 From: "C. Scott Ananian" Date: Fri, 20 Sep 2024 14:30:34 -0400 Subject: [PATCH] Use DataMwError Depends-On: I63f48887690ea916c4e54b4a1a703f301dd64b6a Depends-On: I63ca14ad4d5b2cdbce2fd61c4d6eb544b634bc8d Followup-To: I14930937c1beca039484cffd19e486cd06cbc124 Change-Id: Ic8ee0a7c9ba50e19baf06e21251f8f7710110051 --- src/Parsoid/References.php | 67 ++++++++++++++++++++-------------- src/Parsoid/ReferencesData.php | 4 +- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/Parsoid/References.php b/src/Parsoid/References.php index 646070a1c..071cf813f 100644 --- a/src/Parsoid/References.php +++ b/src/Parsoid/References.php @@ -19,6 +19,7 @@ use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI; use Wikimedia\Parsoid\Ext\PHPUtils; use Wikimedia\Parsoid\Ext\WTUtils; use Wikimedia\Parsoid\NodeData\DataMw; +use Wikimedia\Parsoid\NodeData\DataMwError; use Wikimedia\Parsoid\NodeData\DataParsoid; use Wikimedia\Parsoid\Utils\DOMCompat; @@ -140,8 +141,10 @@ class References extends ExtensionTagHandler { $refsData->inReferencesContent() && $groupName !== $refsData->referencesGroup ) { - $errs[] = (object)[ 'key' => 'cite_error_references_group_mismatch', - 'params' => [ $refDmw->attrs->group ] ]; + $errs[] = new DataMwError( + 'cite_error_references_group_mismatch', + [ $refDmw->attrs->group ] + ); } // NOTE: This will have been trimmed in Utils::getExtArgInfo()'s call @@ -179,7 +182,7 @@ class References extends ExtensionTagHandler { if ( $hasRefName ) { if ( $hasFollow ) { // 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] ) ) { $ref = $group->indexByName[$refName]; @@ -201,10 +204,10 @@ class References extends ExtensionTagHandler { } } else { if ( $refsData->inReferencesContent() ) { - $errs[] = (object)[ - 'key' => 'cite_error_references_missing_key', - 'params' => [ $refDmw->attrs->name ] - ]; + $errs[] = new DataMwError( + 'cite_error_references_missing_key', + [ $refDmw->attrs->name ] + ); } } } else { @@ -220,11 +223,13 @@ class References extends ExtensionTagHandler { // section and it's the $followName we care about, but the // extension to the legacy parser doesn't have an // equivalent key and just outputs something wacky. - $errs[] = (object)[ 'key' => 'cite_error_references_missing_key', - 'params' => [ $refDmw->attrs->follow ] ]; + $errs[] = new DataMwError( + 'cite_error_references_missing_key', + [ $refDmw->attrs->follow ] + ); } } 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 ( $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 ) { - $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. // Follow refs do not duplicate the error which can be correlated with the original ref. 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 @@ -307,13 +312,15 @@ class References extends ExtensionTagHandler { // 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. if ( $refsData->inReferencesContent() ) { - $errs[] = (object)[ 'key' => 'cite_error_empty_references_define', - 'params' => [ $refDmw->attrs->name ?? '', $refDmw->attrs->group ?? '' ] ]; + $errs[] = new DataMwError( + 'cite_error_empty_references_define', + [ $refDmw->attrs->name ?? '', $refDmw->attrs->group ?? '' ] + ); } elseif ( !$hasRefName ) { if ( !empty( $cDp->selfClose ) ) { - $errs[] = (object)[ 'key' => 'cite_error_ref_no_key' ]; + $errs[] = new DataMwError( 'cite_error_ref_no_key' ); } 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 ) { // TODO: Since this error is being placed on the ref, the // key should arguably be "cite_error_ref_duplicate_key" - $errs[] = (object)[ - 'key' => 'cite_error_references_duplicate_key', - 'params' => [ $refDmw->attrs->name ] - ]; + $errs[] = new DataMwError( + 'cite_error_references_duplicate_key', + [ $refDmw->attrs->name ] + ); $refDmw->body = (object)[ 'html' => $html ]; } else { $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 ); } + /** + * @param Element $node + * @param list $errs + */ private static function addErrorsToNode( Element $node, array $errs ): void { DOMUtils::addTypeOf( $node, 'mw:Error' ); $dmw = DOMDataUtils::getDataMw( $node ); @@ -482,17 +493,19 @@ class References extends ExtensionTagHandler { $errs = []; // Mark all refs that are part of a group that is autogenerated if ( $autoGeneratedWithGroup ) { - $errs[] = (object)[ 'key' => 'cite_error_group_refs_without_references', - 'params' => [ $group ] ]; + $errs[] = new DataMwError( + 'cite_error_group_refs_without_references', + [ $group ] + ); } // Mark all refs that are named without content if ( ( $ref->name !== '' ) && $ref->contentId === null ) { // TODO: Since this error is being placed on the ref, // the key should arguably be "cite_error_ref_no_text" - $errs[] = (object)[ - 'key' => 'cite_error_references_no_text', - 'params' => [ $ref->name ] - ]; + $errs[] = new DataMwError( + 'cite_error_references_no_text', + [ $ref->name ] + ); } if ( $errs ) { foreach ( $ref->nodes as $node ) { diff --git a/src/Parsoid/ReferencesData.php b/src/Parsoid/ReferencesData.php index 4e999daf1..60e6483ae 100644 --- a/src/Parsoid/ReferencesData.php +++ b/src/Parsoid/ReferencesData.php @@ -3,9 +3,9 @@ declare( strict_types = 1 ); namespace Cite\Parsoid; -use stdClass; use Wikimedia\Parsoid\Core\Sanitizer; use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI; +use Wikimedia\Parsoid\NodeData\DataMwError; /** * @license GPL-2.0-or-later @@ -15,7 +15,7 @@ class ReferencesData { private int $index = 0; /** @var array indexed by group name */ private array $refGroups = []; - /** @var array */ + /** @var array> */ public array $embeddedErrors = []; /** @var string[] */ private array $inEmbeddedContent = [];