BBP_Converter_Base::convert_table( $to_type,  $start )

Convert Table

Description

Parameters

(Required) Start row

Source

File: bp-forums/admin/converter.php

822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
public function convert_table( $to_type, $start ) {
 
    // Are we usig a sync table, or postmeta?
    if ( $this->wpdb->get_var( "SHOW TABLES LIKE '" . $this->sync_table_name . "'" ) == $this->sync_table_name ) {
        $this->sync_table = true;
    } else {
        $this->sync_table = false;
    }
 
    // Set some defaults
    $has_insert     = false;
    $from_tablename = '';
    $field_list     = $from_tables = $tablefield_array = array();
 
    // Toggle Table Name based on $to_type (destination)
    switch ( $to_type ) {
        case 'user' :
            $tablename = $this->wpdb->users;
            break;
 
        case 'tags' :
            $tablename = '';
            break;
 
        default :
            $tablename = $this->wpdb->posts;
    }
 
    // Get the fields from the destination table
    if ( !empty( $tablename ) ) {
        $tablefield_array = $this->get_fields( $tablename );
    }
 
    /** Step 1 ************************************************************/
 
    // Loop through the field maps, and look for to_type matches
    foreach ( $this->field_map as $item ) {
 
        // Yay a match, and we have a from table, too
        if ( ( $item['to_type'] == $to_type ) && !empty( $item['from_tablename'] ) ) {
 
            // $from_tablename was set from a previous loop iteration
            if ( ! empty( $from_tablename ) ) {
 
                // Doing some joining
                if ( !in_array( $item['from_tablename'], $from_tables ) && in_array( $item['join_tablename'], $from_tables ) ) {
                    $from_tablename .= ' ' . $item['join_type'] . ' JOIN ' . $this->opdb->prefix . $item['from_tablename'] . ' AS ' . $item['from_tablename'] . ' ' . $item['join_expression'];
                }
 
            // $from_tablename needs to be set
            } else {
                $from_tablename = $item['from_tablename'] . ' AS ' . $item['from_tablename'];
            }
 
            // Specific FROM expression data used
            if ( !empty( $item['from_expression'] ) ) {
 
                // No 'WHERE' in expression
                if ( stripos( $from_tablename, "WHERE" ) === false ) {
                    $from_tablename .= ' ' . $item['from_expression'];
 
                // 'WHERE' in expression, so replace with 'AND'
                } else {
                    $from_tablename .= ' ' . str_replace( "WHERE", "AND", $item['from_expression'] );
                }
            }
 
            // Add tablename and fieldname to arrays, formatted for querying
            $from_tables[] = $item['from_tablename'];
            $field_list[]  = 'convert(' . $item['from_tablename'] . '.' . $item['from_fieldname'] . ' USING "' . $this->charset . '") AS ' . $item['from_fieldname'];
        }
    }
 
    /** Step 2 ************************************************************/
 
    // We have a $from_tablename, so we want to get some data to convert
    if ( !empty( $from_tablename ) ) {
 
        // Get some data from the old forums
        $field_list  = array_unique( $field_list );
        $forum_query = 'SELECT ' . implode( ',', $field_list ) . ' FROM ' . $this->opdb->prefix . $from_tablename . ' LIMIT ' . $start . ', ' . $this->max_rows;
        $forum_array = $this->opdb->get_results( $forum_query, ARRAY_A );
 
        // Set this query as the last one ran
        update_option( '_bbp_converter_query', $forum_query );
 
        // Query returned some results
        if ( !empty( $forum_array ) ) {
 
            // Loop through results
            foreach ( (array) $forum_array as $forum ) {
 
                // Reset some defaults
                $insert_post = $insert_postmeta = $insert_data = array();
 
                // Loop through field map, again...
                foreach ( $this->field_map as $row ) {
 
                    // Types matchand to_fieldname is present. This means
                    // we have some work to do here.
                    if ( ( $row['to_type'] == $to_type ) && ! is_null( $row['to_fieldname'] ) ) {
 
                        // This row has a destination that matches one of the
                        // columns in this table.
                        if ( in_array( $row['to_fieldname'], $tablefield_array ) ) {
 
                            // Allows us to set default fields.
                            if ( isset( $row['default'] ) ) {
                                $insert_post[$row['to_fieldname']] = $row['default'];
 
                            // Translates a field from the old forum.
                            } elseif ( isset( $row['callback_method'] ) ) {
                                if ( ( 'callback_userid' == $row['callback_method'] ) && empty( $_POST['_bbp_converter_convert_users'] ) ) {
                                    $insert_post[$row['to_fieldname']] = $forum[$row['from_fieldname']];
                                } else {
                                    $insert_post[$row['to_fieldname']] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[$row['from_fieldname']], $forum ) );
                                }
 
                            // Maps the field from the old forum.
                            } else {
                                $insert_post[$row['to_fieldname']] = $forum[$row['from_fieldname']];
                            }
 
                        // Destination field is not empty, so we might need
                        // to do some extra work or set a default.
                        } elseif ( !empty( $row['to_fieldname'] ) ) {
 
                            // Allows us to set default fields.
                            if ( isset( $row['default'] ) ) {
                                $insert_postmeta[$row['to_fieldname']] = $row['default'];
 
                            // Translates a field from the old forum.
                            } elseif ( isset( $row['callback_method'] ) ) {
                                if ( ( $row['callback_method'] == 'callback_userid' ) && ( 0 == $_POST['_bbp_converter_convert_users'] ) ) {
                                    $insert_postmeta[$row['to_fieldname']] = $forum[$row['from_fieldname']];
                                } else {
                                    $insert_postmeta[$row['to_fieldname']] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[$row['from_fieldname']], $forum ) );
                                }
 
                            // Maps the field from the old forum.
                            } else {
                                $insert_postmeta[$row['to_fieldname']] = $forum[$row['from_fieldname']];
                            }
                        }
                    }
                }
 
                /** Step 3 ************************************************/
 
                // Something to insert into the destination field
                if ( count( $insert_post ) > 0 || ( $to_type == 'tags' && count( $insert_postmeta ) > 0 ) ) {
 
                    switch ( $to_type ) {
 
                        /** New user **************************************/
 
                        case 'user':
                            if ( username_exists( $insert_post['user_login'] ) ) {
                                $insert_post['user_login'] = 'imported_' . $insert_post['user_login'];
                            }
 
                            if ( email_exists( $insert_post['user_email'] ) ) {
                                $insert_post['user_email'] = 'imported_' . $insert_post['user_email'];
                            }
 
                            $post_id = wp_insert_user( $insert_post );
 
                            if ( is_numeric( $post_id ) ) {
 
                                foreach ( $insert_postmeta as $key => $value ) {
 
                                    add_user_meta( $post_id, $key, $value, true );
 
                                    if ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) {
                                        $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'user', 'value_id' => $post_id, 'meta_key' => $key, 'meta_value' => $value ) );
                                    }
                                }
                            }
                            break;
 
                        /** New Topic-Tag *********************************/
 
                        case 'tags':
                            $post_id = wp_set_object_terms( $insert_postmeta['objectid'], $insert_postmeta['name'], 'topic-tag', true );
                            $term = get_term_by( 'name', $insert_postmeta['name'], 'topic-tag');
                            if ( false !== $term ) {
                                wp_update_term( $term->term_id, 'topic-tag', array(
                                    'description' => $insert_postmeta['description'],
                                    'slug'        => $insert_postmeta['slug']
                                ) );
                            }
                            break;
 
                        /** Forum, Topic, Reply ***************************/
 
                        default:
                            $post_id = wp_insert_post( $insert_post );
 
                            if ( is_numeric( $post_id ) ) {
 
                                foreach ( $insert_postmeta as $key => $value ) {
 
                                    add_post_meta( $post_id, $key, $value, true );
 
                                    // Forums need to save their old ID for group forum association
                                    if ( ( 'forum' == $to_type ) && ( '_bbp_forum_id' == $key ) )
                                        add_post_meta( $post_id, '_bbp_old_forum_id', $value );
 
                                    // Topics need an extra bit of metadata
                                    // to be keyed to the new post_id
                                    if ( ( 'topic' == $to_type ) && ( '_bbp_topic_id' == $key ) ) {
 
                                        // Update the live topic ID
                                        update_post_meta( $post_id, $key, $post_id );
 
                                        // Save the old topic ID
                                        add_post_meta( $post_id, '_bbp_old_topic_id', $value );
                                        if ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) {
                                            $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'post', 'value_id' => $post_id, 'meta_key' => '_bbp_topic_id',     'meta_value' => $post_id ) );
                                            $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'post', 'value_id' => $post_id, 'meta_key' => '_bbp_old_topic_id', 'meta_value' => $value   ) );
                                        }
 
                                    } elseif ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) {
                                        $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'post', 'value_id' => $post_id, 'meta_key' => $key, 'meta_value' => $value ) );
                                    }
 
                                    // Replies need to save their old reply_to ID for hierarchical replies association
                                    if ( ( 'reply' == $to_type ) && ( '_bbp_reply_to' == $key ) ) {
                                        add_post_meta( $post_id, '_bbp_old_reply_to', $value );
                                    }
                                }
                            }
                            break;
                    }
                    $has_insert = true;
                }
            }
        }
    }
 
    return ! $has_insert;
}

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.