Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / macro / markup / PhabricatorMemeRemarkupRule.php
blobeacb1a33edbe8ba1536e90d300af97181f9b404f
1 <?php
3 final class PhabricatorMemeRemarkupRule extends PhutilRemarkupRule {
5 private $images;
7 public function getPriority() {
8 return 200.0;
11 public function apply($text) {
12 return preg_replace_callback(
13 '@{meme,((?:[^}\\\\]+|\\\\.)+)}@m',
14 array($this, 'markupMeme'),
15 $text);
18 public function markupMeme(array $matches) {
19 if (!$this->isFlatText($matches[0])) {
20 return $matches[0];
23 $options = array(
24 'src' => null,
25 'above' => null,
26 'below' => null,
29 $parser = new PhutilSimpleOptions();
30 $options = $parser->parse($matches[1]) + $options;
32 $engine = id(new PhabricatorMemeEngine())
33 ->setViewer(PhabricatorUser::getOmnipotentUser())
34 ->setTemplate($options['src'])
35 ->setAboveText($options['above'])
36 ->setBelowText($options['below']);
38 $asset = $engine->loadCachedFile();
40 $is_html_mail = $this->getEngine()->isHTMLMailMode();
41 $is_text = $this->getEngine()->isTextMode();
42 $must_inline = ($is_html_mail || $is_text);
44 if ($must_inline) {
45 if (!$asset) {
46 try {
47 $asset = $engine->newAsset();
48 } catch (Exception $ex) {
49 return $matches[0];
54 if ($asset) {
55 $uri = $asset->getViewURI();
56 } else {
57 $uri = $engine->getGenerateURI();
60 if ($is_text) {
61 $parts = array();
63 $above = $options['above'];
64 if (strlen($above)) {
65 $parts[] = pht('"%s"', $above);
68 $parts[] = $options['src'].' <'.$uri.'>';
70 $below = $options['below'];
71 if (strlen($below)) {
72 $parts[] = pht('"%s"', $below);
75 $parts = implode("\n", $parts);
76 return $this->getEngine()->storeText($parts);
79 $alt_text = pht(
80 'Macro %s: %s %s',
81 $options['src'],
82 $options['above'],
83 $options['below']);
85 if ($asset) {
86 $img = $this->newTag(
87 'img',
88 array(
89 'src' => $uri,
90 'class' => 'phabricator-remarkup-macro',
91 'alt' => $alt_text,
92 ));
93 } else {
94 $img = id(new PHUIRemarkupImageView())
95 ->setURI($uri)
96 ->addClass('phabricator-remarkup-macro')
97 ->setAlt($alt_text);
100 return $this->getEngine()->storeText($img);