Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/diviner/publisher/DivinerPublisher.php
12256 views
1
<?php
2
3
abstract class DivinerPublisher extends Phobject {
4
5
private $atomCache;
6
private $atomGraphHashToNodeHashMap;
7
private $atomMap = array();
8
private $renderer;
9
private $config;
10
private $symbolReverseMap;
11
private $dropCaches;
12
private $repositoryPHID;
13
14
final public function setDropCaches($drop_caches) {
15
$this->dropCaches = $drop_caches;
16
return $this;
17
}
18
19
final public function setRenderer(DivinerRenderer $renderer) {
20
$renderer->setPublisher($this);
21
$this->renderer = $renderer;
22
return $this;
23
}
24
25
final public function getRenderer() {
26
return $this->renderer;
27
}
28
29
final public function setConfig(array $config) {
30
$this->config = $config;
31
return $this;
32
}
33
34
final public function getConfig($key, $default = null) {
35
return idx($this->config, $key, $default);
36
}
37
38
final public function getConfigurationData() {
39
return $this->config;
40
}
41
42
final public function setAtomCache(DivinerAtomCache $cache) {
43
$this->atomCache = $cache;
44
$graph_map = $this->atomCache->getGraphMap();
45
$this->atomGraphHashToNodeHashMap = array_flip($graph_map);
46
return $this;
47
}
48
49
final protected function getAtomFromGraphHash($graph_hash) {
50
if (empty($this->atomGraphHashToNodeHashMap[$graph_hash])) {
51
throw new Exception(pht("No such atom '%s'!", $graph_hash));
52
}
53
54
return $this->getAtomFromNodeHash(
55
$this->atomGraphHashToNodeHashMap[$graph_hash]);
56
}
57
58
final protected function getAtomFromNodeHash($node_hash) {
59
if (empty($this->atomMap[$node_hash])) {
60
$dict = $this->atomCache->getAtom($node_hash);
61
$this->atomMap[$node_hash] = DivinerAtom::newFromDictionary($dict);
62
}
63
return $this->atomMap[$node_hash];
64
}
65
66
final protected function getSimilarAtoms(DivinerAtom $atom) {
67
if ($this->symbolReverseMap === null) {
68
$rmap = array();
69
$smap = $this->atomCache->getSymbolMap();
70
foreach ($smap as $nhash => $shash) {
71
$rmap[$shash][$nhash] = true;
72
}
73
$this->symbolReverseMap = $rmap;
74
}
75
76
$shash = $atom->getRef()->toHash();
77
78
if (empty($this->symbolReverseMap[$shash])) {
79
throw new Exception(pht('Atom has no symbol map entry!'));
80
}
81
82
$hashes = $this->symbolReverseMap[$shash];
83
84
$atoms = array();
85
foreach ($hashes as $hash => $ignored) {
86
$atoms[] = $this->getAtomFromNodeHash($hash);
87
}
88
89
$atoms = msort($atoms, 'getSortKey');
90
return $atoms;
91
}
92
93
/**
94
* If a book contains multiple definitions of some atom, like some function
95
* `f()`, we assign them an arbitrary (but fairly stable) order and publish
96
* them as `function/f/1/`, `function/f/2/`, etc., or similar.
97
*/
98
final protected function getAtomSimilarIndex(DivinerAtom $atom) {
99
$atoms = $this->getSimilarAtoms($atom);
100
if (count($atoms) == 1) {
101
return 0;
102
}
103
104
$index = 1;
105
foreach ($atoms as $similar_atom) {
106
if ($atom === $similar_atom) {
107
return $index;
108
}
109
$index++;
110
}
111
112
throw new Exception(pht('Expected to find atom while disambiguating!'));
113
}
114
115
abstract protected function loadAllPublishedHashes();
116
abstract protected function deleteDocumentsByHash(array $hashes);
117
abstract protected function createDocumentsByHash(array $hashes);
118
abstract public function findAtomByRef(DivinerAtomRef $ref);
119
120
final public function publishAtoms(array $hashes) {
121
$existing = $this->loadAllPublishedHashes();
122
123
if ($this->dropCaches) {
124
$deleted = $existing;
125
$created = $hashes;
126
} else {
127
$existing_map = array_fill_keys($existing, true);
128
$hashes_map = array_fill_keys($hashes, true);
129
130
$deleted = array_diff_key($existing_map, $hashes_map);
131
$created = array_diff_key($hashes_map, $existing_map);
132
133
$deleted = array_keys($deleted);
134
$created = array_keys($created);
135
}
136
137
$console = PhutilConsole::getConsole();
138
139
$console->writeOut(
140
"%s\n",
141
pht(
142
'Deleting %s document(s).',
143
phutil_count($deleted)));
144
$this->deleteDocumentsByHash($deleted);
145
146
$console->writeOut(
147
"%s\n",
148
pht(
149
'Creating %s document(s).',
150
phutil_count($created)));
151
$this->createDocumentsByHash($created);
152
}
153
154
final protected function shouldGenerateDocumentForAtom(DivinerAtom $atom) {
155
switch ($atom->getType()) {
156
case DivinerAtom::TYPE_METHOD:
157
case DivinerAtom::TYPE_FILE:
158
return false;
159
case DivinerAtom::TYPE_ARTICLE:
160
default:
161
break;
162
}
163
164
return true;
165
}
166
167
final public function getRepositoryPHID() {
168
return $this->repositoryPHID;
169
}
170
171
final public function setRepositoryPHID($repository_phid) {
172
$this->repositoryPHID = $repository_phid;
173
return $this;
174
}
175
176
}
177
178