Path: blob/master/src/applications/diffusion/data/DiffusionBrowseResultSet.php
12241 views
<?php12final class DiffusionBrowseResultSet extends Phobject {34const REASON_IS_FILE = 'is-file';5const REASON_IS_SUBMODULE = 'is-submodule';6const REASON_IS_DELETED = 'is-deleted';7const REASON_IS_NONEXISTENT = 'nonexistent';8const REASON_BAD_COMMIT = 'bad-commit';9const REASON_IS_EMPTY = 'empty';10const REASON_IS_UNTRACKED_PARENT = 'untracked-parent';1112private $paths;13private $isValidResults;14private $reasonForEmptyResultSet;15private $existedAtCommit;16private $deletedAtCommit;1718public function setPaths(array $paths) {19assert_instances_of($paths, 'DiffusionRepositoryPath');20$this->paths = $paths;21return $this;22}23public function getPaths() {24return $this->paths;25}2627public function setIsValidResults($is_valid) {28$this->isValidResults = $is_valid;29return $this;30}31public function isValidResults() {32return $this->isValidResults;33}3435public function setReasonForEmptyResultSet($reason) {36$this->reasonForEmptyResultSet = $reason;37return $this;38}39public function getReasonForEmptyResultSet() {40return $this->reasonForEmptyResultSet;41}4243public function setExistedAtCommit($existed_at_commit) {44$this->existedAtCommit = $existed_at_commit;45return $this;46}47public function getExistedAtCommit() {48return $this->existedAtCommit;49}5051public function setDeletedAtCommit($deleted_at_commit) {52$this->deletedAtCommit = $deleted_at_commit;53return $this;54}55public function getDeletedAtCommit() {56return $this->deletedAtCommit;57}5859public function toDictionary() {60$paths = $this->getPathDicts();6162return array(63'paths' => $paths,64'isValidResults' => $this->isValidResults(),65'reasonForEmptyResultSet' => $this->getReasonForEmptyResultSet(),66'existedAtCommit' => $this->getExistedAtCommit(),67'deletedAtCommit' => $this->getDeletedAtCommit(),68);69}7071public function getPathDicts() {72$paths = $this->getPaths();73if ($paths) {74return mpull($paths, 'toDictionary');75}76return array();77}7879/**80* Get the best README file in this result set, if one exists.81*82* Callers should normally use `diffusion.filecontentquery` to pull README83* content.84*85* @return string|null Full path to best README, or null if one does not86* exist.87*/88public function getReadmePath() {89$allowed_types = array(90ArcanistDiffChangeType::FILE_NORMAL => true,91ArcanistDiffChangeType::FILE_TEXT => true,92);9394$candidates = array();95foreach ($this->getPaths() as $path_object) {96if (empty($allowed_types[$path_object->getFileType()])) {97// Skip directories, images, etc.98continue;99}100101$local_path = $path_object->getPath();102if (!preg_match('/^readme(\.|$)/i', $local_path)) {103// Skip files not named "README".104continue;105}106107$full_path = $path_object->getFullPath();108$candidates[$full_path] = self::getReadmePriority($local_path);109}110111if (!$candidates) {112return null;113}114115arsort($candidates);116return head_key($candidates);117}118119/**120* Get the priority of a README file.121*122* When a directory contains several README files, this function scores them123* so the caller can select a preferred file. See @{method:getReadmePath}.124*125* @param string Local README path, like "README.txt".126* @return int Priority score, with higher being more preferred.127*/128public static function getReadmePriority($path) {129$path = phutil_utf8_strtolower($path);130if ($path == 'readme') {131return 90;132}133134$ext = last(explode('.', $path));135switch ($ext) {136case 'remarkup':137return 100;138case 'rainbow':139return 80;140case 'md':141return 70;142case 'txt':143return 60;144default:145return 50;146}147}148149public static function newFromConduit(array $data) {150$paths = array();151$path_dicts = $data['paths'];152foreach ($path_dicts as $dict) {153$paths[] = DiffusionRepositoryPath::newFromDictionary($dict);154}155return id(new DiffusionBrowseResultSet())156->setPaths($paths)157->setIsValidResults($data['isValidResults'])158->setReasonForEmptyResultSet($data['reasonForEmptyResultSet'])159->setExistedAtCommit($data['existedAtCommit'])160->setDeletedAtCommit($data['deletedAtCommit']);161}162}163164165