@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class DifferentialDiffCreateController extends DifferentialController {
4
5 public function handleRequest(AphrontRequest $request) {
6 $viewer = $this->getViewer();
7
8 // If we're on the "Update Diff" workflow, load the revision we're going
9 // to update.
10 $revision = null;
11 $revision_id = $request->getURIData('revisionID');
12 if ($revision_id) {
13 $revision = id(new DifferentialRevisionQuery())
14 ->setViewer($viewer)
15 ->withIDs(array($revision_id))
16 ->requireCapabilities(
17 array(
18 PhabricatorPolicyCapability::CAN_VIEW,
19 PhabricatorPolicyCapability::CAN_EDIT,
20 ))
21 ->executeOne();
22 if (!$revision) {
23 return new Aphront404Response();
24 }
25 }
26
27 $diff = null;
28 // This object is just for policy stuff
29 $diff_object = DifferentialDiff::initializeNewDiff($viewer);
30
31 if ($revision) {
32 $repository_phid = $revision->getRepositoryPHID();
33 } else {
34 $repository_phid = null;
35 }
36
37 $errors = array();
38 $e_diff = null;
39 $e_file = null;
40 $validation_exception = null;
41 if ($request->isFormPost()) {
42
43 $repository_tokenizer = $request->getArr(
44 id(new DifferentialRepositoryField())->getFieldKey());
45 if ($repository_tokenizer) {
46 $repository_phid = reset($repository_tokenizer);
47 }
48
49 if ($request->getFileExists('diff-file')) {
50 $diff = PhabricatorFile::readUploadedFileData($_FILES['diff-file']);
51 } else {
52 $diff = $request->getStr('diff');
53 }
54
55 if (!strlen($diff)) {
56 $errors[] = pht(
57 'You can not create an empty diff. Paste a diff or upload a '.
58 'file containing a diff.');
59 $e_diff = pht('Required');
60 $e_file = pht('Required');
61 }
62
63 if (!$errors) {
64 try {
65 $call = new ConduitCall(
66 'differential.createrawdiff',
67 array(
68 'diff' => $diff,
69 'repositoryPHID' => $repository_phid,
70 'viewPolicy' => $request->getStr('viewPolicy'),
71 ));
72 $call->setUser($viewer);
73 $result = $call->execute();
74
75 $diff_id = $result['id'];
76
77 $uri = $this->getApplicationURI("diff/{$diff_id}/");
78 $uri = new PhutilURI($uri);
79 if ($revision) {
80 $uri->replaceQueryParam('revisionID', $revision->getID());
81 }
82
83 return id(new AphrontRedirectResponse())->setURI($uri);
84 } catch (PhabricatorApplicationTransactionValidationException $ex) {
85 $validation_exception = $ex;
86 }
87 }
88 }
89
90 $form = new AphrontFormView();
91 $arcanist_href = PhabricatorEnv::getDoclink('Arcanist User Guide');
92 $arcanist_link = phutil_tag(
93 'a',
94 array(
95 'href' => $arcanist_href,
96 'target' => '_blank',
97 ),
98 pht('Learn More'));
99
100 $cancel_uri = $this->getApplicationURI();
101
102 $policies = id(new PhabricatorPolicyQuery())
103 ->setViewer($viewer)
104 ->setObject($diff_object)
105 ->execute();
106
107 $info_view = null;
108 if (!$request->isFormPost()) {
109 $info_view = id(new PHUIInfoView())
110 ->setSeverity(PHUIInfoView::SEVERITY_NOTICE)
111 ->setErrors(
112 array(
113 array(
114 pht(
115 'The best way to create a diff is to use the %s '.
116 'command-line tool.',
117 PlatformSymbols::getPlatformClientName()),
118 ' ',
119 $arcanist_link,
120 ),
121 pht(
122 'You can also paste a diff above, or upload a file '.
123 'containing a diff (for example, from %s, %s or %s).',
124 phutil_tag('tt', array(), 'svn diff'),
125 phutil_tag('tt', array(), 'git diff'),
126 phutil_tag('tt', array(), 'hg diff --git')),
127 ));
128 }
129
130 if ($revision) {
131 $title = pht('Update Diff');
132 $header = pht('Update Diff');
133 $button = pht('Continue');
134 $header_icon = 'fa-upload';
135 } else {
136 $title = pht('Create Diff');
137 $header = pht('Create New Diff');
138 $button = pht('Create Diff');
139 $header_icon = 'fa-plus-square';
140 }
141
142 $form
143 ->setEncType('multipart/form-data')
144 ->setViewer($viewer);
145
146 if ($revision) {
147 $form->appendChild(
148 id(new AphrontFormMarkupControl())
149 ->setLabel(pht('Updating Revision'))
150 ->setValue($viewer->renderHandle($revision->getPHID())));
151 }
152
153 if ($repository_phid) {
154 $repository_value = array($repository_phid);
155 } else {
156 $repository_value = array();
157 }
158
159 $form
160 ->appendChild(
161 id(new AphrontFormTextAreaControl())
162 ->setLabel(pht('Raw Diff'))
163 ->setName('diff')
164 ->setValue($diff)
165 ->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
166 ->setError($e_diff))
167 ->appendChild(
168 id(new AphrontFormFileControl())
169 ->setLabel(pht('Raw Diff From File'))
170 ->setName('diff-file')
171 ->setError($e_file))
172 ->appendControl(
173 id(new AphrontFormTokenizerControl())
174 ->setName(id(new DifferentialRepositoryField())->getFieldKey())
175 ->setLabel(pht('Repository'))
176 ->setDatasource(new DiffusionRepositoryDatasource())
177 ->setValue($repository_value)
178 ->setLimit(1))
179 ->appendChild(
180 id(new AphrontFormPolicyControl())
181 ->setViewer($viewer)
182 ->setName('viewPolicy')
183 ->setPolicyObject($diff_object)
184 ->setPolicies($policies)
185 ->setCapability(PhabricatorPolicyCapability::CAN_VIEW))
186 ->appendChild(
187 id(new AphrontFormSubmitControl())
188 ->addCancelButton($cancel_uri)
189 ->setValue($button));
190
191 $form_box = id(new PHUIObjectBoxView())
192 ->setHeaderText($title)
193 ->setValidationException($validation_exception)
194 ->setForm($form)
195 ->setBackground(PHUIObjectBoxView::WHITE_CONFIG)
196 ->setFormErrors($errors);
197
198 $crumbs = $this->buildApplicationCrumbs();
199 if ($revision) {
200 $crumbs->addTextCrumb(
201 $revision->getMonogram(),
202 '/'.$revision->getMonogram());
203 }
204 $crumbs->addTextCrumb($title);
205 $crumbs->setBorder(true);
206
207 $view = id(new PHUITwoColumnView())
208 ->setFooter(array(
209 $form_box,
210 $info_view,
211 ));
212
213 return $this->newPage()
214 ->setTitle($title)
215 ->setCrumbs($crumbs)
216 ->appendChild($view);
217 }
218
219}