lib_collection.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /**
  3. * ownCloud - media plugin
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2010 Robin Appelman icewind1991@gmail.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. //class for managing a music collection
  23. class OC_MEDIA_COLLECTION{
  24. public static $uid;
  25. private static $artistIdCache=array();
  26. private static $albumIdCache=array();
  27. private static $songIdCache=array();
  28. private static $queries=array();
  29. /**
  30. * get the id of an artist (case-insensitive)
  31. * @param string name
  32. * @return int
  33. */
  34. public static function getArtistId($name){
  35. if(empty($name)){
  36. return 0;
  37. }
  38. $name=strtolower($name);
  39. if(isset(self::$artistIdCache[$name])){
  40. return self::$artistIdCache[$name];
  41. }else{
  42. $query=OCP\DB::prepare("SELECT artist_id FROM *PREFIX*media_artists WHERE lower(artist_name) LIKE ?");
  43. $artists=$query->execute(array($name))->fetchAll();
  44. if(is_array($artists) and isset($artists[0])){
  45. self::$artistIdCache[$name]=$artists[0]['artist_id'];
  46. return $artists[0]['artist_id'];
  47. }else{
  48. return 0;
  49. }
  50. }
  51. }
  52. /**
  53. * get the id of an album (case-insensitive)
  54. * @param string name
  55. * @param int artistId
  56. * @return int
  57. */
  58. public static function getAlbumId($name,$artistId){
  59. if(empty($name)){
  60. return 0;
  61. }
  62. $name=strtolower($name);
  63. if(!isset(self::$albumIdCache[$artistId])){
  64. self::$albumIdCache[$artistId]=array();
  65. }
  66. if(isset(self::$albumIdCache[$artistId][$name])){
  67. return self::$albumIdCache[$artistId][$name];
  68. }else{
  69. $query=OCP\DB::prepare("SELECT album_id FROM *PREFIX*media_albums WHERE lower(album_name) LIKE ? AND album_artist=?");
  70. $albums=$query->execute(array($name,$artistId))->fetchAll();
  71. if(is_array($albums) and isset($albums[0])){
  72. self::$albumIdCache[$artistId][$name]=$albums[0]['album_id'];
  73. return $albums[0]['album_id'];
  74. }else{
  75. return 0;
  76. }
  77. }
  78. }
  79. /**
  80. * get the id of an song (case-insensitive)
  81. * @param string name
  82. * @param int artistId
  83. * @param int albumId
  84. * @return int
  85. */
  86. public static function getSongId($name,$artistId,$albumId){
  87. if(empty($name)){
  88. return 0;
  89. }
  90. $name=strtolower($name);
  91. if(!isset(self::$albumIdCache[$artistId])){
  92. self::$albumIdCache[$artistId]=array();
  93. }
  94. if(!isset(self::$albumIdCache[$artistId][$albumId])){
  95. self::$albumIdCache[$artistId][$albumId]=array();
  96. }
  97. if(isset(self::$albumIdCache[$artistId][$albumId][$name])){
  98. return self::$albumIdCache[$artistId][$albumId][$name];
  99. }else{
  100. $uid=$_SESSION['user_id'];
  101. $query=OCP\DB::prepare("SELECT song_id FROM *PREFIX*media_songs WHERE song_user=? AND song_name LIKE ? AND song_artist=? AND song_album=?");
  102. $songs=$query->execute(array($uid,$name,$artistId,$albumId))->fetchAll();
  103. if(is_array($songs) and isset($songs[0])){
  104. self::$albumIdCache[$artistId][$albumId][$name]=$songs[0]['song_id'];
  105. return $songs[0]['song_id'];
  106. }else{
  107. return 0;
  108. }
  109. }
  110. }
  111. /**
  112. * Get the list of artists that (optionally) match a search string
  113. * @param string search optional
  114. * @return array the list of artists found
  115. */
  116. static public function getArtists($search='%',$exact=false){
  117. $uid=self::$uid;
  118. if(empty($uid)){
  119. $uid=self::$uid=$_SESSION['user_id'];
  120. }
  121. if(!$exact and $search!='%'){
  122. $search="%$search%";
  123. }elseif($search==''){
  124. $search='%';
  125. }
  126. $query=OCP\DB::prepare("SELECT DISTINCT artist_name, artist_id FROM *PREFIX*media_artists
  127. INNER JOIN *PREFIX*media_songs ON artist_id=song_artist WHERE artist_name LIKE ? AND song_user=? ORDER BY artist_name");
  128. $result=$query->execute(array($search,self::$uid));
  129. return $result->fetchAll();
  130. }
  131. /**
  132. * Add an artists to the database
  133. * @param string name
  134. * @return integer the artist_id of the added artist
  135. */
  136. static public function addArtist($name){
  137. $name=trim($name);
  138. if($name==''){
  139. return 0;
  140. }
  141. //check if the artist is already in the database
  142. $artistId=self::getArtistId($name);
  143. if($artistId!=0){
  144. return $artistId;
  145. }else{
  146. $query=OCP\DB::prepare("INSERT INTO `*PREFIX*media_artists` (`artist_name`) VALUES (?)");
  147. $result=$query->execute(array($name));
  148. return self::getArtistId($name);;
  149. }
  150. }
  151. /**
  152. * Get the list of albums that (optionally) match an artist and/or search string
  153. * @param integer artist optional
  154. * @param string search optional
  155. * @return array the list of albums found
  156. */
  157. static public function getAlbums($artist=0,$search='%',$exact=false){
  158. $uid=self::$uid;
  159. if(empty($uid)){
  160. $uid=self::$uid=$_SESSION['user_id'];
  161. }
  162. $cmd="SELECT DISTINCT album_name, album_artist, album_id
  163. FROM *PREFIX*media_albums INNER JOIN *PREFIX*media_songs ON album_id=song_album WHERE song_user=? ";
  164. $params=array(self::$uid);
  165. if($artist!=0){
  166. $cmd.="AND album_artist = ? ";
  167. array_push($params,$artist);
  168. }
  169. if($search!='%'){
  170. $cmd.="AND album_name LIKE ? ";
  171. if(!$exact){
  172. $search="%$search%";
  173. }
  174. array_push($params,$search);
  175. }
  176. $cmd.=' ORDER BY album_name';
  177. $query=OCP\DB::prepare($cmd);
  178. return $query->execute($params)->fetchAll();
  179. }
  180. /**
  181. * Add an album to the database
  182. * @param string name
  183. * @param integer artist
  184. * @return integer the album_id of the added artist
  185. */
  186. static public function addAlbum($name,$artist){
  187. $name=trim($name);
  188. if($name==''){
  189. return 0;
  190. }
  191. //check if the album is already in the database
  192. $albumId=self::getAlbumId($name,$artist);
  193. if($albumId!=0){
  194. return $albumId;
  195. }else{
  196. $query=OCP\DB::prepare("INSERT INTO `*PREFIX*media_albums` (`album_name` ,`album_artist`) VALUES ( ?, ?)");
  197. $query->execute(array($name,$artist));
  198. return self::getAlbumId($name,$artist);
  199. }
  200. }
  201. /**
  202. * Get the list of songs that (optionally) match an artist and/or album and/or search string
  203. * @param integer artist optional
  204. * @param integer album optional
  205. * @param string search optional
  206. * @return array the list of songs found
  207. */
  208. static public function getSongs($artist=0,$album=0,$search='',$exact=false){
  209. $uid=self::$uid;
  210. if(empty($uid)){
  211. $uid=self::$uid=$_SESSION['user_id'];
  212. }
  213. $params=array($uid);
  214. if($artist!=0){
  215. $artistString="AND song_artist = ?";
  216. array_push($params,$artist);
  217. }else{
  218. $artistString='';
  219. }
  220. if($album!=0){
  221. $albumString="AND song_album = ?";
  222. array_push($params,$album);
  223. }else{
  224. $albumString='';
  225. }
  226. if($search){
  227. if(!$exact){
  228. $search="%$search%";
  229. }
  230. $searchString ="AND song_name LIKE ?";
  231. array_push($params,$search);
  232. }else{
  233. $searchString='';
  234. }
  235. $query=OCP\DB::prepare("SELECT * FROM *PREFIX*media_songs WHERE song_user=? $artistString $albumString $searchString ORDER BY song_track, song_name, song_path");
  236. return $query->execute($params)->fetchAll();
  237. }
  238. /**
  239. * Add an song to the database
  240. * @param string name
  241. * @param string path
  242. * @param integer artist
  243. * @param integer album
  244. * @return integer the song_id of the added artist
  245. */
  246. static public function addSong($name,$path,$artist,$album,$length,$track,$size){
  247. $name=trim($name);
  248. $path=trim($path);
  249. if($name=='' or $path==''){
  250. return 0;
  251. }
  252. $uid=OCP\USER::getUser();
  253. //check if the song is already in the database
  254. $songId=self::getSongId($name,$artist,$album);
  255. if($songId!=0){
  256. $songInfo=self::getSong($songId);
  257. self::moveSong($songInfo['song_path'],$path);
  258. return $songId;
  259. }else{
  260. if(!isset(self::$queries['addsong'])){
  261. $query=OCP\DB::prepare("INSERT INTO `*PREFIX*media_songs` (`song_name` ,`song_artist` ,`song_album` ,`song_path` ,`song_user`,`song_length`,`song_track`,`song_size`,`song_playcount`,`song_lastplayed`)
  262. VALUES (?, ?, ?, ?,?,?,?,?,0,0)");
  263. self::$queries['addsong']=$query;
  264. }else{
  265. $query=self::$queries['addsong'];
  266. }
  267. $query->execute(array($name,$artist,$album,$path,$uid,$length,$track,$size));
  268. $songId=OCP\DB::insertid('*PREFIX*media_songs');
  269. // self::setLastUpdated();
  270. return self::getSongId($name,$artist,$album);
  271. }
  272. }
  273. public static function getSongCount(){
  274. $query=OCP\DB::prepare("SELECT COUNT(song_id) AS count FROM *PREFIX*media_songs");
  275. $result=$query->execute()->fetchAll();
  276. return $result[0]['count'];
  277. }
  278. public static function getArtistCount(){
  279. $query=OCP\DB::prepare("SELECT COUNT(artist_id) AS count FROM *PREFIX*media_artists");
  280. $result=$query->execute()->fetchAll();
  281. return $result[0]['count'];
  282. }
  283. public static function getAlbumCount(){
  284. $query=OCP\DB::prepare("SELECT COUNT(album_id) AS count FROM *PREFIX*media_albums");
  285. $result=$query->execute()->fetchAll();
  286. return $result[0]['count'];
  287. }
  288. public static function getArtistName($artistId){
  289. $query=OCP\DB::prepare("SELECT artist_name FROM *PREFIX*media_artists WHERE artist_id=?");
  290. $artist=$query->execute(array($artistId))->fetchAll();
  291. if(count($artist)>0){
  292. return $artist[0]['artist_name'];
  293. }else{
  294. return '';
  295. }
  296. }
  297. public static function getAlbumName($albumId){
  298. $query=OCP\DB::prepare("SELECT album_name FROM *PREFIX*media_albums WHERE album_id=?");
  299. $album=$query->execute(array($albumId))->fetchAll();
  300. if(count($album)>0){
  301. return $album[0]['album_name'];
  302. }else{
  303. return '';
  304. }
  305. }
  306. public static function getSong($id){
  307. $query=OCP\DB::prepare("SELECT * FROM *PREFIX*media_songs WHERE song_id=?");
  308. $song=$query->execute(array($id))->fetchAll();
  309. if(count($song)>0){
  310. return $song[0];
  311. }else{
  312. return '';
  313. }
  314. }
  315. /**
  316. * get the number of songs in a directory
  317. * @param string $path
  318. */
  319. public static function getSongCountByPath($path){
  320. $query=OCP\DB::prepare("SELECT COUNT(song_id) AS count FROM *PREFIX*media_songs WHERE song_path LIKE ?");
  321. $result=$query->execute(array("$path%"))->fetchAll();
  322. return $result[0]['count'];
  323. }
  324. /**
  325. * remove a song from the database by path
  326. * @param string $path the path of the song
  327. *
  328. * if a path of a folder is passed, all songs stored in the folder will be removed from the database
  329. */
  330. public static function deleteSongByPath($path){
  331. $query=OCP\DB::prepare("DELETE FROM *PREFIX*media_songs WHERE song_path LIKE ?");
  332. $query->execute(array("$path%"));
  333. }
  334. /**
  335. * increase the play count of a song
  336. * @param int songId
  337. */
  338. public static function registerPlay($songId){
  339. $now=time();
  340. $query=OCP\DB::prepare('UPDATE *PREFIX*media_songs SET song_playcount=song_playcount+1, song_lastplayed=? WHERE song_id=? AND song_lastplayed<?');
  341. $query->execute(array($now,$songId,$now-60));
  342. }
  343. /**
  344. * get the id of the song by path
  345. * @param string $path
  346. * @return int
  347. */
  348. public static function getSongByPath($path){
  349. $query=OCP\DB::prepare("SELECT song_id FROM *PREFIX*media_songs WHERE song_path = ?");
  350. $result=$query->execute(array($path));
  351. if($row=$result->fetchRow()){
  352. return $row['song_id'];
  353. }else{
  354. return 0;
  355. }
  356. }
  357. /**
  358. * set the path of a song
  359. * @param string $oldPath
  360. * @param string $newPath
  361. */
  362. public static function moveSong($oldPath,$newPath){
  363. $query=OCP\DB::prepare("UPDATE *PREFIX*media_songs SET song_path = ? WHERE song_path = ?");
  364. $query->execute(array($newPath,$oldPath));
  365. }
  366. }
  367. ?>