diff --git a/SETUP/tests/unittests/ApiTest.php b/SETUP/tests/unittests/ApiTest.php index ad3794bb1..61ccb3127 100644 --- a/SETUP/tests/unittests/ApiTest.php +++ b/SETUP/tests/unittests/ApiTest.php @@ -691,6 +691,20 @@ public function test_validate_text() ]; $this->assertEquals($expected, $response); } + + public function test_pickersets() + { + $project = $this->_create_project(); + $path = "v1/projects/$project->projectid/pickersets"; + $query_params = ""; + $router = ApiRouter::get_router(); + $_SERVER["REQUEST_METHOD"] = "GET"; + $response = $router->route($path, $query_params); + $this->assertEquals("basic-latin", $response[0]["name"]); + $this->assertEquals("!", $response[0]["subsets"][3]["name"]); + $this->assertEquals("Punctuation", $response[0]["subsets"][3]["title"]); + $this->assertEquals(["0", "DIGIT ZERO"], $response[0]["subsets"][5]["rows"][0][0]); + } } // this mocks the function in index.php diff --git a/api/dp-openapi.yaml b/api/dp-openapi.yaml index 33ed0d2cd..c0f593868 100644 --- a/api/dp-openapi.yaml +++ b/api/dp-openapi.yaml @@ -979,6 +979,66 @@ paths: default: $ref: '#/components/responses/UnexpectedError' + /projects/{projectid}/pickersets: + get: + tags: + - project + description: Gets pickersets for the project including utf8 character names + parameters: + - $ref: '#/components/parameters/projectid' + responses: + 200: + description: picker data + content: + application/json: + schema: + type: array + items: + type: object + properties: + name: + type: string + subsets: + type: array + items: + type: object + properties: + name: + type: string + title: + type: string + rows: + type: array + items: + type: array + items: + type: array + items: + type: string + example: + - name: basic-latin + subsets: + - name: A-Z + title: A-Z + rows: + - [[A, LATIN CAPITAL LETTER A], [B, LATIN CAPITAL LETTER B]] + - [[a, LATIN SMALL LETTER A], [b, LATIN SMALL LETTER B]] + - name: ^ + title: Letters with acute, grave, and circumflex + rows: + - [[Á, LATIN CAPITAL LETTER A WITH ACUTE]] + + '400': + $ref: '#/components/responses/InvalidValue' + '401': + $ref: '#/components/responses/Unauthorized' + '404': + $ref: '#/components/responses/NotFound' + '429': + $ref: '#/components/responses/RateLimitExceeded' + default: + $ref: '#/components/responses/UnexpectedError' + /projects/{projectid}/pages/{pagename}: put: tags: diff --git a/api/v1.inc b/api/v1.inc index e379193c1..dc7ed54f0 100644 --- a/api/v1.inc +++ b/api/v1.inc @@ -48,6 +48,7 @@ $router->add_route("GET", "v1/queues/:queueid/projects", "api_v1_queue_projects" $router->add_route("PUT", "v1/projects/:projectid/checkout", "api_v1_project_checkout"); $router->add_route("PUT", "v1/projects/:projectid/validatetext", "api_v1_project_validatetext"); +$router->add_route("GET", "v1/projects/:projectid/pickersets", "api_v1_project_pickersets"); $router->add_route("PUT", "v1/projects/:projectid/pages/:pagename", "api_v1_project_page"); $router->add_route("GET", "v1/projects/:projectid/pages/:pagename", "api_v1_project_page"); diff --git a/api/v1_projects.inc b/api/v1_projects.inc index ea28b3175..298c3e2a9 100644 --- a/api/v1_projects.inc +++ b/api/v1_projects.inc @@ -799,6 +799,40 @@ function api_v1_project_validatetext($method, $data, array $query_params) return $text_checker->analyse(receive_project_text_from_request_body('text'), $data[":projectid"]); } +function api_v1_project_pickersets($method, $data, $query_params) +{ + // expand the character ranges so that each character can have its name + $project = $data[":projectid"]; + return expand_pickersets($project->get_pickersets()); +} + +function expand_pickersets($pickersets) +{ + $expanded_pickersets = []; + foreach ($pickersets as $pickerset) { + $expanded_subsets = []; + foreach ($pickerset->get_subsets() as $subset_name => $codepoint_rows) { + $expanded_rows = []; + foreach ($codepoint_rows as $row) { + $chars = convert_codepoint_ranges_to_characters($row); + $char_titles = []; + foreach ($chars as $char) { + if (null == $char) { + $char_title = [null, null]; + } else { + $char_title = [$char, utf8_character_name($char)]; + } + $char_titles[] = $char_title; + } + $expanded_rows[] = $char_titles; + } + $expanded_subsets[] = ["name" => $subset_name, "title" => $pickerset->get_title($subset_name), "rows" => $expanded_rows]; + } + $expanded_pickersets[] = ["name" => $pickerset->name, "subsets" => $expanded_subsets]; + } + return $expanded_pickersets; +} + function api_v1_project_page(string $method, array $data, array $query_params) { global $pguser;