0 ? $max : 30; $seconds = min( 20.0, max( 3.0, $base * 0.4 ) ); return (float) apply_filters( 'migratex_tick_seconds', $seconds ); } /** * @return int Bytes, or 0 when PHP imposes no limit. */ public static function memoryLimitBytes(): int { $limit = ini_get( 'memory_limit' ); if ( false === $limit || '' === $limit || '-1' === (string) $limit ) { return 0; } return max( 0, self::iniBytes( (string) $limit ) ); } /** * "128M", "1G", "512K" or a plain number of bytes, as php.ini writes sizes. The same rules as * WordPress's wp_convert_hr_to_bytes(), kept here so this class works without WordPress loaded * (the unit tests, WP-CLI before bootstrap) and nothing depends on that function being defined. */ public static function iniBytes( string $value ): int { $value = trim( $value ); if ( '' === $value ) { return 0; } $number = (int) $value; switch ( strtolower( substr( $value, -1 ) ) ) { case 'g': $number *= 1024; // Fall through. case 'm': $number *= 1024; // Fall through. case 'k': $number *= 1024; } return $number; } /** * Rows read per query when dumping. Shrinks on small-memory hosts. */ public static function dbBatchRows(): int { $memory = self::memoryLimitBytes(); $rows = 1000; if ( $memory > 0 && $memory < 96 * 1024 * 1024 ) { $rows = 250; } elseif ( $memory > 0 && $memory < 192 * 1024 * 1024 ) { $rows = 500; } return (int) apply_filters( 'migratex_db_batch_rows', $rows ); } /** * Largest single INSERT/replay statement. Kept well under max_allowed_packet. */ public static function maxStatementBytes( int $max_allowed_packet = 0 ): int { $cap = 1024 * 1024; if ( $max_allowed_packet > 0 ) { $cap = min( $cap, (int) ( $max_allowed_packet / 2 ) ); } return max( 64 * 1024, $cap ); } /** * Size of the pieces an import upload is cut into: MIGRATEX_CHUNK_SIZE (5 MB), or less where the * host could not take that much in one request. * * - post_max_size: PHP discards a request body larger than this, so a piece must stay under it * (the plugin used to promise "2 MB pieces, so post_max_size does not matter"). * - memory_limit: the piece is held in memory while it is checked and written, so it is kept to a * small share of it. * * The result is a multiple of 64 KB and never below 256 KB. The same value is given to the browser * and stored in the job, so both sides always cut the file the same way. */ public static function chunkBytes(): int { $cap = (int) MIGRATEX_CHUNK_SIZE; $post = (string) ini_get( 'post_max_size' ); if ( '' !== $post && '0' !== $post && '-1' !== $post ) { $bytes = self::iniBytes( $post ); if ( $bytes > 0 ) { $cap = min( $cap, (int) ( $bytes * 0.8 ) ); } } $memory = self::memoryLimitBytes(); if ( $memory > 0 ) { $cap = min( $cap, (int) ( $memory / 16 ) ); } $cap = (int) apply_filters( 'migratex_chunk_bytes', $cap ); return max( 262144, $cap - ( $cap % 65536 ) ); } /** * The largest site-to-site piece this server is willing to receive, asked for in the handshake: the * 10 MB default (MIGRATEX_REMOTE_CHUNK_SIZE), or less where memory is tight. A piece is held in memory * several times over while it is unwrapped, checked and written, so it is kept to an eighth of * memory_limit, and never below 1 MB. A multiple of 64 KB. */ public static function remoteChunkMax(): int { $cap = (int) MIGRATEX_REMOTE_CHUNK_SIZE; $memory = self::memoryLimitBytes(); if ( $memory > 0 ) { $cap = min( $cap, (int) ( $memory / 8 ) ); } $cap = (int) apply_filters( 'migratex_remote_chunk_max', $cap ); $cap = max( 1048576, $cap ); return $cap - ( $cap % 65536 ); } /** * Free bytes on the disk that holds $path, or null when the host will not say. * * Shared hosts often list disk_free_space in disable_functions (the function then does not exist and * calling it is a fatal error), or have it return false, or 0 on a virtual or quota-limited volume. * All of those mean "unknown", never "full": a disk that really is full is reported by the write that * fails. A folder that does not exist yet is measured through its nearest existing parent. */ public static function freeDiskSpace( string $path ): ?float { if ( ! function_exists( 'disk_free_space' ) ) { return null; } $probe = $path; while ( ! is_dir( $probe ) && dirname( $probe ) !== $probe ) { $probe = dirname( $probe ); } $free = @\disk_free_space( $probe ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- may warn on restricted hosts; false is handled. // For hosts that report a number that is not the real quota, and for tests. $free = apply_filters( 'migratex_free_disk_space', $free, $path ); return ( false !== $free && null !== $free && $free > 0 ) ? (float) $free : null; } /** * Whether a folder looks writable. Nothing is written to find out: a probe file in wp-content or * languages is a write the person never asked for. wp_is_writable() is WordPress's own check (it also * handles Windows ACLs), so a host whose flags are wrong is caught when the real write fails, and * that failure is reported by the stage that wrote. */ public static function canWriteTo( string $dir ): bool { return is_dir( $dir ) && wp_is_writable( $dir ); } public static function ignoreUserAbort( bool $ignore ): void { if ( function_exists( 'ignore_user_abort' ) ) { ignore_user_abort( $ignore ); } } /** * The process id, for names that must not collide between concurrent requests. Some hardened hosts * disable getmypid(); a random number then does the same job. */ public static function processId(): string { return function_exists( 'getmypid' ) ? (string) getmypid() : bin2hex( random_bytes( 4 ) ); } /** * Bytes copied per read/write when streaming files. */ public static function copyBlockBytes(): int { $memory = self::memoryLimitBytes(); return ( $memory > 0 && $memory < 64 * 1024 * 1024 ) ? 256 * 1024 : 1024 * 1024; } }