Skip to content

Commit de7ff45

Browse files
authored
[Data Liberation] Filesystem entity reader (#2125)
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.
1 parent e4b3c87 commit de7ff45

File tree

11 files changed

+666
-349
lines changed

11 files changed

+666
-349
lines changed

packages/playground/data-liberation-markdown/src/WP_Markdown_Importer.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use WordPress\Filesystem\WP_Filesystem;
3+
use WordPress\Filesystem\WP_Local_Filesystem;
44

55
class WP_Markdown_Importer extends WP_Stream_Importer {
66

@@ -9,13 +9,13 @@ public static function create_for_markdown_directory( $markdown_directory, $opti
99
function ( $cursor = null ) use ( $markdown_directory ) {
1010
// @TODO: Handle $cursor
1111
return new WP_Directory_Tree_Entity_Reader(
12-
new WP_Filesystem(),
12+
new WP_Local_Filesystem(),
1313
array(
1414
'root_dir' => $markdown_directory,
1515
'first_post_id' => 1,
1616
'allowed_extensions' => array( 'md' ),
1717
'index_file_patterns' => array( '#^index\.md$#' ),
18-
'markup_converter_factory' => function ( $content ) {
18+
'data_consumer_factory' => function ( $content ) {
1919
return new WP_Markdown_Consumer( $content );
2020
},
2121
)

packages/playground/data-liberation/bootstrap.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
require_once __DIR__ . '/blueprints-library/src/WordPress/AsyncHttp/Client.php';
1313

1414
require_once __DIR__ . '/blueprints-library/src/WordPress/Filesystem/WP_Abstract_Filesystem.php';
15-
require_once __DIR__ . '/blueprints-library/src/WordPress/Filesystem/WP_Filesystem.php';
15+
require_once __DIR__ . '/blueprints-library/src/WordPress/Filesystem/WP_Local_Filesystem.php';
1616
require_once __DIR__ . '/blueprints-library/src/WordPress/Filesystem/WP_File_Visitor_Event.php';
1717
require_once __DIR__ . '/blueprints-library/src/WordPress/Filesystem/WP_Filesystem_Visitor.php';
18+
require_once __DIR__ . '/blueprints-library/src/WordPress/Filesystem/functions.php';
1819

1920
require_once __DIR__ . '/blueprints-library/src/WordPress/ByteReader/WP_Byte_Reader.php';
2021
require_once __DIR__ . '/blueprints-library/src/WordPress/ByteReader/WP_File_Reader.php';
@@ -71,7 +72,6 @@
7172
require_once __DIR__ . '/src/entity-readers/WP_HTML_Entity_Reader.php';
7273
require_once __DIR__ . '/src/entity-readers/WP_EPub_Entity_Reader.php';
7374
require_once __DIR__ . '/src/entity-readers/WP_WXR_Entity_Reader.php';
74-
require_once __DIR__ . '/src/entity-readers/WP_Directory_Tree_Entity_Reader.php';
7575

7676
require_once __DIR__ . '/src/xml-api/WP_XML_Decoder.php';
7777
require_once __DIR__ . '/src/xml-api/WP_XML_Processor.php';
@@ -87,6 +87,9 @@
8787
require_once __DIR__ . '/src/import/WP_Retry_Frontloading_Iterator.php';
8888
require_once __DIR__ . '/src/entity-readers/WP_Entity_Reader.php';
8989
require_once __DIR__ . '/src/entity-readers/WP_HTML_Entity_Reader.php';
90+
require_once __DIR__ . '/src/entity-readers/WP_Filesystem_Entity_Reader.php';
91+
92+
require_once __DIR__ . '/src/WP_Data_Liberation_HTML_Processor.php';
9093

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

packages/playground/data-liberation/phpunit.xml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<file>tests/WPMarkupProcessorConsumerTests.php</file>
99
<file>tests/WPHTMLEntityReaderTests.php</file>
1010
<file>tests/WPURLInTextProcessorTests.php</file>
11+
<file>tests/WPFilesystemEntityReaderTests.php</file>
1112
<file>tests/WPBlockMarkupProcessorTests.php</file>
1213
<file>tests/WPBlockMarkupUrlProcessorTests.php</file>
1314
<file>tests/URLParserWHATWGComplianceTests.php</file>

packages/playground/data-liberation/src/WP_Data_Liberation_HTML_Processor.php

+8
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,12 @@ public function skip_to_closer() {
5050

5151
return false;
5252
}
53+
54+
public function get_string_index_after_current_token() {
55+
$name = 'current_token';
56+
$this->set_bookmark( $name );
57+
$bookmark = $this->bookmarks[ '_' . $name ];
58+
$this->release_bookmark( $name );
59+
return $bookmark->start + $bookmark->length;
60+
}
5361
}

0 commit comments

Comments
 (0)