$TODAY = date("Y-m-d");
$TODAY_TIMESTAMP = strtotime("{$TODAY} 00:00:00");
$REMOTE_FOLDER = 'download/xmldump';
$STATIC_FILES = file(Config::get('static.url') . 'fileList.txt');
$LAST_DUMP = getLastDumpDate($REMOTE_FOLDER);
$LAST_DUMP_TIMESTAMP = $LAST_DUMP ? strtotime("{$LAST_DUMP} 00:00:00") : null;
$USERS = getActiveUsers();
$FTP = new FtpUtil();
log_scriptLog("generating dump for {$TODAY}; previous dump is " . ($LAST_DUMP ? $LAST_DUMP : '-never-'));
if ($LAST_DUMP == $TODAY) {
    log_scriptLog("a dump already exists for {$TODAY}; aborting");
    die("a dump already exists for {$TODAY}; aborting\n");
}
dumpSources("{$REMOTE_FOLDER}/{$TODAY}-sources.xml.gz");
dumpInflections("{$REMOTE_FOLDER}/{$TODAY}-inflections.xml.gz");
dumpAbbrevs("{$REMOTE_FOLDER}/{$TODAY}-abbrevs.xml.gz");
dumpDefinitions("SELECT * FROM Definition WHERE sourceId IN (SELECT id FROM Source WHERE canDistribute) AND status = 0 AND modDate < {$TODAY_TIMESTAMP}", "{$REMOTE_FOLDER}/{$TODAY}-definitions.xml.gz", 'dumping definitions');
dumpLexems("SELECT * FROM Lexem where modDate < {$TODAY_TIMESTAMP}", "{$REMOTE_FOLDER}/{$TODAY}-lexems.xml.gz", 'dumping lexems and inflected forms');
dumpLdm("SELECT M.lexemId, M.definitionId FROM LexemDefinitionMap M, Definition D " . "WHERE D.id = M.definitionId AND D.sourceId in (SELECT id FROM Source WHERE canDistribute) " . "AND D.status = 0 AND M.modDate < {$TODAY_TIMESTAMP} ORDER BY M.lexemId, M.definitionId", "{$REMOTE_FOLDER}/{$TODAY}-ldm.xml.gz", 'dumping lexem-definition map');
if ($LAST_DUMP) {
    dumpDefinitions("SELECT * FROM Definition WHERE sourceId IN (SELECT id FROM Source WHERE canDistribute) " . "AND modDate >= {$LAST_DUMP_TIMESTAMP} AND modDate < {$TODAY_TIMESTAMP}", "{$REMOTE_FOLDER}/{$TODAY}-definitions-diff.xml.gz", 'dumping definitions diff');
    dumpLexems("SELECT * FROM Lexem where modDate >= {$LAST_DUMP_TIMESTAMP} AND modDate < {$TODAY_TIMESTAMP}", "{$REMOTE_FOLDER}/{$TODAY}-lexems-diff.xml.gz", 'dumping lexems and inflected forms diff');
    dumpLdmDiff("{$REMOTE_FOLDER}/{$LAST_DUMP}-ldm.xml.gz", "{$REMOTE_FOLDER}/{$TODAY}-ldm.xml.gz", "{$REMOTE_FOLDER}/{$TODAY}-ldm-diff.xml.gz");
}
removeOldDumps($REMOTE_FOLDER, $TODAY, $LAST_DUMP);
/**************************************************************************/
function getActiveUsers()
{
    $results = db_execute("SELECT id, nick FROM User WHERE id IN (SELECT DISTINCT userId FROM Definition)");
    $users = array();
    foreach ($results as $row) {
require_once "../phplib/util.php";
smarty_init();
$TODAY = date("Y-m-d");
$TODAY_TIMESTAMP = strtotime("{$TODAY} 00:00:00");
$FOLDER = util_getRootPath() . '/wwwbase/download/xmldump';
$LAST_DUMP = getLastDumpDate($FOLDER);
$LAST_DUMP_TIMESTAMP = $LAST_DUMP ? strtotime("{$LAST_DUMP} 00:00:00") : null;
$USERS = getActiveUsers();
log_scriptLog("generating dump for {$TODAY}; previous dump is " . ($LAST_DUMP ? $LAST_DUMP : '-never-'));
if ($LAST_DUMP == $TODAY) {
    log_scriptLog("a dump already exists for {$TODAY}; aborting");
    exit(1);
}
dumpSources("{$FOLDER}/{$TODAY}-sources.xml.gz");
dumpInflections("{$FOLDER}/{$TODAY}-inflections.xml.gz");
dumpAbbrevs("{$FOLDER}/{$TODAY}-abbrevs.xml.gz");
dumpDefinitions("SELECT * FROM Definition WHERE sourceId IN (SELECT id FROM Source WHERE canDistribute) AND status = 0 AND modDate < {$TODAY_TIMESTAMP}", "{$FOLDER}/{$TODAY}-definitions.xml.gz", 'dumping definitions');
dumpLexems("SELECT * FROM Lexem where modDate < {$TODAY_TIMESTAMP}", "{$FOLDER}/{$TODAY}-lexems.xml.gz", 'dumping lexems and inflected forms');
dumpLdm("SELECT M.lexemId, M.definitionId FROM LexemDefinitionMap M, Definition D " . "WHERE D.id = M.definitionId AND D.sourceId in (SELECT id FROM Source WHERE canDistribute) " . "AND D.status = 0 AND M.modDate < {$TODAY_TIMESTAMP} ORDER BY M.lexemId, M.definitionId", "{$FOLDER}/{$TODAY}-ldm.xml.gz", 'dumping lexem-definition map');
if ($LAST_DUMP) {
    dumpDefinitions("SELECT * FROM Definition WHERE sourceId IN (SELECT id FROM Source WHERE canDistribute) " . "AND modDate >= {$LAST_DUMP_TIMESTAMP} AND modDate < {$TODAY_TIMESTAMP}", "{$FOLDER}/{$TODAY}-definitions-diff.xml.gz", 'dumping definitions diff');
    dumpLexems("SELECT * FROM Lexem where modDate >= {$LAST_DUMP_TIMESTAMP} AND modDate < {$TODAY_TIMESTAMP}", "{$FOLDER}/{$TODAY}-lexems-diff.xml.gz", 'dumping lexems and inflected forms diff');
    dumpLdmDiff("{$FOLDER}/{$LAST_DUMP}-ldm.xml.gz", "{$FOLDER}/{$TODAY}-ldm.xml.gz", "{$FOLDER}/{$TODAY}-ldm-diff.xml.gz");
}
removeOldDumps($FOLDER, $TODAY, $LAST_DUMP);
/**************************************************************************/
function getActiveUsers()
{
    $results = db_execute("SELECT id, nick FROM User WHERE id IN (SELECT DISTINCT userId FROM Definition)");
    $users = array();
    foreach ($results as $row) {