Skip to content

Commit

Permalink
Migrate import logic to PHP
Browse files Browse the repository at this point in the history
The code now uses ZipArchive via the worker thread for
both the export and import logic.

This commit removes reference to JSZip, as it is no longer
needed.

It also modifies the calls to workerThread.run() to correspond
to the method's new syntax.
  • Loading branch information
artemiomorales committed Jan 20, 2023
1 parent f3fad90 commit 7bc02d5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 51 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@
"is-reference": "^3.0.0",
"jest": "^29.3.1",
"jest-esbuild": "^0.2.9",
"jszip": "^3.10.1",
"live-server": "^1.2.2",
"magic-string": "^0.26.7",
"npm-run-all": "^4.1.5",
Expand Down
106 changes: 56 additions & 50 deletions src/wordpress-playground/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
} from '../php-wasm-browser/index';
import { ProgressObserver, ProgressType } from './progress-observer';
import { PromiseQueue } from './promise-queue';
import JSZip from 'jszip';
import { saveAs } from 'file-saver';

const query = new URL(document.location.href).searchParams as any;
Expand Down Expand Up @@ -310,44 +309,46 @@ async function overwriteFile() {
}

async function generateZip() {
await workerThread.run(`
<?php
$zip = new ZipArchive;
$res = $zip->open('/wordpress-playground-export.zip', ZipArchive::CREATE);
if ($res === TRUE) {
$directories = array();
$directories[] = '/wordpress/';
while(sizeof($directories)) {
$dir = array_pop($directories);
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
if ($entry == '.' || $entry == '..') {
continue;
}
await workerThread.run({
code: `
<?php
$zip = new ZipArchive;
$res = $zip->open('/wordpress-playground-export.zip', ZipArchive::CREATE);
if ($res === TRUE) {
$directories = array();
$directories[] = '/wordpress/';
while(sizeof($directories)) {
$dir = array_pop($directories);
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
if ($entry == '.' || $entry == '..') {
continue;
}
$entry = $dir . $entry;
$entry = $dir . $entry;
if (is_dir($entry)) {
if (is_dir($entry)) {
$directory_path = $entry . '/';
array_push($directories, $directory_path);
$directory_path = $entry . '/';
array_push($directories, $directory_path);
} elseif (is_file($entry)) {
} elseif (is_file($entry)) {
$zip->addFile($entry);
$zip->addFile($entry);
}
}
closedir($handle);
}
closedir($handle);
}
$zip->close();
}
$zip->close();
}
?>
`);
?>
`,
});
const fileBuffer = await workerThread.readFileAsBuffer(
'/wordpress-playground-export.zip'
);
Expand All @@ -357,27 +358,32 @@ async function generateZip() {

async function importFile() {
const selectedFile = document.getElementById('file-input').files[0];

JSZip.loadAsync(selectedFile) // 1) read the Blob
.then(
function (zip) {
zip.forEach(async function (relativePath, zipEntry: any) {
// 2) print entries
if (
!zipEntry.dir &&
!relativePath.includes('wp-content/database') &&
!relativePath.includes('wp-includes')
) {
console.log(relativePath);
const content = new TextDecoder().decode(
zipEntry._data.compressedContent
);
await workerThread.writeFile(relativePath, content);
const fileContents = await selectedFile.arrayBuffer();
await workerThread.writeFile('/import.zip', new Uint8Array(fileContents));
await workerThread.run({
code: `
<?php
$zip = new ZipArchive;
$res = $zip->open('/import.zip');
if ($res === TRUE) {
$counter = 0;
while ($zip->statIndex($counter)) {
$file = $zip->statIndex($counter);
$fileString .= $file['name'] . ',';
if (
strpos($file['name'], 'wp-content/database') === false &&
strpos($file['name'], 'wp-includes') === false
) {
$overwrite = fopen($file['name'], 'w');
fwrite($overwrite, $zip->getFromIndex($counter));
}
$counter++;
}
});
},
function (e) {}
);
$zip->close();
}
?>
`,
});
}

const overwriteButton = document.getElementById('overwrite-button');
Expand Down

0 comments on commit 7bc02d5

Please sign in to comment.