Skip to content

Commit

Permalink
Merge pull request #77 from BenjaminMedia/WILL-1085/avoid-looping-red…
Browse files Browse the repository at this point in the history
…irects

WILL-1085 - Avoid looping redirects
  • Loading branch information
MFlor authored Mar 15, 2019
2 parents cc4b93e + 7874499 commit 45cde0e
Show file tree
Hide file tree
Showing 34 changed files with 557 additions and 481 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"require": {
"php": ">=7.1",
"illuminate/support": "^5.7",
"symfony/http-foundation": "^4.2"
"symfony/http-foundation": "^4.2",
"league/csv": "^9.2"
},
"require-dev": {
"johnpbloch/wordpress": "^4.9",
Expand Down
115 changes: 91 additions & 24 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 18 additions & 3 deletions src/Commands/RedirectCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Bonnier\WP\Redirect\Commands;

use Bonnier\WP\Redirect\Database\DB;
use Bonnier\WP\Redirect\Database\Exceptions\DuplicateEntryException;
use Bonnier\WP\Redirect\Database\Migrations\Migrate;
use Bonnier\WP\Redirect\Helpers\UrlHelper;
use Bonnier\WP\Redirect\Models\Redirect;
Expand Down Expand Up @@ -33,7 +34,10 @@ public function normalize()
}
if ($results->isNotEmpty()) {
/** @var Bar $progress */
$progress = make_progress_bar(sprintf('Normalizing %s redirects', $results->count()), $results->count());
$progress = make_progress_bar(
sprintf('Normalizing %s redirects', number_format($results->count())),
$results->count()
);
$results->each(function (array $redirect) use ($database, $progress) {
$redirectID = $redirect['id'];
unset($redirect['id']);
Expand All @@ -49,7 +53,13 @@ public function normalize()
$redirect['paramless_from_hash'] = hash('md5', parse_url($redirect['from'], PHP_URL_PATH));
$redirect['to'] = UrlHelper::normalizeUrl($redirect['to']);
$redirect['to_hash'] = hash('md5', $redirect['to']);
$database->update($redirectID, $redirect);
try {
$database->update($redirectID, $redirect);
} catch (DuplicateEntryException $exception) {
$database->delete($redirectID);
} catch (\Exception $exception) {
\WP_CLI::warning(sprintf('Failed updating redirect \'%s\'', $redirectID));
}
}
$progress->tick();
});
Expand All @@ -73,14 +83,19 @@ public function unchain()
\WP_CLI::error(sprintf('Failed instantiating RedirectRepository (%s)', $exception->getMessage()));
return;
}
\WP_CLI::line('Retrieving redirects...');
try {
$redirects = $repository->findAll();
} catch (\Exception $exception) {
\WP_CLI::error(sprintf('Failed finding redirects (%s)', $exception->getMessage()));
return;
}
$redirectCount = $redirects->count();
/** @var Bar $progress */
$progress = make_progress_bar(sprintf('Unchaining %s redirects', $redirects->count()), $redirects->count());
$progress = make_progress_bar(
sprintf('Unchaining %s redirects', number_format($redirectCount)),
$redirectCount
);
$redirects->each(function (Redirect $redirect) use ($repository, $progress) {
try {
$repository->save($redirect);
Expand Down
46 changes: 46 additions & 0 deletions src/Database/Migrations/AlterRedirectTableAddIgnoreQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Bonnier\WP\Redirect\Database\Migrations;

use Illuminate\Support\Str;
use League\Csv\CannotInsertRecord;
use League\Csv\Writer;

class AlterRedirectTableAddIgnoreQuery implements Migration
{
Expand All @@ -15,6 +17,8 @@ public static function migrate()
global $wpdb;
$table = $wpdb->prefix . Migrate::REDIRECTS_TABLE;

self::deduplicate($wpdb, $table);

$sql = "
ALTER TABLE `$table`
ADD `keep_query` tinyint(1) DEFAULT 0,
Expand All @@ -35,4 +39,46 @@ public static function verify(): bool
$result = $wpdb->get_row("SHOW CREATE TABLE $table", ARRAY_A);
return isset($result['Create Table']) && Str::contains($result['Create Table'], 'keep_query');
}

private static function deduplicate(\wpdb $wpdb, string $table)
{
if (defined('WP_ENV') && WP_ENV === 'testing') {
return;
}
$csv = Writer::createFromPath(sprintf('%s_deleted_duplicates.csv', strtotime('now')), 'w+');
try {
$csv->insertOne(['id', 'from', 'to', 'locale']);
} catch (CannotInsertRecord $exception) {
return;
}
$wpdb->update($table, ['locale' => 'nb'], ['locale' => 'no']);
$wpdb->delete($table, ['locale' => '']);
$sql = "SELECT `locale` FROM `$table` GROUP BY `locale`";
$locales = $wpdb->get_results($sql);
foreach ($locales as $locale) {
$results = $wpdb->get_results("
SELECT `from_hash`, count(id) AS cnt
FROM `$table`
WHERE `locale` = '$locale->locale'
GROUP BY `from` HAVING cnt > 1
");
foreach ($results as $result) {
$redirects = $wpdb->get_results("
SELECT * FROM `$table`
WHERE `from_hash` = '$result->from_hash'
AND `locale` = '$locale->locale'
ORDER BY id DESC
");
array_shift($redirects); // Keep the newest redirect
foreach ($redirects as $redirect) {
$wpdb->delete($table, ['id' => $redirect->id]);
try {
$csv->insertOne([$redirect->id, $redirect->from, $redirect->to, $redirect->locale]);
} catch (CannotInsertRecord $exception) {
continue;
}
}
}
}
}
}
7 changes: 7 additions & 0 deletions src/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ public function offset(int $offset): Query
return $this;
}

public function groupBy(string $column): Query
{
$this->query .= " GROUP BY `$column`";

return $this;
}

/**
* @return string
* @throws \Exception
Expand Down
Loading

0 comments on commit 45cde0e

Please sign in to comment.