Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

Commit

Permalink
indexer:search:build command
Browse files Browse the repository at this point in the history
int. ticket #30916
  • Loading branch information
amenk committed Feb 15, 2018
1 parent 5a89178 commit 5133ac3
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 2 deletions.
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ commands:
- IMI\Contao\Command\Admin\User\DeleteUserCommand
- IMI\Contao\Command\Admin\User\ChangePasswordCommand
- IMI\Contao\Command\Developer\Log\FollowCommand
- IMI\Contao\Command\Indexer\Search\BuildCommand
- IMI\Contao\Command\ScriptCommand
- IMI\Contao\Command\SelfUpdateCommand
- IMI\Contao\Command\System\InfoCommand
Expand Down
2 changes: 1 addition & 1 deletion src/IMI/Contao/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ function _initContao1()

\System::loadLanguageFile('default');
// Restore autoloaders that might be removed by extensions that overwrite Varien/Autoload
$this->_restoreAutoloaders($autoloaders);
//$this->_restoreAutoloaders($autoloaders);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/IMI/Contao/Command/AbstractContaoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,4 +509,10 @@ protected function isSourceTypeRepository($type)
{
return in_array($type, array('git', 'hg'));
}

protected function getClass($strClass)
{
return (in_array('getInstance', get_class_methods($strClass))) ? call_user_func(array($strClass, 'getInstance')) : new $strClass();
}

}
62 changes: 62 additions & 0 deletions src/IMI/Contao/Command/Indexer/Search/BuildCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace IMI\Contao\Command\Indexer\Search;

use IMI\Contao\Application;
use IMI\Contao\Command\AbstractContaoCommand;
use IMI\Contao\System\IndexerSearchBackend;
use IMI\Contao\System\PurgeData;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class BuildCommand extends AbstractContaoCommand
{
protected function configure()
{
$this
->setName('indexer:search:build')
->setDescription('Build search index');
}




/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return int|void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->detectContao($output, true);
if (!$this->initContao()) {
return;
}

$backend = new IndexerSearchBackend();
$searchablePages = $backend->getSearchablePages();

if (!$GLOBALS['TL_CONFIG']['enableSearch']) {
$output->writeln('Search has to be enabled');
die();
}

$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);

foreach ($searchablePages as $url) {
@file_get_contents($url, false, stream_context_create($arrContextOptions));
$code = $http_response_header[0];
if (strpos($code, ' 200 ') !== false) {
$output->writeln("<info>$code</info> $url");
} else {
$output->writeln("<error>$code</error> $url");
}
}
$output->writeln('<info>DCAs generated. You might want to clean / rebuild the DCA cache.</info>');
}
}
1 change: 0 additions & 1 deletion src/IMI/Contao/System/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,5 @@ public function listRunOnce()
}

return $result;

}
}
58 changes: 58 additions & 0 deletions src/IMI/Contao/System/IndexerSearchBackend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace IMI\Contao\System;


/**
* @category IMI
* @package _
*/
class IndexerSearchBackend extends Backend {
/**
* Mostly copied from \Contao\Automator::generateSitemap
* @param int $intId
*/
public function getSearchablePages()
{
$time = \Date::floorToMinute();
$objDatabase = \Database::getInstance();

// Get all published root pages
$objRoot = $objDatabase->execute("SELECT id, language, sitemapName FROM tl_page WHERE type='root' AND createSitemap='1' AND sitemapName!='' AND (start='' OR start<='$time') AND (stop='' OR stop>'" . ($time + 60) . "') AND published='1'");

// Return if there are no pages
if ($objRoot->numRows < 1)
{
return;
}

$result = array();

// Create the XML file
while ($objRoot->next())
{
$objFile = new \File('share/' . $objRoot->sitemapName . '.xml', true);

// Find the searchable pages
$arrPages = \Backend::findSearchablePages($objRoot->id, '', true);

// HOOK: take additional pages
if (isset($GLOBALS['TL_HOOKS']['getSearchablePages']) && is_array($GLOBALS['TL_HOOKS']['getSearchablePages']))
{
foreach ($GLOBALS['TL_HOOKS']['getSearchablePages'] as $callback)
{
$this->import($callback[0]);
$arrPages = $this->{$callback[0]}->{$callback[1]}($arrPages, $objRoot->id, true, $objRoot->language);
}
}

// Add pages
foreach ($arrPages as $strUrl)
{
$result[] = $strUrl;
}
}

return $result;
}
}

0 comments on commit 5133ac3

Please sign in to comment.