Skip to content

Commit 21f07bf

Browse files
author
epriestley
committedDec 20, 2018
Make Images in Pholio refer to mocks by PHID instead of ID
Summary: Ref T11351. In Pholio, we currently use a `mockID`, but a `mockPHID` is generally preferable / more modern / more flexible. In particular, we need PHIDs to load handles and prefer PHIDs when exposing information to the API, and using PHIDs internally makes a bunch of things easier/better/faster and ~nothing harder/worse/slower. I'll add some inlines about a few things. Test Plan: Ran migrations, spot-checked database for sanity. Loaded Pholio, saw data unchanged. Created and edited images. Reviewers: amckinley Reviewed By: amckinley Subscribers: PHID-OPKG-gm6ozazyms6q6i22gyam Maniphest Tasks: T11351 Differential Revision: https://secure.phabricator.com/D19914
1 parent 961fd7e commit 21f07bf

10 files changed

+88
-49
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE {$NAMESPACE}_pholio.pholio_image
2+
ADD mockPHID VARBINARY(64);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
// Old images used a "mockID" instead of a "mockPHID" to reference mocks.
4+
// Set the "mockPHID" column to the value that corresponds to the "mockID".
5+
6+
$image = new PholioImage();
7+
$mock = new PholioMock();
8+
9+
$conn = $image->establishConnection('w');
10+
$iterator = new LiskRawMigrationIterator($conn, $image->getTableName());
11+
12+
foreach ($iterator as $image_row) {
13+
if ($image_row['mockPHID']) {
14+
continue;
15+
}
16+
17+
$mock_id = $image_row['mockID'];
18+
19+
$mock_row = queryfx_one(
20+
$conn,
21+
'SELECT phid FROM %R WHERE id = %d',
22+
$mock,
23+
$mock_id);
24+
25+
if (!$mock_row) {
26+
continue;
27+
}
28+
29+
queryfx(
30+
$conn,
31+
'UPDATE %R SET mockPHID = %s WHERE id = %d',
32+
$image,
33+
$mock_row['phid'],
34+
$image_row['id']);
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE {$NAMESPACE}_pholio.pholio_image
2+
DROP mockID;

‎src/applications/pholio/editor/PholioMockEditor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected function applyFinalEffects(
9191

9292
$images = $this->getNewImages();
9393
foreach ($images as $image) {
94-
$image->setMockID($object->getID());
94+
$image->setMockPHID($object->getPHID());
9595
$image->save();
9696
}
9797

‎src/applications/pholio/lipsum/PhabricatorPholioMockTestDataGenerator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function generateObject() {
6565
->setActor($author)
6666
->applyTransactions($mock, $transactions);
6767
foreach ($images as $image) {
68-
$image->setMockID($mock->getID());
68+
$image->setMockPHID($mock->getPHID());
6969
$image->save();
7070
}
7171

‎src/applications/pholio/phid/PholioImagePHIDType.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,9 @@ public function loadHandles(
3232
foreach ($handles as $phid => $handle) {
3333
$image = $objects[$phid];
3434

35-
$id = $image->getID();
36-
$mock_id = $image->getMockID();
37-
$name = $image->getName();
38-
39-
$handle->setURI("/M{$mock_id}/{$id}/");
40-
$handle->setName($name);
41-
$handle->setFullName($name);
35+
$handle
36+
->setName($image->getName())
37+
->setURI($image->getURI());
4238
}
4339
}
4440

‎src/applications/pholio/query/PholioImageQuery.php

+12-23
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ final class PholioImageQuery
55

66
private $ids;
77
private $phids;
8-
private $mockIDs;
9-
private $obsolete;
8+
private $mockPHIDs;
109

1110
private $needInlineComments;
1211
private $mockCache = array();
@@ -21,13 +20,8 @@ public function withPHIDs(array $phids) {
2120
return $this;
2221
}
2322

24-
public function withMockIDs(array $mock_ids) {
25-
$this->mockIDs = $mock_ids;
26-
return $this;
27-
}
28-
29-
public function withObsolete($obsolete) {
30-
$this->obsolete = $obsolete;
23+
public function withMockPHIDs(array $mock_phids) {
24+
$this->mockPHIDs = $mock_phids;
3125
return $this;
3226
}
3327

@@ -69,18 +63,11 @@ protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
6963
$this->phids);
7064
}
7165

72-
if ($this->mockIDs !== null) {
66+
if ($this->mockPHIDs !== null) {
7367
$where[] = qsprintf(
7468
$conn,
75-
'mockID IN (%Ld)',
76-
$this->mockIDs);
77-
}
78-
79-
if ($this->obsolete !== null) {
80-
$where[] = qsprintf(
81-
$conn,
82-
'isObsolete = %d',
83-
$this->obsolete);
69+
'mockPHID IN (%Ls)',
70+
$this->mockPHIDs);
8471
}
8572

8673
return $where;
@@ -92,16 +79,18 @@ protected function willFilterPage(array $images) {
9279
if ($this->getMockCache()) {
9380
$mocks = $this->getMockCache();
9481
} else {
95-
$mock_ids = mpull($images, 'getMockID');
82+
$mock_phids = mpull($images, 'getMockPHID');
83+
9684
// DO NOT set needImages to true; recursion results!
9785
$mocks = id(new PholioMockQuery())
9886
->setViewer($this->getViewer())
99-
->withIDs($mock_ids)
87+
->withPHIDs($mock_phids)
10088
->execute();
101-
$mocks = mpull($mocks, null, 'getID');
89+
$mocks = mpull($mocks, null, 'getPHID');
10290
}
91+
10392
foreach ($images as $index => $image) {
104-
$mock = idx($mocks, $image->getMockID());
93+
$mock = idx($mocks, $image->getMockPHID());
10594
if ($mock) {
10695
$image->attachMock($mock);
10796
} else {

‎src/applications/pholio/query/PholioMockQuery.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,18 @@ public static function loadImages(
115115
$need_inline_comments) {
116116
assert_instances_of($mocks, 'PholioMock');
117117

118-
$mock_map = mpull($mocks, null, 'getID');
118+
$mock_map = mpull($mocks, null, 'getPHID');
119119
$all_images = id(new PholioImageQuery())
120120
->setViewer($viewer)
121121
->setMockCache($mock_map)
122-
->withMockIDs(array_keys($mock_map))
122+
->withMockPHIDs(array_keys($mock_map))
123123
->needInlineComments($need_inline_comments)
124124
->execute();
125125

126-
$image_groups = mgroup($all_images, 'getMockID');
126+
$image_groups = mgroup($all_images, 'getMockPHID');
127127

128128
foreach ($mocks as $mock) {
129-
$mock_images = idx($image_groups, $mock->getID(), array());
129+
$mock_images = idx($image_groups, $mock->getPHID(), array());
130130
$mock->attachAllImages($mock_images);
131131
$active_images = mfilter($mock_images, 'getIsObsolete', true);
132132
$mock->attachImages(msort($active_images, 'getSequence'));

‎src/applications/pholio/storage/PholioImage.php

+27-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ final class PholioImage extends PholioDAO
66
PhabricatorExtendedPolicyInterface {
77

88
protected $authorPHID;
9-
protected $mockID;
9+
protected $mockPHID;
1010
protected $filePHID;
1111
protected $name;
1212
protected $description;
@@ -29,22 +29,17 @@ protected function getConfiguration() {
2929
return array(
3030
self::CONFIG_AUX_PHID => true,
3131
self::CONFIG_COLUMN_SCHEMA => array(
32-
'mockID' => 'id?',
32+
'mockPHID' => 'phid?',
3333
'name' => 'text128',
3434
'description' => 'text',
3535
'sequence' => 'uint32',
3636
'isObsolete' => 'bool',
3737
'replacesImagePHID' => 'phid?',
3838
),
3939
self::CONFIG_KEY_SCHEMA => array(
40-
'key_phid' => null,
41-
'keyPHID' => array(
42-
'columns' => array('phid'),
43-
'unique' => true,
44-
),
45-
'mockID' => array(
46-
'columns' => array('mockID', 'isObsolete', 'sequence'),
47-
),
40+
// TODO: There should be a key starting with "mockPHID" here at a
41+
// minimum, but it's not entirely clear what other columns we should
42+
// have as part of the key.
4843
),
4944
) + parent::getConfiguration();
5045
}
@@ -71,6 +66,10 @@ public function getMock() {
7166
return $this->assertAttached($this->mock);
7267
}
7368

69+
public function hasMock() {
70+
return (bool)$this->getMockPHID();
71+
}
72+
7473
public function attachInlineComments(array $inline_comments) {
7574
assert_instances_of($inline_comments, 'PholioTransactionComment');
7675
$this->inlineComments = $inline_comments;
@@ -82,6 +81,22 @@ public function getInlineComments() {
8281
return $this->inlineComments;
8382
}
8483

84+
public function getURI() {
85+
if ($this->hasMock()) {
86+
$mock = $this->getMock();
87+
88+
$mock_uri = $mock->getURI();
89+
$image_id = $this->getID();
90+
91+
return "{$mock_uri}/{$image_id}/";
92+
}
93+
94+
// For now, standalone images have no URI. We could provide one at some
95+
// point, although it's not clear that there's any motivation to do so.
96+
97+
return null;
98+
}
99+
85100

86101
/* -( PhabricatorPolicyInterface )----------------------------------------- */
87102

@@ -96,7 +111,7 @@ public function getCapabilities() {
96111
public function getPolicy($capability) {
97112
// If the image is attached to a mock, we use an extended policy to match
98113
// the mock's permissions.
99-
if ($this->getMockID()) {
114+
if ($this->hasMock()) {
100115
return PhabricatorPolicies::getMostOpenPolicy();
101116
}
102117

@@ -113,7 +128,7 @@ public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
113128

114129

115130
public function getExtendedPolicy($capability, PhabricatorUser $viewer) {
116-
if ($this->getMockID()) {
131+
if ($this->hasMock()) {
117132
return array(
118133
array(
119134
$this->getMock(),

‎src/applications/pholio/storage/PholioMock.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public function destroyObjectPermanently(
259259
$this->openTransaction();
260260
$images = id(new PholioImageQuery())
261261
->setViewer($engine->getViewer())
262-
->withMockIDs(array($this->getID()))
262+
->withMockIDs(array($this->getPHID()))
263263
->execute();
264264
foreach ($images as $image) {
265265
$image->delete();

0 commit comments

Comments
 (0)
Failed to load comments.