From f6201fd711b06daf1e99ed28439fb00c5b5a87b0 Mon Sep 17 00:00:00 2001 From: Kostiantyn Miakshyn Date: Fri, 20 Dec 2024 12:38:57 +0100 Subject: [PATCH] Slugify Collectives and Pages (move slug generation into a separate console command) Signed-off-by: Kostiantyn Miakshyn --- appinfo/info.xml | 3 +- lib/Command/GenerateSlugs.php | 106 ++++++++++++++++++ .../Version021500Date20240820000000.php | 76 ------------- .../Version021500Date20240820000001.php | 95 ---------------- .../Version021600Date20240820000000.php | 37 ++++++ .../Version021600Date20240820000001.php | 36 ++++++ 6 files changed, 181 insertions(+), 172 deletions(-) create mode 100644 lib/Command/GenerateSlugs.php delete mode 100644 lib/Migration/Version021500Date20240820000000.php delete mode 100644 lib/Migration/Version021500Date20240820000001.php create mode 100644 lib/Migration/Version021600Date20240820000000.php create mode 100644 lib/Migration/Version021600Date20240820000001.php diff --git a/appinfo/info.xml b/appinfo/info.xml index 291fab648..bab6aedbe 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -26,7 +26,7 @@ In your Nextcloud instance, simply navigate to **»Apps«**, find the **»Teams«** and **»Collectives«** apps and enable them. ]]> - 2.15.2 + 2.16.0 agpl CollectiveCloud Team Collectives @@ -59,6 +59,7 @@ In your Nextcloud instance, simply navigate to **»Apps«**, find the OCA\Collectives\Command\CreateCollective OCA\Collectives\Command\ExpirePageVersions + OCA\Collectives\Command\GenerateSlugs OCA\Collectives\Command\IndexCollectives OCA\Collectives\Command\PageTrashCleanup OCA\Collectives\Command\PurgeObsoletePages diff --git a/lib/Command/GenerateSlugs.php b/lib/Command/GenerateSlugs.php new file mode 100644 index 000000000..7a49d6538 --- /dev/null +++ b/lib/Command/GenerateSlugs.php @@ -0,0 +1,106 @@ +setName('collectives:generate-slugs') + ->setDescription('Generate slugs for collectives and pages'); + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $output->write('Generating slugs for collectives ... '); + $this->generateCollectiveSlugs(); + $output->writeln('done'); + + $output->write('Generating slugs for pages ... '); + $this->generatePageSlugs(); + $output->writeln('done'); + + return 0; + } + + private function generateCollectiveSlugs(): void { + $query = $this->connection->getQueryBuilder(); + $query->select(['id', 'circle_unique_id']) + ->from('collectives') + ->where('(slug IS NULL OR slug = \'\')'); + $result = $query->executeQuery(); + + $update = $this->connection->getQueryBuilder(); + $update->update('collectives') + ->set('slug', $update->createParameter('slug')) + ->where($update->expr()->eq('id', $update->createParameter('id'))); + + while ($row = $result->fetch()) { + $circle = $this->circleHelper->getCircle($row['circle_unique_id'], null, true); + $slug = $this->slugService->generateCollectiveSlug($row['id'], $circle->getSanitizedName()); + + $update + ->setParameter('id', (int)$row['id'], IQueryBuilder::PARAM_INT) + ->setParameter('slug', $slug, IQueryBuilder::PARAM_STR) + ->executeStatement(); + } + $result->closeCursor(); + } + + private function generatePageSlugs(): void { + $queryCollectives = $this->connection->getQueryBuilder(); + $queryCollectives->select(['id', 'circle_unique_id']) + ->from('collectives') + ->where('trash_timestamp IS NULL'); + $resultCollectives = $queryCollectives->executeQuery(); + + $update = $this->connection->getQueryBuilder(); + $update->update('collectives_pages') + ->set('slug', $update->createParameter('slug')) + ->where($update->expr()->eq('file_id', $update->createParameter('file_id'))); + + while ($rowCollective = $resultCollectives->fetch()) { + $circle = $this->circleHelper->getCircle($rowCollective['circle_unique_id'], null, true); + $pageInfos = $this->pageService->findAll($rowCollective['id'], $circle->getOwner()->getUserId()); + + foreach ($pageInfos as $pageInfo) { + if ($pageInfo->getFileName() === PageInfo::INDEX_PAGE_TITLE . PageInfo::SUFFIX) { + continue; + } + + $slug = $this->slugService->generatePageSlug($pageInfo->getTitle()); + $update + ->setParameter('file_id', $pageInfo->getId(), IQueryBuilder::PARAM_INT) + ->setParameter('slug', $slug, IQueryBuilder::PARAM_STR) + ->executeStatement(); + } + } + + $resultCollectives->closeCursor(); + } +} diff --git a/lib/Migration/Version021500Date20240820000000.php b/lib/Migration/Version021500Date20240820000000.php deleted file mode 100644 index 1dfeec72e..000000000 --- a/lib/Migration/Version021500Date20240820000000.php +++ /dev/null @@ -1,76 +0,0 @@ -getTable('collectives'); - if (!$table->hasColumn('slug')) { - $this->runSlugGeneration = true; - $table->addColumn('slug', Types::STRING, [ - 'notnull' => false, - 'default' => false, - 'length' => 255, - ]); - - return $schema; - } - - return null; - } - - public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { - if (!$this->runSlugGeneration) { - return; - } - - $query = $this->connection->getQueryBuilder(); - $query->select(['id', 'circle_unique_id'])->from('collectives'); - $result = $query->executeQuery(); - - $update = $this->connection->getQueryBuilder(); - $update->update('collectives') - ->set('slug', $update->createParameter('slug')) - ->where($update->expr()->eq('id', $update->createParameter('id'))); - - while ($row = $result->fetch()) { - $circle = $this->circleHelper->getCircle($row['circle_unique_id'], null, true); - $slug = $this->slugService->generateCollectiveSlug($row['id'], $circle->getSanitizedName()); - - $update - ->setParameter('id', (int)$row['id'], IQueryBuilder::PARAM_INT) - ->setParameter('slug', $slug, IQueryBuilder::PARAM_STR) - ->executeStatement(); - } - $result->closeCursor(); - } -} diff --git a/lib/Migration/Version021500Date20240820000001.php b/lib/Migration/Version021500Date20240820000001.php deleted file mode 100644 index fc33c805b..000000000 --- a/lib/Migration/Version021500Date20240820000001.php +++ /dev/null @@ -1,95 +0,0 @@ -getTable('collectives_pages'); - if (!$table->hasColumn('slug')) { - $this->runSlugGeneration = true; - $table->addColumn('slug', Types::STRING, [ - 'notnull' => false, - 'default' => false, - 'length' => 255, - ]); - - return $schema; - } - - return null; - } - - public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { - if (!$this->runSlugGeneration) { - return; - } - - $queryCollectives = $this->connection->getQueryBuilder(); - $queryCollectives->select(['id', 'circle_unique_id']) - ->from('collectives') - ->where('trash_timestamp IS NULL'); - $resultCollectives = $queryCollectives->executeQuery(); - - $queryPages = $this->connection->getQueryBuilder(); - $queryPages->select(['id']) - ->from('collectives_pages'); - $resultPages = $queryPages->executeQuery(); - - $update = $this->connection->getQueryBuilder(); - $update->update('collectives_pages') - ->set('slug', $update->createParameter('slug')) - ->where($update->expr()->eq('file_id', $update->createParameter('file_id'))); - - while ($rowCollective = $resultCollectives->fetch()) { - $circle = $this->circleHelper->getCircle($rowCollective['circle_unique_id'], null, true); - $pageInfos = $this->pageService->findAll($rowCollective['id'], $circle->getOwner()->getUserId()); - - foreach ($pageInfos as $pageInfo) { - if ($pageInfo->getFileName() === PageInfo::INDEX_PAGE_TITLE . PageInfo::SUFFIX) { - continue; - } - - $slug = $this->slugService->generatePageSlug($pageInfo->getTitle()); - $update - ->setParameter('file_id', $pageInfo->getId(), IQueryBuilder::PARAM_INT) - ->setParameter('slug', $slug, IQueryBuilder::PARAM_STR) - ->executeStatement(); - } - } - - $resultCollectives->closeCursor(); - $resultPages->closeCursor(); - } -} diff --git a/lib/Migration/Version021600Date20240820000000.php b/lib/Migration/Version021600Date20240820000000.php new file mode 100644 index 000000000..67a9ac944 --- /dev/null +++ b/lib/Migration/Version021600Date20240820000000.php @@ -0,0 +1,37 @@ +getTable('collectives'); + if (!$table->hasColumn('slug')) { + $table->addColumn('slug', Types::STRING, [ + 'notnull' => false, + 'default' => false, + 'length' => 255, + ]); + + return $schema; + } + + return null; + } + +} diff --git a/lib/Migration/Version021600Date20240820000001.php b/lib/Migration/Version021600Date20240820000001.php new file mode 100644 index 000000000..7d4349e7a --- /dev/null +++ b/lib/Migration/Version021600Date20240820000001.php @@ -0,0 +1,36 @@ +getTable('collectives_pages'); + if (!$table->hasColumn('slug')) { + $table->addColumn('slug', Types::STRING, [ + 'notnull' => false, + 'default' => false, + 'length' => 255, + ]); + + return $schema; + } + + return null; + } +}