In `insert_with_markers()` restore the 4.3 behaviour of creating the file if it doesn't exist.

This change also makes it bail early (without writing) if the markers content is the same as the existing, and uses `ftell()` rather than `$bytes` for the location to truncate the file to - based on the file pointer being at the end of the written stream.

Props willmot tigertech kevinatelement
See #31767

Built from https://develop.svn.wordpress.org/trunk@35267


git-svn-id: http://core.svn.wordpress.org/trunk@35233 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dion Hulse 2015-10-19 00:53:24 +00:00
parent fd9bcf111d
commit 85258bb914
2 changed files with 26 additions and 9 deletions

View File

@ -96,18 +96,25 @@ function extract_from_markers( $filename, $marker ) {
*
* @since 1.5.0
*
* @param string $filename
* @param string $marker
* @param array $insertion
* @param string $filename Filename to alter.
* @param string $marker The marker to alter.
* @param array|string $insertion The new content to insert.
* @return bool True on write success, false on failure.
*/
function insert_with_markers( $filename, $marker, $insertion ) {
if ( ! is_writeable( $filename ) ) {
if ( ! file_exists( $filename ) ) {
if ( ! is_writable( dirname( $filename ) ) ) {
return false;
}
if ( ! touch( $filename ) ) {
return false;
}
} elseif ( ! is_writeable( $filename ) ) {
return false;
}
if ( ! is_array( $insertion ) ) {
$insertion = array( $insertion );
$insertion = explode( "\n", $insertion );
}
$start_marker = "# BEGIN {$marker}";
@ -127,7 +134,7 @@ function insert_with_markers( $filename, $marker, $insertion ) {
}
// Split out the existing file into the preceeding lines, and those that appear after the marker
$pre_lines = $post_lines = array();
$pre_lines = $post_lines = $existing_lines = array();
$found_marker = $found_end_marker = false;
foreach ( $lines as $line ) {
if ( ! $found_marker && false !== strpos( $line, $start_marker ) ) {
@ -141,9 +148,19 @@ function insert_with_markers( $filename, $marker, $insertion ) {
$pre_lines[] = $line;
} elseif ( $found_marker && $found_end_marker ) {
$post_lines[] = $line;
} else {
$existing_lines[] = $line;
}
}
// Check to see if there was a change
if ( $existing_lines === $insertion ) {
flock( $fp, LOCK_UN );
fclose( $fp );
return true;
}
// Generate the new file data
$new_file_data = implode( "\n", array_merge(
$pre_lines,
@ -157,9 +174,9 @@ function insert_with_markers( $filename, $marker, $insertion ) {
fseek( $fp, 0 );
$bytes = fwrite( $fp, $new_file_data );
if ( $bytes ) {
ftruncate( $fp, $bytes );
ftruncate( $fp, ftell( $fp ) );
}
fflush( $fp );
flock( $fp, LOCK_UN );
fclose( $fp );

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.4-alpha-35266';
$wp_version = '4.4-alpha-35267';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.