Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Nov 20, 2024
1 parent 5dd3237 commit 6ca3184
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class WP_File_Reader implements WP_Byte_Reader {
protected $chunk_size;
protected $file_pointer;
protected $offset_in_file;
protected $output_bytes = '';
protected $output_bytes = '';
protected $last_chunk_size = 0;
protected $last_error;
protected $state = self::STATE_STREAMING;
Expand Down Expand Up @@ -37,7 +37,7 @@ public function resume( $paused_state ): bool {
_doing_it_wrong( __METHOD__, 'Cannot resume a file reader that is already initialized.', '1.0.0' );
return false;
}
$this->offset_in_file = $paused_state['offset_in_file'];
$this->offset_in_file = $paused_state['offset_in_file'];
$this->last_chunk_size = 0;
return true;
}
Expand All @@ -55,7 +55,7 @@ public function get_last_error(): string|null {
}

public function next_bytes(): bool {
$this->output_bytes = '';
$this->output_bytes = '';
$this->last_chunk_size = 0;
if ( $this->last_error || $this->is_finished() ) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ class WP_Attachment_Downloader {
private $client;
private $fps = array();
private $output_root;
private $output_paths = array();
private $output_paths = array();

private $current_event;
private $pending_events = array();
private $pending_events = array();
private $downloads_so_far = 0;
private $last_enqueued_resource_id;
private $enqueued_resource_id;

public function __construct( $output_root ) {
$this->client = new Client();
Expand All @@ -24,7 +24,7 @@ public function has_pending_requests() {
}

public function enqueue_if_not_exists( $url, $output_path ) {
$this->last_enqueued_resource_id = null;
$this->enqueued_resource_id = null;

$output_path = $this->output_root . '/' . ltrim( $output_path, '/' );
if ( file_exists( $output_path ) ) {
Expand All @@ -51,44 +51,30 @@ public function enqueue_if_not_exists( $url, $output_path ) {
if ( false === $local_path ) {
return false;
}

// Just copy the file over.
// @TODO: think through the chmod of the created file.

$this->last_enqueued_resource_id = 'file:' . $this->downloads_so_far;
$this->pending_events[] = new WP_Attachment_Downloader_Event(
WP_Attachment_Downloader_Event::STARTED,
$url,
$output_path,
$this->last_enqueued_resource_id
);
$success = copy( $local_path, $output_path );
$this->pending_events[] = new WP_Attachment_Downloader_Event(
$this->enqueued_resource_id = 'file:' . $this->downloads_so_far;
$success = copy( $local_path, $output_path );
$this->pending_events[] = new WP_Attachment_Downloader_Event(
$this->enqueued_resource_id,
$success ? WP_Attachment_Downloader_Event::SUCCESS : WP_Attachment_Downloader_Event::FAILURE,
$url,
$output_path,
$this->last_enqueued_resource_id
);
return true;
case 'http':
case 'https':
$request = new Request( $url );
$this->last_enqueued_resource_id = 'http:' . $request->id;
$this->enqueued_resource_id = 'http:' . $request->id;
$this->output_paths[ $request->id ] = $output_path;
$this->pending_events[] = new WP_Attachment_Downloader_Event(
WP_Attachment_Downloader_Event::STARTED,
$url,
$output_path,
$this->last_enqueued_resource_id
);
$this->client->enqueue( $request );
return true;
}
return false;
}

public function get_last_enqueued_resource_id() {
return $this->last_enqueued_resource_id;
public function get_enqueued_resource_id() {
return $this->enqueued_resource_id;
}

public function queue_full() {
Expand Down Expand Up @@ -149,10 +135,8 @@ public function poll() {
}
}
$this->pending_events[] = new WP_Attachment_Downloader_Event(
'http:' . $original_request_id,
WP_Attachment_Downloader_Event::FAILURE,
$request->original_request()->url,
$this->output_paths[ $original_request_id ],
'http:' . $original_request_id
);
unset( $this->output_paths[ $original_request_id ] );
break;
Expand All @@ -171,10 +155,8 @@ public function poll() {
}
}
$this->pending_events[] = new WP_Attachment_Downloader_Event(
'http:' . $original_request_id,
WP_Attachment_Downloader_Event::SUCCESS,
$request->original_request()->url,
$this->output_paths[ $original_request_id ],
'http:' . $original_request_id
);
unset( $this->output_paths[ $original_request_id ] );
}
Expand All @@ -185,22 +167,3 @@ public function poll() {
return true;
}
}

class WP_Attachment_Downloader_Event {

const STARTED = '#started';
const SUCCESS = '#success';
const FAILURE = '#failure';

public $type;
public $url;
public $target_path;
public $resource_id;

public function __construct( $type, $url, $target_path, $resource_id ) {
$this->type = $type;
$this->url = $url;
$this->target_path = $target_path;
$this->resource_id = $resource_id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

class WP_Attachment_Downloader_Event {

const SUCCESS = '#success';
const FAILURE = '#failure';

public $type;
public $resource_id;

public function __construct( $resource_id, $type ) {
$this->resource_id = $resource_id;
$this->type = $type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function pause() {
}

public function resume( $paused_state ) {
$this->state = $paused_state['state'];
$this->state = $paused_state['state'];
$this->entities_cursor = $paused_state['entities_cursor'];
// @TODO: Should resume() call next_step() or just prepare the state?
$this->next_step();
Expand Down Expand Up @@ -163,17 +163,17 @@ public function advance_frontloading_cursor() {
switch ( $event->type ) {
case WP_Attachment_Downloader_Event::SUCCESS:
case WP_Attachment_Downloader_Event::FAILURE:
foreach( $this->frontloading_state as $state ) {
foreach ( $this->frontloading_state as $state ) {
$resource_id = $event->resource_id;
unset( $state['active_downloads'][ $resource_id ] );
}
break;
}
}

while(count($this->frontloading_state) > 0) {
while ( count( $this->frontloading_state ) > 0 ) {
$oldest_download_key = key( $this->frontloading_state );
if( ! empty( $this->frontloading_state[ $oldest_download_key ]['active_downloads'] ) ) {
if ( ! empty( $this->frontloading_state[ $oldest_download_key ]['active_downloads'] ) ) {
break;
}
// Advance the cursor to the next entity.
Expand All @@ -193,7 +193,7 @@ public function frontload_assets_step() {
if ( null === $this->entities_iterator ) {
$factory = $this->entity_iterator_factory;
$this->entities_iterator = $factory();
if( $this->entities_cursor ) {
if ( $this->entities_cursor ) {
$this->entities_iterator->resume( $this->entities_cursor );
}
$this->downloader = new WP_Attachment_Downloader( $this->options );
Expand Down Expand Up @@ -225,7 +225,7 @@ public function frontload_assets_step() {
* fix it later on. In a CLI-based Blueprint step importer scenario, we
* might want to provide an "image not found" placeholder OR ignore the
* failure.
*
*
* @TODO: Update the download progress:
* * After every downloaded file.
* * For large files, every time a full megabyte is downloaded above 10MB.
Expand All @@ -237,17 +237,17 @@ public function frontload_assets_step() {
* Identify the static assets referenced in the current entity
* and enqueue them for download.
*/
$entity = $this->entities_iterator->current();
$entity_key = $this->entities_iterator->key();
$entity = $this->entities_iterator->current();
$entity_key = $this->entities_iterator->key();
$this->frontloading_state[ $entity_key ] = array(
'key' => $entity_key,
// @TODO: Consider making key() and pause() the same thing.
// Then seek() and resume() would be the same, too!
'cursor' => $this->entities_iterator->pause(),
'active_downloads' => array()
'active_downloads' => array(),
);

$data = $entity->get_data();
$data = $entity->get_data();
switch ( $entity->get_type() ) {
case 'site_option':
if ( $data['option_name'] === 'home' ) {
Expand Down Expand Up @@ -297,7 +297,7 @@ public function import_entities_step() {
$factory = $this->entity_iterator_factory;
$this->entities_iterator = $factory();
$this->importer = new WP_Entity_Importer();
if( $this->entities_cursor ) {
if ( $this->entities_cursor ) {
$this->entities_iterator->resume( $this->entities_cursor );
}
}
Expand All @@ -310,7 +310,7 @@ public function import_entities_step() {
return;
}

$entity = $this->entities_iterator->current();
$entity = $this->entities_iterator->current();
$attachments = array();
// Rewrite the URLs in the post.
switch ( $entity->get_type() ) {
Expand Down Expand Up @@ -373,7 +373,7 @@ protected function enqueue_attachment_download( string $raw_url, $context_path =
$enqueued = $this->downloader->enqueue_if_not_exists( $url, $output_path );
if ( $enqueued ) {
$resource_id = $this->downloader->get_last_enqueued_resource_id();
$entity_key = $this->entities_iterator->key();
$entity_key = $this->entities_iterator->key();
$this->frontloading_state[ $entity_key ]['active_downloads'][ $resource_id ] = true;
}
return $enqueued;
Expand Down
6 changes: 3 additions & 3 deletions packages/playground/data-liberation/src/wxr/WP_WXR_Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,9 @@ public function pause() {
// XML breadcrumbs and the parsing context. Perhaps the cursor could
// be a class with a method such as "override_byte_offset(int $offset)"?
// Or resume() could have an optional argument?
$xml_state = $this->xml->pause();
$xml_state = $this->xml->pause();
$xml_state['token_starts_at_in_current_chunk'] = 0;
$xml_state['upstream_bytes_forgotten'] = $this->entity_byte_offset;
$xml_state['upstream_bytes_forgotten'] = $this->entity_byte_offset;
return array(
'xml' => $xml_state,
'upstream' => $upstream_state,
Expand Down Expand Up @@ -901,7 +901,7 @@ public function next(): void {
}

public function key(): string {
return sha1(json_encode($this->pause()));
return sha1( json_encode( $this->pause() ) );
}

public function valid(): bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,11 +689,11 @@ public function pause() {
* – Validate the paused state, return false if it's invalid.
*/
public function resume( $paused_state ) {
$this->bytes_already_parsed = $paused_state['token_starts_at_in_current_chunk'];
$this->bytes_already_parsed = $paused_state['token_starts_at_in_current_chunk'];
$this->upstream_bytes_forgotten = $paused_state['upstream_bytes_forgotten'];
$this->stack_of_open_elements = $paused_state['stack_of_open_elements'];
$this->parser_context = $paused_state['parser_context'];
$this->expecting_more_input = $paused_state['expecting_more_input'];
$this->stack_of_open_elements = $paused_state['stack_of_open_elements'];
$this->parser_context = $paused_state['parser_context'];
$this->expecting_more_input = $paused_state['expecting_more_input'];
$this->next_token();
}

Expand Down

0 comments on commit 6ca3184

Please sign in to comment.