Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error handling for tools:rename-slug. (#1666) #1669

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 67 additions & 6 deletions lib/task/tools/renameSlugTask.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

class renameSlugTask extends arBaseTask
{
protected $failedSlugs = [];

protected $logFile;

protected function configure()
{
$this->addArguments([
Expand Down Expand Up @@ -61,6 +65,16 @@ protected function execute($arguments = [], $options = [])
} else {
throw new Exception('Either provide old and new slug values, or use the CSV option and supply a CSV file containing those values.');
}

if (!empty($this->failedSlugs)) {
$this->logSection('rename-slug', 'The following slugs were not updated:', null, 'ERROR');
foreach ($this->failedSlugs as $err) {
$this->logSection('rename-slug', $err, null, 'ERROR');
}
}

$f = $this->logFile;
$this->logSection('rename-slug', "Log file: {$f}.");
}

protected function updateSlugsFromCSV($filename)
Expand Down Expand Up @@ -95,13 +109,60 @@ protected function renameSlug($oldSlug, $newSlug)
$criteria = new Criteria();
$criteria->add(QubitSlug::SLUG, $oldSlug);
$slug = QubitSlug::getOne($criteria);
if (!$slug) {
$this->logSection('rename-slug', "No slug matching {$oldSlug} found.");

return;
$existingSlugs = $this->getAllSlugs();

if (in_array($newSlug, $existingSlugs)) {
$this->failedSlugs[] = "{$newSlug} already exists.";
} elseif (!$slug) {
$this->failedSlugs[] = "{$oldSlug} not found.";
} else {
$slug->slug = $newSlug;
$slug->save();
$this->logSection('rename-slug', "Slug {$oldSlug} updated to {$newSlug} successfully.");
$this->addToLogFile($oldSlug, $newSlug);
}
}

protected function initLogFile()
{
$dateFormat = date('Y-m-d-H-i');
$f = $this->logFile = sfConfig::get('sf_log_dir')."/rename-slug-{$dateFormat}.log.txt";

$file = fopen($f, 'w');

if ($file) {
$header = "rename-slug-{$dateFormat} report:\n\n";
fwrite($file, $header);
fclose($file);
} else {
$this->logSection('rename-slug', "Log file {$f} failed to open for writing.", null, 'ERROR');
}
$slug->slug = $newSlug;
$slug->save();
$this->logSection('rename-slug', "Slug {$oldSlug} updated to {$newSlug} successfully.");
}

protected function addToLogFile($oldSlug, $newSlug)
{
if (!isset($this->logFile)) {
$this->initLogFile();
}

$log = "/{$oldSlug} updated to /{$newSlug}.\n";
$custom_logger = new sfFileLogger(new sfEventDispatcher(), ['file' => $this->logFile]);
$custom_logger->info($log);
}

protected function getAllSlugs()
{
$slugs = [];
$databaseManager = new sfDatabaseManager($this->configuration);
$conn = $databaseManager->getDatabase('propel')->getConnection();

// Create hash of slugs already in database
$sql = 'SELECT slug FROM slug ORDER BY slug';
foreach ($conn->query($sql, PDO::FETCH_NUM) as $row) {
$slugs[] = $row[0];
}

return $slugs;
}
}
Loading