Completed
Push — php-7.1 ( 77242e...1bf6d8 )
by Pauli
14:40
created

ResetDatabase::cleanupRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
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