Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/diffusion/data/DiffusionBrowseResultSet.php
12241 views
1
<?php
2
3
final class DiffusionBrowseResultSet extends Phobject {
4
5
const REASON_IS_FILE = 'is-file';
6
const REASON_IS_SUBMODULE = 'is-submodule';
7
const REASON_IS_DELETED = 'is-deleted';
8
const REASON_IS_NONEXISTENT = 'nonexistent';
9
const REASON_BAD_COMMIT = 'bad-commit';
10
const REASON_IS_EMPTY = 'empty';
11
const REASON_IS_UNTRACKED_PARENT = 'untracked-parent';
12
13
private $paths;
14
private $isValidResults;
15
private $reasonForEmptyResultSet;
16
private $existedAtCommit;
17
private $deletedAtCommit;
18
19
public function setPaths(array $paths) {
20
assert_instances_of($paths, 'DiffusionRepositoryPath');
21
$this->paths = $paths;
22
return $this;
23
}
24
public function getPaths() {
25
return $this->paths;
26
}
27
28
public function setIsValidResults($is_valid) {
29
$this->isValidResults = $is_valid;
30
return $this;
31
}
32
public function isValidResults() {
33
return $this->isValidResults;
34
}
35
36
public function setReasonForEmptyResultSet($reason) {
37
$this->reasonForEmptyResultSet = $reason;
38
return $this;
39
}
40
public function getReasonForEmptyResultSet() {
41
return $this->reasonForEmptyResultSet;
42
}
43
44
public function setExistedAtCommit($existed_at_commit) {
45
$this->existedAtCommit = $existed_at_commit;
46
return $this;
47
}
48
public function getExistedAtCommit() {
49
return $this->existedAtCommit;
50
}
51
52
public function setDeletedAtCommit($deleted_at_commit) {
53
$this->deletedAtCommit = $deleted_at_commit;
54
return $this;
55
}
56
public function getDeletedAtCommit() {
57
return $this->deletedAtCommit;
58
}
59
60
public function toDictionary() {
61
$paths = $this->getPathDicts();
62
63
return array(
64
'paths' => $paths,
65
'isValidResults' => $this->isValidResults(),
66
'reasonForEmptyResultSet' => $this->getReasonForEmptyResultSet(),
67
'existedAtCommit' => $this->getExistedAtCommit(),
68
'deletedAtCommit' => $this->getDeletedAtCommit(),
69
);
70
}
71
72
public function getPathDicts() {
73
$paths = $this->getPaths();
74
if ($paths) {
75
return mpull($paths, 'toDictionary');
76
}
77
return array();
78
}
79
80
/**
81
* Get the best README file in this result set, if one exists.
82
*
83
* Callers should normally use `diffusion.filecontentquery` to pull README
84
* content.
85
*
86
* @return string|null Full path to best README, or null if one does not
87
* exist.
88
*/
89
public function getReadmePath() {
90
$allowed_types = array(
91
ArcanistDiffChangeType::FILE_NORMAL => true,
92
ArcanistDiffChangeType::FILE_TEXT => true,
93
);
94
95
$candidates = array();
96
foreach ($this->getPaths() as $path_object) {
97
if (empty($allowed_types[$path_object->getFileType()])) {
98
// Skip directories, images, etc.
99
continue;
100
}
101
102
$local_path = $path_object->getPath();
103
if (!preg_match('/^readme(\.|$)/i', $local_path)) {
104
// Skip files not named "README".
105
continue;
106
}
107
108
$full_path = $path_object->getFullPath();
109
$candidates[$full_path] = self::getReadmePriority($local_path);
110
}
111
112
if (!$candidates) {
113
return null;
114
}
115
116
arsort($candidates);
117
return head_key($candidates);
118
}
119
120
/**
121
* Get the priority of a README file.
122
*
123
* When a directory contains several README files, this function scores them
124
* so the caller can select a preferred file. See @{method:getReadmePath}.
125
*
126
* @param string Local README path, like "README.txt".
127
* @return int Priority score, with higher being more preferred.
128
*/
129
public static function getReadmePriority($path) {
130
$path = phutil_utf8_strtolower($path);
131
if ($path == 'readme') {
132
return 90;
133
}
134
135
$ext = last(explode('.', $path));
136
switch ($ext) {
137
case 'remarkup':
138
return 100;
139
case 'rainbow':
140
return 80;
141
case 'md':
142
return 70;
143
case 'txt':
144
return 60;
145
default:
146
return 50;
147
}
148
}
149
150
public static function newFromConduit(array $data) {
151
$paths = array();
152
$path_dicts = $data['paths'];
153
foreach ($path_dicts as $dict) {
154
$paths[] = DiffusionRepositoryPath::newFromDictionary($dict);
155
}
156
return id(new DiffusionBrowseResultSet())
157
->setPaths($paths)
158
->setIsValidResults($data['isValidResults'])
159
->setReasonForEmptyResultSet($data['reasonForEmptyResultSet'])
160
->setExistedAtCommit($data['existedAtCommit'])
161
->setDeletedAtCommit($data['deletedAtCommit']);
162
}
163
}
164
165