Skip to content

Commit

Permalink
Merge pull request #10 from nook-ru/search
Browse files Browse the repository at this point in the history
Search reindex command
  • Loading branch information
niksamokhvalov committed May 5, 2016
2 parents 00cf4f9 + a580778 commit de116d3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Notamedia\ConsoleJedi\Cache\Command\ClearCommand;
use Notamedia\ConsoleJedi\Environment\Command\InitCommand;
use Notamedia\ConsoleJedi\Module\Command as Module;
use Notamedia\ConsoleJedi\Search\Command\ReIndexCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -161,6 +162,7 @@ protected function getBitrixCommands()
new ExecuteCommand(),
new ClearCommand(),
new InitCommand(),
new ReIndexCommand(),
],
Module\ModuleCommand::getCommands()
);
Expand Down
82 changes: 82 additions & 0 deletions src/Search/Command/ReIndexCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Notamedia\ConsoleJedi\Search\Command;

use Bitrix\Main\Loader;
use Bitrix\Main\ModuleManager;
use Notamedia\ConsoleJedi\Application\Command\BitrixCommand;
use Notamedia\ConsoleJedi\Application\Exception\BitrixException;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Command for search module reindex
*
* @author Marat Shamshutdinov <[email protected]>
*/
class ReIndexCommand extends BitrixCommand
{
const UPDATE_TIME = 5;

/**
* {@inheritdoc}
*/
protected function configure()
{
parent::configure();

$this->setName('search:reindex')
->setDescription('Rebuild search index')
->addOption('full', 'f', InputOption::VALUE_NONE,
'Clears existing index (otherwise only changed entries would be indexed)');
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if (!Loader::includeModule('search'))
{
throw new BitrixException('Search module is not installed');
}

$searchResult = array();

$bar = new ProgressBar($output, 0);
do
{
$bar->display();

$searchResult = \CSearch::ReIndexAll($input->getOption('full'), static::UPDATE_TIME, $searchResult);

$bar->advance();
$bar->clear();

if (is_array($searchResult) && $searchResult['MODULE'] == 'main')
{
list(, $path) = explode("|", $searchResult["ID"], 2);
$output->writeln("\r " . $path, OutputInterface::VERBOSITY_VERBOSE);
}
} while (is_array($searchResult));

$bar->finish();
$bar->clear();
$output->write("\r");

if (ModuleManager::isModuleInstalled('socialnetwork'))
{
$output->writeln('<info>The Social Network module needs to be reindexed using the Social Network component in the public section of site.</info>');
}

$output->writeln(sprintf('<info>Reindexed</info> %d element%s.', $searchResult, $searchResult > 1 ? 's' : ''));

return 0;
}
}

0 comments on commit de116d3

Please sign in to comment.