Commit inicial powerservice

This commit is contained in:
Gabriel
2026-10-19 12:45:45 -06:00
commit ad85486fe2
10158 changed files with 2736804 additions and 0 deletions
@@ -0,0 +1,37 @@
<?php
namespace WordPress\DataLiberation\URL;
use VendorPrefix\Rowbot\URL\URL;
/**
* Value object returned by WPURL::replace_base_url().
*
* - Cast to string to get the updated URL as a string.
* - When the original URL was relative, casting returns a relative string against
* the new base.
*/
class ConvertedUrl {
/** @var URL */
public $new_url;
/** @var string */
public $new_raw_url;
/** @var string|null */
public $new_raw_relative_url;
/** @var bool */
public $was_relative = false;
/**
* Returns the updated URL string. If the original was relative, returns a relative string.
*/
public function __toString(): string {
if ( $this->was_relative ) {
return $this->new_raw_relative_url;
}
return $this->new_raw_url;
}
}
@@ -0,0 +1,94 @@
<?php
namespace WordPress\DataLiberation\URL;
use WordPress\DataLiberation\CSS\CSSProcessor;
/**
* Provides URL specific helpers on top of the CSSProcessor tokenizer.
*/
class CSSURLProcessor {
/**
* @var CSSProcessor
*/
private $processor;
/**
* @param string $css CSS source without wrapping braces.
*/
public function __construct( string $css ) {
$this->processor = CSSProcessor::create( $css );
}
/**
* Moves the cursor to the next URL token, if available.
*
* @return bool
*/
public function next_url(): bool {
while ( $this->processor->next_token() ) {
$type = $this->processor->get_token_type();
// Direct URL token.
if ( CSSProcessor::TOKEN_URL === $type ) {
return true;
}
// url() function with STRING token.
if ( CSSProcessor::TOKEN_FUNCTION === $type &&
0 === strcasecmp( $this->processor->get_token_value(), 'url' ) ) {
// Look ahead for STRING token, skipping whitespace.
while ( $this->processor->next_token() ) {
$inner_type = $this->processor->get_token_type();
if ( CSSProcessor::TOKEN_WHITESPACE === $inner_type ) {
continue; // Skip whitespace.
}
if ( CSSProcessor::TOKEN_STRING === $inner_type ) {
return true; // Found the URL string.
}
// Hit something else (like RIGHT_PAREN or another token).
break;
}
}
}
return false;
}
/**
* Returns the raw (decoded) URL for the current match.
*
* @return string|false
*/
public function get_raw_url() {
$value = $this->processor->get_token_value();
return false !== $value ? $value : false;
}
/**
* Replaces the currently matched URL with a new value.
*
* @param string $new_url Replacement URL without quoting.
* @return bool
*/
public function set_raw_url( string $new_url ): bool {
return $this->processor->set_token_value( $new_url );
}
/**
* Returns the updated CSS with all replacements applied.
*
* @return string
*/
public function get_updated_css(): string {
return $this->processor->get_updated_css();
}
/**
* Determines whether the current URL is a data URI.
*
* @return bool
*/
public function is_data_uri(): bool {
return $this->processor->is_data_uri();
}
}
@@ -0,0 +1,384 @@
<?php
namespace WordPress\DataLiberation\URL;
use WordPress\DataLiberation\BlockMarkup\URL;
use WP_HTML_Text_Replacement;
/**
* Finds string fragments that look like URLs and allows replacing them.
*
* This class implements two stages of detection:
*
* 1. **A "thick" sieve**
* 2. **A "fine" sieve**
*
* The thick sieve uses a regular expression to match URL-like substrings. It matches too
* much and may yield false positives.
*
* The fine sieve filters out invalid candidates using a WHATWG-compliant parser so only
* real URLs are returned.
*
* ## URL Detection
*
* The thick sieve looks for URLs:
*
* * Starting with http://, https://, or //, e.g. //wp.org.
* * With no protocol, e.g. www.wp.org or wp.org/path
*
* Here's a list of matching-related rules, limitations, and assumptions:
*
* ### Protocols
*
* As a site migration tool, this processor only considers URLs with HTTP
* and HTTPS protocols.
*
* ### Domain names
*
* UTF-8 characters in the domain names are supported even if they're
* not encoded as punycode. For example, scanning the text:
*
* > Więcej na łąka.pl
*
* Would yield `łąka.pl`
*
* ### Paths
*
* The path is limited to ASCII characters, as per the URL specification.
* For example, scanning the text:
*
* > Visit the WordPress plugins directory https://w.org/plugins?łąka=1
*
* Would yield `https://w.org/plugins?`, not `https://w.org/plugins?łąka=1`.
* However, scanning this text:
*
* > Visit the WordPress plugins directory https://w.org/plugins?%C5%82%C4%85ka=1
*
* Would yield `https://w.org/plugins?%C5%82%C4%85ka=1`.
*
* ### Parenthesis treatment
*
* This scanner captures parentheses as a part of the path, query, or fragment, except
* when they're seen as the last character in the URL. For example, scanning the text:
*
* > Visit the WordPress plugins directory (https://w.org/plugins)
*
* Would yield `https://w.org/plugins`, but scanning the text:
*
* > Visit the WordPress plugins directory (https://w.org/plug(in)s
*
* Would yield `https://w.org/plug(in)s`.
*
* ### Rejecting URLs with embedded credentials
*
* `https://user:pass@wp.org` is not matched. Rewriting URLs that presume transferable
* credentials is hazardous and rarely correct for migrations.
*
* ### Reject non-HTTP(S) schemes
*
* Out of scope for site moves; all of these are rejected:
* `gopher://site.com`, `blob:afgh2-48189d`, `ahttp://site.com`, `mailto:user@site.com`, `file://asset.zip`.
* If we need additional schemes later, we can add them intentionally.
*
* ### Reject non‑absolute‑looking references
*
* While we do rely on a base URL, inputs like `::`, `/index.html`, `?query` are still ignored.
* Bare-domain forms like `mysite.org/?query` are still matched.
*
* ### Handle trailing punctuation sensibly
*
* `https://mysite.com/path/..` is interpreted as `https://mysite.com/path/` rather than
* collapsing to the origin. A final period is far more likely sentence punctuation than `../`.
* If a user truly writes `https://mysite.com/path/../`, we parse it as expected.
*
* ### Fuzzy matching for malformed ports
*
* * **WHATWG**: `"http://w.org:100000 plugins are in the directory" → failure`.
* * **Inline detection**: `"http://w.org:100000 plugins are in the directory" → "http://w.org/"`
* (truncate at the invalid port).
*
* This is a best‑effort extraction of the valid prefix rather than an all‑or‑nothing rejection.
*
* ### Whitespace handling
*
* * **WHATWG**: `"http://example\t.\norg" → "http://example.org/"`.
* * **Inline detection**: stops at the first whitespace, yielding `"http://example/"`.
*
* This reflects how URLs actually appear in text blocks where whitespace often terminates a link.
*/
class URLInTextProcessor {
private $text;
private $url_starts_at;
private $url_length;
private $bytes_already_parsed = 0;
/**
* @var string
*/
private $matched_url;
/**
* @var URL
*/
private $parsed_url;
private $did_prepend_protocol;
/**
* The base URL for the parsing algorithm.
* See https://url.spec.whatwg.org/.
*
* @var mixed|null
*/
private $base_url;
private $base_protocol;
/**
* The regular expression pattern used for the matchin URL candidates
* from the text.
*
* @var string
*/
private $regex;
/**
* @see \WP_HTML_Tag_Processor
* @var WP_HTML_Text_Replacement[]
*/
private $lexical_updates = array();
/**
* @var bool
* A flag to indicate whether the URL matching should be strict or not.
* If set to true, the matching will be strict, meaning it will only match URLs that strictly adhere to the pattern.
* If set to false, the matching will be more lenient, allowing for potential false positives.
*/
private $strict = false;
public function __construct( $text, $base_url = null ) {
$this->text = $text;
$this->base_url = $base_url;
$this->base_protocol = $base_url ? parse_url( $base_url, PHP_URL_SCHEME ) : null;
$prefix = $this->strict ? '^' : '';
$suffix = $this->strict ? '$' : '';
// Source: https://github.com/vstelmakh/url-highlight/blob/master/src/Matcher/Matcher.php.
$this->regex = '/' . $prefix . '
(?: # scheme
(?<scheme>[a-z0-9\+]+?:)? #
(?:\/*) # The protocol may optionally be followed by one or more slashes
)?
(?: # userinfo
(?:
(?<=\/{2}) # prefixed with \/\/
| # or
(?=[^\p{Sm}\p{Sc}\p{Sk}\p{P}]) # start with not: mathematical, currency, modifier symbol, punctuation
)
(?<userinfo>[^\s<>@\/]+) # not: whitespace, < > @ \/
@ # at
)?
(?=%|[^\p{Z}\p{Sm}\p{Sc}\p{Sk}\p{C}\p{P}]) # followed by valid host char
(?| # host
(?<host> # host prefixed by scheme or userinfo (less strict)
(?<=\/\/|@) # prefixed with \/\/ or @
(?=[^\-]) # label start, not: -
(?:%|[^\p{Z}\p{Sm}\p{Sc}\p{Sk}\p{C}\p{P}]|-){1,63} # label not: whitespace, mathematical, currency, modifier symbol, control point, punctuation | except -
(?<=[^\-]) # label end, not: -
(?: # more label parts
\.
(?=[^\-]) # label start, not: -
(?<tld>(?:[^\p{Z}\p{Sm}\p{Sc}\p{Sk}\p{C}\p{P}]|-){1,63}) # label not: whitespace, mathematical, currency, modifier symbol, control point, punctuation | except -
(?<=[^\-]) # label end, not: -
)*
)
| # or
(?<host> # host with tld (no scheme or userinfo)
(?=[^\-]) # label start, not: -
(?:%|[^\p{Z}\p{Sm}\p{Sc}\p{Sk}\p{C}\p{P}]|-){1,63} # label not: whitespace, mathematical, currency, modifier symbol, control point, punctuation | except -
(?<=[^\-]) # label end, not: -
(?: # more label parts
\.
(?=[^\-]) # label start, not: -
(?:%|[^\p{Z}\p{Sm}\p{Sc}\p{Sk}\p{C}\p{P}]|-){1,63} # label not: whitespace, mathematical, currency, modifier symbol, control point, punctuation | except -
(?<=[^\-]) # label end, not: -
)*
\.(?<tld>\w{2,63}) # tld
)
)
(?:\:(?<port>\d{1,5}(?!\d)))? # port
(?<path> # path, query, fragment
[\/?#] # prefixed with \/ or ? or #
[^\s<>]* # any chars except whitespace and <>
(?<=[^\s<>({\[`!;:\'".,?«»“”‘’]) # end with not a space or some punctuation chars
)?
' . $suffix . '/ixuJ';
}
/**
* @return string
*/
public function next_url() {
while ( true ) {
$this->matched_url = null;
$this->parsed_url = null;
$this->url_starts_at = null;
$this->url_length = null;
$this->did_prepend_protocol = false;
/**
* Thick sieve – eagerly match things that look like URLs but turn out to not be URLs in the end.
*/
$matches = array();
$found = preg_match( $this->regex, $this->text, $matches, PREG_OFFSET_CAPTURE, $this->bytes_already_parsed );
if ( 1 !== $found ) {
return false;
}
$this->matched_url = $matches[0][0];
// Do not consider just :: as a URL.
if ( '::' === $this->matched_url ) {
continue;
}
if (
')' === $this->matched_url[ strlen( $this->matched_url ) - 1 ] ||
'.' === $this->matched_url[ strlen( $this->matched_url ) - 1 ]
) {
$this->matched_url = substr( $this->matched_url, 0, - 1 );
}
$url_starts_at = $matches[0][1];
$this->bytes_already_parsed = $url_starts_at + strlen( $this->matched_url );
$had_protocol = WPURL::has_http_https_protocol( $this->matched_url );
$preprocessed_url = $this->matched_url;
if ( $this->base_url && $this->base_protocol && ! $had_protocol ) {
$preprocessed_url = WPURL::ensure_protocol( $preprocessed_url, $this->base_protocol );
$this->did_prepend_protocol = true;
}
/*
* Extra fine sieve – parse the candidates using a WHATWG-compliant parser to rule out false positives.
*/
$parsed_url = WPURL::parse( $preprocessed_url, $this->base_url );
if ( false === $parsed_url ) {
continue;
}
// Only consider HTTP and HTTPS URLs.
if ( $parsed_url->protocol && ! in_array( $parsed_url->protocol, array( 'http:', 'https:' ), true ) ) {
continue;
}
// Disregard URLs with auth details.
if ( $parsed_url->username || $parsed_url->password ) {
continue;
}
// Additional rigor for URLs that are not explicitly preceded by a double slash.
if ( ! $had_protocol ) {
/*
* Skip TLDs that are not in the public suffix.
* This reduces false positives like `index.html` or `plugins.php`.
*
* See https://publicsuffix.org/.
*/
$last_dot_position = strrpos( $parsed_url->hostname, '.' );
if ( false === $last_dot_position ) {
/*
* Oh, there was no dot in the hostname AND no double slash at
* the beginning! Let's assume this isn't a valid URL and move on.
* @TODO: Explore updating the regular expression above to avoid matching
* URLs without a dot in the hostname when they're not preceeded
* by a protocol.
*/
continue;
}
$tld = substr( $parsed_url->hostname, $last_dot_position + 1 );
if ( ! WPURL::is_known_public_domain( $tld ) ) {
// This TLD is not in the public suffix list. It's not a valid domain name.
continue;
}
}
$this->parsed_url = $parsed_url;
$this->url_starts_at = $url_starts_at;
$this->url_length = strlen( $matches[0][0] );
return true;
}
}
public function get_raw_url() {
return $this->matched_url ?? false;
}
public function get_parsed_url() {
if ( null === $this->parsed_url ) {
return false;
}
return $this->parsed_url;
}
public function set_raw_url( $new_url ) {
if ( null === $this->matched_url ) {
return false;
}
if ( $this->did_prepend_protocol ) {
$new_url = substr( $new_url, strpos( $new_url, '://' ) + 3 );
}
$this->matched_url = $new_url;
$this->lexical_updates[ $this->url_starts_at ] = new WP_HTML_Text_Replacement(
$this->url_starts_at,
$this->url_length,
$new_url
);
return true;
}
private function apply_lexical_updates() {
if ( ! count( $this->lexical_updates ) ) {
return 0;
}
/*
* Attribute updates can be enqueued in any order but updates
* to the document must occur in lexical order; that is, each
* replacement must be made before all others which follow it
* at later string indices in the input document.
*
* Sorting avoid making out-of-order replacements which
* can lead to mangled output, partially-duplicated
* attributes, and overwritten attributes.
*/
ksort( $this->lexical_updates );
$bytes_already_copied = 0;
$output_buffer = '';
foreach ( $this->lexical_updates as $diff ) {
$shift = strlen( $diff->text ) - $diff->length;
// Adjust the cursor position by however much an update affects it.
if ( $diff->start < $this->bytes_already_parsed ) {
$this->bytes_already_parsed += $shift;
}
$output_buffer .= substr( $this->text, $bytes_already_copied, $diff->start - $bytes_already_copied );
if ( $diff->start === $this->url_starts_at ) {
$this->url_starts_at = strlen( $output_buffer );
$this->url_length = strlen( $diff->text );
}
$output_buffer .= $diff->text;
$bytes_already_copied = $diff->start + $diff->length;
}
$this->text = $output_buffer . substr( $this->text, $bytes_already_copied );
$this->lexical_updates = array();
}
public function get_updated_text() {
$this->apply_lexical_updates();
return $this->text;
}
}
@@ -0,0 +1,264 @@
<?php
namespace WordPress\DataLiberation\URL;
use VendorPrefix\Rowbot\URL\URL;
/**
* An abstraction to make swapping the URL parser easier later on.
* We do not need it in the long run. It adds extra overhead of the
* function call – let's remove it once the URL parser is decided.
*/
class WPURL {
public static function parse( $url, $base = null ) {
if ( is_string( $url ) ) {
return URL::parse( $url, $base ) ?? false;
} elseif ( is_a( $url, 'VendorPrefix\Rowbot\URL\URL' ) ) {
return $url;
}
return false;
}
public static function can_parse( $url, $base = null ) {
return URL::canParse( $url, $base );
}
/**
* Replaces the "base" of a URL — scheme, host (and port), and the portion of the path that
* belongs to the old base — with a new base while keeping the remainder of the URL intact.
*
* This is intended for content migrations, where URLs embedded in block markup, HTML attributes,
* or inline text must be moved from one site root to another without losing the rest of the path,
* query, or fragment. It handles simple domain swaps, ports, and deep path bases. When the old
* base includes path segments, only that matched prefix is substituted and the unmatched tail is
* carried over to the target base.
*
* For example:
* * URL: https://example.com/a/b/c/d/e/f/g/h/i/j/page/
* * Old base: https://example.com/a/b/c/d/e/f/
* * New base: https://example.org/docs/
* * Result: https://example.org/docs/g/h/i/j/page/
*
* ## Trailing slash handling
*
* Trailing slash style is preserved from the original URL. If it has no trailing slash, the
* result will also omit the trailing slash and vice versa.
*
* For example, here the final result has no trailing slash:
* * URL: https://example.com/uploads/file.txt
* * Old base: https://example.com/uploads/
* * New base: https://example.org/docs/
* * Result: https://example.org/docs/file.txt
*
* And here it does:
* * URL: https://example.com/uploads/2018/
* * Old base: https://example.com/uploads/
* * New base: https://example.org/docs/
* * Result: https://example.org/docs/2018/
*
* ## URL-encoded path segments
*
* URL-encoded path segments are respected and not inadvertently decoded or re-encoded. Only the
* matched base prefix is considered for alignment, so inputs that contain percent-encoded content
* keep that content exactly as-is in the output. This prevents data corruption in tricky cases such
* as "/~jappleseed/1997.10.1/%2561-reasons-to-migrate-data/" where the "%2561" must remain
* double-escaped after the move.
*
* ## Relative URLs
*
* This method can preserve the relative nature of the original URL. Say you are processing a markup
* that contains `<a href="/uploads/file.txt">`. The original URL string is "/uploads/file.txt",
* and the URL actually resolves to "https://example.com/uploads/file.txt". If you want to replace
* the base URL from "https://example.com/uploads/" to "https://newsite.com/files/" but keep the
* URL relative, you can pass the raw URL string via the "raw_url" option.
*
* For example:
* * URL: https://example.com/uploads/file.txt
* * Raw URL: /uploads/file.txt
* * Old base: https://example.com/uploads/
* * New base: https://example.org/files/
* * Result: /files/file.txt
*
* The method also supports relative inputs commonly found in markup. If you pass the raw URL
* string via the "raw_url" option, the method can infer whether the author originally wrote a
* relative URL like "docs/page.html" or an absolute one. You may also explicitly
* assert relativity with "is_relative" to avoid inference.
*
* @param string|URL $url The URL to replace the base of.
* @param array $options Associative options: old_base_url, new_base_url; optional raw_url.
* @return ConvertedUrl|false Returns a ConvertedUrl value object on success, or false when parsing
* or replacement cannot be performed.
*/
public static function replace_base_url( $url, $options ) {
if ( ! is_array( $options ) ) {
return false;
}
foreach ( array( 'old_base_url', 'new_base_url' ) as $required ) {
if ( ! array_key_exists( $required, $options ) || null === $options[ $required ] ) {
return false;
}
}
$old_base_url = self::parse( $options['old_base_url'] );
$new_base_url = self::parse( $options['new_base_url'] );
$url = self::parse( $url, $old_base_url ? $old_base_url->toString() : null );
if ( false === $old_base_url || false === $new_base_url || false === $url ) {
return false;
}
$updated_url = clone $url;
$updated_url->hostname = $new_base_url->hostname;
$updated_url->protocol = $new_base_url->protocol;
$updated_url->port = $new_base_url->port;
$from_pathname = $url->pathname;
$to_pathname = $new_base_url->pathname;
$base_pathname = $old_base_url->pathname;
if ( $base_pathname !== $to_pathname ) {
$base_pathname_with_trailing_slash = rtrim( $base_pathname, '/' ) . '/';
$decoded_matched_pathname = urldecode_n(
$from_pathname,
strlen( $base_pathname_with_trailing_slash )
);
$to_pathname_with_trailing_slash = rtrim( $to_pathname, '/' ) . '/';
$remaining_pathname = substr(
$decoded_matched_pathname,
strlen( $base_pathname_with_trailing_slash )
);
$updated_url->pathname = $to_pathname_with_trailing_slash . $remaining_pathname;
}
/*
* Stylistic choice – if the updated URL has no trailing slash,
* do not add it to the new URL. The WHATWG URL parser will
* add one automatically if the path is empty, so we have to
* explicitly remove it.
*/
$new_raw_url = $updated_url->toString();
$should_trim_trailing_slash = (
'' !== $from_pathname &&
'/' !== substr( $from_pathname, -1 ) &&
'/' !== $from_pathname &&
'' === $url->search &&
'' === $url->hash
);
if ( $should_trim_trailing_slash ) {
$new_raw_url = rtrim( $new_raw_url, '/' );
}
if ( ! $new_raw_url ) {
// This may technically happen, but does it happen in practice?
return false;
}
$converted_url = new ConvertedUrl();
$converted_url->new_url = $updated_url;
$converted_url->new_raw_url = $new_raw_url;
// Preserve the relative nature of the original URL.
if ( array_key_exists( 'raw_url', $options ) && is_string( $options['raw_url'] ) ) {
if ( ! array_key_exists( 'is_relative', $options ) ) {
$options['is_relative'] = self::can_parse( $options['raw_url'] );
}
if ( $options['is_relative'] ) {
$relative_url = $updated_url->pathname;
// Remove the trailing slash if it's not the root path.
if ( strlen( $relative_url ) > 1 && $should_trim_trailing_slash ) {
$relative_url = rtrim( $relative_url, '/' );
}
if ( '' !== $updated_url->search ) {
$relative_url .= $updated_url->search;
}
if ( '' !== $updated_url->hash ) {
$relative_url .= $updated_url->hash;
}
$converted_url->was_relative = true;
$converted_url->new_raw_relative_url = $relative_url;
}
}
return $converted_url;
}
/**
* Prepends a protocol to any matched URL without the double slash.
*
* Imagine we have a base URL of `https://example.com` and a text like `Visit myblog.com`.
* This Processor would match `myblog.com` as a URL candidate and then the parser
* would parse it as `https://example.com/myblog.com`, which is not what a user would expect.
*
* To get `https://myblog.com`, we need to prepend a protocol and turn that candidate into
* `https://myblog.com` before parsing.
*/
public static function ensure_protocol( $raw_url, $protocol = 'https' ) {
if ( ! self::has_http_https_protocol( $raw_url ) ) {
$raw_url = $protocol . '://' . $raw_url;
}
return $raw_url;
}
/**
* This method only considers http and https protocols.
*/
public static function has_http_https_protocol( $raw_url ) {
return (
(
// Protocol-relative URLs.
strlen( $raw_url ) > 2 &&
'/' === $raw_url[0] &&
'/' === $raw_url[1]
) || (
strlen( $raw_url ) > 5 &&
( 'h' === $raw_url[0] || 'H' === $raw_url[0] ) &&
( 't' === $raw_url[1] || 'T' === $raw_url[1] ) &&
( 't' === $raw_url[2] || 'T' === $raw_url[2] ) &&
( 'p' === $raw_url[3] || 'P' === $raw_url[3] ) &&
':' === $raw_url[4]
) || (
strlen( $raw_url ) > 6 &&
( 'h' === $raw_url[0] || 'H' === $raw_url[0] ) &&
( 't' === $raw_url[1] || 'T' === $raw_url[1] ) &&
( 't' === $raw_url[2] || 'T' === $raw_url[2] ) &&
( 'p' === $raw_url[3] || 'P' === $raw_url[3] ) &&
( 's' === $raw_url[4] || 'S' === $raw_url[4] ) &&
':' === $raw_url[5]
)
);
}
public static function append_path( $base_url, $path ) {
$base_url = self::parse( $base_url );
$base_url->pathname = rtrim( $base_url->pathname, '/' ) . '/' . ltrim( $path, '/' );
return $base_url->toString();
}
/**
* Checks if a TLD is in the known public domain suffix list.
* This reduces false positives like `index.html` or `plugins.php`.
*
* @see https://publicsuffix.org/
*
* @param string $tld The top-level domain to check.
* @return bool True if the TLD is a known public domain, false otherwise.
*/
public static function is_known_public_domain( $tld ) {
static $public_suffix_list = null;
if ( null === $public_suffix_list ) {
$public_suffix_list = require_once __DIR__ . '/public-suffix-list.php';
}
// @TODO: Parse wildcards and exceptions from the public suffix list.
$tld = strtolower( $tld );
return ! empty( $public_suffix_list[ $tld ] ) || 'internal' === $tld;
}
}
@@ -0,0 +1,164 @@
<?php
namespace WordPress\DataLiberation\URL;
use VendorPrefix\Rowbot\URL\URL;
use WordPress\DataLiberation\BlockMarkup\BlockMarkupUrlProcessor;
/**
* Migrate URLs in post content. See WPRewriteUrlsTests for
* specific examples. TODO: A better description.
*
* Example:
*
* ```php
* php > wp_rewrite_urls([
* 'block_markup' => '<!-- wp:image {"src": "http://legacy-blog.com/image.jpg"} -->',
* 'url-mapping' => [
* 'http://legacy-blog.com' => 'https://modern-webstore.org'
* ]
* ])
* <!-- wp:image {"src":"https:\/\/modern-webstore.org\/image.jpg"} -->
* ```
*
* @TODO Use a proper JSON parser and encoder to:
* * Support UTF-16 characters
* * Gracefully handle recoverable encoding issues
* * Avoid changing the whitespace in the same manner as
* we do in WP_HTML_Tag_Processor. e.g. if we start with:
*
* ```html
* <!-- wp:block {"url":"https://w.org"}` -->
* ^ no space here
* ```
*
* then it would be nice to re-encode that block markup also without the space character. This is similar
* to how the tag processor avoids changing parts of the tag it doesn't need to change.
*/
function wp_rewrite_urls( $options ) {
if ( empty( $options['base_url'] ) ) {
// Use first from-url as base_url if not specified.
$from_urls = array_keys( $options['url-mapping'] );
$options['base_url'] = $from_urls[0];
}
$url_mapping = array();
foreach ( $options['url-mapping'] as $from_url_string => $to_url_string ) {
$url_mapping[] = array(
'from_url' => WPURL::parse( $from_url_string ),
'to_url' => WPURL::parse( $to_url_string ),
);
}
$p = new BlockMarkupUrlProcessor( $options['block_markup'], $options['base_url'] );
while ( $p->next_url() ) {
$parsed_url = $p->get_parsed_url();
foreach ( $url_mapping as $mapping ) {
if ( is_child_url_of( $parsed_url, $mapping['from_url'] ) ) {
$p->replace_base_url( $mapping['to_url'] );
break;
}
}
}
return $p->get_updated_html();
}
/**
* Check if a given URL matches the current site URL.
*
* @param URL $child The URL to check.
* @param string $parent_url The current site URL to compare against.
*
* @return bool Whether the URL matches the current site URL.
*/
function is_child_url_of( $child, $parent_url ) {
$parent_url = is_string( $parent_url ) ? WPURL::parse( $parent_url ) : $parent_url;
$child = is_string( $child ) ? WPURL::parse( $child ) : $child;
$child_pathname_no_trailing_slash = rtrim( urldecode( $child->pathname ), '/' );
if ( false === $child || false === $parent_url ) {
return false;
}
if ( $parent_url->hostname !== $child->hostname ) {
return false;
}
if ( $parent_url->protocol !== $child->protocol ) {
return false;
}
$parent_pathname = urldecode( $parent_url->pathname );
return (
// Direct match.
$parent_pathname === $child_pathname_no_trailing_slash ||
$parent_pathname === $child_pathname_no_trailing_slash . '/' ||
// Path prefix.
0 === strncmp( $child_pathname_no_trailing_slash . '/', $parent_pathname, strlen( $parent_pathname ) )
);
}
/**
* Decodes the first n **encoded bytes** a URL-encoded string.
*
* For example, `urldecode_n( '%22is 6 %3C 6?%22 – asked Achilles', 1 )` returns
* '"is 6 %3C 6?%22 – asked Achilles' because only the first encoded byte is decoded.
*
* @param string $input The string to decode.
* @param int $decode_n The number of bytes to decode in $input
*
* @return string The decoded string.
*/
function urldecode_n( $input, $decode_n ) {
// Fast paths: nothing to do.
if ( $decode_n <= 0 || false === strpos( $input, '%' ) ) {
return $input;
}
$result = '';
$at = 0;
while ( true ) {
if ( $at + 3 > strlen( $input ) ) {
break;
}
$last_at = $at;
$at += strcspn( $input, '%', $at );
// Consume bytes except for the percent sign.
$result .= substr( $input, $last_at, $at - $last_at );
// If we've already decoded the requested number of bytes, stop.
if ( strlen( $result ) >= $decode_n ) {
break;
}
++$at;
if ( $at > strlen( $input ) ) {
break;
}
$decodable_length = strspn(
$input,
'0123456789ABCDEFabcdef',
$at,
2
);
if ( 2 === $decodable_length ) {
// Decodes the urlencoded hex sequence from URL.
// Note: This decodes bytes, not characters. It will recover the original byte sequence,
// not necessarily any valid UTF-8 characters.
$result .= chr( hexdec( $input[ $at ] . $input[ $at + 1 ] ) );
$at += 2;
} else {
// Consume the next byte and move on.
$result .= '%';
}
}
$result .= substr( $input, $at );
return $result;
}