|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @group conduit |
| 5 | + */ |
| 6 | +final class ConduitAPI_phragment_getpatch_Method |
| 7 | + extends ConduitAPI_phragment_Method { |
| 8 | + |
| 9 | + public function getMethodStatus() { |
| 10 | + return self::METHOD_STATUS_UNSTABLE; |
| 11 | + } |
| 12 | + |
| 13 | + public function getMethodDescription() { |
| 14 | + return pht("Retrieve the patches to apply for a given set of files."); |
| 15 | + } |
| 16 | + |
| 17 | + public function defineParamTypes() { |
| 18 | + return array( |
| 19 | + 'path' => 'required string', |
| 20 | + 'state' => 'required dict<string, string>', |
| 21 | + ); |
| 22 | + } |
| 23 | + |
| 24 | + public function defineReturnType() { |
| 25 | + return 'nonempty dict'; |
| 26 | + } |
| 27 | + |
| 28 | + public function defineErrorTypes() { |
| 29 | + return array( |
| 30 | + 'ERR_BAD_FRAGMENT' => 'No such fragment exists', |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + protected function execute(ConduitAPIRequest $request) { |
| 35 | + $path = $request->getValue('path'); |
| 36 | + $state = $request->getValue('state'); |
| 37 | + // The state is an array mapping file paths to hashes. |
| 38 | + |
| 39 | + $patches = array(); |
| 40 | + |
| 41 | + // We need to get all of the mappings (like phragment.getstate) first |
| 42 | + // so that we can detect deletions and creations of files. |
| 43 | + $fragment = id(new PhragmentFragmentQuery()) |
| 44 | + ->setViewer($request->getUser()) |
| 45 | + ->withPaths(array($path)) |
| 46 | + ->executeOne(); |
| 47 | + if ($fragment === null) { |
| 48 | + throw new ConduitException('ERR_BAD_FRAGMENT'); |
| 49 | + } |
| 50 | + |
| 51 | + $mappings = $fragment->getFragmentMappings( |
| 52 | + $request->getUser(), |
| 53 | + $fragment->getPath()); |
| 54 | + |
| 55 | + $file_phids = mpull(mpull($mappings, 'getLatestVersion'), 'getFilePHID'); |
| 56 | + $files = id(new PhabricatorFileQuery()) |
| 57 | + ->setViewer($request->getUser()) |
| 58 | + ->withPHIDs($file_phids) |
| 59 | + ->execute(); |
| 60 | + $files = mpull($files, null, 'getPHID'); |
| 61 | + |
| 62 | + // Scan all of the files that the caller currently has and iterate |
| 63 | + // over that. |
| 64 | + foreach ($state as $path => $hash) { |
| 65 | + // If $mappings[$path] exists, then the user has the file and it's |
| 66 | + // also a fragment. |
| 67 | + if (array_key_exists($path, $mappings)) { |
| 68 | + $file_phid = $mappings[$path]->getLatestVersion()->getFilePHID(); |
| 69 | + if ($file_phid !== null) { |
| 70 | + // If the file PHID is present, then we need to check the |
| 71 | + // hashes to see if they are the same. |
| 72 | + $hash_caller = strtolower($state[$path]); |
| 73 | + $hash_current = $files[$file_phid]->getContentHash(); |
| 74 | + if ($hash_caller === $hash_current) { |
| 75 | + // The user's version is identical to our version, so |
| 76 | + // there is no update needed. |
| 77 | + } else { |
| 78 | + // The hash differs, and the user needs to update. |
| 79 | + $patches[] = array( |
| 80 | + 'path' => $path, |
| 81 | + 'fileOld' => null, |
| 82 | + 'fileNew' => $files[$file_phid], |
| 83 | + 'hashOld' => $hash_caller, |
| 84 | + 'hashNew' => $hash_current, |
| 85 | + 'patchURI' => null); |
| 86 | + } |
| 87 | + } else { |
| 88 | + // We have a record of this as a file, but there is no file |
| 89 | + // attached to the latest version, so we consider this to be |
| 90 | + // a deletion. |
| 91 | + $patches[] = array( |
| 92 | + 'path' => $path, |
| 93 | + 'fileOld' => null, |
| 94 | + 'fileNew' => null, |
| 95 | + 'hashOld' => $hash_caller, |
| 96 | + 'hashNew' => PhragmentPatchUtil::EMPTY_HASH, |
| 97 | + 'patchURI' => null); |
| 98 | + } |
| 99 | + } else { |
| 100 | + // If $mappings[$path] does not exist, then the user has a file, |
| 101 | + // and we have absolutely no record of it what-so-ever (we haven't |
| 102 | + // even recorded a deletion). Assuming most applications will store |
| 103 | + // some form of data near their own files, this is probably a data |
| 104 | + // file relevant for the application that is not versioned, so we |
| 105 | + // don't tell the client to do anything with it. |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // Check the remaining files that we know about but the caller has |
| 110 | + // not reported. |
| 111 | + foreach ($mappings as $path => $child) { |
| 112 | + if (array_key_exists($path, $state)) { |
| 113 | + // We have already evaluated this above. |
| 114 | + } else { |
| 115 | + $file_phid = $mappings[$path]->getLatestVersion()->getFilePHID(); |
| 116 | + if ($file_phid !== null) { |
| 117 | + // If the file PHID is present, then this is a new file that |
| 118 | + // we know about, but the caller does not. We need to tell |
| 119 | + // the caller to create the file. |
| 120 | + $hash_current = $files[$file_phid]->getContentHash(); |
| 121 | + $patches[] = array( |
| 122 | + 'path' => $path, |
| 123 | + 'fileOld' => null, |
| 124 | + 'fileNew' => $files[$file_phid], |
| 125 | + 'hashOld' => PhragmentPatchUtil::EMPTY_HASH, |
| 126 | + 'hashNew' => $hash_current, |
| 127 | + 'patchURI' => null); |
| 128 | + } else { |
| 129 | + // We have a record of deleting this file, and the caller hasn't |
| 130 | + // reported it, so they've probably deleted it in a previous |
| 131 | + // update. |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + // Before we can calculate patches, we need to resolve the old versions |
| 137 | + // of files so we can draw diffs on them. |
| 138 | + $hashes = array(); |
| 139 | + foreach ($patches as $patch) { |
| 140 | + if ($patch["hashOld"] !== PhragmentPatchUtil::EMPTY_HASH) { |
| 141 | + $hashes[] = $patch["hashOld"]; |
| 142 | + } |
| 143 | + } |
| 144 | + $old_files = array(); |
| 145 | + if (count($hashes) !== 0) { |
| 146 | + $old_files = id(new PhabricatorFileQuery()) |
| 147 | + ->setViewer($request->getUser()) |
| 148 | + ->withContentHashes($hashes) |
| 149 | + ->execute(); |
| 150 | + } |
| 151 | + $old_files = mpull($old_files, null, 'getContentHash'); |
| 152 | + foreach ($patches as $key => $patch) { |
| 153 | + if ($patch["hashOld"] !== PhragmentPatchUtil::EMPTY_HASH) { |
| 154 | + if (array_key_exists($patch['hashOld'], $old_files)) { |
| 155 | + $patches[$key]['fileOld'] = $old_files[$patch['hashOld']]; |
| 156 | + } else { |
| 157 | + // We either can't see or can't read the old file. |
| 158 | + $patches[$key]['hashOld'] = PhragmentPatchUtil::EMPTY_HASH; |
| 159 | + $patches[$key]['fileOld'] = null; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + // Now run through all of the patch entries, calculate the patches |
| 165 | + // and return the results. |
| 166 | + foreach ($patches as $key => $patch) { |
| 167 | + $data = PhragmentPatchUtil::calculatePatch( |
| 168 | + $patches[$key]['fileOld'], |
| 169 | + $patches[$key]['fileNew']); |
| 170 | + unset($patches[$key]['fileOld']); |
| 171 | + unset($patches[$key]['fileNew']); |
| 172 | + |
| 173 | + $file = PhabricatorFile::buildFromFileDataOrHash( |
| 174 | + $data, |
| 175 | + array( |
| 176 | + 'name' => 'patch.dmp', |
| 177 | + 'ttl' => time() + 60 * 60 * 24, |
| 178 | + )); |
| 179 | + $patches[$key]['patchURI'] = $file->getDownloadURI(); |
| 180 | + } |
| 181 | + |
| 182 | + return $patches; |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments