From 87110937a45bd9a94a6ed74456a8b8889d66bace Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= <rubencm@gmail.com>
Date: Thu, 7 Jun 2018 14:56:08 +0200
Subject: [PATCH] [ticket/15663] Add migration

PHPBB3-15663
---
 phpBB/install/schemas/schema_data.sql         |  3 -
 .../data/v330/remove_attachment_flash.php     | 87 +++++++++++++++++++
 2 files changed, 87 insertions(+), 3 deletions(-)
 create mode 100644 phpBB/phpbb/db/migration/data/v330/remove_attachment_flash.php

diff --git a/phpBB/install/schemas/schema_data.sql b/phpBB/install/schemas/schema_data.sql
index a3af830817..e5eebae36d 100644
--- a/phpBB/install/schemas/schema_data.sql
+++ b/phpBB/install/schemas/schema_data.sql
@@ -739,7 +739,6 @@ INSERT INTO phpbb_extension_groups (group_name, cat_id, allow_group, upload_icon
 INSERT INTO phpbb_extension_groups (group_name, cat_id, allow_group, upload_icon, max_filesize, allowed_forums) VALUES ('ARCHIVES', 0, 1, '', 0, '');
 INSERT INTO phpbb_extension_groups (group_name, cat_id, allow_group, upload_icon, max_filesize, allowed_forums) VALUES ('PLAIN_TEXT', 0, 0, '', 0, '');
 INSERT INTO phpbb_extension_groups (group_name, cat_id, allow_group, upload_icon, max_filesize, allowed_forums) VALUES ('DOCUMENTS', 0, 0, '', 0, '');
-INSERT INTO phpbb_extension_groups (group_name, cat_id, allow_group, upload_icon, max_filesize, allowed_forums) VALUES ('FLASH_FILES', 5, 0, '', 0, '');
 INSERT INTO phpbb_extension_groups (group_name, cat_id, allow_group, upload_icon, max_filesize, allowed_forums) VALUES ('DOWNLOADABLE_FILES', 0, 0, '', 0, '');
 
 # -- extensions
@@ -796,8 +795,6 @@ INSERT INTO phpbb_extensions (group_id, extension) VALUES (4, 'ods');
 INSERT INTO phpbb_extensions (group_id, extension) VALUES (4, 'odt');
 INSERT INTO phpbb_extensions (group_id, extension) VALUES (4, 'rtf');
 
-INSERT INTO phpbb_extensions (group_id, extension) VALUES (5, 'swf');
-
 INSERT INTO phpbb_extensions (group_id, extension) VALUES (6, 'mp3');
 INSERT INTO phpbb_extensions (group_id, extension) VALUES (6, 'mpeg');
 INSERT INTO phpbb_extensions (group_id, extension) VALUES (6, 'mpg');
diff --git a/phpBB/phpbb/db/migration/data/v330/remove_attachment_flash.php b/phpBB/phpbb/db/migration/data/v330/remove_attachment_flash.php
new file mode 100644
index 0000000000..2150b5c1c9
--- /dev/null
+++ b/phpBB/phpbb/db/migration/data/v330/remove_attachment_flash.php
@@ -0,0 +1,87 @@
+<?php
+/**
+*
+* This file is part of the phpBB Forum Software package.
+*
+* @copyright (c) phpBB Limited <https://www.phpbb.com>
+* @license GNU General Public License, version 2 (GPL-2.0)
+*
+* For full copyright and license information, please see
+* the docs/CREDITS.txt file.
+*
+*/
+
+namespace phpbb\db\migration\data\v330;
+
+
+class remove_attachment_flash extends \phpbb\db\migration\migration
+{
+	// Following constants were deprecated in 3.3
+	// and moved from constants.php to compatibility_globals.php,
+	// thus define them as class constants
+	const ATTACHMENT_CATEGORY_FLASH = 5;
+
+	protected $cat_id = array(
+			self::ATTACHMENT_CATEGORY_FLASH,
+		);
+
+	public function update_data()
+	{
+		return array(
+			array('custom', array(array($this, 'remove_flash_group'))),
+		);
+	}
+
+	public function remove_flash_group()
+	{
+		// select group ids of outdated media
+		$sql = 'SELECT group_id
+			FROM ' . EXTENSION_GROUPS_TABLE . '
+			WHERE ' . $this->db->sql_in_set('cat_id', $this->cat_id);
+		$result = $this->db->sql_query($sql);
+
+		$group_ids = array();
+		while ($group_id = (int) $this->db->sql_fetchfield('group_id'))
+		{
+			$group_ids[] = $group_id;
+		}
+		$this->db->sql_freeresult($result);
+
+		// nothing to do, admin has removed all the outdated media extension groups
+		if (empty($group_ids))
+		{
+			return true;
+		}
+
+		// get the group id of downloadable files
+		$sql = 'SELECT group_id
+			FROM ' . EXTENSION_GROUPS_TABLE . "
+			WHERE group_name = 'DOWNLOADABLE_FILES'";
+		$result = $this->db->sql_query($sql);
+		$download_id = (int) $this->db->sql_fetchfield('group_id');
+		$this->db->sql_freeresult($result);
+
+		if (empty($download_id))
+		{
+			$sql = 'UPDATE ' . EXTENSIONS_TABLE . '
+				SET group_id = 0
+				WHERE ' . $this->db->sql_in_set('group_id', $group_ids);
+		}
+		else
+		{
+			// move outdated media extensions to downloadable files
+			$sql = 'UPDATE ' . EXTENSIONS_TABLE . "
+				SET group_id = $download_id" . '
+				WHERE ' . $this->db->sql_in_set('group_id', $group_ids);
+		}
+
+		$result = $this->db->sql_query($sql);
+		$this->db->sql_freeresult($result);
+
+		// delete the now empty, outdated media extension groups
+		$sql = 'DELETE FROM ' . EXTENSION_GROUPS_TABLE . '
+			WHERE ' . $this->db->sql_in_set('group_id', $group_ids);
+		$result = $this->db->sql_query($sql);
+		$this->db->sql_freeresult($result);
+	}
+}