<commit_before><?php
/* Copyright (c) 2013 Apycat / Association France-ioi, MIT License http://opensource.org/licenses/MIT */

require_once __DIR__."/../modelsManager/modelsManager.php";
require_once __DIR__."/../modelsManager/modelsManagerVersions.php";
require_once __DIR__."/../../shared/models.php";

if (file_exists( __DIR__."/../../shared/debug.php")) {
   include_once __DIR__."/../../shared/debug.php"; // not required
} else {
   function syncDebug($type, $b_or_e, $subtype='') {}
}


function syncUpdateVersions($db, $lastServerVersion) {
   $query = "UPDATE `synchro_version` SET `iLastServerVersion` = :lastServerVersion, `iLastClientVersion` = `iVersion`";
   $stmt = $db->prepare($query);
   $stmt->execute(array("lastServerVersion" => $lastServerVersion));
}

function syncGetVersions($db) {
   $query = "SELECT * FROM `synchro_version`";
   $stmt = $db->query($query);
   return $stmt->fetchObject();
}

function syncIncrementVersion($db) {
   $query = "UPDATE `synchro_version` SET `iVersion` = `iVersion` + 1";
   $db->exec($query);
}

function createViewModelFromTable($tableName) {
   global $viewsModels;
   $viewModel = null;
   $tableModel = getTableModel($tableName);
   if (isset($viewsModels) && isset($viewsModels[$tableName])) {
      $viewModel = $viewsModels[$tableName];
      $viewModel["fields"] = $tableModel["fields"];
   } else {
      $viewModel = array("mainTable" => $tableName, "joins" => array(), "fields" => $tableModel["fields"], "filters" => array());
   }
   return $viewModel;

}

function syncGetTablesRequests($tables = null, $useCount = true) {
   global $tablesModels;
   $requests = array();
   foreach ($tablesModels as $tableName => $tableModel) {
      if (isset($tableModel["hasHistory"]) && (!$tableModel["hasHistory"])) {
         continue;
      }
      if (($tables != null) && (!isset($tables[$tableName]))) {
         continue;
      }
      $viewModel = createViewModelFromTable($tableName);
      $requests[$tableName] = array(
         "modelName" => $tableName,
         "model" => $viewModel,
         "fields" => getViewModelFieldsList($viewModel),
         "filters"  => array(),
         "countRows" => $useCount
      );
   }
   return $requests;
}

function syncGetCounts($db, $requests, $minVersion, $maxVersion, $maxVersionIsDefault) {
   $allCounts = array();
   foreach ($requests as $requestName => $request) {
      if (isset($request["countRows"]) && !$request["countRows"]) {
         continue;
      }
      $curMinVersion = $minVersion;
      if (isset($request["minVersion"])) {
         $curMinVersion = $request["minVersion"];
      }
      $requestCounts = getChangesCountSince($db, $request, $curMinVersion, $maxVersion, $maxVersionIsDefault);
      $allCounts[$requestName] = $requestCounts;
   }
   return $allCounts;
}

function runBeforeSelectListeners($db, $requests) {
   $allJoinsUsed = array();
   foreach ($requests as $requestName => $request) {
      $allJoinsUsed = getJoinsUsed($request, "read", "select");
   }
   foreach ($allJoinsUsed as $tableName => $isUsed) {
      callTableListener($db, $tableName, "before");
   }
}

