Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 60 additions & 27 deletions packages/playground/wordpress/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { PHP, UniversalPHP } from '@php-wasm/universal';
import { joinPaths, phpVar } from '@php-wasm/util';
import { unzipFile, createMemoizedFetch } from '@wp-playground/common';
import { logger } from '@php-wasm/logger';

export {
bootWordPress,
bootRequestHandler,
Expand Down Expand Up @@ -529,20 +531,60 @@ export async function unzipWordPress(php: PHP, wpZip: File) {
}
}

if (
php.isDir(php.documentRoot) &&
isCleanDirContainingSiteMetadata(php.documentRoot, php)
) {
// We cannot mv the directory over a non-empty directory,
// but we can move the children one by one.
for (const file of php.listFiles(wpPath)) {
const sourcePath = joinPaths(wpPath, file);
const targetPath = joinPaths(php.documentRoot, file);
php.mv(sourcePath, targetPath);
const moveRecursively = (source: string, target: string, php: PHP) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far, I've avoided moving this into FS helpers because it is an odd sort of operation:
Move a single directory, or if a directory already exists at the target path, move its children.

If there are existing files at the target paths, those files will be left intact, and the source files will not be moved. (But they will be deleted as leftovers of the unzipped WP dir later on.)

if (php.fileExists(target)) {
/*
* Something exists at the target path.
* Let's check to make sure we aren't copying conflicting types.
*
* In this context, if the source path points to a file and the
* target path points to a directory, we do not intend for the file
* to be moved into the directory.
*/

if (!php.isDir(source) && php.isDir(target)) {
throw new Error(
`The target ${target} is a directory but the source ${source} is not. This is not supported.`
);
}
if (php.isDir(source) && !php.isDir(target)) {
throw new Error(
`The source ${source} is a directory but the target ${target} is not. This is not supported.`
);
}
}

if (isNonEmptyDir(target, php)) {
// We cannot move a directory over a non-empty directory,
// so we move the children one by one.
for (const file of php.listFiles(source)) {
const sourcePath = joinPaths(source, file);
const targetPath = joinPaths(target, file);
moveRecursively(sourcePath, targetPath, php);
}
} else {
if (php.fileExists(target) && !php.isDir(target)) {
// Refuse to overwrite existing files to avoid the chance of data loss.
const wpPath = source.replace(
/^\/tmp\/unzipped-wordpress\//,
'/'
);
logger.warn(
`Skipping ${wpPath} because a file exists at the target path.`
);
return;
}
Copy link
Member Author

@brandonpayton brandonpayton Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned that we might overwrite existing files. It is an mv-style operation, but the FSHelpers.mv() copies recursively if moving between different filesystems. And that can result in user data loss.

So we are skipping files that already exist.

php.mv(source, target);
}
};
try {
moveRecursively(wpPath, php.documentRoot, php);
// Remove any directories left because there were existing dirs at the target path.
if (php.fileExists(wpPath)) {
php.rmdir(wpPath, { recursive: true });
}
php.rmdir(wpPath, { recursive: true });
} else {
php.mv(wpPath, php.documentRoot);
} catch (e) {
throw e;
}

if (
Expand All @@ -558,21 +600,12 @@ export async function unzipWordPress(php: PHP, wpZip: File) {
}
}

function isCleanDirContainingSiteMetadata(path: string, php: PHP) {
const files = php.listFiles(path);
if (files.length === 0) {
return true;
function isNonEmptyDir(path: string, php: PHP) {
if (!php.isDir(path)) {
return false;
}

if (
files.length === 1 &&
// TODO: use a constant from a site storage package
files[0] === 'playground-site-metadata.json'
) {
return true;
}

return false;
const files = php.listFiles(path);
return files.length > 0;
}

const memoizedFetch = createMemoizedFetch(fetch);
Expand Down
Loading