Total Complexity | 106 |
Total Lines | 849 |
Duplicated Lines | 0 % |
Changes | 14 | ||
Bugs | 0 | Features | 0 |
Complex classes like DocumentRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DocumentRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class DocumentRepository extends \TYPO3\CMS\Extbase\Persistence\Repository |
||
29 | { |
||
30 | /** |
||
31 | * The controller settings passed to the repository for some special actions. |
||
32 | * |
||
33 | * @var array |
||
34 | * @access protected |
||
35 | */ |
||
36 | protected $settings; |
||
37 | |||
38 | /** |
||
39 | * Find one document by given parameters |
||
40 | * |
||
41 | * GET parameters may be: |
||
42 | * |
||
43 | * - 'id': the uid of the document |
||
44 | * - 'location': the URL of the location of the XML file |
||
45 | * - 'recordId': the record_id of the document |
||
46 | * |
||
47 | * Currently used by EXT:slub_digitalcollections |
||
48 | * |
||
49 | * @param array $parameters |
||
50 | * |
||
51 | * @return \Kitodo\Dlf\Domain\Model\Document|null |
||
52 | */ |
||
53 | public function findOneByParameters($parameters) |
||
54 | { |
||
55 | $doc = null; |
||
56 | $document = null; |
||
57 | |||
58 | if (isset($parameters['id']) && MathUtility::canBeInterpretedAsInteger($parameters['id'])) { |
||
59 | |||
60 | $document = $this->findOneByIdAndSettings($parameters['id']); |
||
61 | |||
62 | } else if (isset($parameters['recordId'])) { |
||
63 | |||
64 | $document = $this->findOneByRecordId($parameters['recordId']); |
||
|
|||
65 | |||
66 | } else if (isset($parameters['location']) && GeneralUtility::isValidUrl($parameters['location'])) { |
||
67 | |||
68 | $doc = Doc::getInstance($parameters['location'], [], true); |
||
69 | |||
70 | if ($doc->recordId) { |
||
71 | $document = $this->findOneByRecordId($doc->recordId); |
||
72 | } |
||
73 | |||
74 | if ($document === null) { |
||
75 | // create new (dummy) Document object |
||
76 | $document = GeneralUtility::makeInstance(Document::class); |
||
77 | $document->setLocation($parameters['location']); |
||
78 | } |
||
79 | |||
80 | } |
||
81 | |||
82 | if ($document !== null && $doc === null) { |
||
83 | $doc = Doc::getInstance($document->getLocation(), [], true); |
||
84 | } |
||
85 | |||
86 | if ($doc !== null) { |
||
87 | $document->setDoc($doc); |
||
88 | } |
||
89 | |||
90 | return $document; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Find the oldest document |
||
95 | * |
||
96 | * @return \Kitodo\Dlf\Domain\Model\Document|null |
||
97 | */ |
||
98 | public function findOldestDocument() |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param int $partOf |
||
110 | * @param \Kitodo\Dlf\Domain\Model\Structure $structure |
||
111 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
112 | */ |
||
113 | public function getChildrenOfYearAnchor($partOf, $structure) |
||
114 | { |
||
115 | $query = $this->createQuery(); |
||
116 | |||
117 | $query->matching($query->equals('structure', $structure)); |
||
118 | $query->matching($query->equals('partof', $partOf)); |
||
119 | |||
120 | $query->setOrderings([ |
||
121 | 'mets_orderlabel' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING |
||
122 | ]); |
||
123 | |||
124 | return $query->execute(); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Finds all documents for the given settings |
||
129 | * |
||
130 | * @param int $uid |
||
131 | * @param array $settings |
||
132 | * |
||
133 | * @return \Kitodo\Dlf\Domain\Model\Document|null |
||
134 | */ |
||
135 | public function findOneByIdAndSettings($uid, $settings = []) |
||
136 | { |
||
137 | $settings = ['documentSets' => $uid]; |
||
138 | |||
139 | return $this->findDocumentsBySettings($settings)->getFirst(); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Finds all documents for the given settings |
||
144 | * |
||
145 | * @param array $settings |
||
146 | * |
||
147 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
148 | */ |
||
149 | public function findDocumentsBySettings($settings = []) |
||
150 | { |
||
151 | $query = $this->createQuery(); |
||
152 | |||
153 | $constraints = []; |
||
154 | |||
155 | if ($settings['documentSets']) { |
||
156 | $constraints[] = $query->in('uid', GeneralUtility::intExplode(',', $settings['documentSets'])); |
||
157 | } |
||
158 | |||
159 | if (isset($settings['excludeOther']) && (int) $settings['excludeOther'] === 0) { |
||
160 | $query->getQuerySettings()->setRespectStoragePage(false); |
||
161 | } |
||
162 | |||
163 | if (count($constraints)) { |
||
164 | $query->matching( |
||
165 | $query->logicalAnd($constraints) |
||
166 | ); |
||
167 | } |
||
168 | |||
169 | return $query->execute(); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Finds all documents for the given collections |
||
174 | * |
||
175 | * @param array $collections |
||
176 | * @param int $limit |
||
177 | * |
||
178 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
179 | */ |
||
180 | public function findAllByCollectionsLimited($collections, $limit = 50) |
||
181 | { |
||
182 | $query = $this->createQuery(); |
||
183 | |||
184 | // order by start_date -> start_time... |
||
185 | $query->setOrderings( |
||
186 | ['tstamp' => QueryInterface::ORDER_DESCENDING] |
||
187 | ); |
||
188 | |||
189 | $constraints = []; |
||
190 | if ($collections) { |
||
191 | $constraints[] = $query->in('collections.uid', $collections); |
||
192 | } |
||
193 | |||
194 | if (count($constraints)) { |
||
195 | $query->matching( |
||
196 | $query->logicalAnd($constraints) |
||
197 | ); |
||
198 | } |
||
199 | |||
200 | if ($limit > 0) { |
||
201 | $query->setLimit((int) $limit); |
||
202 | } |
||
203 | |||
204 | return $query->execute(); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Count the titles and volumes for statistics |
||
209 | * |
||
210 | * Volumes are documents that are both |
||
211 | * a) "leaf" elements i.e. partof != 0 |
||
212 | * b) "root" elements that are not referenced by other documents ("root" elements that have no descendants) |
||
213 | |||
214 | * @param array $settings |
||
215 | * |
||
216 | * @return array |
||
217 | */ |
||
218 | public function getStatisticsForSelectedCollection($settings) |
||
219 | { |
||
220 | if ($settings['collections']) { |
||
221 | // Include only selected collections. |
||
222 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
223 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
224 | |||
225 | $countTitles = $queryBuilder |
||
226 | ->count('tx_dlf_documents.uid') |
||
227 | ->from('tx_dlf_documents') |
||
228 | ->innerJoin( |
||
229 | 'tx_dlf_documents', |
||
230 | 'tx_dlf_relations', |
||
231 | 'tx_dlf_relations_joins', |
||
232 | $queryBuilder->expr()->eq( |
||
233 | 'tx_dlf_relations_joins.uid_local', |
||
234 | 'tx_dlf_documents.uid' |
||
235 | ) |
||
236 | ) |
||
237 | ->innerJoin( |
||
238 | 'tx_dlf_relations_joins', |
||
239 | 'tx_dlf_collections', |
||
240 | 'tx_dlf_collections_join', |
||
241 | $queryBuilder->expr()->eq( |
||
242 | 'tx_dlf_relations_joins.uid_foreign', |
||
243 | 'tx_dlf_collections_join.uid' |
||
244 | ) |
||
245 | ) |
||
246 | ->where( |
||
247 | $queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['storagePid'])), |
||
248 | $queryBuilder->expr()->eq('tx_dlf_collections_join.pid', intval($settings['storagePid'])), |
||
249 | $queryBuilder->expr()->eq('tx_dlf_documents.partof', 0), |
||
250 | $queryBuilder->expr()->in('tx_dlf_collections_join.uid', $queryBuilder->createNamedParameter(GeneralUtility::intExplode(',', $settings['collections']), Connection::PARAM_INT_ARRAY)), |
||
251 | $queryBuilder->expr()->eq('tx_dlf_relations_joins.ident', $queryBuilder->createNamedParameter('docs_colls')) |
||
252 | ) |
||
253 | ->execute() |
||
254 | ->fetchColumn(0); |
||
255 | |||
256 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
257 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
258 | $subQueryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
259 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
260 | |||
261 | $subQuery = $subQueryBuilder |
||
262 | ->select('tx_dlf_documents.partof') |
||
263 | ->from('tx_dlf_documents') |
||
264 | ->where( |
||
265 | $subQueryBuilder->expr()->neq('tx_dlf_documents.partof', 0) |
||
266 | ) |
||
267 | ->groupBy('tx_dlf_documents.partof') |
||
268 | ->getSQL(); |
||
269 | |||
270 | $countVolumes = $queryBuilder |
||
271 | ->count('tx_dlf_documents.uid') |
||
272 | ->from('tx_dlf_documents') |
||
273 | ->innerJoin( |
||
274 | 'tx_dlf_documents', |
||
275 | 'tx_dlf_relations', |
||
276 | 'tx_dlf_relations_joins', |
||
277 | $queryBuilder->expr()->eq( |
||
278 | 'tx_dlf_relations_joins.uid_local', |
||
279 | 'tx_dlf_documents.uid' |
||
280 | ) |
||
281 | ) |
||
282 | ->innerJoin( |
||
283 | 'tx_dlf_relations_joins', |
||
284 | 'tx_dlf_collections', |
||
285 | 'tx_dlf_collections_join', |
||
286 | $queryBuilder->expr()->eq( |
||
287 | 'tx_dlf_relations_joins.uid_foreign', |
||
288 | 'tx_dlf_collections_join.uid' |
||
289 | ) |
||
290 | ) |
||
291 | ->where( |
||
292 | $queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['storagePid'])), |
||
293 | $queryBuilder->expr()->eq('tx_dlf_collections_join.pid', intval($settings['storagePid'])), |
||
294 | $queryBuilder->expr()->notIn('tx_dlf_documents.uid', $subQuery), |
||
295 | $queryBuilder->expr()->in('tx_dlf_collections_join.uid', $queryBuilder->createNamedParameter(GeneralUtility::intExplode(',', $settings['collections']), Connection::PARAM_INT_ARRAY)), |
||
296 | $queryBuilder->expr()->eq('tx_dlf_relations_joins.ident', $queryBuilder->createNamedParameter('docs_colls')) |
||
297 | ) |
||
298 | ->execute() |
||
299 | ->fetchColumn(0); |
||
300 | } else { |
||
301 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
302 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
303 | |||
304 | // Include all collections. |
||
305 | $countTitles = $queryBuilder |
||
306 | ->count('tx_dlf_documents.uid') |
||
307 | ->from('tx_dlf_documents') |
||
308 | ->where( |
||
309 | $queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['storagePid'])), |
||
310 | $queryBuilder->expr()->eq('tx_dlf_documents.partof', 0), |
||
311 | Helper::whereExpression('tx_dlf_documents') |
||
312 | ) |
||
313 | ->execute() |
||
314 | ->fetchColumn(0); |
||
315 | |||
316 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
317 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
318 | $subQueryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
319 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
320 | |||
321 | $subQuery = $subQueryBuilder |
||
322 | ->select('tx_dlf_documents.partof') |
||
323 | ->from('tx_dlf_documents') |
||
324 | ->where( |
||
325 | $subQueryBuilder->expr()->neq('tx_dlf_documents.partof', 0) |
||
326 | ) |
||
327 | ->groupBy('tx_dlf_documents.partof') |
||
328 | ->getSQL(); |
||
329 | |||
330 | $countVolumes = $queryBuilder |
||
331 | ->count('tx_dlf_documents.uid') |
||
332 | ->from('tx_dlf_documents') |
||
333 | ->where( |
||
334 | $queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['storagePid'])), |
||
335 | $queryBuilder->expr()->notIn('tx_dlf_documents.uid', $subQuery) |
||
336 | ) |
||
337 | ->execute() |
||
338 | ->fetchColumn(0); |
||
339 | } |
||
340 | |||
341 | return ['titles' => $countTitles, 'volumes' => $countVolumes]; |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Build table of contents |
||
346 | * |
||
347 | * @param int $uid |
||
348 | * @param int $pid |
||
349 | * @param array $settings |
||
350 | * |
||
351 | * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
352 | */ |
||
353 | public function getTableOfContentsFromDb($uid, $pid, $settings) |
||
354 | { |
||
355 | // Build table of contents from database. |
||
356 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
357 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
358 | |||
359 | $excludeOtherWhere = ''; |
||
360 | if ($settings['excludeOther']) { |
||
361 | $excludeOtherWhere = 'tx_dlf_documents.pid=' . intval($settings['storagePid']); |
||
362 | } |
||
363 | // Check if there are any metadata to suggest. |
||
364 | $result = $queryBuilder |
||
365 | ->select( |
||
366 | 'tx_dlf_documents.uid AS uid', |
||
367 | 'tx_dlf_documents.title AS title', |
||
368 | 'tx_dlf_documents.volume AS volume', |
||
369 | 'tx_dlf_documents.mets_label AS mets_label', |
||
370 | 'tx_dlf_documents.mets_orderlabel AS mets_orderlabel', |
||
371 | 'tx_dlf_structures_join.index_name AS type' |
||
372 | ) |
||
373 | ->innerJoin( |
||
374 | 'tx_dlf_documents', |
||
375 | 'tx_dlf_structures', |
||
376 | 'tx_dlf_structures_join', |
||
377 | $queryBuilder->expr()->eq( |
||
378 | 'tx_dlf_structures_join.uid', |
||
379 | 'tx_dlf_documents.structure' |
||
380 | ) |
||
381 | ) |
||
382 | ->from('tx_dlf_documents') |
||
383 | ->where( |
||
384 | $queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($uid)), |
||
385 | $queryBuilder->expr()->eq('tx_dlf_structures_join.pid', intval($pid)), |
||
386 | $excludeOtherWhere |
||
387 | ) |
||
388 | ->addOrderBy('tx_dlf_documents.volume_sorting') |
||
389 | ->addOrderBy('tx_dlf_documents.mets_orderlabel') |
||
390 | ->execute(); |
||
391 | return $result; |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Find one document by given settings and identifier |
||
396 | * |
||
397 | * @param array $settings |
||
398 | * @param array $parameters |
||
399 | * |
||
400 | * @return array The found document object |
||
401 | */ |
||
402 | public function getOaiRecord($settings, $parameters) |
||
403 | { |
||
404 | $where = ''; |
||
405 | |||
406 | if (!$settings['show_userdefined']) { |
||
407 | $where .= 'AND tx_dlf_collections.fe_cruser_id=0 '; |
||
408 | } |
||
409 | |||
410 | $connection = GeneralUtility::makeInstance(ConnectionPool::class) |
||
411 | ->getConnectionForTable('tx_dlf_documents'); |
||
412 | |||
413 | $sql = 'SELECT `tx_dlf_documents`.*, GROUP_CONCAT(DISTINCT `tx_dlf_collections`.`oai_name` ORDER BY `tx_dlf_collections`.`oai_name` SEPARATOR " ") AS `collections` ' . |
||
414 | 'FROM `tx_dlf_documents` ' . |
||
415 | 'INNER JOIN `tx_dlf_relations` ON `tx_dlf_relations`.`uid_local` = `tx_dlf_documents`.`uid` ' . |
||
416 | 'INNER JOIN `tx_dlf_collections` ON `tx_dlf_collections`.`uid` = `tx_dlf_relations`.`uid_foreign` ' . |
||
417 | 'WHERE `tx_dlf_documents`.`record_id` = ? ' . |
||
418 | 'AND `tx_dlf_relations`.`ident`="docs_colls" ' . |
||
419 | $where; |
||
420 | |||
421 | $values = [ |
||
422 | $parameters['identifier'] |
||
423 | ]; |
||
424 | |||
425 | $types = [ |
||
426 | Connection::PARAM_STR |
||
427 | ]; |
||
428 | |||
429 | // Create a prepared statement for the passed SQL query, bind the given params with their binding types and execute the query |
||
430 | $statement = $connection->executeQuery($sql, $values, $types); |
||
431 | |||
432 | return $statement->fetch(); |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * Finds all documents for the given settings |
||
437 | * |
||
438 | * @param array $settings |
||
439 | * @param array $documentsToProcess |
||
440 | * |
||
441 | * @return array The found document objects |
||
442 | */ |
||
443 | public function getOaiDocumentList($settings, $documentsToProcess) |
||
444 | { |
||
445 | $connection = GeneralUtility::makeInstance(ConnectionPool::class) |
||
446 | ->getConnectionForTable('tx_dlf_documents'); |
||
447 | |||
448 | $sql = 'SELECT `tx_dlf_documents`.*, GROUP_CONCAT(DISTINCT `tx_dlf_collections`.`oai_name` ORDER BY `tx_dlf_collections`.`oai_name` SEPARATOR " ") AS `collections` ' . |
||
449 | 'FROM `tx_dlf_documents` ' . |
||
450 | 'INNER JOIN `tx_dlf_relations` ON `tx_dlf_relations`.`uid_local` = `tx_dlf_documents`.`uid` ' . |
||
451 | 'INNER JOIN `tx_dlf_collections` ON `tx_dlf_collections`.`uid` = `tx_dlf_relations`.`uid_foreign` ' . |
||
452 | 'WHERE `tx_dlf_documents`.`uid` IN ( ? ) ' . |
||
453 | 'AND `tx_dlf_relations`.`ident`="docs_colls" ' . |
||
454 | 'AND ' . Helper::whereExpression('tx_dlf_collections') . ' ' . |
||
455 | 'GROUP BY `tx_dlf_documents`.`uid` '; |
||
456 | |||
457 | $values = [ |
||
458 | $documentsToProcess, |
||
459 | ]; |
||
460 | |||
461 | $types = [ |
||
462 | Connection::PARAM_INT_ARRAY, |
||
463 | ]; |
||
464 | |||
465 | // Create a prepared statement for the passed SQL query, bind the given params with their binding types and execute the query |
||
466 | $documents = $connection->executeQuery($sql, $values, $types); |
||
467 | |||
468 | return $documents; |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * Finds all documents with given uids |
||
473 | * |
||
474 | * @param array $uids |
||
475 | * |
||
476 | * @return array |
||
477 | */ |
||
478 | private function findAllByUids($uids) |
||
479 | { |
||
480 | // get all documents from db we are talking about |
||
481 | $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); |
||
482 | $queryBuilder = $connectionPool->getQueryBuilderForTable('tx_dlf_documents'); |
||
483 | // Fetch document info for UIDs in $documentSet from DB |
||
484 | $kitodoDocuments = $queryBuilder |
||
485 | ->select( |
||
486 | 'tx_dlf_documents.uid AS uid', |
||
487 | 'tx_dlf_documents.title AS title', |
||
488 | 'tx_dlf_documents.structure AS structure', |
||
489 | 'tx_dlf_documents.thumbnail AS thumbnail', |
||
490 | 'tx_dlf_documents.volume_sorting AS volumeSorting', |
||
491 | 'tx_dlf_documents.mets_orderlabel AS metsOrderlabel', |
||
492 | 'tx_dlf_documents.partof AS partOf' |
||
493 | ) |
||
494 | ->from('tx_dlf_documents') |
||
495 | ->where( |
||
496 | $queryBuilder->expr()->in('tx_dlf_documents.pid', $this->settings['storagePid']), |
||
497 | $queryBuilder->expr()->in('tx_dlf_documents.uid', $uids) |
||
498 | ) |
||
499 | ->addOrderBy('tx_dlf_documents.volume_sorting', 'asc') |
||
500 | ->addOrderBy('tx_dlf_documents.mets_orderlabel', 'asc') |
||
501 | ->execute(); |
||
502 | |||
503 | $allDocuments = []; |
||
504 | $documentStructures = Helper::getDocumentStructures($this->settings['storagePid']); |
||
505 | // Process documents in a usable array structure |
||
506 | while ($resArray = $kitodoDocuments->fetch()) { |
||
507 | $resArray['structure'] = $documentStructures[$resArray['structure']]; |
||
508 | $allDocuments[$resArray['uid']] = $resArray; |
||
509 | } |
||
510 | |||
511 | return $allDocuments; |
||
512 | } |
||
513 | |||
514 | /** |
||
515 | * Find all documents with given collection from Solr |
||
516 | * |
||
517 | * @param \Kitodo\Dlf\Domain\Model\Collection $collection |
||
518 | * @param array $settings |
||
519 | * @param array $searchParams |
||
520 | * @param \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult $listedMetadata |
||
521 | * @return array |
||
522 | */ |
||
523 | public function findSolrByCollection($collection, $settings, $searchParams, $listedMetadata = null) |
||
724 | } |
||
725 | |||
726 | /** |
||
727 | * Find all listed metadata for given document |
||
728 | * |
||
729 | * @param int $uid the uid of the document |
||
730 | * @param \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult $listedMetadata |
||
731 | * @return array |
||
732 | */ |
||
733 | protected function fetchMetadataFromSolr($uid, $listedMetadata = []) |
||
771 | } |
||
772 | |||
773 | /** |
||
774 | * Processes a search request |
||
775 | * |
||
776 | * @access public |
||
777 | * |
||
778 | * @param array $parameters: Additional search parameters |
||
779 | * @param boolean $enableCache: Enable caching of Solr requests |
||
780 | * |
||
781 | * @return array The Apache Solr Documents that were fetched |
||
782 | */ |
||
783 | protected function searchSolr($parameters = [], $enableCache = true) |
||
877 | } |
||
878 | |||
879 | } |
||
880 |