function syncGetChanges($db, $requests, $minVersion, $maxVersion, $maxChanges, $maxVersionIsDefault) {
   $allChanges = array("requestSets" => array());
   $nbChanges = 0;
   foreach ($requests as $requestName => $request) {
      if (!$request || !is_array($request)) {
         error_log('something is wrong with request '.print_r($request, true));
         continue;
      }
      syncDebug('getChanges', 'begin', $requestName);

      $curMinVersion = $minVersion;
      if (isset($request["minVersion"])) {
         $curMinVersion = $request["minVersion"];
      }
      $markRequest = isset($request["markRequest"]) ? $request["markRequest"] : false;
      if (!isset($request["getChanges"]) || $request["getChanges"]) {
         $requestChanges = getChangesSince($db, $request, $curMinVersion, $maxVersion, $requestName, $markRequest, $maxVersionIsDefault);
      } else {<commit_msg>add write only requests<commit_after><?php
/* Copyright (c) 2013 Apycat / Association France-ioi, MIT License http://opensource.org/licenses/MIT */

require_once __DIR__."/../modelsManager/modelsManager.php";
require_once __DIR__."/../modelsManager/modelsManagerVersions.php";
require_once __DIR__."/../../shared/models.php";

if (file_exists( __DIR__."/../../shared/debug.php")) {
   include_once __DIR__."/../../shared/debug.php"; // not required
} else {
   function syncDebug($type, $b_or_e, $subtype='') {}
}


function syncUpdateVersions($db, $lastServerVersion) {
   $query = "UPDATE `synchro_version` SET `iLastServerVersion` = :lastServerVersion, `iLastClientVersion` = `iVersion`";
   $stmt = $db->prepare($query);
   $stmt->execute(array("lastServerVersion" => $lastServerVersion));
}

function syncGetVersions($db) {
   $query = "SELECT * FROM `synchro_version`";
   $stmt = $db->query($query);
   return $stmt->fetchObject();
}

function syncIncrementVersion($db) {
   $query = "UPDATE `synchro_version` SET `iVersion` = `iVersion` + 1";
   $db->exec($query);
}

function createViewModelFromTable($tableName) {
   global $viewsModels;
   $viewModel = null;
   $tableModel = getTableModel($tableName);
   if (isset($viewsModels) && isset($viewsModels[$tableName])) {
      $viewModel = $viewsModels[$tableName];
      $viewModel["fields"] = $tableModel["fields"];
   } else {
      $viewModel = array("mainTable" => $tableName, "joins" => array(), "fields" => $tableModel["fields"], "filters" => array());
   }
   return $viewModel;

}

function syncGetTablesRequests($tables = null, $useCount = true) {
   global $tablesModels;
   $requests = array();
   foreach ($tablesModels as $tableName => $tableModel) {
      if (isset($tableModel["hasHistory"]) && (!$tableModel["hasHistory"])) {
         continue;
      }
      if (($tables != null) && (!isset($tables[$tableName]))) {
         continue;
      }
      $viewModel = createViewModelFromTable($tableName);
      $requests[$tableName] = array(
         "modelName" => $tableName,
         "model" => $viewModel,
         "fields" => getViewModelFieldsList($viewModel),
         "filters"  => array(),
         "countRows" => $useCount
      );
   }
   return $requests;
}

function syncGetCounts($db, $requests, $minVersion, $maxVersion, $maxVersionIsDefault) {
   $allCounts = array();
   foreach ($requests as $requestName => $request) {
      if (isset($request["countRows"]) && !$request["countRows"]) {
         continue;
      }
      $curMinVersion = $minVersion;
      if (isset($request["minVersion"])) {
         $curMinVersion = $request["minVersion"];
      }
      $requestCounts = getChangesCountSince($db, $request, $curMinVersion, $maxVersion, $maxVersionIsDefault);
      $allCounts[$requestName] = $requestCounts;
   }
   return $allCounts;
}

function runBeforeSelectListeners($db, $requests) {
   $allJoinsUsed = array();
   foreach ($requests as $requestName => $request) {
      $allJoinsUsed = getJoinsUsed($request, "read", "select");
   }
   foreach ($allJoinsUsed as $tableName => $isUsed) {
      callTableListener($db, $tableName, "before");
   }
}

function syncGetChanges($db, $requests, $minVersion, $maxVersion, $maxChanges, $maxVersionIsDefault) {
   $allChanges = array("requestSets" => array());
   $nbChanges = 0;
   foreach ($requests as $requestName => $request) {
      if (!$request || !is_array($request)) {
         error_log('something is wrong with request '.print_r($request, true));
         continue;
      }
      if (isset($request['writeOnly']) && $request['writeOnly']) {
         continue;
      }
      syncDebug('getChanges', 'begin', $requestName);

      $curMinVersion = $minVersion;
      if (isset($request["minVersion"])) {
         $curMinVersion = $request["minVersion"];
      }
      $markRequest = isset($request["markRequest"]) ? $request["markRequest"] : false;
      if (!isset($request["getChanges"]) || $request["getChanges"]) {
         $requestChanges = getChangesSince($db, $request, $curMinVersion, $maxVersion, $requestName, $markRequest, $maxVersionIsDefault);
      } else {
{'fraction_non_alphanumeric': 0.17293510324483777, 'fraction_numerical': 0.0014749262536873156, 'mean_word_length': 3.9315151515151516, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3176120', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2475, 'n_words': 481}