Remove product literal strings in "pht()", part 6
[phabricator.git] / src / applications / differential / query / DifferentialDiffInlineCommentQuery.php
blob7e2f7127b1d6fbfabaa50db706c60d8c8ac4bea3
1 <?php
3 final class DifferentialDiffInlineCommentQuery
4 extends PhabricatorDiffInlineCommentQuery {
6 private $revisionPHIDs;
8 protected function newApplicationTransactionCommentTemplate() {
9 return new DifferentialTransactionComment();
12 public function withRevisionPHIDs(array $phids) {
13 $this->revisionPHIDs = $phids;
14 return $this;
17 public function withObjectPHIDs(array $phids) {
18 return $this->withRevisionPHIDs($phids);
21 protected function buildInlineCommentWhereClauseParts(
22 AphrontDatabaseConnection $conn) {
23 $where = array();
24 $alias = $this->getPrimaryTableAlias();
26 $where[] = qsprintf(
27 $conn,
28 'changesetID IS NOT NULL');
30 return $where;
33 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
34 $where = parent::buildWhereClauseParts($conn);
35 $alias = $this->getPrimaryTableAlias();
37 if ($this->revisionPHIDs !== null) {
38 $where[] = qsprintf(
39 $conn,
40 '%T.revisionPHID IN (%Ls)',
41 $alias,
42 $this->revisionPHIDs);
45 return $where;
48 protected function loadHiddenCommentIDs(
49 $viewer_phid,
50 array $comments) {
52 $table = new DifferentialHiddenComment();
53 $conn = $table->establishConnection('r');
55 $rows = queryfx_all(
56 $conn,
57 'SELECT commentID FROM %R
58 WHERE userPHID = %s
59 AND commentID IN (%Ld)',
60 $table,
61 $viewer_phid,
62 mpull($comments, 'getID'));
64 $id_map = ipull($rows, 'commentID');
65 $id_map = array_fuse($id_map);
67 return $id_map;
70 protected function newInlineContextFromCacheData(array $map) {
71 return PhabricatorDiffInlineCommentContext::newFromCacheData($map);
74 protected function newInlineContextMap(array $inlines) {
75 $viewer = $this->getViewer();
76 $map = array();
78 $changeset_ids = mpull($inlines, 'getChangesetID');
80 $changesets = id(new DifferentialChangesetQuery())
81 ->setViewer($viewer)
82 ->withIDs($changeset_ids)
83 ->needHunks(true)
84 ->execute();
85 $changesets = mpull($changesets, null, 'getID');
87 foreach ($inlines as $key => $inline) {
88 $changeset = idx($changesets, $inline->getChangesetID());
90 if (!$changeset) {
91 continue;
94 $hunks = $changeset->getHunks();
96 $is_simple =
97 (count($hunks) === 1) &&
98 ((int)head($hunks)->getOldOffset() <= 1) &&
99 ((int)head($hunks)->getNewOffset() <= 1);
101 if (!$is_simple) {
102 continue;
105 if ($inline->getIsNewFile()) {
106 $vector = $changeset->getNewStatePathVector();
107 $filename = last($vector);
108 $corpus = $changeset->makeNewFile();
109 } else {
110 $vector = $changeset->getOldStatePathVector();
111 $filename = last($vector);
112 $corpus = $changeset->makeOldFile();
115 $corpus = phutil_split_lines($corpus);
117 // Adjust the line number into a 0-based offset.
118 $offset = $inline->getLineNumber();
119 $offset = $offset - 1;
121 // Adjust the inclusive range length into a row count.
122 $length = $inline->getLineLength();
123 $length = $length + 1;
125 $head_min = max(0, $offset - 3);
126 $head_max = $offset;
127 $head_len = $head_max - $head_min;
129 if ($head_len) {
130 $head = array_slice($corpus, $head_min, $head_len, true);
131 $head = $this->simplifyContext($head, true);
132 } else {
133 $head = array();
136 $body = array_slice($corpus, $offset, $length, true);
138 $tail = array_slice($corpus, $offset + $length, 3, true);
139 $tail = $this->simplifyContext($tail, false);
141 $context = id(new PhabricatorDiffInlineCommentContext())
142 ->setFilename($filename)
143 ->setHeadLines($head)
144 ->setBodyLines($body)
145 ->setTailLines($tail);
147 $map[$key] = $context;
150 return $map;