Skip to content

Commit b9f5edb

Browse files
authored
[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.
1 parent 869f6bd commit b9f5edb

20 files changed

+251
-85
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use KevinGH\Box\Compactor\Compactor;
4+
5+
class DataLiberationBoxCompactor implements Compactor
6+
{
7+
/**
8+
* {@inheritdoc}
9+
*/
10+
public function compact(string $file, string $contents): string
11+
{
12+
if (!preg_match('/\.(php|json|lock)$/', $file)) {
13+
return '';
14+
}
15+
16+
if (
17+
str_contains($file, 'platform_check.php') ||
18+
str_contains($file, '/tests/') ||
19+
str_contains($file, '/.git/') ||
20+
str_contains($file, '/.github/') ||
21+
str_contains($file, '/bin/')
22+
) {
23+
return '';
24+
}
25+
26+
if( str_contains($contents, 'Your Composer dependencies require ') ) {
27+
return '';
28+
}
29+
30+
31+
return $contents;
32+
}
33+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
$box_base_path = dirname(getenv('BOX_BASE_PATH'));
4+
require_once $box_base_path . '/../autoload.php';
5+
require_once __DIR__ . '/DataLiberationBoxCompactor.php';
6+
require_once $box_base_path . '/box';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
require_once __DIR__ . '/../../bootstrap.php';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../../../data-liberation/dist/data-liberation-core.phar.gz';
4+
require_once __DIR__ . '/../../dist/data-liberation-markdown.phar';
5+
6+
/**
7+
* None of this will actually try to parse a file or import
8+
* any data. We're just making sure the importer can
9+
* be created without throwing an exception.
10+
*/
11+
$markdown_root = __DIR__ . '/markdown-test-data';
12+
$c = WP_Markdown_Importer::create_for_markdown_directory(
13+
$markdown_root,
14+
array(
15+
'source_site_url' => 'file://' . $markdown_root,
16+
'local_markdown_assets_root' => $markdown_root,
17+
'local_markdown_assets_url_prefix' => '@site/',
18+
),
19+
$import['cursor'] ?? null
20+
);
21+
22+
echo 'Markdown importer created!';
23+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
$file = $argv[1];
4+
$phar = new Phar($file);
5+
$phar->startBuffering();
6+
7+
8+
/**
9+
* Box, includes an autoloader with a fixed name in every build.
10+
* However, we want to load two .phar files built with Box, not
11+
* one. Unfortunately this yields an error:
12+
*
13+
* Cannot declare class ComposerAutoloaderInitHumbugBox451
14+
*
15+
* Therefore, we're giving all the HumbugBox classes a unique suffix.
16+
*/
17+
$autoloadSuffix = substr(md5(__FILE__), 0, 8);
18+
foreach (new RecursiveIteratorIterator($phar) as $file) {
19+
if(!$file->isFile()) {
20+
continue;
21+
}
22+
$relativePath = $file->getPathname();
23+
$relativePath = str_replace('phar://', '', $relativePath);
24+
$relativePath = str_replace($phar->getPath().'/', '', $relativePath);
25+
$contents = $file->getContent();
26+
$updated_contents = $contents;
27+
foreach([
28+
'InitHumbugBox',
29+
] as $class) {
30+
$updated_contents = str_replace($class, $class . $autoloadSuffix, $updated_contents);
31+
}
32+
if($updated_contents !== $contents) {
33+
$phar[$relativePath] = $updated_contents;
34+
}
35+
}
36+
37+
/**
38+
* Box, very annoyingly, force-adds a platform_check.php file
39+
* into the final built .phar archive. The vendor libraries
40+
* do work with a PHP version lower than 8.1 enforced by that
41+
* platform_check.php file, so let's just truncate it.
42+
*/
43+
$phar['vendor/composer/platform_check.php'] = '';
44+
$phar['.box/bin/check-requirements.php'] = '';
45+
$phar->stopBuffering();
46+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/box-project/box/refs/heads/main/res/schema.json",
3+
"main": "src/bootstrap.php",
4+
"output": "dist/data-liberation-markdown.phar",
5+
"force-autodiscovery": true,
6+
"compactors": [
7+
"KevinGH\\Box\\Compactor\\Php",
8+
"DataLiberationBoxCompactor"
9+
],
10+
"annotations": false,
11+
"directories": ["src/", "vendor/"]
12+
}

packages/playground/data-liberation-markdown/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"name": "wordpress/data-liberation-markdown",
23
"name": "wordpress/data-liberation-markdown",
34
"prefer-stable": true,
45
"require": {

packages/playground/data-liberation-markdown/dist/.gitkeep

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
# Builds the standalone dist/core-data-liberation.phar.gz file meant for
4+
# use in the importWxr Blueprint step.
5+
#
6+
# This is a temporary measure until we have a canonical way of distributing,
7+
# versioning, and using the Data Liberation modules and their dependencies.
8+
# Possible solutions might include composer packages, WordPress plugins, or
9+
# tree-shaken zip files with each module and its composer deps.
10+
11+
set -e
12+
echo "Building data liberation plugin"
13+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
14+
DATA_LIBERATION_DIR=$SCRIPT_DIR
15+
BUILD_DIR=$DATA_LIBERATION_DIR/bin/build
16+
DIST_DIR=$DATA_LIBERATION_DIR/dist
17+
18+
rm $DIST_DIR/* > /dev/null 2>&1 || true
19+
export BOX_BASE_PATH=$(type -a box | grep -v 'alias' | awk '{print $3}')
20+
php $BUILD_DIR/box.php compile -d $DATA_LIBERATION_DIR -c $DATA_LIBERATION_DIR/box.json
21+
php -d 'phar.readonly=0' $BUILD_DIR/truncate-composer-checks.php $DIST_DIR/data-liberation-markdown.phar
22+
php $BUILD_DIR/smoke-test.php
23+
PHP=8.0 bun $DATA_LIBERATION_DIR/../../php-wasm/cli/src/main.ts $BUILD_DIR/smoke-test.php
24+
cd $DIST_DIR
25+
gzip data-liberation-markdown.phar
26+
ls -sgh $DIST_DIR

0 commit comments

Comments
 (0)