<commit_before><?php
$FROM_APP = defined('RUNNING_FROM_APP'); //Are we running from app or framework?

#Arguments
$argv = $_SERVER['argv'];
$count = count($argv);

#Usage strings
$usage = "Usage: php {$argv[0]} " . ($FROM_APP ? '' : '<app_root> '). '[--all] [-su options [-su options] ... ]';

if(!$FROM_APP) {
	$usage.=PHP_EOL.PHP_EOL.'<app_root> must be a path to the application root directory';
}

$usage = <<<EOT
$usage

Options:

--all Will install plugins listed in Q/app/plugins in \$APP_DIR/config/app.json
  All connections/schemas listed in Q/pluginInfo and Q/appInfo will be installed also.

--noapp Will skip installation of the app. Can be used to install/update plugins.

-p \$PLUGIN_NAME Install/update plugin

-s \$CONN_NAME
  This will execute app's install/upgrade sql scripts for connections \$CONN_NAME.
  Note: running SQL statements may require elevated privileges.

-su \$CONN_NAME \$USER_NAME
  This will execute apps's install/upgrade sql scripts for connections \$CONN_NAME,
  using the provided username and will prompt for a password.
  Note: running SQL statements may require elevated privileges.

--group \$GROUP
  Set filesystem group (by name or id) to the \$GROUP,
  accessible by http server and file permisions to 0664/0775
  If no --group parameter specified file permisions are set to 0666/0777
  Note: may require elevated privileges.

--deep
  Make recursive check of filemode and group. Can be used when 'files' folder is not empty

--noreq
  Skip requirements checking (useful when installing 2 interdependent plugins)

--trace
  Print stacktraces on errors 

EOT;

$help = <<<EOT
Script to install an app.

1) Install required plugins by running plugin.php script (or run this script with --all after step 2)
2) Customize the database config in your \$local dir to add the necessary DB connections
3) Run this script.

$usage

EOT;

#Is it a call for help?
if (isset($argv[1]) and in_array($argv[1], array('--help', '/?', '-h', '-?', '/h')))
	die($help);

#Check primary arguments count: 1 if running /app/scripts/Q/install.php, 2 if running /framework/scripts/app.php
if ($count < ($FROM_APP ? 1 : 2))
	die($usage);

#Read primary arguments
$LOCAL_DIR = $FROM_APP ? APP_DIR : $argv[1];

