/** * Create a user chosen $alias pointing to $location in $languageCode. * * This method runs URL filters and transformers before storing them. * Hence the path returned in the URLAlias Value may differ from the given. * $alwaysAvailable makes the alias available in all languages. * * @param \eZ\Publish\API\Repository\Values\Content\Location $location * @param string $path * @param bool $forwarding if true a redirect is performed * @param string $languageCode the languageCode for which this alias is valid * @param bool $alwaysAvailable * * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the path already exists for the given language * * @return \eZ\Publish\API\Repository\Values\Content\URLAlias */ public function createUrlAlias(Location $location, $path, $languageCode, $forwarding = false, $alwaysAvailable = false) { $path = $this->cleanUrl($path); $this->repository->beginTransaction(); try { $spiUrlAlias = $this->urlAliasHandler->createCustomUrlAlias($location->id, $path, $forwarding, $languageCode, $alwaysAvailable); $this->repository->commit(); } catch (ForbiddenException $e) { $this->repository->rollback(); throw new InvalidArgumentException('$path', $e->getMessage(), $e); } catch (Exception $e) { $this->repository->rollback(); throw $e; } return $this->buildUrlAliasDomainObject($spiUrlAlias, $path); }
/** * Restores custom Location URL aliases from the backup table. * * @see \eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage\RegenerateUrlAliasesCommand::restoreCustomLocationAliases() */ protected function doRestoreCustomLocationAliases() { $totalCount = $this->getTotalBackupCount(static::CUSTOM_ALIAS_BACKUP_TABLE); $passCount = ceil($totalCount / $this->bulkCount); $createdAliasCount = 0; $conflictCount = 0; if ($totalCount === 0) { $this->output->writeln('Could not find any backed up custom Location URL aliases, nothing to restore.'); $this->output->writeln(''); return; } $queryBuilder = $this->connection->createQueryBuilder(); $queryBuilder->select('*')->from(static::CUSTOM_ALIAS_BACKUP_TABLE)->orderBy('id', 'ASC'); $this->output->writeln("Restoring {$totalCount} custom URL alias(es)."); $progressBar = $this->getProgressBar($totalCount); $progressBar->start(); for ($pass = 0; $pass <= $passCount; ++$pass) { $rows = $this->loadPassData($queryBuilder, $pass); foreach ($rows as $row) { try { $this->setMigrationTable(); $this->urlAliasHandler->createCustomUrlAlias($row['location_id'], $row['path'], (bool) $row['forwarding'], $row['language_code'], (bool) $row['always_available']); $createdAliasCount += 1; $this->setDefaultTable(); } catch (ForbiddenException $e) { $conflictCount += 1; } catch (Exception $e) { $this->setDefaultTable(); throw $e; } } $progressBar->advance(count($rows)); } $progressBar->finish(); $this->output->writeln(''); $this->output->writeln("Done. Restored {$createdAliasCount} custom URL alias(es) " . "with {$conflictCount} conflict(s)."); $this->output->writeln(''); }