Skip to content

Commit

Permalink
Markup migration
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Jun 24, 2024
1 parent a212515 commit 719eee4
Show file tree
Hide file tree
Showing 15 changed files with 399 additions and 350 deletions.
2 changes: 1 addition & 1 deletion transfer-protocol/.phpunit.result.cache

Large diffs are not rendered by default.

97 changes: 97 additions & 0 deletions transfer-protocol/bin/rewrite-urls.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

use Rowbot\URL\URL;

require_once __DIR__ . "/../bootstrap.php";

if ( $argc < 2 ) {
echo "Usage: php script.php <command> --file <input-file> --from-site-url <current site url> --to-url <target url>\n";
echo "Commands:\n";
echo " list_urls: List all the URLs found in the input file.\n";
echo " migrate_urls: Migrate all the URLs found in the input file from the current site to the target site.\n";
exit( 1 );
}

$command = $argv[1];
$options = [];

for ( $i = 2; $i < $argc; $i ++ ) {
if ( str_starts_with( $argv[ $i ], '--' ) && isset( $argv[ $i + 1 ] ) ) {
$options[ substr( $argv[ $i ], 2 ) ] = $argv[ $i + 1 ];
$i ++;
}
}

if ( ! isset( $options['file'] ) ) {
echo "The file option is required.\n";
exit( 1 );
}

$inputFile = $options['file'];
$targetDomain = @$options['target-domain'];

if ( ! file_exists( $inputFile ) ) {
echo "The file $inputFile does not exist.\n";
exit( 1 );
}

$block_markup = file_get_contents( $inputFile );

// @TODO: Should a base URL be always required?
$previous_url = $options['from-site-url'] ?? 'https://w.org';
$p = new WP_Block_Markup_Url_Processor( $block_markup, $previous_url );

switch ( $command ) {
case 'list_urls':
echo "URLs found in the markup:\n\n";
while ( $p->next_url() ) {
// Skip empty relative URLs.
if ( ! trim( $p->get_url() ) ) {
continue;
}
echo '* ';
switch ( $p->get_token_type() ) {
case '#tag':
echo 'In <' . $p->get_tag() . '> tag attribute "' . $p->get_inspected_attribute_name() . '": ';
break;
case '#block-comment':
echo 'In a ' . $p->get_block_name() . ' block attribute "' . $p->get_block_attribute_key() . '": ';
break;
case '#text':
echo 'In #text: ';
break;
}
echo $p->get_url() . "\n";
}
echo "\n";
break;
case 'migrate_urls':
if ( ! isset( $options['from-site-url'] ) ) {
echo "The --from-site-url option is required for the migrate_urls command.\n";
exit( 1 );
}
if ( ! isset( $options['to-url'] ) ) {
echo "The --to-url option is required for the migrate_urls command.\n";
exit( 1 );
}
$parsed_prev_url = URL::parse( $options['from-site-url'] );
$next_url = $options['to-url'];
$parsed_new_url = URL::parse( $next_url );
echo "Replacing $previous_url with $next_url in the input.\n";
echo "Note this is not yet enough to migrate the site as both the previous and the new";
echo "site might be hosted on specific paths.\n\n";
while ( $p->next_url() ) {
$updated = false;
$url = $p->get_url();
$parsed_url = URL::parse( $url, $parsed_prev_url );
if ( $parsed_url->hostname === $parsed_prev_url->hostname ) {
$parsed_url->hostname = $parsed_new_url->hostname;
if ( str_starts_with( $parsed_url->pathname, $parsed_prev_url->pathname ) ) {
$parsed_url->pathname = $parsed_new_url->pathname . substr( $parsed_url->pathname, strlen( $parsed_prev_url->pathname ) );
}
$p->set_url( $parsed_url->toString() );
}
}
echo $p->get_updated_html();
break;
}
4 changes: 4 additions & 0 deletions transfer-protocol/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ function __($input) {
function esc_attr($input) {
return htmlspecialchars($input);
}

function esc_html($input) {
return htmlspecialchars($input);
}
32 changes: 0 additions & 32 deletions transfer-protocol/iter.php

This file was deleted.

14 changes: 14 additions & 0 deletions transfer-protocol/married-short.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- wp:image {"src": "https://mysite.com/wp-content/image.png", "meta": {"src": "https://mysite.com/wp-content/image.png"}} -->
<img src="https://mysite.com/wp-content/image.png">
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>During the <a href="writeofpassage.school">Write of Passage</a>, I stubbornly tried to beat my writer’s block by writing until 3am multiple times. The burnout returned. I dropped everything and went to Greece for a week.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>
Have you seen my blog, adamadam.blog? I told a story there of how I got my Bachelor's degree,
check it out: https://adamadam.blog/2021/09/16/how-i-got-bachelors-in-six-months/
</p>
<!-- /wp:paragraph -->
2 changes: 1 addition & 1 deletion transfer-protocol/married.html
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,4 @@ <h2 id="what-s-next">What’s next?</h2>

<!-- wp:paragraph -->
<p>I will make 2022 about doing&nbsp;<em>less</em>. My only map is my intuition.&nbsp;<a href="https://en.wikipedia.org/wiki/Amor_fati">Amor fati</a>, as the Stoics would say. I am open to whatever life will bring. Where is it going to take me? Who knows? We'll see next year!</p>
<!-- /wp:paragraph -->
<!-- /wp:paragraph -->
8 changes: 6 additions & 2 deletions transfer-protocol/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<phpunit bootstrap="bootstrap.php">
<phpunit
bootstrap="bootstrap.php"
colors="true"
verbose="true"
>
<testsuites>
<testsuite name="Project Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
</phpunit>
170 changes: 0 additions & 170 deletions transfer-protocol/rewrite-urls.php

This file was deleted.

13 changes: 9 additions & 4 deletions transfer-protocol/run-tests.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/bin/bash
#COMMAND="phpunit tests/WP_Migration_*"
#COMMAND="phpunit tests/WP_Block_Markup_P*"
COMMAND="phpunit tests/WP_Block_Markup_Url*"
$COMMAND
fswatch -o ./**/*.php | xargs -n1 -I{} $COMMAND
#COMMAND="phpunit tests/*.php"
#COMMAND="phpunit tests/WP_Block_Markup_Url_Processor_Tests.php"
#COMMAND="phpunit -c phpunit.xml"
#$COMMAND
#fswatch -o ./**/*.php | xargs -n1 -I{} $COMMAND

for i in $(ls tests/*.php | grep -v URL_Parser); do
phpunit $i
done
Loading

0 comments on commit 719eee4

Please sign in to comment.