<?php

class XenForo_Install_Upgrade_1020010 extends XenForo_Install_Upgrade_Abstract
{
	public function getVersionName()
	{
		return '1.2.0 Alpha';
	}

	public function step1()
	{
		$tables = XenForo_Install_Data_MySql::getTables();

		$this->executeUpgradeQuery('
			DROP TABLE IF EXISTS xf_permission_cache_content_type
		');
		$this->executeUpgradeQuery('
			DROP TABLE IF EXISTS xf_permission_cache_global_group
		');

		$this->executeUpgradeQuery($tables['xf_deferred']);
		$this->executeUpgradeQuery($tables['xf_draft']);
		$this->executeUpgradeQuery($tables['xf_edit_history']);
		$this->executeUpgradeQuery($tables['xf_forum_watch']);
		$this->executeUpgradeQuery($tables['xf_registration_spam_cache']);
		$this->executeUpgradeQuery($tables['xf_content_spam_cache']);
		$this->executeUpgradeQuery($tables['xf_bb_code_parse_cache']);
		$this->executeUpgradeQuery($tables['xf_route_filter']);
		$this->executeUpgradeQuery($tables['xf_mail_queue']);
		
		$this->executeUpgradeQuery($tables['xf_template_history']);
		$this->executeUpgradeQuery($tables['xf_template_modification']);
		$this->executeUpgradeQuery($tables['xf_template_modification_log']);
		$this->executeUpgradeQuery($tables['xf_admin_template_modification']);
		$this->executeUpgradeQuery($tables['xf_admin_template_modification_log']);
		$this->executeUpgradeQuery($tables['xf_email_template_modification']);
		$this->executeUpgradeQuery($tables['xf_email_template_modification_log']);

		return true;
	}

	public function step2()
	{
		$this->applyGlobalPermission('general', 'editProfile');
		$this->applyGlobalPermission('forum', 'editOwnThreadTitle', 'forum', 'editOwnPost', false);
		$this->applyGlobalPermission('signature', 'basicText', 'general', 'editSignature', false);
		$this->applyGlobalPermission('signature', 'extendedText', 'general', 'editSignature', false);
		$this->applyGlobalPermission('signature', 'align', 'general', 'editSignature', false);
		$this->applyGlobalPermission('signature', 'list', 'general', 'editSignature', false);
		$this->applyGlobalPermission('signature', 'image', 'general', 'editSignature', false);
		$this->applyGlobalPermission('signature', 'link', 'general', 'editSignature', false);
		$this->applyGlobalPermission('signature', 'basicText', 'general', 'editSignature', false);
		$this->applyGlobalPermission('signature', 'media', 'general', 'editSignature', false);
		$this->applyGlobalPermission('signature', 'block', 'general', 'editSignature', false);
		$this->applyGlobalPermissionInt('signature', 'maxPrintable', -1, 'general', 'editSignature');
		$this->applyGlobalPermissionInt('signature', 'maxLines', -1, 'general', 'editSignature');
		$this->applyGlobalPermissionInt('signature', 'maxLinks', -1, 'general', 'editSignature');
		$this->applyGlobalPermissionInt('signature', 'maxImages', -1, 'general', 'editSignature');
		$this->applyGlobalPermissionInt('signature', 'maxSmilies', -1, 'general', 'editSignature');
		$this->applyGlobalPermissionInt('signature', 'maxTextSize', -1, 'general', 'editSignature');
		$this->applyGlobalPermissionInt('general', 'maxTaggedUsers', 5, 'forum', 'postReply');

		$this->executeUpgradeQuery("
			INSERT INTO xf_content_type
				(content_type, addon_id, fields)
			VALUES
				('page',  'XenForo', '')
		");

		$this->executeUpgradeQuery("
			INSERT IGNORE INTO xf_content_type_field
				(content_type, field_name, field_value)
			VALUES
				('post', 'edit_history_handler_class', 'XenForo_EditHistoryHandler_Post'),
				('page', 'search_handler_class', 'XenForo_Search_DataHandler_Page'),
				('conversation', 'spam_handler_class', 'XenForo_SpamHandler_Conversation')
		");

		return true;
	}

	public function step3()
	{
		$this->executeUpgradeQuery("
			ALTER TABLE `xf_session_activity` ADD `robot_key` VARBINARY(25) NOT NULL DEFAULT ''
		");

		$this->executeUpgradeQuery("
			ALTER TABLE  `xf_template`
				ADD `disable_modifications` TINYINT UNSIGNED NOT NULL DEFAULT  '0',
				ADD  `last_edit_date` INT UNSIGNED NOT NULL DEFAULT  '0'
		");

		$this->executeUpgradeQuery("
			ALTER TABLE xf_ban_email CHANGE banned_email banned_email VARCHAR(120) NOT NULL
		");

		$this->executeUpgradeQuery("
			ALTER TABLE  `xf_user_group`
				ADD  `banner_css_class` VARCHAR( 75 ) NOT NULL DEFAULT  '',
				ADD  `banner_text` VARCHAR( 100 ) NOT NULL DEFAULT  ''
		");

		return true;
	}

	public function step4()
	{
		$this->executeUpgradeQuery("
			ALTER TABLE `xf_node` ADD `breadcrumb_data` BLOB NULL DEFAULT NULL
		");

		$this->executeUpgradeQuery("
			ALTER TABLE xf_forum
				ADD require_prefix TINYINT UNSIGNED NOT NULL DEFAULT '0',
				ADD allowed_watch_notifications VARCHAR(10) NOT NULL DEFAULT 'all',
				ADD default_sort_order VARCHAR(25) NOT NULL DEFAULT 'last_post_date',
				ADD default_sort_direction VARCHAR(5) NOT NULL DEFAULT 'desc'
		");

		$this->executeUpgradeQuery("
			ALTER TABLE xf_code_event_listener ADD hint VARCHAR(255) NOT NULL DEFAULT ''
		");

		$this->executeUpgradeQuery("
			ALTER TABLE xf_option
				CHANGE edit_format edit_format ENUM('textbox','spinbox','onoff','radio','select','checkbox','template','callback','onofftextbox') NOT NULL
		");

		return true;
	}

	public function step5()
	{
		$this->executeUpgradeQuery("
			ALTER TABLE  `xf_report` ADD  `report_count` INT UNSIGNED NOT NULL DEFAULT  '0'
		");
		$this->executeUpgradeQuery("
			ALTER TABLE `xf_report_comment` ADD  `is_report` TINYINT UNSIGNED NOT NULL DEFAULT  '1'
		");

		$ids = $this->_getDb()->fetchCol("(SELECT user_id FROM xf_moderator) UNION (SELECT user_id FROM xf_admin)");
		if ($ids)
		{
			$this->executeUpgradeQuery("
				UPDATE xf_report_comment SET is_report = 0 WHERE user_id IN (" . $this->_getDb()->quote($ids) . ")
			");
		}

		return true;
	}

	public function step6()
	{
		$this->executeUpgradeQuery("
			ALTER TABLE `xf_liked_content` ADD INDEX `like_user_content_type_id` (`like_user_id`, `content_type`, `content_id`)
		");

		return true;
	}

	public function step7()
	{
		$this->executeUpgradeQuery("
			ALTER TABLE  `xf_user`
				ADD `is_staff` TINYINT UNSIGNED NOT NULL DEFAULT  '0',
				ADD INDEX message_count (  `message_count` ),
				ADD INDEX trophy_points (  `trophy_points` ),
				ADD INDEX like_count (  `like_count` ),
				ADD INDEX register_date (  `register_date` ),
				ADD INDEX `staff_username` (  `is_staff` ,  `username` )
		");

		return true;
	}

	public function step8()
	{
		$this->executeUpgradeQuery("
			ALTER TABLE `xf_user_profile` ADD INDEX  `dob` (  `dob_month` ,  `dob_day` ,  `dob_year` )
		");
		$this->executeUpgradeQuery("
			ALTER TABLE xf_user_option ADD enable_flash_uploader TINYINT UNSIGNED NOT NULL DEFAULT 1
		");

		$this->executeUpgradeQuery("
			UPDATE xf_moderator AS moderator
			INNER JOIN xf_user AS user ON (moderator.user_id = user.user_id)
			SET user.is_staff = 1
		");

		return true;
	}

	public function step9()
	{
		$this->executeUpgradeQuery("
			UPDATE xf_user_upgrade
			SET description = REPLACE(REPLACE(description, '\r', ''), '\n', '<br />\n')
		");

		$this->executeUpgradeQuery("
			ALTER TABLE xf_user_upgrade_expired
				ADD extra MEDIUMBLOB NOT NULL,
				ADD original_end_date INT UNSIGNED NOT NULL DEFAULT 0
		");

		$this->executeUpgradeQuery("
			ALTER TABLE xf_user_upgrade_log
				ADD subscriber_id VARCHAR(50) NOT NULL DEFAULT '',
				ADD INDEX subscriber_id (subscriber_id)
		");

		return true;
	}

	public function step10()
	{
		$this->executeUpgradeQuery("
			ALTER TABLE xf_conversation_master
				ADD recipients mediumblob NOT NULL,
				ADD INDEX user_id (user_id)
		");

		$this->executeUpgradeQuery("
			ALTER TABLE xf_conversation_user
				ADD is_starred TINYINT UNSIGNED NOT NULL DEFAULT '0',
				ADD INDEX owner_starred_date (owner_user_id, is_starred, last_message_date)
		");

		return true;
	}

	public function step11()
	{
		$this->executeUpgradeQuery('
			ALTER TABLE xf_post
				ADD last_edit_date INT UNSIGNED NOT NULL DEFAULT 0,
				ADD last_edit_user_id INT UNSIGNED NOT NULL DEFAULT 0,
				ADD edit_count INT UNSIGNED NOT NULL DEFAULT 0
		');

		return true;
	}

	public function step12()
	{
		$this->executeUpgradeQuery("
			ALTER TABLE `xf_ip` ADD INDEX log_date (`log_date`)
		");

		$this->executeUpgradeQuery("
			INSERT IGNORE INTO xf_option
				(option_id, option_value, default_value, edit_format, edit_format_params, data_type, sub_options,
				can_backup, validation_class, validation_method, addon_id)
			VALUES
				('ipLogCleanUp', 'a:2:{s:7:\"enabled\";b:0;s:5:\"delay\";b:0;}', '', '', '', '', '',
				1, '', '', 'XenForo')
		");

		return true;
	}

	public function step13()
	{
		$this->executeUpgradeQuery("
			ALTER TABLE `xf_thread`
				DROP INDEX node_id_sticky_last_post_date,
				ADD INDEX  `node_id_sticky_state_last_post` (  `node_id` ,  `sticky` ,  `discussion_state` , `last_post_date` )
		");

		return true;
	}
}