Skip to content

Commit

Permalink
[Data Liberation] Filesystem entity reader
Browse files Browse the repository at this point in the history
A part of #1894

Adds a new API for loading content from a WP_Filesystem instance:

* `WP_Filesystem_To_Post_Tree` for traversing a directory tree and
  mapping the structure a hierarchical WordPress post/meta entity stream
* `WP_Filesystem_Entity_Reader` for converting the raw file content into
  WordPress blocks

To convert a set of zipped files into WordPress entities:

```php
// Any Filesystem instance works here. Could be WP_Local_Filesystem,
// WP_Git_Filesystem, or anything else. Let's read from a zip file here:
$fs = new WP_Zip_Filesystem(
    WP_File_Reader::create('./docs.zip')
);

$reader = new WP_Filesystem_Entity_Reader( $fs );

foreach($reader as $entity) {
    var_dump($entity);
}
```

 ## Testing

The code isn't used anywhere yet – just rely on the CI checks.
  • Loading branch information
adamziel committed Jan 10, 2025
1 parent 4c2964a commit 77f8446
Show file tree
Hide file tree
Showing 10 changed files with 633 additions and 347 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use WordPress\Filesystem\WP_Filesystem;
use WordPress\Filesystem\WP_Local_Filesystem;

class WP_Markdown_Importer extends WP_Stream_Importer {

Expand All @@ -9,13 +9,13 @@ public static function create_for_markdown_directory( $markdown_directory, $opti
function ( $cursor = null ) use ( $markdown_directory ) {
// @TODO: Handle $cursor
return new WP_Directory_Tree_Entity_Reader(
new WP_Filesystem(),
new WP_Local_Filesystem(),
array(
'root_dir' => $markdown_directory,
'first_post_id' => 1,
'allowed_extensions' => array( 'md' ),
'index_file_patterns' => array( '#^index\.md$#' ),
'markup_converter_factory' => function ( $content ) {
'data_consumer_factory' => function ( $content ) {
return new WP_Markdown_Consumer( $content );
},
)
Expand Down
5 changes: 4 additions & 1 deletion packages/playground/data-liberation/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
require_once __DIR__ . '/blueprints-library/src/WordPress/AsyncHttp/Client.php';

require_once __DIR__ . '/blueprints-library/src/WordPress/Filesystem/WP_Abstract_Filesystem.php';
require_once __DIR__ . '/blueprints-library/src/WordPress/Filesystem/WP_Filesystem.php';
require_once __DIR__ . '/blueprints-library/src/WordPress/Filesystem/WP_Local_Filesystem.php';
require_once __DIR__ . '/blueprints-library/src/WordPress/Filesystem/WP_File_Visitor_Event.php';
require_once __DIR__ . '/blueprints-library/src/WordPress/Filesystem/WP_Filesystem_Visitor.php';
require_once __DIR__ . '/blueprints-library/src/WordPress/Filesystem/functions.php';

require_once __DIR__ . '/blueprints-library/src/WordPress/ByteReader/WP_Byte_Reader.php';
require_once __DIR__ . '/blueprints-library/src/WordPress/ByteReader/WP_File_Reader.php';
Expand Down Expand Up @@ -87,6 +88,8 @@
require_once __DIR__ . '/src/import/WP_Retry_Frontloading_Iterator.php';
require_once __DIR__ . '/src/entity-readers/WP_Entity_Reader.php';
require_once __DIR__ . '/src/entity-readers/WP_HTML_Entity_Reader.php';
require_once __DIR__ . '/src/entity-readers/WP_Filesystem_To_Post_Tree.php';
require_once __DIR__ . '/src/entity-readers/WP_Filesystem_Entity_Reader.php';

require_once __DIR__ . '/src/utf8_decoder.php';

Expand Down
1 change: 1 addition & 0 deletions packages/playground/data-liberation/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<file>tests/WPMarkupProcessorConsumerTests.php</file>
<file>tests/WPHTMLEntityReaderTests.php</file>
<file>tests/WPURLInTextProcessorTests.php</file>
<file>tests/WPFilesystemToPostTreeTests.php</file>
<file>tests/WPBlockMarkupProcessorTests.php</file>
<file>tests/WPBlockMarkupUrlProcessorTests.php</file>
<file>tests/URLParserWHATWGComplianceTests.php</file>
Expand Down

This file was deleted.

Loading

0 comments on commit 77f8446

Please # to comment.