diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -10755,7 +10755,11 @@ '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 --- a/src/applications/phriction/query/PhrictionContentQuery.php +++ b/src/applications/phriction/query/PhrictionContentQuery.php @@ -5,6 +5,7 @@ private $ids; private $phids; + private $documentPHIDs; public function withIDs(array $ids) { $this->ids = $ids; @@ -16,6 +17,11 @@ return $this; } + public function withDocumentPHIDs(array $phids) { + $this->documentPHIDs = $phids; + return $this; + } + public function newResultObject() { return new PhrictionContent(); } @@ -30,20 +36,78 @@ 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 --- a/src/applications/phriction/storage/PhrictionContent.php +++ b/src/applications/phriction/storage/PhrictionContent.php @@ -1,9 +1,11 @@ true, @@ -56,4 +60,50 @@ ->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 --- a/src/applications/phriction/storage/PhrictionDocument.php +++ b/src/applications/phriction/storage/PhrictionDocument.php @@ -236,15 +236,16 @@ $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(); }