Skip to:
Content

bbPress.org

Changeset 6785


Ignore:
Timestamp:
02/19/2018 07:00:55 AM (7 years ago)
Author:
johnjamesjacoby
Message:

Converter: remove WP_SETUP_CONFIG constant setting from converter process.

This introduces a helper database class to avoid directly connecting to the external database. Instead, we'll attempt to control that connection, and provide feedback to the user in the event a connection cannot be made. The WP_SETUP_CONFIG was causing calls to the options API to fail, resulting in broken calculations and invalid offsets.

This commit includes some general load order clean-up, which also fixes a regression causing fatal errors when attempting to upgrade converted user passwords from the old platform. It also fixes the condition where boundaries being converted would display beyond their maximums.

Fixes #3191.

Location:
trunk/src/includes
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/admin/classes/class-bbp-converter-base.php

    r6780 r6785  
    139139        // Maybe add port to server
    140140        if ( ! empty( $db_port ) && ! empty( $db_host ) && ! strstr( $db_host, ':' ) ) {
    141             $db_host = $db_host . ':' . $db_port;
     141            $db_host = "{$db_host}:{$db_port}";
    142142        }
    143143
     
    147147        $this->wpdb = bbp_db();
    148148
    149         // Control WPDB db_connect() bailing
    150         define( 'WP_SETUP_CONFIG', true );
    151 
    152149        // Setup old forum Database
    153         $this->opdb = new wpdb( $db_user, $db_pass, $db_name, $db_host );
     150        $this->opdb = new BBP_Converter_DB( $db_user, $db_pass, $db_name, $db_host );
    154151
    155152        // Connection failed
     
    499496                            /** New user **************************************/
    500497
    501                             case 'user':
     498                            case 'user' :
    502499                                if ( username_exists( $insert_post['user_login'] ) ) {
    503                                     $insert_post['user_login'] = 'imported_' . $insert_post['user_login'];
     500                                    $insert_post['user_login'] = "imported_{$insert_post['user_login']}";
    504501                                }
    505502
    506503                                if ( email_exists( $insert_post['user_email'] ) ) {
    507                                     $insert_post['user_email'] = 'imported_' . $insert_post['user_email'];
     504                                    $insert_post['user_email'] = "imported_{$insert_post['user_email']}";
    508505                                }
    509506
     
    523520
    524521                                        if ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) {
    525                                             $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'user', 'value_id' => $post_id, 'meta_key' => $key, 'meta_value' => $value ) );
     522                                            $this->wpdb->insert( $this->sync_table_name, array(
     523                                                'value_type' => 'user',
     524                                                'value_id'   => $post_id,
     525                                                'meta_key'   => $key,
     526                                                'meta_value' => $value
     527                                            ) );
    526528                                        }
    527529                                    }
     
    531533                            /** New Topic-Tag *********************************/
    532534
    533                             case 'tags':
     535                            case 'tags' :
    534536                                $post_id = wp_set_object_terms( $insert_postmeta['objectid'], $insert_postmeta['name'], 'topic-tag', true );
    535537                                $term = get_term_by( 'name', $insert_postmeta['name'], 'topic-tag');
     
    544546                            /** Forum Subscriptions ***************************/
    545547
    546                             case 'forum_subscriptions':
     548                            case 'forum_subscriptions' :
    547549                                $user_id = $insert_post['user_id'];
    548550                                $items   = wp_list_pluck( $insert_postmeta, '_bbp_forum_subscriptions' );
     
    565567                            /** Subscriptions *********************************/
    566568
    567                             case 'topic_subscriptions':
     569                            case 'topic_subscriptions' :
    568570                                $user_id = $insert_post['user_id'];
    569571                                $items   = wp_list_pluck( $insert_postmeta, '_bbp_subscriptions' );
     
    586588                            /** Favorites *************************************/
    587589
    588                             case 'favorites':
     590                            case 'favorites' :
    589591                                $user_id = $insert_post['user_id'];
    590592                                $items   = wp_list_pluck( $insert_postmeta, '_bbp_favorites' );
     
    607609                            /** Forum, Topic, Reply ***************************/
    608610
    609                             default:
     611                            default :
    610612                                $post_id = wp_insert_post( $insert_post, true );
    611613
     
    634636                                         */
    635637                                        if ( '_id' === substr( $key, -3 ) && ( true === $this->sync_table ) ) {
    636                                             $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'post', 'value_id' => $post_id, 'meta_key' => $key, 'meta_value' => $value ) );
     638                                            $this->wpdb->insert( $this->sync_table_name, array(
     639                                                'value_type' => 'post',
     640                                                'value_id'   => $post_id,
     641                                                'meta_key'   => $key,
     642                                                'meta_value' => $value
     643                                            ) );
    637644                                        }
    638645
     
    665672        $has_update = false;
    666673        $query      = ! empty( $this->sync_table )
    667             ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value > 0 LIMIT {$start}, {$this->max_rows}", '_bbp_old_forum_parent_id' )
     674            ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value > 0 LIMIT {$start}, {$this->max_rows}",           '_bbp_old_forum_parent_id' )
    668675            : $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value > 0 LIMIT {$start}, {$this->max_rows}", '_bbp_old_forum_parent_id' );
    669676
     
    685692        $has_update = false;
    686693        $query      = ! empty( $this->sync_table )
    687             ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_sticky_status_id', 'sticky' )
     694            ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}",           '_bbp_old_sticky_status_id', 'sticky' )
    688695            : $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_sticky_status_id', 'sticky' );
    689696
     
    704711        $has_update = false;
    705712        $query      = ! empty( $this->sync_table )
    706             ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_sticky_status_id', 'super-sticky' )
     713            ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}",           '_bbp_old_sticky_status_id', 'super-sticky' )
    707714            : $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_sticky_status_id', 'super-sticky' );
    708715
     
    724731        $has_update = false;
    725732        $query      = ! empty( $this->sync_table )
    726             ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_closed_status_id', 'closed' )
     733            ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}",           '_bbp_old_closed_status_id', 'closed' )
    727734            : $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_closed_status_id', 'closed' );
    728735
     
    743750        $has_update = false;
    744751        $query      = ! empty( $this->sync_table )
    745             ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value > 0 LIMIT {$start}, {$this->max_rows}", '_bbp_old_reply_to_id' )
     752            ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value > 0 LIMIT {$start}, {$this->max_rows}",           '_bbp_old_reply_to_id' )
    746753            : $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value > 0 LIMIT {$start}, {$this->max_rows}", '_bbp_old_reply_to_id' );
    747754
     
    766773        if ( ! empty( $this->sync_table ) ) {
    767774            $query = $this->wpdb->prepare( "SELECT sync_table1.value_id AS topic_id, sync_table1.meta_value AS topic_is_anonymous, sync_table2.meta_value AS topic_author
    768                             FROM {$this->sync_table_name} AS sync_table1
    769                             INNER JOIN {$this->sync_table_name} AS sync_table2
    770                             ON ( sync_table1.value_id = sync_table2.value_id )
    771                             WHERE sync_table1.meta_value = %s
    772                             AND sync_table2.meta_key = %s
    773                             LIMIT {$start}, {$this->max_rows}", 'true', '_bbp_old_topic_author_name_id' );
     775                FROM {$this->sync_table_name} AS sync_table1
     776                INNER JOIN {$this->sync_table_name} AS sync_table2
     777                ON ( sync_table1.value_id = sync_table2.value_id )
     778                WHERE sync_table1.meta_value = %s
     779                AND sync_table2.meta_key = %s
     780                LIMIT {$start}, {$this->max_rows}", 'true', '_bbp_old_topic_author_name_id' );
    774781        } else {
    775782            $query = $this->wpdb->prepare( "SELECT wp_postmeta1.post_id AS topic_id, wp_postmeta1.meta_value AS topic_is_anonymous, wp_postmeta2.meta_value AS topic_author
    776                             FROM {$this->wpdb->postmeta} AS wp_postmeta1
    777                             INNER JOIN {$this->wpdb->postmeta} AS wp_postmeta2
    778                             ON ( wp_postmeta1.post_id = wp_postmeta2.post_id )
    779                             WHERE wp_postmeta1.meta_value = %s
    780                             AND wp_postmeta2.meta_key = %s
    781                             LIMIT {$start}, {$this->max_rows}", 'true', '_bbp_old_topic_author_name_id' );
     783                FROM {$this->wpdb->postmeta} AS wp_postmeta1
     784                INNER JOIN {$this->wpdb->postmeta} AS wp_postmeta2
     785                ON ( wp_postmeta1.post_id = wp_postmeta2.post_id )
     786                WHERE wp_postmeta1.meta_value = %s
     787                AND wp_postmeta2.meta_key = %s
     788                LIMIT {$start}, {$this->max_rows}", 'true', '_bbp_old_topic_author_name_id' );
    782789        }
    783790
     
    805812        if ( ! empty( $this->sync_table ) ) {
    806813            $query = $this->wpdb->prepare( "SELECT sync_table1.value_id AS reply_id, sync_table1.meta_value AS reply_is_anonymous, sync_table2.meta_value AS reply_author
    807                             FROM {$this->sync_table_name} AS sync_table1
    808                             INNER JOIN {$this->sync_table_name} AS sync_table2
    809                             ON ( sync_table1.value_id = sync_table2.value_id )
    810                             WHERE sync_table1.meta_value = %s
    811                             AND sync_table2.meta_key = %s
    812                             LIMIT {$start}, {$this->max_rows}", 'true', '_bbp_old_reply_author_name_id' );
     814                FROM {$this->sync_table_name} AS sync_table1
     815                INNER JOIN {$this->sync_table_name} AS sync_table2
     816                ON ( sync_table1.value_id = sync_table2.value_id )
     817                WHERE sync_table1.meta_value = %s
     818                AND sync_table2.meta_key = %s
     819                LIMIT {$start}, {$this->max_rows}", 'true', '_bbp_old_reply_author_name_id' );
    813820        } else {
    814821            $query = $this->wpdb->prepare( "SELECT wp_postmeta1.post_id AS reply_id, wp_postmeta1.meta_value AS reply_is_anonymous, wp_postmeta2.meta_value AS reply_author
    815                             FROM {$this->wpdb->postmeta} AS wp_postmeta1
    816                             INNER JOIN {$this->wpdb->postmeta} AS wp_postmeta2
    817                             ON ( wp_postmeta1.post_id = wp_postmeta2.post_id )
    818                             WHERE wp_postmeta1.meta_value = %s
    819                             AND wp_postmeta2.meta_key = %s
    820                             LIMIT {$start}, {$this->max_rows}", 'true', '_bbp_old_reply_author_name_id' );
     822                FROM {$this->wpdb->postmeta} AS wp_postmeta1
     823                INNER JOIN {$this->wpdb->postmeta} AS wp_postmeta2
     824                ON ( wp_postmeta1.post_id = wp_postmeta2.post_id )
     825                WHERE wp_postmeta1.meta_value = %s
     826                AND wp_postmeta2.meta_key = %s
     827                LIMIT {$start}, {$this->max_rows}", 'true', '_bbp_old_reply_author_name_id' );
    821828        }
    822829
     
    928935     * @param string The table name to grab fields from
    929936     */
    930     private function get_fields( $tablename ) {
    931         $rval        = array();
    932         $field_array = $this->get_results( 'DESCRIBE ' . $tablename, ARRAY_A );
    933 
     937    private function get_fields( $tablename = '' ) {
     938        $retval      = array();
     939        $field_array = $this->get_results( "DESCRIBE {$tablename}", ARRAY_A );
     940
     941        // Bail if no fields
     942        if ( empty( $field_array ) ) {
     943            return $retval;
     944        }
     945
     946        // Add fields to array
    934947        foreach ( $field_array as $field ) {
    935             $rval[] = $field['Field'];
    936         }
    937 
     948            if ( ! empty( $field['Field'] ) ) {
     949                $retval[] = $field['Field'];
     950            }
     951        }
     952
     953        // Add social fields for users table
    938954        if ( $tablename === $this->wpdb->users ) {
    939             $rval[] = 'role';
    940             $rval[] = 'yim';
    941             $rval[] = 'aim';
    942             $rval[] = 'jabber';
    943         }
    944 
    945         return $rval;
     955            $retval[] = 'role';
     956            $retval[] = 'yim';
     957            $retval[] = 'aim';
     958            $retval[] = 'jabber';
     959        }
     960
     961        return $retval;
    946962    }
    947963
  • trunk/src/includes/admin/classes/class-bbp-converter.php

    r6681 r6785  
    216216
    217217        // Maybe prepend the step
    218         $progress = ! empty( $this->step )
    219             ? sprintf( '<span class="step">%s.</span><span class="output">%s</span><span class="mini-step">%s</span>', $this->step, $output, $this->step_percentage . '%' )
    220             : $output;
     218        if ( ! empty( $this->step ) ) {
     219
     220            // Include percentage
     221            if ( ! empty( $this->rows_in_step ) ) {
     222                $progress = sprintf( '<span class="step">%s.</span><span class="output">%s</span><span class="mini-step">%s</span>', $this->step, $output, $this->step_percentage . '%' );
     223
     224            // Don't include percentage
     225            } else {
     226                $progress = sprintf( '<span class="step">%s.</span><span class="output">%s</span>', $this->step, $output );
     227            }
     228
     229        // Raw text
     230        } else {
     231            $progress = $output;
     232        }
    221233
    222234        // Output
     
    428440     */
    429441    private function bump_start() {
     442
     443        // Set rows in step from option
     444        $this->rows_in_step = get_option( '_bbp_converter_rows_in_step', 0 );
     445
     446        // Get rows to start from
    430447        $start = (int) ( $this->start + $this->rows );
    431448
     449        // Enforce maximum if exists
     450        if ( $this->rows_in_step > 0 ) {
     451
     452            // Start cannot be larger than total rows
     453            if ( $start > $this->rows_in_step ) {
     454                $start = $this->rows_in_step;
     455            }
     456
     457            // Max can't be greater than total rows
     458            if ( $this->max > $this->rows_in_step ) {
     459                $this->max = $this->rows_in_step;
     460            }
     461        }
     462
     463        // Update the start option
    432464        update_option( '_bbp_converter_start', $start );
    433465    }
     
    467499            } else {
    468500                $this->bump_start();
    469                 $this->converter_response( sprintf( esc_html__( 'Deleting previously converted data (%1$s through %2$s of %3$s)', 'bbpress' ), $this->start, $this->max, $this->rows_in_step) );
     501                $this->converter_response( sprintf( esc_html__( 'Deleting previously converted data (%1$s through %2$s)', 'bbpress' ), $this->start, $this->max ) );
    470502            }
    471503
     
    493525            } else {
    494526                $this->bump_start();
    495                 $this->converter_response( sprintf(  esc_html__( 'Converting users (%1$s through %2$s of %3$s)', 'bbpress' ), $this->start, $this->max, $this->rows_in_step ) );
     527                $this->converter_response( sprintf( esc_html__( 'Converting users (%1$s through %2$s of %3$s)', 'bbpress' ), $this->start, $this->max, $this->rows_in_step ) );
    496528            }
    497529        } else {
     
    516548            } else {
    517549                $this->bump_start();
    518                 $this->converter_response( sprintf( esc_html__( 'Delete default WordPress user passwords (%1$s through %2$s of %3$s)', 'bbpress' ), $this->start, $this->max, $this->rows_in_step ) );
     550                $this->converter_response( sprintf( esc_html__( 'Delete default WordPress user passwords (%1$s through %2$s)', 'bbpress' ), $this->start, $this->max ) );
    519551            }
    520552        } else {
  • trunk/src/includes/admin/tools/converter.php

    r6601 r6785  
    2121 */
    2222function bbp_get_converters() {
     23    static $files = array();
    2324
    24     // Default
    25     $files  = array();
    26     $path   = bbp_setup_converter()->converters_dir;
    27     $curdir = opendir( $path );
     25    // Only hit the file system one time per page load
     26    if ( empty( $files ) ) {
    2827
    29     // Look for the converter file in the converters directory
    30     if ( false !== $curdir ) {
    31         while ( $file = readdir( $curdir ) ) {
    32             if ( stristr( $file, '.php' ) && stristr( $file, 'index' ) === false ) {
    33                 $name = preg_replace( '/.php/', '', $file );
    34                 if ( 'Example' !== $name ) {
    35                     $files[ $name ] = $path . $file;
     28        // Open the converter directory
     29        $path   = bbp_setup_converter()->converters_dir;
     30        $curdir = opendir( $path );
     31
     32        // Look for the converter file in the converters directory
     33        if ( false !== $curdir ) {
     34            while ( $file = readdir( $curdir ) ) {
     35                if ( stristr( $file, '.php' ) && stristr( $file, 'index' ) === false ) {
     36                    $name = preg_replace( '/.php/', '', $file );
     37                    if ( 'Example' !== $name ) {
     38                        $files[ $name ] = $path . $file;
     39                    }
    3640                }
    3741            }
    3842        }
    39     }
    4043
    41     // Close the directory
    42     closedir( $curdir );
     44        // Close the directory
     45        closedir( $curdir );
    4346
    44     // Sort keys alphabetically, ignoring upper/lower casing
    45     if ( ! empty( $files ) ) {
    46         natcasesort( $files );
     47        // Sort keys alphabetically, ignoring upper/lower casing
     48        if ( ! empty( $files ) ) {
     49            natcasesort( $files );
     50        }
    4751    }
    4852
     
    6064 * @param string $platform Name of valid platform class.
    6165 */
    62 function bbp_new_converter( $platform ) {
     66function bbp_new_converter( $platform = '' ) {
    6367
    64     // Default value
    65     $converters = bbp_get_converters();
     68    // Bail if no platform
     69    if ( ! empty( $platform ) ) {
    6670
    67     // Create a new converter object if it's found
    68     if ( isset( $converters[ $platform ] ) ) {
     71        // Get the available converters
     72        $converters = bbp_get_converters();
    6973
    70         // Include & create the converter
    71         require_once $converters[ $platform ];
    72         if ( class_exists( $platform ) ) {
    73             return new $platform;
     74        // Create a new converter object if it's found
     75        if ( isset( $converters[ $platform ] ) ) {
     76
     77            // Include & create the converter
     78            require_once $converters[ $platform ];
     79            if ( class_exists( $platform ) ) {
     80                return new $platform;
     81            }
    7482        }
    7583    }
  • trunk/src/includes/core/abstraction.php

    r6768 r6785  
    6060    if ( empty( $bbp_admin->converter ) ) {
    6161
    62         // Require the converter classes
     62        // Require the converter files
     63        require_once $bbp_admin->admin_dir . 'tools/converter.php';
     64        require_once $bbp_admin->admin_dir . 'classes/class-bbp-converter.php';
     65        require_once $bbp_admin->admin_dir . 'classes/class-bbp-converter-db.php';
    6366        require_once $bbp_admin->admin_dir . 'classes/class-bbp-converter-base.php';
    64         require_once $bbp_admin->admin_dir . 'classes/class-bbp-converter.php';
    6567
    6668        // Setup
  • trunk/src/includes/users/functions.php

    r6740 r6785  
    982982
    983983    // Try to call the conversion method
    984     if ( is_a( $converter, 'BBP_Converter_Base' ) && method_exists( $converter, 'callback_pass' ) ) {
     984    if ( ( $converter instanceof BBP_Converter_Base ) && method_exists( $converter, 'callback_pass' ) ) {
    985985        $converter->callback_pass( $username, $_POST['pwd'] );
    986986    }
Note: See TracChangeset for help on using the changeset viewer.