Skip to content

Commit

Permalink
Change validatetext API to return array of characters (#1380)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
70ray authored Dec 8, 2024
1 parent d1a742a commit 578abd5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 70 deletions.
16 changes: 7 additions & 9 deletions SETUP/tests/unittests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
28 changes: 10 additions & 18 deletions api/dp-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions api/v1_projects.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions pinc/Project.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 0 additions & 40 deletions pinc/ProofProject.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
}

0 comments on commit 578abd5

Please sign in to comment.