From 578abd5108f899a035b28322d5feec9d0ffc5cfb Mon Sep 17 00:00:00 2001 From: Ray Papworth <31585092+70ray@users.noreply.github.com> Date: Sun, 8 Dec 2024 02:14:03 +0000 Subject: [PATCH] Change validatetext API to return array of characters (#1380) Update the validatetext API to return a simple array of invalid characters with their name rather than a complex object with their position. This shifts the burden of finding the characters to the caller but is seen as more flexible. --- SETUP/tests/unittests/ApiTest.php | 16 ++++++------- api/dp-openapi.yaml | 28 ++++++++-------------- api/v1_projects.inc | 7 +++--- pinc/Project.inc | 12 ++++++++++ pinc/ProofProject.inc | 40 ------------------------------- 5 files changed, 33 insertions(+), 70 deletions(-) diff --git a/SETUP/tests/unittests/ApiTest.php b/SETUP/tests/unittests/ApiTest.php index ad3794bb1..d9ee0f7a2 100644 --- a/SETUP/tests/unittests/ApiTest.php +++ b/SETUP/tests/unittests/ApiTest.php @@ -674,21 +674,19 @@ public function test_info_for_previous_proofreaders() $this->assertEquals($this->TEST_OLDUSERNAME, $info[1]->username); } - public function test_validate_text() + public function test_validate_bad_text() { $project = $this->_create_available_project(); $response = $this->validate_text($project->projectid, "This is an invĀlid test file"); - $expected = [ - 'valid' => false, - 'mark_array' => [["This is an inv", "", 0], ["Ā", "LATIN CAPITAL LETTER A WITH MACRON", 1], ["lid test file", "", 0]], - ]; + $expected = ["invalid_chars" => ["Ā" => "LATIN CAPITAL LETTER A WITH MACRON"]]; $this->assertEquals($expected, $response); + } + public function test_validate_good_text() + { + $project = $this->_create_available_project(); $response = $this->validate_text($project->projectid, "This is a valid test file"); - $expected = [ - 'valid' => true, - 'mark_array' => [["This is a valid test file", "", 0]], - ]; + $expected = ["invalid_chars" => []]; $this->assertEquals($expected, $response); } } diff --git a/api/dp-openapi.yaml b/api/dp-openapi.yaml index 33ed0d2cd..5b372f630 100644 --- a/api/dp-openapi.yaml +++ b/api/dp-openapi.yaml @@ -956,18 +956,25 @@ paths: put: tags: - project - description: Checks the given text for invalid characters, returns data indicating them + description: Checks the given text for invalid characters and returns any found with their names parameters: - $ref: '#/components/parameters/projectid' requestBody: $ref: '#/components/requestBodies/page_text' responses: 200: - description: analysis data + description: invalid characters content: application/json: schema: - $ref: '#/components/schemas/text_analysis' + type: object + properties: + invalid_chars: + type: array + items: + type: object + additionalProperties: + type: string '400': $ref: '#/components/responses/InvalidValue' '401': @@ -1604,21 +1611,6 @@ components: description: true if the page has been saved. type: boolean - text_analysis: - type: object - properties: - valid: - type: boolean - mark_array: - type: array - items: - type: array - items: - # grapheme string, utf8_character_name of grapheme, code integer: 0=valid 1=invalid - oneOf: - - type: string - - type: integer - Error: required: - message diff --git a/api/v1_projects.inc b/api/v1_projects.inc index ea28b3175..bffcb12a7 100644 --- a/api/v1_projects.inc +++ b/api/v1_projects.inc @@ -793,10 +793,11 @@ function api_v1_project_checkout($method, array $data, array $query_params) } } -function api_v1_project_validatetext($method, $data, array $query_params) +function api_v1_project_validatetext(string $method, array $data, array $query_params) { - $text_checker = new TextChecker(); - return $text_checker->analyse(receive_project_text_from_request_body('text'), $data[":projectid"]); + $project = $data[":projectid"]; + $invalid_characters = $project->find_invalid_characters(receive_project_text_from_request_body('text')); + return ["invalid_chars" => $invalid_characters]; } function api_v1_project_page(string $method, array $data, array $query_params) diff --git a/pinc/Project.inc b/pinc/Project.inc index e26765ddb..a1d07f088 100644 --- a/pinc/Project.inc +++ b/pinc/Project.inc @@ -1813,6 +1813,18 @@ class Project } } } + + public function find_invalid_characters(string $text): array + { + $pattern_string = build_character_regex_filter($this->get_valid_codepoints()); + $invalid_characters = []; + foreach (split_graphemes($text) as $grapheme) { + if (1 !== preg_match("/$pattern_string/u", $grapheme)) { + $invalid_characters[$grapheme] = utf8_character_name($grapheme); + } + } + return $invalid_characters; + } } // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX diff --git a/pinc/ProofProject.inc b/pinc/ProofProject.inc index defdd9e33..31ad69c75 100644 --- a/pinc/ProofProject.inc +++ b/pinc/ProofProject.inc @@ -118,43 +118,3 @@ class ProofProject return $proof_project_page->pp_checkout($pguser); } } - -class TextChecker -{ - private const VALID_TEXT = 0; - private const INVALID_TEXT = 1; - - private $text_array; - private $valid_text_block; - - private function append_valid(): void - { - if ($this->valid_text_block != "") { - $this->text_array[] = [$this->valid_text_block, "", self::VALID_TEXT]; - $this->valid_text_block = ""; - } - } - - public function analyse(string $page_text, object $project): array - { - $pattern_string = build_character_regex_filter($project->get_valid_codepoints()); - $this->text_array = []; - $this->valid_text_block = ""; - $all_valid = true; - foreach (split_graphemes($page_text) as $grapheme) { - if (1 === preg_match("/$pattern_string/u", $grapheme)) { - $this->valid_text_block .= $grapheme; - } else { - $all_valid = false; - $this->append_valid(); - $this->text_array[] = [$grapheme, utf8_character_name($grapheme), self::INVALID_TEXT]; - } - } - // append any remaining valid text - $this->append_valid(); - return [ - "mark_array" => $this->text_array, - "valid" => $all_valid, - ]; - } -}