1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-04-12 20:38:41 +02:00

Reduce the amount of weird "static" and "cache" behavior in Pholio query classes

Summary: Depends on D19922. Ref T11351. These query classes have some slightly weird behavior, including `public static function loadImages(...)`. Convert all this stuff into more standard query patterns.

Test Plan: Grepped for callsites, browsed around in Pholio.

Reviewers: amckinley

Reviewed By: amckinley

Maniphest Tasks: T11351

Differential Revision: https://secure.phabricator.com/D19923
This commit is contained in:
epriestley 2018-12-19 11:47:45 -08:00
parent 1e2bc7775b
commit b1e7e3a10e
4 changed files with 95 additions and 88 deletions

View file

@ -7,10 +7,13 @@ final class PholioMockTimelineEngine
$viewer = $this->getViewer(); $viewer = $this->getViewer();
$object = $this->getObject(); $object = $this->getObject();
PholioMockQuery::loadImages( $images = id(new PholioImageQuery())
$viewer, ->setViewer($viewer)
array($object), ->withMocks(array($object))
$need_inline_comments = true); ->needInlineComments(true)
->execute();
$object->attachImages($images);
return id(new PholioTransactionView()) return id(new PholioTransactionView())
->setMock($object); ->setMock($object);

View file

@ -6,9 +6,9 @@ final class PholioImageQuery
private $ids; private $ids;
private $phids; private $phids;
private $mockPHIDs; private $mockPHIDs;
private $mocks;
private $needInlineComments; private $needInlineComments;
private $mockCache = array();
public function withIDs(array $ids) { public function withIDs(array $ids) {
$this->ids = $ids; $this->ids = $ids;
@ -20,6 +20,16 @@ final class PholioImageQuery
return $this; return $this;
} }
public function withMocks(array $mocks) {
assert_instances_of($mocks, 'PholioMock');
$mocks = mpull($mocks, null, 'getPHID');
$this->mocks = $mocks;
$this->mockPHIDs = array_keys($mocks);
return $this;
}
public function withMockPHIDs(array $mock_phids) { public function withMockPHIDs(array $mock_phids) {
$this->mockPHIDs = $mock_phids; $this->mockPHIDs = $mock_phids;
return $this; return $this;
@ -30,14 +40,6 @@ final class PholioImageQuery
return $this; return $this;
} }
public function setMockCache($mock_cache) {
$this->mockCache = $mock_cache;
return $this;
}
public function getMockCache() {
return $this->mockCache;
}
public function newResultObject() { public function newResultObject() {
return new PholioImage(); return new PholioImage();
} }
@ -76,26 +78,40 @@ final class PholioImageQuery
protected function willFilterPage(array $images) { protected function willFilterPage(array $images) {
assert_instances_of($images, 'PholioImage'); assert_instances_of($images, 'PholioImage');
if ($this->getMockCache()) { $mock_phids = array();
$mocks = $this->getMockCache(); foreach ($images as $image) {
} else { if (!$image->hasMock()) {
$mock_phids = mpull($images, 'getMockPHID'); continue;
}
// DO NOT set needImages to true; recursion results! $mock_phids[] = $image->getMockPHID();
}
if ($mock_phids) {
if ($this->mocks) {
$mocks = $this->mocks;
} else {
$mocks = id(new PholioMockQuery()) $mocks = id(new PholioMockQuery())
->setViewer($this->getViewer()) ->setViewer($this->getViewer())
->withPHIDs($mock_phids) ->withPHIDs($mock_phids)
->execute(); ->execute();
$mocks = mpull($mocks, null, 'getPHID');
} }
foreach ($images as $index => $image) { $mocks = mpull($mocks, null, 'getPHID');
foreach ($images as $key => $image) {
if (!$image->hasMock()) {
continue;
}
$mock = idx($mocks, $image->getMockPHID()); $mock = idx($mocks, $image->getMockPHID());
if ($mock) { if (!$mock) {
unset($images[$key]);
$this->didRejectResult($image);
continue;
}
$image->attachMock($mock); $image->attachMock($mock);
} else {
// mock is missing or we can't see it
unset($images[$index]);
} }
} }

View file

@ -58,21 +58,14 @@ final class PholioMockQuery
} }
protected function loadPage() { protected function loadPage() {
$mocks = $this->loadStandardPage($this->newResultObject()); if ($this->needInlineComments && !$this->needImages) {
throw new Exception(
if ($mocks && $this->needImages) { pht(
self::loadImages($this->getViewer(), $mocks, $this->needInlineComments); 'You can not query for inline comments without also querying for '.
'images.'));
} }
if ($mocks && $this->needCoverFiles) { return $this->loadStandardPage(new PholioMock());
$this->loadCoverFiles($mocks);
}
if ($mocks && $this->needTokenCounts) {
$this->loadTokenCounts($mocks);
}
return $mocks;
} }
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
@ -109,60 +102,55 @@ final class PholioMockQuery
return $where; return $where;
} }
public static function loadImages( protected function didFilterPage(array $mocks) {
PhabricatorUser $viewer, $viewer = $this->getViewer();
array $mocks,
$need_inline_comments) {
assert_instances_of($mocks, 'PholioMock');
$mock_map = mpull($mocks, null, 'getPHID'); if ($this->needImages) {
$all_images = id(new PholioImageQuery()) $images = id(new PholioImageQuery())
->setViewer($viewer) ->setViewer($viewer)
->setMockCache($mock_map) ->withMocks($mocks)
->withMockPHIDs(array_keys($mock_map)) ->needInlineComments($this->needInlineComments)
->needInlineComments($need_inline_comments)
->execute(); ->execute();
$image_groups = mgroup($all_images, 'getMockPHID'); $image_groups = mgroup($images, 'getMockPHID');
foreach ($mocks as $mock) { foreach ($mocks as $mock) {
$mock_images = idx($image_groups, $mock->getPHID(), array()); $images = idx($image_groups, $mock->getPHID(), array());
$mock->attachImages($mock_images); $mock->attachImages($images);
} }
} }
private function loadCoverFiles(array $mocks) { if ($this->needCoverFiles) {
assert_instances_of($mocks, 'PholioMock');
$cover_file_phids = mpull($mocks, 'getCoverPHID');
$cover_files = id(new PhabricatorFileQuery()) $cover_files = id(new PhabricatorFileQuery())
->setViewer($this->getViewer()) ->setViewer($viewer)
->withPHIDs($cover_file_phids) ->withPHIDs(mpull($mocks, 'getCoverPHID'))
->execute(); ->execute();
$cover_files = mpull($cover_files, null, 'getPHID'); $cover_files = mpull($cover_files, null, 'getPHID');
foreach ($mocks as $mock) { foreach ($mocks as $mock) {
$file = idx($cover_files, $mock->getCoverPHID()); $file = idx($cover_files, $mock->getCoverPHID());
if (!$file) { if (!$file) {
$file = PhabricatorFile::loadBuiltin($this->getViewer(), 'missing.png'); $file = PhabricatorFile::loadBuiltin(
$viewer,
'missing.png');
} }
$mock->attachCoverFile($file); $mock->attachCoverFile($file);
} }
} }
private function loadTokenCounts(array $mocks) { if ($this->needTokenCounts) {
assert_instances_of($mocks, 'PholioMock');
$phids = mpull($mocks, 'getPHID');
$counts = id(new PhabricatorTokenCountQuery()) $counts = id(new PhabricatorTokenCountQuery())
->withObjectPHIDs($phids) ->withObjectPHIDs(mpull($mocks, 'getPHID'))
->execute(); ->execute();
foreach ($mocks as $mock) { foreach ($mocks as $mock) {
$mock->attachTokenCount(idx($counts, $mock->getPHID(), 0)); $token_count = idx($counts, $mock->getPHID(), 0);
$mock->attachTokenCount($token_count);
} }
} }
return $mocks;
}
public function getQueryApplicationClass() { public function getQueryApplicationClass() {
return 'PhabricatorPholioApplication'; return 'PhabricatorPholioApplication';
} }

View file

@ -110,7 +110,7 @@ final class PholioMockImagesView extends AphrontView {
); );
} }
$ids = mpull($mock->getActiveImages(), 'getID'); $ids = mpull($mock->getActiveImages(), null, 'getID');
if ($this->imageID && isset($ids[$this->imageID])) { if ($this->imageID && isset($ids[$this->imageID])) {
$selected_id = $this->imageID; $selected_id = $this->imageID;
} else { } else {