Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
wordpress_migrate_sql
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
wordpress_migrate_sql
Commits
d9382130
Commit
d9382130
authored
6 months ago
by
Omar Mohamad - El Hassan Lopesino
Browse files
Options
Downloads
Plain Diff
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
6 months ago
Stage: build
Stage: validate
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/Drush/Commands/WordpressMigrateSqlUrlCommands.php
+71
-0
71 additions, 0 deletions
src/Drush/Commands/WordpressMigrateSqlUrlCommands.php
src/MigrationStatus.php
+81
-0
81 additions, 0 deletions
src/MigrationStatus.php
src/WordpressPost.php
+25
-0
25 additions, 0 deletions
src/WordpressPost.php
with
177 additions
and
0 deletions
src/Drush/Commands/WordpressMigrateSqlUrlCommands.php
0 → 100644
+
71
−
0
View file @
d9382130
<?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
)));
}
}
}
This diff is collapsed.
Click to expand it.
src/MigrationStatus.php
0 → 100644
+
81
−
0
View file @
d9382130
<?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
();
}
}
This diff is collapsed.
Click to expand it.
src/WordpressPost.php
0 → 100644
+
25
−
0
View file @
d9382130
<?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
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment