Completed
Pull Request — master (#67)
by Joas
02:50
created

UpdateShareTimeToTimestamp   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 80
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 3 1
A run() 0 3 1
B updateShares() 0 34 3
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 *
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\Circles\Migration;
25
26
use OCA\Circles\Model\Circle;
27
use OCA\Circles\Model\Member;
28
use OCP\IConfig;
29
use OCP\IDBConnection;
30
use OCP\Migration\IOutput;
31
use OCP\Migration\IRepairStep;
32
use OCP\Share;
33
34
/**
35
 * Class UpdateShareTimeToTimestamp
36
 *
37
 * @package OCA\Circles\Migration
38
 */
39
class UpdateShareTimeToTimestamp implements IRepairStep {
40
41
	/** @var IDBConnection */
42
	protected $connection;
43
44
	/** @var  IConfig */
45
	protected $config;
46
47
	/** @var array */
48
	protected $circlesById = [];
49
	/** @var array */
50
	protected $circlesByUri = [];
51
	/** @var array */
52
	protected $circleHasAdmin = [];
53
54
	public function __construct(IDBConnection $connection, IConfig $config) {
55
		$this->connection = $connection;
56
		$this->config = $config;
57
	}
58
59
	/**
60
	 * Returns the step's name
61
	 *
62
	 * @return string
63
	 * @since 9.1.0
64
	 */
65
	public function getName() {
66
		return 'Fix the shares to use timestamp instead of datetime';
67
	}
68
69
	/**
70
	 * @param IOutput $output
71
	 */
72
	public function run(IOutput $output) {
73
		$this->updateShares($output);
74
	}
75
76
	/**
77
	 * Update shares
78
	 * - type 7 instead of 1
79
	 * - with circle ID instead of `customgroup_` + group URI
80
	 *
81
	 * @param IOutput $output
82
	 */
83
	public function updateShares(IOutput $output) {
84
		$output->info('Update timestamp of shares');
85
86
		$select = $this->connection->getQueryBuilder();
87
		$select->select('*')
88
			->from('share')
89
			->where($select->expr()->eq('share_type', $select->createNamedParameter(Share::SHARE_TYPE_CIRCLE)));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 103 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
90
91
		$update = $this->connection->getQueryBuilder();
92
		$update->update('share')
93
			->set('stime', $update->createParameter('time'))
94
			->where($update->expr()->eq('id', $update->createParameter('id')));
95
96
		$output->startProgress();
97
		$result = $select->execute();
98
99
		while ($row = $result->fetch()) {
100
			$dateTime = \DateTime::createFromFormat('YmdHis', $row['stime']);
101
102
			if ($dateTime === false) {
103
				// Not the invalid format
104
				continue;
105
			}
106
107
			$update->setParameter('time', $dateTime->getTimestamp())
108
				->setParameter('id', $row['id']);
109
110
			$update->execute();
111
			$output->advance();
112
		}
113
114
		$result->closeCursor();
115
		$output->finishProgress();
116
	}
117
118
}
119