Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/differential/query/DifferentialDiffInlineCommentQuery.php
12256 views
1
<?php
2
3
final class DifferentialDiffInlineCommentQuery
4
extends PhabricatorDiffInlineCommentQuery {
5
6
private $revisionPHIDs;
7
8
protected function newApplicationTransactionCommentTemplate() {
9
return new DifferentialTransactionComment();
10
}
11
12
public function withRevisionPHIDs(array $phids) {
13
$this->revisionPHIDs = $phids;
14
return $this;
15
}
16
17
public function withObjectPHIDs(array $phids) {
18
return $this->withRevisionPHIDs($phids);
19
}
20
21
protected function buildInlineCommentWhereClauseParts(
22
AphrontDatabaseConnection $conn) {
23
$where = array();
24
$alias = $this->getPrimaryTableAlias();
25
26
$where[] = qsprintf(
27
$conn,
28
'changesetID IS NOT NULL');
29
30
return $where;
31
}
32
33
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
34
$where = parent::buildWhereClauseParts($conn);
35
$alias = $this->getPrimaryTableAlias();
36
37
if ($this->revisionPHIDs !== null) {
38
$where[] = qsprintf(
39
$conn,
40
'%T.revisionPHID IN (%Ls)',
41
$alias,
42
$this->revisionPHIDs);
43
}
44
45
return $where;
46
}
47
48
protected function loadHiddenCommentIDs(
49
$viewer_phid,
50
array $comments) {
51
52
$table = new DifferentialHiddenComment();
53
$conn = $table->establishConnection('r');
54
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'));
63
64
$id_map = ipull($rows, 'commentID');
65
$id_map = array_fuse($id_map);
66
67
return $id_map;
68
}
69
70
protected function newInlineContextFromCacheData(array $map) {
71
return PhabricatorDiffInlineCommentContext::newFromCacheData($map);
72
}
73
74
protected function newInlineContextMap(array $inlines) {
75
$viewer = $this->getViewer();
76
$map = array();
77
78
$changeset_ids = mpull($inlines, 'getChangesetID');
79
80
$changesets = id(new DifferentialChangesetQuery())
81
->setViewer($viewer)
82
->withIDs($changeset_ids)
83
->needHunks(true)
84
->execute();
85
$changesets = mpull($changesets, null, 'getID');
86
87
foreach ($inlines as $key => $inline) {
88
$changeset = idx($changesets, $inline->getChangesetID());
89
90
if (!$changeset) {
91
continue;
92
}
93
94
$hunks = $changeset->getHunks();
95
96
$is_simple =
97
(count($hunks) === 1) &&
98
((int)head($hunks)->getOldOffset() <= 1) &&
99
((int)head($hunks)->getNewOffset() <= 1);
100
101
if (!$is_simple) {
102
continue;
103
}
104
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();
113
}
114
115
$corpus = phutil_split_lines($corpus);
116
117
// Adjust the line number into a 0-based offset.
118
$offset = $inline->getLineNumber();
119
$offset = $offset - 1;
120
121
// Adjust the inclusive range length into a row count.
122
$length = $inline->getLineLength();
123
$length = $length + 1;
124
125
$head_min = max(0, $offset - 3);
126
$head_max = $offset;
127
$head_len = $head_max - $head_min;
128
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();
134
}
135
136
$body = array_slice($corpus, $offset, $length, true);
137
138
$tail = array_slice($corpus, $offset + $length, 3, true);
139
$tail = $this->simplifyContext($tail, false);
140
141
$context = id(new PhabricatorDiffInlineCommentContext())
142
->setFilename($filename)
143
->setHeadLines($head)
144
->setBodyLines($body)
145
->setTailLines($tail);
146
147
$map[$key] = $context;
148
}
149
150
return $map;
151
}
152
153
}
154
155