Path: blob/master/src/applications/diffusion/conduit/DiffusionQueryPathsConduitAPIMethod.php
12242 views
<?php12final class DiffusionQueryPathsConduitAPIMethod3extends DiffusionQueryConduitAPIMethod {45public function getAPIMethodName() {6return 'diffusion.querypaths';7}89public function getMethodDescription() {10return pht('Filename search on a repository.');11}1213protected function defineReturnType() {14return 'list<string>';15}1617protected function defineCustomParamTypes() {18return array(19'path' => 'required string',20'commit' => 'required string',21'pattern' => 'optional string',22'limit' => 'optional int',23'offset' => 'optional int',24);25}2627protected function getResult(ConduitAPIRequest $request) {28$results = parent::getResult($request);29$offset = $request->getValue('offset');30return array_slice($results, $offset);31}3233protected function getGitResult(ConduitAPIRequest $request) {34$drequest = $this->getDiffusionRequest();35$path = $drequest->getPath();36$commit = $request->getValue('commit');37$repository = $drequest->getRepository();3839// Recent versions of Git don't work if you pass the empty string, and40// require "." to list everything.41if (!strlen($path)) {42$path = '.';43}4445$future = $repository->getLocalCommandFuture(46'ls-tree --name-only -r -z %s -- %s',47gitsprintf('%s', $commit),48$path);4950$lines = id(new LinesOfALargeExecFuture($future))->setDelimiter("\0");51return $this->filterResults($lines, $request);52}5354protected function getMercurialResult(ConduitAPIRequest $request) {55$drequest = $this->getDiffusionRequest();56$repository = $drequest->getRepository();57$path = $request->getValue('path');58$commit = $request->getValue('commit');5960$entire_manifest = id(new DiffusionLowLevelMercurialPathsQuery())61->setRepository($repository)62->withCommit($commit)63->withPath($path)64->execute();6566$match_against = trim($path, '/');67$match_len = strlen($match_against);6869$lines = array();70foreach ($entire_manifest as $path) {71if (strlen($path) && !strncmp($path, $match_against, $match_len)) {72$lines[] = $path;73}74}7576return $this->filterResults($lines, $request);77}7879protected function filterResults($lines, ConduitAPIRequest $request) {80$pattern = $request->getValue('pattern');81$limit = (int)$request->getValue('limit');82$offset = (int)$request->getValue('offset');8384if (strlen($pattern)) {85// Add delimiters to the regex pattern.86$pattern = '('.$pattern.')';87}8889$results = array();90$count = 0;91foreach ($lines as $line) {92if (strlen($pattern) && !preg_match($pattern, $line)) {93continue;94}9596$results[] = $line;97$count++;9899if ($limit && ($count >= ($offset + $limit))) {100break;101}102}103104return $results;105}106107}108109110