Failed Conditions
Pull Request — master (#72)
by Sander
03:09
created

migration/migrategroups.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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->migrateGroups();
68
	}
69
70
	private function fetchAll($sql) {
71
		return $this->db->executeQuery($sql)->fetchAll();
72
	}
73
74
	private function migrateGroups() {
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
				if (!$user['uid']) {
79
					die('err');
0 ignored issues
show
Coding Style Compatibility introduced by
The method migrateGroups() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
80
				}
81
				$user = $user['uid'];
82
				$groups = $this->fetchAll('SELECT DISTINCT(grouping) FROM `*PREFIX*nextnote` WHERE uid = "' . $user . '"');
83
				foreach ($groups as $group) {
84
					if ($group['grouping']) {
85
						$g = new Notebook();
86
						$g->setName($group['grouping']);
87
						$g->setUid($user);
88
						$g->setGuid(Utils::GUID());
89
						$this->groupService->create($g, $user);
90
						//$this->db->executeQuery("UPDATE `*PREFIX*nextnote` set grouping =". $g->getId() ." WHERE grouping = \"". $group['grouping'] ."\"");
91
					}
92
				}
93
			}
94
			$notes = $this->fetchAll('SELECT * FROM *PREFIX*nextnote order by id ASC');
95
			$maxId = 0;
96
			foreach($notes as $n){
97
				if($n['id'] > $maxId){
98
					$maxId = $n['id'];
99
				}
100
101
				$note = new Note();
102
				$note->setId($n['id']);
103
				$note->setGuid(Utils::GUID());
104
				$note->setUid($n['uid']);
105
				$note->setName($n['name']);
106
				$note->setMtime($n['mtime']);
107
				$note->setDeleted($n['deleted']);
108
				$note->setGrouping($n['grouping']);
109
				$this->noteService->create($note);
110
			}
111
			$maxId++;
112
			$this->db->executeQuery('ALTER TABLE *PREFIX*nextnote_notes AUTO_INCREMENT='. $maxId);
113
			$this->db->executeQuery('DROP TABLE *PREFIX*nextnote');
114
		}
115
	}
116
117
}
118