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);
 }
 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 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 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;
 }
 public function browseDirectory(DiffusionBrowseResultSet $results, PHUIPagerView $pager)
 {
     $request = $this->getRequest();
     $drequest = $this->getDiffusionRequest();
     $repository = $drequest->getRepository();
     $reason = $results->getReasonForEmptyResultSet();
     $content = array();
     $actions = $this->buildActionView($drequest);
     $properties = $this->buildPropertyView($drequest, $actions);
     $object_box = id(new PHUIObjectBoxView())->setHeader($this->buildHeaderView($drequest))->addPropertyList($properties);
     $content[] = $object_box;
     $content[] = $this->renderSearchForm($collapsed = true);
     if (!$results->isValidResults()) {
         $empty_result = new DiffusionEmptyResultView();
         $empty_result->setDiffusionRequest($drequest);
         $empty_result->setDiffusionBrowseResultSet($results);
         $empty_result->setView($request->getStr('view'));
         $content[] = $empty_result;
     } else {
         $phids = array();
         foreach ($results->getPaths() as $result) {
             $data = $result->getLastCommitData();
             if ($data) {
                 if ($data->getCommitDetail('authorPHID')) {
                     $phids[$data->getCommitDetail('authorPHID')] = true;
                 }
             }
         }
         $phids = array_keys($phids);
         $handles = $this->loadViewerHandles($phids);
         $browse_table = new DiffusionBrowseTableView();
         $browse_table->setDiffusionRequest($drequest);
         $browse_table->setHandles($handles);
         $browse_table->setPaths($results->getPaths());
         $browse_table->setUser($request->getUser());
         $browse_panel = new PHUIObjectBoxView();
         $browse_panel->setHeaderText($drequest->getPath(), '/');
         $browse_panel->setTable($browse_table);
         $content[] = $browse_panel;
     }
     $content[] = $this->buildOpenRevisions();
     $content[] = $this->renderDirectoryReadme($results);
     $crumbs = $this->buildCrumbs(array('branch' => true, 'path' => true, 'view' => 'browse'));
     $pager_box = $this->renderTablePagerBox($pager);
     return $this->newPage()->setTitle(array(nonempty(basename($drequest->getPath()), '/'), $repository->getDisplayName()))->setCrumbs($crumbs)->appendChild(array($content, $pager_box));
 }
 protected function renderDirectoryReadme(DiffusionBrowseResultSet $browse)
 {
     $readme_path = $browse->getReadmePath();
     if ($readme_path === null) {
         return null;
     }
     $drequest = $this->getDiffusionRequest();
     $viewer = $this->getViewer();
     try {
         $result = $this->callConduitWithDiffusionRequest('diffusion.filecontentquery', array('path' => $readme_path, 'commit' => $drequest->getStableCommit()));
     } catch (Exception $ex) {
         return null;
     }
     $file_phid = $result['filePHID'];
     if (!$file_phid) {
         return null;
     }
     $file = id(new PhabricatorFileQuery())->setViewer($viewer)->withPHIDs(array($file_phid))->executeOne();
     if (!$file) {
         return null;
     }
     $corpus = $file->loadFileData();
     if (!strlen($corpus)) {
         return null;
     }
     return id(new DiffusionReadmeView())->setUser($this->getViewer())->setPath($readme_path)->setContent($corpus);
 }
 public function browseDirectory(DiffusionBrowseResultSet $results, PHUIPagerView $pager)
 {
     $request = $this->getRequest();
     $drequest = $this->getDiffusionRequest();
     $repository = $drequest->getRepository();
     $reason = $results->getReasonForEmptyResultSet();
     $curtain = $this->buildCurtain($drequest);
     $details = $this->buildPropertyView($drequest);
     $header = $this->buildHeaderView($drequest);
     $header->setHeaderIcon('fa-folder-open');
     $search_form = $this->renderSearchForm();
     $empty_result = null;
     $browse_panel = null;
     if (!$results->isValidResults()) {
         $empty_result = new DiffusionEmptyResultView();
         $empty_result->setDiffusionRequest($drequest);
         $empty_result->setDiffusionBrowseResultSet($results);
         $empty_result->setView($request->getStr('view'));
     } else {
         $phids = array();
         foreach ($results->getPaths() as $result) {
             $data = $result->getLastCommitData();
             if ($data) {
                 if ($data->getCommitDetail('authorPHID')) {
                     $phids[$data->getCommitDetail('authorPHID')] = true;
                 }
             }
         }
         $phids = array_keys($phids);
         $handles = $this->loadViewerHandles($phids);
         $browse_table = id(new DiffusionBrowseTableView())->setDiffusionRequest($drequest)->setHandles($handles)->setPaths($results->getPaths())->setUser($request->getUser());
         $browse_header = id(new PHUIHeaderView())->setHeader(nonempty(basename($drequest->getPath()), '/'))->setHeaderIcon('fa-folder-open');
         $browse_panel = id(new PHUIObjectBoxView())->setHeader($browse_header)->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)->setTable($browse_table);
         $browse_panel->setShowHide(array(pht('Show Search')), pht('Hide Search'), $search_form, '#');
     }
     $open_revisions = $this->buildOpenRevisions();
     $readme = $this->renderDirectoryReadme($results);
     $crumbs = $this->buildCrumbs(array('branch' => true, 'path' => true, 'view' => 'browse'));
     $pager_box = $this->renderTablePagerBox($pager);
     $crumbs->setBorder(true);
     $view = id(new PHUITwoColumnView())->setHeader($header)->setCurtain($curtain)->setMainColumn(array($empty_result, $browse_panel))->setFooter(array($open_revisions, $readme, $pager_box));
     if ($details) {
         $view->addPropertySection(pht('Details'), $details);
     }
     return $this->newPage()->setTitle(array(nonempty(basename($drequest->getPath()), '/'), $repository->getDisplayName()))->setCrumbs($crumbs)->appendChild(array($view));
 }
 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;
 }