| 427 | | if ( !$this->snoop->fetch( $this->comments_url . '?get=comment_meta&startid=' . ( $highest_id + 1 ) ) ) |
| 428 | | return new WP_Error( 'Snoopy', __( 'Failed to retrieve comment meta information from LiveJournal. Please try again soon.' ) ); |
| 429 | | |
| 430 | | // Snoopy doesn't provide an accessor for results... |
| 431 | | $results = $this->snoop->results; |
| | 428 | $results = wp_remote_get( $this->comments_url . '?get=comment_meta&startid=' . ( $highest_id + 1 ), |
| | 429 | array( 'cookies' => array( $cookie ), 'timeout' => 20 ) ); |
| | 430 | if ( is_wp_error( $results ) ) |
| | 431 | return new WP_Error( 'comment_meta', __( 'Failed to retrieve comment meta information from LiveJournal. Please try again soon.' ) ); |
| 456 | | // Download Comment XML |
| 457 | | |
| 458 | | // Load previous state (if any) |
| 459 | | $highest_id = (int) get_option( 'ljapi_highest_comment_id' ); |
| 460 | | $comment_xml_files = get_option( 'ljapi_comment_xml_files' ); |
| 461 | | if ( !is_array( $comment_xml_files ) ) { |
| 462 | | update_option( 'ljapi_comment_xml_files', array() ); |
| 463 | | $comment_xml_files = array(); |
| 464 | | } |
| 465 | | |
| 466 | | echo '<ol>'; |
| 467 | | |
| 468 | | // And now request the actual comments, and keep going until we have them all |
| 469 | | while ( $maxid > $highest_id ) { |
| 470 | | // Get a batch of comments, using the highest_id we've already got as a starting point |
| 471 | | if ( !$this->snoop->fetch( $this->comments_url . '?get=comment_body&startid=' . ( $highest_id + 1 ) ) ) |
| 472 | | return new WP_Error( 'Snoopy', __( 'Failed to retrieve comment bodies from LiveJournal. Please try again soon.' ) ); |
| 473 | | |
| 474 | | // Get the highest post ID in this batch (required for loop control) |
| 475 | | $results = $this->snoop->results; |
| 476 | | preg_match_all( '|<comment id=\'(\d+)\'|i', $results, $comments ); |
| 477 | | for ( $r = 0; $r < count( $comments[1] ); $r++ ) { |
| 478 | | if ( $comments[1][$r] > $highest_id ) |
| 479 | | $highest_id = $comments[1][$r]; |
| 480 | | } |
| 481 | | |
| 482 | | // $this->snoop-results is where the actual response is stored |
| 483 | | $this->log( $this->snoop->results, 'ljimport-comment-bodies-' . $highest_id . '.txt' ); |
| 484 | | |
| 485 | | // Store in uploads dir. Can't use *.xml because it's not allowed |
| 486 | | $results = wp_upload_bits( 'raw-comments-' . $highest_id . '.txt', null, $results ); |
| 487 | | if ( !empty( $results['error'] ) ) |
| 488 | | return new WP_Error( 'xml', $results['error'] ); |
| 489 | | $comment_xml_files[] = $results['file']; |
| 490 | | |
| 491 | | echo '<li>' . sprintf( __( 'Downloaded <strong>%s</strong>' ), basename( $results['file'] ) ) . '</li>'; |
| 492 | | ob_flush(); flush(); |
| 493 | | |
| 494 | | $comment_xml_files = array_unique( $comment_xml_files ); |
| 495 | | update_option( 'ljapi_comment_xml_files', $comment_xml_files ); |
| 496 | | update_option( 'ljapi_comment_xml_files_count', count( $comment_xml_files ) ); |
| 497 | | } |
| 498 | | // endwhile - all comments downloaded and ready for bulk processing |
| 499 | | |
| 500 | | echo '</ol>'; |
| 501 | | |
| 505 | | function parse_comment_xml( $xml_file ) { |
| 506 | | if ( !is_file( $xml_file ) || !is_readable( $xml_file ) ) |
| 507 | | return new WP_Error( 'file', sprintf( __( 'Could not access comment XML file: %s'), $filename ) ); |
| 508 | | |
| 509 | | // Get content from file |
| 510 | | $xml = @file_get_contents( $xml_file ); |
| 511 | | |
| 512 | | $cache_files = get_option( 'ljapi_comment_cache_files' ); |
| 513 | | if ( !is_array( $cache_files ) ) |
| 514 | | $cache_files = array(); |
| | 471 | // Downloads actual comment bodies from LJ |
| | 472 | // Inserts them all directly to the DB, with additional info stored in "spare" fields |
| | 473 | function download_comment_bodies() { |
| | 474 | global $wpdb; |
| | 475 | $cookie = $this->getsession(); |
| 516 | | // Parse XML into comments |
| 517 | | preg_match_all( '|<comment id.*</comment>|iUs', $xml, $matches ); |
| 518 | | unset( $xml ); |
| 519 | | for ( $c = 0; $c < count( $matches[0] ); $c++ ) { |
| 520 | | $comment = $matches[0][$c]; |
| | 477 | // Load previous state (if any) |
| | 478 | $this->usermap = (array) get_option( 'ljapi_usermap' ); |
| | 479 | $maxid = get_option( 'ljapi_maxid' ) ? (int) get_option( 'ljapi_maxid' ) : 1; |
| | 480 | $highest_id = (int) get_option( 'ljapi_highest_comment_id' ); |
| | 481 | $loop = 0; |
| | 482 | while ( $maxid > $highest_id && $loop < 5 ) { // We do 5 loops per call to avoid memory limits |
| | 483 | $loop++; |
| 522 | | // Filter out any captured, deleted comments (nothing useful to import) |
| 523 | | $comment = preg_replace( '|<comment id=\'\d+\' jitemid=\'\d+\' posterid=\'\d+\' state=\'D\'[^/]*/>|is', '', $comment ); |
| | 485 | // Get a batch of comments, using the highest_id we've already got as a starting point |
| | 486 | $results = wp_remote_get( $this->comments_url . '?get=comment_body&startid=' . ( $highest_id + 1 ), |
| | 487 | array( 'cookies' => array( $cookie ), 'timeout' => 20 ) ); |
| | 488 | if ( is_wp_error( $results ) ) |
| | 489 | return new WP_Error( 'comment_bodies', __( 'Failed to retrieve comment bodies from LiveJournal. Please try again soon.' ) ); |
| 530 | | // Add this comment to the appropriate cache file |
| 531 | | $filename = $this->full_path( 'ljimport-comments-' . $comment['comment_post_ID'] . '.php' ); |
| 532 | | if ( $this->write_file( '<?php $comments[] = ' . var_export( $comment, true ) . '; ?>' . "\n", |
| 533 | | $filename, |
| 534 | | $comment['comment_post_ID'], |
| 535 | | 'a' ) ) |
| 536 | | { |
| 537 | | // Keep track of files used |
| 538 | | $cache_files[] = $filename; |
| | 493 | // Parse out each comment and insert directly |
| | 494 | preg_match_all( '|<comment id=\'(\d+)\'.*</comment>|iUs', $results, $matches ); |
| | 495 | for ( $c = 0; $c < count( $matches[0] ); $c++ ) { |
| | 496 | // Keep track of highest id seen |
| | 497 | if ( $matches[1][$c] > $highest_id ) { |
| | 498 | $highest_id = $matches[1][$c]; |
| | 499 | update_option( 'ljapi_highest_comment_id', $highest_id ); |
| | 500 | } |
| | 501 | |
| | 502 | $comment = $matches[0][$c]; |
| | 503 | |
| | 504 | // Filter out any captured, deleted comments (nothing useful to import) |
| | 505 | $comment = preg_replace( '|<comment id=\'\d+\' jitemid=\'\d+\' posterid=\'\d+\' state=\'D\'[^/]*/>|is', '', $comment ); |
| | 506 | |
| | 507 | // Parse this comment into an array and insert |
| | 508 | $comment = $this->parse_comment( $comment ); |
| | 509 | $id = wp_insert_comment( $comment ); |
| | 510 | |
| | 511 | // Clear cache |
| | 512 | clean_comment_cache( $id ); |
| 542 | | // Update list of files in the DB |
| 543 | | sort( $cache_files ); |
| 544 | | $cache_files = array_unique( $cache_files ); |
| 545 | | update_option( 'ljapi_comment_cache_files', $cache_files ); |
| 546 | | update_option( 'ljapi_comment_cache_files_count', count( $cache_files ) ); |
| 547 | | $this->close_file_pointers(); |
| | 520 | // Counter just used to show progress to user |
| | 521 | update_option( 'ljapi_comment_batch', ( (int) get_option( 'ljapi_comment_batch' ) + 1 ) ); |
| 565 | | $comment_author_ID = $matches[1]; |
| 566 | | preg_match( '| parentid=\'(\d+)\'|i', $attribs[1], $matches ); |
| 567 | | $lj_comment_parent = $matches[1]; |
| 568 | | preg_match( '| state=\'([SDFA])\'|i', $attribs[1], $matches ); |
| 569 | | $lj_comment_state = !empty( $matches[1] ) ? $matches[1] : 'A'; |
| | 537 | $comment_author_ID = isset( $matches[1] ) ? $matches[1] : 0; |
| | 538 | preg_match( '| parentid=\'(\d+)\'|i', $attribs[1], $matches ); // optional |
| | 539 | $lj_comment_parent = isset( $matches[1] ) ? $matches[1] : 0; |
| | 540 | preg_match( '| state=\'([SDFA])\'|i', $attribs[1], $matches ); // optional |
| | 541 | $lj_comment_state = isset( $matches[1] ) ? $matches[1] : 'A'; |
| 634 | | // Re-build the threading within a single cache file |
| 635 | | function thread_comments( $filename ) { |
| 636 | | if ( !is_file( $filename ) || !is_readable( $filename ) ) |
| 637 | | return new WP_Error( 'File', __( sprintf( 'Cannot access file %s', $filename ) ) ); |
| 638 | | |
| 639 | | $comments = array(); |
| 640 | | @include( $filename ); |
| 641 | | $this->comments = $comments; |
| 642 | | unset( $comments ); |
| 643 | | if ( !is_array( $this->comments ) ) |
| 644 | | $this->comments = array(); |
| 645 | | |
| 646 | | $count = count( $this->comments ); |
| 647 | | for ( $c = 0; $c < $count; $c++ ) { |
| 648 | | // Skip anything that's not "top-level" for now |
| 649 | | if ( 0 != $this->comments[$c]['lj_comment_parent'] ) |
| 650 | | continue; |
| 651 | | $this->comments[$c]['children'] = $this->get_child_comments( $this->comments[$c]['lj_comment_ID'] ); |
| 652 | | } |
| 653 | | |
| 654 | | // Remove anything that's not supposed to be at top level |
| 655 | | $top_comments = array(); |
| 656 | | for ( $c = 0; $c < $count; $c++ ) { |
| 657 | | if ( 0 == $this->comments[$c]['lj_comment_parent'] ) { |
| 658 | | $top_comments[] = $this->comments[$c]; |
| 659 | | } |
| 660 | | } |
| 661 | | |
| 662 | | // Write back to file |
| 663 | | @unlink( $filename ); |
| 664 | | $this->write_file( '<?php $comments = ' . var_export( $top_comments, true ) . '; ?>', $filename, $count, 'w' ); |
| 665 | | unset( $top_comments ); |
| 666 | | $this->close_file_pointers(); |
| 667 | | |
| 668 | | // Reference this file as being threaded |
| 669 | | $files = get_option( 'ljapi_comment_threaded_files' ); |
| 670 | | $files[] = $filename; |
| 671 | | array_unique( $files ); |
| 672 | | update_option( 'ljapi_comment_threaded_files', $files ); |
| 673 | | update_option( 'ljapi_comment_threaded_files_count', count( $files ) ); |
| 674 | | |
| 675 | | return true; |
| | 611 | // Gets the comment_ID that a LJ comment has been saved as within WP |
| | 612 | function get_wp_comment_ID( $comment ) { |
| | 613 | global $wpdb; |
| | 614 | if ( empty( $this->commentmap[$comment] ) ) |
| | 615 | $this->commentmap[$comment] = $wpdb->get_var( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_karma = %d", $comment ) ); |
| | 616 | return $this->commentmap[$comment]; |
| 677 | | |
| 678 | | function get_child_comments( $id ) { |
| 679 | | $children = array(); |
| 680 | | $count = count( $this->comments ); |
| 681 | | for ( $c = 0; $c < $count; $c++ ) { |
| 682 | | // This comment is a child of the $id |
| 683 | | if ( $id == $this->comments[$c]['lj_comment_parent'] ) { |
| 684 | | $this->comments[$c]['children'] = $this->get_child_comments( $this->comments[$c]['lj_comment_ID'] ); |
| 685 | | $children[] = $this->comments[$c]; |
| 686 | | } |
| 687 | | } |
| 688 | | return $children; |
| 689 | | } |
| 690 | | |
| 691 | | // Inserts the contents of each cache file (should be threaded already) |
| 692 | | function insert_comments( $filename ) { |
| 693 | | echo '<ol>'; |
| 694 | | |
| 695 | | if ( !is_file( $filename ) || !is_readable( $filename ) ) |
| 696 | | return new WP_Error( 'File', __( sprintf( 'Cannot access file %s', $filename ) ) ); |
| 697 | | |
| 698 | | $comments = array(); |
| 699 | | @include( $filename ); |
| 700 | | $this->comments = $comments; |
| 701 | | unset( $comments ); |
| 702 | | if ( !is_array( $this->comments ) ) |
| 703 | | $this->comments = array(); |
| 705 | | $count = count( $this->comments ); |
| 706 | | for ( $c = 0; $c < $count; $c++ ) { |
| 707 | | $comment =& $this->comments[$c]; |
| 708 | | echo '<li>'; |
| 709 | | printf( __( 'Imported comment from <strong>%s</strong> on %s' ), $comment['comment_author'], $comment['comment_date'] ); |
| 710 | | |
| 711 | | $id = wp_insert_comment( $comment ); |
| 712 | | $comment['comment_ID'] = $id; |
| 713 | | if ( count( $comment['children'] ) ) { |
| 714 | | _e( ' and replies:' ); |
| 715 | | $this->insert_child_comments( $comment['children'], $id ); |
| 716 | | } |
| 717 | | |
| 718 | | echo '</li>'; |
| 719 | | } |
| 720 | | |
| 721 | | // Remove the file now that we're done with it |
| 722 | | @unlink( $filename ); |
| 723 | | |
| 724 | | echo '</ol>'; |
| 725 | | |
| 726 | | return true; |
| 727 | | } |
| 728 | | |
| 729 | | function insert_child_comments( &$comments, $parent ) { |
| 730 | | echo '<ol>'; |
| 731 | | $count = count( $comments ); |
| 732 | | for ( $c = 0; $c < $count; $c++ ) { |
| 733 | | $comment =& $comments[$c]; |
| 734 | | $comment['comment_parent'] = $parent; |
| 735 | | echo '<li>'; |
| 736 | | printf( __( 'Imported reply from <strong>%s</strong> on %s' ), $comment['comment_author'], $comment['comment_date'] ); |
| 737 | | |
| 738 | | $id = wp_insert_comment( $comment ); |
| 739 | | $comment['comment_ID'] = $id; |
| 740 | | if ( count( $comment['children'] ) ) { |
| 741 | | _e( ' and replies:' ); |
| 742 | | $this->insert_child_comments( $comment['children'], $id ); |
| 743 | | } |
| 744 | | |
| 745 | | echo '</li>'; |
| 746 | | } |
| 747 | | echo '</ol>'; |
| 748 | | } |
| 749 | | |
| 851 | | add_option( 'ljapi_sync_item_times', '', '', 'no' ); |
| 852 | | add_option( 'ljapi_usermap', '', '', 'no' ); |
| 853 | | add_option( 'ljapi_comment_xml_files', '', '', 'no' ); |
| 854 | | add_option( 'ljapi_comment_cache_files', '', '', 'no' ); |
| 855 | | add_option( 'ljapi_comment_threaded_files', '', '', 'no' ); |
| | 719 | add_option( 'ljapi_sync_item_times', '', '', 'no' ); |
| | 720 | add_option( 'ljapi_usermap', '', '', 'no' ); |
| | 721 | update_option( 'ljapi_comment_batch', 0 ); |
| 892 | | $result = $this->download_comments(); |
| 893 | | if ( is_wp_error( $result ) ) |
| 894 | | return $result; |
| 895 | | |
| 896 | | echo '<p>' . __( 'Your comments have all been downloaded to this server now, so we can process them and get them ready for importing.' ) . '</p>'; |
| 897 | | echo $this->next_step( 3, __( 'Process my comment files »' ) ); |
| 898 | | $this->auto_submit(); |
| 899 | | } |
| 900 | | |
| 901 | | // Parse XML into comment cache files |
| 902 | | function step3() { |
| 903 | | |
| 904 | | set_time_limit( 0 ); |
| 905 | | update_option( 'ljapi_step', 3 ); |
| 906 | | |
| 907 | | $this->usermap = get_option( 'ljapi_usermap' ); |
| 908 | | |
| 909 | | echo '<div id="ljapi-status">'; |
| 910 | | echo '<h3>' . __( 'Parsing Comments' ) . '</h3>'; |
| 911 | | echo '<p>' . __( 'Time to clean up your comments and get them into a format WordPress understands...' ) . '</p>'; |
| 912 | | ob_flush(); flush(); |
| 913 | | |
| 914 | | $files = get_option( 'ljapi_comment_xml_files' ); |
| 915 | | if ( count( $files ) ) { |
| 916 | | $file = array_pop( $files ); |
| 917 | | |
| 918 | | $result = $this->parse_comment_xml( $file ); |
| | 760 | if ( !get_option( 'ljapi_usermap' ) ) { |
| | 761 | // We haven't downloaded meta yet, so do that first |
| | 762 | $result = $this->download_comment_meta(); |
| 924 | | |
| 925 | | if ( count( $files ) ) { |
| 926 | | ?> |
| 927 | | <form action="admin.php?import=livejournal" method="post" id="ljapi-auto-repost"> |
| 928 | | <p><strong><?php printf( __( 'Processed comment file %d of %d' ), ( get_option( 'ljapi_comment_xml_files_count' ) - count( $files ) ), get_option( 'ljapi_comment_xml_files_count' ) ) ?></strong></p> |
| 929 | | <?php wp_nonce_field( 'lj-api-import' ) ?> |
| 930 | | <input type="hidden" name="step" id="step" value="3" /> |
| 931 | | <p><input type="submit" class="button-primary" value="<?php echo attribute_escape( __( 'Process the next comment file »' ) ) ?>" /> <span id="auto-message"></span></p> |
| 932 | | </form> |
| 933 | | <?php $this->auto_ajax( 'ljapi-auto-repost', 'auto-message', 0 ); ?> |
| 934 | | <?php |
| | 766 | |
| | 767 | // Download a batch of actual comments |
| | 768 | $result = $this->download_comment_bodies(); |
| | 769 | if ( is_wp_error( $result ) ) |
| | 770 | return $result; |
| | 771 | |
| | 772 | $maxid = get_option( 'ljapi_maxid' ) ? (int) get_option( 'ljapi_maxid' ) : 1; |
| | 773 | $highest_id = (int) get_option( 'ljapi_highest_comment_id' ); |
| | 774 | if ( $maxid > $highest_id ) { |
| | 775 | ?> |
| | 776 | <form action="admin.php?import=livejournal" method="post" id="ljapi-auto-repost"> |
| | 777 | <p><strong><?php printf( __( 'Imported comment batch %d of <strong>approximately</strong> %d' ), get_option( 'ljapi_comment_batch' ), ( $maxid / 5000 ) ) ?></strong></p> |
| | 778 | <?php wp_nonce_field( 'lj-api-import' ) ?> |
| | 779 | <input type="hidden" name="step" id="step" value="2" /> |
| | 780 | <p><input type="submit" class="button-primary" value="<?php echo attribute_escape( __( 'Import the next batch »' ) ) ?>" /> <span id="auto-message"></span></p> |
| | 781 | </form> |
| | 782 | <?php $this->auto_ajax( 'ljapi-auto-repost', 'auto-message', 0 ); ?> |
| | 783 | <?php |
| 953 | | $files = get_option( 'ljapi_comment_cache_files' ); |
| 954 | | if ( count( $files ) ) { |
| 955 | | $file = array_pop( $files ); |
| 956 | | |
| 957 | | $result = $this->thread_comments( $file ); |
| 958 | | if ( is_wp_error( $result ) ) |
| 959 | | return $result; |
| 960 | | |
| 961 | | update_option( 'ljapi_comment_cache_files', $files ); |
| | 803 | // Only bother adding indexes if they have over 5000 comments (arbitrary number) |
| | 804 | $imported_comments = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_type = 'livejournal'" ); |
| | 805 | $added_indices = false; |
| | 806 | if ( 5000 < $imported_comments ) { |
| | 807 | include_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| | 808 | $added_indices = true; |
| | 809 | add_clean_index( $wpdb->comments, 'comment_type' ); |
| | 810 | add_clean_index( $wpdb->comments, 'comment_karma' ); |
| | 811 | add_clean_index( $wpdb->comments, 'comment_agent' ); |
| 964 | | if ( count( $files ) ) { |
| 965 | | ?> |
| 966 | | <form action="admin.php?import=livejournal" method="post" id="ljapi-auto-repost"> |
| 967 | | <p><strong><?php printf( __( 'Threaded cache file %d of %d' ), ( get_option( 'ljapi_comment_cache_files_count' ) - count( $files ) ), get_option( 'ljapi_comment_cache_files_count' ) ) ?></strong></p> |
| 968 | | <?php wp_nonce_field( 'lj-api-import' ) ?> |
| 969 | | <input type="hidden" name="step" id="step" value="4" /> |
| 970 | | <p><input type="submit" class="button-primary" value="<?php echo attribute_escape( __( 'Thread the next cache file »' ) ) ?>" /> <span id="auto-message"></span></p> |
| 971 | | </form> |
| 972 | | <?php $this->auto_ajax( 'ljapi-auto-repost', 'auto-message', 0 ); ?> |
| 973 | | <?php |
| 974 | | } else { |
| 975 | | echo '<p>' . __( "Alrighty, your comments are all threaded. There's just one last step -- time to actually import them all now!" ) . '</p>'; |
| 976 | | echo '<p>' . __( 'This last part in particular can take a really long time if you have a lot of comments. You might want to go and do something else while you wait.' ) . '</p>'; |
| 977 | | echo $this->next_step( 5, __( 'Import my threaded comments into WordPress »' ) ); |
| 978 | | $this->auto_submit(); |
| | 814 | // Get LJ comments, which haven't been threaded yet, 5000 at a time and thread them |
| | 815 | while ( $comments = $wpdb->get_results( "SELECT comment_ID, comment_agent FROM {$wpdb->comments} WHERE comment_type = 'livejournal' AND comment_agent != '0' LIMIT 5000", OBJECT ) ) { |
| | 816 | foreach ( $comments as $comment ) { |
| | 817 | $wpdb->update( $wpdb->comments, |
| | 818 | array( 'comment_parent' => $this->get_wp_comment_ID( $comment->comment_agent ), 'comment_type' => 'livejournal-done' ), |
| | 819 | array( 'comment_ID' => $comment->comment_ID ) ); |
| | 820 | } |
| | 821 | wp_cache_flush(); |
| | 822 | $wpdb->flush(); |
| 988 | | |
| 989 | | echo '<div id="ljapi-status">'; |
| 990 | | echo '<h3>' . __( 'Importing Comments' ) . '</h3>'; |
| 991 | | echo '<p>' . __( 'This is the big one -- we are now inserting your comment threads into WordPress...' ) . '</p>'; |
| 992 | | |
| 993 | | $files = get_option( 'ljapi_comment_threaded_files' ); |
| 994 | | echo '<p><strong>' . sprintf( __( 'Importing cache file %d of %d' ), ( get_option( 'ljapi_comment_threaded_files_count' ) - count( $files ) + 1 ), get_option( 'ljapi_comment_threaded_files_count' ) ) . '</strong></p>'; |
| 995 | | ob_flush(); flush(); |
| 996 | | |
| 997 | | if ( count( $files ) ) { |
| 998 | | $file = array_pop( $files ); |
| 999 | | |
| 1000 | | $result = $this->insert_comments( $file ); |
| 1001 | | if ( is_wp_error( $result ) ) |
| 1002 | | return $result; |
| 1003 | | |
| 1004 | | update_option( 'ljapi_comment_threaded_files', $files ); |
| | 825 | // Revert the comments table back to normal and optimize it to reclaim space |
| | 826 | if ( $added_indices ) { |
| | 827 | drop_index( $wpdb->comments, 'comment_type' ); |
| | 828 | drop_index( $wpdb->comments, 'comment_karma' ); |
| | 829 | drop_index( $wpdb->comments, 'comment_agent' ); |
| | 830 | $wpdb->query( "OPTIMIZE TABLE {$wpdb->comments}" ); |
| 1007 | | if ( count( $files ) ) { |
| 1008 | | ?> |
| 1009 | | <form action="admin.php?import=livejournal" method="post" id="ljapi-auto-repost"> |
| 1010 | | <?php wp_nonce_field( 'lj-api-import' ) ?> |
| 1011 | | <input type="hidden" name="step" id="step" value="5" /> |
| 1012 | | <p><input type="submit" class="button-primary" value="<?php echo attribute_escape( __( 'Import the next cache file »' ) ) ?>" /> <span id="auto-message"></span></p> |
| 1013 | | </form> |
| 1014 | | <?php $this->auto_ajax( 'ljapi-auto-repost', 'auto-message', 0 ); ?> |
| 1015 | | <?php |
| 1016 | | } else { |
| 1017 | | // Clean up database and we're out |
| 1018 | | $this->cleanup(); |
| 1019 | | do_action( 'import_done', 'livejournal' ); |
| 1020 | | echo '<h3>'; |
| 1021 | | printf( __( 'All done. <a href="%s">Have fun!</a>' ), get_option( 'home' ) ); |
| 1022 | | echo '</h3>'; |
| 1023 | | } |
| | 833 | // Clean up database and we're out |
| | 834 | $this->cleanup(); |
| | 835 | do_action( 'import_done', 'livejournal' ); |
| | 836 | if ( $imported_comments > 1 ) |
| | 837 | echo '<p>' . sprintf( __( "Successfully re-threaded %d comments." ), number_format( $imported_comments ) ) . '</p>'; |
| | 838 | echo '<h3>'; |
| | 839 | printf( __( 'All done. <a href="%s">Have fun!</a>' ), get_option( 'home' ) ); |
| | 840 | echo '</h3>'; |
| 1038 | | |
| 1039 | | // Automatically submit the form with #id to continue the process |
| 1040 | | // Hide any submit buttons to avoid people clicking them |
| 1041 | | // Display a countdown in the element indicated by $msg for "Continuing in x" |
| 1042 | | function auto_ajax( $id = 'ljapi-next-form', $msg = 'auto-message', $seconds = 5 ) { |
| | 855 | |
| | 856 | // Automatically submit the specified form after $seconds |
| | 857 | // Include a friendly countdown in the element with id=$msg |
| | 858 | function auto_submit( $id = 'ljapi-next-form', $msg = 'auto-message', $seconds = 10 ) { |
| 1071 | | // Automatically submit the specified form after $seconds |
| 1072 | | // Include a friendly countdown in the element with id=$msg |
| 1073 | | function auto_submit( $id = 'ljapi-next-form', $msg = 'auto-message', $seconds = 10 ) { |
| | 883 | // Automatically submit the form with #id to continue the process |
| | 884 | // Hide any submit buttons to avoid people clicking them |
| | 885 | // Display a countdown in the element indicated by $msg for "Continuing in x" |
| | 886 | function auto_ajax( $id = 'ljapi-next-form', $msg = 'auto-message', $seconds = 5 ) { |
| | 936 | |
| | 937 | $wpdb->update( $wpdb->comments, |
| | 938 | array( 'comment_karma' => 0, 'comment_agent' => 'WP LJ Importer', 'comment_type' => '' ), |
| | 939 | array( 'comment_type' => 'livejournal-done' ) ); |
| | 940 | $wpdb->update( $wpdb->comments, |
| | 941 | array( 'comment_karma' => 0, 'comment_agent' => 'WP LJ Importer', 'comment_type' => '' ), |
| | 942 | array( 'comment_type' => 'livejournal' ) ); |
| 1123 | | // Dump a string to a log file (appends to existing file) |
| 1124 | | function log( $string, $name ) { |
| 1125 | | return; // remove this to enable "debugging" output to files in /wp-content/ljimport |
| 1126 | | $path = wp_upload_dir(); |
| 1127 | | $path = $path['path']; |
| 1128 | | if ( get_option( 'uploads_use_yearmonth_folders' ) ) |
| 1129 | | $path = substr( $path, 0, -8 ); |
| 1130 | | |
| 1131 | | if ( !is_dir( $path . '/ljimport' ) ) |
| 1132 | | mkdir( $path . '/ljimport' ); |
| 1133 | | |
| 1134 | | $fh = @fopen( $path . '/ljimport/' . $name, 'a' ); |
| 1135 | | if ( $fh ) { |
| 1136 | | if ( is_array( $string ) || is_object( $string ) ) |
| 1137 | | fwrite( $fh, var_export( $string, true ) . "\n\n" ); |
| 1138 | | else |
| 1139 | | fwrite( $fh, $string . "\n\n" ); |
| 1140 | | fclose( $fh ); |
| 1141 | | } |
| 1142 | | } |
| 1143 | | |
| 1144 | | function write_file( $data, $name, $id, $mode = 'a' ) { |
| 1145 | | if ( empty( $this->pointers[$id] ) ) |
| 1146 | | $this->pointers[$id] = @fopen( $name, $mode ); |
| 1147 | | if ( $this->pointers[$id] ) |
| 1148 | | return fwrite( $this->pointers[$id], $data ); |
| 1149 | | return false; |
| 1150 | | } |
| 1151 | | |
| 1152 | | function full_path( $basename ) { |
| 1153 | | $uploads = wp_upload_dir(); |
| 1154 | | return $uploads['path'] . '/' . $basename; |
| 1155 | | } |
| 1156 | | |
| 1157 | | function close_file_pointers() { |
| 1158 | | foreach ( $this->pointers as $p ) |
| 1159 | | @fclose( $p ); |
| 1160 | | } |
| 1161 | | |