New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle all cases for Utils\wp_version_compare() #4380

Closed
danielbachhuber opened this Issue Sep 26, 2017 · 1 comment

Comments

2 participants
@danielbachhuber
Member

danielbachhuber commented Sep 26, 2017

WordPress uses a few more versions than we currently accommodate in Utils\wp_version_compare():

While this works, it will have the unintended side effect of not prompting for updates when they are available. For example, if I have deployed 4.9-alpha-40870-src but then a nightly build of 4.9-beta1-45000 is available, then I should still get prompted to update. But it should not update using the nightly build because then that will blow away the -src. So in that case, a different notification should be displayed.

We should:

  • Identify all of these potential version strings.
  • Update Utils\wp_version_compare() to handle the potential version strings.
  • Write unit tests around each case.

From pantheon-systems/WordPress#136 (comment)

@westonruter

This comment has been minimized.

Show comment
Hide comment
@westonruter

westonruter Sep 26, 2017

Contributor

I think the comparison logic should be something like this:

/**
 * Compare two WordPress version numbers which look like '4.9-alpha-40870-src'.
 *
 * @param string $ver_a Version A.
 * @param string $ver_b Version B.
 * @return int Comparison.
 */
$compare = function( $ver_a, $ver_b ) {
	$ver_a_parts = explode( '-', $ver_a );
	$ver_b_parts = explode( '-', $ver_b );

	// Compare semantic versions.
	$semver_a = array_shift( $ver_a_parts );
	$semver_b = array_shift( $ver_b_parts );
	$comparison = version_compare( $semver_a, $semver_b );
	if ( 0 !== $comparison ) {
		return $comparison;
	}

	// Compare pre-release identifiers 'alpha', 'beta', 'rc'.
	$prerelease_a = array_shift( $ver_a_parts );
	$prerelease_b = array_shift( $ver_b_parts );
	$comparison = strcmp( $prerelease_a, $prerelease_b );
	if ( 0 !== $comparison ) {
		return $comparison;
	}

	$rev_a = intval( array_shift( $ver_a_parts ) );
	$rev_b = intval( array_shift( $ver_b_parts ) );

	// If either version is a src (but not both) then consider them equal.
	$src_a = 'src' === array_shift( $ver_a_parts );
	$src_b = 'src' === array_shift( $ver_b_parts );
	if ( $src_a xor $src_b ) {
		return 0;
	}

	// Compare revision numbers.
	return $rev_b - $rev_a;
};
Contributor

westonruter commented Sep 26, 2017

I think the comparison logic should be something like this:

/**
 * Compare two WordPress version numbers which look like '4.9-alpha-40870-src'.
 *
 * @param string $ver_a Version A.
 * @param string $ver_b Version B.
 * @return int Comparison.
 */
$compare = function( $ver_a, $ver_b ) {
	$ver_a_parts = explode( '-', $ver_a );
	$ver_b_parts = explode( '-', $ver_b );

	// Compare semantic versions.
	$semver_a = array_shift( $ver_a_parts );
	$semver_b = array_shift( $ver_b_parts );
	$comparison = version_compare( $semver_a, $semver_b );
	if ( 0 !== $comparison ) {
		return $comparison;
	}

	// Compare pre-release identifiers 'alpha', 'beta', 'rc'.
	$prerelease_a = array_shift( $ver_a_parts );
	$prerelease_b = array_shift( $ver_b_parts );
	$comparison = strcmp( $prerelease_a, $prerelease_b );
	if ( 0 !== $comparison ) {
		return $comparison;
	}

	$rev_a = intval( array_shift( $ver_a_parts ) );
	$rev_b = intval( array_shift( $ver_b_parts ) );

	// If either version is a src (but not both) then consider them equal.
	$src_a = 'src' === array_shift( $ver_a_parts );
	$src_b = 'src' === array_shift( $ver_b_parts );
	if ( $src_a xor $src_b ) {
		return 0;
	}

	// Compare revision numbers.
	return $rev_b - $rev_a;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment