Skip to content

Commit 31e2243

Browse files
committed
listing remote restoringpoint
1 parent 0a21d66 commit 31e2243

13 files changed

+383
-52
lines changed

appinfo/info.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<description><![CDATA[Backup. Restore.]]></description>
77
<licence>agpl</licence>
88
<author mail="maxence@artificial-owl.com">Maxence Lange</author>
9-
<version>0.1.2</version>
9+
<version>0.1.4</version>
1010
<namespace>Backup</namespace>
1111
<category>tools</category>
1212
<website>https://github.com/nextcloud/backup</website>
@@ -27,6 +27,7 @@
2727
<command>OCA\Backup\Command\RemoteRemove</command>
2828

2929
<command>OCA\Backup\Command\RestoringBrowse</command>
30+
<command>OCA\Backup\Command\Reset</command>
3031

3132
<!-- <command>OCA\Backup\Command\Create</command>-->
3233
<!-- <command>OCA\Backup\Command\Listing</command>-->

lib/Command/RemoteAdd.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use OCA\Backup\AppInfo\Application;
4343
use OCA\Backup\Db\RemoteRequest;
4444
use OCA\Backup\Exceptions\RemoteInstanceDuplicateException;
45+
use OCA\Backup\Exceptions\RemoteInstanceException;
4546
use OCA\Backup\Exceptions\RemoteInstanceNotFoundException;
4647
use OCA\Backup\Exceptions\RemoteInstanceUidException;
4748
use OCA\Backup\Model\RemoteInstance;
@@ -106,9 +107,13 @@ protected function configure() {
106107
* @throws SignatoryException
107108
* @throws RemoteInstanceUidException
108109
* @throws RemoteInstanceDuplicateException
110+
* @throws RemoteInstanceException
109111
*/
110112
protected function execute(InputInterface $input, OutputInterface $output): int {
111113
$address = $input->getArgument('address');
114+
if ($address === RemoteInstance::LOCAL) {
115+
throw new RemoteInstanceException('\'local\' is a reserved name');
116+
}
112117

113118
$resource = $this->getCurrentResourceFromAddress($output, $address);
114119

lib/Command/Reset.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
/**
7+
* Nextcloud - Backup
8+
*
9+
* This file is licensed under the Affero General Public License version 3 or
10+
* later. See the COPYING file.
11+
*
12+
* @author Maxence Lange <maxence@artificial-owl.com>
13+
* @copyright 2021, Maxence Lange <maxence@artificial-owl.com>
14+
* @license GNU AGPL version 3 or any later version
15+
*
16+
* This program is free software: you can redistribute it and/or modify
17+
* it under the terms of the GNU Affero General Public License as
18+
* published by the Free Software Foundation, either version 3 of the
19+
* License, or (at your option) any later version.
20+
*
21+
* This program is distributed in the hope that it will be useful,
22+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
23+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24+
* GNU Affero General Public License for more details.
25+
*
26+
* You should have received a copy of the GNU Affero General Public License
27+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
28+
*
29+
*/
30+
31+
32+
namespace OCA\Backup\Command;
33+
34+
35+
use OC\Core\Command\Base;
36+
use OCA\Backup\Db\CoreRequestBuilder;
37+
use Symfony\Component\Console\Input\InputInterface;
38+
use Symfony\Component\Console\Input\InputOption;
39+
use Symfony\Component\Console\Output\OutputInterface;
40+
use Symfony\Component\Console\Question\ConfirmationQuestion;
41+
use Symfony\Component\Console\Question\Question;
42+
43+
44+
/**
45+
* Class Reset
46+
*
47+
* @package OCA\Backup\Command
48+
*/
49+
class Reset extends Base {
50+
51+
52+
/** @var CoreRequestBuilder */
53+
private $coreRequestBuilder;
54+
55+
56+
/**
57+
* Reset constructor.
58+
*
59+
* @param CoreRequestBuilder $coreRequestBuilder
60+
*/
61+
public function __construct(CoreRequestBuilder $coreRequestBuilder) {
62+
$this->coreRequestBuilder = $coreRequestBuilder;
63+
64+
parent::__construct();
65+
}
66+
67+
68+
/**
69+
*
70+
*/
71+
protected function configure() {
72+
$this->setName('backup:reset')
73+
->setDescription('Remove all data related to the Backup App')
74+
->addOption(
75+
'uninstall', '', InputOption::VALUE_NONE, 'Also uninstall the app from the instance'
76+
);
77+
}
78+
79+
80+
/**
81+
* @param InputInterface $input
82+
* @param OutputInterface $output
83+
*
84+
* @return int
85+
*/
86+
protected function execute(InputInterface $input, OutputInterface $output): int {
87+
$action = ($input->getOption('uninstall')) ? 'uninstall' : 'reset';
88+
89+
$output->writeln('');
90+
$output->writeln('');
91+
$output->writeln(
92+
'<error>WARNING! You are about to delete all data (and Restoring Point) related to the Backup App!</error>'
93+
);
94+
$question = new ConfirmationQuestion(
95+
'<comment>Do you really want to ' . $action . ' the Backup App?</comment> (y/N) ', false,
96+
'/^(y|Y)/i'
97+
);
98+
99+
$helper = $this->getHelper('question');
100+
if (!$helper->ask($input, $output, $question)) {
101+
$output->writeln('aborted.');
102+
103+
return 0;
104+
}
105+
106+
$output->writeln('');
107+
$output->writeln('<error>WARNING! This operation is not reversible.</error>');
108+
109+
$question = new Question(
110+
'<comment>Please confirm this destructive operation by typing \'' . $action
111+
. '\'</comment>: ', ''
112+
);
113+
114+
$helper = $this->getHelper('question');
115+
$confirmation = $helper->ask($input, $output, $question);
116+
if (strtolower($confirmation) !== $action) {
117+
$output->writeln('aborted.');
118+
119+
return 0;
120+
}
121+
122+
$this->coreRequestBuilder->cleanDatabase();
123+
if ($action === 'uninstall') {
124+
$this->coreRequestBuilder->uninstall();
125+
}
126+
127+
$output->writeln('<info>' . $action . ' done</info>');
128+
129+
return 0;
130+
}
131+
132+
}

lib/Command/RestoringBrowse.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ class RestoringBrowse extends Base {
5959
use TNC23Deserialize;
6060

6161

62-
const LOCAL = 'local';
63-
64-
6562
/** @var RemoteRequest */
6663
private $remoteRequest;
6764

@@ -101,7 +98,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
10198
$output->writeln('');
10299
$output->writeln('Browsing available Restoring Point on <info>' . $instance . '</info>');
103100

104-
$rp = $this->getRestoringPoint($instance, ($instance === self::LOCAL));
101+
$rp = $this->getRestoringPoint($instance, ($instance === RemoteInstance::LOCAL));
105102
echo 'RP: ' . json_encode($rp, JSON_PRETTY_PRINT);
106103

107104
return 0;
@@ -131,7 +128,7 @@ function (RemoteInstance $remoteInstance): ?string {
131128
$helper = $this->getHelper('question');
132129
$question = new ChoiceQuestion(
133130
'Which location to browse ?',
134-
array_merge([self::LOCAL], $remote),
131+
array_merge([RemoteInstance::LOCAL], $remote),
135132
0
136133
);
137134
$question->setErrorMessage('Instance %s is not known.');

lib/Controller/RemoteController.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use ArtificialOwl\MySmallPhpTools\Model\Nextcloud\nc23\NC23SignedRequest;
4141
use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Controller;
4242
use Exception;
43+
use OC;
4344
use OC\AppFramework\Middleware\Security\Exceptions\NotLoggedInException;
4445
use OCA\Backup\Exceptions\RemoteRequestException;
4546
use OCA\Backup\IRemoteRequest;
@@ -50,6 +51,8 @@
5051
use OCP\AppFramework\Http;
5152
use OCP\AppFramework\Http\DataResponse;
5253
use OCP\IRequest;
54+
use ReflectionClass;
55+
use ReflectionException;
5356

5457

5558
/**
@@ -164,15 +167,25 @@ private function extractRequest(string $class = ''): ?IRemoteRequest {
164167
return null;
165168
}
166169

167-
$request = new $class();
168-
if (!($request instanceof IRemoteRequest)) {
169-
throw new RemoteRequestException('invalid class ' . $class);
170+
try {
171+
$test = new ReflectionClass($class);
172+
} catch (ReflectionException $e) {
173+
throw new RemoteRequestException('ReflectionException with ' . $class . ': ' . $e->getMessage());
174+
}
175+
176+
if (!in_array(IRemoteRequest::class, $test->getInterfaceNames())) {
177+
throw new RemoteRequestException($class . ' does not implements IRemoteRequest');
178+
}
179+
180+
$item = OC::$server->get($class);
181+
if (!($item instanceof IRemoteRequest)) {
182+
throw new RemoteRequestException($class . ' not an IRemoteRequest');
170183
}
171184

172-
$request->import(json_decode($signed->getBody(), true));
173-
$request->setSignedRequest($signed);
185+
$item->import(json_decode($signed->getBody(), true));
186+
$item->setSignedRequest($signed);
174187

175-
return $request;
188+
return $item;
176189
}
177190

178191

lib/Db/CoreQueryBuilder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131

3232
namespace OCA\Backup\Db;
3333

34-
use ArtificialOwl\MySmallPhpTools\Db\Nextcloud\nc22\NC22ExtendedQueryBuilder;
34+
use ArtificialOwl\MySmallPhpTools\Db\Nextcloud\nc23\NC23ExtendedQueryBuilder;
3535

3636

3737
/**
3838
* Class CoreQueryBuilder
3939
*
4040
* @package OCA\Backup\Db
4141
*/
42-
class CoreQueryBuilder extends NC22ExtendedQueryBuilder {
42+
class CoreQueryBuilder extends NC23ExtendedQueryBuilder {
4343

4444

4545
/**
@@ -61,10 +61,10 @@ public function limitToId(int $id): void {
6161

6262

6363
/**
64-
* @param string $host
64+
* @param string $instance
6565
*/
66-
public function limitToInstance(string $host): void {
67-
$this->limit('instance', $host, '', false);
66+
public function limitToInstance(string $instance): void {
67+
$this->limit('instance', $instance, '', false);
6868
}
6969

7070
}

lib/Db/CoreRequestBuilder.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,11 @@ class CoreRequestBuilder {
6161
],
6262
self::TABLE_RESTORING_POINT => [
6363
'id',
64-
'user_id',
65-
'preferred_username',
66-
'name',
67-
'summary',
68-
'sa.public_key',
69-
'avatar_version',
70-
'private_key',
71-
'creation'
64+
'uid',
65+
'instance',
66+
'status',
67+
'metadata',
68+
'date'
7269
]
7370
];
7471

@@ -141,7 +138,6 @@ public function uninstallFromMigrations() {
141138
$qb = $this->getQueryBuilder();
142139
$qb->delete('migrations');
143140
$qb->limit('app', 'backup');
144-
$qb->unlike('version', '001%');
145141

146142
$qb->execute();
147143
}

lib/Db/BackupsRequest.php renamed to lib/Db/PointRequest.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@
3232

3333

3434
use OCA\Backup\Model\Backup;
35+
use OCA\Backup\Model\RestoringPoint;
3536

3637

3738
/**
3839
* Class BackupsRequest
3940
*
4041
* @package OCA\Backup\Db
4142
*/
42-
class BackupsRequest extends BackupsRequestBuilder {
43+
class PointRequest extends PointRequestBuilder {
4344

4445

4546
/**
@@ -50,8 +51,7 @@ class BackupsRequest extends BackupsRequestBuilder {
5051
* @return int
5152
*/
5253
public function create(Backup $backup): int {
53-
54-
$qb = $this->getBackupsInsertSql();
54+
$qb = $this->getPointInsertSql();
5555

5656
// $qb->setValue('id', $qb->createNamedParameter($id))
5757
//// ->setValue('type', $qb->createNamedParameter($actor->getType()))
@@ -71,10 +71,23 @@ public function create(Backup $backup): int {
7171
* @param Backup $backup
7272
*/
7373
public function update(Backup $backup) {
74-
$qb = $this->getBackupsUpdateSql();
75-
$qb->limitToId($backup->getId());
74+
// $qb = $this->getPointUpdateSql();
75+
// $qb->limitToId($backup->getId());
76+
//
77+
// $qb->execute();
78+
}
79+
80+
81+
/**
82+
* @param string $instance
83+
*
84+
* @return RestoringPoint[]
85+
*/
86+
public function getByInstance(string $instance): array {
87+
$qb = $this->getPointSelectSql();
88+
$qb->limitToInstance($instance);
7689

77-
$qb->execute();
90+
return $this->getItemsFromRequest($qb);
7891
}
7992

8093
}

0 commit comments

Comments
 (0)