Skip to content

Commit a1fb171

Browse files
authored
Use ChangeTrackerService in OAI server. (#3706)
1 parent ba70094 commit a1fb171

7 files changed

Lines changed: 155 additions & 50 deletions

File tree

module/VuFind/src/VuFind/Db/Service/ChangeTrackerService.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
namespace VuFind\Db\Service;
3232

33+
use DateTime;
3334
use VuFind\Db\Entity\ChangeTrackerEntityInterface;
3435
use VuFind\Db\Table\DbTableAwareInterface;
3536
use VuFind\Db\Table\DbTableAwareTrait;
@@ -50,6 +51,74 @@ class ChangeTrackerService extends AbstractDbService implements
5051
{
5152
use DbTableAwareTrait;
5253

54+
/**
55+
* Format to use when sending dates to legacy code.
56+
*
57+
* @var string
58+
*/
59+
protected string $dateFormat = 'Y-m-d H:i:s';
60+
61+
/**
62+
* Retrieve a row from the database based on primary key; return null if it
63+
* is not found.
64+
*
65+
* @param string $indexName The name of the Solr index holding the record.
66+
* @param string $id The ID of the record being indexed.
67+
*
68+
* @return ?ChangeTrackerEntityInterface
69+
*/
70+
public function getChangeTrackerEntity(string $indexName, string $id): ?ChangeTrackerEntityInterface
71+
{
72+
return $this->getDbTable('ChangeTracker')->retrieve($indexName, $id);
73+
}
74+
75+
/**
76+
* Retrieve a count of deleted rows from the database.
77+
*
78+
* @param string $indexName The name of the Solr index holding the record.
79+
* @param DateTime $from The beginning date of the range to search.
80+
* @param DateTime $until The end date of the range to search.
81+
*
82+
* @return int
83+
*/
84+
public function getDeletedCount(string $indexName, DateTime $from, DateTime $until): int
85+
{
86+
return $this->getDbTable('ChangeTracker')->retrieveDeletedCount(
87+
$indexName,
88+
$from->format($this->dateFormat),
89+
$until->format($this->dateFormat)
90+
);
91+
}
92+
93+
/**
94+
* Retrieve a set of deleted rows from the database.
95+
*
96+
* @param string $indexName The name of the Solr index holding the record.
97+
* @param DateTime $from The beginning date of the range to search.
98+
* @param DateTime $until The end date of the range to search.
99+
* @param int $offset Record number to retrieve first.
100+
* @param ?int $limit Retrieval limit (null for no limit)
101+
*
102+
* @return ChangeTrackerEntityInterface[]
103+
*/
104+
public function getDeletedEntities(
105+
string $indexName,
106+
DateTime $from,
107+
DateTime $until,
108+
int $offset = 0,
109+
?int $limit = null
110+
): array {
111+
return iterator_to_array(
112+
$this->getDbTable('ChangeTracker')->retrieveDeleted(
113+
$indexName,
114+
$from->format($this->dateFormat),
115+
$until->format($this->dateFormat),
116+
$offset,
117+
$limit
118+
)
119+
);
120+
}
121+
53122
/**
54123
* Update the change_tracker table to reflect that a record has been indexed.
55124
* We need to know the date of the last change to the record (independent of

module/VuFind/src/VuFind/Db/Service/ChangeTrackerServiceInterface.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
namespace VuFind\Db\Service;
3232

33+
use DateTime;
3334
use VuFind\Db\Entity\ChangeTrackerEntityInterface;
3435

3536
/**
@@ -44,6 +45,47 @@
4445
*/
4546
interface ChangeTrackerServiceInterface extends DbServiceInterface
4647
{
48+
/**
49+
* Retrieve a row from the database based on primary key; return null if it
50+
* is not found.
51+
*
52+
* @param string $indexName The name of the Solr index holding the record.
53+
* @param string $id The ID of the record being indexed.
54+
*
55+
* @return ?ChangeTrackerEntityInterface
56+
*/
57+
public function getChangeTrackerEntity(string $indexName, string $id): ?ChangeTrackerEntityInterface;
58+
59+
/**
60+
* Retrieve a count of deleted rows from the database.
61+
*
62+
* @param string $indexName The name of the Solr index holding the record.
63+
* @param DateTime $from The beginning date of the range to search.
64+
* @param DateTime $until The end date of the range to search.
65+
*
66+
* @return int
67+
*/
68+
public function getDeletedCount(string $indexName, DateTime $from, DateTime $until): int;
69+
70+
/**
71+
* Retrieve a set of deleted rows from the database.
72+
*
73+
* @param string $indexName The name of the Solr index holding the record.
74+
* @param DateTime $from The beginning date of the range to search.
75+
* @param DateTime $until The end date of the range to search.
76+
* @param int $offset Record number to retrieve first.
77+
* @param ?int $limit Retrieval limit (null for no limit)
78+
*
79+
* @return ChangeTrackerEntityInterface[]
80+
*/
81+
public function getDeletedEntities(
82+
string $indexName,
83+
DateTime $from,
84+
DateTime $until,
85+
int $offset = 0,
86+
?int $limit = null
87+
): array;
88+
4789
/**
4890
* Update the change_tracker table to reflect that a record has been indexed.
4991
* We need to know the date of the last change to the record (independent of

module/VuFind/src/VuFind/OAI/Server.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
namespace VuFind\OAI;
3333

3434
use SimpleXMLElement;
35+
use VuFind\Db\Entity\ChangeTrackerEntityInterface;
36+
use VuFind\Db\Service\ChangeTrackerServiceInterface;
3537
use VuFind\Db\Service\OaiResumptionServiceInterface;
3638
use VuFind\Exception\RecordMissing as RecordMissingException;
3739
use VuFind\SimpleXML;
@@ -213,13 +215,13 @@ class Server
213215
*
214216
* @param \VuFind\Search\Results\PluginManager $resultsManager Search manager for retrieving records
215217
* @param \VuFind\Record\Loader $recordLoader Record loader
216-
* @param \VuFind\Db\Table\PluginManager $tableManager Table manager
218+
* @param ChangeTrackerServiceInterface $trackerService ChangeTracker Service
217219
* @param OaiResumptionServiceInterface $resumptionService Database service for resumption tokens
218220
*/
219221
public function __construct(
220222
protected \VuFind\Search\Results\PluginManager $resultsManager,
221223
protected \VuFind\Record\Loader $recordLoader,
222-
protected \VuFind\Db\Table\PluginManager $tableManager,
224+
protected ChangeTrackerServiceInterface $trackerService,
223225
protected OaiResumptionServiceInterface $resumptionService
224226
) {
225227
}
@@ -321,22 +323,22 @@ public function getResponse()
321323
/**
322324
* Assign necessary interface variables to display a deleted record.
323325
*
324-
* @param SimpleXMLElement $xml XML to update
325-
* @param array $tracker Array representing a change_tracker row
326-
* @param bool $headerOnly Only attach the header?
326+
* @param SimpleXMLElement $xml XML to update
327+
* @param ChangeTrackerEntityInterface $trackerEntity ChangeTracker entity
328+
* @param bool $headerOnly Only attach the header?
327329
*
328330
* @return void
329331
*/
330-
protected function attachDeleted($xml, $tracker, $headerOnly = false)
332+
protected function attachDeleted($xml, $trackerEntity, $headerOnly = false)
331333
{
332334
// Deleted records only have a header, no metadata. However, depending
333335
// on the context we are attaching them, they may or may not need a
334336
// <record> tag wrapping the header.
335337
$record = $headerOnly ? $xml : $xml->addChild('record');
336338
$this->attachRecordHeader(
337339
$record,
338-
$this->prefixID($tracker['id']),
339-
date($this->iso8601, $this->normalizeDate($tracker['deleted'])),
340+
$this->prefixID($trackerEntity->getId()),
341+
date($this->iso8601, $trackerEntity->getDeleted()->getTimestamp()),
340342
[],
341343
'deleted'
342344
);
@@ -530,13 +532,13 @@ protected function getRecord()
530532
}
531533
} else {
532534
// No record in index -- is this deleted?
533-
$tracker = $this->tableManager->get('ChangeTracker');
534-
$row = $tracker->retrieve(
535+
536+
$row = $this->trackerService->getChangeTrackerEntity(
535537
$this->core,
536538
$this->stripID($this->params['identifier'])
537539
);
538-
if (!empty($row) && !empty($row->deleted)) {
539-
$this->attachDeleted($xml, $row->toArray());
540+
if (!empty($row) && !empty($row->getDeleted())) {
541+
$this->attachDeleted($xml, $row);
540542
} else {
541543
// Not deleted and not found in index -- error!
542544
return $this->showError('idDoesNotExist', 'Unknown Record');
@@ -944,15 +946,14 @@ protected function listSets()
944946
* @param int $until End date.
945947
* @param int $currentCursor Offset into result set
946948
*
947-
* @return \Laminas\Db\ResultSet\AbstractResultSet
949+
* @return ChangeTrackerEntityInterface[]
948950
*/
949951
protected function listRecordsGetDeleted($from, $until, $currentCursor)
950952
{
951-
$tracker = $this->tableManager->get('ChangeTracker');
952-
return $tracker->retrieveDeleted(
953+
return $this->trackerService->getDeletedEntities(
953954
$this->core,
954-
date('Y-m-d H:i:s', $from),
955-
date('Y-m-d H:i:s', $until),
955+
\DateTime::createFromFormat('U', $from),
956+
\DateTime::createFromFormat('U', $until),
956957
$currentCursor,
957958
$this->pageSize
958959
);
@@ -968,11 +969,10 @@ protected function listRecordsGetDeleted($from, $until, $currentCursor)
968969
*/
969970
protected function listRecordsGetDeletedCount($from, $until)
970971
{
971-
$tracker = $this->tableManager->get('ChangeTracker');
972-
return $tracker->retrieveDeletedCount(
972+
return $this->trackerService->getDeletedCount(
973973
$this->core,
974-
date('Y-m-d H:i:s', $from),
975-
date('Y-m-d H:i:s', $until)
974+
\DateTime::createFromFormat('U', $from),
975+
\DateTime::createFromFormat('U', $until)
976976
);
977977
}
978978

module/VuFind/src/VuFind/OAI/Server/Auth.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
namespace VuFind\OAI\Server;
3131

32-
use VuFind\Db\Service\OaiResumptionServiceInterface;
3332
use VuFind\OAI\Server as Base;
3433

3534
/**
@@ -46,23 +45,18 @@
4645
class Auth extends Base
4746
{
4847
/**
49-
* Constructor
48+
* Search object class to use
5049
*
51-
* @param \VuFind\Search\Results\PluginManager $resultsManager Search manager for retrieving records
52-
* @param \VuFind\Record\Loader $recordLoader Record loader
53-
* @param \VuFind\Db\Table\PluginManager $tableManager Table manager
54-
* @param OaiResumptionServiceInterface $resumptionService Database service for resumption tokens
50+
* @var string
5551
*/
56-
public function __construct(
57-
protected \VuFind\Search\Results\PluginManager $resultsManager,
58-
protected \VuFind\Record\Loader $recordLoader,
59-
protected \VuFind\Db\Table\PluginManager $tableManager,
60-
protected OaiResumptionServiceInterface $resumptionService
61-
) {
62-
parent::__construct($resultsManager, $recordLoader, $tableManager, $resumptionService);
63-
$this->core = 'authority';
64-
$this->searchClassId = 'SolrAuth';
65-
}
52+
protected $searchClassId = 'SolrAuth';
53+
54+
/**
55+
* What Solr core are we serving up?
56+
*
57+
* @var string
58+
*/
59+
protected $core = 'authority';
6660

6761
/**
6862
* Load data from the OAI section of config.ini. (This is called by the

module/VuFind/src/VuFind/OAI/ServerFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function __invoke(
7474
return new $requestedName(
7575
$container->get(\VuFind\Search\Results\PluginManager::class),
7676
$container->get(\VuFind\Record\Loader::class),
77-
$container->get(\VuFind\Db\Table\PluginManager::class),
77+
$servicePluginManager->get(\VuFind\Db\Service\ChangeTrackerServiceInterface::class),
7878
$servicePluginManager->get(\VuFind\Db\Service\OaiResumptionServiceInterface::class)
7979
);
8080
}

module/VuFind/tests/unit-tests/src/VuFindTest/OAI/Server/AuthTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected function getAuth(array $config = []): Auth
7474
$auth = new Auth(
7575
$this->getMockResultsManager(),
7676
$this->getMockRecordLoader(),
77-
$this->getMockTableManager(),
77+
$this->getMockChangeTracker(),
7878
$this->getMockResumptionService()
7979
);
8080
$auth->setRecordFormatter($this->getMockRecordFormatter());
@@ -84,7 +84,7 @@ protected function getAuth(array $config = []): Auth
8484
/**
8585
* Get a mock results manager
8686
*
87-
* @return \VuFind\Search\Results\PluginManager
87+
* @return MockObject&\VuFind\Search\Results\PluginManager
8888
*/
8989
protected function getMockResultsManager(): MockObject&\VuFind\Search\Results\PluginManager
9090
{
@@ -94,21 +94,21 @@ protected function getMockResultsManager(): MockObject&\VuFind\Search\Results\Pl
9494
/**
9595
* Get a mock record loader
9696
*
97-
* @return \VuFind\Record\Loader
97+
* @return MockObject&\VuFind\Record\Loader
9898
*/
9999
protected function getMockRecordLoader(): MockObject&\VuFind\Record\Loader
100100
{
101101
return $this->createMock(\VuFind\Record\Loader::class);
102102
}
103103

104104
/**
105-
* Get a mock table manager
105+
* Get a mock change tracker service
106106
*
107-
* @return MockObject&\VuFind\Db\Table\PluginManager
107+
* @return MockObject&\VuFind\Db\Service\ChangeTrackerServiceInterface
108108
*/
109-
protected function getMockTableManager(): MockObject&\VuFind\Db\Table\PluginManager
109+
protected function getMockChangeTracker(): MockObject&\VuFind\Db\Service\ChangeTrackerServiceInterface
110110
{
111-
return $this->createMock(\VuFind\Db\Table\PluginManager::class);
111+
return $this->createMock(\VuFind\Db\Service\ChangeTrackerServiceInterface::class);
112112
}
113113

114114
/**

module/VuFind/tests/unit-tests/src/VuFindTest/OAI/ServerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected function getServer($config = []): Server
7474
$server = new Server(
7575
$this->getMockResultsManager(),
7676
$this->getMockRecordLoader(),
77-
$this->getMockTableManager(),
77+
$this->getMockChangeTracker(),
7878
$this->getMockResumptionService()
7979
);
8080
$server->setRecordFormatter($this->getMockRecordFormatter());
@@ -102,13 +102,13 @@ protected function getMockRecordLoader(): MockObject&\VuFind\Record\Loader
102102
}
103103

104104
/**
105-
* Get a mock table manager
105+
* Get a mock change tracker service
106106
*
107-
* @return MockObject&\VuFind\Db\Table\PluginManager
107+
* @return MockObject&\VuFind\Db\Service\ChangeTrackerServiceInterface
108108
*/
109-
protected function getMockTableManager(): MockObject&\VuFind\Db\Table\PluginManager
109+
protected function getMockChangeTracker(): MockObject&\VuFind\Db\Service\ChangeTrackerServiceInterface
110110
{
111-
return $this->createMock(\VuFind\Db\Table\PluginManager::class);
111+
return $this->createMock(\VuFind\Db\Service\ChangeTrackerServiceInterface::class);
112112
}
113113

114114
/**

0 commit comments

Comments
 (0)