/**
  * @todo optimize db query: use a prepared statement
  */
 public function checkFiles($doDelete = false, $returnData = false)
 {
     $violations = array();
     $dir = $this->clusterizeDir(eZSys::storageDirectory() . '/original');
     if (!is_dir($dir)) {
         return $violations;
     }
     $dir = realpath($dir);
     foreach (glob($dir . '/*', GLOB_ONLYDIR) as $storageDir) {
         if (in_array(basename($storageDir), array('.', '..'))) {
             continue;
         }
         foreach (glob($storageDir . '/*') as $storageFile) {
             if (!is_file($storageFile)) {
                 continue;
             }
             $fileName = basename($storageFile);
             $dirName = basename($storageDir);
             $sql1 = "SELECT COUNT(*) AS found FROM ezbinaryfile " . "WHERE filename = '" . $this->db->escapeString($fileName) . "' AND mime_type LIKE '" . $this->db->escapeString($dirName) . "/%'";
             $sql2 = "SELECT COUNT(*) AS found FROM ezmedia " . "WHERE filename = '" . $this->db->escapeString($fileName) . "' AND mime_type LIKE '" . $this->db->escapeString($dirName) . "/%'";
             $results1 = $this->db->arrayQuery($sql1);
             $results2 = $this->db->arrayQuery($sql2);
             if ($results1[0]['found'] == 0 && $results2[0]['found'] == 0) {
                 if (isset($violations['violatingFileCount'])) {
                     $violations['violatingFileCount']++;
                 } else {
                     $violations['violatingFileCount'] = 1;
                 }
                 if ($returnData) {
                     $violations['violatingFiles'][] = $storageFile;
                 }
                 if ($doDelete) {
                     unlink($storageFile);
                 }
             }
             /*else
               {
                   echo "OK: $storageFile\n";
               }*/
         }
     }
     return $violations;
 }
 public function checkCustomQuery($sql)
 {
     return $this->db->arrayQuery($sql);
 }
/**
 * @param eZCLI $cli
 * @param eZDBInterface $db
 * @param string $cronPart
 * @param int $maxRetries
 * @return int offset to use or -1 if an error occures
 */
function computeOffsetByFork ( $cli, $db, $cronPart, $maxRetries )
{
    // Normal case
    if (!preg_match('#^[a-zA-Z_]+(\d)#', $cronPart, $match))
    {
        return 0;
    }

    $ezfindIni   = eZINI::instance('ezfind.ini');
    $maxForks    = $ezfindIni->variable('IndexOptions', 'MaxPendingForkCount');
    $matchOffset = $match[1] - 1;

    if ( $matchOffset > $maxForks )
    {
        $cli->output( "Ini setting states you can't run more than $maxForks forks, you try to run fork #" . ($matchOffset + 1) );
        return -1;
    }


    $countQuery  = "SELECT count(id) as nb_pending FROM ezpending_actions WHERE action = 'index_object' AND ( param_int IS NULL OR param_int <= $maxRetries )";

    $result = $db->arrayQuery($countQuery);
    if ( !$result || !isset($result[0]['nb_pending']))
    {
        $cli->error("Mysql unexpected error. Script will be stopped.");
        return -1;
    }

    $forkInterval = $ezfindIni->variable('IndexOptions', 'MinPendingForkInterval');
    $pendingCount = $result[0]['nb_pending'];
    $forkMinCount = $matchOffset * $forkInterval;

    if ($pendingCount < $forkMinCount)
    {
        $cli->warning( "Trying to run pending fork #$matchOffset but we have less than $forkMinCount pending objects. Script will be stopped.");

        return -1;
    }

    $offset = max($forkMinCount, round($pendingCount/$maxForks) * $matchOffset);

    $cli->output( "Fork #" . ($matchOffset + 1) . " starting at offset #$offset");

    return $offset;
}