1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ownCloud - Music app |
5
|
|
|
* |
6
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
7
|
|
|
* later. See the COPYING file. |
8
|
|
|
* |
9
|
|
|
* @author Morris Jobke <[email protected]> |
10
|
|
|
* @copyright Morris Jobke 2014 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace OCA\Music\Command; |
14
|
|
|
|
15
|
|
|
use OCP\IDBConnection; |
16
|
|
|
use Symfony\Component\Console\Command\Command; |
17
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Input\InputOption; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
class ResetDatabase extends Command { |
24
|
|
|
|
25
|
|
|
/** @var IDBConnection */ |
26
|
|
|
private $db; |
27
|
|
|
|
28
|
|
|
public function __construct(IDBConnection $db) { |
29
|
|
|
$this->db = $db; |
30
|
|
|
parent::__construct(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function configure() { |
34
|
|
|
$this |
35
|
|
|
->setName('music:reset-database') |
36
|
|
|
->setDescription('will drop all metadata gathered by the music app (artists, albums, tracks, playlists)') |
37
|
|
|
->addArgument( |
38
|
|
|
'user_id', |
39
|
|
|
InputArgument::OPTIONAL | InputArgument::IS_ARRAY, |
40
|
|
|
'specify the user' |
41
|
|
|
) |
42
|
|
|
->addOption( |
43
|
|
|
'all', |
44
|
|
|
null, |
45
|
|
|
InputOption::VALUE_NONE, |
46
|
|
|
'use all known users' |
47
|
|
|
) |
48
|
|
|
; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
52
|
|
|
if ($input->getOption('all')) { |
53
|
|
|
$output->writeln("Drop tables for <info>all users</info>"); |
54
|
|
|
$this->dropTables(); |
55
|
|
|
} else { |
56
|
|
|
$users = $input->getArgument('user_id'); |
57
|
|
|
foreach($users as $user) { |
58
|
|
|
$output->writeln("Drop tables for <info>$user</info>"); |
59
|
|
|
$this->dropTables($user); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function dropTables($userID=null) { |
65
|
|
|
$tables = array('tracks', 'albums', 'artists', 'playlists'); |
66
|
|
|
foreach($tables as $table) { |
67
|
|
|
$sql = 'DELETE FROM `*PREFIX*music_' . $table . '` '; |
68
|
|
|
$params = array(); |
69
|
|
|
if($userID) { |
70
|
|
|
$sql .= 'WHERE `user_id` = ?'; |
71
|
|
|
$params[] = $userID; |
72
|
|
|
} |
73
|
|
|
$query = $this->db->prepare($sql); |
74
|
|
|
$query->execute($params); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|