public function processRequest()
 {
     $drequest = $this->diffusionRequest;
     $request = $this->getRequest();
     // Figure out if we're browsing a directory, a file, or a search result
     // list. Then delegate to the appropriate controller.
     $grep = $request->getStr('grep');
     $find = $request->getStr('find');
     if (strlen($grep) || strlen($find)) {
         $controller = new DiffusionBrowseSearchController($request);
     } else {
         $results = DiffusionBrowseResultSet::newFromConduit($this->callConduitWithDiffusionRequest('diffusion.browsequery', array('path' => $drequest->getPath(), 'commit' => $drequest->getStableCommit())));
         $reason = $results->getReasonForEmptyResultSet();
         $is_file = $reason == DiffusionBrowseResultSet::REASON_IS_FILE;
         if ($is_file) {
             $controller = new DiffusionBrowseFileController($request);
         } else {
             $controller = new DiffusionBrowseDirectoryController($request);
             $controller->setBrowseQueryResults($results);
         }
     }
     $controller->setDiffusionRequest($drequest);
     $controller->setCurrentApplication($this->getCurrentApplication());
     return $this->delegateToController($controller);
 }
 public function handleRequest(AphrontRequest $request)
 {
     $response = $this->loadDiffusionContext();
     if ($response) {
         return $response;
     }
     $viewer = $this->getViewer();
     $drequest = $this->getDiffusionRequest();
     $query_path = $request->getStr('q');
     if (preg_match('@/$@', $query_path)) {
         $query_dir = $query_path;
     } else {
         $query_dir = dirname($query_path) . '/';
     }
     $query_dir = ltrim($query_dir, '/');
     $browse_results = DiffusionBrowseResultSet::newFromConduit($this->callConduitWithDiffusionRequest('diffusion.browsequery', array('path' => $query_dir, 'commit' => $drequest->getCommit())));
     $paths = $browse_results->getPaths();
     $output = array();
     foreach ($paths as $path) {
         $full_path = $query_dir . $path->getPath();
         if ($path->getFileType() == DifferentialChangeType::FILE_DIRECTORY) {
             $full_path .= '/';
         }
         $output[] = array('/' . $full_path, null, substr(md5($full_path), 0, 7));
     }
     return id(new AphrontAjaxResponse())->setContent($output);
 }
 public function handleRequest(AphrontRequest $request)
 {
     $response = $this->loadDiffusionContext();
     if ($response) {
         return $response;
     }
     $drequest = $this->getDiffusionRequest();
     // Figure out if we're browsing a directory, a file, or a search result
     // list.
     $grep = $request->getStr('grep');
     $find = $request->getStr('find');
     if (strlen($grep) || strlen($find)) {
         return $this->browseSearch();
     }
     $pager = id(new PHUIPagerView())->readFromRequest($request);
     $results = DiffusionBrowseResultSet::newFromConduit($this->callConduitWithDiffusionRequest('diffusion.browsequery', array('path' => $drequest->getPath(), 'commit' => $drequest->getStableCommit(), 'offset' => $pager->getOffset(), 'limit' => $pager->getPageSize() + 1)));
     $reason = $results->getReasonForEmptyResultSet();
     $is_file = $reason == DiffusionBrowseResultSet::REASON_IS_FILE;
     if ($is_file) {
         return $this->browseFile($results);
     } else {
         $paths = $results->getPaths();
         $paths = $pager->sliceResults($paths);
         $results->setPaths($paths);
         return $this->browseDirectory($results, $pager);
     }
 }
 protected function processDiffusionRequest(AphrontRequest $request)
 {
     $repository_phid = $request->getStr('repositoryPHID');
     $repository = id(new PhabricatorRepositoryQuery())->setViewer($request->getUser())->withPHIDs(array($repository_phid))->executeOne();
     if (!$repository) {
         return new Aphront400Response();
     }
     $query_path = $request->getStr('q');
     if (preg_match('@/$@', $query_path)) {
         $query_dir = $query_path;
     } else {
         $query_dir = dirname($query_path) . '/';
     }
     $query_dir = ltrim($query_dir, '/');
     $drequest = DiffusionRequest::newFromDictionary(array('user' => $request->getUser(), 'repository' => $repository, 'path' => $query_dir));
     $this->setDiffusionRequest($drequest);
     $browse_results = DiffusionBrowseResultSet::newFromConduit($this->callConduitWithDiffusionRequest('diffusion.browsequery', array('path' => $drequest->getPath(), 'commit' => $drequest->getCommit())));
     $paths = $browse_results->getPaths();
     $output = array();
     foreach ($paths as $path) {
         $full_path = $query_dir . $path->getPath();
         if ($path->getFileType() == DifferentialChangeType::FILE_DIRECTORY) {
             $full_path .= '/';
         }
         $output[] = array('/' . $full_path, null, substr(md5($full_path), 0, 7));
     }
     return id(new AphrontAjaxResponse())->setContent($output);
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $repository_phid = $request->getStr('repositoryPHID');
     $repository = id(new PhabricatorRepositoryQuery())->setViewer($request->getUser())->withPHIDs(array($repository_phid))->executeOne();
     if (!$repository) {
         return new Aphront400Response();
     }
     $path = $request->getStr('path');
     $path = ltrim($path, '/');
     $drequest = DiffusionRequest::newFromDictionary(array('user' => $request->getUser(), 'repository' => $repository, 'path' => $path));
     $this->setDiffusionRequest($drequest);
     $browse_results = DiffusionBrowseResultSet::newFromConduit($this->callConduitWithDiffusionRequest('diffusion.browsequery', array('path' => $drequest->getPath(), 'commit' => $drequest->getCommit(), 'needValidityOnly' => true)));
     $valid = $browse_results->isValidResults();
     if (!$valid) {
         switch ($browse_results->getReasonForEmptyResultSet()) {
             case DiffusionBrowseResultSet::REASON_IS_FILE:
                 $valid = true;
                 break;
             case DiffusionBrowseResultSet::REASON_IS_EMPTY:
                 $valid = true;
                 break;
         }
     }
     $output = array('valid' => (bool) $valid);
     if (!$valid) {
         $branch = $drequest->getBranch();
         if ($branch) {
             $message = pht('Not found in %s', $branch);
         } else {
             $message = pht('Not found at HEAD');
         }
     } else {
         $message = pht('OK');
     }
     $output['message'] = $message;
     return id(new AphrontAjaxResponse())->setContent($output);
 }
 public function handleRequest(AphrontRequest $request)
 {
     $response = $this->loadDiffusionContext();
     if ($response) {
         return $response;
     }
     $viewer = $this->getViewer();
     $drequest = $this->getDiffusionRequest();
     $repository = $drequest->getRepository();
     $path = $request->getStr('path');
     $path = ltrim($path, '/');
     $browse_results = DiffusionBrowseResultSet::newFromConduit($this->callConduitWithDiffusionRequest('diffusion.browsequery', array('path' => $path, 'commit' => $drequest->getCommit(), 'needValidityOnly' => true)));
     $valid = $browse_results->isValidResults();
     if (!$valid) {
         switch ($browse_results->getReasonForEmptyResultSet()) {
             case DiffusionBrowseResultSet::REASON_IS_FILE:
                 $valid = true;
                 break;
             case DiffusionBrowseResultSet::REASON_IS_EMPTY:
                 $valid = true;
                 break;
         }
     }
     $output = array('valid' => (bool) $valid);
     if (!$valid) {
         $branch = $drequest->getBranch();
         if ($branch) {
             $message = pht('Not found in %s', $branch);
         } else {
             $message = pht('Not found at %s', 'HEAD');
         }
     } else {
         $message = pht('OK');
     }
     $output['message'] = $message;
     return id(new AphrontAjaxResponse())->setContent($output);
 }
 private function buildNormalContent(DiffusionRequest $drequest)
 {
     $repository = $drequest->getRepository();
     $phids = array();
     $content = array();
     try {
         $history_results = $this->callConduitWithDiffusionRequest('diffusion.historyquery', array('commit' => $drequest->getCommit(), 'path' => $drequest->getPath(), 'offset' => 0, 'limit' => 15));
         $history = DiffusionPathChange::newFromConduit($history_results['pathChanges']);
         foreach ($history as $item) {
             $data = $item->getCommitData();
             if ($data) {
                 if ($data->getCommitDetail('authorPHID')) {
                     $phids[$data->getCommitDetail('authorPHID')] = true;
                 }
                 if ($data->getCommitDetail('committerPHID')) {
                     $phids[$data->getCommitDetail('committerPHID')] = true;
                 }
             }
         }
         $history_exception = null;
     } catch (Exception $ex) {
         $history_results = null;
         $history = null;
         $history_exception = $ex;
     }
     try {
         $browse_results = DiffusionBrowseResultSet::newFromConduit($this->callConduitWithDiffusionRequest('diffusion.browsequery', array('path' => $drequest->getPath(), 'commit' => $drequest->getCommit())));
         $browse_paths = $browse_results->getPaths();
         foreach ($browse_paths as $item) {
             $data = $item->getLastCommitData();
             if ($data) {
                 if ($data->getCommitDetail('authorPHID')) {
                     $phids[$data->getCommitDetail('authorPHID')] = true;
                 }
                 if ($data->getCommitDetail('committerPHID')) {
                     $phids[$data->getCommitDetail('committerPHID')] = true;
                 }
             }
         }
         $browse_exception = null;
     } catch (Exception $ex) {
         $browse_results = null;
         $browse_paths = null;
         $browse_exception = $ex;
     }
     $phids = array_keys($phids);
     $handles = $this->loadViewerHandles($phids);
     $readme = null;
     if ($browse_results) {
         $readme_path = $browse_results->getReadmePath();
         if ($readme_path) {
             $readme_content = $this->callConduitWithDiffusionRequest('diffusion.filecontentquery', array('path' => $readme_path, 'commit' => $drequest->getStableCommit()));
             if ($readme_content) {
                 $readme = id(new DiffusionReadmeView())->setUser($this->getViewer())->setPath($readme_path)->setContent($readme_content['corpus']);
             }
         }
     }
     $content[] = $this->buildBrowseTable($browse_results, $browse_paths, $browse_exception, $handles);
     $content[] = $this->buildHistoryTable($history_results, $history, $history_exception);
     try {
         $content[] = $this->buildTagListTable($drequest);
     } catch (Exception $ex) {
         if (!$repository->isImporting()) {
             $content[] = $this->renderStatusMessage(pht('Unable to Load Tags'), $ex->getMessage());
         }
     }
     try {
         $content[] = $this->buildBranchListTable($drequest);
     } catch (Exception $ex) {
         if (!$repository->isImporting()) {
             $content[] = $this->renderStatusMessage(pht('Unable to Load Branches'), $ex->getMessage());
         }
     }
     if ($readme) {
         $content[] = $readme;
     }
     return $content;
 }
 private function buildNormalContent(DiffusionRequest $drequest)
 {
     $repository = $drequest->getRepository();
     $phids = array();
     $content = array();
     try {
         $history_results = $this->callConduitWithDiffusionRequest('diffusion.historyquery', array('commit' => $drequest->getCommit(), 'path' => $drequest->getPath(), 'offset' => 0, 'limit' => 15));
         $history = DiffusionPathChange::newFromConduit($history_results['pathChanges']);
         foreach ($history as $item) {
             $data = $item->getCommitData();
             if ($data) {
                 if ($data->getCommitDetail('authorPHID')) {
                     $phids[$data->getCommitDetail('authorPHID')] = true;
                 }
                 if ($data->getCommitDetail('committerPHID')) {
                     $phids[$data->getCommitDetail('committerPHID')] = true;
                 }
             }
         }
         $history_exception = null;
     } catch (Exception $ex) {
         $history_results = null;
         $history = null;
         $history_exception = $ex;
     }
     try {
         $browse_results = DiffusionBrowseResultSet::newFromConduit($this->callConduitWithDiffusionRequest('diffusion.browsequery', array('path' => $drequest->getPath(), 'commit' => $drequest->getCommit())));
         $browse_paths = $browse_results->getPaths();
         foreach ($browse_paths as $item) {
             $data = $item->getLastCommitData();
             if ($data) {
                 if ($data->getCommitDetail('authorPHID')) {
                     $phids[$data->getCommitDetail('authorPHID')] = true;
                 }
                 if ($data->getCommitDetail('committerPHID')) {
                     $phids[$data->getCommitDetail('committerPHID')] = true;
                 }
             }
         }
         $browse_exception = null;
     } catch (Exception $ex) {
         $browse_results = null;
         $browse_paths = null;
         $browse_exception = $ex;
     }
     $phids = array_keys($phids);
     $handles = $this->loadViewerHandles($phids);
     if ($browse_results) {
         $readme = $this->callConduitWithDiffusionRequest('diffusion.readmequery', array('paths' => $browse_results->getPathDicts(), 'commit' => $drequest->getStableCommit()));
     } else {
         $readme = null;
     }
     $content[] = $this->buildBrowseTable($browse_results, $browse_paths, $browse_exception, $handles);
     $content[] = $this->buildHistoryTable($history_results, $history, $history_exception, $handles);
     try {
         $content[] = $this->buildTagListTable($drequest);
     } catch (Exception $ex) {
         if (!$repository->isImporting()) {
             $content[] = $this->renderStatusMessage(pht('Unable to Load Tags'), $ex->getMessage());
         }
     }
     try {
         $content[] = $this->buildBranchListTable($drequest);
     } catch (Exception $ex) {
         if (!$repository->isImporting()) {
             $content[] = $this->renderStatusMessage(pht('Unable to Load Branches'), $ex->getMessage());
         }
     }
     if ($readme) {
         $box = new PHUIBoxView();
         $box->appendChild($readme);
         $box->addPadding(PHUI::PADDING_LARGE);
         $panel = new PHUIObjectBoxView();
         $panel->setHeaderText(pht('README'));
         $panel->appendChild($box);
         $content[] = $panel;
     }
     return $content;
 }
 public function save()
 {
     if ($this->getID()) {
         $is_new = false;
     } else {
         $is_new = true;
     }
     $this->openTransaction();
     $ret = parent::save();
     $add_owners = array();
     $remove_owners = array();
     $all_owners = array();
     if ($this->unsavedOwners) {
         $new_owners = array_fill_keys($this->unsavedOwners, true);
         $cur_owners = array();
         foreach ($this->loadOwners() as $owner) {
             if (empty($new_owners[$owner->getUserPHID()])) {
                 $remove_owners[$owner->getUserPHID()] = true;
                 $owner->delete();
                 continue;
             }
             $cur_owners[$owner->getUserPHID()] = true;
         }
         $add_owners = array_diff_key($new_owners, $cur_owners);
         $all_owners = array_merge(array($this->getPrimaryOwnerPHID() => true), $new_owners, $remove_owners);
         foreach ($add_owners as $phid => $ignored) {
             $owner = new PhabricatorOwnersOwner();
             $owner->setPackageID($this->getID());
             $owner->setUserPHID($phid);
             $owner->save();
         }
         unset($this->unsavedOwners);
     }
     $add_paths = array();
     $remove_paths = array();
     $touched_repos = array();
     if ($this->unsavedPaths) {
         $new_paths = igroup($this->unsavedPaths, 'repositoryPHID', 'path');
         $cur_paths = $this->loadPaths();
         foreach ($cur_paths as $key => $path) {
             $repository_phid = $path->getRepositoryPHID();
             $new_path = head(idx(idx($new_paths, $repository_phid, array()), $path->getPath(), array()));
             $excluded = $path->getExcluded();
             if (!$new_path || idx($new_path, 'excluded') != $excluded) {
                 $touched_repos[$repository_phid] = true;
                 $remove_paths[$repository_phid][$path->getPath()] = $excluded;
                 $path->delete();
                 unset($cur_paths[$key]);
             }
         }
         $cur_paths = mgroup($cur_paths, 'getRepositoryPHID', 'getPath');
         foreach ($new_paths as $repository_phid => $paths) {
             // TODO: (T603) Thread policy stuff in here.
             // get repository object for path validation
             $repository = id(new PhabricatorRepository())->loadOneWhere('phid = %s', $repository_phid);
             if (!$repository) {
                 continue;
             }
             foreach ($paths as $path => $dicts) {
                 $path = ltrim($path, '/');
                 // build query to validate path
                 $drequest = DiffusionRequest::newFromDictionary(array('user' => $this->getActor(), 'repository' => $repository, 'path' => $path));
                 $results = DiffusionBrowseResultSet::newFromConduit(DiffusionQuery::callConduitWithDiffusionRequest($this->getActor(), $drequest, 'diffusion.browsequery', array('commit' => $drequest->getCommit(), 'path' => $path, 'needValidityOnly' => true)));
                 $valid = $results->isValidResults();
                 $is_directory = true;
                 if (!$valid) {
                     switch ($results->getReasonForEmptyResultSet()) {
                         case DiffusionBrowseResultSet::REASON_IS_FILE:
                             $valid = true;
                             $is_directory = false;
                             break;
                         case DiffusionBrowseResultSet::REASON_IS_EMPTY:
                             $valid = true;
                             break;
                     }
                 }
                 if ($is_directory && substr($path, -1) != '/') {
                     $path .= '/';
                 }
                 if (substr($path, 0, 1) != '/') {
                     $path = '/' . $path;
                 }
                 if (empty($cur_paths[$repository_phid][$path]) && $valid) {
                     $touched_repos[$repository_phid] = true;
                     $excluded = idx(reset($dicts), 'excluded', 0);
                     $add_paths[$repository_phid][$path] = $excluded;
                     $obj = new PhabricatorOwnersPath();
                     $obj->setPackageID($this->getID());
                     $obj->setRepositoryPHID($repository_phid);
                     $obj->setPath($path);
                     $obj->setExcluded($excluded);
                     $obj->save();
                 }
             }
         }
         unset($this->unsavedPaths);
     }
     $this->saveTransaction();
     if ($is_new) {
         $mail = new PackageCreateMail($this);
     } else {
         $mail = new PackageModifyMail($this, array_keys($add_owners), array_keys($remove_owners), array_keys($all_owners), array_keys($touched_repos), $add_paths, $remove_paths);
     }
     $mail->setActor($this->getActor());
     $mail->send();
     return $ret;
 }