#Check paths
if (!file_exists($Q_filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Q.inc.php')) #Q Platform
	die("[ERROR] $Q_filename not found" . PHP_EOL);

if (!is_dir($LOCAL_DIR)) #App dir
	die("[ERROR] $LOCAL_DIR doesn't exist or is not a directory" . PHP_EOL);

#Define APP_DIR
if (!defined('APP_DIR'))
	define('APP_DIR', $LOCAL_DIR);

#Include Q
try {
	$Q_installing = true;
	$Q_Bootstrap_config_plugin_limit = 1;
	include($Q_filename);
} catch (Exception $e) {
	die('[ERROR] ' . $e->getMessage() . PHP_EOL . $e->getTraceAsString() . PHP_EOL);
}

#Parse secondary arguments -sql, -sql-user-pass, -auto-install-prerequisites
$auto_plugins = $noapp = false;
$sql_array = $plugins = array();
$options = array(
	'filemode' => 0666,
	'dirmode' => 0777
);

$mode = '';
for ($i = ($FROM_APP ? 1 : 2); $i < $count; ++$i) {
	switch ($mode) {
		case 'sql':
			$sql_array[$argv[$i]] = array('enabled'=>true);
			$plugins[] = $argv[$i];
			$mode = '';
			break;
		case 'sql-user-pass':
			$sql_array[$argv[$i]] = array(
				'enabled'=>true,
				'username' => $argv[$i + 1],
				'password' => true //$argv[$i + 2]
			);
			$i = $i + 1;
			$mode = '';
			break;
		case 'plugin':
			$plugins[] = $argv[$i];
			$mode = '';
			break;
		case 'group':
			$options['group'] = $argv[$i];
			$options['filemode'] = 0664;
			$options['dirmode'] = 0775;
			$mode = '';
			break;
		case '':
			switch ($argv[$i]) {
				case '-s':
					if ($i + 1 > $count - 1) {
						echo "Not enough parameters to $argv[$i] option\n$usage";
						exit;
					}
					$mode = 'sql';
					break;
				case '-su':
				case '-sum':
					if ($i + 2 > $count - 1) {
						echo "Not enough parameters to $argv[$i] option\n$usage";
						exit;
					}
					$mode = 'sql-user-pass';
					break;
				case '--trace':
					$trace = true;
					break;
				case '--noreq':
					$options['noreq'] = true;
					break;
				case '--deep':
					$options['deep'] = true;
					break;
				case '-all':
				case '--all':
					$auto_plugins = true;
					break;
				case '-p':
					if ($i + 1 > $count - 1) {
						echo "Not enough parameters to $argv[$i] option\n$usage";
						exit;
					}
					$mode = 'plugin';
					break;
				case '--group':
					if (isset($options['group'])) {
						echo "You can specify $argv[$i] option only once\n$usage";
						exit;
					}
					if ($i + 1 > $count - 1) {
						echo "Not enough parameters to $argv[$i] option\n$usage";
						exit;
					}
					$mode = 'group';
					break;
				case '--noapp':
					$noapp = true;
					break;
				case '--noinit':
					$noInit = true;
					break;
			}
			break;
	}
}

$options['sql'] = $sql_array;

echo 'Q Platform app installer'.PHP_EOL;

$app = Q_Config::expect('Q', 'app');
$uploads_dir = APP_FILES_DIR.DS.$app.DS.'uploads';
if (is_dir($uploads_dir)) {
	if (file_exists(APP_WEB_DIR.DS.'uploads')) {
		unlink(APP_WEB_DIR.DS.'uploads');
	}
	Q_Utils::symlink("../files/$app/uploads", APP_WEB_DIR.DS.'uploads');
}

if ($auto_plugins) {
	$plugins = Q_Config::get('Q', 'plugins', array());
	if (!in_array("Q", $plugins)) {
		array_unshift($plugins, "Q");
	}
}

Q_Plugin::checkPermissions(APP_FILES_DIR, array_merge($options, array('deep' => true)));

foreach ($plugins as $plugin) {
	$cons = Q_Config::get('Q', 'pluginInfo', $plugin, 'connections', array());
	foreach ($cons as $con) {
		if (empty($options['sql'][$con])) {
			$options['sql'][$con] = array('enabled' => true);
		}
	}
	Q_Plugin::installPlugin($plugin, $options);
	++$Q_Bootstrap_config_plugin_limit;
	Q_Bootstrap::configure(true);
}

if (!$noapp) {
	// if application is installed/updated, it's schema is always installed/updated
	$cons = Q_Config::get('Q', 'appInfo', 'connections', array());
	foreach ($cons as $con)
		if (empty($options['sql'][$con]))
			$options['sql'][$con] = array('enabled' => true);

	Q_Plugin::installApp($options);
	if (empty($noInit) && file_exists($LOCAL_DIR.DS.'scripts'.DS.'init.php')) {
		echo 'Running initialization script'.PHP_EOL;
		include($LOCAL_DIR.DS.'scripts'.DS.'init.php');
	}
}
<commit_msg>Fix symlinks for Win systems.<commit_after><?php
$FROM_APP = defined('RUNNING_FROM_APP'); //Are we running from app or framework?

#Arguments
$argv = $_SERVER['argv'];
$count = count($argv);

#Usage strings
$usage = "Usage: php {$argv[0]} " . ($FROM_APP ? '' : '<app_root> '). '[--all] [-su options [-su options] ... ]';

if(!$FROM_APP) {
	$usage.=PHP_EOL.PHP_EOL.'<app_root> must be a path to the application root directory';
}

$usage = <<<EOT
$usage

Options:

--all Will install plugins listed in Q/app/plugins in \$APP_DIR/config/app.json
  All connections/schemas listed in Q/pluginInfo and Q/appInfo will be installed also.

--noapp Will skip installation of the app. Can be used to install/update plugins.

-p \$PLUGIN_NAME Install/update plugin

-s \$CONN_NAME
  This will execute app's install/upgrade sql scripts for connections \$CONN_NAME.
  Note: running SQL statements may require elevated privileges.

-su \$CONN_NAME \$USER_NAME
  This will execute apps's install/upgrade sql scripts for connections \$CONN_NAME,
  using the provided username and will prompt for a password.
  Note: running SQL statements may require elevated privileges.

--group \$GROUP
  Set filesystem group (by name or id) to the \$GROUP,
  accessible by http server and file permisions to 0664/0775
  If no --group parameter specified file permisions are set to 0666/0777
  Note: may require elevated privileges.

--deep
  Make recursive check of filemode and group. Can be used when 'files' folder is not empty

--noreq
  Skip requirements checking (useful when installing 2 interdependent plugins)

--trace
  Print stacktraces on errors 

EOT;

$help = <<<EOT
Script to install an app.

1) Install required plugins by running plugin.php script (or run this script with --all after step 2)
2) Customize the database config in your \$local dir to add the necessary DB connections
3) Run this script.

$usage

EOT;

#Is it a call for help?
if (isset($argv[1]) and in_array($argv[1], array('--help', '/?', '-h', '-?', '/h')))
	die($help);

#Check primary arguments count: 1 if running /app/scripts/Q/install.php, 2 if running /framework/scripts/app.php
if ($count < ($FROM_APP ? 1 : 2))
	die($usage);

#Read primary arguments
$LOCAL_DIR = $FROM_APP ? APP_DIR : $argv[1];

