<commit_before><?php
	// Installation/upgrade file	
	define('VERSION', 'v0.9.4');
	
	require 'inc/functions.php';
	require 'inc/display.php';
	require 'inc/template.php';
	require 'inc/database.php';
	require 'inc/user.php';
	$step = isset($_GET['step']) ? round($_GET['step']) : 0;
	$page = Array(
		'config' => $config,
		'title' => 'Install',
		'body' => '',
		'nojavascript' => true
	);
	
	// this breaks the dispaly of licenses if enabled
	$config['minify_html'] = false;
	
	if(file_exists($config['has_installed'])) {
		
		// Check the version number
		$version = trim(file_get_contents($config['has_installed']));
		if(empty($version))
			$version = 'v0.9.1';
		
		$boards = listBoards();
		
		switch($version) {
			case 'v0.9':
			case 'v0.9.1':
				// Upgrade to v0.9.2-dev
				
				foreach($boards as &$_board) {
					// Add `capcode` field after `trip`
					query(sprintf("ALTER TABLE `posts_%s` ADD  `capcode` VARCHAR( 50 ) NULL AFTER  `trip`", $_board['uri'])) or error(db_error());
					
					// Resize `trip` to 15 characters
					query(sprintf("ALTER TABLE `posts_%s` CHANGE  `trip`  `trip` VARCHAR( 15 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL", $_board['uri'])) or error(db_error());
				}
			case 'v0.9.2-dev':
				// Upgrade to v0.9.2-dev-1
				
				// New table: `theme_settings`
				query("CREATE TABLE IF NOT EXISTS `theme_settings` ( `name` varchar(40) NOT NULL, `value` text, UNIQUE KEY `name` (`name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;") or error(db_error());
				
				// New table: `news`
				query("CREATE TABLE IF NOT EXISTS `news` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` text NOT NULL, `time` int(11) NOT NULL, `subject` text NOT NULL, `body` text NOT NULL, UNIQUE KEY `id` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;") or error(db_error());
			case 'v0.9.2.1-dev':
			case 'v0.9.2-dev-1':
				// Fix broken version number/mistake
				$version = 'v0.9.2-dev-1';
				// Upgrade to v0.9.2-dev-2
				
				foreach($boards as &$_board) {
					// Increase field sizes
					query(sprintf("ALTER TABLE `posts_%s` CHANGE  `subject` `subject` VARCHAR( 50 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL", $_board['uri'])) or error(db_error());
					query(sprintf("ALTER TABLE `posts_%s` CHANGE  `name` `name` VARCHAR( 35 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL", $_board['uri'])) or error(db_error());
				}
			case 'v0.9.2-dev-2':
				// Upgrade to v0.9.2-dev-3 (v0.9.2)
				
				foreach($boards as &$_board) {
					// Add `custom_fields` field
					query(sprintf("ALTER TABLE `posts_%s` ADD `embed` TEXT NULL", $_board['uri'])) or error(db_error());
				}
			case 'v0.9.2-dev-3': // v0.9.2-dev-3 == v0.9.2
			case 'v0.9.2':
				// Upgrade to v0.9.3-dev-1
				
				// Upgrade `theme_settings` table
				query("TRUNCATE TABLE `theme_settings`") or error(db_error());
				query("ALTER TABLE  `theme_settings` ADD  `theme` VARCHAR( 40 ) NOT NULL FIRST") or error(db_error());
				query("ALTER TABLE  `theme_settings` CHANGE  `name`  `name` VARCHAR( 40 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL") or error(db_error());
				query("ALTER TABLE  `theme_settings` DROP INDEX  `name`") or error(db_error());
			case 'v0.9.3-dev-1':
				query("ALTER TABLE  `mods` ADD  `boards` TEXT NOT NULL") or error(db_error());
				query("UPDATE `mods` SET `boards` = '*'") or error(db_error());
			case 'v0.9.3-dev-2':
				foreach($boards as &$_board) {
					query(sprintf("ALTER TABLE `posts_%s` CHANGE `filehash`  `filehash` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL", $_board['uri'])) or error(db_error());
				}
			case 'v0.9.3-dev-3':
				// Board-specifc bans
				query("ALTER TABLE `bans` ADD  `board` SMALLINT NULL AFTER  `reason`") or error(db_error());
			case 'v0.9.3-dev-4':
				// add ban ID
				query("ALTER TABLE `bans` ADD  `id` INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY ( `id` ), ADD UNIQUE (`id`)");
			case 'v0.9.3-dev-5':
				foreach($boards as &$_board) {
					// Increase subject field size
					query(sprintf("ALTER TABLE `posts_%s` CHANGE  `subject` `subject` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL", $_board['uri'])) or error(db_error());
				}
			case 'v0.9.3-dev-6':
				// change to MyISAM
				$tables = Array(
					'bans', 'boards', 'ip_notes', 'modlogs', 'mods', 'mutes', 'noticeboard', 'pms', 'reports', 'robot', 'theme_settings', 'news'
				);
				foreach($boards as &$board) {
					$tables[] = "posts_{$board['uri']}";
				}
				
				foreach($tables as &$table) {
					query("ALTER TABLE  `{$table}` ENGINE = MYISAM DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci") or error(db_error());
				}
			case 'v0.9.3-dev-7':
				foreach($boards as &$board) {
					query(sprintf("ALTER TABLE  `posts_%s` CHANGE  `filename` `filename` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL", $board['uri'])) or error(db_error());
				}
			case 'v0.9.3-dev-8':
				foreach($boards as &$board) {
					query(sprintf("ALTER TABLE `posts_%s` ADD INDEX (  `thread` )", $board['uri'])) or error(db_error());
				}
			case 'v0.9.3-dev-9':
				foreach($boards as &$board) {
					query(sprintf("ALTER TABLE `posts_%s`ADD INDEX (  `time` )", $board['uri'])) or error(db_error());
					query(sprintf("ALTER TABLE `posts_%s`ADD FULLTEXT (`body`)", $board['uri'])) or error(db_error());
				}
			case 'v0.9.3-dev-10':
			case 'v0.9.3':
				query("ALTER TABLE  `bans` DROP INDEX `id`") or error(db_error());
				query("ALTER TABLE  `pms` DROP INDEX  `id`") or error(db_error());
				query("ALTER TABLE  `boards` DROP PRIMARY KEY") or error(db_error());
				query("ALTER TABLE  `reports` DROP INDEX  `id`") or error(db_error());
				query("ALTER TABLE  `boards` DROP INDEX `uri`") or error(db_error());
				
				query("ALTER IGNORE TABLE  `robot` ADD PRIMARY KEY (`hash`)") or error(db_error());
				query("ALTER TABLE  `bans` ADD FULLTEXT (`ip`)") or error(db_error());
				query("ALTER TABLE  `ip_notes` ADD INDEX (`ip`)") or error(db_error());				
				query("ALTER TABLE  `modlogs` ADD INDEX (`time`)") or error(db_error());
				query("ALTER TABLE  `boards` ADD PRIMARY KEY(`uri`)") or error(db_error());
				query("ALTER TABLE  `mutes` ADD INDEX (`ip`)") or error(db_error());
				query("ALTER TABLE  `news` ADD INDEX (`time`)") or error(db_error());
				query("ALTER TABLE  `theme_settings` ADD INDEX (`theme`)") or error(db_error());
			case 'v0.9.4-dev-1':
				foreach($boards as &$board) {
					query(sprintf("ALTER TABLE  `posts_%s` ADD  `sage` INT( 1 ) NOT NULL AFTER  `locked`", $board['uri'])) or error(db_error());
				}
			case 'v0.9.4-dev-2':
				if(!isset($_GET['confirm'])) {
					$page['title'] = 'License Change';
					$page['body'] = '<p style="text-align:center">You are upgrading to a version which uses an amended license. The licenses included with Tinyboard distributions prior to this version (v0.9.4-dev-2) are still valid for those versions, but no longer apply to this and newer versions.</p>' .
						'<textarea style="width:700px;height:370px;margin:auto;display:block;background:white;color:black" disabled>' . htmlentities(file_get_contents('LICENSE.md')) . '</textarea>
						<p style="text-align:center">
							<a href="?confirm=1">I have read and understood the agreement. Proceed to upgrading.</a>
						</p>';
					break;
				}
			case 'v0.9.4-dev-3':
			case 'v0.9.4-dev-4':
				// nothing
			case false:
				// Update version number
				file_write($config['has_installed'], VERSION);
				
				$page['title'] = 'Upgraded';
				$page['body'] = '<p style="text-align:center">Successfully upgraded from ' . $version . ' to <strong>' . VERSION . '</strong>.</p>';
				break;
			default:
				$page['title'] = 'Unknown version';
				$page['body'] = '<p style="text-align:center">Tinyboard was unable to determine what version is currently installed.</p>';
				break;
			case VERSION:
				$page['title'] = 'Already installed';<commit_msg>last minute bug-fix with upgrading<commit_after><?php
	// Installation/upgrade file	
	define('VERSION', 'v0.9.4');
	
	require 'inc/functions.php';
	require 'inc/display.php';
	require 'inc/template.php';
	require 'inc/database.php';
	require 'inc/user.php';
	$step = isset($_GET['step']) ? round($_GET['step']) : 0;
	$page = Array(
		'config' => $config,
		'title' => 'Install',
		'body' => '',
		'nojavascript' => true
	);
	
	// this breaks the dispaly of licenses if enabled
	$config['minify_html'] = false;
	
	if(file_exists($config['has_installed'])) {
		
		// Check the version number
		$version = trim(file_get_contents($config['has_installed']));
		if(empty($version))
			$version = 'v0.9.1';
		
		$boards = listBoards();
		
		switch($version) {
			case 'v0.9':
			case 'v0.9.1':
				// Upgrade to v0.9.2-dev
				
				foreach($boards as &$_board) {
					// Add `capcode` field after `trip`
					query(sprintf("ALTER TABLE `posts_%s` ADD  `capcode` VARCHAR( 50 ) NULL AFTER  `trip`", $_board['uri'])) or error(db_error());
					
					// Resize `trip` to 15 characters
					query(sprintf("ALTER TABLE `posts_%s` CHANGE  `trip`  `trip` VARCHAR( 15 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL", $_board['uri'])) or error(db_error());
				}
			case 'v0.9.2-dev':
				// Upgrade to v0.9.2-dev-1
				
				// New table: `theme_settings`
				query("CREATE TABLE IF NOT EXISTS `theme_settings` ( `name` varchar(40) NOT NULL, `value` text, UNIQUE KEY `name` (`name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;") or error(db_error());
				
				// New table: `news`
				query("CREATE TABLE IF NOT EXISTS `news` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` text NOT NULL, `time` int(11) NOT NULL, `subject` text NOT NULL, `body` text NOT NULL, UNIQUE KEY `id` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;") or error(db_error());
			case 'v0.9.2.1-dev':
			case 'v0.9.2-dev-1':
				// Fix broken version number/mistake
				$version = 'v0.9.2-dev-1';
				// Upgrade to v0.9.2-dev-2
				
				foreach($boards as &$_board) {
					// Increase field sizes
					query(sprintf("ALTER TABLE `posts_%s` CHANGE  `subject` `subject` VARCHAR( 50 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL", $_board['uri'])) or error(db_error());
					query(sprintf("ALTER TABLE `posts_%s` CHANGE  `name` `name` VARCHAR( 35 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL", $_board['uri'])) or error(db_error());
				}
			case 'v0.9.2-dev-2':
				// Upgrade to v0.9.2-dev-3 (v0.9.2)
				
				foreach($boards as &$_board) {
					// Add `custom_fields` field
					query(sprintf("ALTER TABLE `posts_%s` ADD `embed` TEXT NULL", $_board['uri'])) or error(db_error());
				}
			case 'v0.9.2-dev-3': // v0.9.2-dev-3 == v0.9.2
			case 'v0.9.2':
				// Upgrade to v0.9.3-dev-1
				
				// Upgrade `theme_settings` table
				query("TRUNCATE TABLE `theme_settings`") or error(db_error());
				query("ALTER TABLE  `theme_settings` ADD  `theme` VARCHAR( 40 ) NOT NULL FIRST") or error(db_error());
				query("ALTER TABLE  `theme_settings` CHANGE  `name`  `name` VARCHAR( 40 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL") or error(db_error());
				query("ALTER TABLE  `theme_settings` DROP INDEX  `name`") or error(db_error());
			case 'v0.9.3-dev-1':
				query("ALTER TABLE  `mods` ADD  `boards` TEXT NOT NULL") or error(db_error());
				query("UPDATE `mods` SET `boards` = '*'") or error(db_error());
			case 'v0.9.3-dev-2':
				foreach($boards as &$_board) {
					query(sprintf("ALTER TABLE `posts_%s` CHANGE `filehash`  `filehash` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL", $_board['uri'])) or error(db_error());
				}
			case 'v0.9.3-dev-3':
				// Board-specifc bans
				query("ALTER TABLE `bans` ADD  `board` SMALLINT NULL AFTER  `reason`") or error(db_error());
			case 'v0.9.3-dev-4':
				// add ban ID
				query("ALTER TABLE `bans` ADD  `id` INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY ( `id` ), ADD UNIQUE (`id`)");
			case 'v0.9.3-dev-5':
				foreach($boards as &$_board) {
					// Increase subject field size
					query(sprintf("ALTER TABLE `posts_%s` CHANGE  `subject` `subject` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL", $_board['uri'])) or error(db_error());
				}
			case 'v0.9.3-dev-6':
				// change to MyISAM
				$tables = Array(
					'bans', 'boards', 'ip_notes', 'modlogs', 'mods', 'mutes', 'noticeboard', 'pms', 'reports', 'robot', 'theme_settings', 'news'
				);
				foreach($boards as &$board) {
					$tables[] = "posts_{$board['uri']}";
				}
				
				foreach($tables as &$table) {
					query("ALTER TABLE  `{$table}` ENGINE = MYISAM DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci") or error(db_error());
				}
			case 'v0.9.3-dev-7':
				foreach($boards as &$board) {
					query(sprintf("ALTER TABLE  `posts_%s` CHANGE  `filename` `filename` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL", $board['uri'])) or error(db_error());
				}
			case 'v0.9.3-dev-8':
				foreach($boards as &$board) {
					query(sprintf("ALTER TABLE `posts_%s` ADD INDEX (  `thread` )", $board['uri'])) or error(db_error());
				}
			case 'v0.9.3-dev-9':
				foreach($boards as &$board) {
					query(sprintf("ALTER TABLE `posts_%s`ADD INDEX (  `time` )", $board['uri'])) or error(db_error());
					query(sprintf("ALTER TABLE `posts_%s`ADD FULLTEXT (`body`)", $board['uri'])) or error(db_error());
				}
			case 'v0.9.3-dev-10':
			case 'v0.9.3':
				query("ALTER TABLE  `bans` DROP INDEX `id`");
				query("ALTER TABLE  `pms` DROP INDEX  `id`");
				query("ALTER TABLE  `boards` DROP PRIMARY KEY");
				query("ALTER TABLE  `reports` DROP INDEX  `id`");
				query("ALTER TABLE  `boards` DROP INDEX `uri`");
				
				query("ALTER IGNORE TABLE  `robot` ADD PRIMARY KEY (`hash`)");
				query("ALTER TABLE  `bans` ADD FULLTEXT (`ip`)");
				query("ALTER TABLE  `ip_notes` ADD INDEX (`ip`)");	
				query("ALTER TABLE  `modlogs` ADD INDEX (`time`)");
				query("ALTER TABLE  `boards` ADD PRIMARY KEY(`uri`)");
				query("ALTER TABLE  `mutes` ADD INDEX (`ip`)");
				query("ALTER TABLE  `news` ADD INDEX (`time`)");
				query("ALTER TABLE  `theme_settings` ADD INDEX (`theme`)");
			case 'v0.9.4-dev-1':
				foreach($boards as &$board) {
					query(sprintf("ALTER TABLE  `posts_%s` ADD  `sage` INT( 1 ) NOT NULL AFTER  `locked`", $board['uri'])) or error(db_error());
				}
			case 'v0.9.4-dev-2':
				if(!isset($_GET['confirm'])) {
					$page['title'] = 'License Change';
					$page['body'] = '<p style="text-align:center">You are upgrading to a version which uses an amended license. The licenses included with Tinyboard distributions prior to this version (v0.9.4-dev-2) are still valid for those versions, but no longer apply to this and newer versions.</p>' .
						'<textarea style="width:700px;height:370px;margin:auto;display:block;background:white;color:black" disabled>' . htmlentities(file_get_contents('LICENSE.md')) . '</textarea>
						<p style="text-align:center">
							<a href="?confirm=1">I have read and understood the agreement. Proceed to upgrading.</a>
						</p>';
					break;
				}
			case 'v0.9.4-dev-3':
			case 'v0.9.4-dev-4':
				// nothing
			case false:
				// Update version number
				file_write($config['has_installed'], VERSION);
				
				$page['title'] = 'Upgraded';
				$page['body'] = '<p style="text-align:center">Successfully upgraded from ' . $version . ' to <strong>' . VERSION . '</strong>.</p>';
				break;
			default:
				$page['title'] = 'Unknown version';
				$page['body'] = '<p style="text-align:center">Tinyboard was unable to determine what version is currently installed.</p>';
				break;
			case VERSION:
				$page['title'] = 'Already installed';
{'fraction_non_alphanumeric': 0.17780483116550266, 'fraction_numerical': 0.023194720317806113, 'mean_word_length': 3.600058944886531, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4278576', 'n_tokens_mistral': 6635, 'n_tokens_neox': 5543, 'n_words': 1469}