@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Implement "replace" transactions in Pholio without "applyInitialEffects"

Summary:
Depends on D19923. Ref T11351. Currently, this transaction takes an `Image` as the `newValue` and uses some magic to reduce it into PHIDs by the time we're done.

This creates some problems today where I'd like to get rid of `applyInitialEffects` for MFA code. In the future, it creates a problem becuase there's no way to pass an entire `Image` object over the API.

Instead, create the `Image` first, then just provide the PHID. This is generally simpler, will work well with the API in the future, and stops us from needing any `applyInitialEffects` stuff.

Test Plan: Replaced images in a Pholio mock.

Reviewers: amckinley

Reviewed By: amckinley

Maniphest Tasks: T11351

Differential Revision: https://secure.phabricator.com/D19924

+128 -41
+8 -9
src/applications/pholio/controller/PholioMockEditController.php
··· 143 143 $replace_image = PholioImage::initializeNewImage() 144 144 ->setAuthorPHID($viewer->getPHID()) 145 145 ->setReplacesImagePHID($replaces_image_phid) 146 - ->setFilePhid($file_phid) 146 + ->setFilePHID($file_phid) 147 147 ->attachFile($file) 148 148 ->setName(strlen($title) ? $title : $file->getName()) 149 149 ->setDescription($description) 150 - ->setSequence($sequence); 150 + ->setSequence($sequence) 151 + ->save(); 152 + 151 153 $xactions[] = id(new PholioTransaction()) 152 - ->setTransactionType( 153 - PholioImageReplaceTransaction::TRANSACTIONTYPE) 154 - ->setNewValue($replace_image); 154 + ->setTransactionType(PholioImageReplaceTransaction::TRANSACTIONTYPE) 155 + ->setNewValue($replace_image->getPHID()); 156 + 155 157 $posted_mock_images[] = $replace_image; 156 158 } else if (!$existing_image) { // this is an add 157 159 $add_image = PholioImage::initializeNewImage() 158 160 ->setAuthorPHID($viewer->getPHID()) 159 - ->setFilePhid($file_phid) 161 + ->setFilePHID($file_phid) 160 162 ->attachFile($file) 161 163 ->setName(strlen($title) ? $title : $file->getName()) 162 164 ->setDescription($description) ··· 202 204 ->setMetadataValue('edge:type', $proj_edge_type) 203 205 ->setNewValue(array('=' => array_fuse($v_projects))); 204 206 205 - $mock->openTransaction(); 206 207 $editor = id(new PholioMockEditor()) 207 208 ->setContentSourceFromRequest($request) 208 209 ->setContinueOnNoEffect(true) 209 210 ->setActor($viewer); 210 211 211 212 $xactions = $editor->applyTransactions($mock, $xactions); 212 - 213 - $mock->saveTransaction(); 214 213 215 214 return id(new AphrontRedirectResponse()) 216 215 ->setURI('/M'.$mock->getID());
+35 -7
src/applications/pholio/editor/PholioMockEditor.php
··· 4 4 5 5 private $newImages = array(); 6 6 7 + private $images = array(); 8 + 7 9 public function getEditorApplicationClass() { 8 10 return 'PhabricatorPholioApplication'; 9 11 } ··· 48 50 foreach ($xactions as $xaction) { 49 51 switch ($xaction->getTransactionType()) { 50 52 case PholioImageFileTransaction::TRANSACTIONTYPE: 51 - case PholioImageReplaceTransaction::TRANSACTIONTYPE: 52 53 return true; 53 - break; 54 54 } 55 55 } 56 56 return false; ··· 74 74 $new_images[] = $image; 75 75 } 76 76 } 77 - break; 78 - case PholioImageReplaceTransaction::TRANSACTIONTYPE: 79 - $image = $xaction->getNewValue(); 80 - $image->save(); 81 - $new_images[] = $image; 82 77 break; 83 78 } 84 79 } ··· 273 268 } 274 269 275 270 return parent::shouldImplyCC($object, $xaction); 271 + } 272 + 273 + public function loadPholioImage($object, $phid) { 274 + if (!isset($this->images[$phid])) { 275 + 276 + $image = id(new PholioImageQuery()) 277 + ->setViewer($this->getActor()) 278 + ->withPHIDs(array($phid)) 279 + ->executeOne(); 280 + 281 + if (!$image) { 282 + throw new Exception( 283 + pht( 284 + 'No image exists with PHID "%s".', 285 + $phid)); 286 + } 287 + 288 + $mock_phid = $image->getMockPHID(); 289 + if ($mock_phid) { 290 + if ($mock_phid !== $object->getPHID()) { 291 + throw new Exception( 292 + pht( 293 + 'Image ("%s") belongs to the wrong object ("%s", expected "%s").', 294 + $phid, 295 + $mock_phid, 296 + $object->getPHID())); 297 + } 298 + } 299 + 300 + $this->images[$phid] = $image; 301 + } 302 + 303 + return $this->images[$phid]; 276 304 } 277 305 278 306 }
+85 -25
src/applications/pholio/xaction/PholioImageReplaceTransaction.php
··· 6 6 const TRANSACTIONTYPE = 'image-replace'; 7 7 8 8 public function generateOldValue($object) { 9 - $new_image = $this->getNewValue(); 10 - return $new_image->getReplacesImagePHID(); 11 - } 9 + $editor = $this->getEditor(); 10 + $new_phid = $this->getNewValue(); 12 11 13 - public function generateNewValue($object, $value) { 14 - return $value->getPHID(); 12 + return $editor->loadPholioImage($object, $new_phid) 13 + ->getReplacesImagePHID(); 15 14 } 16 15 17 - public function applyInternalEffects($object, $value) { 18 - $old = $this->getOldValue(); 19 - $images = $object->getImages(); 20 - foreach ($images as $seq => $image) { 21 - if ($image->getPHID() == $old) { 22 - $image->setIsObsolete(1); 23 - $image->save(); 24 - unset($images[$seq]); 25 - } 26 - } 27 - $object->attachImages($images); 16 + public function applyExternalEffects($object, $value) { 17 + $editor = $this->getEditor(); 18 + $old_phid = $this->getOldValue(); 19 + 20 + $old_image = $editor->loadPholioImage($object, $old_phid) 21 + ->setIsObsolete(1) 22 + ->save(); 23 + 24 + $editor->loadPholioImage($object, $value) 25 + ->setMockPHID($object->getPHID()) 26 + ->setSequence($old_image->getSequence()) 27 + ->save(); 28 28 } 29 29 30 30 public function getTitle() { ··· 54 54 $object, 55 55 PhabricatorApplicationTransaction $u, 56 56 PhabricatorApplicationTransaction $v) { 57 - $u_img = $u->getNewValue(); 58 - $v_img = $v->getNewValue(); 59 - if ($u_img->getReplacesImagePHID() == $v_img->getReplacesImagePHID()) { 57 + 58 + $u_phid = $u->getOldValue(); 59 + $v_phid = $v->getOldValue(); 60 + 61 + if ($u_phid === $v_phid) { 60 62 return $v; 61 63 } 64 + 65 + return null; 62 66 } 63 67 64 68 public function extractFilePHIDs($object, $value) { 65 - $file_phids = array(); 69 + $editor = $this->getEditor(); 70 + 71 + $file_phid = $editor->loadPholioImage($object, $value) 72 + ->getFilePHID(); 73 + 74 + return array($file_phid); 75 + } 76 + 77 + public function validateTransactions($object, array $xactions) { 78 + $errors = array(); 79 + 80 + $mock_phid = $object->getPHID(); 66 81 67 82 $editor = $this->getEditor(); 68 - $images = $editor->getNewImages(); 69 - foreach ($images as $image) { 70 - if ($image->getPHID() !== $value) { 83 + foreach ($xactions as $xaction) { 84 + $new_phid = $xaction->getNewValue(); 85 + 86 + try { 87 + $new_image = $editor->loadPholioImage($object, $new_phid); 88 + } catch (Exception $ex) { 89 + $errors[] = $this->newInvalidError( 90 + pht( 91 + 'Unable to load replacement image ("%s"): %s', 92 + $new_phid, 93 + $ex->getMessage()), 94 + $xaction); 95 + continue; 96 + } 97 + 98 + $old_phid = $new_image->getReplacesImagePHID(); 99 + if (!$old_phid) { 100 + $errors[] = $this->newInvalidError( 101 + pht( 102 + 'Image ("%s") does not specify which image it replaces.', 103 + $new_phid), 104 + $xaction); 105 + continue; 106 + } 107 + 108 + try { 109 + $old_image = $editor->loadPholioImage($object, $old_phid); 110 + } catch (Exception $ex) { 111 + $errors[] = $this->newInvalidError( 112 + pht( 113 + 'Unable to load replaced image ("%s"): %s', 114 + $old_phid, 115 + $ex->getMessage()), 116 + $xaction); 117 + continue; 118 + } 119 + 120 + if ($old_image->getMockPHID() !== $mock_phid) { 121 + $errors[] = $this->newInvalidError( 122 + pht( 123 + 'Replaced image ("%s") belongs to the wrong mock ("%s", expected '. 124 + '"%s").', 125 + $old_phid, 126 + $old_image->getMockPHID(), 127 + $mock_phid), 128 + $xaction); 71 129 continue; 72 130 } 73 131 74 - $file_phids[] = $image->getFilePHID(); 132 + // TODO: You shouldn't be able to replace an image if it has already 133 + // been replaced. 134 + 75 135 } 76 136 77 - return $file_phids; 137 + return $errors; 78 138 } 79 139 80 140 }