Skip to content

Commit

Permalink
Slugify Collectives and Pages (migration)
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantin Myakshin <molodchick@gmail.com>
  • Loading branch information
Koc committed Sep 11, 2024
1 parent bb0635f commit f8372d7
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 6 deletions.
39 changes: 39 additions & 0 deletions lib/Migration/Version021200Date20240820000000.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,31 @@
namespace OCA\Collectives\Migration;

use Closure;
use OCA\Collectives\Service\CircleHelper;
use OCA\Collectives\Service\SlugGeneratorService;
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 Version021200Date20240820000000 extends SimpleMigrationStep {
private bool $runSlugGeneration = false;

public function __construct(
private IDBConnection $connection,
private CircleHelper $circleHelper,
private SlugGeneratorService $slugGeneratorService, ) {
}

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 @@ -33,4 +46,30 @@ 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->slugGeneratorService->generateCollectiveSlug($row['id'], $circle->getSanitizedName());

$update
->setParameter('id', (int)$row['id'], IQueryBuilder::PARAM_INT)
->setParameter('slug', $slug, IQueryBuilder::PARAM_STR)
->executeStatement();
}
$result->closeCursor();
}
}
58 changes: 58 additions & 0 deletions lib/Migration/Version021200Date20240820000001.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,34 @@
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\SlugGeneratorService;
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 Version021200Date20240820000001 extends SimpleMigrationStep {
private bool $runSlugGeneration = false;

public function __construct(
private IDBConnection $connection,
private CircleHelper $circleHelper,
private PageService $service,
private SlugGeneratorService $slugGeneratorService, ) {
}

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 @@ -33,4 +49,46 @@ 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');
$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);
/** @var PageInfo[] $pageInfos */
$pageInfos = $this->service->findAll($rowCollective['id'], $circle->getOwner()->getUserId());

Check failure on line 76 in lib/Migration/Version021200Date20240820000001.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedMethod

lib/Migration/Version021200Date20240820000001.php:76:72: UndefinedMethod: Method OCA\Circles\Model\Circle::getOwner does not exist (see https://psalm.dev/022)

foreach ($pageInfos as $pageInfo) {
if ($pageInfo->getFileName() === PageInfo::INDEX_PAGE_TITLE . PageInfo::SUFFIX) {
continue;
}

$slug = $this->slugGeneratorService->generatePageSlug($pageInfo->getId(), $pageInfo->getTitle());
$update
->setParameter('file_id', $pageInfo->getId(), IQueryBuilder::PARAM_INT)
->setParameter('slug', $slug, IQueryBuilder::PARAM_STR)
->executeStatement();
}
}

$resultCollectives->closeCursor();
$resultPages->closeCursor();
}
}
5 changes: 2 additions & 3 deletions lib/Service/CollectiveService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use OCP\Files\NotFoundException as FilesNotFoundException;
use OCP\Files\NotPermittedException as FilesNotPermittedException;
use OCP\IL10N;
use Symfony\Component\String\Slugger\SluggerInterface;

class CollectiveService extends CollectiveServiceBase {
private ?PageTrashBackend $pageTrashBackend = null;
Expand All @@ -47,7 +46,7 @@ public function __construct(
private IL10N $l10n,
private IEventDispatcher $eventDispatcher,
private NodeHelper $nodeHelper,
private SluggerInterface $slugger, ) {
private SlugGeneratorService $slugGeneratorService, ) {
parent::__construct($collectiveMapper, $circleHelper);
}

Expand Down Expand Up @@ -216,7 +215,7 @@ public function createCollective(string $userId,
}
$collective = $this->collectiveMapper->insert($collective);

$slug = $this->slugger->slug($name, locale: $userLang)->toString().'-'.$collective->getId();
$slug = $this->slugGeneratorService->generateCollectiveSlug($collective->getId(), $name);
$collective->setSlug($slug);
$this->collectiveMapper->update($collective);

Expand Down
5 changes: 2 additions & 3 deletions lib/Service/PageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
use OCP\IUserManager;
use OCP\Lock\LockedException;
use Psr\Container\ContainerInterface;
use Symfony\Component\String\Slugger\SluggerInterface;

class PageService {
private const DEFAULT_PAGE_TITLE = 'New Page';
Expand All @@ -49,7 +48,7 @@ public function __construct(private IAppManager $appManager,
private IConfig $config,
ContainerInterface $container,
private SessionService $sessionService,
private SluggerInterface $slugger, ) {
private SlugGeneratorService $slugGeneratorService, ) {
try {
$this->pushQueue = $container->get(IQueue::class);
} catch (Exception) {
Expand Down Expand Up @@ -1096,6 +1095,6 @@ private function generateSlugForPage(string $title, ?File $file): ?string {
return null;
}

return $this->slugger->slug($title)->toString() . '-' . $file->getId();
return $this->slugGeneratorService->generatePageSlug($file->getId(), $title);
}
}
18 changes: 18 additions & 0 deletions lib/Service/SlugGeneratorService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace OCA\Collectives\Service;

use Symfony\Component\String\Slugger\SluggerInterface;

class SlugGeneratorService {
public function __construct(private SluggerInterface $slugger) {
}

public function generateCollectiveSlug(int $collectiveId, string $name): string {
return $this->slugger->slug($name)->toString().'-'.$collectiveId;
}

public function generatePageSlug(int $fileId, string $title): string {
return $this->slugger->slug($title)->toString().'-'.$fileId;
}
}

0 comments on commit f8372d7

Please sign in to comment.