Path: blob/master/src/applications/differential/query/DifferentialDiffInlineCommentQuery.php
12256 views
<?php12final class DifferentialDiffInlineCommentQuery3extends PhabricatorDiffInlineCommentQuery {45private $revisionPHIDs;67protected function newApplicationTransactionCommentTemplate() {8return new DifferentialTransactionComment();9}1011public function withRevisionPHIDs(array $phids) {12$this->revisionPHIDs = $phids;13return $this;14}1516public function withObjectPHIDs(array $phids) {17return $this->withRevisionPHIDs($phids);18}1920protected function buildInlineCommentWhereClauseParts(21AphrontDatabaseConnection $conn) {22$where = array();23$alias = $this->getPrimaryTableAlias();2425$where[] = qsprintf(26$conn,27'changesetID IS NOT NULL');2829return $where;30}3132protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {33$where = parent::buildWhereClauseParts($conn);34$alias = $this->getPrimaryTableAlias();3536if ($this->revisionPHIDs !== null) {37$where[] = qsprintf(38$conn,39'%T.revisionPHID IN (%Ls)',40$alias,41$this->revisionPHIDs);42}4344return $where;45}4647protected function loadHiddenCommentIDs(48$viewer_phid,49array $comments) {5051$table = new DifferentialHiddenComment();52$conn = $table->establishConnection('r');5354$rows = queryfx_all(55$conn,56'SELECT commentID FROM %R57WHERE userPHID = %s58AND commentID IN (%Ld)',59$table,60$viewer_phid,61mpull($comments, 'getID'));6263$id_map = ipull($rows, 'commentID');64$id_map = array_fuse($id_map);6566return $id_map;67}6869protected function newInlineContextFromCacheData(array $map) {70return PhabricatorDiffInlineCommentContext::newFromCacheData($map);71}7273protected function newInlineContextMap(array $inlines) {74$viewer = $this->getViewer();75$map = array();7677$changeset_ids = mpull($inlines, 'getChangesetID');7879$changesets = id(new DifferentialChangesetQuery())80->setViewer($viewer)81->withIDs($changeset_ids)82->needHunks(true)83->execute();84$changesets = mpull($changesets, null, 'getID');8586foreach ($inlines as $key => $inline) {87$changeset = idx($changesets, $inline->getChangesetID());8889if (!$changeset) {90continue;91}9293$hunks = $changeset->getHunks();9495$is_simple =96(count($hunks) === 1) &&97((int)head($hunks)->getOldOffset() <= 1) &&98((int)head($hunks)->getNewOffset() <= 1);99100if (!$is_simple) {101continue;102}103104if ($inline->getIsNewFile()) {105$vector = $changeset->getNewStatePathVector();106$filename = last($vector);107$corpus = $changeset->makeNewFile();108} else {109$vector = $changeset->getOldStatePathVector();110$filename = last($vector);111$corpus = $changeset->makeOldFile();112}113114$corpus = phutil_split_lines($corpus);115116// Adjust the line number into a 0-based offset.117$offset = $inline->getLineNumber();118$offset = $offset - 1;119120// Adjust the inclusive range length into a row count.121$length = $inline->getLineLength();122$length = $length + 1;123124$head_min = max(0, $offset - 3);125$head_max = $offset;126$head_len = $head_max - $head_min;127128if ($head_len) {129$head = array_slice($corpus, $head_min, $head_len, true);130$head = $this->simplifyContext($head, true);131} else {132$head = array();133}134135$body = array_slice($corpus, $offset, $length, true);136137$tail = array_slice($corpus, $offset + $length, 3, true);138$tail = $this->simplifyContext($tail, false);139140$context = id(new PhabricatorDiffInlineCommentContext())141->setFilename($filename)142->setHeadLines($head)143->setBodyLines($body)144->setTailLines($tail);145146$map[$key] = $context;147}148149return $map;150}151152}153154155