2018-10-26 19:13:19 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
|
|
|
|
*
|
|
|
|
* @author John Molakvoæ <skjnldsv@protonmail.com>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-10-26 19:27:39 +03:00
|
|
|
namespace OC\Repair\NC15;
|
2018-10-26 19:13:19 +03:00
|
|
|
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\Migration\IOutput;
|
|
|
|
use OCP\Migration\IRepairStep;
|
2018-10-30 16:03:41 +03:00
|
|
|
use Sabre\VObject\Reader;
|
2018-10-26 19:13:19 +03:00
|
|
|
|
|
|
|
class SetVcardDatabaseUID implements IRepairStep {
|
|
|
|
const MAX_ROWS = 1000;
|
|
|
|
|
|
|
|
/** @var IDBConnection */
|
|
|
|
private $connection;
|
2018-10-26 19:27:39 +03:00
|
|
|
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
|
2018-10-26 19:13:19 +03:00
|
|
|
private $updateQuery;
|
|
|
|
|
2018-10-26 19:27:39 +03:00
|
|
|
public function __construct(IDBConnection $connection, IConfig $config) {
|
2018-10-26 19:13:19 +03:00
|
|
|
$this->connection = $connection;
|
2018-10-26 19:27:39 +03:00
|
|
|
$this->config = $config;
|
2018-10-26 19:13:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getName() {
|
|
|
|
return 'Extract the vcard uid and store it in the db';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Generator
|
|
|
|
* @suppress SqlInjectionChecker
|
|
|
|
*/
|
|
|
|
private function getInvalidEntries() {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
$builder->select('id', 'carddata')
|
2018-10-26 19:27:39 +03:00
|
|
|
->from('cards')
|
|
|
|
->where($builder->expr()->isNull('uid'))
|
|
|
|
->setMaxResults(self::MAX_ROWS);
|
2018-10-26 19:13:19 +03:00
|
|
|
|
|
|
|
do {
|
|
|
|
$result = $builder->execute();
|
2018-10-26 19:27:39 +03:00
|
|
|
$rows = $result->fetchAll();
|
2018-10-26 19:13:19 +03:00
|
|
|
foreach ($rows as $row) {
|
|
|
|
yield $row;
|
|
|
|
}
|
|
|
|
$result->closeCursor();
|
|
|
|
} while (count($rows) > 0);
|
|
|
|
}
|
|
|
|
|
2018-10-30 16:03:41 +03:00
|
|
|
/**
|
|
|
|
* Extract UID from vcard
|
|
|
|
*
|
|
|
|
* @param string $cardData the vcard raw data
|
|
|
|
* @return string the uid or empty if none
|
|
|
|
*/
|
|
|
|
private function getUID(string $cardData): string {
|
|
|
|
$vCard = Reader::read($cardData);
|
|
|
|
if ($vCard->UID) {
|
|
|
|
$uid = $vCard->UID->getValue();
|
|
|
|
return $uid;
|
2018-10-26 19:13:19 +03:00
|
|
|
}
|
2018-10-26 19:27:39 +03:00
|
|
|
|
2018-10-30 16:03:41 +03:00
|
|
|
return '';
|
2018-10-26 19:13:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $id
|
|
|
|
* @param string $uid
|
|
|
|
*/
|
2018-10-30 16:03:41 +03:00
|
|
|
private function update(int $id, string $uid) {
|
2018-10-26 19:13:19 +03:00
|
|
|
if (!$this->updateQuery) {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
$this->updateQuery = $builder->update('cards')
|
2018-10-26 19:27:39 +03:00
|
|
|
->set('uid', $builder->createParameter('uid'))
|
|
|
|
->where($builder->expr()->eq('id', $builder->createParameter('id')));
|
2018-10-26 19:13:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->updateQuery->setParameter('id', $id);
|
|
|
|
$this->updateQuery->setParameter('uid', $uid);
|
|
|
|
|
|
|
|
$this->updateQuery->execute();
|
|
|
|
}
|
|
|
|
|
2018-10-30 16:03:41 +03:00
|
|
|
private function repair(): int {
|
2018-10-26 19:13:19 +03:00
|
|
|
$this->connection->beginTransaction();
|
|
|
|
$entries = $this->getInvalidEntries();
|
2018-10-26 19:27:39 +03:00
|
|
|
$count = 0;
|
2018-10-26 19:13:19 +03:00
|
|
|
foreach ($entries as $entry) {
|
|
|
|
$count++;
|
2018-11-13 16:26:33 +03:00
|
|
|
$cardData = $entry['carddata'];
|
|
|
|
if (is_resource($cardData)) {
|
|
|
|
$cardData = stream_get_contents($cardData);
|
|
|
|
}
|
|
|
|
$uid = $this->getUID($cardData);
|
2018-10-30 16:03:41 +03:00
|
|
|
$this->update($entry['id'], $uid);
|
2018-10-26 19:13:19 +03:00
|
|
|
}
|
|
|
|
$this->connection->commit();
|
2018-10-26 19:27:39 +03:00
|
|
|
|
2018-10-26 19:13:19 +03:00
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function shouldRun() {
|
2018-10-26 19:27:39 +03:00
|
|
|
$versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0.0');
|
2018-10-26 19:13:19 +03:00
|
|
|
|
2018-10-26 19:27:39 +03:00
|
|
|
// was added to 15.0.0.2
|
2018-10-30 16:05:34 +03:00
|
|
|
return version_compare($versionFromBeforeUpdate, '15.0.0.2', '<=');
|
2018-10-26 19:13:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function run(IOutput $output) {
|
|
|
|
if ($this->shouldRun()) {
|
|
|
|
$count = $this->repair();
|
|
|
|
|
|
|
|
$output->info('Fixed ' . $count . ' vcards');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|