From 6a2ae077915c21407f249c7b5af7cad52af39c7b Mon Sep 17 00:00:00 2001 From: epriestley Date: Fri, 21 Jun 2013 12:54:56 -0700 Subject: [PATCH] Abstract access to DifferentialInlineComment behind a Query Summary: Ref T2222. See D6260. Push all this junk behind a Query so I can move the storage out from underneath it. Test Plan: Viewed home page, list view, revision. Made draft, looked at preview, submitted draft, viewed inline, replied to inline. Reviewers: btrahan Reviewed By: btrahan CC: chad, aran Maniphest Tasks: T2222 Differential Revision: https://secure.phabricator.com/D6262 --- src/__phutil_library_map__.php | 2 + ...ifferential_getrevisioncomments_Method.php | 6 +- .../DifferentialChangesetViewController.php | 7 +- .../DifferentialCommentSaveController.php | 7 +- ...ifferentialInlineCommentEditController.php | 4 +- ...erentialInlineCommentPreviewController.php | 7 +- .../DifferentialRevisionViewController.php | 16 +-- .../editor/DifferentialCommentEditor.php | 7 +- .../query/DifferentialInlineCommentQuery.php | 129 ++++++++++++++++++ .../query/DifferentialRevisionQuery.php | 6 +- .../search/DifferentialSearchIndexer.php | 9 +- .../storage/DifferentialRevision.php | 6 +- 12 files changed, 165 insertions(+), 41 deletions(-) create mode 100644 src/applications/differential/query/DifferentialInlineCommentQuery.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 80b3bec8c2..ad1b08d1e6 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -370,6 +370,7 @@ phutil_register_library_map(array( 'DifferentialInlineCommentEditController' => 'applications/differential/controller/DifferentialInlineCommentEditController.php', 'DifferentialInlineCommentEditView' => 'applications/differential/view/DifferentialInlineCommentEditView.php', 'DifferentialInlineCommentPreviewController' => 'applications/differential/controller/DifferentialInlineCommentPreviewController.php', + 'DifferentialInlineCommentQuery' => 'applications/differential/query/DifferentialInlineCommentQuery.php', 'DifferentialInlineCommentView' => 'applications/differential/view/DifferentialInlineCommentView.php', 'DifferentialLinesFieldSpecification' => 'applications/differential/field/specification/DifferentialLinesFieldSpecification.php', 'DifferentialLintFieldSpecification' => 'applications/differential/field/specification/DifferentialLintFieldSpecification.php', @@ -2249,6 +2250,7 @@ phutil_register_library_map(array( 'DifferentialInlineCommentEditController' => 'PhabricatorInlineCommentController', 'DifferentialInlineCommentEditView' => 'AphrontView', 'DifferentialInlineCommentPreviewController' => 'PhabricatorInlineCommentPreviewController', + 'DifferentialInlineCommentQuery' => 'PhabricatorOffsetPagedQuery', 'DifferentialInlineCommentView' => 'AphrontView', 'DifferentialLinesFieldSpecification' => 'DifferentialFieldSpecification', 'DifferentialLintFieldSpecification' => 'DifferentialFieldSpecification', diff --git a/src/applications/differential/conduit/ConduitAPI_differential_getrevisioncomments_Method.php b/src/applications/differential/conduit/ConduitAPI_differential_getrevisioncomments_Method.php index 889ce6d98e..d2aaf69e57 100644 --- a/src/applications/differential/conduit/ConduitAPI_differential_getrevisioncomments_Method.php +++ b/src/applications/differential/conduit/ConduitAPI_differential_getrevisioncomments_Method.php @@ -40,9 +40,9 @@ final class ConduitAPI_differential_getrevisioncomments_Method $with_inlines = $request->getValue('inlines'); if ($with_inlines) { - $inlines = id(new DifferentialInlineComment())->loadAllWhere( - 'revisionID IN (%Ld)', - $revision_ids); + $inlines = id(new DifferentialInlineCommentQuery()) + ->withRevisionIDs($revision_ids) + ->execute(); $changesets = array(); if ($inlines) { $changesets = id(new DifferentialChangeset())->loadAllWhere( diff --git a/src/applications/differential/controller/DifferentialChangesetViewController.php b/src/applications/differential/controller/DifferentialChangesetViewController.php index e1c35f9808..0ef1ed0bd5 100644 --- a/src/applications/differential/controller/DifferentialChangesetViewController.php +++ b/src/applications/differential/controller/DifferentialChangesetViewController.php @@ -268,10 +268,9 @@ final class DifferentialChangesetViewController extends DifferentialController { return; } - return id(new DifferentialInlineComment())->loadAllWhere( - 'changesetID IN (%Ld) AND (commentID IS NOT NULL OR authorPHID = %s)', - $changeset_ids, - $author_phid); + return id(new DifferentialInlineCommentQuery()) + ->withAuthorAndChangesetIDs($author_phid, $changeset_ids) + ->execute(); } private function buildRawFileResponse( diff --git a/src/applications/differential/controller/DifferentialCommentSaveController.php b/src/applications/differential/controller/DifferentialCommentSaveController.php index 6f84e33282..d79b0dcb7b 100644 --- a/src/applications/differential/controller/DifferentialCommentSaveController.php +++ b/src/applications/differential/controller/DifferentialCommentSaveController.php @@ -39,10 +39,9 @@ final class DifferentialCommentSaveController extends DifferentialController { ->setAddedCCs($ccs) ->save(); } catch (DifferentialActionHasNoEffectException $no_effect) { - $has_inlines = id(new DifferentialInlineComment())->loadAllWhere( - 'authorPHID = %s AND revisionID = %d AND commentID IS NULL', - $request->getUser()->getPHID(), - $revision->getID()); + $has_inlines = id(new DifferentialInlineCommentQuery()) + ->withDraftComments($request->getUser()->getPHID(), $revision->getID()) + ->execute(); $dialog = new AphrontDialogView(); $dialog->setUser($request->getUser()); diff --git a/src/applications/differential/controller/DifferentialInlineCommentEditController.php b/src/applications/differential/controller/DifferentialInlineCommentEditController.php index 4e1987e917..83756fa88d 100644 --- a/src/applications/differential/controller/DifferentialInlineCommentEditController.php +++ b/src/applications/differential/controller/DifferentialInlineCommentEditController.php @@ -29,7 +29,9 @@ final class DifferentialInlineCommentEditController } protected function loadComment($id) { - return id(new DifferentialInlineComment())->load($id); + return id(new DifferentialInlineCommentQuery()) + ->withIDs(array($id)) + ->executeOne(); } protected function loadCommentForEdit($id) { diff --git a/src/applications/differential/controller/DifferentialInlineCommentPreviewController.php b/src/applications/differential/controller/DifferentialInlineCommentPreviewController.php index e5fea8c72f..7d330aa086 100644 --- a/src/applications/differential/controller/DifferentialInlineCommentPreviewController.php +++ b/src/applications/differential/controller/DifferentialInlineCommentPreviewController.php @@ -12,10 +12,9 @@ extends PhabricatorInlineCommentPreviewController { protected function loadInlineComments() { $user = $this->getRequest()->getUser(); - $inlines = id(new DifferentialInlineComment())->loadAllWhere( - 'authorPHID = %s AND revisionID = %d AND commentID IS NULL', - $user->getPHID(), - $this->revisionID); + $inlines = id(new DifferentialInlineCommentQuery()) + ->withDraftComments($user->getPHID(), $this->revisionID) + ->execute(); return $inlines; } diff --git a/src/applications/differential/controller/DifferentialRevisionViewController.php b/src/applications/differential/controller/DifferentialRevisionViewController.php index 6068f35eea..3f6e07f381 100644 --- a/src/applications/differential/controller/DifferentialRevisionViewController.php +++ b/src/applications/differential/controller/DifferentialRevisionViewController.php @@ -194,12 +194,9 @@ final class DifferentialRevisionViewController extends DifferentialController { pht('Show All Files Inline')))); $warning = $warning->render(); - $my_inlines = id(new DifferentialInlineComment())->loadAllWhere( - 'revisionID = %d AND commentID IS NULL AND authorPHID = %s AND '. - 'changesetID IN (%Ld)', - $this->revisionID, - $user->getPHID(), - mpull($changesets, 'getID')); + $my_inlines = id(new DifferentialInlineCommentQuery()) + ->withDraftComments($user->getPHID(), $this->revisionID) + ->execute(); $visible_changesets = array(); foreach ($inlines + $my_inlines as $inline) { @@ -643,10 +640,9 @@ final class DifferentialRevisionViewController extends DifferentialController { return $inline_comments; } - $inline_comments = id(new DifferentialInlineComment()) - ->loadAllWhere( - 'commentID in (%Ld)', - $comment_ids); + $inline_comments = id(new DifferentialInlineCommentQuery()) + ->withCommentIDs($comment_ids) + ->execute(); $load_changesets = array(); foreach ($inline_comments as $inline) { diff --git a/src/applications/differential/editor/DifferentialCommentEditor.php b/src/applications/differential/editor/DifferentialCommentEditor.php index b285a8cdc8..0bc098d751 100644 --- a/src/applications/differential/editor/DifferentialCommentEditor.php +++ b/src/applications/differential/editor/DifferentialCommentEditor.php @@ -116,10 +116,9 @@ final class DifferentialCommentEditor extends PhabricatorEditor { $inline_comments = array(); if ($this->attachInlineComments) { - $inline_comments = id(new DifferentialInlineComment())->loadAllWhere( - 'authorPHID = %s AND revisionID = %d AND commentID IS NULL', - $actor_phid, - $revision->getID()); + $inline_comments = id(new DifferentialInlineCommentQuery()) + ->withDraftComments($actor_phid, $revision->getID()) + ->execute(); } switch ($action) { diff --git a/src/applications/differential/query/DifferentialInlineCommentQuery.php b/src/applications/differential/query/DifferentialInlineCommentQuery.php new file mode 100644 index 0000000000..12191fa79a --- /dev/null +++ b/src/applications/differential/query/DifferentialInlineCommentQuery.php @@ -0,0 +1,129 @@ +revisionIDs = $ids; + return $this; + } + + public function withNotDraft($not_draft) { + $this->notDraft = $not_draft; + return $this; + } + + public function withIDs(array $ids) { + $this->ids = $ids; + return $this; + } + + public function withCommentIDs(array $ids) { + $this->commentIDs = $ids; + return $this; + } + + public function withAuthorAndChangesetIDs($author_phid, array $ids) { + $this->authorAndChangesetIDs = array($author_phid, $ids); + return $this; + } + + public function withDraftComments($author_phid, $revision_id) { + $this->draftComments = array($author_phid, $revision_id); + return $this; + } + + public function withDraftsByAuthors(array $author_phids) { + $this->draftsByAuthors = $author_phids; + return $this; + } + + public function execute() { + $table = new DifferentialInlineComment(); + $conn_r = $table->establishConnection('r'); + + $data = queryfx_all( + $conn_r, + 'SELECT * FROM %T %Q %Q', + $table->getTableName(), + $this->buildWhereClause($conn_r), + $this->buildLimitClause($conn_r)); + + return $table->loadAllFromArray($data); + } + + public function executeOne() { + return head($this->execute()); + } + + private function buildWhereClause(AphrontDatabaseConnection $conn_r) { + $where = array(); + + if ($this->revisionIDs) { + $where[] = qsprintf( + $conn_r, + 'revisionID IN (%Ld)', + $this->revisionIDs); + } + + if ($this->notDraft) { + $where[] = qsprintf( + $conn_r, + 'commentID IS NOT NULL'); + } + + if ($this->ids) { + $where[] = qsprintf( + $conn_r, + 'id IN (%Ld)', + $this->ids); + } + + if ($this->commentIDs) { + $where[] = qsprintf( + $conn_r, + 'commentID in (%Ld)', + $this->commentIDs); + } + + if ($this->authorAndChangesetIDs) { + list($phid, $ids) = $this->authorAndChangesetIDs; + $where[] = qsprintf( + $conn_r, + 'authorPHID = %s AND changesetID IN (%Ld)', + $phid, + $ids); + } + + if ($this->draftComments) { + list($phid, $rev_id) = $this->draftComments; + $where[] = qsprintf( + $conn_r, + 'authorPHID = %s AND revisionID = %d AND commentID IS NULL', + $phid, + $rev_id); + } + + if ($this->draftsByAuthors) { + $where[] = qsprintf( + $conn_r, + 'authorPHID IN (%Ls) AND commentID IS NULL', + $this->draftsByAuthors); + } + + return $this->formatWhereClause($where); + } + +} diff --git a/src/applications/differential/query/DifferentialRevisionQuery.php b/src/applications/differential/query/DifferentialRevisionQuery.php index 1e5f490ed0..c3bac9c3b1 100644 --- a/src/applications/differential/query/DifferentialRevisionQuery.php +++ b/src/applications/differential/query/DifferentialRevisionQuery.php @@ -492,9 +492,9 @@ final class DifferentialRevisionQuery { $this->draftRevisions[] = substr($draft->getDraftKey(), $len); } - $inlines = id(new DifferentialInlineComment())->loadAllWhere( - 'commentID IS NULL AND authorPHID IN (%Ls)', - $this->draftAuthors); + $inlines = id(new DifferentialInlineCommentQuery()) + ->withDraftsByAuthors($this->draftAuthors) + ->execute(); foreach ($inlines as $inline) { $this->draftRevisions[] = $inline->getRevisionID(); } diff --git a/src/applications/differential/search/DifferentialSearchIndexer.php b/src/applications/differential/search/DifferentialSearchIndexer.php index 02b8e0d52b..3d40175650 100644 --- a/src/applications/differential/search/DifferentialSearchIndexer.php +++ b/src/applications/differential/search/DifferentialSearchIndexer.php @@ -57,11 +57,10 @@ final class DifferentialSearchIndexer ->withRevisionIDs(array($rev->getID())) ->execute(); - $inlines = $rev->loadRelatives( - new DifferentialInlineComment(), - 'revisionID', - 'getID', - '(commentID IS NOT NULL)'); + $inlines = id(new DifferentialInlineCommentQuery()) + ->withRevisionIDs(array($rev->getID())) + ->withNotDraft(true) + ->execute(); $touches = array(); diff --git a/src/applications/differential/storage/DifferentialRevision.php b/src/applications/differential/storage/DifferentialRevision.php index 472fae1239..ae2edafb18 100644 --- a/src/applications/differential/storage/DifferentialRevision.php +++ b/src/applications/differential/storage/DifferentialRevision.php @@ -199,9 +199,9 @@ final class DifferentialRevision extends DifferentialDAO $comment->delete(); } - $inlines = id(new DifferentialInlineComment())->loadAllWhere( - 'revisionID = %d', - $this->getID()); + $inlines = id(new DifferentialInlineCommentQuery()) + ->withRevisionIDs(array($this->getID())) + ->execute(); foreach ($inlines as $inline) { $inline->delete(); }