gzip = $gzip; $handle = $gzip ? (Fs::writable($path) ? gzopen($path, 'ab6') : false) : Fs::open($path, 'ab'); if ($handle === false) { throw new RelayError('export.open_failed', esc_html__('Could not open the export file for writing.', 'siterelay'), esc_html__('Check that wp-content/uploads is writable.', 'siterelay')); } $this->handle = $handle; } public function statement(string $sql): void { $this->write($sql . ";\n"); } public function write(string $text): void { if ($text === '') { return; } $written = $this->gzip ? gzwrite($this->handle, $text) : Fs::write($this->handle, $text); if ($written === false || $written < strlen($text)) { throw new RelayError('export.write_failed', esc_html__('Could not write to the export file. The disk may be full.', 'siterelay')); } $this->bytes += strlen($text); } public function bytes(): int { return $this->bytes; } public function close(): void { if ($this->handle !== null) { $this->gzip ? gzclose($this->handle) : Fs::close($this->handle); $this->handle = null; } } /** Cut a file back to a previously saved size (each gzip session is its own member, so this stays valid). */ public static function truncate(string $path, int $size): void { clearstatcache(true, $path); if (is_file($path) && filesize($path) > $size) { $h = Fs::open($path, 'r+'); ftruncate($h, $size); Fs::close($h); } } public function __destruct() { $this->close(); } }