Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/meta/engineextension/PhabricatorSelfHyperlinkEngineExtension.php
12256 views
1
<?php
2
3
final class PhabricatorSelfHyperlinkEngineExtension
4
extends PhabricatorRemarkupHyperlinkEngineExtension {
5
6
const LINKENGINEKEY = 'phabricator-self';
7
8
public function processHyperlinks(array $hyperlinks) {
9
$engine = $this->getEngine();
10
$viewer = $engine->getConfig('viewer');
11
12
// If we don't have a valid viewer, just bail out. We aren't going to be
13
// able to do very much.
14
if (!$viewer) {
15
return;
16
}
17
18
$self_links = $this->getSelfLinks($hyperlinks);
19
20
// For links in the form "/X123", we can reasonably guess that they are
21
// fairly likely to be object names. Try to look them up.
22
$object_names = array();
23
foreach ($self_links as $key => $link) {
24
$uri = new PhutilURI($link->getURI());
25
26
$matches = null;
27
$path = $uri->getPath();
28
if (!preg_match('(^/([^/]+)\z)', $path, $matches)) {
29
continue;
30
}
31
32
$object_names[$key] = $matches[1];
33
}
34
35
if ($object_names) {
36
$object_query = id(new PhabricatorObjectQuery())
37
->setViewer($viewer)
38
->withNames($object_names);
39
40
$object_query->execute();
41
42
$object_map = $object_query->getNamedResults();
43
} else {
44
$object_map = array();
45
}
46
47
if ($object_map) {
48
$object_phids = mpull($object_map, 'getPHID');
49
} else {
50
$object_phids = array();
51
}
52
53
$handles = $viewer->loadHandles($object_phids);
54
55
foreach ($object_names as $key => $object_name) {
56
$object = idx($object_map, $object_name);
57
if (!$object) {
58
continue;
59
}
60
61
$phid = $object->getPHID();
62
$handle = $handles[$phid];
63
64
$link = $self_links[$key];
65
$raw_uri = $link->getURI();
66
$is_embed = $link->isEmbed();
67
68
$tag = $handle->renderTag()
69
->setPHID($phid)
70
->setHref($raw_uri);
71
72
if (!$is_embed) {
73
$tag->setName($raw_uri);
74
}
75
76
$link->setResult($tag);
77
78
unset($self_links[$key]);
79
}
80
81
$key_mentioned = PhabricatorObjectRemarkupRule::KEY_MENTIONED_OBJECTS;
82
$mentioned_phids = $engine->getTextMetadata($key_mentioned, array());
83
foreach ($object_phids as $object_phid) {
84
$mentioned_phids[$object_phid] = $object_phid;
85
}
86
$engine->setTextMetadata($key_mentioned, $mentioned_phids);
87
}
88
89
}
90
91