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 Pauli Järvinen <[email protected]> |
11
|
|
|
* @copyright Morris Jobke 2013, 2014 |
12
|
|
|
* @copyright Pauli Järvinen 2017 - 2023 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace OCA\Music\Db; |
16
|
|
|
|
17
|
|
|
use OCP\IL10N; |
18
|
|
|
use OCP\IURLGenerator; |
19
|
|
|
|
20
|
|
|
use OCA\Music\Utility\Util; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @method ?string getName() |
24
|
|
|
* @method void setName(?string $name) |
25
|
|
|
* @method ?string getMbid() |
26
|
|
|
* @method void setMbid(?string $mbid) |
27
|
|
|
* @method ?string getMbidGroup() |
28
|
|
|
* @method void setMbidGroup(?string $mbidGroup) |
29
|
|
|
* @method ?int getCoverFileId() |
30
|
|
|
* @method void setCoverFileId(?int $coverFileId) |
31
|
|
|
* @method int getAlbumArtistId() |
32
|
|
|
* @method void setAlbumArtistId(int $albumArtistId) |
33
|
|
|
* @method string getHash() |
34
|
|
|
* @method void setHash(string $hash) |
35
|
|
|
* @method ?string getStarred() |
36
|
|
|
* @method void setStarred(?string $timestamp) |
37
|
|
|
* @method ?int getRating() |
38
|
|
|
* @method setRating(?int $rating) |
39
|
|
|
* @method ?string getAlbumArtistName() |
40
|
|
|
*/ |
41
|
|
|
class Album extends Entity { |
42
|
|
|
public $name; |
43
|
|
|
public $mbid; |
44
|
|
|
public $mbidGroup; |
45
|
|
|
public $coverFileId; |
46
|
|
|
public $albumArtistId; |
47
|
|
|
public $hash; |
48
|
|
|
public $starred; |
49
|
|
|
public $rating; |
50
|
|
|
public $albumArtistName; // not from music_albums table but still part of the standard content |
51
|
|
|
public $disk; // deprecated |
52
|
|
|
|
53
|
|
|
// extra fields injected separately by AlbumBusinessLayer |
54
|
|
|
private $years; |
55
|
|
|
private $genres; // *partial* Genre objects, not all properties are set |
56
|
|
|
private $artistIds; |
57
|
|
|
private $numberOfDisks; |
58
|
|
|
|
59
|
|
|
// injected separately when needed |
60
|
|
|
private $tracks; |
61
|
|
|
|
62
|
|
|
public function __construct() { |
63
|
|
|
$this->addType('disk', 'int'); |
64
|
|
|
$this->addType('coverFileId', 'int'); |
65
|
|
|
$this->addType('albumArtistId', 'int'); |
66
|
|
|
$this->addType('rating', 'int'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getYears() : ?array { |
70
|
|
|
return $this->years; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function setYears(?array $years) : void { |
74
|
|
|
$this->years = $years; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getGenres() : ?array { |
78
|
|
|
return $this->genres; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function setGenres(?array $genres) : void { |
82
|
|
|
$this->genres = $genres; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getArtistIds() : ?array { |
86
|
|
|
return $this->artistIds; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function setArtistIds(?array $artistIds) : void { |
90
|
|
|
$this->artistIds = $artistIds; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getNumberOfDisks() : ?int { |
94
|
|
|
return $this->numberOfDisks; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function setNumberOfDisks(?int $count) : void { |
98
|
|
|
$this->numberOfDisks = $count; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return ?Track[] |
103
|
|
|
*/ |
104
|
|
|
public function getTracks() : ?array { |
105
|
|
|
return $this->tracks; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param Track[] $tracks |
110
|
|
|
*/ |
111
|
|
|
public function setTracks(array $tracks) : void { |
112
|
|
|
$this->tracks = $tracks; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Generates URL to album |
117
|
|
|
* @param \OCP\IURLGenerator $urlGenerator |
118
|
|
|
* @return string the url |
119
|
|
|
*/ |
120
|
|
|
public function getUri(IURLGenerator $urlGenerator) : string { |
121
|
|
|
return $urlGenerator->linkToRoute( |
122
|
|
|
'music.shivaApi.album', |
123
|
|
|
['albumId' => $this->id] |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns an array of all artists - each with ID and URL for that artist |
129
|
|
|
* @param \OCP\IURLGenerator $urlGenerator URLGenerator |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function getArtists(IURLGenerator $urlGenerator) : array { |
133
|
|
|
$artists = []; |
134
|
|
|
foreach ($this->artistIds as $artistId) { |
135
|
|
|
$artists[] = [ |
136
|
|
|
'id' => $artistId, |
137
|
|
|
'uri' => $urlGenerator->linkToRoute( |
138
|
|
|
'music.shivaApi.artist', |
139
|
|
|
['artistId' => $artistId] |
140
|
|
|
) |
141
|
|
|
]; |
142
|
|
|
} |
143
|
|
|
return $artists; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Returns the years(s) of the album. |
148
|
|
|
* The album may have zero, one, or multiple years as people may tag tracks of |
149
|
|
|
* colletion albums with their original release dates. The respective formatted |
150
|
|
|
* year ranges could be e.g. null, '2016', and '1995 - 2000'. |
151
|
|
|
* @return string|null |
152
|
|
|
*/ |
153
|
|
|
public function getYearRange() : ?string { |
154
|
|
|
$count = empty($this->years) ? 0 : \count($this->years); |
155
|
|
|
|
156
|
|
|
if ($count == 0) { |
157
|
|
|
return null; |
158
|
|
|
} elseif ($count == 1) { |
159
|
|
|
return (string)$this->years[0]; |
160
|
|
|
} else { |
161
|
|
|
return \min($this->years) . ' - ' . \max($this->years); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* The Shiva and Ampache API definitions require the year to be a single numeric value. |
167
|
|
|
* In case the album has multiple years, output the largest of these in the API. |
168
|
|
|
* @return int|null |
169
|
|
|
*/ |
170
|
|
|
public function yearToAPI() : ?int { |
171
|
|
|
return empty($this->years) ? null : (int)\max($this->years); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Returns the name of the album - if empty it returns the translated |
176
|
|
|
* version of "Unknown album" |
177
|
|
|
* @param IL10N $l10n |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public function getNameString(IL10N $l10n) : string { |
181
|
|
|
return $this->getName() ?: self::unknownNameString($l10n); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Returns the name of the album artist - if empty it returns the translated |
186
|
|
|
* version of "Unknown artist" |
187
|
|
|
* @param IL10N $l10n |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
public function getAlbumArtistNameString(IL10N $l10n) : string { |
191
|
|
|
return $this->getAlbumArtistName() ?: Artist::unknownNameString($l10n); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Return the cover URL to be used in the Shiva API |
196
|
|
|
* @param IURLGenerator $urlGenerator |
197
|
|
|
* @return string|null |
198
|
|
|
*/ |
199
|
|
|
public function coverToAPI(IURLGenerator $urlGenerator) : ?string { |
200
|
|
|
$coverUrl = null; |
201
|
|
|
if ($this->getCoverFileId() > 0) { |
202
|
|
|
$coverUrl = $urlGenerator->linkToRoute('music.api.albumCover', |
203
|
|
|
['albumId' => $this->getId()]); |
204
|
|
|
} |
205
|
|
|
return $coverUrl; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* If the cover image is already cached, the cover is presented with a link containing the image hash. |
210
|
|
|
* Otherwise, the collection contains an URL which triggers the caching and then redirects to the |
211
|
|
|
* URL with image hash. |
212
|
|
|
* @param IURLGenerator $urlGenerator URL Generator |
213
|
|
|
* @param string|null $cachedCoverHash Cached cover image hash if available |
214
|
|
|
* @return string|null |
215
|
|
|
*/ |
216
|
|
|
public function coverToCollection(IURLGenerator $urlGenerator, ?string $cachedCoverHash) : ?string { |
217
|
|
|
if (!empty($cachedCoverHash)) { |
218
|
|
|
return $urlGenerator->linkToRoute('music.api.cachedCover', ['hash' => $cachedCoverHash]); |
219
|
|
|
} elseif ($this->getCoverFileId() > 0) { |
220
|
|
|
return $this->coverToAPI($urlGenerator); |
221
|
|
|
} else { |
222
|
|
|
return null; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Creates object used for collection API (array with name, year, cover URL and ID) |
228
|
|
|
* @param IURLGenerator $urlGenerator URL Generator |
229
|
|
|
* @param IL10N $l10n Localization handler |
230
|
|
|
* @param string|null $cachedCoverHash Cached cover image hash if available |
231
|
|
|
* @param Track[] $tracks Tracks of the album in the "toCollection" format |
232
|
|
|
* @return array collection API object |
233
|
|
|
*/ |
234
|
|
|
public function toCollection(IURLGenerator $urlGenerator, IL10N $l10n, ?string $cachedCoverHash, array $tracks) : array { |
235
|
|
|
return [ |
236
|
|
|
'name' => $this->getNameString($l10n), |
237
|
|
|
'year' => $this->getYearRange(), |
238
|
|
|
'cover' => $this->coverToCollection($urlGenerator, $cachedCoverHash), |
239
|
|
|
'id' => $this->getId(), |
240
|
|
|
'diskCount' => $this->getNumberOfDisks(), |
241
|
|
|
'tracks' => $tracks |
242
|
|
|
]; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Creates object used by the Shiva API (array with name, year, cover URL, ID, slug, URI and artists Array) |
247
|
|
|
* @param IURLGenerator $urlGenerator URL Generator |
248
|
|
|
* @param IL10N $l10n Localization handler |
249
|
|
|
* @return array shiva API object |
250
|
|
|
*/ |
251
|
|
|
public function toAPI(IURLGenerator $urlGenerator, IL10N $l10n) : array { |
252
|
|
|
return [ |
253
|
|
|
'name' => $this->getNameString($l10n), |
254
|
|
|
'year' => $this->yearToAPI(), |
255
|
|
|
'cover' => $this->coverToAPI($urlGenerator), |
256
|
|
|
'id' => $this->getId(), |
257
|
|
|
'uri' => $this->getUri($urlGenerator), |
258
|
|
|
'slug' => $this->slugify('name'), |
259
|
|
|
'albumArtistId' => $this->getAlbumArtistId(), |
260
|
|
|
'artists' => $this->getArtists($urlGenerator) |
261
|
|
|
]; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public static function compareYearAndName(Album $a, Album $b) : int { |
265
|
|
|
$yearResult = \strcmp($a->getYearRange() ?? '', $b->getYearRange() ?? ''); |
266
|
|
|
|
267
|
|
|
return $yearResult ?: Util::stringCaseCompare($a->getName(), $b->getName()); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public static function unknownNameString(IL10N $l10n) : string { |
271
|
|
|
return (string) $l10n->t('Unknown album'); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|