PluginProbe
bbPress / 2.1-rc2
bbPress v2.1-rc2
2.6.17 trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 2.2.1 All 72 releases
bbpress / bbp-admin / bbp-converter.php

bbp-converter.php in bbPress 2.1-rc2, at bbp-admin/bbp-converter.php

1,260 lines 39.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Converter
5 *
6 * Based on the hard work of Adam Ellis at http://bbconverter.com
7 *
8 * @package bbPress
9 * @subpackage Administration
10 */
11
12 // Exit if accessed directly
13 if ( !defined( 'ABSPATH' ) ) exit;
14
15 /**
16 * Main BBP_Converter Class
17 */
18 class BBP_Converter {
19
20 /**
21 * The main bbPress Converter loader
22 *
23 * @since bbPress (r3813)
24 * @uses BBP_Converter::includes() Include the required files
25 * @uses BBP_Converter::setup_actions() Setup the actions
26 */
27 public function __construct() {
28 $this->setup_actions();
29 }
30
31 /**
32 * Setup the default actions
33 *
34 * @since bbPress (r3813)
35 * @uses add_action() To add various actions
36 */
37 private function setup_actions() {
38
39 // Attach to the admin head with our ajax requests cycle and css
40 add_action( 'bbp_admin_head', array( $this, 'admin_head' ) );
41
42 // Attach the bbConverter admin settings action to the WordPress admin init action.
43 add_action( 'bbp_register_admin_settings', array( $this, 'register_admin_settings' ) );
44
45 // Attach to the login process to aid in converting passwords to wordpress.
46 add_action( 'login_form_login', array( $this, 'convert_pass' ) );
47
48 // Attach to the admin ajax request to process cycles
49 add_action( 'wp_ajax_bbconverter_process', array( $this, 'process_callback' ) );
50 }
51
52 /**
53 * Register the settings
54 *
55 * @since bbPress (r3813)
56 * @uses add_settings_section() To add our own settings section
57 * @uses add_settings_field() To add various settings fields
58 * @uses register_setting() To register various settings
59 */
60 public function register_admin_settings() {
61
62 // Add the main section
63 add_settings_section( 'bbpress_converter_main', __( 'Main Settings', 'bbpress' ), 'bbp_converter_setting_callback_main_section', 'bbpress_converter' );
64
65 // System Select
66 add_settings_field( '_bbp_converter_platform', __( 'Select Platform', 'bbpress' ), 'bbp_converter_setting_callback_platform', 'bbpress_converter', 'bbpress_converter_main' );
67 register_setting ( 'bbpress_converter_main', '_bbp_converter_platform', 'sanitize_title' );
68
69 // Database Server
70 add_settings_field( '_bbp_converter_db_server', __( 'Database Server', 'bbpress' ), 'bbp_converter_setting_callback_dbserver', 'bbpress_converter', 'bbpress_converter_main' );
71 register_setting ( 'bbpress_converter_main', '_bbp_converter_db_server', 'sanitize_title' );
72
73 // Database Server Port
74 add_settings_field( '_bbp_converter_db_port', __( 'Database Port', 'bbpress' ), 'bbp_converter_setting_callback_dbport', 'bbpress_converter', 'bbpress_converter_main' );
75 register_setting ( 'bbpress_converter_main', '_bbp_converter_db_port', 'sanitize_title' );
76
77 // Database Name
78 add_settings_field( '_bbp_converter_db_name', __( 'Database Name', 'bbpress' ), 'bbp_converter_setting_callback_dbname', 'bbpress_converter', 'bbpress_converter_main' );
79 register_setting ( 'bbpress_converter_main', '_bbp_converter_db_name', 'sanitize_title' );
80
81 // Database User
82 add_settings_field( '_bbp_converter_db_user', __( 'Database User', 'bbpress' ), 'bbp_converter_setting_callback_dbuser', 'bbpress_converter', 'bbpress_converter_main' );
83 register_setting ( 'bbpress_converter_main', '_bbp_converter_db_user', 'sanitize_title' );
84
85 // Database Pass
86 add_settings_field( '_bbp_converter_db_pass', __( 'Database Password', 'bbpress' ), 'bbp_converter_setting_callback_dbpass', 'bbpress_converter', 'bbpress_converter_main' );
87 register_setting ( 'bbpress_converter_main', '_bbp_converter_db_pass', 'sanitize_title' );
88
89 // Database Prefix
90 add_settings_field( '_bbp_converter_db_prefix', __( 'Table Prefix', 'bbpress' ), 'bbp_converter_setting_callback_dbprefix', 'bbpress_converter', 'bbpress_converter_main' );
91 register_setting ( 'bbpress_converter_main', '_bbp_converter_db_prefix', 'sanitize_title' );
92
93 // Add the options section
94 add_settings_section( 'bbpress_converter_opt', __( 'Options', 'bbpress' ), 'bbp_converter_setting_callback_options_section', 'bbpress_converter' );
95
96 // Rows Limit
97 add_settings_field( '_bbp_converter_rows', __( 'Rows Limit', 'bbpress' ), 'bbp_converter_setting_callback_rows', 'bbpress_converter', 'bbpress_converter_opt' );
98 register_setting ( 'bbpress_converter_opt', '_bbp_converter_rows', 'intval' );
99
100 // Delay Time
101 add_settings_field( '_bbp_converter_delay_time', __( 'Delay Time', 'bbpress' ), 'bbp_converter_setting_callback_delay_time', 'bbpress_converter', 'bbpress_converter_opt' );
102 register_setting ( 'bbpress_converter_opt', '_bbp_converter_delay_time', 'intval' );
103
104 // Convert Users ?
105 add_settings_field( '_bbp_converter_convert_users', __( 'Convert Users', 'bbpress' ), 'bbp_converter_setting_callback_convert_users', 'bbpress_converter', 'bbpress_converter_opt' );
106 register_setting ( 'bbpress_converter_opt', '_bbp_converter_convert_users', 'intval' );
107
108 // Clean
109 add_settings_field( '_bbp_converter_clean', __( 'Clean', 'bbpress' ), 'bbp_converter_setting_callback_clean', 'bbpress_converter', 'bbpress_converter_opt' );
110 register_setting ( 'bbpress_converter_opt', '_bbp_converter_clean', 'intval' );
111
112 // Restart
113 add_settings_field( '_bbp_converter_restart', __( 'Restart', 'bbpress' ), 'bbp_converter_setting_callback_restart', 'bbpress_converter', 'bbpress_converter_opt' );
114 register_setting ( 'bbpress_converter_opt', '_bbp_converter_restart', 'intval' );
115 }
116
117 /**
118 * Admin scripts
119 *
120 * @since bbPress (r3813)
121 */
122 public function admin_head() { ?>
123
124 <style type="text/css" media="screen">
125 /*<![CDATA[*/
126
127 div.bbp-converter-updated,
128 div.bbp-converter-warning {
129 border-radius: 3px 3px 3px 3px;
130 border-style: solid;
131 border-width: 1px;
132 padding: 5px 5px 5px 5px;
133 }
134
135 div.bbp-converter-updated {
136 height: 300px;
137 overflow: auto;
138 display: none;
139 background-color: #FFFFE0;
140 border-color: #E6DB55;
141 font-family: monospace;
142 }
143
144 div.bbp-converter-updated p {
145 margin: 0.5em 0;
146 padding: 2px;
147 }
148
149 div.bbp-converter-updated p strong.loading {
150 padding: 2px 20px 2px 0;
151 background-image: url('<?php echo admin_url(); ?>images/wpspin_light.gif');
152 background-repeat: no-repeat;
153 background-position: center right;
154 }
155
156 #bbp-converter-stop {
157 display:none;
158 }
159
160 #bbp-converter-progress {
161 display:none;
162 }
163
164 /*]]>*/
165 </style>
166
167 <script language="javascript">
168
169 var bbconverter_is_running = false;
170 var bbconverter_run_timer;
171 var bbconverter_delay_time = 0;
172
173 function bbconverter_grab_data() {
174 var values = {};
175 jQuery.each(jQuery('#bbp-converter-settings').serializeArray(), function(i, field) {
176 values[field.name] = field.value;
177 });
178
179 if( values['_bbp_converter_restart'] ) {
180 jQuery('#_bbp_converter_restart').removeAttr("checked");
181 }
182
183 if( values['_bbp_converter_delay_time'] ) {
184 bbconverter_delay_time = values['_bbp_converter_delay_time'] * 1000;
185 }
186
187 values['action'] = 'bbconverter_process';
188 return values;
189 }
190
191 function bbconverter_start() {
192 if( false == bbconverter_is_running ) {
193 bbconverter_is_running = true;
194 jQuery('#bbp-converter-start').hide();
195 jQuery('#bbp-converter-stop').show();
196 jQuery('#bbp-converter-progress').show();
197 bbconverter_log( "Starting Conversion..." );
198 bbconverter_run();
199 }
200 }
201
202 function bbconverter_run() {
203 jQuery.post(ajaxurl, bbconverter_grab_data(), function(response) {
204 var response_length = response.length - 1;
205 response = response.substring(0,response_length);
206 bbconverter_success(response);
207 });
208 }
209
210 function bbconverter_stop() {
211 bbconverter_is_running = false;
212 jQuery('#bbp-converter-message strong').removeClass( 'loading' );
213 }
214
215 function bbconverter_success(response) {
216 bbconverter_log(response);
217
218 if ( response == 'Conversion Complete' || response.indexOf('error') > -1 ) {
219 bbconverter_log('<b>Repair any missing information: <a href="<?php echo admin_url(); ?>tools.php?page=bbp-repair">Continue</a></b>');
220 jQuery('#bbp-converter-start').show();
221 jQuery('#bbp-converter-stop').hide();
222 jQuery('#bbp-converter-progress').hide();
223 bbconverter_stop();
224 clearTimeout( bbconverter_run_timer );
225 } else if( bbconverter_is_running ) { // keep going
226 jQuery('#bbp-converter-progress').show();
227 clearTimeout( bbconverter_run_timer );
228 bbconverter_run_timer = setTimeout( 'bbconverter_run()', bbconverter_delay_time );
229 } else {
230 jQuery('#bbp-converter-start').show();
231 jQuery('#bbp-converter-stop').hide();
232 jQuery('#bbp-converter-progress').hide();
233 clearTimeout( bbconverter_run_timer );
234 }
235 }
236
237 function bbconverter_log(text) {
238 if ( jQuery('#bbp-converter-message').css('display') == 'none' ) {
239 jQuery('#bbp-converter-message').show();
240 }
241 if ( text ) {
242 jQuery('#bbp-converter-message strong').removeClass( 'loading' );
243 jQuery('#bbp-converter-message').prepend('<p><strong class="loading">' + text + '</strong></p>');
244 }
245 }
246
247 </script>
248
249 <?php
250 }
251
252 /**
253 * Callback processor
254 *
255 * @since bbPress (r3813)
256 */
257 public function process_callback() {
258
259 if ( ! ini_get( 'safe_mode' ) ) {
260 set_time_limit( 0 );
261 ini_set( 'memory_limit', '256M' );
262 ini_set( 'implicit_flush', '1' );
263 ignore_user_abort( true );
264 }
265
266 // Save step and count so that it can be restarted.
267 if ( ! get_option( '_bbp_converter_step' ) || ( !empty( $_POST['_bbp_converter_restart'] ) ) ) {
268 update_option( '_bbp_converter_step', 1 );
269 update_option( '_bbp_converter_start', 0 );
270 }
271
272 $step = (int) get_option( '_bbp_converter_step', 1 );
273 $min = (int) get_option( '_bbp_converter_start', 0 );
274 $count = (int) ! empty( $_POST['_bbp_converter_rows'] ) ? $_POST['_bbp_converter_rows'] : 100;
275 $max = ( $min + $count ) - 1;
276 $start = $min;
277
278 // Bail if platform did not get saved
279 $platform = !empty( $_POST['_bbp_converter_platform' ] ) ? $_POST['_bbp_converter_platform' ] : get_option( '_bbp_converter_platform' );
280 if ( empty( $platform ) )
281 return;
282
283 // Include the appropriate converter.
284 $converter = bbp_new_converter( $platform );
285
286 switch ( $step ) {
287
288 // STEP 1. Clean all tables.
289 case 1 :
290 if ( !empty( $_POST['_bbp_converter_clean'] ) ) {
291 if ( $converter->clean( $start ) ) {
292 update_option( '_bbp_converter_step', $step + 1 );
293 update_option( '_bbp_converter_start', 0 );
294 $this->sync_table();
295 if ( empty( $start ) ) {
296 _e( 'No data to clean', 'bbpress' );
297 }
298 } else {
299 update_option( '_bbp_converter_start', $max + 1 );
300 printf( __( 'Deleting previously converted data (%1$s - %2$s)', 'bbpress' ), $min, $max );
301 }
302 } else {
303 update_option( '_bbp_converter_step', $step + 1 );
304 update_option( '_bbp_converter_start', 0 );
305 }
306
307 break;
308
309 // STEP 2. Convert users.
310 case 2 :
311 if ( !empty( $_POST['_bbp_converter_convert_users'] ) ) {
312 if ( $converter->convert_users( $start ) ) {
313 update_option( '_bbp_converter_step', $step + 1 );
314 update_option( '_bbp_converter_start', 0 );
315 if ( empty( $start ) ) {
316 _e( 'No users to convert', 'bbpress' );
317 }
318 } else {
319 update_option( '_bbp_converter_start', $max + 1 );
320 printf( __( 'Converting users (%1$s - %2$s)', 'bbpress' ), $min, $max );
321 }
322 } else {
323 update_option( '_bbp_converter_step', $step + 1 );
324 update_option( '_bbp_converter_start', 0 );
325 }
326
327 break;
328
329 // STEP 3. Clean passwords.
330 case 3 :
331 if ( !empty( $_POST['_bbp_converter_convert_users'] ) ) {
332 if ( $converter->clean_passwords( $start ) ) {
333 update_option( '_bbp_converter_step', $step + 1 );
334 update_option( '_bbp_converter_start', 0 );
335 if ( empty( $start ) ) {
336 _e( 'No passwords to clear', 'bbpress' );
337 }
338 } else {
339 update_option( '_bbp_converter_start', $max + 1 );
340 printf( __( 'Delete users wordpress default passwords (%1$s - %2$s)', 'bbpress' ), $min, $max );
341 }
342 } else {
343 update_option( '_bbp_converter_step', $step + 1 );
344 update_option( '_bbp_converter_start', 0 );
345 }
346
347 break;
348
349 // STEP 4. Convert forums.
350 case 4 :
351 if ( $converter->convert_forums( $start ) ) {
352 update_option( '_bbp_converter_step', $step + 1 );
353 update_option( '_bbp_converter_start', 0 );
354 if ( empty( $start ) ) {
355 _e( 'No forums to convert', 'bbpress' );
356 }
357 } else {
358 update_option( '_bbp_converter_start', $max + 1 );
359 printf( __( 'Converting forums (%1$s - %2$s)', 'bbpress' ), $min, $max );
360 }
361
362 break;
363
364 // STEP 5. Convert forum parents.
365 case 5 :
366
367 if ( $converter->convert_forum_parents( $start ) ) {
368 update_option( '_bbp_converter_step', $step + 1 );
369 update_option( '_bbp_converter_start', 0 );
370 if ( empty( $start ) ) {
371 _e( 'No forum parents to convert', 'bbpress' );
372 }
373 } else {
374 update_option( '_bbp_converter_start', $max + 1 );
375 printf( __( 'Calculating forum hierarchy (%1$s - %2$s)', 'bbpress' ), $min, $max );
376 }
377
378 break;
379
380 // STEP 6. Convert topics.
381 case 6 :
382
383 if ( $converter->convert_topics( $start ) ) {
384 update_option( '_bbp_converter_step', $step + 1 );
385 update_option( '_bbp_converter_start', 0 );
386 if ( empty( $start ) ) {
387 _e( 'No topics to convert', 'bbpress' );
388 }
389 } else {
390 update_option( '_bbp_converter_start', $max + 1 );
391 printf( __( 'Converting topics (%1$s - %2$s)', 'bbpress' ), $min, $max );
392 }
393
394 break;
395
396 // STEP 7. Convert tags.
397 case 7 :
398
399 if ( $converter->convert_tags( $start ) ) {
400 update_option( '_bbp_converter_step', $step + 1 );
401 update_option( '_bbp_converter_start', 0 );
402 if ( empty( $start ) ) {
403 _e( 'No tags to convert', 'bbpress' );
404 }
405 } else {
406 update_option( '_bbp_converter_start', $max + 1 );
407 printf( __( 'Converting topic tags (%1$s - %2$s)', 'bbpress' ), $min, $max );
408 }
409
410 break;
411
412 // STEP 8. Convert replies.
413 case 8 :
414 if ( $converter->convert_replies( $start ) ) {
415 update_option( '_bbp_converter_step', $step + 1 );
416 update_option( '_bbp_converter_start', 0 );
417 if ( empty( $start ) ) {
418 _e( 'No replies to convert', 'bbpress' );
419 }
420 } else {
421 update_option( '_bbp_converter_start', $max + 1 );
422 printf( __( 'Converting replies (%1$s - %2$s)', 'bbpress' ), $min, $max );
423 }
424
425 break;
426
427 default :
428 delete_option( '_bbp_converter_step' );
429 delete_option( '_bbp_converter_start' );
430
431 _e( 'Conversion Complete', 'bbpress' );
432
433 break;
434
435 }
436 }
437
438 /**
439 * Convert passwords from previous forum to wordpress.
440 *
441 * @since bbPress (r3813)
442 * @global WPDB $wpdb
443 */
444 public function convert_pass() {
445
446 $username = !empty( $_POST['log'] ) ? $_POST['log'] : '';
447
448 if ( !empty( $username ) ) {
449
450 global $wpdb;
451
452 $row = $wpdb->get_row( "SELECT * FROM {$wpdb->users} INNER JOIN {$wpdb->usermeta} ON user_id = ID WHERE meta_key = '_bbp_converter_class' AND user_login = '{$username}' LIMIT 1" );
453
454 if ( !empty( $row ) ) {
455 $converter = bbp_new_converter( $row->meta_value );
456 $converter->callback_pass( $username, $_POST['pwd'] );
457 }
458 }
459 }
460
461 /**
462 * Create Tables for fast syncing
463 *
464 * @since bbPress (r3813)
465 */
466 public function sync_table( $drop = false ) {
467 global $wpdb;
468
469 $table_name = $wpdb->prefix . 'bbp_converter_translator';
470 if ( $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" ) == $table_name ) {
471 $wpdb->query( "DROP TABLE {$table_name}" );
472 }
473
474 if ( empty( $drop ) ) {
475 require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
476
477 if ( !empty( $wpdb->charset ) ) {
478 $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
479 }
480
481 if ( !empty( $wpdb->collate ) ) {
482 $charset_collate .= " COLLATE $wpdb->collate";
483 }
484
485 /** Translator ****************************************************/
486
487 $sql = "CREATE TABLE {$table_name} (
488 meta_id mediumint(8) unsigned not null auto_increment,
489 value_type varchar(25) null,
490 value_id bigint(20) unsigned not null default '0',
491 meta_key varchar(25) null,
492 meta_value varchar(25) null,
493 PRIMARY KEY (meta_id),
494 KEY value_id (value_id),
495 KEY meta_join (meta_key, meta_value) ) {$charset_collate};";
496
497 dbDelta( $sql );
498 }
499 }
500 }
501
502 /**
503 * Base class to be extended by specific individual importers
504 *
505 * @since bbPress (r3813)
506 */
507 abstract class BBP_Converter_Base {
508
509 /**
510 * @var array() This is the field mapping array to process.
511 */
512 protected $field_map = array();
513
514 /**
515 * @var object This is the connection to the wordpress datbase.
516 */
517 protected $wpdb;
518
519 /**
520 * @var object This is the connection to the other platforms database.
521 */
522 protected $opdb;
523
524 /**
525 * @var int This is the max rows to process at a time.
526 */
527 public $max_rows;
528
529 /**
530 * @var array() Map of topic to forum. It is for optimization.
531 */
532 private $map_topicid_to_forumid = array();
533
534 /**
535 * @var array() Map of from old forum ids to new forum ids. It is for optimization.
536 */
537 private $map_forumid = array();
538
539 /**
540 * @var array() Map of from old topic ids to new topic ids. It is for optimization.
541 */
542 private $map_topicid = array();
543
544 /**
545 * @var array() Map of from old user ids to new user ids. It is for optimization.
546 */
547 private $map_userid = array();
548
549 /**
550 * @var str This is the charset for your wp database.
551 */
552 public $charset;
553
554 /**
555 * @var boolean Sync table available.
556 */
557 public $sync_table = false;
558
559 /**
560 * @var str Sync table name.
561 */
562 public $sync_table_name;
563
564 /** Methods ***************************************************************/
565
566 /**
567 * This is the constructor and it connects to the platform databases.
568 */
569 public function __construct() {
570 $this->setup_globals();
571 }
572
573 private function setup_globals() {
574 global $wpdb;
575
576 /** Get database connections ******************************************/
577
578 $this->wpdb = $wpdb;
579 $this->max_rows = (int) $_POST['_bbp_converter_rows'];
580 $this->opdb = new wpdb( $_POST['_bbp_converter_db_user'], $_POST['_bbp_converter_db_pass'], $_POST['_bbp_converter_db_name'], $_POST['_bbp_converter_db_server'] );
581 $this->opdb->prefix = $_POST['_bbp_converter_db_prefix'];
582
583 /**
584 * Error Reporting
585 */
586 $this->wpdb->show_errors();
587 $this->opdb->show_errors();
588
589 /**
590 * Syncing
591 */
592 $this->sync_table_name = $this->wpdb->prefix . 'bbp_converter_translator';
593 if ( $this->wpdb->get_var( "SHOW TABLES LIKE '" . $this->sync_table_name . "'" ) == $this->sync_table_name ) {
594 $this->sync_table = true;
595 } else {
596 $this->sync_table = false;
597 }
598
599 /**
600 * Charset
601 */
602 if ( empty( $this->wpdb->charset ) ) {
603 $this->charset = 'UTF8';
604 } else {
605 $this->charset = $this->wpdb->charset;
606 }
607
608 /**
609 * Default mapping.
610 */
611
612 /** Forum Section *****************************************************/
613
614 $this->field_map[] = array(
615 'to_type' => 'forum',
616 'to_fieldname' => 'post_status',
617 'default' => 'publish'
618 );
619 $this->field_map[] = array(
620 'to_type' => 'forum',
621 'to_fieldname' => 'comment_status',
622 'default' => 'closed'
623 );
624 $this->field_map[] = array(
625 'to_type' => 'forum',
626 'to_fieldname' => 'ping_status',
627 'default' => 'closed'
628 );
629 $this->field_map[] = array(
630 'to_type' => 'forum',
631 'to_fieldname' => 'post_type',
632 'default' => 'forum'
633 );
634
635 /** Topic Section *****************************************************/
636
637 $this->field_map[] = array(
638 'to_type' => 'topic',
639 'to_fieldname' => 'post_status',
640 'default' => 'publish'
641 );
642 $this->field_map[] = array(
643 'to_type' => 'topic',
644 'to_fieldname' => 'comment_status',
645 'default' => 'closed'
646 );
647 $this->field_map[] = array(
648 'to_type' => 'topic',
649 'to_fieldname' => 'ping_status',
650 'default' => 'closed'
651 );
652 $this->field_map[] = array(
653 'to_type' => 'topic',
654 'to_fieldname' => 'post_type',
655 'default' => 'topic'
656 );
657
658 /** Post Section ******************************************************/
659
660 $this->field_map[] = array(
661 'to_type' => 'reply',
662 'to_fieldname' => 'post_status',
663 'default' => 'publish'
664 );
665 $this->field_map[] = array(
666 'to_type' => 'reply',
667 'to_fieldname' => 'comment_status',
668 'default' => 'closed'
669 );
670 $this->field_map[] = array(
671 'to_type' => 'reply',
672 'to_fieldname' => 'ping_status',
673 'default' => 'closed'
674 );
675 $this->field_map[] = array(
676 'to_type' => 'reply',
677 'to_fieldname' => 'post_type',
678 'default' => 'reply'
679 );
680
681 /** User Section ******************************************************/
682
683 $default_role = ( is_multisite() && get_option( '_bbp_allow_global_access' ) ) ? bbp_get_participant_role() : get_option( 'default_role' );
684 $this->field_map[] = array(
685 'to_type' => 'user',
686 'to_fieldname' => 'role',
687 'default' => $default_role
688 );
689 }
690
691 /**
692 * Convert Forums
693 */
694 public function convert_forums( $start = 1 ) {
695 return $this->convert_table( 'forum', $start );
696 }
697
698 /**
699 * Convert Topics / Threads
700 */
701 public function convert_topics( $start = 1 ) {
702 return $this->convert_table( 'topic', $start );
703 }
704
705 /**
706 * Convert Posts
707 */
708 public function convert_replies( $start = 1 ) {
709 return $this->convert_table( 'reply', $start );
710 }
711
712 /**
713 * Convert Users
714 */
715 public function convert_users( $start = 1 ) {
716 return $this->convert_table( 'user', $start );
717 }
718
719 /**
720 * Convert Tags
721 */
722 public function convert_tags( $start = 1 ) {
723 return $this->convert_table( 'tags', $start );
724 }
725
726 /**
727 * Convert Table
728 *
729 * @param string to type
730 * @param int Start row
731 */
732 public function convert_table( $to_type, $start ) {
733
734 // Are we usig a sync table, or postmeta?
735 if ( $this->wpdb->get_var( "SHOW TABLES LIKE '" . $this->sync_table_name . "'" ) == $this->sync_table_name ) {
736 $this->sync_table = true;
737 } else {
738 $this->sync_table = false;
739 }
740
741 // Set some defaults
742 $has_insert = false;
743 $from_tablename = '';
744 $field_list = $from_tables = $tablefield_array = array();
745
746 // Toggle Table Name based on $to_type (destination)
747 switch ( $to_type ) {
748 case 'user' :
749 $tablename = $this->wpdb->users;
750 break;
751
752 case 'tags' :
753 $tablename = '';
754 break;
755
756 default :
757 $tablename = $this->wpdb->posts;
758 }
759
760 // Get the fields from the destination table
761 if ( !empty( $tablename ) ) {
762 $tablefield_array = $this->get_fields( $tablename );
763 }
764
765 /** Step 1 ************************************************************/
766
767 // Loop through the field maps, and look for to_type matches
768 foreach ( $this->field_map as $item ) {
769
770 // Yay a match, and we have a from table, too
771 if ( ( $item['to_type'] == $to_type ) && !empty( $item['from_tablename'] ) ) {
772
773 // $from_tablename was set from a previous loop iteration
774 if ( ! empty( $from_tablename ) ) {
775
776 // Doing some joining
777 if ( !in_array( $item['from_tablename'], $from_tables ) && in_array( $item['join_tablename'], $from_tables ) ) {
778 $from_tablename .= ' ' . $item['join_type'] . ' JOIN ' . $this->opdb->prefix . $item['from_tablename'] . ' AS ' . $item['from_tablename'] . ' ' . $item['join_expression'];
779 }
780
781 // $from_tablename needs to be set
782 } else {
783 $from_tablename = $item['from_tablename'] . ' AS ' . $item['from_tablename'];
784 }
785
786 // Specific FROM expression data used
787 if ( !empty( $item['from_expression'] ) ) {
788
789 // No 'WHERE' in expression
790 if ( stripos( $from_tablename, "WHERE" ) === false ) {
791 $from_tablename .= ' ' . $item['from_expression'];
792
793 // 'WHERE' in expression, so replace with 'AND'
794 } else {
795 $from_tablename .= ' ' . str_replace( "WHERE", "AND", $item['from_expression'] );
796 }
797 }
798
799 // Add tablename and fieldname to arrays, formatted for querying
800 $from_tables[] = $item['from_tablename'];
801 $field_list[] = 'convert(' . $item['from_tablename'] . '.' . $item['from_fieldname'] . ' USING "' . $this->charset . '") AS ' . $item['from_fieldname'];
802 }
803 }
804
805 /** Step 2 ************************************************************/
806
807 // We have a $from_tablename, so we want to get some data to convert
808 if ( !empty( $from_tablename ) ) {
809
810 // Get some data from the old forums
811 $field_list = array_unique( $field_list );
812 $forum_query = 'SELECT ' . implode( ',', $field_list ) . ' FROM ' . $this->opdb->prefix . $from_tablename . ' LIMIT ' . $start . ', ' . $this->max_rows;
813 $forum_array = $this->opdb->get_results( $forum_query, ARRAY_A );
814
815 // Output the query, for better debugging
816 printf( __( '<span title="%s">View Query</span>%s', 'bbpress' ), esc_attr( $forum_query ), '<br />' );
817
818 // Query returned some results
819 if ( !empty( $forum_array ) ) {
820
821 // Loop through results
822 foreach ( (array) $forum_array as $forum ) {
823
824 // Reset some defaults
825 $insert_post = $insert_postmeta = $insert_data = array();
826
827 // Loop through field map, again...
828 foreach ( $this->field_map as $row ) {
829
830 // Types matchand to_fieldname is present. This means
831 // we have some work to do here.
832 if ( ( $row['to_type'] == $to_type ) && ! is_null( $row['to_fieldname'] ) ) {
833
834 // This row has a destination that matches one of the
835 // columns in this table.
836 if ( in_array( $row['to_fieldname'], $tablefield_array ) ) {
837
838 // Allows us to set default fields.
839 if ( isset( $row['default'] ) ) {
840 $insert_post[$row['to_fieldname']] = $row['default'];
841
842 // Translates a field from the old forum.
843 } elseif ( isset( $row['callback_method'] ) ) {
844 if ( ( 'callback_userid' == $row['callback_method'] ) && empty( $_POST['_bbp_converter_convert_users'] ) ) {
845 $insert_post[$row['to_fieldname']] = $forum[$row['from_fieldname']];
846 } else {
847 $insert_post[$row['to_fieldname']] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[$row['from_fieldname']], $forum ) );
848 }
849
850 // Maps the field from the old forum.
851 } else {
852 $insert_post[$row['to_fieldname']] = $forum[$row['from_fieldname']];
853 }
854
855 // Destination field is not empty, so we might need
856 // to do some extra work or set a default.
857 } elseif ( !empty( $row['to_fieldname'] ) ) {
858
859 // Allows us to set default fields.
860 if ( isset( $row['default'] ) ) {
861 $insert_postmeta[$row['to_fieldname']] = $row['default'];
862
863 // Translates a field from the old forum.
864 } elseif ( isset( $row['callback_method'] ) ) {
865 if ( ( $row['callback_method'] == 'callback_userid' ) && ( 0 == $_POST['_bbp_converter_convert_users'] ) ) {
866 $insert_postmeta[$row['to_fieldname']] = $forum[$row['from_fieldname']];
867 } else {
868 $insert_postmeta[$row['to_fieldname']] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[$row['from_fieldname']], $forum ) );
869 }
870
871 // Maps the field from the old forum.
872 } else {
873 $insert_postmeta[$row['to_fieldname']] = $forum[$row['from_fieldname']];
874 }
875 }
876 }
877 }
878
879 /** Step 3 ************************************************/
880
881 // Something to insert into the destination field
882 if ( count( $insert_post ) > 0 || ( $to_type == 'tags' && count( $insert_postmeta ) > 0 ) ) {
883
884 switch ( $to_type ) {
885
886 /** New user **************************************/
887
888 case 'user':
889 if ( username_exists( $insert_post['user_login'] ) ) {
890 $insert_post['user_login'] = 'imported_' . $insert_post['user_login'];
891 }
892
893 if ( email_exists( $insert_post['user_email'] ) ) {
894 $insert_post['user_email'] = 'imported_' . $insert_post['user_email'];
895 }
896
897 $post_id = wp_insert_user( $insert_post );
898
899 if ( is_numeric( $post_id ) ) {
900
901 foreach ( $insert_postmeta as $key => $value ) {
902
903 add_user_meta( $post_id, $key, $value, true );
904
905 if ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) {
906 $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'user', 'value_id' => $post_id, 'meta_key' => $key, 'meta_value' => $value ) );
907 }
908 }
909 }
910 break;
911
912 /** New Topic-Tag *********************************/
913
914 case 'tags':
915 $post_id = wp_set_object_terms( $insert_postmeta['objectid'], $insert_postmeta['name'], 'topic-tag', true );
916 break;
917
918 /** Forum, Topic, Reply ***************************/
919
920 default:
921 $post_id = wp_insert_post( $insert_post );
922
923 if ( is_numeric( $post_id ) ) {
924
925 foreach ( $insert_postmeta as $key => $value ) {
926
927 add_post_meta( $post_id, $key, $value, true );
928
929 // Topics need an extra bit of metadata
930 // to be keyed to the new post_id
931 if ( ( 'topic' == $to_type ) && ( '_bbp_topic_id' == $key ) ) {
932
933 // Update the live topic ID
934 update_post_meta( $post_id, $key, $post_id );
935
936 // Save the old topic ID
937 add_post_meta( $post_id, '_bbp_old_topic_id', $value );
938 if ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) {
939 $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'post', 'value_id' => $post_id, 'meta_key' => '_bbp_topic_id', 'meta_value' => $post_id ) );
940 $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'post', 'value_id' => $post_id, 'meta_key' => '_bbp_old_topic_id', 'meta_value' => $value ) );
941 }
942
943 } elseif ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) {
944 $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'post', 'value_id' => $post_id, 'meta_key' => $key, 'meta_value' => $value ) );
945 }
946 }
947 }
948 break;
949 }
950 $has_insert = true;
951 }
952 }
953 }
954 }
955
956 return ! $has_insert;
957 }
958
959 public function convert_forum_parents( $start ) {
960 $has_update = false;
961
962 if ( !empty( $this->sync_table ) ) {
963 $forum_array = $this->wpdb->get_results( 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_forum_parent_id" AND meta_value > 0 LIMIT ' . $start . ', ' . $this->max_rows );
964 } else {
965 $forum_array = $this->wpdb->get_results( 'SELECT post_id AS value_id, meta_value FROM ' . $this->wpdb->postmeta . ' WHERE meta_key = "_bbp_forum_parent_id" AND meta_value > 0 LIMIT ' . $start . ', ' . $this->max_rows );
966 }
967
968 foreach ( (array) $forum_array as $row ) {
969 $parent_id = $this->callback_forumid( $row->meta_value );
970 $this->wpdb->query( 'UPDATE ' . $this->wpdb->posts . ' SET post_parent = "' . $parent_id . '" WHERE ID = "' . $row->value_id . '" LIMIT 1' );
971 $has_update = true;
972 }
973
974 return ! $has_update;
975 }
976
977 /**
978 * This method deletes data from the wp database.
979 */
980 public function clean( $start ) {
981 $start = 0;
982 $has_delete = false;
983
984 /** Delete bbconverter topics/forums/posts ****************************/
985
986 if ( true === $this->sync_table ) {
987 $bbconverter = $this->wpdb->get_results( 'SELECT value_id FROM ' . $this->sync_table_name . ' INNER JOIN ' . $this->wpdb->posts . ' ON(value_id = ID) WHERE meta_key LIKE "_bbp_%" AND value_type = "post" GROUP BY value_id ORDER BY value_id DESC LIMIT ' . $this->max_rows, ARRAY_A );
988 } else {
989 $bbconverter = $this->wpdb->get_results( 'SELECT post_id AS value_id FROM ' . $this->wpdb->postmeta . ' WHERE meta_key LIKE "_bbp_%" GROUP BY post_id ORDER BY post_id DESC LIMIT ' . $this->max_rows, ARRAY_A );
990 }
991
992 if ( !empty( $bbconverter ) ) {
993 foreach ( (array) $bbconverter as $value ) {
994 wp_delete_post( $value['value_id'], true );
995 }
996 $has_delete = true;
997 }
998
999 /** Delete bbconverter users ******************************************/
1000
1001 if ( true === $this->sync_table ) {
1002 $bbconverter = $this->wpdb->get_results( 'SELECT value_id FROM ' . $this->sync_table_name . ' INNER JOIN ' . $this->wpdb->users . ' ON(value_id = ID) WHERE meta_key = "_bbp_user_id" AND value_type = "user" LIMIT ' . $this->max_rows, ARRAY_A );
1003 } else {
1004 $bbconverter = $this->wpdb->get_results( 'SELECT user_id AS value_id FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_user_id" LIMIT ' . $this->max_rows, ARRAY_A );
1005 }
1006
1007 if ( !empty( $bbconverter ) ) {
1008 foreach ( $bbconverter as $value ) {
1009 wp_delete_user( $value['value_id'] );
1010 }
1011 $has_delete = true;
1012 }
1013
1014 return ! $has_delete;
1015 }
1016
1017 /**
1018 * This method deletes passwords from the wp database.
1019 *
1020 * @param int Start row
1021 */
1022 public function clean_passwords( $start ) {
1023 $has_delete = false;
1024
1025 /** Delete bbconverter passwords **************************************/
1026
1027 $bbconverter = $this->wpdb->get_results( 'SELECT user_id, meta_value FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_password" LIMIT ' . $start . ', ' . $this->max_rows, ARRAY_A );
1028 if ( !empty( $bbconverter ) ) {
1029
1030 foreach ( $bbconverter as $value ) {
1031 if ( is_serialized( $value['meta_value'] ) ) {
1032 $this->wpdb->query( 'UPDATE ' . $this->wpdb->users . ' ' . 'SET user_pass = "" ' . 'WHERE ID = "' . $value['user_id'] . '"' );
1033 } else {
1034 $this->wpdb->query( 'UPDATE ' . $this->wpdb->users . ' ' . 'SET user_pass = "' . $value['meta_value'] . '" ' . 'WHERE ID = "' . $value['user_id'] . '"' );
1035 $this->wpdb->query( 'DELETE FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_password" AND user_id = "' . $value['user_id'] . '"' );
1036 }
1037 }
1038 $has_delete = true;
1039 }
1040 return ! $has_delete;
1041 }
1042
1043 /**
1044 * This method implements the authentication for the different forums.
1045 *
1046 * @param string Unencoded password.
1047 */
1048 abstract protected function authenticate_pass( $password, $hash );
1049
1050 /**
1051 * Info
1052 */
1053 abstract protected function info();
1054
1055 /**
1056 * This method grabs appropriate fields from the table specified
1057 *
1058 * @param string The table name to grab fields from
1059 */
1060 private function get_fields( $tablename ) {
1061 $rval = array();
1062 $field_array = $this->wpdb->get_results( 'DESCRIBE ' . $tablename, ARRAY_A );
1063
1064 foreach ( $field_array as $field ) {
1065 $rval[] = $field['Field'];
1066 }
1067
1068 if ( $tablename == $this->wpdb->users ) {
1069 $rval[] = 'role';
1070 $rval[] = 'yim';
1071 $rval[] = 'aim';
1072 $rval[] = 'jabber';
1073 }
1074 return $rval;
1075 }
1076
1077 /** Callbacks *************************************************************/
1078
1079 /**
1080 * Run password through wp_hash_password()
1081 *
1082 * @param string $username
1083 * @param string $password
1084 */
1085 public function callback_pass( $username, $password ) {
1086 $user = $this->wpdb->get_row( 'SELECT * FROM ' . $this->wpdb->users . ' WHERE user_login = "' . $username . '" AND user_pass = "" LIMIT 1' );
1087 if ( !empty( $user ) ) {
1088 $usermeta = $this->wpdb->get_row( 'SELECT * FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_password" AND user_id = "' . $user->ID . '" LIMIT 1' );
1089
1090 if ( !empty( $usermeta ) ) {
1091 if ( $this->authenticate_pass( $password, $usermeta->meta_value ) ) {
1092 $this->wpdb->query( 'UPDATE ' . $this->wpdb->users . ' ' . 'SET user_pass = "' . wp_hash_password( $password ) . '" ' . 'WHERE ID = "' . $user->ID . '"' );
1093 $this->wpdb->query( 'DELETE FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_password" AND user_id = "' . $user->ID . '"' );
1094 }
1095 }
1096 }
1097 }
1098
1099 /**
1100 * A mini cache system to reduce database calls to forum ID's
1101 *
1102 * @param string $field
1103 * @return string
1104 */
1105 private function callback_forumid( $field ) {
1106 if ( !isset( $this->map_forumid[$field] ) ) {
1107 if ( !empty( $this->sync_table ) ) {
1108 $row = $this->wpdb->get_row( 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_forum_id" AND meta_value = "' . $field . '" LIMIT 1' );
1109 } else {
1110 $row = $this->wpdb->get_row( 'SELECT post_id AS value_id FROM ' . $this->wpdb->postmeta . ' WHERE meta_key = "_bbp_forum_id" AND meta_value = "' . $field . '" LIMIT 1' );
1111 }
1112
1113 if ( !is_null( $row ) ) {
1114 $this->map_forumid[$field] = $row->value_id;
1115 } else {
1116 $this->map_forumid[$field] = 0;
1117 }
1118 }
1119 return $this->map_forumid[$field];
1120 }
1121
1122 /**
1123 * A mini cache system to reduce database calls to topic ID's
1124 *
1125 * @param string $field
1126 * @return string
1127 */
1128 private function callback_topicid( $field ) {
1129 if ( !isset( $this->map_topicid[$field] ) ) {
1130 if ( !empty( $this->sync_table ) ) {
1131 $row = $this->wpdb->get_row( 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_old_topic_id" AND meta_value = "' . $field . '" LIMIT 1' );
1132 } else {
1133 $row = $this->wpdb->get_row( 'SELECT post_id AS value_id FROM ' . $this->wpdb->postmeta . ' WHERE meta_key = "_bbp_old_topic_id" AND meta_value = "' . $field . '" LIMIT 1' );
1134 }
1135
1136 if ( !is_null( $row ) ) {
1137 $this->map_topicid[$field] = $row->value_id;
1138 } else {
1139 $this->map_topicid[$field] = 0;
1140 }
1141 }
1142 return $this->map_topicid[$field];
1143 }
1144
1145 /**
1146 * A mini cache system to reduce database calls to user ID's
1147 *
1148 * @param string $field
1149 * @return string
1150 */
1151 private function callback_userid( $field ) {
1152 if ( !isset( $this->map_userid[$field] ) ) {
1153 if ( !empty( $this->sync_table ) ) {
1154 $row = $this->wpdb->get_row( 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_user_id" AND meta_value = "' . $field . '" LIMIT 1' );
1155 } else {
1156 $row = $this->wpdb->get_row( 'SELECT user_id AS value_id FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_user_id" AND meta_value = "' . $field . '" LIMIT 1' );
1157 }
1158
1159 if ( !is_null( $row ) ) {
1160 $this->map_userid[$field] = $row->value_id;
1161 } else {
1162 if ( !empty( $_POST['_bbp_converter_convert_users'] ) && ( $_POST['_bbp_converter_convert_users'] == 1 ) ) {
1163 $this->map_userid[$field] = 0;
1164 } else {
1165 $this->map_userid[$field] = $field;
1166 }
1167 }
1168 }
1169 return $this->map_userid[$field];
1170 }
1171
1172 /**
1173 * A mini cache system to reduce database calls map topics ID's to forum ID's
1174 *
1175 * @param string $field
1176 * @return string
1177 */
1178 private function callback_topicid_to_forumid( $field ) {
1179 $topicid = $this->callback_topicid( $field );
1180 if ( empty( $topicid ) ) {
1181 $this->map_topicid_to_forumid[$topicid] = 0;
1182 } elseif ( ! isset( $this->map_topicid_to_forumid[$topicid] ) ) {
1183 $row = $this->wpdb->get_row( 'SELECT post_parent FROM ' . $this->wpdb->posts . ' WHERE ID = "' . $topicid . '" LIMIT 1' );
1184
1185 if ( !is_null( $row ) ) {
1186 $this->map_topicid_to_forumid[$topicid] = $row->post_parent;
1187 } else {
1188 $this->map_topicid_to_forumid[$topicid] = 0;
1189 }
1190 }
1191
1192 return $this->map_topicid_to_forumid[$topicid];
1193 }
1194
1195 protected function callback_slug( $field ) {
1196 return sanitize_title_with_dashes( $field );
1197 }
1198
1199 protected function callback_negative( $field ) {
1200 if ( $field < 0 ) {
1201 return 0;
1202 } else {
1203 return $field;
1204 }
1205 }
1206
1207 protected function callback_html( $field ) {
1208 require_once( bbpress()->admin->admin_dir . 'bbp-parser.php' );
1209 $bbcode = BBCode::getInstance();
1210 return html_entity_decode( $bbcode->Parse( $field ) );
1211 }
1212
1213 protected function callback_null( $field ) {
1214 if ( is_null( $field ) ) {
1215 return '';
1216 } else {
1217 return $field;
1218 }
1219 }
1220
1221 protected function callback_datetime( $field ) {
1222 if ( is_numeric( $field ) ) {
1223 return date( 'Y-m-d H:i:s', $field );
1224 } else {
1225 return date( 'Y-m-d H:i:s', strtotime( $field ) );
1226 }
1227 }
1228 }
1229
1230 /**
1231 * This is a function that is purposely written to look like a "new" statement.
1232 * It is basically a dynamic loader that will load in the platform conversion
1233 * of your choice.
1234 *
1235 * @param string $platform Name of valid platform class.
1236 */
1237 function bbp_new_converter( $platform ) {
1238 $found = false;
1239
1240 if ( $curdir = opendir( bbpress()->admin->admin_dir . 'converters/' ) ) {
1241 while ( $file = readdir( $curdir ) ) {
1242 if ( stristr( $file, '.php' ) && stristr( $file, 'index' ) === FALSE ) {
1243 $file = preg_replace( '/.php/', '', $file );
1244 if ( $platform == $file ) {
1245 $found = true;
1246 continue;
1247 }
1248 }
1249 }
1250 closedir( $curdir );
1251 }
1252
1253 if ( true === $found ) {
1254 require_once( bbpress()->admin->admin_dir . 'converters/' . $platform . '.php' );
1255 return new $platform;
1256 } else {
1257 return null;
1258 }
1259 }
1260