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 Jan 3, 2025
1 parent 03688cd commit 893a5c8
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 172 deletions.
3 changes: 2 additions & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ In your Nextcloud instance, simply navigate to **»Apps«**, find the
**»Teams«** and **»Collectives«** apps and enable them.
]]></description>
<version>2.15.2</version>
<version>2.16.0</version>
<licence>agpl</licence>
<author>CollectiveCloud Team</author>
<namespace>Collectives</namespace>
Expand Down 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();
}
}
76 changes: 0 additions & 76 deletions lib/Migration/Version021500Date20240820000000.php

This file was deleted.

95 changes: 0 additions & 95 deletions lib/Migration/Version021500Date20240820000001.php

This file was deleted.

37 changes: 37 additions & 0 deletions lib/Migration/Version021600Date20240820000000.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

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

namespace OCA\Collectives\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

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

$table = $schema->getTable('collectives');
if (!$table->hasColumn('slug')) {
$table->addColumn('slug', Types::STRING, [
'notnull' => false,
'default' => false,
'length' => 255,
]);

return $schema;
}

return null;
}

}
36 changes: 36 additions & 0 deletions lib/Migration/Version021600Date20240820000001.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

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

namespace OCA\Collectives\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

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

$table = $schema->getTable('collectives_pages');
if (!$table->hasColumn('slug')) {
$table->addColumn('slug', Types::STRING, [
'notnull' => false,
'default' => false,
'length' => 255,
]);

return $schema;
}

return null;
}
}

0 comments on commit 893a5c8

Please sign in to comment.