diff options
author | Paul 'flowerysong' Arthur <flowerysong00@yahoo.com> | 2010-06-10 05:33:57 +0000 |
---|---|---|
committer | Paul 'flowerysong' Arthur <flowerysong00@yahoo.com> | 2010-06-10 05:33:57 +0000 |
commit | 7f3669335313adf9108119df4186a51a5f94e292 (patch) | |
tree | f8b488a09df2198723de5661b050bc31e112b7ab /lib | |
parent | c1ed41a16dcbf1a632ad2ac99b40174628bf072e (diff) | |
download | ampache-7f3669335313adf9108119df4186a51a5f94e292.tar.gz ampache-7f3669335313adf9108119df4186a51a5f94e292.tar.bz2 ampache-7f3669335313adf9108119df4186a51a5f94e292.zip |
Change Browse from static to instantiable. Among other things, fixes FS#13;
probably also breaks things. Most things appear to still work, but I may have
missed some cases.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/class/api.class.php | 81 | ||||
-rw-r--r-- | lib/class/art.class.php | 36 | ||||
-rw-r--r-- | lib/class/browse.class.php | 137 | ||||
-rw-r--r-- | lib/class/query.class.php | 571 | ||||
-rw-r--r-- | lib/class/update.class.php | 23 |
5 files changed, 447 insertions, 401 deletions
diff --git a/lib/class/api.class.php b/lib/class/api.class.php index 4fef01d4..821ad6f6 100644 --- a/lib/class/api.class.php +++ b/lib/class/api.class.php @@ -22,13 +22,16 @@ /** * API Class - * This handles functions relating to the API written for ampache, initially this is very focused - * on providing functionality for Amarok so it can integrate with Ampache + * This handles functions relating to the API written for ampache, initially + * this is very focused on providing functionality for Amarok so it can + * integrate with Ampache. */ class Api { public static $version = '350001'; + private static $browse = null; + /** * constructor * This really isn't anything to do here, so it's private @@ -40,6 +43,16 @@ class Api { } // constructor /** + * _auto_init + * Automatically called when this class is loaded. + */ + public static function _auto_init() { + if (is_null(self::$browse)) { + self::$browse = new Browse(); + } + } + + /** * set_filter * This is a play on the browse function, it's different as we expose * the filters in a slightly different and vastly simpler way to the @@ -55,29 +68,29 @@ class Api { // Check for a range, if no range default to gt if (strpos($value,'/')) { $elements = explode('/',$value); - Browse::set_filter('add_lt',strtotime($elements['1'])); - Browse::set_filter('add_gt',strtotime($elements['0'])); + self::$browse->set_filter('add_lt',strtotime($elements['1'])); + self::$browse->set_filter('add_gt',strtotime($elements['0'])); } else { - Browse::set_filter('add_gt',strtotime($value)); + self::$browse->set_filter('add_gt',strtotime($value)); } break; case 'update': // Check for a range, if no range default to gt if (strpos($value,'/')) { $elements = explode('/',$value); - Browse::set_filter('update_lt',strtotime($elements['1'])); - Browse::set_filter('update_gt',strtotime($elements['0'])); + self::$browse->set_filter('update_lt',strtotime($elements['1'])); + self::$browse->set_filter('update_gt',strtotime($elements['0'])); } else { - Browse::set_filter('update_gt',strtotime($value)); + self::$browse->set_filter('update_gt',strtotime($value)); } break; case 'alpha_match': - Browse::set_filter('alpha_match',$value); + self::$browse->set_filter('alpha_match',$value); break; case 'exact_match': - Browse::set_filter('exact_match',$value); + self::$browse->set_filter('exact_match',$value); break; default: // Rien a faire @@ -242,9 +255,9 @@ class Api { */ public static function artists($input) { - Browse::reset_filters(); - Browse::set_type('artist'); - Browse::set_sort('name','ASC'); + self::$browse->reset_filters(); + self::$browse->set_type('artist'); + self::$browse->set_sort('name','ASC'); $method = $input['exact'] ? 'exact_match' : 'alpha_match'; Api::set_filter($method,$input['filter']); @@ -255,7 +268,7 @@ class Api { xmlData::set_offset($input['offset']); xmlData::set_limit($input['limit']); - $artists = Browse::get_objects(); + $artists = self::$browse->get_objects(); // echo out the resulting xml document ob_end_clean(); echo xmlData::artists($artists); @@ -315,15 +328,15 @@ class Api { */ public static function albums($input) { - Browse::reset_filters(); - Browse::set_type('album'); - Browse::set_sort('name','ASC'); + self::$browse->reset_filters(); + self::$browse->set_type('album'); + self::$browse->set_sort('name','ASC'); $method = $input['exact'] ? 'exact_match' : 'alpha_match'; Api::set_filter($method,$input['filter']); Api::set_filter('add',$input['add']); Api::set_filter('update',$input['update']); - $albums = Browse::get_objects(); + $albums = self::$browse->get_objects(); // Set the offset xmlData::set_offset($input['offset']); @@ -368,13 +381,13 @@ class Api { */ public static function tags($input) { - Browse::reset_filters(); - Browse::set_type('tag'); - Browse::set_sort('name','ASC'); + self::$browse->reset_filters(); + self::$browse->set_type('tag'); + self::$browse->set_sort('name','ASC'); $method = $input['exact'] ? 'exact_match' : 'alpha_match'; Api::set_filter($method,$input['filter']); - $tags = Browse::get_objects(); + $tags = self::$browse->get_objects(); // Set the offset xmlData::set_offset($input['offset']); @@ -451,16 +464,16 @@ class Api { */ public static function songs($input) { - Browse::reset_filters(); - Browse::set_type('song'); - Browse::set_sort('title','ASC'); + self::$browse->reset_filters(); + self::$browse->set_type('song'); + self::$browse->set_sort('title','ASC'); $method = $input['exact'] ? 'exact_match' : 'alpha_match'; Api::set_filter($method,$input['filter']); Api::set_filter('add',$input['add']); Api::set_filter('update',$input['update']); - $songs = Browse::get_objects(); + $songs = self::$browse->get_objects(); // Set the offset xmlData::set_offset($input['offset']); @@ -506,14 +519,14 @@ class Api { */ public static function playlists($input) { - Browse::reset_filters(); - Browse::set_type('playlist'); - Browse::set_sort('name','ASC'); + self::$browse->reset_filters(); + self::$browse->set_type('playlist'); + self::$browse->set_sort('name','ASC'); $method = $input['exact'] ? 'exact_match' : 'alpha_match'; Api::set_filter($method,$input['filter']); - $playlist_ids = Browse::get_objects(); + $playlist_ids = self::$browse->get_objects(); xmlData::set_offset($input['offset']); xmlData::set_limit($input['limit']); @@ -586,14 +599,14 @@ class Api { */ public static function videos($input) { - Browse::reset_filters(); - Browse::set_type('video'); - Browse::set_sort('title','ASC'); + self::$browse->reset_filters(); + self::$browse->set_type('video'); + self::$browse->set_sort('title','ASC'); $method = $input['exact'] ? 'exact_match' : 'alpha_match'; Api::set_filter($method,$input['filter']); - $video_ids = Browse::get_objects(); + $video_ids = self::$browse->get_objects(); xmlData::set_offset($input['offset']); xmlData::set_limit($input['limit']); diff --git a/lib/class/art.class.php b/lib/class/art.class.php index cdf77cc1..ddb74539 100644 --- a/lib/class/art.class.php +++ b/lib/class/art.class.php @@ -36,6 +36,7 @@ class Art extends database_object { public $thumb; public $thumb_mime; + private static $enabled; /** * Constructor @@ -50,6 +51,41 @@ class Art extends database_object { } // constructor /** + * _auto_init + * Called on creation of the class + */ + public static function _auto_init() { + self::$enabled = make_bool($_SESSION['art_enabled']); + } + + /** + * is_enabled + * Checks whether the user currently wants art + */ + public static function is_enabled() { + if (self::$enabled || (Config::get('bandwidth') > 25)) { + return true; + } + + return false; + } + + /** + * set_enabled + * Changes the value of enabled + */ + public static function set_enabled($value = null) { + if (is_null($value)) { + self::$enabled = self::$enabled ? false : true; + } + else { + self::$enabled = make_bool($value); + } + + $_SESSION['art_enabled'] = self::$enabled; + } + + /** * validate_type * This validates the type */ diff --git a/lib/class/browse.class.php b/lib/class/browse.class.php index 17408a37..7b8a75d9 100644 --- a/lib/class/browse.class.php +++ b/lib/class/browse.class.php @@ -29,40 +29,14 @@ */ class Browse extends Query { - // Public static vars that are cached - public static $sql; - public static $start; - public static $offset; - public static $total_objects; - public static $type; - - // Boolean if this is a simple browse method (use different paging code) - public static $simple_browse; - - // Static Content, this is defaulted to false, if set to true then when we can't - // apply any filters that would change the result set. - public static $static_content = false; - private static $_cache = array(); - - - /** - * constructor - * This should never be called - */ - private function __construct() { - - // Rien a faire - - } // __construct - /** * set_simple_browse * This sets the current browse object to a 'simple' browse method * which means use the base query provided and expand from there */ - public static function set_simple_browse($value) { + public function set_simple_browse($value) { - parent::set_is_simple($value); + $this->set_is_simple($value); } // set_simple_browse @@ -70,9 +44,9 @@ class Browse extends Query { * add_supplemental_object * Legacy function, need to find a better way to do that */ - public static function add_supplemental_object($class,$uid) { + public function add_supplemental_object($class, $uid) { - $_SESSION['browse']['supplemental'][$class] = intval($uid); + $_SESSION['browse']['supplemental'][$this->id][$class] = intval($uid); return true; @@ -80,12 +54,12 @@ class Browse extends Query { /** * get_supplemental_objects - * This returns an array of 'class','id' for additional objects that need to be - * created before we start this whole browsing thing + * This returns an array of 'class','id' for additional objects that + * need to be created before we start this whole browsing thing. */ - public static function get_supplemental_objects() { + public function get_supplemental_objects() { - $objects = $_SESSION['browse']['supplemental']; + $objects = $_SESSION['browse']['supplemental'][$this->id]; if (!is_array($objects)) { $objects = array(); } @@ -94,73 +68,57 @@ class Browse extends Query { } // get_supplemental_objects /** - * is_enabled - * This checks if the specified function/feature - * of browsing is enabled, not sure if this is the best - * way to go about it, but hey. Returns boolean t/f - */ - public static function is_enabled($item) { - - switch ($item) { - case 'show_art': - if (Browse::get_filter('show_art')) { - return true; - } - if (Config::get('bandwidth') > 25) { - return true; - } - break; - } // end switch - - return false; - - } // is_enabled - - /** * show_objects * This takes an array of objects * and requires the correct template based on the * type that we are currently browsing */ - public static function show_objects($object_ids=false) { + public function show_objects($object_ids = null) { - if (parent::is_simple()) { - $object_ids = parent::get_saved(); + if ($this->is_simple() || ! is_array($object_ids)) { + $object_ids = $this->get_saved(); } else { - $object_ids = is_array($object_ids) ? $object_ids : parent::get_saved(); - parent::save_objects($object_ids); + $this->save_objects($object_ids); } - // Reset the total items - self::$total_objects = parent::get_total($object_ids); - - // Limit is based on the users preferences if this is not a simple browse because we've got too much here - if (count($object_ids) > parent::get_start() AND !parent::is_simple()) { - $object_ids = array_slice($object_ids,parent::get_start(),parent::get_offset(),TRUE); + // Limit is based on the user's preferences if this is not a + // simple browse because we've got too much here + if ((count($object_ids) > $this->get_start()) && + ! $this->is_simple() && + ! $this->is_static_content()) { + $object_ids = array_slice( + $object_ids, + $this->get_start(), + $this->get_offset(), + true + ); } // Load any additional object we need for this - $extra_objects = self::get_supplemental_objects(); + $extra_objects = $this->get_supplemental_objects(); + $browse = $this; foreach ($extra_objects as $class_name => $id) { ${$class_name} = new $class_name($id); } // Format any matches we have so we can show them to the masses - if ($filter_value = parent::get_filter('alpha_match')) { + if ($filter_value = $this->get_filter('alpha_match')) { $match = ' (' . $filter_value . ')'; } - elseif ($filter_value = parent::get_filter('starts_with')) { + elseif ($filter_value = $this->get_filter('starts_with')) { $match = ' (' . $filter_value . ')'; } + $type = $this->get_type(); + // Set the correct classes based on type - $class = "box browse_".self::$type; + $class = "box browse_" . $type; Ajax::start_container('browse_content'); // Switch on the type of browsing we're doing - switch (parent::get_type()) { + switch ($type) { case 'song': show_box_top(_('Songs') . $match, $class); Song::build_cache($object_ids); @@ -241,45 +199,34 @@ class Browse extends Query { // Rien a faire break; } // end switch on type + echo '<script type="text/javascript">ajaxPut("' . Config::get('ajax_url') . '?page=browse&action=get_filters&browse_id=' . $this->id . '","");</script>'; Ajax::end_container(); } // show_object /** - * _auto_init - * this function reloads information back from the session - * it is called on creation of the class - */ - public static function _auto_init() { - - $offset = Config::get('offset_limit') ? Config::get('offset_limit') : '25'; - parent::set_offset($offset); - - } // _auto_init - - /** * set_filter_from_request * //FIXME */ - public static function set_filter_from_request($r) { - foreach($r as $k=>$v) { + public function set_filter_from_request($request) { + foreach($request as $key => $value) { //reinterpret v as a list of int - $vl = explode(',', $v); - $ok = 1; - foreach($vl as $i) { - if (!is_numeric($i)) { - $ok = 0; + $list = explode(',', $value); + $ok = true; + foreach($list as $item) { + if (!is_numeric($item)) { + $ok = false; break; } } if ($ok) { - if (sizeof($vl) == 1) { - self::set_filter($k, $vl[0]); + if (sizeof($list) == 1) { + $this->set_filter($key, $list[0]); } } else { - self::set_filter($k, $vl); + $this->set_filter($key, $list); } } } // set_filter_from_request diff --git a/lib/class/query.class.php b/lib/class/query.class.php index 109cef1f..112f7a07 100644 --- a/lib/class/query.class.php +++ b/lib/class/query.class.php @@ -23,67 +23,78 @@ /** * Query Class * This handles all of the sql/filtering for the ampache database - * this was seperated out from browse, to accomodate Dynamic Playlists + * this was seperated out from browse to accomodate Dynamic Playlists */ class Query { - // Public static vars that are cached - public static $sql; - public static $start; - public static $offset; - public static $total_objects; - public static $type; + public $id; - // Static Content, this is defaulted to false, if set to true then when we can't - // apply any filters that would change the result set. - public static $static_content = false; - - // Private cache information - private static $_cache = array(); - private static $_state = array(); + protected $_state = array(); + protected $_cache; /** * constructor - * This should never be called + * This should be called */ - private function __construct() { + public function __construct($id = null) { + $sid = Dba::escape(session_id()); + + if (is_null($id)) { + $this->reset(); + $data = Dba::escape(serialize($this->_state)); + + $sql = "INSERT INTO `tmp_browse` (`sid`, `data`) " . + "VALUES('$sid', '$data')"; + $db_results = Dba::write($sql); + $this->id = Dba::insert_id(); + return true; + } + + $this->id = $id; - // Rien a faire + $sql = "SELECT `data` FROM `tmp_browse` " . + "WHERE `id`='$id' AND `sid`='$sid'"; + + $db_results = Dba::read($sql); + + if ($results = Dba::fetch_assoc($db_results)) { + $this->_state = unserialize($results['data']); + + } - } // __construct + Error::add('browse', _('Browse not found or expired, try reloading the page')); + return false; + } /** * set_filter - * This saves the filter data we pass it into the session - * This is done here so that it's easy to modify where the data - * is saved should I change my mind in the future. It also makes - * a single point for whitelist tweaks etc + * This saves the filter data we pass it. */ - public static function set_filter($key,$value) { + public function set_filter($key, $value) { switch ($key) { case 'show_art': - if (self::get_filter($key)) { - unset(self::$_state['filter'][self::$type][$key]); + if ($this->get_filter($key, $id)) { + unset($this->_state['filter'][$key]); } else { - self::$_state['filter'][self::$type][$key] = 1; + $this->_state['filter'][$key] = true; } break; case 'tag': if (is_array($value)) { - self::$_state['filter'][self::$type][$key] = $value; + $this->_state['filter'][$key] = $value; } elseif (is_numeric($value)) { - self::$_state['filter'][self::$type][$key] = array($value); + $this->_state['filter'][$key] = array($value); } else { - self::$_state['filter'][self::$type][$key] = array(); + $this->_state['filter'][$key] = array(); } break; case 'artist': case 'album': - self::$_state['filter'][self::$type][$key] = $value; + $this->_state['filter'][$key] = $value; break; case 'min_count': case 'unplayed': @@ -94,18 +105,18 @@ class Query { case 'add_gt': case 'update_lt': case 'update_gt': - self::$_state['filter'][self::$type][$key] = intval($value); + $this->_state['filter'][$key] = intval($value); break; case 'exact_match': case 'alpha_match': case 'starts_with': - if (self::$static_content) { return false; } - self::$_state['filter'][self::$type][$key] = $value; + if ($this->is_static_content($id)) { return false; } + $this->_state['filter'][$key] = $value; break; case 'playlist_type': - // They must be content managers to turn this off - if (self::$_state['filter'][self::$type][$key] AND Access::check('interface','50')) { unset(self::$_state['filter'][self::$type][$key]); } - else { self::$_state['filter'][self::$type][$key] = '1'; } + // Must be a content manager to turn this off + if ($this->_state['filter'][$key] AND Access::check('interface','50')) { unset($this->_state['filter'][$key]); } + else { $this->_state['filter'][$key] = '1'; } break; default: // Rien a faire @@ -114,8 +125,8 @@ class Query { } // end switch // If we've set a filter we need to reset the totals - self::reset_total(); - self::set_start(0); + $this->reset_total($id); + $this->set_start(0, $id); return true; @@ -123,18 +134,20 @@ class Query { /** * reset - * Reset everything, this should only be called when we are starting fresh + * Reset everything, this should only be called when we are starting + * fresh */ - public static function reset() { - - self::reset_base(); - self::reset_filters(); - self::reset_total(); - self::reset_join(); - self::reset_select(); - self::reset_having(); - self::set_is_simple(0); - self::set_start(0); + public function reset() { + + $this->reset_base(); + $this->reset_filters(); + $this->reset_total(); + $this->reset_join(); + $this->reset_select(); + $this->reset_having(); + $this->set_is_simple(false); + $this->set_start(0); + $this->set_offset(Config::get('offset_limit') ? Config::get('offset_limit') : '25'); } // reset @@ -142,9 +155,9 @@ class Query { * reset_base * this resets the base string */ - public static function reset_base() { + public function reset_base() { - self::$_state['base'][self::$type] = NULL; + $this->_state['base'] = NULL; } // reset_base @@ -152,9 +165,9 @@ class Query { * reset_select * This resets the select fields that we've added so far */ - public static function reset_select() { + public function reset_select() { - self::$_state['select'][self::$type] = array(); + $this->_state['select'] = array(); } // reset_select @@ -162,9 +175,9 @@ class Query { * reset_having * Null out the having clause */ - public static function reset_having() { + public function reset_having() { - unset(self::$_state['having'][self::$type]); + unset($this->_state['having']); } // reset_having @@ -172,9 +185,9 @@ class Query { * reset_join * clears the joins if there are any */ - public static function reset_join() { + public function reset_join() { - unset(self::$_state['join'][self::$type]); + unset($this->_state['join']); } // reset_join @@ -182,9 +195,9 @@ class Query { * reset_filter * This is a wrapper function that resets the filters */ - public static function reset_filters() { + public function reset_filters() { - self::$_state['filter'] = array(); + $this->_state['filter'] = array(); } // reset_filters @@ -192,9 +205,9 @@ class Query { * reset_total * This resets the total for the browse type */ - public static function reset_total() { + public function reset_total() { - unset(self::$_state['total'][self::$type]); + unset($this->_state['total']); } // reset_total @@ -202,10 +215,11 @@ class Query { * get_filter * returns the specified filter value */ - public static function get_filter($key) { + public function get_filter($key) { // Simple enough, but if we ever move this crap - return self::$_state['filter'][self::$type][$key]; + // If we ever move this crap what? + return $this->_state['filter'][$key]; } // get_filter @@ -213,9 +227,9 @@ class Query { * get_start * This returns the current value of the start */ - public static function get_start() { + public function get_start() { - return self::$start; + return $this->_state['start']; } // get_start @@ -223,33 +237,44 @@ class Query { * get_offset * This returns the current offset */ - public static function get_offset() { - - return self::$offset; + public function get_offset() { + if ($this->is_static_content()) { + return $this->get_total(); + } + return $this->_state['offset']; } // get_offset /** + * set_total + * This sets the total number of objects + */ + public function set_total($total) { + $this->_state['total'] = $total; + } + + /** * get_total - * This returns the toal number of obejcts for this current sort type. If it's already cached used it! - * if they pass us an array then use that! + * This returns the total number of objects for this current sort type. + * If it's already cached used it. if they pass us an array then use + * that. */ - public static function get_total($objects=false) { + public function get_total($objects = null) { // If they pass something then just return that - if (is_array($objects) and !self::is_simple()) { + if (is_array($objects) and !$this->is_simple()) { return count($objects); } // See if we can find it in the cache - if (isset(self::$_state['total'][self::$type])) { - return self::$_state['total'][self::$type]; + if (isset($this->_state['total'])) { + return $this->_state['total']; } - $db_results = Dba::read(self::get_sql(false)); + $db_results = Dba::read($this->get_sql(false)); $num_rows = Dba::num_rows($db_results); - self::$_state['total'][self::$type] = $num_rows; + $this->_state['total'] = $num_rows; return $num_rows; @@ -257,12 +282,13 @@ class Query { /** * get_allowed_filters - * This returns an array of the allowed filters based on the type of object we are working - * with, this is used to display the 'filter' sidebar stuff, must be called post browse stuff + * This returns an array of the allowed filters based on the type of + * object we are working with, this is used to display the 'filter' + * sidebar stuff. */ - public static function get_allowed_filters() { + public static function get_allowed_filters($type) { - switch (self::$type) { + switch ($type) { case 'album': $valid_array = array('add_lt','add_gt','update_lt','update_gt','show_art', 'starts_with','exact_match','alpha_match'); @@ -301,7 +327,7 @@ class Query { * we do this here so we only have to maintain a single whitelist * and if I want to change the location I only have to do it here */ - public static function set_type($type) { + public function set_type($type) { switch($type) { case 'user': @@ -319,8 +345,8 @@ class Query { case 'live_stream': case 'democratic': // Set it - self::$type = $type; - self::load_start(); + $this->_state['type'] = $type; + $this->set_base_sql(true); break; default: // Rien a faire @@ -332,9 +358,9 @@ class Query { * get_type * This returns the type of the browse we currently are using */ - public static function get_type() { + public function get_type() { - return self::$type; + return $this->_state['type']; } // get_type @@ -342,9 +368,9 @@ class Query { * set_sort * This sets the current sort(s) */ - public static function set_sort($sort,$order='') { + public function set_sort($sort,$order='') { - switch (self::get_type()) { + switch ($this->get_type()) { case 'playlist_song': case 'song': $valid_array = array('title','year','track','time','album','artist'); @@ -376,27 +402,27 @@ class Query { } // end switch // If it's not in our list, smeg off! - if (!in_array($sort,$valid_array)) { + if (!in_array($sort, $valid_array)) { return false; } if ($order) { $order = ($order == 'DESC') ? 'DESC' : 'ASC'; - self::$_state['sort'][self::$type] = array(); - self::$_state['sort'][self::$type][$sort] = $order; + $this->_state['sort'] = array(); + $this->_state['sort'][$sort] = $order; } - elseif (self::$_state['sort'][self::$type][$sort] == 'DESC') { + elseif ($this->_state['sort'][$sort] == 'DESC') { // Reset it till I can figure out how to interface the hotness - self::$_state['sort'][self::$type] = array(); - self::$_state['sort'][self::$type][$sort] = 'ASC'; + $this->_state['sort'] = array(); + $this->_state['sort'][$sort] = 'ASC'; } else { // Reset it till I can figure out how to interface the hotness - self::$_state['sort'][self::$type] = array(); - self::$_state['sort'][self::$type][$sort] = 'DESC'; + $this->_state['sort'] = array(); + $this->_state['sort'][$sort] = 'DESC'; } - self::resort_objects(); + $this->resort_objects(); } // set_sort @@ -404,20 +430,21 @@ class Query { * set_offset * This sets the current offset of this query */ - public static function set_offset($offset) { + public function set_offset($offset) { - self::$offset = abs($offset); + $this->_state['offset'] = abs($offset); } // set_offset /** * set_select - * This appends more information to the select part of the SQL statement, we're going to move to the - * %%SELECT%% style queries, as I think it's the only way to do this.... + * This appends more information to the select part of the SQL + * statement, we're going to move to the %%SELECT%% style queries, as I + * think it's the only way to do this... */ - public static function set_select($field) { + public function set_select($field) { - self::$_state['select'][self::$type][] = $field; + $this->_state['select'][] = $field; } // set_select @@ -425,19 +452,20 @@ class Query { * set_join * This sets the joins for the current browse object */ - public static function set_join($type,$table,$source,$dest,$priority=100) { + public function set_join($type, $table, $source, $dest, $priority) { - self::$_state['join'][self::$type][$priority][$table] = strtoupper($type) . ' JOIN ' . $table . ' ON ' . $source . '=' . $dest; + $this->_state['join'][$priority][$table] = strtoupper($type) . ' JOIN ' . $table . ' ON ' . $source . '=' . $dest; } // set_join /** * set_having - * This sets the "HAVING" part of the query, we can only have one.. god this is ugly + * This sets the "HAVING" part of the query, we can only have one.. + * god this is ugly */ - public static function set_having($condition) { + public function set_having($condition) { - self::$_state['having'][self::$type] = $condition; + $this->_state['having'] = $condition; } // set_having @@ -447,12 +475,14 @@ class Query { * We need to store this in the session so that it can be pulled * back, if they hit the back button */ - public static function set_start($start) { + public function set_start($start) { - if (!self::$static_content) { - self::$_state[self::$type]['start'] = intval($start); + + $start = intval($start); + + if (!$this->is_static_content()) { + $this->_state['start'] = $start; } - self::$start = intval($start); } // set_start @@ -461,10 +491,10 @@ class Query { * This sets the current browse object to a 'simple' browse method * which means use the base query provided and expand from there */ - public static function set_is_simple($value) { + public function set_is_simple($value) { $value = make_bool($value); - self::$_state['simple'][self::$type] = $value; + $this->_state['simple'] = $value; } // set_is_simple @@ -474,66 +504,58 @@ class Query { * should be static, if they are then content filtering/altering * methods will be skipped */ - public static function set_static_content($value) { + public function set_static_content($value) { $value = make_bool($value); - self::$static_content = $value; - // We want to start at 0 it's static + // We want to start at 0 if it's static if ($value) { - self::set_start('0'); + $this->set_start('0'); } - self::$_state[self::$type]['static'] = $value; + $this->_state['static'] = $value; } // set_static_content + public function is_static_content() { + return $this->_state['static']; + } + /** * is_simple - * this returns true or false if the current browse type is set to static + * This returns whether or not the current browse type is set to static. */ - public static function is_simple() { + public function is_simple() { - return self::$_state['simple'][self::$type]; + return $this->_state['simple']; } // is_simple /** - * load_start - * This returns a stored start point for the browse mojo - */ - public static function load_start() { - - self::$start = intval(self::$_state[self::$type]['start']); - - } // end load_start - - /** * get_saved - * This looks in the session for the saved - * stuff and returns what it finds + * This looks in the session for the saved stuff and returns what it + * finds. */ - public static function get_saved() { + public function get_saved() { // See if we have it in the local cache first - if (is_array(self::$_cache['browse'][self::$type])) { - return self::$_cache['browse'][self::$type]; + if (is_array($this->_cache)) { + return $this->_cache; } - if (!self::is_simple()) { - // If not then we're going to need to read from the database :( - $sid = session_id(); - $type = Dba::escape(self::$type); - - $sql = "SELECT `data` FROM `tmp_browse` WHERE `sid`='$sid' AND `type`='$type'"; + if (!$this->is_simple()) { + $sid = Dba::escape(session_id()); + $id = Dba::escape($this->id); + $sql = "SELECT `object_data` FROM `tmp_browse` WHERE `sid`='$sid' AND `id`='$id'"; $db_results = Dba::read($sql); $row = Dba::fetch_assoc($db_results); - $objects = unserialize($row['data']); + $this->_cache = unserialize($row['object_data']); + return $this->_cache; } else { - $objects = self::get_objects(); + $objects = $this->get_objects(); } return $objects; @@ -546,11 +568,11 @@ class Query { * currently browsing by it applies the sql and logic based * filters */ - public static function get_objects() { + public function get_objects() { // First we need to get the SQL statement we are going to run // This has to run against any possible filters (dependent on type) - $sql = self::get_sql(); + $sql = $this->get_sql(true); $db_results = Dba::read($sql); $results = array(); @@ -558,17 +580,17 @@ class Query { $results[] = $data; } - $results = self::post_process($results); + $results = $this->post_process($results); $filtered = array(); foreach ($results as $data) { // Make sure that this object passes the logic filter - if (self::logic_filter($data['id'])) { + if ($this->logic_filter($data['id'])) { $filtered[] = $data['id']; } } // end while // Save what we've found and then return it - self::save_objects($filtered); + $this->save_objects($filtered); return $filtered; @@ -578,84 +600,81 @@ class Query { * set_base_sql * This saves the base sql statement we are going to use. */ - private static function set_base_sql() { - + private function set_base_sql($force = false) { + // Only allow it to be set once - if (strlen(self::$_state['base'][self::$type])) { return true; } + if (strlen($this->_state['base']) && !$force) { return true; } - switch (self::$type) { + switch ($this->get_type()) { case 'album': - self::set_select("DISTINCT(`album`.`id`)"); + $this->set_select("DISTINCT(`album`.`id`)"); $sql = "SELECT %%SELECT%% FROM `album` "; break; case 'artist': - self::set_select("DISTINCT(`artist`.`id`)"); + $this->set_select("DISTINCT(`artist`.`id`)"); $sql = "SELECT %%SELECT%% FROM `artist` "; break; case 'user': - self::set_select("`user`.`id`"); + $this->set_select("`user`.`id`"); $sql = "SELECT %%SELECT%% FROM `user` "; break; case 'live_stream': - self::set_select("`live_stream`.`id`"); + $this->set_select("`live_stream`.`id`"); $sql = "SELECT %%SELECT%% FROM `live_stream` "; break; case 'playlist': - self::set_select("`playlist`.`id`"); + $this->set_select("`playlist`.`id`"); $sql = "SELECT %%SELECT%% FROM `playlist` "; break; case 'flagged': - self::set_select("`flagged`.`id`"); + $this->set_select("`flagged`.`id`"); $sql = "SELECT %%SELECT%% FROM `flagged` "; break; case 'shoutbox': - self::set_select("`user_shout`.`id`"); + $this->set_select("`user_shout`.`id`"); $sql = "SELECT %%SELECT%% FROM `user_shout` "; break; case 'video': - self::set_select("`video`.`id`"); + $this->set_select("`video`.`id`"); $sql = "SELECT %%SELECT%% FROM `video` "; break; case 'tag': - self::set_select("DISTINCT(`tag`.`id`)"); - self::set_join('left','tag_map','`tag_map`.`tag_id`','`tag`.`id`',1); + $this->set_select("DISTINCT(`tag`.`id`)"); + $this->set_join('left', 'tag_map', '`tag_map`.`tag_id`', '`tag`.`id`', 1); $sql = "SELECT %%SELECT%% FROM `tag` "; break; case 'playlist_song': case 'song': default: - self::set_select("DISTINCT(`song`.`id`)"); + $this->set_select("DISTINCT(`song`.`id`)"); $sql = "SELECT %%SELECT%% FROM `song` "; break; } // end base sql - self::$_state['base'][self::$type] = $sql; + $this->_state['base'] = $sql; } // set_base_sql /** * get_select - * This returns the selects in a format that is friendly for a sql statement + * This returns the selects in a format that is friendly for a sql + * statement. */ - private static function get_select() { + private function get_select() { - $select_string = implode(self::$_state['select'][self::$type],", "); + $select_string = implode($this->_state['select'], ", "); return $select_string; } // get_select /** * get_base_sql - * This returns the base sql statement all parsed up, this should be called after all set operations + * This returns the base sql statement all parsed up, this should be + * called after all set operations. */ - private static function get_base_sql() { - - // Legacy code, should be removed once other code is updated - //FIXME: REMOVE - if (!self::$_state['base'][self::$type]) { self::set_base_sql(); } - - $sql = str_replace("%%SELECT%%",self::get_select(),self::$_state['base'][self::$type]); + private function get_base_sql() { + $sql = str_replace("%%SELECT%%", $this->get_select(), $this->_state['base']); return $sql; } // get_base_sql @@ -664,16 +683,17 @@ class Query { * get_filter_sql * This returns the filter part of the sql statement */ - private static function get_filter_sql() { + private function get_filter_sql() { - if (!is_array(self::$_state['filter'][self::$type])) { + if (!is_array($this->_state['filter'])) { return ''; } $sql = "WHERE 1=1 AND "; - foreach (self::$_state['filter'][self::$type] as $key=>$value) { - $sql .= self::sql_filter($key,$value); + foreach ($this->_state['filter'] + as $key => $value) { + $sql .= $this->sql_filter($key, $value); } $sql = rtrim($sql,'AND ') . ' '; @@ -686,14 +706,17 @@ class Query { * get_sort_sql * Returns the sort sql part */ - private static function get_sort_sql() { + private function get_sort_sql() { - if (!is_array(self::$_state['sort'][self::$type])) { return ''; } + if (!is_array($this->_state['sort'])) { + return ''; + } $sql = 'ORDER BY '; - foreach (self::$_state['sort'][self::$type] as $key=>$value) { - $sql .= self::sql_sort($key,$value); + foreach ($this->_state['sort'] + as $key => $value) { + $sql .= $this->sql_sort($key, $value); } $sql = rtrim($sql,'ORDER BY '); @@ -707,11 +730,11 @@ class Query { * get_limit_sql * This returns the limit part of the sql statement */ - private static function get_limit_sql() { + private function get_limit_sql() { - if (!self::is_simple()) { return ''; } + if (!$this->is_simple()) { return ''; } - $sql = ' LIMIT ' . intval(self::$start) . ',' . intval(self::$offset); + $sql = ' LIMIT ' . intval($this->get_start()) . ',' . intval($this->get_offset()); return $sql; @@ -721,16 +744,15 @@ class Query { * get_join_sql * This returns the joins that this browse may need to work correctly */ - private static function get_join_sql() { + private function get_join_sql() { - if (!is_array(self::$_state['join'][self::$type])) { + if (!is_array($this->_state['join'])) { return ''; } $sql = ''; - // We need to itterate through these from 0 - 100 so that we add the joins in the right order - foreach (self::$_state['join'][self::$type] as $joins) { + foreach ($this->_state['join'] as $joins) { foreach ($joins as $join) { $sql .= $join . ' '; } // end foreach joins at this level @@ -744,9 +766,9 @@ class Query { * get_having_sql * this returns the having sql stuff, if we've got anything */ - public static function get_having_sql() { + public function get_having_sql() { - $sql = self::$_state['having'][self::$type]; + $sql = $this->_state['having']; return $sql; @@ -755,18 +777,18 @@ class Query { /** * get_sql * This returns the sql statement we are going to use this has to be run - * every time we get the objects because it depends on the filters and the - * type of object we are currently browsing + * every time we get the objects because it depends on the filters and + * the type of object we are currently browsing. */ - public static function get_sql($limit=true) { + public function get_sql($limit = true) { - $sql = self::get_base_sql(); + $sql = $this->get_base_sql(); - $filter_sql = self::get_filter_sql(); - $join_sql = self::get_join_sql(); - $having_sql = self::get_having_sql(); - $order_sql = self::get_sort_sql(); - $limit_sql = $limit ? self::get_limit_sql() : ''; + $filter_sql = $this->get_filter_sql(); + $join_sql = $this->get_join_sql(); + $having_sql = $this->get_having_sql(); + $order_sql = $this->get_sort_sql(); + $limit_sql = $limit ? $this->get_limit_sql() : ''; $final_sql = $sql . $join_sql . $filter_sql . $having_sql . $order_sql . $limit_sql; @@ -776,49 +798,51 @@ class Query { /** * post_process - * This does some additional work on the results that we've received before returning them + * This does some additional work on the results that we've received + * before returning them. */ - private static function post_process($results) { + private function post_process($data) { - $tags = self::$_state['filter']['tag']; + $tags = $this->_state['filter']['tag']; if (!is_array($tags) || sizeof($tags) < 2) { - return $results; + return $data; } - $cnt = sizeof($tags); - $ar = array(); - foreach($results as $row) { - $ar[$row['id']]++; + $tag_count = sizeof($tags); + $count = array(); + + foreach($data as $row) { + $count[$row['id']]++; } - $res = array(); + $results = array(); - foreach($ar as $k=>$v) { - if ($v >= $cnt) { - $res[] = array('id' => $k); + foreach($count as $key => $value) { + if ($value >= $tag_count) { + $results[] = array('id' => $key); } } // end foreach - return $res; + return $results; } // post_process /** * sql_filter * This takes a filter name and value and if it is possible - * to filter by this name on this type returns the approiate sql + * to filter by this name on this type returns the appropriate sql * if not returns nothing */ - private static function sql_filter($filter,$value) { + private function sql_filter($filter, $value) { $filter_sql = ''; - switch (self::$type) { + switch ($this->get_type()) { case 'song': switch($filter) { case 'tag': - self::set_join('left','`tag_map`','`tag_map`.`object_id`','`song`.`id`'); + $this->set_join('left', '`tag_map`', '`tag_map`.`object_id`', '`song`.`id`', 100); $filter_sql = " `tag_map`.`object_type`='song' AND ("; foreach ($value as $tag_id) { @@ -881,19 +905,19 @@ class Query { $filter_sql = " `artist`.`id` = '". Dba::escape($value) . "' AND "; break; case 'add_lt': - self::set_join('left','`song`','`song`.`album`','`album`.`id`'); + $this->set_join('left', '`song`', '`song`.`album`', '`album`.`id`', 100); $filter_sql = " `song`.`addition_time` <= '" . Dba::escape($value) . "' AND "; break; case 'add_gt': - self::set_join('left','`song`','`song`.`album`','`album`.`id`'); + $this->set_join('left', '`song`', '`song`.`album`', '`album`.`id`', 100); $filter_sql = " `song`.`addition_time` >= '" . Dba::escape($value) . "' AND "; break; case 'update_lt': - self::set_join('left','`song`','`song`.`album`','`album`.`id`'); + $this->set_join('left', '`song`', '`song`.`album`', '`album`.`id`', 100); $filter_sql = " `song`.`update_time` <= '" . Dba::escape($value) . "' AND "; break; case 'update_gt': - self::set_join('left','`song`','`song`.`album`','`album`.`id`'); + $this->set_join('left', '`song`', '`song`.`album`', '`album`.`id`', 100); $filter_sql = " `song`.`update_time` >= '" . Dba::escape($value) . "' AND "; break; default: @@ -913,19 +937,19 @@ class Query { $filter_sql = " `artist`.`name` LIKE '" . Dba::escape($value) . "%' AND "; break; case 'add_lt': - self::set_join('left','`song`','`song`.`artist`','`artist`.`id`'); + $this->set_join('left', '`song`', '`song`.`artist`', '`artist`.`id`', 100); $filter_sql = " `song`.`addition_time` <= '" . Dba::escape($value) . "' AND "; break; case 'add_gt': - self::set_join('left','`song`','`song`.`artist`','`artist`.`id`'); + $this->set_join('left', '`song`', '`song`.`artist`', '`artist`.`id`', 100); $filter_sql = " `song`.`addition_time` >= '" . Dba::escape($value) . "' AND "; break; case 'update_lt': - self::set_join('left','`song`','`song`.`artist`','`artist`.`id`'); + $this->set_join('left', '`song`', '`song`.`artist`', '`artist`.`id`', 100); $filter_sql = " `song`.`update_time` <= '" . Dba::escape($value) . "' AND "; break; case 'update_gt': - self::set_join('left','`song`','`song`.`artist`','`artist`.`id`'); + $this->set_join('left', '`song`', '`song`.`artist`', '`artist`.`id`', 100); $filter_sql = " `song`.`update_time` >= '" . Dba::escape($value) . "' AND "; break; default: @@ -1005,7 +1029,7 @@ class Query { * these should be limited as they are often intensive and * require additional queries per object... :( */ - private static function logic_filter($object_id) { + private function logic_filter($object_id) { return true; @@ -1018,12 +1042,13 @@ class Query { * a logic based sort that will come later as that's * a lot more complicated */ - private static function sql_sort($field,$order) { + private function sql_sort($field, $order) { if ($order != 'DESC') { $order == 'ASC'; } - // Depending on the type of browsing we are doing we can apply different filters that apply to different fields - switch (self::$type) { + // Depending on the type of browsing we are doing we can apply + // different filters that apply to different fields + switch ($this->get_type()) { case 'song': switch($field) { case 'title'; @@ -1040,11 +1065,11 @@ class Query { break; case 'album': $sql = '`album`.`name`'; - self::set_join('left','`album`','`album`.`id`','`song`.`album`'); + $this->set_join('left', '`album`', '`album`.`id`', '`song`.`album`', 100); break; case 'artist': $sql = '`artist`.`name`'; - self::set_join('left','`artist`','`artist`.`id`','`song`.`artist`'); + $this->set_join('left', '`artist`', '`artist`.`id`', '`song`.`artist`', 100); break; default: // Rien a faire @@ -1058,8 +1083,8 @@ class Query { break; case 'artist': $sql = "`artist`.`name`"; - self::set_join('left','`song`','`song`.`album`','`album`.`id`'); - self::set_join('left','`artist`','`song`.`artist`','`artist`.`id`'); + $this->set_join('left', '`song`', '`song`.`album`', '`album`.`id`', 100); + $this->set_join('left', '`artist`', '`song`.`artist`', '`artist`.`id`', 100); break; case 'year': $sql = "`album`.`year`"; @@ -1155,23 +1180,23 @@ class Query { * sort method and then re-sorts them This is internally * called by the set_sort() function */ - private static function resort_objects() { + private function resort_objects() { // There are two ways to do this.. the easy way... // and the vollmer way, hopefully we don't have to // do it the vollmer way - if (self::is_simple()) { - $sql = self::get_sql(); + if ($this->is_simple()) { + $sql = $this->get_sql(true); } else { // First pull the objects - $objects = self::get_saved(); + $objects = $this->get_saved(); // If there's nothing there don't do anything if (!count($objects) or !is_array($objects)) { return false; } - $type = self::$type; + $type = $this->get_type(); $where_sql = "WHERE `$type`.`id` IN ("; foreach ($objects as $object_id) { @@ -1182,18 +1207,18 @@ class Query { $where_sql .= ")"; - $sql = self::get_base_sql(); + $sql = $this->get_base_sql(); $order_sql = " ORDER BY "; - foreach (self::$_state['sort'][self::$type] as $key=>$value) { - $order_sql .= self::sql_sort($key,$value); + foreach ($this->_state['sort'] as $key => $value) { + $order_sql .= $this->sql_sort($key, $value); } // Clean her up $order_sql = rtrim($order_sql,"ORDER BY "); $order_sql = rtrim($order_sql,","); - $sql = $sql . self::get_join_sql() . $where_sql . $order_sql; + $sql = $sql . $this->get_join_sql() . $where_sql . $order_sql; } // if not simple $db_results = Dba::read($sql); @@ -1202,34 +1227,48 @@ class Query { $results[] = $row['id']; } - self::save_objects($results); + $this->save_objects($results); return true; } // resort_objects /** + * store + * This saves the current state to the database + */ + public function store() { + $sid = Dba::escape(session_id()); + $id = Dba::escape($this->id); + $data = Dba::escape(serialize($this->_state)); + + $sql = "UPDATE `tmp_browse` SET `data`='$data' " . + "WHERE `sid`='$sid' AND `id`='$id'"; + $db_results = Dba::write($sql); + } + + /** * save_objects - * This takes the full array of object ides, often passed into show and then - * if nessecary it saves them into the session + * This takes the full array of object ids, often passed into show and + * if necessary it saves them */ - public static function save_objects($object_ids) { + public function save_objects($object_ids) { - // Saving these objects has two operations, one hold it in - // a local variable and then second hold it in a row in the tmp_browse - // table - self::$_cache['browse'][self::$type] = $object_ids; + // Saving these objects has two operations, one holds it in + // a local variable and then second holds it in a row in the + // tmp_browse table // Only do this if it's a not a simple browse - if (!self::is_simple()) { + if (!$this->is_simple()) { + $this->_cache = $object_ids; + $this->set_total(count($object_ids)); $sid = Dba::escape(session_id()); - $data = Dba::escape(serialize($object_ids)); - $type = Dba::escape(self::$type); + $id = Dba::escape($this->id); + $data = Dba::escape(serialize($this->_cache)); - $sql = "REPLACE INTO `tmp_browse` SET `data`='$data', `sid`='$sid',`type`='$type'"; + $sql = "UPDATE `tmp_browse` SET `object_data`='$data' " . + "WHERE `sid`='$sid' AND `id`='$id'"; $db_results = Dba::write($sql); - - self::$total_objects = count($object_ids); } // save it return true; @@ -1237,24 +1276,12 @@ class Query { } // save_objects /** - * _auto_init - * this function reloads information back from the session - * it is called on creation of the class - */ - public static function _auto_init() { - - self::$offset = Config::get('offset_limit') ? Config::get('offset_limit') : '25'; - self::$_state = &$_SESSION['browse']; - - } // _auto_init - - /** * get_state * This is a debug only function */ - public static function get_state() { + public function get_state() { - return self::$_state; + return $this->_state; } // get_state diff --git a/lib/class/update.class.php b/lib/class/update.class.php index 80192049..e86c20ef 100644 --- a/lib/class/update.class.php +++ b/lib/class/update.class.php @@ -344,6 +344,9 @@ class Update { $update_string = '- Add uniqueness constraint to ratings.<br />'; $version[] = array('version' => '360004','description' => $update_string); + $update_string = '- Modify tmp_browse to allow caching of multiple browses per session.<br />'; + $version[] = array('version' => '360005','description' => $update_string); + return $version; } // populate_version @@ -1929,5 +1932,25 @@ class Update { self::set_version('db_version','360004'); } // update_360004 + /** + * update_360005 + * This changes the tmp_browse table around. + */ + public static function update_360005() { + $sql = "DROP TABLE `tmp_browse`"; + $db_results = Dba::write($sql); + + $sql = "CREATE TABLE `tmp_browse` (" . + "`id` int(13) NOT NULL auto_increment," . + "`sid` varchar(128) character set utf8 NOT NULL default ''," . + "`data` longtext collate utf8_unicode_ci NOT NULL," . + "`object_data` longtext collate utf8_unicode_ci," . + "PRIMARY KEY (`sid`,`id`)" . + ") ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; + $db_results = Dba::write($sql); + + self::set_version('db_version','360005'); + } // update_360005 + } // end update class ?> |