From 2768a702184429f9324029a30124c31d221aa35e Mon Sep 17 00:00:00 2001 From: sbailey Date: Tue, 7 Feb 2023 12:39:22 -0800 Subject: [PATCH] Fix migrate data error when params has excessively long strings * The linter migrate code for linter_tag field and linter_template field are constrained by the database schema to 32 characters for the tag field and 255 characters for the template field. In some anomalous circumstances parsoid can report tag and or template fields in the linter_params object that exceed those character limits. This code truncates these excessively long strings to protect the database migrate update code from a length exceeded error. Bug: T329113 Change-Id: I8af7c44759f172eae77d3519a6eac47110e9b1e7 --- includes/Database.php | 3 +++ tests/phpunit/RecordLintJobTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/includes/Database.php b/includes/Database.php index 07b85303..099d6866 100644 --- a/includes/Database.php +++ b/includes/Database.php @@ -604,7 +604,10 @@ class Database { $templateInfo = $templateInfo->name ?? ''; } } + $templateInfo = mb_strcut( $templateInfo, 0, self::MAX_TEMPLATE_LENGTH ); + $tagInfo = $linter_params->name ?? ''; + $tagInfo = mb_strcut( $tagInfo, 0, self::MAX_TAG_LENGTH ); // compare the content of linter_params to the template and tag field contents // and if they diverge, update the field with the correct template and tag info. diff --git a/tests/phpunit/RecordLintJobTest.php b/tests/phpunit/RecordLintJobTest.php index 567e03f8..1162119c 100644 --- a/tests/phpunit/RecordLintJobTest.php +++ b/tests/phpunit/RecordLintJobTest.php @@ -356,6 +356,20 @@ class RecordLintJobTest extends MediaWikiIntegrationTestCase { 'TestPageTagAndTemplateMultipart', $error ); + // Create special case test for params containing tag and template info strings exceeding the fields lengths + $tagWithMoreThan30Characters = "center tag exceeding 30 characters"; + $templateWithMoreThan250Characters = str_repeat( "Template:Echo longer than 250 characters ", 8 ); + $error = [ + 'type' => 'obsolete-tag', + 'location' => [ 0, 10 ], + 'params' => [ "name" => $tagWithMoreThan30Characters, + "templateInfo" => $templateWithMoreThan250Characters ], + 'dbid' => null, + ]; + $titleAndPageLengthExceeded = $this->createTitleAndPageForTagsAndRunJob( + 'TestPageTagAndTemplateLengthExceeded', + $error ); + // Verify the create page function did not populate the linter_tag and linter_template field for TestPage0 $pageId = $titleAndPages[ 0 ][ 'pageID' ]; $tag = $this->getTagForPage( $pageId ); @@ -380,6 +394,17 @@ class RecordLintJobTest extends MediaWikiIntegrationTestCase { $this->assertEquals( "center", $tag ); $template = $this->getTemplateForPage( $titleAndPageMultipart[ 'pageID' ] ); $this->assertEquals( "multi-part-template-block", $template ); + + // Verify special case test for migrate code encountering params with tag and template string length exceeded + $tagTruncated = "center tag exceeding 30 charac"; + $templateTruncated = "Template:Echo longer than 250 characters Template:Echo longer than 250 characters " . + "Template:Echo longer than 250 characters Template:Echo longer than 250 characters " . + "Template:Echo longer than 250 characters Template:Echo longer than 250 characters Temp"; + + $tag = $this->getTagForPage( $titleAndPageLengthExceeded[ 'pageID' ] ); + $this->assertEquals( $tagTruncated, $tag ); + $template = $this->getTemplateForPage( $titleAndPageLengthExceeded[ 'pageID' ] ); + $this->assertEquals( $templateTruncated, $template ); } public function testDropInlineMediaCaptionLints() {