1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fab\Media\Resource; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Fab/Media project under GPLv2 or later. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the |
9
|
|
|
* LICENSE.md file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
use Fab\Vidi\Service\DataService; |
13
|
|
|
use TYPO3\CMS\Core\Database\ConnectionPool; |
14
|
|
|
use TYPO3\CMS\Core\Database\Query\QueryBuilder; |
15
|
|
|
use TYPO3\CMS\Core\Resource\File; |
16
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* File Reference Service. |
20
|
|
|
*/ |
21
|
|
|
class FileReferenceService |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Return all references found in sys_file_reference. |
25
|
|
|
* |
26
|
|
|
* @param File|int $file |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public function findFileReferences($file) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
$fileIdentifier = $file instanceof File ? $file->getUid() : (int)$file; |
|
|
|
|
33
|
|
|
|
34
|
|
|
// Get the file references of the file. |
35
|
|
|
return $this->getDataService()->getRecords( |
36
|
|
|
'sys_file_reference', |
37
|
|
|
[ |
38
|
|
|
'uid_local' => $fileIdentifier, |
39
|
|
|
] |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Return soft image references. |
45
|
|
|
* |
46
|
|
|
* @param File|int $file |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function findSoftImageReferences($file) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
|
52
|
|
|
$fileIdentifier = $file instanceof File ? $file->getUid() : (int)$file; |
|
|
|
|
53
|
|
|
|
54
|
|
|
// Get the file references of the file in the RTE. |
55
|
|
|
$softReferences = $this->getDataService()->getRecords( |
56
|
|
|
'sys_refindex', |
57
|
|
|
[ |
58
|
|
|
'softref_key' => 'rtehtmlarea_images', |
59
|
|
|
'ref_table' => 'sys_file', |
60
|
|
|
'ref_uid' => $fileIdentifier, |
61
|
|
|
] |
62
|
|
|
); |
63
|
|
|
return $softReferences; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return link image references. |
68
|
|
|
* |
69
|
|
|
* @param File|int $file |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function findSoftLinkReferences($file) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
|
75
|
|
|
$fileIdentifier = $file instanceof File ? $file->getUid() : (int)$file; |
|
|
|
|
76
|
|
|
|
77
|
|
|
// Get the link references of the file. |
78
|
|
|
$softReferences = $this->getDataService()->getRecords( |
79
|
|
|
'sys_refindex', |
80
|
|
|
[ |
81
|
|
|
'softref_key' => 'typolink_tag', |
82
|
|
|
'ref_table' => 'sys_file', |
83
|
|
|
'ref_uid' => $fileIdentifier, |
84
|
|
|
] |
85
|
|
|
); |
86
|
|
|
return $softReferences; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Count all references found in sys_file_reference. |
91
|
|
|
* |
92
|
|
|
* @param File|int $file |
93
|
|
|
* @return int |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function countFileReferences($file) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$fileIdentifier = $file instanceof File ? $file->getUid() : (int)$file; |
|
|
|
|
98
|
|
|
|
99
|
|
|
return $this->getDataService() |
100
|
|
|
->count( |
101
|
|
|
'sys_file_reference', |
102
|
|
|
[ |
103
|
|
|
'uid_local' => $fileIdentifier |
104
|
|
|
] |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Count soft image references. |
110
|
|
|
* |
111
|
|
|
* @param File|int $file |
112
|
|
|
* @return int |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public function countSoftImageReferences($file) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
$fileIdentifier = $file instanceof File ? $file->getUid() : (int)$file; |
|
|
|
|
117
|
|
|
|
118
|
|
|
return $this->getDataService() |
119
|
|
|
->count( |
120
|
|
|
'sys_refindex', |
121
|
|
|
[ |
122
|
|
|
'softref_key' => 'rtehtmlarea_images', |
123
|
|
|
'ref_table' => 'sys_file', |
124
|
|
|
'ref_uid' => $fileIdentifier |
125
|
|
|
] |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Count link image references. |
131
|
|
|
* |
132
|
|
|
* @param File|int $file |
133
|
|
|
* @return int |
134
|
|
|
*/ |
135
|
|
View Code Duplication |
public function countSoftLinkReferences($file) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
$fileIdentifier = $file instanceof File ? $file->getUid() : (int)$file; |
|
|
|
|
138
|
|
|
|
139
|
|
|
return $this->getDataService() |
140
|
|
|
->count( |
141
|
|
|
'sys_refindex', |
142
|
|
|
[ |
143
|
|
|
'softref_key' => 'typolink_tag', |
144
|
|
|
'ref_table' => 'sys_file', |
145
|
|
|
'ref_uid' => $fileIdentifier |
146
|
|
|
] |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Count total reference. |
152
|
|
|
* |
153
|
|
|
* @param File|int $file |
154
|
|
|
* @return int |
155
|
|
|
*/ |
156
|
|
|
public function countTotalReferences($file) |
157
|
|
|
{ |
158
|
|
|
$numberOfReferences = $this->countFileReferences($file); |
159
|
|
|
$numberOfReferences += $this->countSoftImageReferences($file); |
160
|
|
|
$numberOfReferences += $this->countSoftLinkReferences($file); |
161
|
|
|
|
162
|
|
|
return $numberOfReferences; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $tableName |
167
|
|
|
* @return object|QueryBuilder |
168
|
|
|
*/ |
169
|
|
|
protected function getQueryBuilder($tableName): QueryBuilder |
170
|
|
|
{ |
171
|
|
|
/** @var ConnectionPool $connectionPool */ |
172
|
|
|
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); |
173
|
|
|
return $connectionPool->getQueryBuilderForTable($tableName); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return object|DataService |
178
|
|
|
*/ |
179
|
|
|
protected function getDataService(): DataService |
180
|
|
|
{ |
181
|
|
|
return GeneralUtility::makeInstance(DataService::class); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.