Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 16 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
RecordIdUpdater | |
0.00% |
0 / 16 |
|
0.00% |
0 / 2 |
56 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
updateRecordId | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
42 |
1 | <?php |
2 | |
3 | /** |
4 | * Class for updating the database when a record ID changes. |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2024. |
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 Record |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org Main Site |
28 | */ |
29 | |
30 | namespace VuFind\Record; |
31 | |
32 | use VuFind\Db\Service\CommentsServiceInterface; |
33 | use VuFind\Db\Service\Feature\TransactionInterface; |
34 | use VuFind\Db\Service\ResourceServiceInterface; |
35 | use VuFind\Db\Service\ResourceTagsServiceInterface; |
36 | use VuFind\Db\Service\UserResourceServiceInterface; |
37 | |
38 | /** |
39 | * Class for updating the database when a record ID changes. |
40 | * |
41 | * @category VuFind |
42 | * @package Record |
43 | * @author Demian Katz <demian.katz@villanova.edu> |
44 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
45 | * @link https://vufind.org Main Site |
46 | */ |
47 | class RecordIdUpdater |
48 | { |
49 | /** |
50 | * Constructor |
51 | * |
52 | * @param ResourceServiceInterface&TransactionInterface $resourceService Resource database service |
53 | * @param CommentsServiceInterface $commentsService Comments database service |
54 | * @param UserResourceServiceInterface $userResourceService User/Resource database service |
55 | * @param ResourceTagsServiceInterface $resourceTagsService Resource/Tags database service |
56 | */ |
57 | public function __construct( |
58 | protected ResourceServiceInterface&TransactionInterface $resourceService, |
59 | protected CommentsServiceInterface $commentsService, |
60 | protected UserResourceServiceInterface $userResourceService, |
61 | protected ResourceTagsServiceInterface $resourceTagsService |
62 | ) { |
63 | } |
64 | |
65 | /** |
66 | * Update the database to reflect a changed record identifier. |
67 | * |
68 | * @param string $oldId Original record ID |
69 | * @param string $newId Revised record ID |
70 | * @param string $source Record source |
71 | * |
72 | * @return void |
73 | */ |
74 | public function updateRecordId(string $oldId, string $newId, string $source = DEFAULT_SEARCH_BACKEND): void |
75 | { |
76 | if ( |
77 | $oldId !== $newId |
78 | && $resource = $this->resourceService->getResourceByRecordId($oldId, $source) |
79 | ) { |
80 | $needsDeduplication = false; |
81 | |
82 | // Do this as a transaction to prevent odd behavior: |
83 | $this->resourceService->beginTransaction(); |
84 | // Does the new ID already exist? |
85 | if ($newResource = $this->resourceService->getResourceByRecordId($newId, $source)) { |
86 | // Special case: merge new ID and old ID: |
87 | foreach ([$this->commentsService, $this->userResourceService, $this->resourceTagsService] as $service) { |
88 | $service->changeResourceId($resource->getId(), $newResource->getId()); |
89 | } |
90 | $this->resourceService->deleteResource($resource); |
91 | $needsDeduplication = true; |
92 | } else { |
93 | // Default case: just update the record ID: |
94 | $resource->setRecordId($newId); |
95 | $this->resourceService->persistEntity($resource); |
96 | } |
97 | // Done -- commit the transaction: |
98 | $this->resourceService->commitTransaction(); |
99 | |
100 | // Deduplicate rows where necessary (this can be safely done outside of the transaction): |
101 | if ($needsDeduplication) { |
102 | $this->resourceTagsService->deduplicate(); |
103 | $this->userResourceService->deduplicate(); |
104 | } |
105 | } |
106 | } |
107 | } |