Remove product literal strings in "pht()", part 6
[phabricator.git] / src / applications / differential / render / DifferentialChangesetOneUpRenderer.php
blobbcdbf4dd465aa439f673b610eacfa37983da055a
1 <?php
3 final class DifferentialChangesetOneUpRenderer
4 extends DifferentialChangesetHTMLRenderer {
6 private $simpleMode;
8 public function setSimpleMode($simple_mode) {
9 $this->simpleMode = $simple_mode;
10 return $this;
13 public function getSimpleMode() {
14 return $this->simpleMode;
17 public function isOneUpRenderer() {
18 return true;
21 protected function getRendererTableClass() {
22 return 'diff-1up';
25 public function getRendererKey() {
26 return '1up';
29 protected function renderColgroup() {
30 return phutil_tag('colgroup', array(), array(
31 phutil_tag('col', array('class' => 'num')),
32 phutil_tag('col', array('class' => 'num')),
33 phutil_tag('col', array('class' => 'copy')),
34 phutil_tag('col', array('class' => 'unified')),
35 ));
38 public function renderTextChange(
39 $range_start,
40 $range_len,
41 $rows) {
43 $primitives = $this->buildPrimitives($range_start, $range_len);
44 return $this->renderPrimitives($primitives, $rows);
47 protected function renderPrimitives(array $primitives, $rows) {
48 list($left_prefix, $right_prefix) = $this->getLineIDPrefixes();
50 $is_simple = $this->getSimpleMode();
52 $no_copy = phutil_tag('td', array('class' => 'copy'));
53 $no_coverage = null;
55 $column_width = 4;
57 $aural_minus = javelin_tag(
58 'span',
59 array(
60 'aural' => true,
61 'data-aural' => true,
63 '- ');
65 $aural_plus = javelin_tag(
66 'span',
67 array(
68 'aural' => true,
69 'data-aural' => true,
71 '+ ');
73 $out = array();
74 foreach ($primitives as $k => $p) {
75 $type = $p['type'];
76 switch ($type) {
77 case 'old':
78 case 'new':
79 case 'old-file':
80 case 'new-file':
81 $is_old = ($type == 'old' || $type == 'old-file');
83 $cells = array();
84 if ($is_old) {
85 if ($p['htype']) {
86 if ($p['htype'] === '\\') {
87 $class = 'comment';
88 } else if (empty($p['oline'])) {
89 $class = 'left old old-full';
90 } else {
91 $class = 'left old';
93 $aural = $aural_minus;
94 } else {
95 $class = 'left';
96 $aural = null;
99 if ($type == 'old-file') {
100 $class = "{$class} differential-old-image";
103 if ($left_prefix) {
104 $left_id = $left_prefix.$p['line'];
105 } else {
106 $left_id = null;
109 $line = $p['line'];
111 $cells[] = phutil_tag(
112 'td',
113 array(
114 'id' => $left_id,
115 'class' => $class.' n',
116 'data-n' => $line,
119 $render = $p['render'];
120 if ($aural !== null) {
121 $render = array($aural, $render);
124 $cells[] = phutil_tag(
125 'td',
126 array(
127 'class' => $class.' n',
129 $cells[] = $no_copy;
130 $cells[] = phutil_tag('td', array('class' => $class), $render);
131 $cells[] = $no_coverage;
132 } else {
133 if ($p['htype']) {
134 if ($p['htype'] === '\\') {
135 $class = 'comment';
136 } else if (empty($p['oline'])) {
137 $class = 'right new new-full';
138 } else {
139 $class = 'right new';
141 $cells[] = phutil_tag(
142 'td',
143 array(
144 'class' => $class.' n',
146 $aural = $aural_plus;
147 } else {
148 $class = 'right';
149 if ($left_prefix) {
150 $left_id = $left_prefix.$p['oline'];
151 } else {
152 $left_id = null;
155 $oline = $p['oline'];
157 $cells[] = phutil_tag(
158 'td',
159 array(
160 'id' => $left_id,
161 'class' => 'n',
162 'data-n' => $oline,
164 $aural = null;
167 if ($type == 'new-file') {
168 $class = "{$class} differential-new-image";
171 if ($right_prefix) {
172 $right_id = $right_prefix.$p['line'];
173 } else {
174 $right_id = null;
177 $line = $p['line'];
179 $cells[] = phutil_tag(
180 'td',
181 array(
182 'id' => $right_id,
183 'class' => $class.' n',
184 'data-n' => $line,
187 $render = $p['render'];
188 if ($aural !== null) {
189 $render = array($aural, $render);
192 $cells[] = $no_copy;
194 $cells[] = phutil_tag(
195 'td',
196 array(
197 'class' => $class,
198 'data-copy-mode' => 'copy-unified',
200 $render);
202 $cells[] = $no_coverage;
205 // In simple mode, only render the text. This is used to render
206 // "Edit Suggestions" in inline comments.
207 if ($is_simple) {
208 $cells = array($cells[3]);
211 $out[] = phutil_tag('tr', array(), $cells);
213 break;
214 case 'inline':
215 $inline = $this->buildInlineComment(
216 $p['comment'],
217 $p['right']);
218 $out[] = $this->getRowScaffoldForInline($inline);
219 break;
220 case 'no-context':
221 $out[] = phutil_tag(
222 'tr',
223 array(),
224 phutil_tag(
225 'td',
226 array(
227 'class' => 'show-more',
228 'colspan' => $column_width,
230 pht('Context not available.')));
231 break;
232 case 'context':
233 $top = $p['top'];
234 $len = $p['len'];
236 $links = $this->renderShowContextLinks($top, $len, $rows);
238 $out[] = javelin_tag(
239 'tr',
240 array(
241 'sigil' => 'context-target',
243 phutil_tag(
244 'td',
245 array(
246 'class' => 'show-more',
247 'colspan' => $column_width,
249 $links));
250 break;
251 default:
252 $out[] = hsprintf('<tr><th /><th /><td>%s</td></tr>', $type);
253 break;
257 $result = null;
259 if ($out) {
260 if ($is_simple) {
261 $result = $this->newSimpleTable($out);
262 } else {
263 $result = $this->wrapChangeInTable(phutil_implode_html('', $out));
267 return $result;
270 public function renderDocumentEngineBlocks(
271 PhabricatorDocumentEngineBlocks $block_list,
272 $old_changeset_key,
273 $new_changeset_key) {
275 $engine = $this->getDocumentEngine();
276 $layout = $block_list->newTwoUpLayout();
278 $old_comments = $this->getOldComments();
279 $new_comments = $this->getNewComments();
281 $unchanged = array();
282 foreach ($layout as $key => $row) {
283 list($old, $new) = $row;
285 if (!$old) {
286 continue;
289 if (!$new) {
290 continue;
293 if ($old->getDifferenceType() !== null) {
294 continue;
297 if ($new->getDifferenceType() !== null) {
298 continue;
301 $unchanged[$key] = true;
304 $rows = array();
305 $count = count($layout);
306 for ($ii = 0; $ii < $count;) {
307 $start = $ii;
309 for ($jj = $ii; $jj < $count; $jj++) {
310 list($old, $new) = $layout[$jj];
312 if (empty($unchanged[$jj])) {
313 break;
316 $rows[] = array(
317 'type' => 'unchanged',
318 'layoutKey' => $jj,
321 $ii = $jj;
323 for ($jj = $ii; $jj < $count; $jj++) {
324 list($old, $new) = $layout[$jj];
326 if (!empty($unchanged[$jj])) {
327 break;
330 $rows[] = array(
331 'type' => 'old',
332 'layoutKey' => $jj,
336 for ($jj = $ii; $jj < $count; $jj++) {
337 list($old, $new) = $layout[$jj];
339 if (!empty($unchanged[$jj])) {
340 break;
343 $rows[] = array(
344 'type' => 'new',
345 'layoutKey' => $jj,
348 $ii = $jj;
350 // We always expect to consume at least one row when iterating through
351 // the loop and make progress. If we don't, bail out to avoid spinning
352 // to death.
353 if ($ii === $start) {
354 throw new Exception(
355 pht(
356 'Failed to make progress during 1up diff layout.'));
360 $old_ref = null;
361 $new_ref = null;
362 $refs = $block_list->getDocumentRefs();
363 if ($refs) {
364 list($old_ref, $new_ref) = $refs;
367 $view = array();
368 foreach ($rows as $row) {
369 $row_type = $row['type'];
370 $layout_key = $row['layoutKey'];
371 $row_layout = $layout[$layout_key];
372 list($old, $new) = $row_layout;
374 if ($old) {
375 $old_key = $old->getBlockKey();
376 } else {
377 $old_key = null;
380 if ($new) {
381 $new_key = $new->getBlockKey();
382 } else {
383 $new_key = null;
386 $cells = array();
387 $cell_classes = array();
389 if ($row_type === 'unchanged') {
390 $cell_content = $engine->newBlockContentView(
391 $old_ref,
392 $old);
393 } else if ($old && $new) {
394 $block_diff = $engine->newBlockDiffViews(
395 $old_ref,
396 $old,
397 $new_ref,
398 $new);
400 // TODO: We're currently double-rendering this: once when building
401 // the old row, and once when building the new one. In both cases,
402 // we throw away the other half of the output. We could cache this
403 // to improve performance.
405 if ($row_type === 'old') {
406 $cell_content = $block_diff->getOldContent();
407 $cell_classes = $block_diff->getOldClasses();
408 } else {
409 $cell_content = $block_diff->getNewContent();
410 $cell_classes = $block_diff->getNewClasses();
412 } else if ($row_type === 'old') {
413 if (!$old_ref || !$old) {
414 continue;
417 $cell_content = $engine->newBlockContentView(
418 $old_ref,
419 $old);
421 $cell_classes[] = 'old';
422 $cell_classes[] = 'old-full';
424 $new_key = null;
425 } else if ($row_type === 'new') {
426 if (!$new_ref || !$new) {
427 continue;
430 $cell_content = $engine->newBlockContentView(
431 $new_ref,
432 $new);
434 $cell_classes[] = 'new';
435 $cell_classes[] = 'new-full';
437 $old_key = null;
440 if ($old_key === null) {
441 $old_id = null;
442 } else {
443 $old_id = "C{$old_changeset_key}OL{$old_key}";
446 if ($new_key === null) {
447 $new_id = null;
448 } else {
449 $new_id = "C{$new_changeset_key}NL{$new_key}";
452 $cells[] = phutil_tag(
453 'td',
454 array(
455 'id' => $old_id,
456 'data-n' => $old_key,
457 'class' => 'n',
460 $cells[] = phutil_tag(
461 'td',
462 array(
463 'id' => $new_id,
464 'data-n' => $new_key,
465 'class' => 'n',
468 $cells[] = phutil_tag(
469 'td',
470 array(
471 'class' => 'copy',
474 $cell_classes[] = 'diff-flush';
475 $cell_classes = implode(' ', $cell_classes);
477 $cells[] = phutil_tag(
478 'td',
479 array(
480 'class' => $cell_classes,
481 'data-copy-mode' => 'copy-unified',
483 $cell_content);
485 $view[] = phutil_tag(
486 'tr',
487 array(),
488 $cells);
490 if ($old_key !== null) {
491 $old_inlines = idx($old_comments, $old_key, array());
492 foreach ($old_inlines as $inline) {
493 $inline = $this->buildInlineComment(
494 $inline,
495 $on_right = false);
496 $view[] = $this->getRowScaffoldForInline($inline);
500 if ($new_key !== null) {
501 $new_inlines = idx($new_comments, $new_key, array());
502 foreach ($new_inlines as $inline) {
503 $inline = $this->buildInlineComment(
504 $inline,
505 $on_right = true);
506 $view[] = $this->getRowScaffoldForInline($inline);
511 $output = $this->wrapChangeInTable($view);
512 return $this->renderChangesetTable($output);
515 public function getRowScaffoldForInline(PHUIDiffInlineCommentView $view) {
516 return id(new PHUIDiffOneUpInlineCommentRowScaffold())
517 ->addInlineView($view);
521 private function newSimpleTable($content) {
522 return phutil_tag(
523 'table',
524 array(
525 'class' => 'diff-1up-simple-table',
527 $content);