Exemplo n.º 1
0
 public function execute(InputInterface $in, OutputInterface $out)
 {
     parent::execute($in, $out);
     $searchBaseUrl = $this->resolveBaseUrl('search', $in);
     $user = $this->session->getUser();
     $tokenCredentials = NULL !== $user ? $user->getTokenCredentials() : NULL;
     $consumerCredentials = $this->session->getConsumerCredentials();
     $service = $this->searchServiceFactory->createService($in, $out, $searchBaseUrl, $consumerCredentials, $tokenCredentials);
     $query = new Parameter\Query($in->getArgument('query'));
     $parameters = array($query);
     $facets = $in->getOption('facetField');
     if (!empty($facets)) {
         $facetComponent = new FacetComponent();
         foreach ($facets as $facet) {
             $parameters[] = $facetComponent->facetField($facet);
         }
     }
     $fq = $in->getOption('fq');
     if ($fq) {
         $parameters[] = new Parameter\FilterQuery($fq);
     }
     if ($in->getOption('group')) {
         $parameters[] = new Parameter\Group();
     }
     if ($in->getOptions('rows')) {
         $parameters[] = new Parameter\Rows($in->getOption('rows'));
     }
     if ($in->getOption('start')) {
         $parameters[] = new Parameter\Start($in->getOption('start'));
     }
     $sorts = $in->getOption('sort');
     foreach ($sorts as $sort) {
         // @todo validate the given sort option
         $sort_args = explode(' ', $sort);
         if (count($sort_args) < 2) {
             $sort_args[] = 'asc';
         }
         list($field, $direction) = $sort_args;
         $parameters[] = new Parameter\Sort($field, $direction);
     }
     $result = $service->search($parameters);
     $out->writeln('total: ' . $result->getTotalCount());
     $out->writeln('current: ' . $result->getCurrentCount());
     // @todo consider registering the facet component up-front
     // as a listener to the service, to avoid the need to actively
     // obtain the results afterwards
     if (isset($facetComponent)) {
         $facetComponent->obtainResults($result);
         foreach ($facetComponent->getFacets() as $facet) {
             $out->writeln(str_repeat('-', 10));
             $out->writeln('Facet: ' . $facet->getKey());
             $out->writeln('Results:');
             foreach ($facet->getResult()->getItems() as $name => $number) {
                 $out->writeln("{$name} ({$number})");
             }
             $out->writeln(str_repeat('-', 10));
         }
     }
     $currentCount = $result->getCurrentCount();
     if (0 === $currentCount) {
         return;
     }
     $dialog = $this->getHelperSet()->get('dialog');
     /* @var \Symfony\Component\Console\Helper\DialogHelper $dialog */
     // @todo provide misc. interactive options, like showing summary of all results
     //   and to show details of 1 item (implemented now)
     //   repeat till 'exit' option is used
     do {
         $answer = $dialog->askAndValidate($out, "Specify a number (1 to {$currentCount}) to show details, or 'exit' to quit: ", function ($answer) use($currentCount) {
             if (!(ctype_digit($answer) && $answer > 0 && $answer <= $currentCount) && 'exit' !== $answer) {
                 throw new \RuntimeException('Invalid value');
             }
             return $answer;
         });
         if (ctype_digit($answer)) {
             $items = $result->getItems();
             $item = $items[(int) $answer - 1];
             /* @var \CultureFeed_Cdb_Item_Event $entity */
             $entity = $item->getEntity();
             $details = $entity->getDetails();
             foreach ($details as $detail) {
                 // @todo introduce a command line option to specify the language
                 if ('nl' === $detail->getLanguage()) {
                     $out->writeln($detail->getTitle());
                 }
             }
             $out->writeln($entity->getCdbId());
         }
     } while ('exit' !== $answer);
 }
 /**
  * Execute the search for current page.
  */
 protected function execute($params)
 {
     // Add start index (page number we want)
     $this->parameters[] = new Parameter\Start(($this->pageNumber - 1) * $this->resultsPerPage);
     // Add items / page.
     $this->parameters[] = new Parameter\Rows($this->resultsPerPage);
     // Add grouping so returned data is not duplicate.
     $this->parameters[] = new Parameter\Group($this->group);
     // Add spellcheck if needed
     if (!empty($this->query[0])) {
         $this->parameters[] = new Parameter\Parameter('spellcheck', 'true');
         $this->parameters[] = new Parameter\Parameter('spellcheckQuery', $this->query[0]);
     } else {
         $this->parameters[] = new Parameter\Parameter('spellcheck', 'false');
     }
     drupal_alter('culturefeed_search_page_query', $this);
     // Prepare the search query and add to the search parameters.
     $this->parameters[] = $this->prepareQuery();
     $searchService = culturefeed_get_search_service();
     $this->result = $searchService->search($this->parameters);
     $this->facetComponent->obtainResults($this->result);
 }