From 190301021bc85ea4226c1347e1a1209c95997e6f Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 6 Feb 2014 11:18:59 +0100 Subject: [PATCH] [ticket/12115] Use base class to reduce duplicate code PHPBB3-12115 --- .../data/v310/profilefield_interests.php | 147 +++-------------- .../data/v310/profilefield_occupation.php | 147 +++-------------- .../migration/profilefield_base_migration.php | 155 ++++++++++++++++++ 3 files changed, 203 insertions(+), 246 deletions(-) create mode 100644 phpBB/phpbb/db/migration/profilefield_base_migration.php diff --git a/phpBB/phpbb/db/migration/data/v310/profilefield_interests.php b/phpBB/phpbb/db/migration/data/v310/profilefield_interests.php index 77e23fcd17..d73bc78edb 100644 --- a/phpBB/phpbb/db/migration/data/v310/profilefield_interests.php +++ b/phpBB/phpbb/db/migration/data/v310/profilefield_interests.php @@ -9,13 +9,8 @@ namespace phpbb\db\migration\data\v310; -class profilefield_interests extends \phpbb\db\migration\migration +class profilefield_interests extends \phpbb\db\migration\profilefield_base_migration { - public function effectively_installed() - { - return $this->db_tools->sql_column_exists($this->table_prefix . 'profile_fields_data', 'pf_phpbb_interests'); - } - static public function depends_on() { return array( @@ -23,124 +18,30 @@ class profilefield_interests extends \phpbb\db\migration\migration ); } - public function update_schema() - { - return array( - 'change_columns' => array( - $this->table_prefix . 'profile_fields_data' => array( - 'pf_phpbb_interests' => array('MTEXT', ''), - ), - ), - ); - } + protected $profilefield_name = 'phpbb_interests'; - public function update_data() - { - return array( - array('custom', array(array($this, 'create_interests_custom_field'))), - array('custom', array(array($this, 'convert_interests_to_custom_field'))), - ); - } + protected $profilefield_database_type = array('MTEXT', ''); - public function create_interests_custom_field() - { - $sql = 'SELECT MAX(field_order) as max_field_order - FROM ' . PROFILE_FIELDS_TABLE; - $result = $this->db->sql_query($sql); - $max_field_order = (int) $this->db->sql_fetchfield('max_field_order'); - $this->db->sql_freeresult($result); + protected $profilefield_data = array( + 'field_name' => 'phpbb_interests', + 'field_type' => 'profilefields.type.text', + 'field_ident' => 'phpbb_interests', + 'field_length' => '3|30', + 'field_minlen' => '2', + 'field_maxlen' => '500', + 'field_novalue' => '', + 'field_default_value' => '', + 'field_validation' => '.*', + 'field_required' => 0, + 'field_show_novalue' => 0, + 'field_show_on_reg' => 0, + 'field_show_on_pm' => 0, + 'field_show_on_vt' => 0, + 'field_show_profile' => 1, + 'field_hide' => 0, + 'field_no_view' => 0, + 'field_active' => 1, + ); - $sql_ary = array( - 'field_name' => 'phpbb_interests', - 'field_type' => 'profilefields.type.text', - 'field_ident' => 'phpbb_interests', - 'field_length' => '3|30', - 'field_minlen' => '2', - 'field_maxlen' => '500', - 'field_novalue' => '', - 'field_default_value' => '', - 'field_validation' => '.*', - 'field_required' => 0, - 'field_show_novalue' => 0, - 'field_show_on_reg' => 0, - 'field_show_on_pm' => 0, - 'field_show_on_vt' => 0, - 'field_show_profile' => 1, - 'field_hide' => 0, - 'field_no_view' => 0, - 'field_active' => 1, - 'field_order' => $max_field_order + 1, - ); - - $sql = 'INSERT INTO ' . PROFILE_FIELDS_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary); - $this->db->sql_query($sql); - $field_id = (int) $this->db->sql_nextid(); - - $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, PROFILE_LANG_TABLE); - - $sql = 'SELECT lang_id - FROM ' . LANG_TABLE; - $result = $this->db->sql_query($sql); - while ($lang_id = (int) $this->db->sql_fetchfield('lang_id')) - { - $insert_buffer->insert(array( - 'field_id' => $field_id, - 'lang_id' => $lang_id, - 'lang_name' => 'INTERESTS', - 'lang_explain' => '', - 'lang_default_value' => '', - )); - } - $this->db->sql_freeresult($result); - - $insert_buffer->flush(); - } - - /** - * @param int $start Start of staggering step - * @return mixed int start of the next step, null if the end was reached - */ - public function convert_interests_to_custom_field($start) - { - $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->table_prefix . 'profile_fields_data'); - $limit = 250; - $converted_users = 0; - - $sql = 'SELECT user_id, user_interests - FROM ' . $this->table_prefix . "users - WHERE user_interests <> '' - ORDER BY user_id"; - $result = $this->db->sql_query_limit($sql, $limit, $start); - - while ($row = $this->db->sql_fetchrow($result)) - { - $converted_users++; - - $cp_data = array( - 'pf_phpbb_interests' => $row['user_interests'], - ); - - $sql = 'UPDATE ' . $this->table_prefix . 'profile_fields_data - SET ' . $this->db->sql_build_array('UPDATE', $cp_data) . ' - WHERE user_id = ' . (int) $row['user_id']; - $this->db->sql_query($sql); - - if (!$this->db->sql_affectedrows()) - { - $cp_data['user_id'] = (int) $row['user_id']; - $insert_buffer->insert($cp_data); - } - } - $this->db->sql_freeresult($result); - - $insert_buffer->flush(); - - if ($converted_users < $limit) - { - // No more users left, we are done... - return; - } - - return $start + $limit; - } + protected $user_column_name = 'user_interests'; } diff --git a/phpBB/phpbb/db/migration/data/v310/profilefield_occupation.php b/phpBB/phpbb/db/migration/data/v310/profilefield_occupation.php index ee276fa0f9..b0ea961c08 100644 --- a/phpBB/phpbb/db/migration/data/v310/profilefield_occupation.php +++ b/phpBB/phpbb/db/migration/data/v310/profilefield_occupation.php @@ -9,13 +9,8 @@ namespace phpbb\db\migration\data\v310; -class profilefield_occupation extends \phpbb\db\migration\migration +class profilefield_occupation extends \phpbb\db\migration\profilefield_base_migration { - public function effectively_installed() - { - return $this->db_tools->sql_column_exists($this->table_prefix . 'profile_fields_data', 'pf_phpbb_occupation'); - } - static public function depends_on() { return array( @@ -23,124 +18,30 @@ class profilefield_occupation extends \phpbb\db\migration\migration ); } - public function update_schema() - { - return array( - 'change_columns' => array( - $this->table_prefix . 'profile_fields_data' => array( - 'pf_phpbb_occupation' => array('MTEXT', ''), - ), - ), - ); - } + protected $profilefield_name = 'phpbb_occupation'; - public function update_data() - { - return array( - array('custom', array(array($this, 'create_occupation_custom_field'))), - array('custom', array(array($this, 'convert_occupation_to_custom_field'))), - ); - } + protected $profilefield_database_type = array('MTEXT', ''); - public function create_occupation_custom_field() - { - $sql = 'SELECT MAX(field_order) as max_field_order - FROM ' . PROFILE_FIELDS_TABLE; - $result = $this->db->sql_query($sql); - $max_field_order = (int) $this->db->sql_fetchfield('max_field_order'); - $this->db->sql_freeresult($result); + protected $profilefield_data = array( + 'field_name' => 'phpbb_occupation', + 'field_type' => 'profilefields.type.text', + 'field_ident' => 'phpbb_occupation', + 'field_length' => '3|30', + 'field_minlen' => '2', + 'field_maxlen' => '500', + 'field_novalue' => '', + 'field_default_value' => '', + 'field_validation' => '.*', + 'field_required' => 0, + 'field_show_novalue' => 0, + 'field_show_on_reg' => 0, + 'field_show_on_pm' => 0, + 'field_show_on_vt' => 0, + 'field_show_profile' => 1, + 'field_hide' => 0, + 'field_no_view' => 0, + 'field_active' => 1, + ); - $sql_ary = array( - 'field_name' => 'phpbb_occupation', - 'field_type' => 'profilefields.type.text', - 'field_ident' => 'phpbb_occupation', - 'field_length' => '3|30', - 'field_minlen' => '2', - 'field_maxlen' => '500', - 'field_novalue' => '', - 'field_default_value' => '', - 'field_validation' => '.*', - 'field_required' => 0, - 'field_show_novalue' => 0, - 'field_show_on_reg' => 0, - 'field_show_on_pm' => 0, - 'field_show_on_vt' => 0, - 'field_show_profile' => 1, - 'field_hide' => 0, - 'field_no_view' => 0, - 'field_active' => 1, - 'field_order' => $max_field_order + 1, - ); - - $sql = 'INSERT INTO ' . PROFILE_FIELDS_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary); - $this->db->sql_query($sql); - $field_id = (int) $this->db->sql_nextid(); - - $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, PROFILE_LANG_TABLE); - - $sql = 'SELECT lang_id - FROM ' . LANG_TABLE; - $result = $this->db->sql_query($sql); - while ($lang_id = (int) $this->db->sql_fetchfield('lang_id')) - { - $insert_buffer->insert(array( - 'field_id' => $field_id, - 'lang_id' => $lang_id, - 'lang_name' => 'OCCUPATION', - 'lang_explain' => '', - 'lang_default_value' => '', - )); - } - $this->db->sql_freeresult($result); - - $insert_buffer->flush(); - } - - /** - * @param int $start Start of staggering step - * @return mixed int start of the next step, null if the end was reached - */ - public function convert_occupation_to_custom_field($start) - { - $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->table_prefix . 'profile_fields_data'); - $limit = 250; - $converted_users = 0; - - $sql = 'SELECT user_id, user_occ - FROM ' . $this->table_prefix . "users - WHERE user_occ <> '' - ORDER BY user_id"; - $result = $this->db->sql_query_limit($sql, $limit, $start); - - while ($row = $this->db->sql_fetchrow($result)) - { - $converted_users++; - - $cp_data = array( - 'pf_phpbb_occupation' => $row['user_occ'], - ); - - $sql = 'UPDATE ' . $this->table_prefix . 'profile_fields_data - SET ' . $this->db->sql_build_array('UPDATE', $cp_data) . ' - WHERE user_id = ' . (int) $row['user_id']; - $this->db->sql_query($sql); - - if (!$this->db->sql_affectedrows()) - { - $cp_data['user_id'] = (int) $row['user_id']; - $insert_buffer->insert($cp_data); - } - } - $this->db->sql_freeresult($result); - - $insert_buffer->flush(); - - if ($converted_users < $limit) - { - // No more users left, we are done... - return; - } - - return $start + $limit; - } + protected $user_column_name = 'user_occ'; } diff --git a/phpBB/phpbb/db/migration/profilefield_base_migration.php b/phpBB/phpbb/db/migration/profilefield_base_migration.php new file mode 100644 index 0000000000..e637aec9f3 --- /dev/null +++ b/phpBB/phpbb/db/migration/profilefield_base_migration.php @@ -0,0 +1,155 @@ +db_tools->sql_column_exists($this->table_prefix . 'profile_fields_data', 'pf_' . $this->profilefield_name); + } + + public function update_schema() + { + return array( + 'add_columns' => array( + $this->table_prefix . 'profile_fields_data' => array( + 'pf_' . $this->profilefield_name => $this->profilefield_database_type, + ), + ), + ); + } + + public function revert_schema() + { + return array( + 'drop_columns' => array( + $this->table_prefix . 'profile_fields_data' => array( + 'pf_' . $this->profilefield_name, + ), + ), + ); + } + + public function update_data() + { + return array( + array('custom', array(array($this, 'create_custom_field'))), + array('custom', array(array($this, 'convert_user_field_to_custom_field'))), + ); + } + + public function create_custom_field() + { + $sql = 'SELECT MAX(field_order) as max_field_order + FROM ' . PROFILE_FIELDS_TABLE; + $result = $this->db->sql_query($sql); + $max_field_order = (int) $this->db->sql_fetchfield('max_field_order'); + $this->db->sql_freeresult($result); + + $sql_ary = array_merge($this->profilefield_data, array( + 'field_order' => $max_field_order + 1, + )); + + $sql = 'INSERT INTO ' . PROFILE_FIELDS_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary); + $this->db->sql_query($sql); + $field_id = (int) $this->db->sql_nextid(); + + $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, PROFILE_LANG_TABLE); + + $sql = 'SELECT lang_id + FROM ' . LANG_TABLE; + $result = $this->db->sql_query($sql); + while ($lang_id = (int) $this->db->sql_fetchfield('lang_id')) + { + $insert_buffer->insert(array( + 'field_id' => $field_id, + 'lang_id' => $lang_id, + 'lang_name' => strtoupper(substr($this->profilefield_name, 6)),// Remove phpbb_ from field name + 'lang_explain' => '', + 'lang_default_value' => '', + )); + } + $this->db->sql_freeresult($result); + + $insert_buffer->flush(); + } + + /** + * @param int $start Start of staggering step + * @return mixed int start of the next step, null if the end was reached + */ + public function convert_user_field_to_custom_field($start) + { + $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->table_prefix . 'profile_fields_data'); + $limit = 250; + $converted_users = 0; + + $sql = 'SELECT user_id, ' . $this->user_column_name . ' + FROM ' . $this->table_prefix . 'users + WHERE ' . $this->user_column_name . " <> '' + ORDER BY user_id"; + $result = $this->db->sql_query_limit($sql, $limit, $start); + + while ($row = $this->db->sql_fetchrow($result)) + { + $converted_users++; + + $cp_data = array( + 'pf_' . $this->profilefield_name => $row[$this->user_column_name], + ); + + $sql = 'UPDATE ' . $this->table_prefix . 'profile_fields_data + SET ' . $this->db->sql_build_array('UPDATE', $cp_data) . ' + WHERE user_id = ' . (int) $row['user_id']; + $this->db->sql_query($sql); + + if (!$this->db->sql_affectedrows()) + { + $cp_data['user_id'] = (int) $row['user_id']; + $cp_data = array_merge($this->get_insert_sql_array(), $cp_data); + $insert_buffer->insert($cp_data); + } + } + $this->db->sql_freeresult($result); + + $insert_buffer->flush(); + + if ($converted_users < $limit) + { + // No more users left, we are done... + return; + } + + return $start + $limit; + } + + protected function get_insert_sql_array() + { + static $profile_row; + + if ($profile_row === null) + { + global $phpbb_container; + $manager = $phpbb_container->get('profilefields.manager'); + $profile_row = $manager->build_insert_sql_array(array()); + } + + return $profile_row; + } +}