Skip to content
Snippets Groups Projects
Commit d9382130 authored by Omar Mohamad - El Hassan Lopesino's avatar Omar Mohamad - El Hassan Lopesino
Browse files

Merge branch '3485600-allow-reporting-pages' into '1.0.x'

Issue #3485600: Allow reporting pages that are missing to migrate

See merge request !4
parents 440a2129 55bf240a
No related branches found
No related tags found
No related merge requests found
Pipeline #329952 passed with warnings
<?php
namespace Drupal\wordpress_migrate_sql\Drush\Commands;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drupal\Core\Database\Database;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\redirect\RedirectRepository;
use Drupal\wordpress_migrate_sql\MigrationStatus;
use Drupal\wordpress_migrate_sql\WordpressPost;
use Drush\Attributes as CLI;
use Drush\Commands\DrushCommands;
use FontLib\Table\Type\name;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Mime\MimeTypeGuesserInterface;
final class WordpressMigrateSqlUrlCommands extends DrushCommands {
use StringTranslationTrait;
public function __construct(protected RedirectRepository $redirectRepository, protected FileUrlGeneratorInterface $fileUrlGenerator) {}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): self {
return new static(
$container->get('redirect.repository'),
$container->get('file_url_generator'),
);
}
/**
* Command description here.
*/
#[CLI\Command(name: 'wordpress_migrate_sql:missing-urls', aliases: ['wms:mu'])]
#[CLI\Argument(name: 'database_name', description: 'Database name.')]
#[CLI\Argument(name: 'result_file', description: 'Result file.')]
#[Cli\Option(name: 'post-types', description: 'Post types.')]
#[Cli\Option(name: 'wordpress-base-url', description: 'Wordpress base url.')]
#[CLI\Usage(name: 'wordpress_migrate_sql:missing-urls', description: 'Get list of missing urls')]
public function analyzeAttachments(
$database_name,
string $result_file = 'public://wordpress-migrate-sql-missing-urls.csv',
array $options = [
'post-types' => 'post',
'wordpress-base-url' => '',
]) {
$database = Database::getConnection('default', $database_name);
$migration_status = new MigrationStatus($this->redirectRepository, $database, explode(',', $options['post-types']));
$missing_urls = $migration_status->getMissingUrls();
if (!empty($missing_urls)) {
$base_url = !empty($options['wordpress-base-url']) ? $options['wordpress-base-url'] . '/' : '';
$missing_urls_csv = implode(',', ['Post ID', 'Post type', 'Wordpress URL']);
$missing_urls_csv = $missing_urls_csv . PHP_EOL . implode(PHP_EOL, array_map(function (WordpressPost $post) use ($base_url) {
return implode(',', [
$post->getId(),
$post->getType(),
$base_url . $post->getUrl(),
]);
}, $missing_urls));
file_put_contents($result_file, $missing_urls_csv);
$this->logger()->success(sprintf('Missing %s urls. Full report: %s', count($missing_urls), $this->fileUrlGenerator->generateAbsoluteString($result_file)));
}
}
}
<?php
namespace Drupal\wordpress_migrate_sql;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
use Drupal\Core\Url;
use Drupal\redirect\Exception\RedirectLoopException;
use Drupal\redirect\RedirectRepository;
use Drupal\redirect\Entity\Redirect;
class MigrationStatus {
protected RedirectRepository $redirectRepository;
protected Connection $database;
protected array $postTypes;
public function __construct(RedirectRepository $redirectRepository, Connection $database, array $post_types = ['post']) {
$this->redirectRepository = $redirectRepository;
$this->database = $database;
$this->postTypes = $post_types;
}
public function getMissingUrls() {
return array_filter($this->getAllUrls(), [$this, 'urlNotAvailable']);
}
public function getUrlsWithRedirectLoop() {
return array_filter($this->getAllUrls(), [$this, 'urlHsaRedirectLoop']);
}
protected function urlNotAvailable(WordpressPost $post) {
$drupal_url = Url::fromUserInput('/' . $post->getUrl());
return !$drupal_url->isRouted() && !$this->redirectRepository->findMatchingRedirect($post->getUrl()) instanceof Redirect;
}
public function urlHsaRedirectLoop(WordpressPost $post) {
$url = $post->getUrl();
foreach ([$url, '/' . $url] as $url_candidate) {
try {
$this->redirectRepository->findMatchingRedirect($url_candidate);
}
catch (RedirectLoopException $e) {
return TRUE;
}
}
return FALSE;
}
public function getAllUrls() {
$posts_query_result = $this->database->select('posts')
->fields('posts', ['id', 'post_parent', 'post_name', 'post_type'])
->condition('post_status', 'publish')
->condition('post_type', $this->postTypes, 'IN')
->orderBy('id')
->execute();
$post_urls = [];
while ($post = $posts_query_result->fetch()) {
$post_urls[$post->id] = new WordpressPost($post->id, $this->getPostUrl($post), $post->post_type);
}
return $post_urls;
}
protected function getPostUrl($post) {
if ($post->post_parent == 0) {
return $post->post_name;
}
else {
$parent = $this->findPostParent($post->post_parent);
return !empty($parent) ? $this->getPostUrl($parent) . '/' . $post->post_name : NULL;
}
}
protected function findPostParent($post_id) {
$post_parent_name_query_result = $this->database->select('posts')->fields('posts', ['id', 'post_parent','post_name'])->condition('id', $post_id)->execute();
return $post_parent_name_query_result->fetch();
}
}
<?php
namespace Drupal\wordpress_migrate_sql;
class WordpressPost
{
public function __construct(protected int $id, protected string $url, protected string $type)
{
}
public function getId(): int {
return $this->id;
}
public function getUrl(): string {
return $this->url;
}
public function getType(): string {
return $this->type;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment