diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2021-03-10 12:01:42 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2021-03-10 12:01:42 +0300 |
commit | 4a7daeacc5af75efe76a1c86244e7735a3ae73ef (patch) | |
tree | db5b5c3bd0cdb8ecfa8916b778dab212c298724a /lib | |
parent | 16c1f64f23be029ddcf3f064f3dd9df8f1bec7e6 (diff) |
Add a repair step for missing message-ids
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Db/MessageMapper.php | 15 | ||||
-rw-r--r-- | lib/Migration/AddMissingMessageIds.php | 73 |
2 files changed, 88 insertions, 0 deletions
diff --git a/lib/Db/MessageMapper.php b/lib/Db/MessageMapper.php index 2dfeeb9f3..796c2b551 100644 --- a/lib/Db/MessageMapper.php +++ b/lib/Db/MessageMapper.php @@ -926,4 +926,19 @@ class MessageMapper extends QBMapper { } return (int) $rows[0]['id']; } + + /** + * @return Message[] + */ + public function findWithEmptyMessageId(): array { + $qb = $this->db->getQueryBuilder(); + + $select = $qb->select('*') + ->from($this->getTableName()) + ->where( + $qb->expr()->isNull('message_id') + ); + + return $this->findEntities($select); + } } diff --git a/lib/Migration/AddMissingMessageIds.php b/lib/Migration/AddMissingMessageIds.php new file mode 100644 index 000000000..d17253103 --- /dev/null +++ b/lib/Migration/AddMissingMessageIds.php @@ -0,0 +1,73 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2021 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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/>. + */ + +namespace OCA\Mail\Migration; + +use OCA\Mail\Db\MessageMapper; +use OCA\Mail\Model\IMAPMessage; +use OCP\Migration\IOutput; +use OCP\Migration\IRepairStep; +use Psr\Log\LoggerInterface; +use function sprintf; + +class AddMissingMessageIds implements IRepairStep { + + /** @var MessageMapper */ + private $mapper; + + /** @var LoggerInterface */ + private $logger; + + public function __construct(MessageMapper $mapper, + LoggerInterface $logger) { + $this->mapper = $mapper; + $this->logger = $logger; + } + + public function getName() { + return 'Add a generated message-id to all Mail messages that have none'; + } + + public function run(IOutput $output) { + $output->info('Looking up messages without a message-id'); + $toFix = $this->mapper->findWithEmptyMessageId(); + $output->info(sprintf('%d messages need an update', count($toFix))); + $output->startProgress(count($toFix)); + foreach ($toFix as $message) { + $id = IMAPMessage::generateMessageId(); + $message->setMessageId($id); + + // The thread root is is null if the message wasn't matched to a thread + // based on its subject. In that case we set default for the thread root + // as well. + if ($message->getThreadRootId() === null) { + $message->setThreadRootId($id); + } + + $this->mapper->update($message); + $output->advance(); + } + } +} |