Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ChangeTrackerService
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 5
30
0.00% covered (danger)
0.00%
0 / 1
 getChangeTrackerEntity
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDeletedCount
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getDeletedEntities
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 markDeleted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 index
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Database service for change tracker.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2023.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Database
25 * @author   Sudharma Kellampalli <skellamp@villanova.edu>
26 * @author   Demian Katz <demian.katz@villanova.edu>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org/wiki/development:plugins:database_gateways Wiki
29 */
30
31namespace VuFind\Db\Service;
32
33use DateTime;
34use VuFind\Db\Entity\ChangeTrackerEntityInterface;
35use VuFind\Db\Table\DbTableAwareInterface;
36use VuFind\Db\Table\DbTableAwareTrait;
37
38/**
39 * Database service for change tracker.
40 *
41 * @category VuFind
42 * @package  Database
43 * @author   Sudharma Kellampalli <skellamp@villanova.edu>
44 * @author   Demian Katz <demian.katz@villanova.edu>
45 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
46 * @link     https://vufind.org/wiki/development:plugins:database_gateways Wiki
47 */
48class ChangeTrackerService extends AbstractDbService implements
49    ChangeTrackerServiceInterface,
50    DbTableAwareInterface
51{
52    use DbTableAwareTrait;
53
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
122    /**
123     * Update the change tracker table to indicate that a record has been deleted.
124     *
125     * The method returns the updated/created row when complete.
126     *
127     * @param string $core The Solr core holding the record.
128     * @param string $id   The ID of the record being indexed.
129     *
130     * @return ChangeTrackerEntityInterface
131     */
132    public function markDeleted(string $core, string $id): ChangeTrackerEntityInterface
133    {
134        return $this->getDbTable('ChangeTracker')->markDeleted($core, $id);
135    }
136
137    /**
138     * Update the change_tracker table to reflect that a record has been indexed.
139     * We need to know the date of the last change to the record (independent of
140     * its addition to the index) in order to tell the difference between a
141     * reindex of a previously-encountered record and a genuine change.
142     *
143     * The method returns the updated/created row when complete.
144     *
145     * @param string $core   The Solr core holding the record.
146     * @param string $id     The ID of the record being indexed.
147     * @param int    $change The timestamp of the last record change.
148     *
149     * @return ChangeTrackerEntityInterface
150     */
151    public function index(string $core, string $id, int $change): ChangeTrackerEntityInterface
152    {
153        return $this->getDbTable('ChangeTracker')->index($core, $id, $change);
154    }
155}