Skip to content

Commit

Permalink
param to toogle search behaviour, update sort params for oc >= 16, fi…
Browse files Browse the repository at this point in the history
…x tests
  • Loading branch information
tgloeggl committed Jun 13, 2024
1 parent 3310f7c commit 56b9cc0
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 45 deletions.
7 changes: 7 additions & 0 deletions src/OpencastApi/Opencast.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ private function setEndpointProperties($config, $enableingest)
// NOTE: services must be instantiated before calling setIngest method!
$this->setIngestProperty($config);
}

// allow to disable lucene search present only up to oc 15, default is enabled
if (isset($config['features']['lucene'])
&& $config['features']['lucene'] == false
) {
$this->search->lucene = false;
}
}

private function excludeFilters()
Expand Down
137 changes: 96 additions & 41 deletions src/OpencastApi/Rest/OcSearch.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
<?php
<?php
namespace OpencastApi\Rest;

class OcSearch extends OcRest
{
const URI = '/search';
public $lucene = true;

public function __construct($restClient)
{
$restClient->registerHeaderException('Accept', self::URI);
parent::__construct($restClient);
}


/**
* Search for episodes matching the query parameters as object (JSON) by default or XML (text) on demand.
*
* @param array $params the params to pass to the call: it must cointain the following:
*
* @param array $params the params to pass to the call: it must cointain the following:
* $params = [
* 'id' => '{The ID of the single episode to be returned, if it exists}',
* 'q' => '{Any episode that matches this free-text query.}',
Expand All @@ -28,7 +29,7 @@ public function __construct($restClient)
* 'sign' => '{If results are to be signed (Default value=true)}',
* ]
* @param string $format The output format (json or xml) of the response body. (Default value = 'json')
*
*
* @return array the response result ['code' => 200, 'body' => '{The search results, formatted as xml or json}']
*/
public function getEpisodes($params = [], $format = '')
Expand Down Expand Up @@ -64,30 +65,56 @@ public function getEpisodes($params = [], $format = '')
$query['sign'] = $params['sign'];
}

$sortsASC = [
'DATE_CREATED', 'DATE_MODIFIED', 'TITLE', 'SERIES_ID',
'MEDIA_PACKAGE_ID', 'CREATOR', 'CONTRIBUTOR', 'LANGUAGE',
'LICENSE','SUBJECT','DESCRIPTION','PUBLISHER',
];
$sortsDESC = array_map(function ($sort) {
return "{$sort}_DESC";
}, $sortsASC);

$sorts = array_merge($sortsASC, $sortsDESC);

if (array_key_exists('sort', $params) && !empty($params['sort']) &&
in_array($params['sort'], $sorts)) {
$query['sort'] = $params['sort'];
// OC <= 15
if ($this->lucene) {
$sortsASC = [
'DATE_CREATED', 'DATE_MODIFIED', 'TITLE', 'SERIES_ID',
'MEDIA_PACKAGE_ID', 'CREATOR', 'CONTRIBUTOR', 'LANGUAGE',
'LICENSE','SUBJECT','DESCRIPTION','PUBLISHER',
];
$sortsDESC = array_map(function ($sort) {
return "{$sort}_DESC";
}, $sortsASC);

$sorts = array_merge($sortsASC, $sortsDESC);

if (array_key_exists('sort', $params) && !empty($params['sort']) &&
in_array($params['sort'], $sorts)) {
$query['sort'] = $params['sort'];
}

// OC >= 16
} else {
$sorts = [
'modified', 'title', 'creator', 'contributor'
];

$sortsASC = array_map(function ($sort) {
return "{$sort} asc";
}, $sorts);

$sortsDESC = array_map(function ($sort) {
return "{$sort} desc";
}, $sorts);

$sorts_list = array_merge($sorts, $sortsASC, $sortsDESC);

if (array_key_exists('sort', $params) && !empty($params['sort']) &&
in_array(strtolower($params['sort']), $sorts_list)) {
$query['sort'] = strtolower($params['sort']);
}
}

$options = $this->restClient->getQueryParams($query);
return $this->restClient->performGet($uri, $options);
}


/**
* Search a lucene query as object (JSON) by default or XML (text) on demand.
*
* @param array $params the params to pass to the call: it must cointain the following:
*
* @param array $params the params to pass to the call: it must cointain the following:
* $params = [
* 'q' => '{ The lucene query. }',
* 'series' => '{ Include series in the search result. (Default value=false)}',
Expand All @@ -98,11 +125,15 @@ public function getEpisodes($params = [], $format = '')
* 'sign' => '{If results are to be signed (Default value=true)}',
* ]
* @param string $format The output format (json or xml) of the response body. (Default value = 'json')
*
*
* @return array the response result ['code' => 200, 'body' => '{The search results, formatted as xml or json}']
*/
public function getLucene($params = [], $format = '')
{
if (!$this->lucene) {
return false;
}

$uri = self::URI . "/lucene.json";
if (!empty($format) && strtolower($format) == 'xml') {
$uri = str_replace('json', 'xml', $uri);
Expand Down Expand Up @@ -138,20 +169,20 @@ public function getLucene($params = [], $format = '')
}, $sortsASC);

$sorts = array_merge($sortsASC, $sortsDESC);

if (array_key_exists('sort', $params) && !empty($params['sort']) &&
in_array($params['sort'], $sorts)) {
$query['sort'] = $params['sort'];
}

