diff --git a/lib/Model/Files/MetadataDao.php b/lib/Model/Files/MetadataDao.php index aaf8a50c63..3c985b4332 100644 --- a/lib/Model/Files/MetadataDao.php +++ b/lib/Model/Files/MetadataDao.php @@ -10,6 +10,7 @@ use DataAccess_IDaoStruct; use Database; +use Utils; class MetadataDao extends \DataAccess_AbstractDao { @@ -92,7 +93,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 ); @@ -118,7 +119,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 ); @@ -152,7 +153,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 e75a186827..16b75c1a7f 100644 --- a/lib/Model/ProjectManager/ProjectManagerModel.php +++ b/lib/Model/ProjectManager/ProjectManagerModel.php @@ -172,11 +172,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 abb32e9858..18a7b75db2 100644 --- a/lib/Utils/Files/FilesInfoUtility.php +++ b/lib/Utils/Files/FilesInfoUtility.php @@ -6,6 +6,7 @@ use API\V3\Json\FilesInfo; use Files\MetadataDao as Files_MetadataDao; use Jobs_JobStruct; +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/ProjectManager.php b/lib/Utils/ProjectManager.php index 813ff2c8b2..c2f8ca2283 100644 --- a/lib/Utils/ProjectManager.php +++ b/lib/Utils/ProjectManager.php @@ -871,7 +871,8 @@ public function createProject() { foreach ( $array_files as $index => $filename ) { if ( $file_info[ 'original_filename' ] === $filename ) { if ( isset( $this->projectStructure[ 'instructions' ][ $index ] ) && !empty( $this->projectStructure[ 'instructions' ][ $index ] ) ) { - $this->_insertInstructions( $fid, $this->projectStructure[ 'instructions' ][ $index ] ); + $instructions = Utils::stripTagsPreservingHrefs($this->projectStructure[ 'instructions' ][ $index ]); + $this->_insertInstructions( $fid, $instructions ); } } diff --git a/lib/Utils/Utils.php b/lib/Utils/Utils.php index 5911c03077..462b921972 100644 --- a/lib/Utils/Utils.php +++ b/lib/Utils/Utils.php @@ -917,4 +917,25 @@ 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; + } } diff --git a/test/unit/Utils/DuplicateNewLineTest.php b/test/unit/Utils/DuplicateNewLineTest.php new file mode 100644 index 0000000000..1dacbca6a7 --- /dev/null +++ b/test/unit/Utils/DuplicateNewLineTest.php @@ -0,0 +1,54 @@ + "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