[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/diviner/publisher/ -> DivinerPublisher.php (source)

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


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1