Path: blob/master/src/applications/diviner/publisher/DivinerLivePublisher.php
12256 views
<?php12final class DivinerLivePublisher extends DivinerPublisher {34private $book;56protected function getBook() {7if (!$this->book) {8$book_name = $this->getConfig('name');910$book = id(new DivinerLiveBook())->loadOneWhere(11'name = %s',12$book_name);1314if (!$book) {15$book = id(new DivinerLiveBook())16->setName($book_name)17->setViewPolicy(PhabricatorPolicies::getMostOpenPolicy())18->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN)19->save();20}2122$conn_w = $book->establishConnection('w');23$conn_w->openTransaction();2425$book26->setRepositoryPHID($this->getRepositoryPHID())27->setConfigurationData($this->getConfigurationData())28->save();2930// TODO: This is gross. Without this, the repository won't be updated for31// atoms which have already been published.32queryfx(33$conn_w,34'UPDATE %T SET repositoryPHID = %s WHERE bookPHID = %s',35id(new DivinerLiveSymbol())->getTableName(),36$this->getRepositoryPHID(),37$book->getPHID());3839$conn_w->saveTransaction();40$this->book = $book;4142PhabricatorSearchWorker::queueDocumentForIndexing($book->getPHID());43}4445return $this->book;46}4748private function loadSymbolForAtom(DivinerAtom $atom) {49$symbol = id(new DivinerAtomQuery())50->setViewer(PhabricatorUser::getOmnipotentUser())51->withBookPHIDs(array($this->getBook()->getPHID()))52->withTypes(array($atom->getType()))53->withNames(array($atom->getName()))54->withContexts(array($atom->getContext()))55->withIndexes(array($this->getAtomSimilarIndex($atom)))56->executeOne();5758if ($symbol) {59return $symbol;60}6162return id(new DivinerLiveSymbol())63->setBookPHID($this->getBook()->getPHID())64->setType($atom->getType())65->setName($atom->getName())66->setContext($atom->getContext())67->setAtomIndex($this->getAtomSimilarIndex($atom));68}6970private function loadAtomStorageForSymbol(DivinerLiveSymbol $symbol) {71$storage = id(new DivinerLiveAtom())->loadOneWhere(72'symbolPHID = %s',73$symbol->getPHID());7475if ($storage) {76return $storage;77}7879return id(new DivinerLiveAtom())80->setSymbolPHID($symbol->getPHID());81}8283protected function loadAllPublishedHashes() {84$symbols = id(new DivinerAtomQuery())85->setViewer(PhabricatorUser::getOmnipotentUser())86->withBookPHIDs(array($this->getBook()->getPHID()))87->withGhosts(false)88->execute();8990return mpull($symbols, 'getGraphHash');91}9293protected function deleteDocumentsByHash(array $hashes) {94$atom_table = new DivinerLiveAtom();95$symbol_table = new DivinerLiveSymbol();96$conn_w = $symbol_table->establishConnection('w');9798$strings = array();99foreach ($hashes as $hash) {100$strings[] = qsprintf($conn_w, '%s', $hash);101}102103foreach (PhabricatorLiskDAO::chunkSQL($strings) as $chunk) {104queryfx(105$conn_w,106'UPDATE %T SET graphHash = NULL, nodeHash = NULL107WHERE graphHash IN (%LQ)',108$symbol_table->getTableName(),109$chunk);110}111112queryfx(113$conn_w,114'DELETE a FROM %T a LEFT JOIN %T s115ON a.symbolPHID = s.phid116WHERE s.graphHash IS NULL',117$atom_table->getTableName(),118$symbol_table->getTableName());119}120121protected function createDocumentsByHash(array $hashes) {122foreach ($hashes as $hash) {123$atom = $this->getAtomFromGraphHash($hash);124$ref = $atom->getRef();125126$symbol = $this->loadSymbolForAtom($atom);127128$is_documentable = $this->shouldGenerateDocumentForAtom($atom);129130$symbol131->setRepositoryPHID($this->getRepositoryPHID())132->setGraphHash($hash)133->setIsDocumentable((int)$is_documentable)134->setTitle($ref->getTitle())135->setGroupName($ref->getGroup())136->setNodeHash($atom->getHash());137138if ($atom->getType() !== DivinerAtom::TYPE_FILE) {139$renderer = $this->getRenderer();140$summary = $renderer->getAtomSummary($atom);141$symbol->setSummary($summary);142} else {143$symbol->setSummary('');144}145146$symbol->save();147148PhabricatorSearchWorker::queueDocumentForIndexing($symbol->getPHID());149150// TODO: We probably need a finer-grained sense of what "documentable"151// atoms are. Neither files nor methods are currently considered152// documentable, but for different reasons: files appear nowhere, while153// methods just don't appear at the top level. These are probably154// separate concepts. Since we need atoms in order to build method155// documentation, we insert them here. This also means we insert files,156// which are unnecessary and unused. Make sure this makes sense, but then157// probably introduce separate "isTopLevel" and "isDocumentable" flags?158// TODO: Yeah do that soon ^^^159160if ($atom->getType() !== DivinerAtom::TYPE_FILE) {161$storage = $this->loadAtomStorageForSymbol($symbol)162->setAtomData($atom->toDictionary())163->setContent(null)164->save();165}166}167}168169public function findAtomByRef(DivinerAtomRef $ref) {170// TODO: Actually implement this.171return null;172}173174}175176177