Skip to content

Commit

Permalink
Slugify Collectives and Pages (move slug generation into a separate c…
Browse files Browse the repository at this point in the history
…onsole command)

Signed-off-by: Kostiantyn Miakshyn <[email protected]>
  • Loading branch information
Koc committed Dec 20, 2024
1 parent b09ec94 commit ef0c4cc
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 99 deletions.
1 change: 1 addition & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ In your Nextcloud instance, simply navigate to **»Apps«**, find the
<commands>
<command>OCA\Collectives\Command\CreateCollective</command>
<command>OCA\Collectives\Command\ExpirePageVersions</command>
<command>OCA\Collectives\Command\GenerateSlugs</command>
<command>OCA\Collectives\Command\IndexCollectives</command>
<command>OCA\Collectives\Command\PageTrashCleanup</command>
<command>OCA\Collectives\Command\PurgeObsoletePages</command>
Expand Down
106 changes: 106 additions & 0 deletions lib/Command/GenerateSlugs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Collectives\Command;

use OC\Core\Command\Base;
use OCA\Collectives\Model\PageInfo;
use OCA\Collectives\Service\CircleHelper;
use OCA\Collectives\Service\PageService;
use OCA\Collectives\Service\SlugService;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class GenerateSlugs extends Base {
public function __construct(
private IDBConnection $connection,
private CircleHelper $circleHelper,
private PageService $pageService,
private SlugService $slugService,
) {
parent::__construct();
}

protected function configure(): void {
$this
->setName('collectives:generate-slugs')
->setDescription('Generate slugs for collectives and pages');
parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$output->write('<info>Generating slugs for collectives ... </info>');
$this->generateCollectiveSlugs();
$output->writeln('<info>done</info>');

$output->write('<info>Generating slugs for pages ... </info>');
$this->generatePageSlugs();
$output->writeln('<info>done</info>');

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();
}
}
39 changes: 0 additions & 39 deletions lib/Migration/Version021500Date20240820000000.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,18 @@
namespace OCA\Collectives\Migration;

use Closure;
use OCA\Collectives\Service\CircleHelper;
use OCA\Collectives\Service\SlugService;
use OCP\DB\ISchemaWrapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\Types;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version021500Date20240820000000 extends SimpleMigrationStep {
private bool $runSlugGeneration = false;

public function __construct(
private IDBConnection $connection,
private CircleHelper $circleHelper,
private SlugService $slugService,
) {
}

public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

$table = $schema->getTable('collectives');
if (!$table->hasColumn('slug')) {
$this->runSlugGeneration = true;
$table->addColumn('slug', Types::STRING, [
'notnull' => false,
'default' => false,
Expand All @@ -48,29 +34,4 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
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();
}
}
59 changes: 0 additions & 59 deletions lib/Migration/Version021500Date20240820000001.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,18 @@
namespace OCA\Collectives\Migration;

use Closure;
use OCA\Collectives\Model\PageInfo;
use OCA\Collectives\Service\CircleHelper;
use OCA\Collectives\Service\PageService;
use OCA\Collectives\Service\SlugService;
use OCP\DB\ISchemaWrapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\Types;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version021500Date20240820000001 extends SimpleMigrationStep {
private bool $runSlugGeneration = false;

public function __construct(
private IDBConnection $connection,
private CircleHelper $circleHelper,
private PageService $pageService,
private SlugService $slugService,
) {
}

public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

$table = $schema->getTable('collectives_pages');
if (!$table->hasColumn('slug')) {
$this->runSlugGeneration = true;
$table->addColumn('slug', Types::STRING, [
'notnull' => false,
'default' => false,
Expand All @@ -50,46 +33,4 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt

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();
}
}
3 changes: 2 additions & 1 deletion tests/stub.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ namespace OC\Core\Command {
public function __construct() {}
protected function configure() {}
public function run(InputInterface $input, OutputInterface $output) {}
public function setName(string $name) {}
public function setName(string $name): self {}
public function setDescription(string $description): self {}
public function getHelper(string $name) {}
protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, $items, $prefix = ' - ') {
}
Expand Down

0 comments on commit ef0c4cc

Please sign in to comment.