diff options
author | Paul 'flowerysong' Arthur <flowerysong00@yahoo.com> | 2010-06-22 19:00:45 +0000 |
---|---|---|
committer | Paul 'flowerysong' Arthur <flowerysong00@yahoo.com> | 2010-06-22 19:00:45 +0000 |
commit | a66bf4c5f663d16d6c2ceb4a6cb0a85f642dde43 (patch) | |
tree | 0eb8916039977a20e44260ab8fe522eae1522376 /lib/class | |
parent | 91eab2086875ad35aa93af00fc8b37d039d9f93c (diff) | |
download | ampache-a66bf4c5f663d16d6c2ceb4a6cb0a85f642dde43.tar.gz ampache-a66bf4c5f663d16d6c2ceb4a6cb0a85f642dde43.tar.bz2 ampache-a66bf4c5f663d16d6c2ceb4a6cb0a85f642dde43.zip |
Plugin work. Plugins are now pluggable: no plugin-specific code in the main Ampache
code. Plugins are now updatable, if configuration changes are needed for a new
version.
Diffstat (limited to 'lib/class')
-rw-r--r-- | lib/class/plugin.class.php | 91 | ||||
-rw-r--r-- | lib/class/preference.class.php | 12 | ||||
-rw-r--r-- | lib/class/rating.class.php | 7 | ||||
-rw-r--r-- | lib/class/user.class.php | 27 |
4 files changed, 92 insertions, 45 deletions
diff --git a/lib/class/plugin.class.php b/lib/class/plugin.class.php index 63eeb593..57157723 100644 --- a/lib/class/plugin.class.php +++ b/lib/class/plugin.class.php @@ -71,7 +71,7 @@ class Plugin { * get_plugins * This returns an array of plugin names */ - public static function get_plugins() { + public static function get_plugins($type='') { $results = array(); @@ -87,11 +87,24 @@ class Plugin { // Ignore non-plugin files if (substr($file,-10,10) != 'plugin.php') { continue; } if (is_dir($file)) { continue; } - - // It's a plugin record it $plugin_name = basename($file,'.plugin.php'); + if ($type != '') { + $plugin = new Plugin($plugin_name); + if (! Plugin::is_installed($plugin->_plugin->name)) { + debug_event('Plugins', 'Plugin ' . $plugin->_plugin->name . ' is not installed, skipping', 5); + continue; + } + if (! $plugin->is_valid()) { + debug_event('Plugins', 'Plugin ' . $plugin_name . ' is not valid, skipping', 5); + continue; + } + if (! method_exists($plugin->_plugin, $type)) { + debug_event('Plugins', 'Plugin ' . $plugin_name . ' does not support ' . $type . ', skipping', 5); + continue; + } + } + // It's a plugin record it $results[$plugin_name] = $plugin_name; - } // end while // Little stupid but hey @@ -104,9 +117,10 @@ class Plugin { /** * is_valid * This checks to make sure the plugin has the required functions and - * settings, Ampache requires Name/Description/Version (Int) and a - * install & uninstall method and Ampache must be within the min/max - * version specifications + * settings. Ampache requires public variables name, description, and + * version (as an int), and methods install, uninstall, and load. We + * also check that Ampache's database version falls within the min/max + * version specified by the plugin. */ function is_valid() { @@ -130,6 +144,10 @@ class Plugin { return false; } + if (!method_exists($this->_plugin,'load')) { + return false; + } + /* Make sure it's within the version confines */ $db_version = $this->get_ampache_db_version(); @@ -141,15 +159,15 @@ class Plugin { return false; } - /* We've passed all of the tests its good */ + // We've passed all of the tests return true; } // is_valid /** * is_installed - * This checks to see if the specified plugin is currently installed in the - * database, it doesn't check the files for integrity + * This checks to see if the specified plugin is currently installed in + * the database, it doesn't check the files for integrity */ public static function is_installed($plugin_name) { @@ -160,27 +178,22 @@ class Plugin { /** * install - * This runs the install function of the plugin (must be called install) - * at the end it inserts a row into the update_info table to indicate - * That it's installed + * This runs the install function of the plugin and inserts a row into + * the update_info table to indicate that it's installed. */ public function install() { + if ($this->_plugin->install() && + $this->set_plugin_version($this->_plugin->version)) { + return true; + } - $installed = $this->_plugin->install(); - - $version = $this->set_plugin_version($this->_plugin->version); - - if (!$installed OR !$version) { return false; } - - return true; - + return false; } // install /** * uninstall - * This runs the uninstall function of the plugin (must be called uninstall) - * at the end it removes the row from the update_info table to indicate - * that it isn't installed + * This runs the uninstall function of the plugin and removes the row + * from the update_info table to indicate that it isn't installed. */ public function uninstall() { @@ -191,6 +204,28 @@ class Plugin { } // uninstall /** + * upgrade + * This runs the upgrade function of the plugin (if it exists) and + * updates the database to indicate our new version. + */ + public function upgrade() { + if (method_exists($this->_plugin, 'upgrade')) { + if($this->_plugin->upgrade()) { + $this->set_plugin_version($this->_plugin->version); + } + } + } // upgrade + + /** + * load + * This calls the plugin's load function + */ + public function load() { + $GLOBALS['user']->set_preferences(); + return $this->_plugin->load(); + } + + /** * get_plugin_version * This returns the version of the specified plugin */ @@ -201,9 +236,11 @@ class Plugin { $sql = "SELECT * FROM `update_info` WHERE `key`='$name'"; $db_results = Dba::read($sql); - $results = Dba::fetch_assoc($db_results); + if ($results = Dba::fetch_assoc($db_results)) { + return $results['value']; + } - return $results['value']; + return false; } // get_plugin_version @@ -231,7 +268,7 @@ class Plugin { $name = Dba::escape('Plugin_' . $this->_plugin->name); $version = Dba::escape($version); - $sql = "INSERT INTO `update_info` SET `key`='$name', `value`='$version'"; + $sql = "REPLACE INTO `update_info` SET `key`='$name', `value`='$version'"; $db_results = Dba::read($sql); return true; diff --git a/lib/class/preference.class.php b/lib/class/preference.class.php index a6cf2d11..051e58d8 100644 --- a/lib/class/preference.class.php +++ b/lib/class/preference.class.php @@ -289,6 +289,18 @@ class Preference { } // delete /** + * rename + * This renames a preference in the database + */ + public static function rename($old, $new) { + $old = Dba::escape($old); + $new = Dba::escape($new); + + $sql = "UPDATE `preference` SET `name`='$new' WHERE `name`='$old'"; + $db_results = Dba::write($sql); + } + + /** * rebuild_preferences * This removes any garbage and then adds back in anything missing preferences wise */ diff --git a/lib/class/rating.class.php b/lib/class/rating.class.php index be0ee2f2..19550ca6 100644 --- a/lib/class/rating.class.php +++ b/lib/class/rating.class.php @@ -202,6 +202,13 @@ class Rating extends database_object { parent::add_to_cache('rating_' . $type . '_user' . $user_id, $id, $rating); + foreach (Plugin::get_plugins('save_rating') as $plugin_name) { + $plugin = new Plugin($plugin_name); + if ($plugin->load()) { + $plugin->_plugin->save_rating($this, $score); + } + } + return true; } // set_rating diff --git a/lib/class/user.class.php b/lib/class/user.class.php index 0c5c0653..de759378 100644 --- a/lib/class/user.class.php +++ b/lib/class/user.class.php @@ -564,31 +564,22 @@ class User extends database_object { if (!strlen($song_info->file)) { return false; } - // Make sure we didn't just play this song - $data = Stats::get_last_song($this->id); + // Make sure we've played a significant chunk of the song + $data = Stats::get_last_song($user); $last_song = new Song($data['object_id']); - if ($data['date']+($song_info->time/2) >= time()) { - debug_event('Stats','Not collecting stats less then 50% of song has elapsed','3'); + if ($data['date'] + ($song_info->time / 2) >= time()) { + debug_event('Stats','Not collecting stats less than 50% of song has elapsed','3'); return false; } $this->set_preferences(); - // Check if lastfm is loaded, if so run the update - if (Plugin::is_installed('Last.FM')) { - $lastfm = new Plugin('Lastfm'); - if ($lastfm->_plugin->load($this->prefs,$this->id)) { - $lastfm->_plugin->submit($song_info,$this->id); + foreach (Plugin::get_plugins('save_songplay') as $plugin_name) { + $plugin = new Plugin($plugin_name); + if ($plugin->load()) { + $plugin->_plugin->save_songplay($song_info); } - } // end if is_installed - - // Check and see if librefm is loaded and run scrobblizing - if (Plugin::is_installed('Libre.FM')) { - $librefm = new Plugin('Librefm'); - if ($librefm->_plugin->load($this->prefs,$this->id)) { - $librefm->_plugin->submit($song_info,$this->id); - } - } // end if is_installed + } // Do this last so the 'last played checks are correct' Stats::insert('song',$song_id,$user); |