MigrateGroups   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 0
loc 77
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B doMigration() 0 39 7
A __construct() 0 7 1
A getName() 0 3 1
A run() 0 4 1
A fetchAll() 0 3 1
1
<?php
2
/**
3
 * Nextcloud - namespace OCA\Nextnote
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
6
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\NextNote\Migration;
25
26
27
use OCA\NextNote\Db\Notebook;
28
use OCA\NextNote\Db\Note;
29
use OCA\NextNote\Service\NotebookService;
30
use OCA\NextNote\Service\NoteService;
31
use OCA\NextNote\Utility\Utils;
32
use OCP\IDBConnection;
33
use OCP\ILogger;
34
use OCP\Migration\IOutput;
35
use OCP\Migration\IRepairStep;
36
37
38
class MigrateGroups implements IRepairStep {
39
40
41
	/** @var IDBConnection */
42
	private $db;
43
44
	/** @var string */
45
	private $installedVersion;
46
47
	/** @var ILogger */
48
	private $logger;
49
	private $groupService;
50
	private $noteService;
51
52
53
	public function __construct(IDBConnection $db, ILogger $logger, NotebookService $groupService, NoteService $noteService) {
54
		$this->db = $db;
55
		$this->logger = $logger;
56
		$this->installedVersion = \OC::$server->getConfig()->getAppValue('nextnote', 'installed_version');
57
		$this->groupService = $groupService;
58
		$this->noteService = $noteService;
59
	}
60
61
	public function getName() {
62
		return 'Migrating groups';
63
	}
64
65
	public function run(IOutput $output) {
66
		$output->info('Migrating groups');
67
		$this->doMigration();
68
	}
69
70
	private function fetchAll($sql) {
71
		return $this->db->executeQuery($sql)->fetchAll();
72
	}
73
74
	private function doMigration() {
75
		if (version_compare($this->installedVersion, '1.2.3', '<')) {
76
			$users = $this->fetchAll('SELECT DISTINCT(uid) FROM `*PREFIX*nextnote`');
77
			foreach ($users as $user) {
78
				$user = $user['uid'];
79
				$groups = $this->fetchAll('SELECT DISTINCT(grouping) FROM `*PREFIX*nextnote` WHERE uid="' . $user . '"');
80
				foreach ($groups as $group) {
81
					if ($group['grouping']) {
82
						$g = new Notebook();
83
						$g->setName($group['grouping']);
84
						$g->setUid($user);
85
						$g->setGuid(Utils::GUID());
86
						$this->groupService->create($g, $user);
87
						//$this->db->executeQuery("UPDATE `*PREFIX*nextnote` set grouping =". $g->getId() ." WHERE grouping = \"". $group['grouping'] ."\"");
88
					}
89
				}
90
			}
91
			$notes = $this->fetchAll('SELECT * FROM *PREFIX*nextnote order by id ASC');
92
			$maxId = 0;
93
			foreach($notes as $n){
94
				if($n['id'] > $maxId){
95
					$maxId = $n['id'];
96
				}
97
				$notebook = $this->groupService->findByName($n['grouping']);
98
				$note = new Note();
99
				$note->setId($n['id']);
100
				$note->setGuid(Utils::GUID());
101
				$note->setUid($n['uid']);
102
				$note->setName($n['name']);
103
				$note->setMtime($n['mtime']);
104
				$note->setDeleted($n['deleted']);
105
				$note->setNotebook($notebook->getId());
106
				$this->noteService->create($note);
107
			}
108
			$maxId++;
109
			$this->db->executeQuery('ALTER TABLE *PREFIX*nextnote_notes AUTO_INCREMENT='. $maxId);
110
			$this->db->executeQuery('DROP TABLE *PREFIX*nextnote');
111
		}
112
	}
113
114
}
115