Skip to content

Commit

Permalink
Merge pull request #251 from digirati-co-uk/feature/autocomplete-locale
Browse files Browse the repository at this point in the history
Added locale to autocomplete requests
  • Loading branch information
stephenwf authored Dec 15, 2020
2 parents ebd7281 + 61b2329 commit 6bb9142
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 13 deletions.
5 changes: 3 additions & 2 deletions repos/auto-complete/src/Completion/CompletionContributor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ interface CompletionContributor
* {@code resourceClasses}.
*
* @param string $term
* @param string[] ...$resourceClasses
* @param string $language
* @param string ...$resourceClasses
*
* @return PromiseInterface
*/
public function doCompletion(string $term, string... $resourceClasses): PromiseInterface;
public function doCompletion(string $term, string $language, string... $resourceClasses): PromiseInterface;

/**
* Check if this {@link CompletionContributor} advertises completion results for the given {@code resourceClass}.
Expand Down
7 changes: 4 additions & 3 deletions repos/auto-complete/src/Completion/CompletionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ public function __construct(CompletionContributor... $contributors)

/**
* @param string $term The search term to query for completion results on.
* @param string $language
* @param string[] $resourceClasses A namespaced vocabulary class to filter search results by.
* @return Traversable|CompletionItem[] A collection of {@link CompletionItem}s.
* @throws Exception If no {@link CompletionContributor}s are configured.
* @throws \Throwable
*/
public function getSuggestions(string $term, string... $resourceClasses): array
public function getSuggestions(string $term, string $language, string... $resourceClasses): array
{
if (count($this->contributors) === 0) {
throw new Exception("No completion contributors are configured");
Expand All @@ -41,7 +42,7 @@ public function getSuggestions(string $term, string... $resourceClasses): array
continue;
}

$requests[] = $contributor->doCompletion($term, ...$resourceClasses);
$requests[] = $contributor->doCompletion($term, $language, ...$resourceClasses);
}

$responses = Promise\unwrap($requests);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private function getTagName(int $tag)
}
}

public function doCompletion(string $term, string... $resourceClasses): PromiseInterface
public function doCompletion(string $term, string $language, string... $resourceClasses): PromiseInterface
{
$request = $this->httpClient->requestAsync('GET', static::FAST_URI, [
'query' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(Manager $api)
$this->api = $api;
}

public function doCompletion(string $term, string... $resourceClasses): PromiseInterface
public function doCompletion(string $term, string $language, string... $resourceClasses): PromiseInterface
{
return Promises\task(function () use ($term, $resourceClasses) {
$searchResult = $this->api->search('items', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@ public function __construct(ClientInterface $httpClient)
$this->httpClient = $httpClient;
}

public function doCompletion(string $term, string... $resourceClasses): PromiseInterface
public function doCompletion(string $term, string $language, string... $resourceClasses): PromiseInterface
{
$request = $this->httpClient->requestAsync('GET', static::WIKIDATA_URI, [
'query' => [
'search' => $term,
'limit' => '5',
'action' => 'wbsearchentities',
'language' => 'en',
'language' => $language ? $language : 'en',
'format' => 'json'
]
]);

return $request->then(function (ResponseInterface $response) {
$content = json_decode((string)$response->getBody(), true);

$term = $content['searchinfo']['search'];

if (json_last_error() !== JSON_ERROR_NONE) {
Expand All @@ -47,16 +48,18 @@ public function doCompletion(string $term, string... $resourceClasses): PromiseI
$content['search'],

function (array $record) {
return $record['repository'] === 'local';
return $record['repository'] === 'wikidata';
}
);

$authoritativeRecords = $this->sortWikiDataResults($authoritativeRecords, $term);

return array_map(
function (array $result) {
$label = isset($result['match']) && isset($result['match']['text']) ? $result['match']['text'] : $result['label'];

$uri = $result['concepturi'];
$text = $result['label'] . ' - ' . $result['description'];
$text = $label . ' - ' . $result['description'];
$tag = $result['id'];

return new CompletionItem($uri, $text, $tag);
Expand Down
3 changes: 2 additions & 1 deletion repos/auto-complete/src/Controller/CompletionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ public function completionsAction()
{
$resourceClasses = explode(',', $this->params()->fromQuery('class', []));
$term = $this->params()->fromQuery('q');
$language = $this->params()->fromHeader('X-Annotation-Studio-Locale', 'en')->getFieldValue();

if ($term === null) {
return $this->errorMessage('No search term provided'); // @translate
}

try {
$completions = $this->completionService->getSuggestions($term, ...$resourceClasses);
$completions = $this->completionService->getSuggestions($term, $language, ...$resourceClasses);
} catch (Exception $e) {
return $this->errorMessage($e->getMessage());
}
Expand Down

0 comments on commit 6bb9142

Please sign in to comment.