$options = $this->restClient->getQueryParams($query);
return $this->restClient->performGet($uri, $options);
}

/**
* Search for series matching the query parameters and returns JSON (object) by default or XML (text) on demand
*
* @param array $params the params to pass to the call: it must cointain the following:
*
* @param array $params the params to pass to the call: it must cointain the following:
* $params = [
* 'id' = '{The series ID. If the additional boolean parameter "episodes" is "true", the result set will include this series episodes.}'
* 'q' => '{Any series that matches this free-text query. If the additional boolean parameter "episodes" is "true", the result set will include this series episodes.}',
Expand All @@ -163,7 +194,7 @@ public function getLucene($params = [], $format = '')
* 'sign' => '{If results are to be signed (Default value=true)}',
* ]
* @param string $format The output format (json or xml) of the response body. (Default value = 'json')
*
*
* @return array the response result ['code' => 200, 'body' => '{The search results, formatted as xml or json}']
*/
public function getSeries($params = [], $format = '')
Expand Down Expand Up @@ -196,22 +227,46 @@ public function getSeries($params = [], $format = '')
$query['sign'] = $params['sign'];
}

$sortsASC = [
'DATE_CREATED', 'DATE_MODIFIED', 'TITLE', 'SERIES_ID',
'MEDIA_PACKAGE_ID', 'CREATOR', 'CONTRIBUTOR', 'LANGUAGE',
'LICENSE','SUBJECT','DESCRIPTION','PUBLISHER',
];
$sortsDESC = array_map(function ($sort) {
return "{$sort}_DESC";
}, $sortsASC);
// OC <= 15
if ($this->lucene) {
$sortsASC = [
'DATE_CREATED', 'DATE_MODIFIED', 'TITLE', 'SERIES_ID',
'MEDIA_PACKAGE_ID', 'CREATOR', 'CONTRIBUTOR', 'LANGUAGE',
'LICENSE','SUBJECT','DESCRIPTION','PUBLISHER',
];
$sortsDESC = array_map(function ($sort) {
return "{$sort}_DESC";
}, $sortsASC);

$sorts = array_merge($sortsASC, $sortsDESC);

if (array_key_exists('sort', $params) && !empty($params['sort']) &&
in_array($params['sort'], $sorts)) {
$query['sort'] = $params['sort'];
$sorts = array_merge($sortsASC, $sortsDESC);

if (array_key_exists('sort', $params) && !empty($params['sort']) &&
in_array($params['sort'], $sorts)) {
$query['sort'] = $params['sort'];
}

// OC >= 16
} else {
$sorts = [
'modified', 'title', 'creator', 'contributor'
];

$sortsASC = array_map(function ($sort) {
return "{$sort} asc";
}, $sorts);

$sortsDESC = array_map(function ($sort) {
return "{$sort} desc";
}, $sorts);

$sorts_list = array_merge($sorts, $sortsASC, $sortsDESC);

if (array_key_exists('sort', $params) && !empty($params['sort']) &&
in_array(strtolower($params['sort']), $sorts_list)) {
$query['sort'] = strtolower($params['sort']);
}
}

$options = $this->restClient->getQueryParams($query);
return $this->restClient->performGet($uri, $options);
}
Expand Down
21 changes: 18 additions & 3 deletions tests/DataProvider/SearchDataProvider.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
<?php
namespace Tests\DataProvider;

class SearchDataProvider {

public static function getEpisodeQueryCases(): array
{
return [
Expand All @@ -18,13 +18,28 @@ public static function getEpisodeQueryCases(): array
];
}

public static function getLuceneQueryCases(): array
{
return [
[[], 'json'],
[[], 'xml'],
[[], 'XML'],
[['series' => true], ''],
[['sort' => 'DATE_CREATED_DESC'], ''],
[['offset' => 1], ''],
[['limit' => 1], ''],
[['admin' => true], ''],
[['sign' => true], ''],
];
}

public static function getSeriesQueryCases(): array
{
return [
[[], 'json'],
[['id' => '8010876e-1dce-4d38-ab8d-24b956e3d8b7'], ''],
[['episodes' => true], ''],
[['sort' => 'created desc'], ''],
[['sort' => 'modified desc'], ''],
[['offset' => 1], ''],
[['limit' => 1], ''],
[['admin' => true], ''],
Expand Down
5 changes: 4 additions & 1 deletion tests/DataProvider/SetupDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public static function getConfig($version = ''): array
'password' => $password,
'timeout' => $timeout,
'version' => '1.9.0',
'connect_timeout' => $connectTimeout
'connect_timeout' => $connectTimeout,
'features' => [
'lucene' => false
]
];
if (!empty($version)) {
$config['version'] = $version;
Expand Down
14 changes: 14 additions & 0 deletions tests/Unit/OcSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,19 @@ public function get_series($params, $format): void
$response = $this->ocSearch->getSeries($params, $format);
$this->assertSame(200, $response['code'], 'Failure to search series');
}

/**
* @test
* @dataProvider \Tests\DataProvider\SearchDataProvider::getLuceneQueryCases()
*/
public function get_lucenes($params, $format): void
{
if ($this->ocSearch->lucene) {
$response = $this->ocSearch->getLucene($params, $format);
$this->assertSame(200, $response['code'], 'Failure to search lucene');
} else {
$this->assertTrue(true);
}
}
}
?>

0 comments on commit 56b9cc0

Please sign in to comment.