Generate file attachment transactions for explicit Remarkup attachments on common...
[phabricator.git] / src / applications / phurl / remarkup / PhabricatorPhurlLinkRemarkupRule.php
blobc4ffc366daa64b63a5dae431da309fd8118b1863
1 <?php
3 final class PhabricatorPhurlLinkRemarkupRule extends PhutilRemarkupRule {
5 public function getPriority() {
6 return 200.0;
9 public function apply($text) {
10 // `((123))` remarkup link to `/u/123`
11 // `((alias))` remarkup link to `/u/alias`
12 return preg_replace_callback(
13 '/\(\(([^ )]+)\)\)/',
14 array($this, 'markupLink'),
15 $text);
18 public function markupLink(array $matches) {
19 $engine = $this->getEngine();
20 $viewer = $engine->getConfig('viewer');
22 $text_mode = $engine->isTextMode();
23 $html_mode = $engine->isHTMLMailMode();
25 if (!$this->isFlatText($matches[0])) {
26 return $matches[0];
29 $ref = $matches[1];
30 $monogram = null;
31 $is_monogram = '/^U(?P<id>[1-9]\d*)/';
33 $query = id(new PhabricatorPhurlURLQuery())
34 ->setViewer($viewer);
36 if (preg_match($is_monogram, $ref, $monogram)) {
37 $query->withIDs(array($monogram[1]));
38 } else if (ctype_digit($ref)) {
39 $query->withIDs(array($ref));
40 } else {
41 $query->withAliases(array($ref));
44 $phurl = $query->executeOne();
45 if (!$phurl) {
46 return $matches[0];
49 $uri = $phurl->getRedirectURI();
50 $name = $phurl->getDisplayName();
52 if ($text_mode || $html_mode) {
53 $uri = PhabricatorEnv::getProductionURI($uri);
56 if ($text_mode) {
57 return pht(
58 '%s <%s>',
59 $name,
60 $uri);
61 } else {
62 $link = phutil_tag(
63 'a',
64 array(
65 'href' => $uri,
66 'target' => '_blank',
67 'rel' => 'noreferrer',
69 $name);
72 return $this->getEngine()->storeText($link);