@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 DiffusionPatternSearchView extends DiffusionView {
4
5 private $path;
6 private $matches;
7 private $pattern;
8
9 public function setPath($path) {
10 $this->path = $path;
11 return $this;
12 }
13
14 public function setMatches(array $matches) {
15 $this->matches = $matches;
16 return $this;
17 }
18
19 public function setPattern($pattern) {
20 $this->pattern = $pattern;
21 return $this;
22 }
23
24 public function render() {
25 $drequest = $this->getDiffusionRequest();
26 $path = $this->path;
27 $pattern = $this->pattern;
28 $rows = array();
29
30 foreach ($this->matches as $result) {
31 list($line, $string) = $result;
32
33 $matches = null;
34 $count = @preg_match_all(
35 '('.$pattern.')u',
36 $string,
37 $matches,
38 PREG_OFFSET_CAPTURE);
39
40 if (!$count) {
41 $output = ltrim($string);
42 } else {
43 $output = array();
44 $cursor = 0;
45 $length = strlen($string);
46 foreach ($matches[0] as $match) {
47 $offset = $match[1];
48 if ($cursor != $offset) {
49 $output[] = array(
50 'text' => substr($string, $cursor, ($offset - $cursor)),
51 'highlight' => false,
52 );
53 }
54 $output[] = array(
55 'text' => $match[0],
56 'highlight' => true,
57 );
58 $cursor = $offset + strlen($match[0]);
59 }
60 if ($cursor != $length) {
61 $output[] = array(
62 'text' => substr($string, $cursor),
63 'highlight' => false,
64 );
65 }
66
67 if ($output) {
68 $output[0]['text'] = ltrim($output[0]['text']);
69 }
70
71 foreach ($output as $key => $segment) {
72 if ($segment['highlight']) {
73 $output[$key] = phutil_tag('strong', array(), $segment['text']);
74 } else {
75 $output[$key] = $segment['text'];
76 }
77 }
78 }
79
80 $string = phutil_tag(
81 'pre',
82 array('class' => 'PhabricatorMonospaced phui-source-fragment'),
83 $output);
84
85 $href = $drequest->generateURI(array(
86 'action' => 'browse',
87 'path' => $path,
88 'line' => $line,
89 ));
90
91 $rows[] = array(
92 phutil_tag('a', array('href' => $href), $line),
93 $string,
94 );
95 }
96
97 $path_title = Filesystem::readablePath($this->path, $drequest->getPath());
98
99 $href = $drequest->generateURI(
100 array(
101 'action' => 'browse',
102 'path' => $this->path,
103 ));
104
105 $title = phutil_tag('a', array('href' => $href), $path_title);
106
107
108 $table = id(new AphrontTableView($rows))
109 ->setClassName('remarkup-code')
110 ->setHeaders(array(pht('Line'), pht('String')))
111 ->setColumnClasses(array('n', 'wide'));
112
113 $header = id(new PHUIHeaderView())
114 ->setHeader($title);
115
116 $box = id(new PHUIObjectBoxView())
117 ->setHeader($header)
118 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
119 ->setTable($table);
120
121 return $box->render();
122 }
123
124
125}