Use diffs if provided, otherwise compute it ourselves.
There are four ways to call this function, depending on what data is
available to the caller:
Method 1:
a = text1, b = text2
Method 2:
a = diffs
Method 3 (optimal):
a = text1, b = diffs
Method 4 (deprecated, use method 3):
a = text1, b = text2, c = diffs
public patch_make ( $a, $opt_b = null, $opt_c = null ) : {Array. |
||
return | {Array. |
/** * Calculate the DiffMatchPatch patch between two Phabricator files. * * @phutil-external-symbol class diff_match_patch */ public static function calculatePatch(PhabricatorFile $old = null, PhabricatorFile $new = null) { $root = dirname(phutil_get_library_root('phabricator')); require_once $root . '/externals/diff_match_patch/diff_match_patch.php'; $old_hash = self::EMPTY_HASH; $new_hash = self::EMPTY_HASH; if ($old !== null) { $old_hash = $old->getContentHash(); } if ($new !== null) { $new_hash = $new->getContentHash(); } $old_content = ''; $new_content = ''; if ($old_hash === $new_hash) { return null; } if ($old_hash !== self::EMPTY_HASH) { $old_content = $old->loadFileData(); } else { $old_content = ''; } if ($new_hash !== self::EMPTY_HASH) { $new_content = $new->loadFileData(); } else { $new_content = ''; } $dmp = new diff_match_patch(); $dmp_patches = $dmp->patch_make($old_content, $new_content); return $dmp->patch_toText($dmp_patches); }
} /* First acquire a lock or wait until a lock can be acquired for server * text and shadow. */ $serverTextEntry->lock(); $shadowTextEntry->lock(); $serverText = $serverTextEntry->get_value(); $shadowText = $shadowTextEntry->get_value(); $patchFromClient = $_POST['patch']; /* Patch the shadow and server texts with the edits from the client. */ $dmp = new diff_match_patch(); $patchedServerText = $dmp->patch_apply($dmp->patch_fromText($patchFromClient), $serverText); $serverTextEntry->put_value($patchedServerText[0]); $patchedShadowText = $dmp->patch_apply($dmp->patch_fromText($patchFromClient), $shadowText); /* Make a diff between server text and shadow to get the edits to send * back to the client. */ $patchFromServer = $dmp->patch_toText($dmp->patch_make($patchedShadowText[0], $patchedServerText[0])); /* Apply it to the shadow. */ $patchedShadowText = $dmp->patch_apply($dmp->patch_fromText($patchFromServer), $patchedShadowText[0]); $shadowTextEntry->put_value($patchedShadowText[0]); /* Release locks. */ $serverTextEntry->unlock(); $shadowTextEntry->unlock(); echo formatJSEND('success', $patchFromServer); break; case 'sendHeartbeat': /* Hard coded heartbeat time interval. Beware to keep this value here * twice the value on client side. */ $maxHeartbeatInterval = 5; $currentTime = time(); /* Check if the user is a new user, or if it is just an update of * his heartbeat. */