Code Modernization: Replace the deprecated auto_detect_line_endings setting.

Since PHP 8.1, the `auto_detect_line_endings` setting is deprecated:

> The `auto_detect_line_endings` ini setting modifies the behavior of `file()` and `fgets()` to support an isolated `\r` (as opposed to `\n` or `\r\n`) as a newline character. These newlines were used by “Classic” Mac OS, a system which has been discontinued in 2001, nearly two decades ago. Interoperability with such systems is no longer relevant.

Reference: [https://wiki.php.net/rfc/deprecations_php_8_1#auto_detect_line_endings_ini_setting PHP RFC: Deprecations for PHP 8.1: auto_detect_line_endings ini setting].

> The `auto_detect_line_endings` ini setting has been deprecated. If necessary, handle `\r` line breaks manually instead.

Reference: [1cf4fb739f/UPGRADING (L456-L457) PHP 8.1 Upgrade Notes].

This commits adds code to replace the deprecated setting and still handle old-style `\r` line-terminated files in `PO::read_line()` using `strpos()` + `fseek()`. Includes a unit test covering `\r`, `\n`, and `\r\n` line endings.

Follow-up to [51633], [51636].

Props akirk, apermo, westonruter, SergeyBiryukov.
Fixes #64928.
Built from https://develop.svn.wordpress.org/trunk@62093


git-svn-id: http://core.svn.wordpress.org/trunk@61375 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov
2026-03-23 14:55:58 +00:00
parent 1d1a1ae9e2
commit ff72263643
2 changed files with 26 additions and 13 deletions

View File

@@ -13,16 +13,6 @@ if ( ! defined( 'PO_MAX_LINE_LEN' ) ) {
define( 'PO_MAX_LINE_LEN', 79 );
}
/*
* The `auto_detect_line_endings` setting has been deprecated in PHP 8.1,
* but will continue to work until PHP 9.0.
* For now, we're silencing the deprecation notice as there may still be
* translation files around which haven't been updated in a long time and
* which still use the old MacOS standalone `\r` as a line ending.
* This fix should be revisited when PHP 9.0 is in alpha/beta.
*/
@ini_set( 'auto_detect_line_endings', 1 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
/**
* Routines for working with PO files
*/
@@ -475,8 +465,31 @@ if ( ! class_exists( 'PO', false ) ) :
$use_last_line = true;
return true;
}
$line = $use_last_line ? $last_line : fgets( $f );
$line = ( "\r\n" === substr( $line, -2 ) ) ? rtrim( $line, "\r\n" ) . "\n" : $line;
if ( $use_last_line ) {
$line = $last_line;
} else {
$line = fgets( $f );
if ( false === $line ) {
return $line;
}
// Handle \r-only terminated lines after the deprecation of auto_detect_line_endings in PHP 8.1.
$r = strpos( $line, "\r" );
if ( false !== $r ) {
if ( strlen( $line ) === $r + 1
&& "\r\n" === substr( $line, $r )
) {
$line = rtrim( $line, "\r\n" ) . "\n";
} else {
// The lines are terminated by just \r, so we end the line there and rewind.
$rewind = strlen( $line ) - $r - 1;
$line = substr( $line, 0, $r ) . "\n";
fseek( $f, - $rewind, SEEK_CUR );
}
}
}
$last_line = $line;
$use_last_line = false;
return $line;

View File

@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '7.0-beta6-62092';
$wp_version = '7.0-beta6-62093';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.