diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index b6e29fc5ed..2eb9a1893b 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -10755,7 +10755,11 @@ phutil_register_library_map(array( 'PhrictionChangeType' => 'PhrictionConstants', 'PhrictionConduitAPIMethod' => 'ConduitAPIMethod', 'PhrictionConstants' => 'Phobject', - 'PhrictionContent' => 'PhrictionDAO', + 'PhrictionContent' => array( + 'PhrictionDAO', + 'PhabricatorPolicyInterface', + 'PhabricatorDestructibleInterface', + ), 'PhrictionContentPHIDType' => 'PhabricatorPHIDType', 'PhrictionContentQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', 'PhrictionController' => 'PhabricatorController', diff --git a/src/applications/phriction/query/PhrictionContentQuery.php b/src/applications/phriction/query/PhrictionContentQuery.php index 4f76186a77..0ab132abdd 100644 --- a/src/applications/phriction/query/PhrictionContentQuery.php +++ b/src/applications/phriction/query/PhrictionContentQuery.php @@ -5,6 +5,7 @@ final class PhrictionContentQuery private $ids; private $phids; + private $documentPHIDs; public function withIDs(array $ids) { $this->ids = $ids; @@ -16,6 +17,11 @@ final class PhrictionContentQuery return $this; } + public function withDocumentPHIDs(array $phids) { + $this->documentPHIDs = $phids; + return $this; + } + public function newResultObject() { return new PhrictionContent(); } @@ -30,20 +36,78 @@ final class PhrictionContentQuery if ($this->ids !== null) { $where[] = qsprintf( $conn, - 'id IN (%Ld)', + 'c.id IN (%Ld)', $this->ids); } if ($this->phids !== null) { $where[] = qsprintf( $conn, - 'phid IN (%Ls)', + 'c.phid IN (%Ls)', $this->phids); } + if ($this->documentPHIDs !== null) { + $where[] = qsprintf( + $conn, + 'd.phid IN (%Ls)', + $this->documentPHIDs); + } + return $where; } + protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) { + $joins = parent::buildJoinClauseParts($conn); + + if ($this->shouldJoinDocumentTable()) { + $joins[] = qsprintf( + $conn, + 'JOIN %T d ON d.id = c.documentID', + id(new PhrictionDocument())->getTableName()); + } + + return $joins; + } + + protected function willFilterPage(array $contents) { + $document_ids = mpull($contents, 'getDocumentID'); + + $documents = id(new PhrictionDocumentQuery()) + ->setViewer($this->getViewer()) + ->setParentQuery($this) + ->withIDs($document_ids) + ->execute(); + $documents = mpull($documents, null, 'getID'); + + foreach ($contents as $key => $content) { + $document_id = $content->getDocumentID(); + + $document = idx($documents, $document_id); + if (!$document) { + unset($contents[$key]); + $this->didRejectResult($content); + continue; + } + + $content->attachDocument($document); + } + + return $contents; + } + + private function shouldJoinDocumentTable() { + if ($this->documentPHIDs !== null) { + return true; + } + + return false; + } + + protected function getPrimaryTableAlias() { + return 'c'; + } + public function getQueryApplicationClass() { return 'PhabricatorPhrictionApplication'; } diff --git a/src/applications/phriction/storage/PhrictionContent.php b/src/applications/phriction/storage/PhrictionContent.php index 44c8184eb7..a6aee7226f 100644 --- a/src/applications/phriction/storage/PhrictionContent.php +++ b/src/applications/phriction/storage/PhrictionContent.php @@ -1,9 +1,11 @@ true, @@ -56,4 +60,50 @@ final class PhrictionContent ->setGenerateTableOfContents(true); } + public function attachDocument(PhrictionDocument $document) { + $this->document = $document; + return $this; + } + + public function getDocument() { + return $this->assertAttached($this->document); + } + + +/* -( PhabricatorPolicyInterface )----------------------------------------- */ + + + public function getCapabilities() { + return array( + PhabricatorPolicyCapability::CAN_VIEW, + ); + } + + public function getPolicy($capability) { + return PhabricatorPolicies::getMostOpenPolicy(); + } + + public function hasAutomaticCapability($capability, PhabricatorUser $viewer) { + return false; + } + + +/* -( PhabricatorExtendedPolicyInterface )--------------------------------- */ + + + public function getExtendedPolicy($capability, PhabricatorUser $viewer) { + return array( + array($this->getDocument(), PhabricatorPolicyCapability::CAN_VIEW), + ); + } + + +/* -( PhabricatorDestructibleInterface )----------------------------------- */ + + + public function destroyObjectPermanently( + PhabricatorDestructionEngine $engine) { + $this->delete(); + } + } diff --git a/src/applications/phriction/storage/PhrictionDocument.php b/src/applications/phriction/storage/PhrictionDocument.php index 7a3e178882..01e85d4954 100644 --- a/src/applications/phriction/storage/PhrictionDocument.php +++ b/src/applications/phriction/storage/PhrictionDocument.php @@ -236,15 +236,16 @@ final class PhrictionDocument extends PhrictionDAO $this->openTransaction(); - $this->delete(); - - $contents = id(new PhrictionContent())->loadAllWhere( - 'documentID = %d', - $this->getID()); + $contents = id(new PhrictionContentQuery()) + ->setViewer($engine->getViewer()) + ->withDocumentPHIDs(array($this->getPHID())) + ->execute(); foreach ($contents as $content) { - $content->delete(); + $engine->destroyObject($content); } + $this->delete(); + $this->saveTransaction(); }