Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / diffusion / data / DiffusionBrowseResultSet.php
blob22d9e082518e39317afb62d4df36868b33c1e7d5
1 <?php
3 final class DiffusionBrowseResultSet extends Phobject {
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';
13 private $paths;
14 private $isValidResults;
15 private $reasonForEmptyResultSet;
16 private $existedAtCommit;
17 private $deletedAtCommit;
19 public function setPaths(array $paths) {
20 assert_instances_of($paths, 'DiffusionRepositoryPath');
21 $this->paths = $paths;
22 return $this;
24 public function getPaths() {
25 return $this->paths;
28 public function setIsValidResults($is_valid) {
29 $this->isValidResults = $is_valid;
30 return $this;
32 public function isValidResults() {
33 return $this->isValidResults;
36 public function setReasonForEmptyResultSet($reason) {
37 $this->reasonForEmptyResultSet = $reason;
38 return $this;
40 public function getReasonForEmptyResultSet() {
41 return $this->reasonForEmptyResultSet;
44 public function setExistedAtCommit($existed_at_commit) {
45 $this->existedAtCommit = $existed_at_commit;
46 return $this;
48 public function getExistedAtCommit() {
49 return $this->existedAtCommit;
52 public function setDeletedAtCommit($deleted_at_commit) {
53 $this->deletedAtCommit = $deleted_at_commit;
54 return $this;
56 public function getDeletedAtCommit() {
57 return $this->deletedAtCommit;
60 public function toDictionary() {
61 $paths = $this->getPathDicts();
63 return array(
64 'paths' => $paths,
65 'isValidResults' => $this->isValidResults(),
66 'reasonForEmptyResultSet' => $this->getReasonForEmptyResultSet(),
67 'existedAtCommit' => $this->getExistedAtCommit(),
68 'deletedAtCommit' => $this->getDeletedAtCommit(),
72 public function getPathDicts() {
73 $paths = $this->getPaths();
74 if ($paths) {
75 return mpull($paths, 'toDictionary');
77 return array();
80 /**
81 * Get the best README file in this result set, if one exists.
83 * Callers should normally use `diffusion.filecontentquery` to pull README
84 * content.
86 * @return string|null Full path to best README, or null if one does not
87 * exist.
89 public function getReadmePath() {
90 $allowed_types = array(
91 ArcanistDiffChangeType::FILE_NORMAL => true,
92 ArcanistDiffChangeType::FILE_TEXT => true,
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;
102 $local_path = $path_object->getPath();
103 if (!preg_match('/^readme(\.|$)/i', $local_path)) {
104 // Skip files not named "README".
105 continue;
108 $full_path = $path_object->getFullPath();
109 $candidates[$full_path] = self::getReadmePriority($local_path);
112 if (!$candidates) {
113 return null;
116 arsort($candidates);
117 return head_key($candidates);
121 * Get the priority of a README file.
123 * When a directory contains several README files, this function scores them
124 * so the caller can select a preferred file. See @{method:getReadmePath}.
126 * @param string Local README path, like "README.txt".
127 * @return int Priority score, with higher being more preferred.
129 public static function getReadmePriority($path) {
130 $path = phutil_utf8_strtolower($path);
131 if ($path == 'readme') {
132 return 90;
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;
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);
156 return id(new DiffusionBrowseResultSet())
157 ->setPaths($paths)
158 ->setIsValidResults($data['isValidResults'])
159 ->setReasonForEmptyResultSet($data['reasonForEmptyResultSet'])
160 ->setExistedAtCommit($data['existedAtCommit'])
161 ->setDeletedAtCommit($data['deletedAtCommit']);