-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Data Liberation] Build markdown importer as phar (#2094)
Builds data-liberation-markdown.phar.gz (200KB) to enable downloading the Markdown importer only when needed instead of on every page load. A part of: * #2080 * #1894 ## Testing instructions Run `nx build playground-data-liberation-markdown`, confirm it finished without errors. A smoke test of the built phar file is included in the build command.
- Loading branch information
Showing
20 changed files
with
251 additions
and
85 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
packages/playground/data-liberation-markdown/bin/build/DataLiberationBoxCompactor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
use KevinGH\Box\Compactor\Compactor; | ||
|
||
class DataLiberationBoxCompactor implements Compactor | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function compact(string $file, string $contents): string | ||
{ | ||
if (!preg_match('/\.(php|json|lock)$/', $file)) { | ||
return ''; | ||
} | ||
|
||
if ( | ||
str_contains($file, 'platform_check.php') || | ||
str_contains($file, '/tests/') || | ||
str_contains($file, '/.git/') || | ||
str_contains($file, '/.github/') || | ||
str_contains($file, '/bin/') | ||
) { | ||
return ''; | ||
} | ||
|
||
if( str_contains($contents, 'Your Composer dependencies require ') ) { | ||
return ''; | ||
} | ||
|
||
|
||
return $contents; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/playground/data-liberation-markdown/bin/build/box.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
$box_base_path = dirname(getenv('BOX_BASE_PATH')); | ||
require_once $box_base_path . '/../autoload.php'; | ||
require_once __DIR__ . '/DataLiberationBoxCompactor.php'; | ||
require_once $box_base_path . '/box'; |
2 changes: 2 additions & 0 deletions
2
packages/playground/data-liberation-markdown/bin/build/core.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<?php | ||
require_once __DIR__ . '/../../bootstrap.php'; |
23 changes: 23 additions & 0 deletions
23
packages/playground/data-liberation-markdown/bin/build/smoke-test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/../../../data-liberation/dist/data-liberation-core.phar.gz'; | ||
require_once __DIR__ . '/../../dist/data-liberation-markdown.phar'; | ||
|
||
/** | ||
* None of this will actually try to parse a file or import | ||
* any data. We're just making sure the importer can | ||
* be created without throwing an exception. | ||
*/ | ||
$markdown_root = __DIR__ . '/markdown-test-data'; | ||
$c = WP_Markdown_Importer::create_for_markdown_directory( | ||
$markdown_root, | ||
array( | ||
'source_site_url' => 'file://' . $markdown_root, | ||
'local_markdown_assets_root' => $markdown_root, | ||
'local_markdown_assets_url_prefix' => '@site/', | ||
), | ||
$import['cursor'] ?? null | ||
); | ||
|
||
echo 'Markdown importer created!'; | ||
|
46 changes: 46 additions & 0 deletions
46
packages/playground/data-liberation-markdown/bin/build/truncate-composer-checks.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
$file = $argv[1]; | ||
$phar = new Phar($file); | ||
$phar->startBuffering(); | ||
|
||
|
||
/** | ||
* Box, includes an autoloader with a fixed name in every build. | ||
* However, we want to load two .phar files built with Box, not | ||
* one. Unfortunately this yields an error: | ||
* | ||
* Cannot declare class ComposerAutoloaderInitHumbugBox451 | ||
* | ||
* Therefore, we're giving all the HumbugBox classes a unique suffix. | ||
*/ | ||
$autoloadSuffix = substr(md5(__FILE__), 0, 8); | ||
foreach (new RecursiveIteratorIterator($phar) as $file) { | ||
if(!$file->isFile()) { | ||
continue; | ||
} | ||
$relativePath = $file->getPathname(); | ||
$relativePath = str_replace('phar://', '', $relativePath); | ||
$relativePath = str_replace($phar->getPath().'/', '', $relativePath); | ||
$contents = $file->getContent(); | ||
$updated_contents = $contents; | ||
foreach([ | ||
'InitHumbugBox', | ||
] as $class) { | ||
$updated_contents = str_replace($class, $class . $autoloadSuffix, $updated_contents); | ||
} | ||
if($updated_contents !== $contents) { | ||
$phar[$relativePath] = $updated_contents; | ||
} | ||
} | ||
|
||
/** | ||
* Box, very annoyingly, force-adds a platform_check.php file | ||
* into the final built .phar archive. The vendor libraries | ||
* do work with a PHP version lower than 8.1 enforced by that | ||
* platform_check.php file, so let's just truncate it. | ||
*/ | ||
$phar['vendor/composer/platform_check.php'] = ''; | ||
$phar['.box/bin/check-requirements.php'] = ''; | ||
$phar->stopBuffering(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"$schema": "https://raw.githubusercontent.com/box-project/box/refs/heads/main/res/schema.json", | ||
"main": "src/bootstrap.php", | ||
"output": "dist/data-liberation-markdown.phar", | ||
"force-autodiscovery": true, | ||
"compactors": [ | ||
"KevinGH\\Box\\Compactor\\Php", | ||
"DataLiberationBoxCompactor" | ||
], | ||
"annotations": false, | ||
"directories": ["src/", "vendor/"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Binary file added
BIN
+191 KB
packages/playground/data-liberation-markdown/dist/data-liberation-markdown.phar.gz
Binary file not shown.
26 changes: 26 additions & 0 deletions
26
packages/playground/data-liberation-markdown/phar-build.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/bin/bash | ||
|
||
# Builds the standalone dist/core-data-liberation.phar.gz file meant for | ||
# use in the importWxr Blueprint step. | ||
# | ||
# This is a temporary measure until we have a canonical way of distributing, | ||
# versioning, and using the Data Liberation modules and their dependencies. | ||
# Possible solutions might include composer packages, WordPress plugins, or | ||
# tree-shaken zip files with each module and its composer deps. | ||
|
||
set -e | ||
echo "Building data liberation plugin" | ||
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
DATA_LIBERATION_DIR=$SCRIPT_DIR | ||
BUILD_DIR=$DATA_LIBERATION_DIR/bin/build | ||
DIST_DIR=$DATA_LIBERATION_DIR/dist | ||
|
||
rm $DIST_DIR/* > /dev/null 2>&1 || true | ||
export BOX_BASE_PATH=$(type -a box | grep -v 'alias' | awk '{print $3}') | ||
php $BUILD_DIR/box.php compile -d $DATA_LIBERATION_DIR -c $DATA_LIBERATION_DIR/box.json | ||
php -d 'phar.readonly=0' $BUILD_DIR/truncate-composer-checks.php $DIST_DIR/data-liberation-markdown.phar | ||
php $BUILD_DIR/smoke-test.php | ||
PHP=8.0 bun $DATA_LIBERATION_DIR/../../php-wasm/cli/src/main.ts $BUILD_DIR/smoke-test.php | ||
cd $DIST_DIR | ||
gzip data-liberation-markdown.phar | ||
ls -sgh $DIST_DIR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<ruleset name="WordPressStandard"> | ||
<description>PHP 7.0 compatibility.</description> | ||
<config name="testVersion" value="7.2"/> | ||
<exclude-pattern>vendor/*</exclude-pattern> | ||
<rule ref="PHPCompatibility"> | ||
<exclude name="PHPCompatibility.Keywords.ForbiddenNamesAsDeclared"/> | ||
</rule> | ||
<rule ref="WordPress-Core"> | ||
<exclude name="Generic.Commenting.DocComment.MissingShort"/> | ||
<exclude name="Generic.PHP.DiscourageGoto.Found"/> | ||
<exclude name="Generic.CodeAnalysis.EmptyStatement.DetectedIf"/> | ||
<!-- Unused arguments are necessary when inheriting from classes and overriding methods. --> | ||
<exclude name="Generic.CodeAnalysis.UnusedFunctionParameter.Found"/> | ||
<exclude name="Squiz.PHP.NonExecutableCode.Unreachable"/> | ||
<exclude name="Squiz.Commenting.BlockComment.CloserSameLine"/> | ||
<exclude name="Squiz.Commenting.ClassComment.Missing"/> | ||
<exclude name="Squiz.Commenting.FileComment.WrongStyle"/> | ||
<exclude name="Squiz.Commenting.FileComment.Missing"/> | ||
<exclude name="Squiz.Commenting.FunctionComment.Missing"/> | ||
<exclude name="Squiz.Commenting.FunctionComment.MissingParamTag"/> | ||
<exclude name="Squiz.Commenting.FunctionComment.MissingParamType"/> | ||
<exclude name="Squiz.Commenting.FunctionComment.MissingParamComment"/> | ||
<exclude name="Squiz.Commenting.VariableComment.Missing"/> | ||
<exclude name="Squiz.PHP.CommentedOutCode.Found"/> | ||
<!-- "Parameter comment must end with a full stop" is such a pebble in the shoe. --> | ||
<exclude name="Squiz.Commenting.FunctionComment.ParamCommentFullStop"/> | ||
<exclude name="Squiz.PHP.DisallowSizeFunctionsInLoops.Found"/> | ||
<!-- Aligning the 1500 lines of public_suffix_list.php adds a lot of unnecessary noise and then | ||
the actual indentation is not even correct because the rule seems to cound bytes, not printable | ||
UTF-8 characteds. --> | ||
<exclude name="WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned"/> | ||
<exclude name="WordPress.Files.FileName.InvalidClassFileName"/> | ||
<exclude name="WordPress.Files.FileName.NotHyphenatedLowercase"/> | ||
<exclude name="WordPress.PHP.YodaConditions.NotYoda"/> | ||
<exclude name="WordPress.Security.EscapeOutput.OutputNotEscaped"/> | ||
<exclude name="WordPress.WP.AlternativeFunctions"/> | ||
<exclude name="WordPress.WP.AlternativeFunctions.file_system_operations_fclose"/> | ||
</rule> | ||
</ruleset> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"name": "playground-data-liberation-markdown", | ||
"$schema": "../../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "packages/playground/data-liberation-markdown", | ||
"projectType": "library", | ||
"targets": { | ||
"install": { | ||
"executor": "nx:run-commands", | ||
"options": { | ||
"cwd": "packages/playground/data-liberation-markdown", | ||
"commands": ["composer install"], | ||
"parallel": false | ||
} | ||
}, | ||
"build:phar": { | ||
"executor": "nx:run-commands", | ||
"options": { | ||
"cwd": "packages/playground/data-liberation-markdown", | ||
"commands": ["bash ./phar-build.sh"], | ||
"parallel": false | ||
}, | ||
"dependsOn": ["playground-data-liberation:build:phar"] | ||
}, | ||
"lint:php": { | ||
"executor": "nx:run-commands", | ||
"options": { | ||
"cwd": "packages/playground/data-liberation-markdown", | ||
"commands": [ | ||
"../data-liberation/vendor/bin/phpcs --standard=./phpcs.xml -s ./src ./*.php" | ||
], | ||
"parallel": false | ||
} | ||
}, | ||
"lint:php:fix": { | ||
"executor": "nx:run-commands", | ||
"options": { | ||
"cwd": "packages/playground/data-liberation-markdown", | ||
"commands": [ | ||
"../data-liberation/vendor/bin/phpcbf --standard=./phpcs.xml ./src" | ||
], | ||
"parallel": false | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule blueprints-library
updated
4 files
+2 −2 | README.md | |
+3 −0 | composer.json | |
+1 −1 | src/WordPress/AsyncHttp/Client.php | |
+0 −10 | src/WordPress/AsyncHttp/Request.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
-10.4 KB
(97%)
packages/playground/data-liberation/dist/data-liberation-core.phar.gz
Binary file not shown.
8 changes: 8 additions & 0 deletions
8
packages/playground/data-liberation/src/block-markup/WP_Block_Markup_Converter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
interface WP_Block_Markup_Converter { | ||
public function convert(); | ||
public function get_block_markup(); | ||
public function get_all_metadata(); | ||
public function get_meta_value( $key ); | ||
} |
75 changes: 0 additions & 75 deletions
75
packages/playground/data-liberation/src/import/WP_Markdown_Importer.php
This file was deleted.
Oops, something went wrong.