From 17af1de647d41b9412b4a579b34a86a8f7e5d572 Mon Sep 17 00:00:00 2001 From: Mauro Cassani Date: Wed, 30 Oct 2024 12:19:36 +0100 Subject: [PATCH] Correct handling of markdown instructions --- lib/Model/Files/MetadataDao.php | 7 ++- .../ProjectManager/ProjectManagerModel.php | 8 ++- lib/Utils/Files/FilesInfoUtility.php | 5 +- lib/Utils/Utils.php | 21 +++++++ tests/unit/Utils/DuplicateNewLineTest.php | 56 +++++++++++++++++++ 5 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 tests/unit/Utils/DuplicateNewLineTest.php diff --git a/lib/Model/Files/MetadataDao.php b/lib/Model/Files/MetadataDao.php index 614b41cc4c..7613552c02 100644 --- a/lib/Model/Files/MetadataDao.php +++ b/lib/Model/Files/MetadataDao.php @@ -11,6 +11,7 @@ use DataAccess_AbstractDao; use DataAccess_IDaoStruct; use Database; +use Utils; class MetadataDao extends DataAccess_AbstractDao { @@ -97,7 +98,7 @@ public function insert( $id_project, $id_file, $key, $value, $filePartsId = null 'id_file' => $id_file, 'files_parts_id' => $filePartsId, 'key' => $key, - 'value' => $value + 'value' => Utils::stripTagsPreservingHrefs($value) ] ); return $this->get( $id_project, $id_file, $key, $filePartsId ); @@ -123,7 +124,7 @@ public function update( $id_project, $id_file, $key, $value, $filePartsId = null 'id_file' => $id_file, 'files_parts_id' => $filePartsId, 'key' => $key, - 'value' => $value + 'value' => Utils::stripTagsPreservingHrefs($value) ] ); return $this->get( $id_project, $id_file, $key, $filePartsId ); @@ -157,7 +158,7 @@ public function bulkInsert( $id_project, $id_file, array $metadata = [], $filePa $bind_values[] = $id_project; $bind_values[] = $id_file; $bind_values[] = $key; - $bind_values[] = $value; + $bind_values[] = Utils::stripTagsPreservingHrefs($value); $bind_values[] = $filePartsId; } $index++; diff --git a/lib/Model/ProjectManager/ProjectManagerModel.php b/lib/Model/ProjectManager/ProjectManagerModel.php index f347afc09e..2d5b8c2c79 100644 --- a/lib/Model/ProjectManager/ProjectManagerModel.php +++ b/lib/Model/ProjectManager/ProjectManagerModel.php @@ -177,11 +177,15 @@ public static function bulkInsertSegmentNotes( $notes ) { // check for metaKey is `notes` if(!self::isAMetadata($metaKey)){ - $insert_values[] = [ $id_segment, $internal_id, Utils::stripTagsPreservingHrefs(html_entity_decode($note)), null ]; + $note = Utils::stripTagsPreservingHrefs(html_entity_decode($note)); + $note = Utils::duplicateNewLines($note); + $insert_values[] = [ $id_segment, $internal_id, $note, null ]; } } else { - $insert_values[] = [ $id_segment, $internal_id, Utils::stripTagsPreservingHrefs(html_entity_decode($note)), null ]; + $note = Utils::stripTagsPreservingHrefs(html_entity_decode($note)); + $note = Utils::duplicateNewLines($note); + $insert_values[] = [ $id_segment, $internal_id, $note, null ]; } } } diff --git a/lib/Utils/Files/FilesInfoUtility.php b/lib/Utils/Files/FilesInfoUtility.php index ee5a1c0c06..e6c33cb46d 100644 --- a/lib/Utils/Files/FilesInfoUtility.php +++ b/lib/Utils/Files/FilesInfoUtility.php @@ -8,6 +8,7 @@ use Jobs_JobDao; use Jobs_JobStruct; use Projects_ProjectStruct; +use Utils; class FilesInfoUtility { @@ -141,9 +142,9 @@ public function setInstructions( $id_file, $instructions ) { if ( Files_FileDao::isFileInProject( $id_file, $this->project->id ) ) { $metadataDao = new Files_MetadataDao; if ( $metadataDao->get( $this->project->id, $id_file, 'instructions', 60 * 5 ) ) { - $metadataDao->update( $this->project->id, $id_file, 'instructions', $instructions ); + $metadataDao->update( $this->project->id, $id_file, 'instructions', Utils::stripTagsPreservingHrefs($instructions) ); } else { - $metadataDao->insert( $this->project->id, $id_file, 'instructions', $instructions ); + $metadataDao->insert( $this->project->id, $id_file, 'instructions', Utils::stripTagsPreservingHrefs($instructions) ); } return true; diff --git a/lib/Utils/Utils.php b/lib/Utils/Utils.php index 5e79f414af..a68975572a 100644 --- a/lib/Utils/Utils.php +++ b/lib/Utils/Utils.php @@ -888,6 +888,27 @@ public static function stripTagsPreservingHrefs( $html ) { return $strippedHtml; } + /** + * @param $string + * @return mixed + */ + public static function duplicateNewLines($string) + { + $newLinePlaceholder = "###PHP_EOL###"; + $doubleNewLinePlaceholder = "###DOUBLE_PHP_EOL###"; + + $string = str_replace(PHP_EOL, $newLinePlaceholder, $string); + $string = preg_replace('/\s+/', ' ', $string); + $string = str_replace([ + $newLinePlaceholder.$newLinePlaceholder, + $newLinePlaceholder . " " .$newLinePlaceholder , + ], $doubleNewLinePlaceholder, $string); + $string = str_replace($newLinePlaceholder, PHP_EOL . PHP_EOL, $string); + $string = str_replace($doubleNewLinePlaceholder, PHP_EOL . PHP_EOL, $string); + + return $string; + } + /** * @param $email * diff --git a/tests/unit/Utils/DuplicateNewLineTest.php b/tests/unit/Utils/DuplicateNewLineTest.php new file mode 100644 index 0000000000..22b810ff52 --- /dev/null +++ b/tests/unit/Utils/DuplicateNewLineTest.php @@ -0,0 +1,56 @@ + "This is markdown **string** a " . PHP_EOL ." new line", + 'expected' => "This is markdown **string** a " . PHP_EOL . PHP_EOL ." new line", + ], + [ + 'original' => "This is markdown **string** a " . PHP_EOL ." new line", + 'expected' => "This is markdown **string** a " . PHP_EOL . PHP_EOL ." new line", + ], + [ + 'original' => "This is markdown **string** a " . PHP_EOL . PHP_EOL ." new line", + 'expected' => "This is markdown **string** a " . PHP_EOL . PHP_EOL ." new line", + ], + [ + 'original' => "This is markdown **string** a " . PHP_EOL ." " . PHP_EOL ." new line", + 'expected' => "This is markdown **string** a " . PHP_EOL . PHP_EOL ." new line", + ], + [ + 'original' => "This is markdown **string** a " . PHP_EOL ." " . PHP_EOL ." " . PHP_EOL ." new line", + 'expected' => "This is markdown **string** a " . PHP_EOL . PHP_EOL . " " . PHP_EOL . PHP_EOL ." new line", + ], + [ + 'original' => "This is markdown **string** " . PHP_EOL ." with two break " . PHP_EOL ." new lines", + 'expected' => "This is markdown **string** " . PHP_EOL . PHP_EOL ." with two break " . PHP_EOL . PHP_EOL ." new lines", + ], + [ + 'original' => "This is markdown **string** \n with two break \n new lines", + 'expected' => "This is markdown **string** \n\n with two break \n\n new lines", + ], + [ + 'original' => "This is a comment\nThis is a comment number two\nThis is a comment number three", + 'expected' => "This is a comment\n\nThis is a comment number two\n\nThis is a comment number three", + ], + [ + 'original' => "This is markdown **string** no new line", + 'expected' => "This is markdown **string** no new line", + ], + ]; + + foreach ($strings as $string){ + $this->assertEquals( + Utils::duplicateNewLines($string['original']), + $string['expected'], + 'Error: expected ' . $string['expected'] + ); + } + } +} \ No newline at end of file