Skip to content

Commit

Permalink
Introduce command to drop gathered metadata
Browse files Browse the repository at this point in the history
* occ music:reset-database
  • Loading branch information
MorrisJobke committed Dec 17, 2014
1 parent 81fd0f9 commit 7d39a0b
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 3 deletions.
18 changes: 16 additions & 2 deletions README.md
Expand Up @@ -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.

Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion appframework/core/db.php
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions appinfo/register_command.php
Expand Up @@ -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));
92 changes: 92 additions & 0 deletions command/resetdatabase.php
@@ -0,0 +1,92 @@
<?php

/**
* ownCloud - Music app
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Morris Jobke <hey@morrisjobke.de>
* @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 <info>all users</info>");
$this->dropTables();
} else {
$users = $input->getArgument('user_id');
foreach($users as $user) {
$output->writeln("Drop tables for <info>$user</info>");
$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();
}

}

0 comments on commit 7d39a0b

Please sign in to comment.