<commit_before><?php
namespace vxWeb;

use vxPHP\Orm\Custom\Article;
use vxPHP\File\Exception\FilesystemFileException;
use vxPHP\File\Exception\MetaFolderException;
use vxPHP\File\MetaFile;
use vxPHP\File\FilesystemFolder;
use vxPHP\File\MetaFolder;
use vxPHP\File\FilesystemFile;
use vxPHP\File\UploadedFile;
use vxPHP\Http\Request;
use vxPHP\Application\Application;

/**
 * this class contains some static methods used in various controllers of vxWeb
 *
 * @author <NAME>
 *
 */
class FileUtil {

	/**
	 * add metafolder entries for filesystem subfolders
	 * add metafile entries for filesystem files
	 * remove metafile entries for missing file system files
	 *
	 * @param MetaFolder $metaFolder the folder which will be cleaned up
	 * @return void
	 */
	public static function cleanupMetaFolder(MetaFolder $metaFolder) {

		$application = Application::getInstance();

		$nestingInfo = $metaFolder->getNestingInformation();

		$db = $application->getDb();
		$fileRoot = $application->getAbsoluteAssetsPath();

		$mFolders = $db->doPreparedQuery('
			SELECT foldersID, Path, l, r FROM folders f WHERE l > ? AND r < ? AND f.level = ?
			', array(
				$nestingInfo['l'],
				$nestingInfo['r'],
				$nestingInfo['level'] + 1
		));

		$db->getConnection()->beginTransaction();

		foreach($mFolders as $d) {
			if(!is_dir($fileRoot . $d['Path'])) {

				$l = $d['l'];
				$r = $d['r'];
				$rl = $r - $l + 1;

				// delete "potential" subdirectories

				$db->execute('DELETE FROM folders WHERE l >= ? AND r <= ?', array((int) $l, (int) $r));

				// update nesting

				$db->execute('UPDATE folders SET r = r - ? WHERE r > ?', array((int) $rl, (int) $l));
			}
		}

		$db->getConnection()->commit();

		// create metafolder entries for filesystemfolders

		foreach($metaFolder->getFilesystemFolder()->getFolders() as $d) {
			$d->createMetaFolder();
		}

		$mFiles = $db->doPreparedQuery('SELECT filesID, IFNULL(Obscured_Filename, File) AS Filename FROM files f WHERE f.foldersID = ?', array((int) $metaFolder->getId()));
		$existing = array();

		// delete orphaned metafile entries

		foreach($mFiles as $f) {
			if(! file_exists($metaFolder->getFullPath() . $f['Filename'])) {
				$db->deleteRecord('files', $f['filesID']);
			}
			else {
				$existing[] = $f['Filename'];
			}
		}<commit_msg>Updated method calls to beginTransaction() and commit()<commit_after><?php
namespace vxWeb;

use vxPHP\Orm\Custom\Article;
use vxPHP\File\Exception\FilesystemFileException;
use vxPHP\File\Exception\MetaFolderException;
use vxPHP\File\MetaFile;
use vxPHP\File\FilesystemFolder;
use vxPHP\File\MetaFolder;
use vxPHP\File\FilesystemFile;
use vxPHP\File\UploadedFile;
use vxPHP\Http\Request;
use vxPHP\Application\Application;

/**
 * this class contains some static methods used in various controllers of vxWeb
 *
 * @author <NAME>
 *
 */
class FileUtil {

	/**
	 * add metafolder entries for filesystem subfolders
	 * add metafile entries for filesystem files
	 * remove metafile entries for missing file system files
	 *
	 * @param MetaFolder $metaFolder the folder which will be cleaned up
	 * @return void
	 */
	public static function cleanupMetaFolder(MetaFolder $metaFolder) {

		$application = Application::getInstance();

		$nestingInfo = $metaFolder->getNestingInformation();

		$db = $application->getDb();
		$fileRoot = $application->getAbsoluteAssetsPath();

		$mFolders = $db->doPreparedQuery('
			SELECT foldersID, Path, l, r FROM folders f WHERE l > ? AND r < ? AND f.level = ?
			', array(
				$nestingInfo['l'],
				$nestingInfo['r'],
				$nestingInfo['level'] + 1
		));

		$db->beginTransaction();

		foreach($mFolders as $d) {
			if(!is_dir($fileRoot . $d['Path'])) {

				$l = $d['l'];
				$r = $d['r'];
				$rl = $r - $l + 1;

				// delete "potential" subdirectories

				$db->execute('DELETE FROM folders WHERE l >= ? AND r <= ?', array((int) $l, (int) $r));

				// update nesting

				$db->execute('UPDATE folders SET r = r - ? WHERE r > ?', array((int) $rl, (int) $l));
			}
		}

		$db->commit();

		// create metafolder entries for filesystemfolders

		foreach($metaFolder->getFilesystemFolder()->getFolders() as $d) {
			$d->createMetaFolder();
		}

		$mFiles = $db->doPreparedQuery('SELECT filesID, IFNULL(Obscured_Filename, File) AS Filename FROM files f WHERE f.foldersID = ?', array((int) $metaFolder->getId()));
		$existing = array();

		// delete orphaned metafile entries

		foreach($mFiles as $f) {
			if(! file_exists($metaFolder->getFullPath() . $f['Filename'])) {
				$db->deleteRecord('files', $f['filesID']);
			}
			else {
				$existing[] = $f['Filename'];
			}
		}
{'fraction_non_alphanumeric': 0.15739130434782608, 'fraction_numerical': 0.0008695652173913044, 'mean_word_length': 3.941997851772288, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 43, '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': '4089727', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1492, 'n_words': 380}