diff --git a/README.md b/README.md index 3968a6e43..46811ff58 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,11 @@ ### Commands -Following command is available (see script occ in your ownCloud root folder) +Following commands are available(see script occ in your ownCloud root folder): - ./occ music:scan USERNAME +#### Scan music files + + ./occ music:scan USERNAME1 USERNAME2 ... This scans all not scanned music files of the user USERNAME and saves the extracted metadata into the music tables in the database. This is also done if you browse the music app web interface. There the scan is done in steps of 20 tracks and the current state is visible at the bottom of the interface. @@ -19,6 +21,18 @@ This scans music files for all users. Both of the above commands can be combined with the `--debug` switch, which enables debug output and shows the memory usage of each scan step. +#### Reset scanned metadata + +**Warning:** This command will delete data! It will remove unavailable tracks from playlists as playlists are linked against the track metadata. + + ./occ music:reset-database USERNAME1 USERNAME2 ... + +This will reset the scanned metadata of the provided users. + + ./occ music:reset-database --all + +This will reset the scanned metadata of all users. + ### Ampache In the settings the URL you need for Ampache is listed and looks like this: diff --git a/appframework/core/db.php b/appframework/core/db.php index 44b21f886..75bca30f7 100644 --- a/appframework/core/db.php +++ b/appframework/core/db.php @@ -21,7 +21,7 @@ class Db { * @param string $sql the sql query with ? placeholder for params * @param int $limit the maximum number of rows * @param int $offset from which row we want to start - * @return \OCP\DB a query object + * @return \OC_DB_StatementWrapper a query object */ public function prepareQuery($sql, $limit=null, $offset=null){ return \OCP\DB::prepare($sql, $limit, $offset); diff --git a/appinfo/register_command.php b/appinfo/register_command.php index bb9f4b6b3..9e6ece18b 100644 --- a/appinfo/register_command.php +++ b/appinfo/register_command.php @@ -19,5 +19,7 @@ $userManager = $c->getServer()->getUserManager(); $scanner = $c->query('Scanner'); $rootFolder = $c->query('RootFolder'); +$db = $c->query('Db'); $application->add(new OCA\Music\Command\Scan($userManager, $scanner, $rootFolder)); +$application->add(new OCA\Music\Command\ResetDatabase($db)); diff --git a/command/resetdatabase.php b/command/resetdatabase.php new file mode 100644 index 000000000..77484a3fd --- /dev/null +++ b/command/resetdatabase.php @@ -0,0 +1,92 @@ + + * @copyright Morris Jobke 2014 + */ + +namespace OCA\Music\Command; + +use OCA\Music\AppFramework\Core\Db; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + + +class ResetDatabase extends Command { + + /** @var Db */ + private $db; + + public function __construct(Db $db) { + $this->db = $db; + parent::__construct(); + } + + protected function configure() { + $this + ->setName('music:reset-database') + ->setDescription('will drop all metadata gathered by the music app (artists, albums, tracks)') + ->addArgument( + 'user_id', + InputArgument::OPTIONAL | InputArgument::IS_ARRAY, + 'specify the user' + ) + ->addOption( + 'all', + null, + InputOption::VALUE_NONE, + 'use all known users' + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) { + if ($input->getOption('all')) { + $output->writeln("Drop tables for all users"); + $this->dropTables(); + } else { + $users = $input->getArgument('user_id'); + foreach($users as $user) { + $output->writeln("Drop tables for $user"); + $this->dropTables($user); + } + } + + $output->writeln("Clean up relations (album/artist and playlist/track)"); + $this->cleanupRelation(); + } + + private function dropTables($userID=null) { + $tables = array('tracks', 'albums', 'artists'); + foreach($tables as $table) { + $sql = 'DELETE FROM `*PREFIX*music_' . $table . '` '; + $params = array(); + if($userID) { + $sql .= 'WHERE `user_id` = ?'; + $params[] = $userID; + } + $query = $this->db->prepareQuery($sql); + $query->execute($params); + } + } + + private function cleanupRelation() { + $sql = 'DELETE FROM `*PREFIX*music_album_artists` ' . + 'WHERE `album_id` NOT IN (SELECT `id` FROM `*PREFIX*music_albums`) ' . + 'OR `artist_id` NOT IN (SELECT `id` FROM `*PREFIX*music_artists`)'; + $this->db->prepareQuery($sql)->execute(); + + $sql = 'DELETE FROM `*PREFIX*music_playlist_tracks` ' . + 'WHERE `track_id` NOT IN (SELECT `id` FROM `*PREFIX*music_tracks`)'; + $this->db->prepareQuery($sql)->execute(); + } + +}