public function processRequest()
 {
     $request = $this->getRequest();
     $repository_phid = $request->getStr('repositoryPHID');
     $repository = id(new PhabricatorRepository())->loadOneWhere('phid = %s', $repository_phid);
     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('repository' => $repository, 'path' => $query_dir));
     $browse_query = DiffusionBrowseQuery::newFromDiffusionRequest($drequest);
     $paths = $browse_query->loadPaths();
     $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 PhabricatorRepository())->loadOneWhere('phid = %s', $repository_phid);
     if (!$repository) {
         return new Aphront400Response();
     }
     $path = $request->getStr('path');
     $path = ltrim($path, '/');
     $drequest = DiffusionRequest::newFromDictionary(array('repository' => $repository, 'path' => $path));
     $browse_query = DiffusionBrowseQuery::newFromDiffusionRequest($drequest);
     $browse_query->needValidityOnly(true);
     $valid = $browse_query->loadPaths();
     if (!$valid) {
         switch ($browse_query->getReasonForEmptyResultSet()) {
             case DiffusionBrowseQuery::REASON_IS_FILE:
                 $valid = true;
                 break;
             case DiffusionBrowseQuery::REASON_IS_EMPTY:
                 $valid = true;
                 break;
         }
     }
     $output = array('valid' => (bool) $valid);
     if (!$valid) {
         $branch = $drequest->getBranch();
         if ($branch) {
             $message = 'Not found in ' . $branch;
         } else {
             $message = 'Not found at HEAD';
         }
     } else {
         $message = 'OK';
     }
     $output['message'] = $message;
     return id(new AphrontAjaxResponse())->setContent($output);
 }
 public function processRequest()
 {
     $drequest = $this->diffusionRequest;
     $content = array();
     $crumbs = $this->buildCrumbs();
     $content[] = $crumbs;
     $content[] = $this->buildPropertiesTable($drequest->getRepository());
     $history_query = DiffusionHistoryQuery::newFromDiffusionRequest($drequest);
     $history_query->setLimit(15);
     $history_query->needParents(true);
     $history = $history_query->loadHistory();
     $browse_query = DiffusionBrowseQuery::newFromDiffusionRequest($drequest);
     $browse_results = $browse_query->loadPaths();
     $phids = array();
     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;
             }
         }
     }
     foreach ($browse_results 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;
             }
         }
     }
     $phids = array_keys($phids);
     $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles();
     $history_table = new DiffusionHistoryTableView();
     $history_table->setDiffusionRequest($drequest);
     $history_table->setHandles($handles);
     $history_table->setHistory($history);
     $history_table->loadRevisions();
     $history_table->setParents($history_query->getParents());
     $history_table->setIsHead(true);
     $callsign = $drequest->getRepository()->getCallsign();
     $all = phutil_render_tag('a', array('href' => "/diffusion/{$callsign}/history/"), 'View Full Commit History');
     $panel = new AphrontPanelView();
     $panel->setHeader("Recent Commits · {$all}");
     $panel->appendChild($history_table);
     $content[] = $panel;
     $browse_table = new DiffusionBrowseTableView();
     $browse_table->setDiffusionRequest($drequest);
     $browse_table->setHandles($handles);
     $browse_table->setPaths($browse_results);
     $browse_panel = new AphrontPanelView();
     $browse_panel->setHeader('Browse Repository');
     $browse_panel->appendChild($browse_table);
     $content[] = $browse_panel;
     $content[] = $this->buildTagListTable($drequest);
     $content[] = $this->buildBranchListTable($drequest);
     $readme = $browse_query->renderReadme($browse_results);
     if ($readme) {
         $panel = new AphrontPanelView();
         $panel->setHeader('README');
         $panel->appendChild($readme);
         $content[] = $panel;
     }
     return $this->buildStandardPageResponse($content, array('title' => $drequest->getRepository()->getName()));
 }
 public function processRequest()
 {
     $drequest = $this->diffusionRequest;
     $browse_query = DiffusionBrowseQuery::newFromDiffusionRequest($drequest);
     $results = $browse_query->loadPaths();
     $content = array();
     $content[] = $this->buildCrumbs(array('branch' => true, 'path' => true, 'view' => 'browse'));
     if (!$results) {
         if ($browse_query->getReasonForEmptyResultSet() == DiffusionBrowseQuery::REASON_IS_FILE) {
             $controller = new DiffusionBrowseFileController($this->getRequest());
             $controller->setDiffusionRequest($drequest);
             return $this->delegateToController($controller);
         }
         $empty_result = new DiffusionEmptyResultView();
         $empty_result->setDiffusionRequest($drequest);
         $empty_result->setBrowseQuery($browse_query);
         $empty_result->setView($this->getRequest()->getStr('view'));
         $content[] = $empty_result;
     } else {
         $readme = null;
         $phids = array();
         foreach ($results as $result) {
             $data = $result->getLastCommitData();
             if ($data) {
                 if ($data->getCommitDetail('authorPHID')) {
                     $phids[$data->getCommitDetail('authorPHID')] = true;
                 }
             }
             $path = $result->getPath();
             if (preg_match('/^readme(|\\.txt|\\.remarkup)$/i', $path)) {
                 $readme = $result;
             }
         }
         $phids = array_keys($phids);
         $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles();
         if ($readme) {
             $readme_request = DiffusionRequest::newFromDictionary(array('repository' => $drequest->getRepository(), 'commit' => $drequest->getStableCommitName(), 'path' => $readme->getFullPath()));
             $content_query = DiffusionFileContentQuery::newFromDiffusionRequest($readme_request);
             $content_query->loadFileContent();
             $readme_content = $content_query->getRawData();
             if (preg_match('/.txt$/', $readme->getPath())) {
                 $readme_content = phutil_escape_html($readme_content);
                 $readme_content = nl2br($readme_content);
             } else {
                 // Markup extensionless files as remarkup so we get links and such.
                 $engine = PhabricatorMarkupEngine::newDiffusionMarkupEngine();
                 $readme_content = $engine->markupText($readme_content);
                 $readme_content = phutil_render_tag('div', array('class' => 'phabricator-remarkup'), $readme_content);
             }
             $readme_panel = new AphrontPanelView();
             $readme_panel->setHeader('README');
             $readme_panel->appendChild($readme_content);
             $content[] = $readme_panel;
         }
         $browse_table = new DiffusionBrowseTableView();
         $browse_table->setDiffusionRequest($drequest);
         $browse_table->setHandles($handles);
         $browse_table->setPaths($results);
         $browse_panel = new AphrontPanelView();
         $browse_panel->appendChild($browse_table);
         $content[] = $browse_panel;
     }
     $content[] = $this->buildOpenRevisions();
     $nav = $this->buildSideNav('browse', false);
     $nav->appendChild($content);
     return $this->buildStandardPageResponse($nav, array('title' => basename($drequest->getPath())));
 }
 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) {
             if (empty($new_paths[$path->getRepositoryPHID()][$path->getPath()])) {
                 $touched_repos[$path->getRepositoryPHID()] = true;
                 $remove_paths[$path->getRepositoryPHID()][$path->getPath()] = true;
                 $path->delete();
                 unset($cur_paths[$key]);
             }
         }
         $cur_paths = mgroup($cur_paths, 'getRepositoryPHID', 'getPath');
         foreach ($new_paths as $repository_phid => $paths) {
             // get repository object for path validation
             $repository = id(new PhabricatorRepository())->loadOneWhere('phid = %s', $repository_phid);
             if (!$repository) {
                 continue;
             }
             foreach ($paths as $path => $ignored) {
                 $path = ltrim($path, '/');
                 // build query to validate path
                 $drequest = DiffusionRequest::newFromDictionary(array('repository' => $repository, 'path' => $path));
                 $query = DiffusionBrowseQuery::newFromDiffusionRequest($drequest);
                 $query->needValidityOnly(true);
                 $valid = $query->loadPaths();
                 $is_directory = true;
                 if (!$valid) {
                     switch ($query->getReasonForEmptyResultSet()) {
                         case DiffusionBrowseQuery::REASON_IS_FILE:
                             $valid = true;
                             $is_directory = false;
                             break;
                         case DiffusionBrowseQuery::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;
                     $add_paths[$repository_phid][$path] = true;
                     $obj = new PhabricatorOwnersPath();
                     $obj->setPackageID($this->getID());
                     $obj->setRepositoryPHID($repository_phid);
                     $obj->setPath($path);
                     $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->send();
     return $ret;
 }
 public function processRequest()
 {
     $drequest = $this->diffusionRequest;
     $browse_query = DiffusionBrowseQuery::newFromDiffusionRequest($drequest);
     $results = $browse_query->loadPaths();
     $content = array();
     $content[] = $this->buildCrumbs(array('branch' => true, 'path' => true, 'view' => 'browse'));
     if ($drequest->getTagContent()) {
         $title = 'Tag: ' . $drequest->getSymbolicCommit();
         $tag_view = new AphrontPanelView();
         $tag_view->setHeader(phutil_escape_html($title));
         $tag_view->appendChild($this->markupText($drequest->getTagContent()));
         $content[] = $tag_view;
     }
     if (!$results) {
         if ($browse_query->getReasonForEmptyResultSet() == DiffusionBrowseQuery::REASON_IS_FILE) {
             $controller = new DiffusionBrowseFileController($this->getRequest());
             $controller->setDiffusionRequest($drequest);
             return $this->delegateToController($controller);
         }
         $empty_result = new DiffusionEmptyResultView();
         $empty_result->setDiffusionRequest($drequest);
         $empty_result->setBrowseQuery($browse_query);
         $empty_result->setView($this->getRequest()->getStr('view'));
         $content[] = $empty_result;
     } else {
         $readme = null;
         $phids = array();
         foreach ($results as $result) {
             $data = $result->getLastCommitData();
             if ($data) {
                 if ($data->getCommitDetail('authorPHID')) {
                     $phids[$data->getCommitDetail('authorPHID')] = true;
                 }
             }
             $path = $result->getPath();
             if (preg_match('/^readme(|\\.txt|\\.remarkup)$/i', $path)) {
                 $readme = $result;
             }
         }
         $phids = array_keys($phids);
         $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles();
         $browse_table = new DiffusionBrowseTableView();
         $browse_table->setDiffusionRequest($drequest);
         $browse_table->setHandles($handles);
         $browse_table->setPaths($results);
         $browse_table->setUser($this->getRequest()->getUser());
         $browse_panel = new AphrontPanelView();
         $browse_panel->appendChild($browse_table);
         $content[] = $browse_panel;
     }
     $content[] = $this->buildOpenRevisions();
     $readme_content = $browse_query->renderReadme($results);
     if ($readme_content) {
         $readme_panel = new AphrontPanelView();
         $readme_panel->setHeader('README');
         $readme_panel->appendChild($readme_content);
         $content[] = $readme_panel;
     }
     $nav = $this->buildSideNav('browse', false);
     $nav->appendChild($content);
     return $this->buildStandardPageResponse($nav, array('title' => array(nonempty(basename($drequest->getPath()), '/'), $drequest->getRepository()->getCallsign() . ' Repository')));
 }
 public function processRequest()
 {
     $drequest = $this->diffusionRequest;
     $browse_query = DiffusionBrowseQuery::newFromDiffusionRequest($drequest);
     $results = $browse_query->loadPaths();
     $content = array();
     $content[] = $this->buildCrumbs(array('branch' => true, 'path' => true, 'view' => 'browse'));
     if (!$results) {
         // TODO: Format all these commits into nice VCS-agnostic links, and
         // below.
         $commit = $drequest->getCommit();
         $callsign = $drequest->getRepository()->getCallsign();
         if ($commit) {
             $commit = "r{$callsign}{$commit}";
         } else {
             $commit = 'HEAD';
         }
         switch ($browse_query->getReasonForEmptyResultSet()) {
             case DiffusionBrowseQuery::REASON_IS_NONEXISTENT:
                 $title = 'Path Does Not Exist';
                 // TODO: Under git, this error message should be more specific. It
                 // may exist on some other branch.
                 $body = "This path does not exist anywhere.";
                 $severity = AphrontErrorView::SEVERITY_ERROR;
                 break;
             case DiffusionBrowseQuery::REASON_IS_EMPTY:
                 $title = 'Empty Directory';
                 $body = "This path was an empty directory at {$commit}.\n";
                 $severity = AphrontErrorView::SEVERITY_NOTICE;
                 break;
             case DiffusionBrowseQuery::REASON_IS_DELETED:
                 $deleted = $browse_query->getDeletedAtCommit();
                 $existed = $browse_query->getExistedAtCommit();
                 $deleted = "r{$callsign}{$deleted}";
                 $existed = "r{$callsign}{$existed}";
                 $title = 'Path Was Deleted';
                 $body = "This path does not exist at {$commit}. It was deleted in " . "{$deleted} and last existed at {$existed}.";
                 $severity = AphrontErrorView::SEVERITY_WARNING;
                 break;
             case DiffusionBrowseQuery::REASON_IS_FILE:
                 $controller = new DiffusionBrowseFileController($this->getRequest());
                 $controller->setDiffusionRequest($drequest);
                 return $this->delegateToController($controller);
                 break;
             case DiffusionBrowseQuery::REASON_IS_UNTRACKED_PARENT:
                 $subdir = $drequest->getRepository()->getDetail('svn-subpath');
                 $title = 'Directory Not Tracked';
                 $body = "This repository is configured to track only one subdirectory " . "of the entire repository ('" . phutil_escape_html($subdir) . "'), " . "but you aren't looking at something in that subdirectory, so no " . "information is available.";
                 $severity = AphrontErrorView::SEVERITY_WARNING;
                 break;
             default:
                 throw new Exception("Unknown failure reason!");
         }
         $error_view = new AphrontErrorView();
         $error_view->setSeverity($severity);
         $error_view->setTitle($title);
         $error_view->appendChild('<p>' . $body . '</p>');
         $content[] = $error_view;
     } else {
         $phids = array();
         foreach ($results as $result) {
             $data = $result->getLastCommitData();
             if ($data) {
                 if ($data->getCommitDetail('authorPHID')) {
                     $phids[$data->getCommitDetail('authorPHID')] = true;
                 }
             }
         }
         $phids = array_keys($phids);
         $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles();
         $browse_table = new DiffusionBrowseTableView();
         $browse_table->setDiffusionRequest($drequest);
         $browse_table->setHandles($handles);
         $browse_table->setPaths($results);
         $browse_panel = new AphrontPanelView();
         $browse_panel->appendChild($browse_table);
         $content[] = $browse_panel;
     }
     $content[] = $this->buildOpenRevisions();
     $nav = $this->buildSideNav('browse', false);
     $nav->appendChild($content);
     return $this->buildStandardPageResponse($nav, array('title' => basename($drequest->getPath())));
 }