class-attachmentdownloader.php
2 weeks ago
class-attachmentdownloaderevent.php
2 weeks ago
class-entityimporter.php
2 weeks ago
class-entityiteratorchain.php
2 weeks ago
class-filevisitorevent.php
2 weeks ago
class-importsession.php
2 weeks ago
class-importutils.php
2 weeks ago
class-retryfrontloadingiterator.php
2 weeks ago
class-streamimporter.php
2 weeks ago
class-attachmentdownloader.php
286 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\DataLiberation\Importer; |
| 4 | |
| 5 | use Exception; |
| 6 | use WordPress\Filesystem\Filesystem; |
| 7 | use WordPress\HttpClient\Client; |
| 8 | use WordPress\HttpClient\Request; |
| 9 | |
| 10 | use function WordPress\Filesystem\wp_join_unix_paths; |
| 11 | |
| 12 | class AttachmentDownloader { |
| 13 | private $client; |
| 14 | private $fps = array(); |
| 15 | private $output_root; |
| 16 | private $output_paths = array(); |
| 17 | /** |
| 18 | * @var Filesystem |
| 19 | */ |
| 20 | private $source_from_filesystem; |
| 21 | |
| 22 | private $pending_events = array(); |
| 23 | private $enqueued_url; |
| 24 | private $progress = array(); |
| 25 | |
| 26 | public function __construct( $output_root, $options = array() ) { |
| 27 | $this->client = new Client(); |
| 28 | $this->output_root = $output_root; |
| 29 | $this->source_from_filesystem = $options['source_from_filesystem'] ?? null; |
| 30 | } |
| 31 | |
| 32 | public function get_progress() { |
| 33 | return $this->progress; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Whether any downloads are still in progress. |
| 38 | * |
| 39 | * Note that zero active requests does not mean all work is done. |
| 40 | * Even if all the response bytes are received, we still need to process |
| 41 | * them and emit the final success/failure events. |
| 42 | * |
| 43 | * @return bool |
| 44 | */ |
| 45 | public function has_pending_requests() { |
| 46 | return count( $this->client->get_active_requests() ) > 0 || count( $this->pending_events ) > 0 || count( $this->progress ) > 0; |
| 47 | } |
| 48 | |
| 49 | public function enqueue_if_not_exists( $url, $output_relative_path ) { |
| 50 | $this->enqueued_url = $url; |
| 51 | |
| 52 | $output_path = wp_join_unix_paths( $this->output_root, $output_relative_path ); |
| 53 | if ( file_exists( $output_path ) ) { |
| 54 | $this->pending_events[] = new AttachmentDownloaderEvent( |
| 55 | $this->enqueued_url, |
| 56 | AttachmentDownloaderEvent::ALREADY_EXISTS |
| 57 | ); |
| 58 | |
| 59 | return true; |
| 60 | } |
| 61 | if ( file_exists( $output_path . '.partial' ) ) { |
| 62 | $this->pending_events[] = new AttachmentDownloaderEvent( |
| 63 | $this->enqueued_url, |
| 64 | AttachmentDownloaderEvent::IN_PROGRESS |
| 65 | ); |
| 66 | |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | $output_dir = dirname( $output_path ); |
| 71 | if ( ! file_exists( $output_dir ) ) { |
| 72 | // @TODO: think through the chmod of the created directory. |
| 73 | mkdir( $output_dir, 0777, true ); |
| 74 | } |
| 75 | |
| 76 | $protocol = parse_url( $url, PHP_URL_SCHEME ); |
| 77 | if ( null === $protocol ) { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | switch ( $protocol ) { |
| 82 | case 'file': |
| 83 | if ( ! $this->source_from_filesystem ) { |
| 84 | _doing_it_wrong( |
| 85 | __METHOD__, |
| 86 | 'Cannot process file:// URLs without a source filesystem instance. Use the source_from_filesystem option to pass in a filesystem instance to WP_Attachment_Downloader.', |
| 87 | '1.0' |
| 88 | ); |
| 89 | |
| 90 | return false; |
| 91 | } |
| 92 | $source_path = parse_url( $url, PHP_URL_PATH ); |
| 93 | if ( false === $source_path ) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | // Just copy the file over. |
| 98 | // @TODO: think through the chmod of the created file. |
| 99 | $stream = null; |
| 100 | try { |
| 101 | $stream = $this->source_from_filesystem->open_read_stream( $source_path ); |
| 102 | $fp = fopen( $output_path, 'wb' ); |
| 103 | while ( ! $stream->reached_end_of_data() ) { |
| 104 | $pulled = $stream->pull( 65536 ); |
| 105 | $chunk = $stream->consume( $pulled ); |
| 106 | fwrite( $fp, $chunk ); |
| 107 | } |
| 108 | fclose( $fp ); |
| 109 | |
| 110 | $this->pending_events[] = new AttachmentDownloaderEvent( |
| 111 | $this->enqueued_url, |
| 112 | AttachmentDownloaderEvent::SUCCESS |
| 113 | ); |
| 114 | } catch ( Exception $e ) { |
| 115 | $this->pending_events[] = new AttachmentDownloaderEvent( |
| 116 | $this->enqueued_url, |
| 117 | AttachmentDownloaderEvent::FAILURE, |
| 118 | 'copy_failed' |
| 119 | ); |
| 120 | } finally { |
| 121 | if ( $stream ) { |
| 122 | $stream->close_reading(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return true; |
| 127 | case 'http': |
| 128 | case 'https': |
| 129 | // Create a placeholder file to indicate that the download is in progress. |
| 130 | touch( $output_path . '.partial' ); |
| 131 | $request = new Request( $url ); |
| 132 | $this->output_paths[ $request->id ] = $output_path; |
| 133 | $this->progress[ $this->enqueued_url ] = array( |
| 134 | 'received' => null, |
| 135 | 'total' => null, |
| 136 | ); |
| 137 | $this->client->enqueue( $request ); |
| 138 | |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | public function get_enqueued_url() { |
| 146 | return $this->enqueued_url; |
| 147 | } |
| 148 | |
| 149 | public function queue_full() { |
| 150 | return count( $this->client->get_active_requests() ) >= 10; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Returns and clears all pending events. |
| 155 | * |
| 156 | * @return AttachmentDownloaderEvent[] |
| 157 | */ |
| 158 | public function get_events() { |
| 159 | $events = $this->pending_events; |
| 160 | $this->pending_events = array(); |
| 161 | return $events; |
| 162 | } |
| 163 | |
| 164 | public function poll() { |
| 165 | while ( $this->client->await_next_event() ) { |
| 166 | $event = $this->client->get_event(); |
| 167 | $request = $this->client->get_request(); |
| 168 | if ( Client::EVENT_FAILED === $event ) { |
| 169 | $this->on_failure( $request->url, $request->id, $request->error ); |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | // Only process responses this was the last request in the chain. |
| 174 | if ( $request->is_redirected() ) { |
| 175 | continue; |
| 176 | } |
| 177 | |
| 178 | // The request object we get from the client may be a redirect |
| 179 | // Let's keep referring to the original request. |
| 180 | $original_url = $request->original_request()->url; |
| 181 | $original_request_id = $request->original_request()->id; |
| 182 | |
| 183 | /** |
| 184 | * @TODO: Whenever we get a redirect to a URL we've already processed, |
| 185 | * stop and emit a success event. |
| 186 | */ |
| 187 | switch ( $event ) { |
| 188 | case Client::EVENT_GOT_HEADERS: |
| 189 | if ( file_exists( $this->output_paths[ $original_request_id ] . '.partial' ) ) { |
| 190 | unlink( $this->output_paths[ $original_request_id ] . '.partial' ); |
| 191 | } |
| 192 | echo $this->output_paths[ $original_request_id ] . "\n"; |
| 193 | $fp = fopen( $this->output_paths[ $original_request_id ] . '.partial', 'wb' ); |
| 194 | if ( false !== $fp ) { |
| 195 | $this->fps[ $original_request_id ] = $fp; |
| 196 | $this->progress[ $original_url ]['received'] = 0; |
| 197 | if ( $request->response->get_header( 'Content-Length' ) ) { |
| 198 | $this->progress[ $original_url ]['total'] = $request->response->get_header( 'Content-Length' ); |
| 199 | } |
| 200 | } |
| 201 | break; |
| 202 | case Client::EVENT_BODY_CHUNK_AVAILABLE: |
| 203 | $chunk = $this->client->get_response_body_chunk(); |
| 204 | if ( ! fwrite( $this->fps[ $original_request_id ], $chunk ) ) { |
| 205 | // @TODO: Don't echo the error message. Attach it to the import session instead for the user to review later on. |
| 206 | _doing_it_wrong( |
| 207 | __METHOD__, |
| 208 | sprintf( 'Failed to write to file: %s', $this->output_paths[ $original_request_id ] ), |
| 209 | '1.0' |
| 210 | ); |
| 211 | } |
| 212 | $this->progress[ $original_url ]['received'] += strlen( $chunk ); |
| 213 | break; |
| 214 | case Client::EVENT_FINISHED: |
| 215 | if ( $request->response->ok() ) { |
| 216 | $this->on_success( $original_url, $original_request_id ); |
| 217 | } else { |
| 218 | $this->on_failure( $original_url, $original_request_id, 'http_error_' . $request->response->status_code ); |
| 219 | } |
| 220 | break; |
| 221 | } |
| 222 | |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | private function on_failure( $original_url, $original_request_id, $error = null ) { |
| 230 | if ( isset( $this->fps[ $original_request_id ] ) ) { |
| 231 | if ( is_resource( $this->fps[ $original_request_id ] ) ) { |
| 232 | fclose( $this->fps[ $original_request_id ] ); |
| 233 | } |
| 234 | unset( $this->fps[ $original_request_id ] ); |
| 235 | } |
| 236 | if ( isset( $this->output_paths[ $original_request_id ] ) ) { |
| 237 | $partial_file = $this->output_paths[ $original_request_id ] . '.partial'; |
| 238 | if ( file_exists( $partial_file ) ) { |
| 239 | unlink( $partial_file ); |
| 240 | } |
| 241 | } |
| 242 | $this->pending_events[] = new AttachmentDownloaderEvent( |
| 243 | $original_url, |
| 244 | AttachmentDownloaderEvent::FAILURE, |
| 245 | $error |
| 246 | ); |
| 247 | unset( $this->progress[ $original_url ] ); |
| 248 | unset( $this->output_paths[ $original_request_id ] ); |
| 249 | } |
| 250 | |
| 251 | private function on_success( $original_url, $original_request_id ) { |
| 252 | // Only clean up if this was the last request in the chain. |
| 253 | if ( isset( $this->fps[ $original_request_id ] ) ) { |
| 254 | if ( is_resource( $this->fps[ $original_request_id ] ) ) { |
| 255 | fclose( $this->fps[ $original_request_id ] ); |
| 256 | } |
| 257 | unset( $this->fps[ $original_request_id ] ); |
| 258 | } |
| 259 | if ( isset( $this->output_paths[ $original_request_id ] ) ) { |
| 260 | if ( false === rename( |
| 261 | $this->output_paths[ $original_request_id ] . '.partial', |
| 262 | $this->output_paths[ $original_request_id ] |
| 263 | ) ) { |
| 264 | // @TODO: Log an error. |
| 265 | } |
| 266 | } |
| 267 | $this->pending_events[] = new AttachmentDownloaderEvent( |
| 268 | $original_url, |
| 269 | AttachmentDownloaderEvent::SUCCESS |
| 270 | ); |
| 271 | unset( $this->progress[ $original_url ] ); |
| 272 | unset( $this->output_paths[ $original_request_id ] ); |
| 273 | } |
| 274 | |
| 275 | public function __destruct() { |
| 276 | // Ensure any remaining open file descriptors are closed. |
| 277 | foreach ( $this->fps as $request_id => $fp ) { |
| 278 | if ( is_resource( $fp ) ) { |
| 279 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 280 | @fclose( $fp ); |
| 281 | } |
| 282 | unset( $this->fps[ $request_id ] ); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 |