-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Slugify Collectives and Pages (move slug generation into a separate c…
…onsole command) Signed-off-by: Kostiantyn Miakshyn <[email protected]>
- Loading branch information
Showing
6 changed files
with
181 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |