/** * build_cache * Build a cache based on the array of ids passed, saves lots of little queries */ public static function build_cache($ids = array()) { if (!is_array($ids) or !count($ids)) { return false; } $idlist = '(' . implode(',', $ids) . ')'; $sql = "SELECT * FROM `video` WHERE `video`.`id` IN {$idlist}"; $db_results = Dba::read($sql); while ($row = Dba::fetch_assoc($db_results)) { parent::add_to_cache('video', $row['id'], $row); } }
/** * _get_info */ private function _get_info() { $id = $this->id; if (parent::is_cached('song_preview', $id)) { return parent::get_from_cache('song_preview', $id); } $sql = 'SELECT `id`, `file`, `album_mbid`, `artist`, `artist_mbid`, `title`, `disk`, `track`, `mbid` ' . 'FROM `song_preview` WHERE `id` = ?'; $db_results = Dba::read($sql, array($id)); $results = Dba::fetch_assoc($db_results); if (!empty($results['id'])) { if (empty($results['artist_mbid'])) { $sql = 'SELECT `mbid` FROM `artist` WHERE `id` = ?'; $db_results = Dba::read($sql, array($results['artist'])); if ($artist_res = Dba::fetch_assoc($db_results)) { $results['artist_mbid'] = $artist_res['mbid']; } } parent::add_to_cache('song_preview', $id, $results); return $results; } return false; }
/** * get_catalogs * This returns the catalogs as an array of ids that this user is allowed to access */ public function get_catalogs() { if (parent::is_cached('user_catalog', $this->id)) { return parent::get_from_cache('user_catalog', $this->id); } $sql = "SELECT * FROM `user_catalog` WHERE `user` = ?"; $db_results = Dba::read($sql, array($this->id)); $catalogs = array(); while ($row = Dba::fetch_assoc($db_results)) { $catalogs[] = $row['catalog']; } parent::add_to_cache('user_catalog', $this->id, $catalogs); return $catalogs; }
$this->pass = $pass; $this->connected = false; } function connect() { try { $this->db_connect = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db, $this->user, $this->pass); $this->connected = true; return true; } catch (Exception $e) { $this->connected = false; return false; } } function isConnected() { return $this->connected; } function get_db_connect() { return $this->db_connect; } } $host = "localhost"; $user = "******"; $pass = ""; $db = "museo"; $db_obj = new database_object($host, $db, $user, $pass); if (!$db_obj->isConnected()) { $db_obj->connect(); }
/** * Add a new wanted release. * @param string $mbid * @param int $artist * @param string $artist_mbid * @param string $name * @param int $year */ public static function add_wanted($mbid, $artist, $artist_mbid, $name, $year) { $sql = "INSERT INTO `wanted` (`user`, `artist`, `artist_mbid`, `mbid`, `name`, `year`, `date`, `accepted`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; $accept = $GLOBALS['user']->has_access('75') ? true : AmpConfig::get('wanted_auto_accept'); $params = array($GLOBALS['user']->id, $artist, $artist_mbid, $mbid, $name, $year, time(), '0'); Dba::write($sql, $params); if ($accept) { $wantedid = Dba::insert_id(); $wanted = new Wanted($wantedid); $wanted->accept(); database_object::remove_from_cache('wanted', $wantedid); } }
/** * _get_extra info * This returns the extra information for the artist, this means totals etc * @param int $catalog * @return array */ private function _get_extra_info($catalog = 0, $limit_threshold = '') { // Try to find it in the cache and save ourselves the trouble if (parent::is_cached('artist_extra', $this->id)) { $row = parent::get_from_cache('artist_extra', $this->id); } else { $params = array($this->id); // Calculation $sql = "SELECT COUNT(DISTINCT `song`.`id`) AS `song_count`, COUNT(DISTINCT `song`.`album`) AS `album_count`, SUM(`song`.`time`) AS `time` FROM `song` LEFT JOIN `catalog` ON `catalog`.`id` = `song`.`catalog` "; $sqlw = "WHERE `song`.`artist` = ? "; if ($catalog) { $params[] = $catalog; $sqlw .= "AND (`song`.`catalog` = ?) "; } if (AmpConfig::get('catalog_disable')) { $sqlw .= " AND `catalog`.`enabled` = '1' "; } $sql .= $sqlw . "GROUP BY `song`.`artist`"; $db_results = Dba::read($sql, $params); $row = Dba::fetch_assoc($db_results); // Get associated information from first song only $sql = "SELECT `song`.`artist`, `song`.`catalog` as `catalog_id` FROM `song` LEFT JOIN `catalog` ON `catalog`.`id` = `song`.`catalog` "; $sql .= $sqlw . "LIMIT 1"; $db_results = Dba::read($sql, $params); $row = array_merge($row, Dba::fetch_assoc($db_results)); if (AmpConfig::get('show_played_times')) { $row['object_cnt'] = Stats::get_object_count('artist', $row['artist'], $limit_threshold); } parent::add_to_cache('artist_extra', $row['artist'], $row); } /* Set Object Vars */ $this->songs = $row['song_count']; $this->albums = $row['album_count']; $this->time = $row['time']; $this->catalog_id = $row['catalog_id']; return $row; }
/** * _auto_init * Load in the cache settings once so we can avoid function calls */ public static function _auto_init() { self::$_enabled = AmpConfig::get('memory_cache'); }
/** * id_from_name * This takes a name and returns the id */ public static function id_from_name($name) { $name = Dba::escape($name); if (parent::is_cached('id_from_name', $name)) { return parent::get_from_cache('id_from_name', $name); } $sql = "SELECT `id` FROM `preference` WHERE `name`='{$name}'"; $db_results = Dba::read($sql); $row = Dba::fetch_assoc($db_results); parent::add_to_cache('id_from_name', $name, $row['id']); return $row['id']; }
/** * set_flag * This function sets the user flag for the current object. * If no userid is passed in, we use the currently logged in user. */ public function set_flag($flagged, $user_id = null) { if (is_null($user_id)) { $user_id = $GLOBALS['user']->id; } $user_id = intval($user_id); debug_event('Userflag', "Setting userflag for {$this->type} {$this->id} to {$flagged}", 5); if (!$flagged) { $sql = "DELETE FROM `user_flag` WHERE " . "`object_id` = ? AND " . "`object_type` = ? AND " . "`user` = ?"; $params = array($this->id, $this->type, $user_id); } else { $sql = "REPLACE INTO `user_flag` " . "(`object_id`, `object_type`, `user`, `date`) " . "VALUES (?, ?, ?, ?)"; $params = array($this->id, $this->type, $user_id, time()); } Dba::write($sql, $params); parent::add_to_cache('userflag_' . $this->type . '_user' . $user_id, $this->id, $flagged); return true; }
/** * _get_extra info * This returns the extra information for the artist, this means totals etc */ private function _get_extra_info($catalog = FALSE) { // Try to find it in the cache and save ourselves the trouble if (parent::is_cached('artist_extra', $this->id)) { $row = parent::get_from_cache('artist_extra', $this->id); } else { $uid = Dba::escape($this->id); $sql = "SELECT `song`.`artist`,COUNT(`song`.`id`) AS `song_count`, COUNT(DISTINCT `song`.`album`) AS `album_count`, SUM(`song`.`time`) AS `time` FROM `song` LEFT JOIN `catalog` ON `catalog`.`id` = `song`.`catalog` " . "WHERE `song`.`artist`='{$uid}' "; if ($catalog) { $sql .= "AND (`song`.`catalog` = '{$catalog}') "; } if (AmpConfig::get('catalog_disable')) { $sql .= " AND `catalog`.`enabled` = '1'"; } $sql .= "GROUP BY `song`.`artist`"; $db_results = Dba::read($sql); $row = Dba::fetch_assoc($db_results); if (AmpConfig::get('show_played_times')) { $row['object_cnt'] = Stats::get_object_count('artist', $row['artist']); } parent::add_to_cache('artist_extra', $row['artist'], $row); } /* Set Object Vars */ $this->songs = $row['song_count']; $this->albums = $row['album_count']; $this->time = $row['time']; return $row; }
public static function get_missing_artist($mbid) { $wartist = array(); if (parent::is_cached('missing_artist', $mbid)) { $wartist = parent::get_from_cache('missing_artist', $mbid); } else { $mb = new MusicBrainz(new RequestsMbClient()); $wartist['mbid'] = $mbid; $wartist['name'] = T_('Unknown Artist'); try { $martist = $mb->lookup('artist', $mbid); } catch (Exception $e) { return $wartist; } $wartist['name'] = $martist->name; parent::add_to_cache('missing_artist', $mbid, $wartist); } $wartist['link'] = "<a href=\"" . AmpConfig::get('web_path') . "/artists.php?action=show_missing&mbid=" . $wartist['mbid'] . "\" title=\"" . $wartist['name'] . "\">" . $wartist['name'] . "</a>"; return $wartist; }
/** * _get_extra_info * This pulls the extra information from our tables, this is a 3 table join, which is why we don't normally * do it */ private function _get_extra_info() { if (parent::is_cached('album_extra', $this->id)) { return parent::get_from_cache('album_extra', $this->id); } $sql = "SELECT " . "COUNT(DISTINCT(`song`.`artist`)) AS `artist_count`, " . "COUNT(`song`.`id`) AS `song_count`, " . "SUM(`song`.`time`) as `total_duration`," . "`song`.`catalog` as `catalog_id`," . "`artist`.`name` AS `artist_name`, " . "`artist`.`prefix` AS `artist_prefix`, " . "`artist`.`id` AS `artist_id` " . "FROM `song` INNER JOIN `artist` " . "ON `artist`.`id`=`song`.`artist` "; if (AmpConfig::get('catalog_disable')) { $sql .= "LEFT JOIN `catalog` ON `catalog`.`id` = `song`.`catalog` "; } $suite_array = array(); if ($this->allow_group_disks) { $suite_array = $this->album_suite; } if (!count($suite_array)) { $suite_array[] = $this->id; } $idlist = '(' . implode(',', $suite_array) . ')'; $sql .= "WHERE `song`.`album` IN {$idlist} "; if (AmpConfig::get('catalog_disable')) { $sql .= "AND `catalog`.`enabled` = '1' "; } if (!count($this->album_suite)) { $sql .= "GROUP BY `song`.`album`"; } else { $sql .= "GROUP BY `song`.`artist`"; } $db_results = Dba::read($sql); $results = Dba::fetch_assoc($db_results); $art = new Art($this->id, 'album'); $art->get_db(); $results['has_art'] = make_bool($art->raw); $results['has_thumb'] = make_bool($art->thumb); if (AmpConfig::get('show_played_times')) { $results['object_cnt'] = Stats::get_object_count('album', $this->id); } parent::add_to_cache('album_extra', $this->id, $results); return $results; }
/** * _get_extra info * This returns the extra information for the podcast, this means totals etc */ private function _get_extra_info() { // Try to find it in the cache and save ourselves the trouble if (parent::is_cached('podcast_extra', $this->id)) { $row = parent::get_from_cache('podcast_extra', $this->id); } else { $sql = "SELECT COUNT(`podcast_episode`.`id`) AS `episode_count` FROM `podcast_episode` " . "WHERE `podcast_episode`.`podcast` = ?"; $db_results = Dba::read($sql, array($this->id)); $row = Dba::fetch_assoc($db_results); parent::add_to_cache('podcast_extra', $this->id, $row); } /* Set Object Vars */ $this->episodes = $row['episode_count']; return $row; }
/** * set_flag * This function sets the user flag for the current object. * If no userid is passed in, we use the currently logged in user. */ public function set_flag($flagged, $user_id = null) { if ($user_id === null) { $user_id = $GLOBALS['user']->id; } $user_id = intval($user_id); debug_event('Userflag', "Setting userflag for {$this->type} {$this->id} to {$flagged}", 5); if (!$flagged) { $sql = "DELETE FROM `user_flag` WHERE " . "`object_id` = ? AND " . "`object_type` = ? AND " . "`user` = ?"; $params = array($this->id, $this->type, $user_id); } else { $sql = "REPLACE INTO `user_flag` " . "(`object_id`, `object_type`, `user`, `date`) " . "VALUES (?, ?, ?, ?)"; $params = array($this->id, $this->type, $user_id, time()); Useractivity::post_activity($user_id, 'userflag', $this->type, $this->id); } Dba::write($sql, $params); parent::add_to_cache('userflag_' . $this->type . '_user' . $user_id, $this->id, $flagged); // Forward flag to last.fm and Libre.fm (song only) if ($this->type == 'song') { $user = new User($user_id); $song = new Song($this->id); if ($song) { $song->format(); foreach (Plugin::get_plugins('save_mediaplay') as $plugin_name) { try { $plugin = new Plugin($plugin_name); if ($plugin->load($user)) { $plugin->_plugin->set_flag($song, $flagged); } } catch (Exception $e) { debug_event('user.class.php', 'Stats plugin error: ' . $e->getMessage(), '1'); } } } } return true; }
/** * set_rating * This function sets the rating for the current object. * If no userid is passed in, we use the currently logged in user. */ public function set_rating($rating, $user_id = null) { if (is_null($user_id)) { $user_id = $GLOBALS['user']->id; } $user_id = intval($user_id); debug_event('Rating', "Setting rating for {$this->type} {$this->id} to {$rating}", 5); // If score is -1, then remove rating if ($rating == '-1') { $sql = "DELETE FROM `rating` WHERE " . "`object_id` = ? AND " . "`object_type` = ? AND " . "`user` = ?"; $params = array($this->id, $this->type, $user_id); } else { $sql = "REPLACE INTO `rating` " . "(`object_id`, `object_type`, `rating`, `user`) " . "VALUES (?, ?, ?, ?)"; $params = array($this->id, $this->type, $rating, $user_id); } Dba::write($sql, $params); parent::add_to_cache('rating_' . $this->type . '_user' . $user_id, $this->id, $rating); foreach (Plugin::get_plugins('save_rating') as $plugin_name) { $plugin = new Plugin($plugin_name); if ($plugin->load($GLOBALS['user'])) { $plugin->_plugin->save_rating($this, $rating); } } return true; }
/** * get_tags * This is a non-object non type dependent function that just returns tags * we've got, it can take filters (this is used by the tag cloud) */ public static function get_tags($limit = 0) { //debug_event('tag.class.php', 'Get tags list called...', '5'); if (parent::is_cached('tags_list', 'no_name')) { //debug_event('tag.class.php', 'Tags list found into cache memory!', '5'); return parent::get_from_cache('tags_list', 'no_name'); } $results = array(); $sql = "SELECT `tag_map`.`tag_id`, `tag`.`name`, COUNT(`tag_map`.`object_id`) AS `count` " . "FROM `tag_map` " . "LEFT JOIN `tag` ON `tag`.`id`=`tag_map`.`tag_id` " . "GROUP BY `tag`.`name` ORDER BY `count` DESC "; if ($limit > 0) { $sql .= " LIMIT {$limit}"; } $db_results = Dba::read($sql); while ($row = Dba::fetch_assoc($db_results)) { $results[$row['tag_id']] = array('id' => $row['tag_id'], 'name' => $row['name'], 'count' => $row['count']); } parent::add_to_cache('tags_list', 'no_name', $results); return $results; }
/** * _get_extra info * This returns the extra information for the tv show, this means totals etc */ private function _get_extra_info() { // Try to find it in the cache and save ourselves the trouble if (parent::is_cached('tvshow_extra', $this->id)) { $row = parent::get_from_cache('tvshow_extra', $this->id); } else { $sql = "SELECT COUNT(`tvshow_episode`.`id`) AS `episode_count`, `video`.`catalog` as `catalog_id` FROM `tvshow_season` " . "LEFT JOIN `tvshow_episode` ON `tvshow_episode`.`season` = `tvshow_season`.`id` " . "LEFT JOIN `video` ON `video`.`id` = `tvshow_episode`.`id` " . "WHERE `tvshow_season`.`tvshow` = ?"; $db_results = Dba::read($sql, array($this->id)); $row = Dba::fetch_assoc($db_results); $sql = "SELECT COUNT(`tvshow_season`.`id`) AS `season_count` FROM `tvshow_season` " . "WHERE `tvshow_season`.`tvshow` = ?"; $db_results = Dba::read($sql, array($this->id)); $row2 = Dba::fetch_assoc($db_results); $row['season_count'] = $row2['season_count']; parent::add_to_cache('tvshow_extra', $this->id, $row); } /* Set Object Vars */ $this->episodes = $row['episode_count']; $this->seasons = $row['season_count']; $this->catalog_id = $row['catalog_id']; return $row; }
/** * url * This returns the constructed URL for the art in question */ public static function url($uid, $type, $sid = false) { $sid = $sid ? scrub_out($sid) : scrub_out(session_id()); $type = self::validate_type($type); $key = $type . $uid; if (parent::is_cached('art', $key . '275x275') && AmpConfig::get('resize_images')) { $row = parent::get_from_cache('art', $key . '275x275'); $mime = $row['mime']; } if (parent::is_cached('art', $key . 'original')) { $row = parent::get_from_cache('art', $key . 'original'); $thumb_mime = $row['mime']; } if (!isset($mime) && !isset($thumb_mime)) { $sql = "SELECT `object_type`, `object_id`, `mime`, `size` FROM `image` WHERE `object_type` = ? AND `object_id` = ?"; $db_results = Dba::read($sql, array($type, $uid)); while ($row = Dba::fetch_assoc($db_results)) { parent::add_to_cache('art', $key . $row['size'], $row); if ($row['size'] == 'original') { $mime = $row['mime']; } else { if ($row['size'] == '275x275' && AmpConfig::get('resize_images')) { $thumb_mime = $row['mime']; } } } } $mime = isset($thumb_mime) ? $thumb_mime : (isset($mime) ? $mime : null); $extension = self::extension($mime); $name = 'art.' . $extension; $url = AmpConfig::get('web_path') . '/image.php?id=' . scrub_out($uid) . '&object_type=' . scrub_out($type) . '&auth=' . $sid . '&name=' . $name; return $url; }
/** * url * This returns the constructed URL for the art in question * @param int $uid * @param string $type * @param string $sid * @param int|null $thumb * @return string */ public static function url($uid, $type, $sid = null, $thumb = null) { if (!Core::is_library_item($type)) { return null; } if (AmpConfig::get('use_auth') && AmpConfig::get('require_session')) { $sid = $sid ? scrub_out($sid) : scrub_out(session_id()); if ($sid == null) { $sid = Session::create(array('type' => 'api')); } } $key = $type . $uid; if (parent::is_cached('art', $key . '275x275') && AmpConfig::get('resize_images')) { $row = parent::get_from_cache('art', $key . '275x275'); $mime = $row['mime']; } if (parent::is_cached('art', $key . 'original')) { $row = parent::get_from_cache('art', $key . 'original'); $thumb_mime = $row['mime']; } if (!isset($mime) && !isset($thumb_mime)) { $sql = "SELECT `object_type`, `object_id`, `mime`, `size` FROM `image` WHERE `object_type` = ? AND `object_id` = ?"; $db_results = Dba::read($sql, array($type, $uid)); while ($row = Dba::fetch_assoc($db_results)) { parent::add_to_cache('art', $key . $row['size'], $row); if ($row['size'] == 'original') { $mime = $row['mime']; } else { if ($row['size'] == '275x275' && AmpConfig::get('resize_images')) { $thumb_mime = $row['mime']; } } } } $mime = isset($thumb_mime) ? $thumb_mime : (isset($mime) ? $mime : null); $extension = self::extension($mime); if (AmpConfig::get('stream_beautiful_url')) { if (empty($extension)) { $extension = 'jpg'; } $url = AmpConfig::get('web_path') . '/play/art/' . $sid . '/' . scrub_out($type) . '/' . scrub_out($uid) . '/thumb'; if ($thumb) { $url .= $thumb; } $url .= '.' . $extension; } else { $url = AmpConfig::get('web_path') . '/image.php?object_id=' . scrub_out($uid) . '&object_type=' . scrub_out($type) . '&auth=' . $sid; if ($thumb) { $url .= '&thumb=' . $thumb; } if (!empty($extension)) { $name = 'art.' . $extension; $url .= '&name=' . $name; } } return $url; }
public function get_info($id, $table = 'catalog') { $info = parent::get_info($id, $table); $table = 'catalog_' . $this->get_type(); $sql = "SELECT `id` FROM {$table} WHERE `catalog_id` = ?"; $db_results = Dba::read($sql, array($id)); if ($results = Dba::fetch_assoc($db_results)) { $info_type = parent::get_info($results['id'], $table); foreach ($info_type as $key => $value) { if (!$info[$key]) { $info[$key] = $value; } } } return $info; }
/** * get_tags * This is a non-object non type dependent function that just returns tags * we've got, it can take filters (this is used by the tag cloud) */ public static function get_tags($type = '', $limit = 0, $order = 'count') { //debug_event('tag.class.php', 'Get tags list called...', '5'); if (parent::is_cached('tags_list', 'no_name')) { //debug_event('tag.class.php', 'Tags list found into cache memory!', '5'); return parent::get_from_cache('tags_list', 'no_name'); } $results = array(); $sql = "SELECT `tag_map`.`tag_id`, `tag`.`name`, `tag`.`is_hidden`, COUNT(`tag_map`.`object_id`) AS `count` " . "FROM `tag_map` " . "LEFT JOIN `tag` ON `tag`.`id`=`tag_map`.`tag_id` " . "WHERE `tag`.`is_hidden` = false "; if (!empty($type)) { $sql .= "AND `tag_map`.`object_type` = '" . scrub_in($type) . "' "; } $order = "`" . $order . "`"; if ($order == 'count') { $order .= " DESC"; } $sql .= "GROUP BY `tag`.`name` ORDER BY " . $order; if ($limit > 0) { $sql .= " LIMIT {$limit}"; } $db_results = Dba::read($sql); while ($row = Dba::fetch_assoc($db_results)) { $results[$row['tag_id']] = array('id' => $row['tag_id'], 'name' => $row['name'], 'is_hidden' => $row['is_hidden'], 'count' => $row['count']); } parent::add_to_cache('tags_list', 'no_name', $results); return $results; }
/** * _get_ext_info * This function gathers information from the song_ext_info table and adds it to the * current object * @return array */ public function _get_ext_info() { $id = intval($this->id); if (parent::is_cached('song_data', $id)) { return parent::get_from_cache('song_data', $id); } $sql = "SELECT * FROM song_data WHERE `song_id` = ?"; $db_results = Dba::read($sql, array($id)); $results = Dba::fetch_assoc($db_results); parent::add_to_cache('song_data', $id, $results); return $results; }