@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
at recaptime-dev/main 84 lines 1.8 kB view raw
1<?php 2 3final class DiffusionSourceHyperlinkEngineExtension 4 extends PhabricatorRemarkupHyperlinkEngineExtension { 5 6 const LINKENGINEKEY = 'diffusion-src'; 7 8 public function processHyperlinks(array $hyperlinks) { 9 $engine = $this->getEngine(); 10 $viewer = $engine->getConfig('viewer'); 11 12 if (!$viewer) { 13 return; 14 } 15 16 $hyperlinks = $this->getSelfLinks($hyperlinks); 17 18 $links = array(); 19 foreach ($hyperlinks as $link) { 20 $uri = $link->getURI(); 21 $uri = new PhutilURI($uri); 22 23 $path = $uri->getPath(); 24 25 $pattern = 26 '(^'. 27 '/(?:diffusion|source)'. 28 '/(?P<identifier>[^/]+)'. 29 '/browse'. 30 '/(?P<blob>.*)'. 31 '\z)'; 32 $matches = null; 33 if (!preg_match($pattern, $path, $matches)) { 34 continue; 35 } 36 37 $links[] = array( 38 'ref' => $link, 39 'identifier' => $matches['identifier'], 40 'blob' => $matches['blob'], 41 ); 42 } 43 44 if (!$links) { 45 return; 46 } 47 48 $identifiers = ipull($links, 'identifier'); 49 50 $query = id(new PhabricatorRepositoryQuery()) 51 ->setViewer($viewer) 52 ->withIdentifiers($identifiers); 53 54 $query->execute(); 55 56 $repository_map = $query->getIdentifierMap(); 57 58 foreach ($links as $link) { 59 $identifier = $link['identifier']; 60 61 $repository = idx($repository_map, $identifier); 62 if (!$repository) { 63 continue; 64 } 65 66 $ref = $link['ref']; 67 $uri = $ref->getURI(); 68 69 70 $tag = id(new DiffusionSourceLinkView()) 71 ->setViewer($viewer) 72 ->setRepository($repository) 73 ->setURI($uri) 74 ->setBlob($link['blob']); 75 76 if (!$ref->isEmbed()) { 77 $tag->setText($uri); 78 } 79 80 $ref->setResult($tag); 81 } 82 } 83 84}