Issues (40)

lib/Search/Provider.php (1 issue)

1
<?php declare(strict_types=1);
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Morris Jobke <[email protected]>
10
 * @author Leizh <[email protected]>
11
 * @author Pauli Järvinen <[email protected]>
12
 * @copyright Morris Jobke 2013, 2014
13
 * @copyright Leizh 2014
14
 * @copyright Pauli Järvinen 2018 - 2025
15
 */
16
17
namespace OCA\Music\Search;
18
19
use OCA\Music\AppFramework\Core\Logger;
20
use OCA\Music\AppInfo\Application;
21
use OCA\Music\Db\AlbumMapper;
22
use OCA\Music\Db\ArtistMapper;
23
use OCA\Music\Db\Entity;
24
use OCA\Music\Db\MatchMode;
25
use OCA\Music\Db\TrackMapper;
26
27
use OCP\IL10N;
28
use OCP\IURLGenerator;
29
30
class Provider extends \OCP\Search\Provider {
0 ignored issues
show
Deprecated Code introduced by
The class OCP\Search\Provider has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

30
class Provider extends /** @scrutinizer ignore-deprecated */ \OCP\Search\Provider {
Loading history...
31
32
	/* Limit the maximum number of matches because the Provider API is limited and does
33
	 * not support pagination. The core paginates the results to 30 item pages, but it
34
	 * obtains all the items from the Providers again on creation of each page.
35
	 * If there were thousands of matches, we would end up doing lot of unnecessary work.
36
	 */
37
	const MAX_RESULTS_PER_TYPE = 100;
38
39
	private ArtistMapper $artistMapper;
40
	private AlbumMapper $albumMapper;
41
	private TrackMapper $trackMapper;
42
	private IURLGenerator $urlGenerator;
43
	private string $userId;
44
	private IL10N $l10n;
45
	private array $resultTypeNames;
46
	private array $resultTypePaths;
47
	private Logger $logger;
48
49
	public function __construct() {
50
		$app = \OC::$server->query(Application::class);
51
		$c = $app->getContainer();
52
53
		$this->artistMapper = $c->query(ArtistMapper::class);
54
		$this->albumMapper = $c->query(AlbumMapper::class);
55
		$this->trackMapper = $c->query(TrackMapper::class);
56
		$this->urlGenerator = $c->query(IURLGenerator::class);
57
		$this->userId = $c->query('userId');
58
		$this->l10n = $c->query(IL10N::class);
59
		$this->logger = $c->query(Logger::class);
60
61
		$this->resultTypeNames = [
62
			'music_artist' => $this->l10n->t('Artist'),
63
			'music_album' => $this->l10n->t('Album'),
64
			'music_track' => $this->l10n->t('Track')
65
		];
66
67
		$basePath = $this->urlGenerator->linkToRoute('music.page.index');
68
		$this->resultTypePaths = [
69
			'music_artist' => $basePath . "#/artist/",
70
			'music_album' => $basePath . "#/album/",
71
			'music_track' => $basePath . "#/track/"
72
		];
73
	}
74
75
	private function createResult(Entity $entity, string $type) : Result {
76
		$link = $this->resultTypePaths[$type] . $entity->id;
77
		$titlePrefix = $this->l10n->t('Music') . ' - ' . $this->resultTypeNames[$type] . ': ';
78
		$title = $entity->getNameString($this->l10n);
79
		return new Result($entity->id, $titlePrefix . $title, $link, $type);
80
	}
81
82
	public function search($query) {
83
		$results = [];
84
85
		$artists = $this->artistMapper->findAllByName($query, $this->userId, MatchMode::Substring, self::MAX_RESULTS_PER_TYPE);
86
		foreach ($artists as $artist) {
87
			$results[] = $this->createResult($artist, 'music_artist');
88
		}
89
90
		$albums = $this->albumMapper->findAllByName($query, $this->userId, MatchMode::Substring, self::MAX_RESULTS_PER_TYPE);
91
		foreach ($albums as $album) {
92
			$results[] = $this->createResult($album, 'music_album');
93
		}
94
95
		$tracks = $this->trackMapper->findAllByName($query, $this->userId, MatchMode::Substring, self::MAX_RESULTS_PER_TYPE);
96
		foreach ($tracks as $track) {
97
			$results[] = $this->createResult($track, 'music_track');
98
		}
99
100
		return $results;
101
	}
102
}
103