[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/search/management/ -> PhabricatorSearchManagementIndexWorkflow.php (source)

   1  <?php
   2  
   3  final class PhabricatorSearchManagementIndexWorkflow
   4    extends PhabricatorSearchManagementWorkflow {
   5  
   6    protected function didConstruct() {
   7      $this
   8        ->setName('index')
   9        ->setSynopsis('Build or rebuild search indexes.')
  10        ->setExamples(
  11          "**index** D123\n".
  12          "**index** --type DREV\n".
  13          "**index** --all")
  14        ->setArguments(
  15          array(
  16            array(
  17              'name' => 'all',
  18              'help' => 'Reindex all documents.',
  19            ),
  20            array(
  21              'name'  => 'type',
  22              'param' => 'TYPE',
  23              'help'  => 'PHID type to reindex, like "TASK" or "DREV".',
  24            ),
  25            array(
  26              'name' => 'background',
  27              'help' => 'Instead of indexing in this process, queue tasks for '.
  28                        'the daemons. This can improve performance, but makes '.
  29                        'it more difficult to debug search indexing.',
  30            ),
  31            array(
  32              'name'      => 'objects',
  33              'wildcard'  => true,
  34            ),
  35          ));
  36    }
  37  
  38    public function execute(PhutilArgumentParser $args) {
  39      $console = PhutilConsole::getConsole();
  40  
  41      $is_all = $args->getArg('all');
  42      $is_type = $args->getArg('type');
  43  
  44      $obj_names = $args->getArg('objects');
  45  
  46      if ($obj_names && ($is_all || $is_type)) {
  47        throw new PhutilArgumentUsageException(
  48          "You can not name objects to index alongside the '--all' or '--type' ".
  49          "flags.");
  50      } else if (!$obj_names && !($is_all || $is_type)) {
  51        throw new PhutilArgumentUsageException(
  52          "Provide one of '--all', '--type' or a list of object names.");
  53      }
  54  
  55      if ($obj_names) {
  56        $phids = $this->loadPHIDsByNames($obj_names);
  57      } else {
  58        $phids = $this->loadPHIDsByTypes($is_type);
  59      }
  60  
  61      if (!$phids) {
  62        throw new PhutilArgumentUsageException('Nothing to index!');
  63      }
  64  
  65      if ($args->getArg('background')) {
  66        $is_background = true;
  67      } else {
  68        PhabricatorWorker::setRunAllTasksInProcess(true);
  69        $is_background = false;
  70      }
  71  
  72      if (!$is_background) {
  73        $console->writeOut(
  74          "%s\n",
  75          pht(
  76            'Run this workflow with "--background" to queue tasks for the '.
  77            'daemon workers.'));
  78      }
  79  
  80      $groups = phid_group_by_type($phids);
  81      foreach ($groups as $group_type => $group) {
  82        $console->writeOut(
  83          "%s\n",
  84          pht('Indexing %d object(s) of type %s.', count($group), $group_type));
  85      }
  86  
  87      $bar = id(new PhutilConsoleProgressBar())
  88        ->setTotal(count($phids));
  89  
  90      $indexer = new PhabricatorSearchIndexer();
  91      foreach ($phids as $phid) {
  92        $indexer->queueDocumentForIndexing($phid);
  93        $bar->update(1);
  94      }
  95  
  96      $bar->done();
  97    }
  98  
  99    private function loadPHIDsByNames(array $names) {
 100      $query = id(new PhabricatorObjectQuery())
 101        ->setViewer($this->getViewer())
 102        ->withNames($names);
 103      $query->execute();
 104      $objects = $query->getNamedResults();
 105  
 106      foreach ($names as $name) {
 107        if (empty($objects[$name])) {
 108          throw new PhutilArgumentUsageException(
 109            "'{$name}' is not the name of a known object.");
 110        }
 111      }
 112  
 113      return mpull($objects, 'getPHID');
 114    }
 115  
 116    private function loadPHIDsByTypes($type) {
 117      $indexer_symbols = id(new PhutilSymbolLoader())
 118        ->setAncestorClass('PhabricatorSearchDocumentIndexer')
 119        ->setConcreteOnly(true)
 120        ->setType('class')
 121        ->selectAndLoadSymbols();
 122  
 123      $indexers = array();
 124      foreach ($indexer_symbols as $symbol) {
 125        $indexers[] = newv($symbol['name'], array());
 126      }
 127  
 128      $phids = array();
 129      foreach ($indexers as $indexer) {
 130        $indexer_phid = $indexer->getIndexableObject()->generatePHID();
 131        $indexer_type = phid_get_type($indexer_phid);
 132  
 133        if ($type && strcasecmp($indexer_type, $type)) {
 134          continue;
 135        }
 136  
 137        $iterator = $indexer->getIndexIterator();
 138        foreach ($iterator as $object) {
 139          $phids[] = $object->getPHID();
 140        }
 141      }
 142  
 143      return $phids;
 144    }
 145  
 146  }


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