Path: blob/master/src/applications/diviner/publisher/DivinerPublisher.php
12256 views
<?php12abstract class DivinerPublisher extends Phobject {34private $atomCache;5private $atomGraphHashToNodeHashMap;6private $atomMap = array();7private $renderer;8private $config;9private $symbolReverseMap;10private $dropCaches;11private $repositoryPHID;1213final public function setDropCaches($drop_caches) {14$this->dropCaches = $drop_caches;15return $this;16}1718final public function setRenderer(DivinerRenderer $renderer) {19$renderer->setPublisher($this);20$this->renderer = $renderer;21return $this;22}2324final public function getRenderer() {25return $this->renderer;26}2728final public function setConfig(array $config) {29$this->config = $config;30return $this;31}3233final public function getConfig($key, $default = null) {34return idx($this->config, $key, $default);35}3637final public function getConfigurationData() {38return $this->config;39}4041final public function setAtomCache(DivinerAtomCache $cache) {42$this->atomCache = $cache;43$graph_map = $this->atomCache->getGraphMap();44$this->atomGraphHashToNodeHashMap = array_flip($graph_map);45return $this;46}4748final protected function getAtomFromGraphHash($graph_hash) {49if (empty($this->atomGraphHashToNodeHashMap[$graph_hash])) {50throw new Exception(pht("No such atom '%s'!", $graph_hash));51}5253return $this->getAtomFromNodeHash(54$this->atomGraphHashToNodeHashMap[$graph_hash]);55}5657final protected function getAtomFromNodeHash($node_hash) {58if (empty($this->atomMap[$node_hash])) {59$dict = $this->atomCache->getAtom($node_hash);60$this->atomMap[$node_hash] = DivinerAtom::newFromDictionary($dict);61}62return $this->atomMap[$node_hash];63}6465final protected function getSimilarAtoms(DivinerAtom $atom) {66if ($this->symbolReverseMap === null) {67$rmap = array();68$smap = $this->atomCache->getSymbolMap();69foreach ($smap as $nhash => $shash) {70$rmap[$shash][$nhash] = true;71}72$this->symbolReverseMap = $rmap;73}7475$shash = $atom->getRef()->toHash();7677if (empty($this->symbolReverseMap[$shash])) {78throw new Exception(pht('Atom has no symbol map entry!'));79}8081$hashes = $this->symbolReverseMap[$shash];8283$atoms = array();84foreach ($hashes as $hash => $ignored) {85$atoms[] = $this->getAtomFromNodeHash($hash);86}8788$atoms = msort($atoms, 'getSortKey');89return $atoms;90}9192/**93* If a book contains multiple definitions of some atom, like some function94* `f()`, we assign them an arbitrary (but fairly stable) order and publish95* them as `function/f/1/`, `function/f/2/`, etc., or similar.96*/97final protected function getAtomSimilarIndex(DivinerAtom $atom) {98$atoms = $this->getSimilarAtoms($atom);99if (count($atoms) == 1) {100return 0;101}102103$index = 1;104foreach ($atoms as $similar_atom) {105if ($atom === $similar_atom) {106return $index;107}108$index++;109}110111throw new Exception(pht('Expected to find atom while disambiguating!'));112}113114abstract protected function loadAllPublishedHashes();115abstract protected function deleteDocumentsByHash(array $hashes);116abstract protected function createDocumentsByHash(array $hashes);117abstract public function findAtomByRef(DivinerAtomRef $ref);118119final public function publishAtoms(array $hashes) {120$existing = $this->loadAllPublishedHashes();121122if ($this->dropCaches) {123$deleted = $existing;124$created = $hashes;125} else {126$existing_map = array_fill_keys($existing, true);127$hashes_map = array_fill_keys($hashes, true);128129$deleted = array_diff_key($existing_map, $hashes_map);130$created = array_diff_key($hashes_map, $existing_map);131132$deleted = array_keys($deleted);133$created = array_keys($created);134}135136$console = PhutilConsole::getConsole();137138$console->writeOut(139"%s\n",140pht(141'Deleting %s document(s).',142phutil_count($deleted)));143$this->deleteDocumentsByHash($deleted);144145$console->writeOut(146"%s\n",147pht(148'Creating %s document(s).',149phutil_count($created)));150$this->createDocumentsByHash($created);151}152153final protected function shouldGenerateDocumentForAtom(DivinerAtom $atom) {154switch ($atom->getType()) {155case DivinerAtom::TYPE_METHOD:156case DivinerAtom::TYPE_FILE:157return false;158case DivinerAtom::TYPE_ARTICLE:159default:160break;161}162163return true;164}165166final public function getRepositoryPHID() {167return $this->repositoryPHID;168}169170final public function setRepositoryPHID($repository_phid) {171$this->repositoryPHID = $repository_phid;172return $this;173}174175}176177178