#Check paths
if (!file_exists($Q_filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Q.inc.php')) #Q Platform
	die("[ERROR] $Q_filename not found" . PHP_EOL);

if (!is_dir($LOCAL_DIR)) #App dir
	die("[ERROR] $LOCAL_DIR doesn't exist or is not a directory" . PHP_EOL);

#Define APP_DIR
if (!defined('APP_DIR'))
	define('APP_DIR', $LOCAL_DIR);

#Include Q
try {
	$Q_installing = true;
	$Q_Bootstrap_config_plugin_limit = 1;
	include($Q_filename);
} catch (Exception $e) {
	die('[ERROR] ' . $e->getMessage() . PHP_EOL . $e->getTraceAsString() . PHP_EOL);
}

#Parse secondary arguments -sql, -sql-user-pass, -auto-install-prerequisites
$auto_plugins = $noapp = false;
$sql_array = $plugins = array();
$options = array(
	'filemode' => 0666,
	'dirmode' => 0777
);

$mode = '';
for ($i = ($FROM_APP ? 1 : 2); $i < $count; ++$i) {
	switch ($mode) {
		case 'sql':
			$sql_array[$argv[$i]] = array('enabled'=>true);
			$plugins[] = $argv[$i];
			$mode = '';
			break;
		case 'sql-user-pass':
			$sql_array[$argv[$i]] = array(
				'enabled'=>true,
				'username' => $argv[$i + 1],
				'password' => true //$argv[$i + 2]
			);
			$i = $i + 1;
			$mode = '';
			break;
		case 'plugin':
			$plugins[] = $argv[$i];
			$mode = '';
			break;
		case 'group':
			$options['group'] = $argv[$i];
			$options['filemode'] = 0664;
			$options['dirmode'] = 0775;
			$mode = '';
			break;
		case '':
			switch ($argv[$i]) {
				case '-s':
					if ($i + 1 > $count - 1) {
						echo "Not enough parameters to $argv[$i] option\n$usage";
						exit;
					}
					$mode = 'sql';
					break;
				case '-su':
				case '-sum':
					if ($i + 2 > $count - 1) {
						echo "Not enough parameters to $argv[$i] option\n$usage";
						exit;
					}
					$mode = 'sql-user-pass';
					break;
				case '--trace':
					$trace = true;
					break;
				case '--noreq':
					$options['noreq'] = true;
					break;
				case '--deep':
					$options['deep'] = true;
					break;
				case '-all':
				case '--all':
					$auto_plugins = true;
					break;
				case '-p':
					if ($i + 1 > $count - 1) {
						echo "Not enough parameters to $argv[$i] option\n$usage";
						exit;
					}
					$mode = 'plugin';
					break;
				case '--group':
					if (isset($options['group'])) {
						echo "You can specify $argv[$i] option only once\n$usage";
						exit;
					}
					if ($i + 1 > $count - 1) {
						echo "Not enough parameters to $argv[$i] option\n$usage";
						exit;
					}
					$mode = 'group';
					break;
				case '--noapp':
					$noapp = true;
					break;
				case '--noinit':
					$noInit = true;
					break;
			}
			break;
	}
}

$options['sql'] = $sql_array;
$is_win = (substr(strtolower(PHP_OS), 0, 3) === 'win');

echo 'Q Platform app installer'.PHP_EOL;

$app = Q_Config::expect('Q', 'app');
$uploads_dir = APP_FILES_DIR.DS.$app.DS.'uploads';
if (is_dir($uploads_dir)) {
	if (file_exists(APP_WEB_DIR.DS.'uploads')) {
		unlink(APP_WEB_DIR.DS.'uploads');
	}

	// related path only for non Win OS. Win os don't support links with related path
	if(!$is_win) $uploads_dir =  "../files/$app/uploads";
	Q_Utils::symlink($uploads_dir, APP_WEB_DIR.DS.'uploads');
}

if ($auto_plugins) {
	$plugins = Q_Config::get('Q', 'plugins', array());
	if (!in_array("Q", $plugins)) {
		array_unshift($plugins, "Q");
	}
}

Q_Plugin::checkPermissions(APP_FILES_DIR, array_merge($options, array('deep' => true)));

foreach ($plugins as $plugin) {
	$cons = Q_Config::get('Q', 'pluginInfo', $plugin, 'connections', array());
	foreach ($cons as $con) {
		if (empty($options['sql'][$con])) {
			$options['sql'][$con] = array('enabled' => true);
		}
	}
	Q_Plugin::installPlugin($plugin, $options);
	++$Q_Bootstrap_config_plugin_limit;
	Q_Bootstrap::configure(true);
}

if (!$noapp) {
	// if application is installed/updated, it's schema is always installed/updated
	$cons = Q_Config::get('Q', 'appInfo', 'connections', array());
	foreach ($cons as $con)
		if (empty($options['sql'][$con]))
			$options['sql'][$con] = array('enabled' => true);

	Q_Plugin::installApp($options);
	if (empty($noInit) && file_exists($LOCAL_DIR.DS.'scripts'.DS.'init.php')) {
		echo 'Running initialization script'.PHP_EOL;
		include($LOCAL_DIR.DS.'scripts'.DS.'init.php');
	}
}

{'fraction_non_alphanumeric': 0.17546485975417586, 'fraction_numerical': 0.009454774661203908, 'mean_word_length': 3.5138691322901847, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 37, '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': '4299205', 'n_tokens_mistral': 5138, 'n_tokens_neox': 4415, 'n_words': 1261}