Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / diffusion / controller / DiffusionLastModifiedController.php
blob92f5b50a8a958be2b4d0fca1f28ed416b614b75f
1 <?php
3 final class DiffusionLastModifiedController extends DiffusionController {
5 public function shouldAllowPublic() {
6 return true;
9 public function handleRequest(AphrontRequest $request) {
10 $response = $this->loadDiffusionContext();
11 if ($response) {
12 return $response;
15 $viewer = $this->getViewer();
16 $drequest = $this->getDiffusionRequest();
18 $paths = $request->getStr('paths');
20 try {
21 $paths = phutil_json_decode($paths);
22 } catch (PhutilJSONParserException $ex) {
23 return new Aphront400Response();
26 $modified_map = $this->callConduitWithDiffusionRequest(
27 'diffusion.lastmodifiedquery',
28 array(
29 'paths' => array_fill_keys($paths, $drequest->getCommit()),
30 ));
32 if ($modified_map) {
33 $commit_map = id(new DiffusionCommitQuery())
34 ->setViewer($viewer)
35 ->withRepository($drequest->getRepository())
36 ->withIdentifiers(array_values($modified_map))
37 ->needCommitData(true)
38 ->needIdentities(true)
39 ->execute();
40 $commit_map = mpull($commit_map, null, 'getCommitIdentifier');
41 } else {
42 $commit_map = array();
45 $commits = array();
46 foreach ($paths as $path) {
47 $modified_at = idx($modified_map, $path);
48 if ($modified_at) {
49 $commit = idx($commit_map, $modified_at);
50 if ($commit) {
51 $commits[$path] = $commit;
56 $branch = $drequest->loadBranch();
57 if ($branch && $commits) {
58 $lint_query = id(new DiffusionLintCountQuery())
59 ->withBranchIDs(array($branch->getID()))
60 ->withPaths(array_keys($commits));
62 if ($drequest->getLint()) {
63 $lint_query->withCodes(array($drequest->getLint()));
66 $lint = $lint_query->execute();
67 } else {
68 $lint = array();
71 $output = array();
72 foreach ($commits as $path => $commit) {
73 $prequest = clone $drequest;
74 $prequest->setPath($path);
76 $output[$path] = $this->renderColumns(
77 $prequest,
78 $commit,
79 idx($lint, $path));
82 return id(new AphrontAjaxResponse())->setContent($output);
85 private function renderColumns(
86 DiffusionRequest $drequest,
87 PhabricatorRepositoryCommit $commit = null,
88 $lint = null) {
89 $viewer = $this->getViewer();
91 if ($commit) {
92 $epoch = $commit->getEpoch();
93 $modified = DiffusionView::linkCommit(
94 $drequest->getRepository(),
95 $commit->getCommitIdentifier());
96 $date = $viewer->formatShortDateTime($epoch);
97 } else {
98 $modified = '';
99 $date = '';
102 $data = $commit->getCommitData();
103 $details = DiffusionView::linkDetail(
104 $drequest->getRepository(),
105 $commit->getCommitIdentifier(),
106 $data->getSummary());
107 $details = AphrontTableView::renderSingleDisplayLine($details);
109 $return = array(
110 'commit' => $modified,
111 'date' => $date,
112 'details' => $details,
115 if ($lint !== null) {
116 $return['lint'] = phutil_tag(
117 'a',
118 array(
119 'href' => $drequest->generateURI(array(
120 'action' => 'lint',
121 'lint' => null,
124 number_format($lint));
127 // The client treats these results as markup, so make sure they have been
128 // escaped correctly.
129 foreach ($return as $key => $value) {
130 $return[$key] = hsprintf('%s', $value);
133 return $return;