Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 8 additions & 8 deletions components/Blueprints/SiteResolver/class-newsiteresolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ public static function resolve( Runtime $runtime, Tracker $progress, ?VersionCon
private static function is_wordpress_installed( Runtime $runtime, Tracker $progress ) {
$install_check = $runtime->eval_php_code_in_subprocess(
<<<'PHP'
<?php
$wp_load = getenv('DOCROOT') . '/wp-load.php';
if (!file_exists($wp_load)) {
append_output('0');
exit;
}
require $wp_load;
<?php
$wp_load = getenv('DOCROOT') . '/wp-load.php';
if (!file_exists($wp_load)) {
append_output('0');
exit;
}
require $wp_load;

append_output( function_exists('is_blog_installed') && is_blog_installed() ? '1' : '0' );
append_output( function_exists('is_blog_installed') && is_blog_installed() ? '1' : '0' );
PHP
,
array(
Expand Down
6 changes: 3 additions & 3 deletions components/Blueprints/Steps/scripts/import-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ function map_file_path_to_wordpress_url( $path ) {
* to the format they will have after being imported into WordPress (e.g. /docs/getting-started).
*/
add_action(
'data_liberation.stream_importer.postprocess_url',
'data_liberation_stream_importer_postprocess_url',
function (
$processor,
$context
Expand Down Expand Up @@ -471,7 +471,7 @@ function (
* Assigns post_name to every imported static page.
*/
add_filter(
'data_liberation.stream_importer.preprocess_entity',
'data_liberation_stream_importer_preprocess_entity',
function ( $entity ) use ( &$import_path_prefix, $index_file_pattern ) {
static $preprocessed_an_entity = false;
if ( 'post' !== $entity->get_type() ) {
Expand Down Expand Up @@ -560,7 +560,7 @@ function ( $entity ) use ( &$import_path_prefix, $index_file_pattern ) {
* Drop .xhtml extension from the links.
*/
add_action(
'data_liberation.stream_importer.postprocess_url',
'data_liberation_stream_importer_postprocess_url',
function ( $processor ) {
$parsed_url = $processor->get_parsed_url();
if ( ! str_ends_with( $parsed_url->pathname, '.xhtml' ) ) {
Expand Down
45 changes: 29 additions & 16 deletions components/DataLiberation/Importer/class-attachmentdownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class AttachmentDownloader {
*/
private $source_from_filesystem;

private $current_event;
private $pending_events = array();
private $enqueued_url;
private $progress = array();
Expand Down Expand Up @@ -151,19 +150,15 @@ public function queue_full() {
return count( $this->client->get_active_requests() ) >= 10;
}

public function get_event() {
return $this->current_event;
}

public function next_event() {
$this->current_event = null;
if ( 0 === count( $this->pending_events ) ) {
return false;
}

$this->current_event = array_shift( $this->pending_events );

return true;
/**
* Returns and clears all pending events.
*
* @return AttachmentDownloaderEvent[]
*/
public function get_events() {
$events = $this->pending_events;
$this->pending_events = array();
return $events;
}

public function poll() {
Expand Down Expand Up @@ -194,6 +189,7 @@ public function poll() {
if ( file_exists( $this->output_paths[ $original_request_id ] . '.partial' ) ) {
unlink( $this->output_paths[ $original_request_id ] . '.partial' );
}
echo $this->output_paths[ $original_request_id ] . "\n";
$fp = fopen( $this->output_paths[ $original_request_id ] . '.partial', 'wb' );
if ( false !== $fp ) {
$this->fps[ $original_request_id ] = $fp;
Expand Down Expand Up @@ -232,7 +228,10 @@ public function poll() {

private function on_failure( $original_url, $original_request_id, $error = null ) {
if ( isset( $this->fps[ $original_request_id ] ) ) {
fclose( $this->fps[ $original_request_id ] );
if ( is_resource( $this->fps[ $original_request_id ] ) ) {
fclose( $this->fps[ $original_request_id ] );
}
unset( $this->fps[ $original_request_id ] );
}
if ( isset( $this->output_paths[ $original_request_id ] ) ) {
$partial_file = $this->output_paths[ $original_request_id ] . '.partial';
Expand All @@ -252,7 +251,10 @@ private function on_failure( $original_url, $original_request_id, $error = null
private function on_success( $original_url, $original_request_id ) {
// Only clean up if this was the last request in the chain.
if ( isset( $this->fps[ $original_request_id ] ) ) {
fclose( $this->fps[ $original_request_id ] );
if ( is_resource( $this->fps[ $original_request_id ] ) ) {
fclose( $this->fps[ $original_request_id ] );
}
unset( $this->fps[ $original_request_id ] );
}
if ( isset( $this->output_paths[ $original_request_id ] ) ) {
if ( false === rename(
Expand All @@ -269,4 +271,15 @@ private function on_success( $original_url, $original_request_id ) {
unset( $this->progress[ $original_url ] );
unset( $this->output_paths[ $original_request_id ] );
}

public function __destruct() {
// Ensure any remaining open file descriptors are closed.
foreach ( $this->fps as $request_id => $fp ) {
if ( is_resource( $fp ) ) {
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
@fclose( $fp );
}
unset( $this->fps[ $request_id ] );
}
}
}
15 changes: 7 additions & 8 deletions components/DataLiberation/Importer/class-streamimporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,7 @@ public function get_frontloading_progress() {
* the relevant files already exist in the filesystem.
*/
protected function frontloading_advance_reentrancy_cursor() {
while ( $this->downloader->next_event() ) {
$event = $this->downloader->get_event();
foreach ( $this->downloader->get_events() as $event ) {
switch ( $event->type ) {
case AttachmentDownloaderEvent::FAILURE:
case AttachmentDownloaderEvent::SUCCESS:
Expand Down Expand Up @@ -743,7 +742,7 @@ protected function frontload_next_entity() {
public function get_current_entity() {
$entity = $this->entity_iterator->current();
$entity = apply_filters(
'data_liberation.stream_importer.preprocess_entity',
'data_liberation_stream_importer_preprocess_entity',
$entity,
array(
'importer' => $this,
Expand All @@ -755,7 +754,7 @@ public function get_current_entity() {

protected function get_post_base_url( $post ) {
return apply_filters(
'data_liberation.stream_importer.post_base_url',
'data_liberation_stream_importer_post_base_url',
$post['link'] ?? $this->source_site_url,
array(
'post' => $post,
Expand Down Expand Up @@ -873,12 +872,12 @@ protected function import_next_entity() {
$p->replace_base_url( $mapping_pair['to'], $mapping_pair['from'] );
}
do_action(
'data_liberation.stream_importer.postprocess_url',
'data_liberation_stream_importer_postprocess_url',
$p,
array(
'applied_base_url_mapping' => $mapping_pair,
'raw_url_before' => $raw_url_before,
'entity' => $entity,
'raw_url_before' => $raw_url_before,
'entity' => $entity,
)
);
}
Expand Down Expand Up @@ -1037,7 +1036,7 @@ protected function url_processor_matched_asset_url( BlockMarkupUrlProcessor $p )
*/
$path = $p->get_parsed_url()->pathname;
$extension = pathinfo( $path, PATHINFO_EXTENSION );
if ( ! in_array( $extension, array( 'jpg', 'jpeg', 'png', 'gif', 'webp', 'svg' ) ) ) {
if ( ! in_array( $extension, array( 'jpg', 'jpeg', 'png', 'gif', 'webp', 'svg' ), true ) ) {
/**
* Absent an extension, try to guess whether it's a static asset based
* on its location in the document. For now, we only accept images.
Expand Down
8 changes: 4 additions & 4 deletions components/Filesystem/class-inmemoryfilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ protected function get_root(): string {
return '/';
}

public function ls( $parent = '/' ) {
if ( ! isset( $this->files[ $parent ] ) || 'dir' !== $this->files[ $parent ]['type'] ) {
public function ls( $dir = '/' ) {
if ( ! isset( $this->files[ $dir ] ) || 'dir' !== $this->files[ $dir ]['type'] ) {
throw new FilesystemException(
sprintf( 'Directory not found: %s', $parent )
sprintf( 'Directory not found: %s', $dir )
);
}

return array_keys( $this->files[ $parent ]['contents'] );
return array_keys( $this->files[ $dir ]['contents'] );
}

public function is_dir( $path ) {
Expand Down
3 changes: 3 additions & 0 deletions components/Filesystem/class-localfilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function rename( $old_path, $new_path, $options = array() ) {
}

public function copy_file( $from_path, $to_path, $options ) {
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
if ( false === @copy( $from_path, $to_path ) ) {
throw new FilesystemException(
sprintf( 'Failed to copy file: %s to %s', $from_path, $to_path )
Expand All @@ -134,6 +135,7 @@ protected function mkdir_single( $path, $options = array() ) {
);
}
if ( isset( $options['chmod'] ) ) {
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
if ( false === @chmod( $path, $options['chmod'] ) ) {
throw new FilesystemException(
sprintf( 'Failed to chmod directory: %s', $path )
Expand All @@ -159,6 +161,7 @@ protected function rmdir_single( $path, $options = array() ) {
}

public function put_contents( $path, $data, $options = array() ) {
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
if ( false === @file_put_contents(
$path,
$data
Expand Down
6 changes: 3 additions & 3 deletions components/Filesystem/class-uploadedfilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ private function __construct( $request, $tree_parameter_name, $options = array()
$this->uploads_fs = $options['uploads_fs'] ?? LocalFilesystem::create( '/' );
}

public function ls( $parent = '/' ) {
$parent = '/' . ltrim( wp_unix_path_resolve_dots( $parent ), '/' );
$node = $this->find_node( $parent );
public function ls( $parent_path = '/' ) {
$parent_path = '/' . ltrim( wp_unix_path_resolve_dots( $parent_path ), '/' );
$node = $this->find_node( $parent_path );
if ( ! $node || 'directory' !== $node['type'] ) {
return array();
}
Expand Down
4 changes: 2 additions & 2 deletions components/Filesystem/interface-filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ interface Filesystem {
/**
* List the contents of a directory.
*
* @param string $parent The path to the parent directory.
* @param string $dir The path to the parent directory.
*
* @return array<string> The contents of the directory.
* @throws FilesystemException If the directory cannot be listed.
*/
public function ls( $parent = '/' );
public function ls( $dir = '/' );

/**
* Check if a path is a directory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function handle_redirect( Request $request ) {
return;
}
$code = $response->status_code;
if ( ! in_array( $code, array( 301, 302, 303, 307, 308 ) ) ) {
if ( ! in_array( $code, array( 301, 302, 303, 307, 308 ), true ) ) {
return;
}

Expand Down
13 changes: 10 additions & 3 deletions components/HttpClient/Transport/class-sockettransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ protected function open_nonblocking_http_sockets( $requests ) {
$url = $request->url;
$parts = parse_url( $url );
$scheme = $parts['scheme'];
if ( ! in_array( $scheme, array( 'http', 'https' ) ) ) {
if ( ! in_array( $scheme, array( 'http', 'https' ), true ) ) {
$this->set_error(
$request,
new HttpError( 'stream_http_open_nonblocking: Invalid scheme in URL ' . $url . ' – only http:// and https:// URLs are supported' )
Expand All @@ -153,6 +153,7 @@ protected function open_nonblocking_http_sockets( $requests ) {
)
);

// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
$stream = @stream_socket_client(
'tcp://' . $host . ':' . $port,
$errno,
Expand Down Expand Up @@ -200,7 +201,7 @@ protected function decode_and_monitor_response_body_stream( Request $request ) {
}

$content_encoding = $request->response->get_header( 'content-encoding' );
if ( $content_encoding && ! in_array( $content_encoding, $transfer_encodings ) ) {
if ( $content_encoding && ! in_array( $content_encoding, $transfer_encodings, true ) ) {
$transfer_encodings[] = $content_encoding;
}

Expand Down Expand Up @@ -249,9 +250,11 @@ protected function decode_and_monitor_response_body_stream( Request $request ) {
*/
protected function enable_crypto( array $requests ) {
foreach ( $this->stream_select( $requests, static::STREAM_SELECT_WRITE ) as $request ) {
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
@stream_set_timeout( $this->state->connections[ $request->id ]->http_socket, 1 );

// Use @ to suppress warnings. They're collected by error_get_last().
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
$enabled_crypto = @stream_socket_enable_crypto(
$this->state->connections[ $request->id ]->http_socket,
true,
Expand Down Expand Up @@ -282,6 +285,7 @@ protected function enable_crypto( array $requests ) {
protected function send_request_headers( array $requests ) {
foreach ( $this->stream_select( $requests, static::STREAM_SELECT_WRITE ) as $request ) {
$header_bytes = static::prepare_request_headers( $request );
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
if ( false === @fwrite( $this->state->connections[ $request->id ]->http_socket, $header_bytes ) ) {
$last_error = error_get_last();
$last_error_message = is_array( $last_error ) ? $last_error['message'] : 'unknown';
Expand Down Expand Up @@ -330,6 +334,7 @@ protected function send_request_body( array $requests ) {
}

$chunk = $request->upload_body_stream->consume( $available_bytes );
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
if ( ! @fwrite( $this->state->connections[ $request->id ]->http_socket, $chunk ) ) {
$last_error = error_get_last();
$last_error_message = is_array( $last_error ) ? $last_error['message'] : 'unknown';
Expand Down Expand Up @@ -360,6 +365,7 @@ protected function receive_response_headers( $requests ) {
if (
! $this->state->connections[ $request->id ]->http_socket ||
! is_resource( $this->state->connections[ $request->id ]->http_socket ) ||
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
@feof( $this->state->connections[ $request->id ]->http_socket )
) {
$this->set_error( $request, new HttpError( 'Connection closed while reading response headers.' ) );
Expand All @@ -372,6 +378,7 @@ protected function receive_response_headers( $requests ) {
if (
! $this->state->connections[ $request->id ]->http_socket ||
! is_resource( $this->state->connections[ $request->id ]->http_socket ) ||
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
@feof( $this->state->connections[ $request->id ]->http_socket )
) {
$this->set_error( $request, new HttpError( 'Connection closed while reading response headers.' ) );
Expand Down Expand Up @@ -493,7 +500,7 @@ protected function filter_requests( array $requests, $states ) {
}
$results = array();
foreach ( $requests as $request ) {
if ( in_array( $request->state, $states ) ) {
if ( in_array( $request->state, $states, true ) ) {
$results[] = $request;
}
}
Expand Down
2 changes: 1 addition & 1 deletion components/HttpClient/class-clientstate.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public static function filter_requests_by_state( array $requests, $states ) {
}
$results = array();
foreach ( $requests as $request ) {
if ( in_array( $request->state, $states ) ) {
if ( in_array( $request->state, $states, true ) ) {
$results[] = $request;
}
}
Expand Down
6 changes: 3 additions & 3 deletions components/Zip/class-zipfilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ private function __construct( ByteReadStream $byte_reader ) {
$this->byte_reader = $byte_reader;
}

public function ls( $parent = '/' ) {
public function ls( $dir = '/' ) {
$this->load_central_directory();
$descendants = $this->central_directory;

// Only keep the descendants of the given parent.
$parent = trim( $parent, '/' );
$prefix = $parent ? $parent . '/' : '';
$dir = trim( $dir, '/' );
$prefix = $dir ? $dir . '/' : '';
if ( strlen( $prefix ) > 1 ) {
$filtered_descendants = array();
foreach ( $descendants as $entry ) {
Expand Down
Loading