<commit_before><?php

use ChurchCRM\Service\SystemService;
use ChurchCRM\Slim\Middleware\AdminRoleAuthMiddleware;

// Routes

$app->group('/database', function () {
    $this->post('/backup', function ($request, $response, $args) {
        $input = (object) $request->getParsedBody();
        $backup = $this->SystemService->getDatabaseBackup($input);
        echo json_encode($backup);
    });

    $this->post('/backupRemote', function() use ($app, $systemService) {
        $backup = $this->SystemService->copyBackupToExternalStorage();
        echo json_encode($backup);
    });

    $this->post('/restore', function ($request, $response, $args) {

      if ( $_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST) &&
            empty($_FILES) && $_SERVER['CONTENT_LENGTH'] > 0 )
        {
          $systemService = new SystemService();
          throw new \Exception(gettext('The selected file exceeds this servers maximum upload size of').": ". $systemService->getMaxUploadFileSize()  , 500);
        }
        $fileName = $_FILES['restoreFile'];
        $restore = $this->SystemService->restoreDatabaseFromBackup($fileName);
        echo json_encode($restore);
    });

    $this->get('/download/{filename}', function ($request, $response, $args) {
        $filename = $args['filename'];
        $this->SystemService->download($filename);
    });
})->add(new AdminRoleAuthMiddleware());<commit_msg> new api to drop all table/views from db (#3851)<commit_after><?php

use Slim\Http\Request;
use Slim\Http\Response;
use Propel\Runtime\Propel;
use ChurchCRM\Utils\LoggerUtils;
use ChurchCRM\Service\SystemService;
use ChurchCRM\Slim\Middleware\AdminRoleAuthMiddleware;


// Routes

$app->group('/database', function () {
    
    $this->post('/reset', 'resetDatabase');
    
    $this->post('/backup', function ($request, $response, $args) {
        $input = (object) $request->getParsedBody();
        $backup = $this->SystemService->getDatabaseBackup($input);
        echo json_encode($backup);
    });

    $this->post('/backupRemote', function() use ($app, $systemService) {
        $backup = $this->SystemService->copyBackupToExternalStorage();
        echo json_encode($backup);
    });

    $this->post('/restore', function ($request, $response, $args) {

      if ( $_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST) &&
            empty($_FILES) && $_SERVER['CONTENT_LENGTH'] > 0 )
        {
          $systemService = new SystemService();
          throw new \Exception(gettext('The selected file exceeds this servers maximum upload size of').": ". $systemService->getMaxUploadFileSize()  , 500);
        }
        $fileName = $_FILES['restoreFile'];
        $restore = $this->SystemService->restoreDatabaseFromBackup($fileName);
        echo json_encode($restore);
    });

    $this->get('/download/{filename}', function ($request, $response, $args) {
        $filename = $args['filename'];
        $this->SystemService->download($filename);
    });
})->add(new AdminRoleAuthMiddleware());


/**
 * A method that drops all db tables 
 *
 * @param \Slim\Http\Request $p_request The request.
 * @param \Slim\Http\Response $p_response The response.
 * @param array $p_args Arguments
 * @return \Slim\Http\Response The augmented response.
 */
function resetDatabase(Request $request, Response $response, array $p_args)
{
    $connection = Propel::getConnection();
    $logger = LoggerUtils::getAppLogger();

    $logger->info("DB Drop started ");

    $statement = $connection->prepare("SHOW FULL TABLES;");
    $statement->execute();
    $dbTablesSQLs = $statement->fetchAll();

    foreach ($dbTablesSQLs as $dbTable) {
        if ($dbTable[1] == "VIEW") {
            $alterSQL = "DROP VIEW " . $dbTable[0] . " ;";
        } else {
            $alterSQL = "DROP TABLE " . $dbTable[0] . " ;";
        }
    
        $dbAlterStatement = $connection->exec($alterSQL);
        $logger->info("DB Update: " . $alterSQL . " done.");
    }
    
  return $response->withJson(['success' => true, 'msg' => gettext('The database has been cleared.')]);
}
{'fraction_non_alphanumeric': 0.17216748768472906, 'fraction_numerical': 0.003694581280788177, 'mean_word_length': 3.3619763694951663, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1525088', 'n_tokens_mistral': 1188, 'n_tokens_neox': 1176, 'n_words': 244}