Skip to content

Commit

Permalink
refs vufind-org#2999 Add support for result set identifier in Record …
Browse files Browse the repository at this point in the history
…and Backend classes

* Updated `VuFind\View\Helper\Root\Record` to include `ResultSetIdentifier` in the HTML element IDs, ensuring unique and context-aware IDs across templates.
* Modified `VuFindTest\View\Helper\Root\RecordTest` to account for the new `ResultSetIdentifier` in tests, ensuring the unique identifier is reflected correctly in different scenarios.
* Enhanced `VuFindSearch\Backend\AbstractBackend` by generating and assigning a unique, shorter UUID-like identifier to each record collection using `Laminas\Math\Rand`.
* Implemented `setResultSetIdentifier` and `getResultSetIdentifier` methods in `RecordCollectionInterface`, `AbstractRecordCollection`, `RecordInterface`, and `RecordTrait` to manage and retrieve the result set identifiers.
* Adjusted `RecordTrait` to include a new `resultSetIdentifier` property with appropriate PHPDoc for easier management and retrieval of unique identifiers per record.
  • Loading branch information
RLangeUni committed Oct 16, 2024
1 parent 1848009 commit bd13052
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 5 deletions.
4 changes: 3 additions & 1 deletion module/VuFind/src/VuFind/View/Helper/Root/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,9 @@ public function getUniqueHtmlElementId($idPrefix = '')
return preg_replace(
"/\s+/",
'_',
($idPrefix ? $idPrefix . '-' : '') . $this->getUniqueIdWithSourcePrefix()
($idPrefix ? $idPrefix . '-' : '')
. $this->driver->getResultSetIdentifier() . '-'
. $this->driver->getUniqueId()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,16 @@ public function testGetCheckbox(): void
$driver = $this->loadRecordFixture('testbug1.json');
$tpl = 'record/checkbox.phtml';
$context = $this->getMockContext();
$randomIdentifier = 'baz';
$driver->setResultSetIdentifier($randomIdentifier);

$expectedCalls = [
[
$tpl,
[
'number' => 1,
'id' => 'Solr|000105196',
'checkboxElementId' => 'bar-Solr|000105196',
'checkboxElementId' => "bar-{$randomIdentifier}-000105196",
'prefix' => 'bar',
'formAttr' => 'foo',
],
Expand All @@ -409,7 +411,7 @@ public function testGetCheckbox(): void
[
'number' => 2,
'id' => 'Solr|000105196',
'checkboxElementId' => 'bar-Solr|000105196',
'checkboxElementId' => "bar-{$randomIdentifier}-000105196",
'prefix' => 'bar',
'formAttr' => 'foo',
],
Expand Down Expand Up @@ -439,16 +441,18 @@ public function testGetUniqueHtmlElementId()
{
$driver = $this->loadRecordFixture('testbug1.json');
$record = $this->getRecord($driver);
$randomIdentifier = 'bar';
$driver->setResultSetIdentifier($randomIdentifier);

// with prefix
$this->assertEquals(
'testPrefix-Solr|000105196',
"testPrefix-{$randomIdentifier}-000105196",
$record->getUniqueHtmlElementId('testPrefix')
);

// without prefix
$this->assertEquals(
'Solr|000105196',
"{$randomIdentifier}-000105196",
$record->getUniqueHtmlElementId()
);
}
Expand Down
32 changes: 32 additions & 0 deletions module/VuFindSearch/src/VuFindSearch/Backend/AbstractBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
namespace VuFindSearch\Backend;

use Laminas\Log\LoggerAwareInterface;
use Laminas\Math\Rand;
use VuFindSearch\Response\RecordCollectionFactoryInterface;
use VuFindSearch\Response\RecordCollectionInterface;

Expand Down Expand Up @@ -116,6 +117,37 @@ abstract public function getRecordCollectionFactory();
protected function injectSourceIdentifier(RecordCollectionInterface $response)
{
$response->setSourceIdentifiers($this->identifier);
$response->setResultSetIdentifier($this->generateUuid());

return $response;
}

/**
* Sets the result set identifier for the record collection.
*
* @param string $uuid A valid UUID associated with the data set.
*
* @return void
*/
public function setResultSetIdentifier(string $uuid): void {
$this->recordCollection->setResultSetIdentifier($uuid);
}

/**
* Generates a shorter UUID-like identifier.
*
* This method uses Laminas\Math\Rand to generate cryptographically secure random bytes
* and formats them into a shorter identifier.
*
* @return string A randomly generated shorter UUID-like identifier.
*/
function generateUuid(): string {
$data = bin2hex(Rand::getBytes(8));
return sprintf(
'%08s-%04s-%04s',
substr($data, 0, 8),
substr($data, 8, 4),
substr($data, 12, 4)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,20 @@ public function count(): int
{
return count($this->records);
}

/**
* Sets the result set identifier for all records in the collection.
*
* This method assigns a given UUID to each record in the collection by calling
* the `setResultSetIdentifier` method on each record.
*
* @param string $uuid A valid UUID to be assigned to each record in the collection.
*
* @return void
*/
public function setResultSetIdentifier($uuid) {
foreach ($this->records as $record) {
$record->setResultSetIdentifier($uuid);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ public function setSourceIdentifiers($identifier, $searchBackendId = '');
*/
public function getSourceIdentifier();

/**
* Return the result set identifier.
*
* @return void
*/
public function setResultSetIdentifier($uuid);

/**
* Add a record to the collection.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public function getSourceIdentifier();
*/
public function getSearchBackendIdentifier();

public function setResultSetIdentifier($uuid);

public function getResultSetIdentifier();

/**
* Add a label for the record
*
Expand Down
35 changes: 35 additions & 0 deletions module/VuFindSearch/src/VuFindSearch/Response/RecordTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ trait RecordTrait
* @var string
*/
protected $sourceIdentifier = '';

/**
* The unique identifier for the result set.
*
* This property stores a UUID or similar identifier that uniquely identifies
* the result set. It is typically set by calling the `setResultSetIdentifier` method.
*
* @var string|null
*/
protected $resultSetIdentifier;

/**
* Used for identifying the search backend used to find the record
Expand Down Expand Up @@ -109,6 +119,31 @@ public function getSearchBackendIdentifier()
{
return $this->searchBackendIdentifier;
}

/**
* Sets the unique result set identifier.
*
* This method assigns a UUID or similar identifier to the result set.
*
* @param string $uuid A valid UUID or identifier to assign to the result set.
*
* @return void
*/
public function setResultSetIdentifier(string $uuid): void {
$this->resultSetIdentifier = $uuid;
}

/**
* Retrieves the unique result set identifier.
*
* This method returns the UUID or similar identifier associated with the result set.
* If no identifier has been set, it will return null.
*
* @return string|null The UUID of the result set, or null if not set.
*/
public function getResultSetIdentifier(): ?string {
return $this->resultSetIdentifier;
}

/**
* Add a label for the record
Expand Down

0 comments on commit bd13052

Please sign in to comment.