Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip tags in instructions + duplicate new lines in notes #2874

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/Model/Files/MetadataDao.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use DataAccess_IDaoStruct;
use Database;
use Utils;

class MetadataDao extends \DataAccess_AbstractDao {

Expand Down Expand Up @@ -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 );
Expand All @@ -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 );
Expand Down Expand Up @@ -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++;
Expand Down
8 changes: 6 additions & 2 deletions lib/Model/ProjectManager/ProjectManagerModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/Utils/Files/FilesInfoUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use API\V3\Json\FilesInfo;
use Files\MetadataDao as Files_MetadataDao;
use Jobs_JobStruct;
use Utils;

class FilesInfoUtility {

Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion lib/Utils/ProjectManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

}
Expand Down
21 changes: 21 additions & 0 deletions lib/Utils/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
54 changes: 54 additions & 0 deletions test/unit/Utils/DuplicateNewLineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

class DuplicateNewLineTest extends PHPUnit_Framework_TestCase
{
public function testCanDuplicateNewLinesTest()
{
$strings = [
[
'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 ." 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']
);
}
}
}