Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Record
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 5
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 findRecord
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 findRecords
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 updateRecord
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 cleanup
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * Table Definition for record
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 * Copyright (C) University of Freiburg 2014.
10 * Copyright (C) The National Library of Finland 2015.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2,
14 * as published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
24 *
25 * @category VuFind
26 * @package  Db_Table
27 * @author   Markus Beh <markus.beh@ub.uni-freiburg.de>
28 * @author   Ere Maijala <ere.maijala@helsinki.fi>
29 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
30 * @link     https://vufind.org Main Site
31 */
32
33namespace VuFind\Db\Table;
34
35use Laminas\Db\Adapter\Adapter;
36use Laminas\Db\Sql\Predicate\Expression;
37use Laminas\Db\Sql\Where;
38use VuFind\Db\Row\RowGateway;
39use VuFind\Db\Service\DbServiceAwareInterface;
40use VuFind\Db\Service\DbServiceAwareTrait;
41use VuFind\Db\Service\RecordServiceInterface;
42
43use function count;
44
45/**
46 * Table Definition for record
47 *
48 * @category VuFind
49 * @package  Db_Table
50 * @author   Markus Beh <markus.beh@ub.uni-freiburg.de>
51 * @author   Ere Maijala <ere.maijala@helsinki.fi>
52 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
53 * @link     https://vufind.org Main Site
54 */
55class Record extends Gateway implements DbServiceAwareInterface
56{
57    use DbServiceAwareTrait;
58
59    /**
60     * Constructor
61     *
62     * @param Adapter       $adapter Database adapter
63     * @param PluginManager $tm      Table manager
64     * @param array         $cfg     Laminas configuration
65     * @param RowGateway    $rowObj  Row prototype object (null for default)
66     * @param string        $table   Name of database table to interface with
67     */
68    public function __construct(
69        Adapter $adapter,
70        PluginManager $tm,
71        $cfg,
72        ?RowGateway $rowObj = null,
73        $table = 'record'
74    ) {
75        parent::__construct($adapter, $tm, $cfg, $rowObj, $table);
76    }
77
78    /**
79     * Find a record by id
80     *
81     * @param string $id     Record ID
82     * @param string $source Record source
83     *
84     * @throws \Exception
85     * @return ?\VuFind\Db\Row\Record
86     */
87    public function findRecord($id, $source)
88    {
89        return $this->select(['record_id' => $id, 'source' => $source])->current();
90    }
91
92    /**
93     * Find records by ids
94     *
95     * @param array  $ids    Record IDs
96     * @param string $source Record source
97     *
98     * @throws \Exception
99     * @return array Array of record row objects found
100     */
101    public function findRecords($ids, $source)
102    {
103        if (empty($ids)) {
104            return [];
105        }
106
107        $where = new Where();
108        foreach ($ids as $id) {
109            $nested = $where->or->nest();
110            $nested->addPredicates(
111                ['record_id' => $id, 'source' => $source]
112            );
113        }
114
115        return iterator_to_array($this->select($where));
116    }
117
118    /**
119     * Update an existing entry in the record table or create a new one
120     *
121     * @param string $id      Record ID
122     * @param string $source  Data source
123     * @param mixed  $rawData Raw data from source (must be serializable)
124     *
125     * @return \VuFind\Db\Row\Record Updated or newly added record
126     *
127     * @deprecated Use RecordServiceInterface::updateRecord()
128     */
129    public function updateRecord($id, $source, $rawData)
130    {
131        return $this->getDbService(RecordServiceInterface::class)->updateRecord($id, $source, $rawData);
132    }
133
134    /**
135     * Clean up orphaned entries (i.e. entries that are not in favorites anymore)
136     *
137     * @return int Number of records deleted
138     */
139    public function cleanup()
140    {
141        $callback = function ($select) {
142            $select->columns(['id']);
143            $select->join(
144                'resource',
145                new Expression(
146                    'record.record_id = resource.record_id'
147                    . ' AND record.source = resource.source'
148                ),
149                []
150            )->join(
151                'user_resource',
152                'resource.id = user_resource.resource_id',
153                [],
154                $select::JOIN_LEFT
155            );
156            $select->where->isNull('user_resource.id');
157        };
158
159        $results = $this->select($callback);
160        foreach ($results as $result) {
161            $this->delete(['id' => $result['id']]);
162        }
163
164        return count($results);
165    }
166}