Skip to content

Commit

Permalink
Fix EZP-22368: '--allow-root-user' option not used on forked process.
Browse files Browse the repository at this point in the history
This removes the script re-execution, doing the actual work after fork.
  • Loading branch information
Joao Inacio committed Feb 18, 2014
1 parent 1221fc8 commit 9431458
Showing 1 changed file with 52 additions and 130 deletions.
182 changes: 52 additions & 130 deletions bin/php/updatesearchindexsolr.php
Expand Up @@ -97,7 +97,7 @@ public function run()
$this->Script->startup();

$this->Options = $this->Script->getOptions(
"[db-host:][db-user:][db-password:][db-database:][db-type:|db-driver:][sql][clean][clean-all][conc:][offset:][limit:][topNodeID:][php-exec:][commit-within:]",
"[db-host:][db-user:][db-password:][db-database:][db-type:|db-driver:][sql][clean][clean-all][conc:][php-exec:][commit-within:]",
"",
array(
'db-host' => "Database host",
Expand All @@ -113,9 +113,6 @@ public function run()
'php-exec' => 'Full path to PHP executable',
'commit-within' => 'Commit to Solr within this time in seconds (default '
. self::DEFAULT_COMMIT_WITHIN . ' seconds)',
'offset' => '*For internal use only*',
'limit' => '*For internal use only*',
'topNodeID' => '*For internal use only*',
)
);
$this->Script->initialize();
Expand Down Expand Up @@ -155,90 +152,16 @@ public function run()
$this->commitWithin = (int)$this->Options['commit-within'];
}

// Check if current instance is main or sub process.
// Main process can not have offset or limit set.
// sub process MUST have offset and limit set.
$offset = $this->Options['offset'];
$limit = $this->Options['limit'];
$topNodeID = $this->Options['topNodeID'];
if ( !is_numeric( $offset ) &&
!is_numeric( $limit ) &&
!is_numeric( $topNodeID ) )
{
$this->CLI->output( 'Starting object re-indexing' );

// Get PHP executable from user.
$this->getPHPExecutable();
$this->CLI->output( 'Starting object re-indexing' );

$this->runMain();
}
elseif ( is_numeric( $offset ) &&
is_numeric( $limit ) &&
is_numeric( $topNodeID ) )
{
$this->runSubProcess( $topNodeID, $offset, $limit );
}
else
{
//OBS !!, invalid.
$this->CLI->error( 'Invalid parameters provided.' );
$this->Script->shutdown( 2 );
}
}

/**
* Run sub process.
*
* @param int $nodeID
* @param int $offset
* @param int $limit
*/
protected function runSubProcess( $nodeID, $offset, $limit )
{
$count = 0;
$node = eZContentObjectTreeNode::fetch( $nodeID );
if ( !is_object( $node ) )
{
$this->CLI->error( "An error occured while trying fetching node $nodeID" );
$this->Script->shutdown( 3 );
}
$searchEngine = new eZSolr();

if (
$subTree = $node->subTree(
array(
'Offset' => $offset,
'Limit' => $limit,
'SortBy' => array(),
'Limitation' => array(),
'MainNodeOnly' => true
)
)
)
{
foreach ( $subTree as $innerNode )
{
$object = $innerNode->attribute( 'object' );
if ( !$object )
{
continue;
}

//eZSearch::removeObject( $object );
//pass false as we are going to do a commit at the end
$result = $searchEngine->addObject( $object, false, $this->commitWithin * 1000 );
if ( !$result )
{
$this->CLI->error( ' Failed indexing ' . $object->attribute('class_identifier') . ' object with ID ' . $object->attribute( 'id' ) );
}
++$count;
}
}
// Get PHP executable from user.
$this->getPHPExecutable();

$this->CLI->output( $count );
$this->Script->shutdown( 0 );
$this->runMain();
}


/**
* Get PHP executable from user input. Exit if php executable cannot be
* found and if no executable is entered.
Expand Down Expand Up @@ -448,12 +371,13 @@ protected function iterate( $count = false )
*/
protected function forkAndExecute( $nodeID, $offset, $limit )
{
eZDB::setInstance( null );

$pid = pcntl_fork();
$db = eZDB::instance();
$db->IsConnected = false;
$db = null;
eZDB::setInstance( $db );

// reinitialize DB after fork
$this->initializeDB();

if ( $pid == -1 )
{
die( 'could not fork' );
Expand All @@ -466,8 +390,14 @@ protected function forkAndExecute( $nodeID, $offset, $limit )
else
{
// We are the child process
$this->execute( $nodeID, $offset, $limit, true );
$this->Script->shutdown( 0 );
if ( $this->execute( $nodeID, $offset, $limit ) > 0 )
{
$this->Script->shutdown( 0 );
}
else
{
$this->Script->shutdown( 3 );
}
}
}

Expand All @@ -477,59 +407,51 @@ protected function forkAndExecute( $nodeID, $offset, $limit )
* @param int $nodeID
* @param int $offset
* @param int $limit
* @param bool $isSubProcess.
*
* @return int Number of objects indexed.
*/
protected function execute( $nodeID, $offset, $limit, $isSubProcess = false )
protected function execute( $nodeID, $offset, $limit )
{
// Create options string.
$paramString = '';
$paramList = array( 'db-host', 'db-user', 'db-password', 'db-type', 'db-driver', 'db-database' );
foreach ( $paramList as $param )
{
if ( !empty( $this->Options[$param] ) )
{
$paramString .= ' --' . $param . '=' . escapeshellarg( $this->Options[$param] );
}
}

if ( $this->Options['siteaccess'] )
$count = 0;
$node = eZContentObjectTreeNode::fetch( $nodeID );
if ( !( $node instanceof eZContentObjectTreeNode ) )
{
$paramString .= ' -s ' . escapeshellarg( $this->Options['siteaccess'] );
$this->CLI->error( "An error occured while trying fetching node $nodeID" );
return 0;
}
$searchEngine = new eZSolr();

$paramString .=
' --commit-within=' . $this->commitWithin .
' --limit=' . $limit .
' --offset=' . $offset .
' --topNodeID=' . $nodeID;

$output = array();
$command = $this->Executable . ' ' . $this->executedScript . $paramString;
exec( $command, $output );
// wtf code follows, but leave it here commented for future enhancements
// if ( $isSubProcess )
// {
// exec( $command, $output );
// }
// else
// {
// exec( $command, $output );
// }

if ( !empty( $output ) )
if (
$subTree = $node->subTree(
array(
'Offset' => $offset,
'Limit' => $limit,
'SortBy' => array(),
'Limitation' => array(),
'MainNodeOnly' => true
)
)
)
{
$num = array_pop( $output );
if ( is_numeric( $num ) )
foreach ( $subTree as $innerNode )
{
return $num;
$object = $innerNode->attribute( 'object' );
if ( !$object )
{
continue;
}

//pass false as we are going to do a commit at the end
$result = $searchEngine->addObject( $object, false, $this->commitWithin * 1000 );
if ( !$result )
{
$this->CLI->error( ' Failed indexing ' . $object->attribute('class_identifier') . ' object with ID ' . $object->attribute( 'id' ) );
}
++$count;
}
}

$this->CLI->error( "\n" . 'Did not index content correctly: ' . "\n" . var_export( $output, 1 ) );

return 0;
return $count;
}

/**
Expand Down

0 comments on commit 9431458

Please sign in to comment.