function test_convert_encoding() { printf("Test string: %s <br>", self::$test_str); $this->assert_equal(MultiByte::convert_encoding(self::$test_str), mb_convert_encoding(self::$test_str, 'UTF-8', mb_detect_encoding(self::$test_str))); printf("After being converted to MultiByte's encoding: %s <br>", MultiByte::convert_encoding(self::$test_str)); printf("After being converted by mbstring to MultiByte's encoding without detecting encoding: %s <br>", mb_convert_encoding(self::$test_str, MultiByte::hab_encoding())); printf("After being converted by mbstring to MultiByte's encoding after detecting encoding: %s <br>", mb_convert_encoding(self::$test_str, MultiByte::hab_encoding(), mb_detect_encoding(self::$test_str))); $this->assert_equal(MultiByte::convert_encoding(self::$test_str, 'ASCII'), mb_convert_encoding(self::$test_str, 'ASCII', mb_detect_encoding(self::$test_str))); printf("MultiByte convert to ASCII: %s <br>", MultiByte::convert_encoding(self::$test_str, 'ASCII')); printf("mbstring convert to ASCII without detecting encoding: %s <br>", mb_convert_encoding(self::$test_str, 'ASCII')); printf("mbstring convert to ASCII after detecting encoding: %s <br>", mb_convert_encoding(self::$test_str, 'ASCII', mb_detect_encoding(self::$test_str))); }
/** * Receive a Pingback via XMLRPC * @param array $params An array of XMLRPC parameters from the remote call * @return string The success state of the pingback */ public function xmlrpc_pingback__ping( $params ) { try { list( $source_uri, $target_uri )= $params; // This should really be done by an Habari core function $target_parse = InputFilter::parse_url( $target_uri ); $target_stub = $target_parse['path']; $base_url = Site::get_path( 'base', true ); if ( '/' != $base_url) { $target_stub = str_replace( $base_url, '', $target_stub ); } $target_stub = trim( $target_stub, '/' ); if ( strpos( $target_stub, '?' ) !== false ) { list( $target_stub, $query_string )= explode( '?', $target_stub ); } // Can this be used as a target? $target_slug = URL::parse( $target_stub )->named_arg_values['slug']; if ( $target_slug === false ) { throw new XMLRPCException( 33 ); } // Does the target exist? $target_post = Post::get( array( 'slug' => $target_slug ) ); if ( $target_post === false ) { throw new XMLRPCException( 32 ); } // Is comment allowed? if ( $target_post->info->comments_disabled ) { throw new XMLRPCException( 33 ); } // Is this Pingback already registered? if ( Comments::get( array( 'post_id' => $target_post->id, 'url' => $source_uri, 'type' => Comment::PINGBACK ) )->count() > 0 ) { throw new XMLRPCException( 48 ); } // Retrieve source contents try { $rr = new RemoteRequest( $source_uri ); $rr->execute(); if ( ! $rr->executed() ) { throw new XMLRPCException( 16 ); } $source_contents = $rr->get_response_body(); $headers = $rr->get_response_headers(); } catch ( XMLRPCException $e ) { // catch our special type of exception and re-throw it throw $e; } catch ( Exception $e ) { throw new XMLRPCException( -32300 ); } // Encoding is converted into internal encoding. // First, detect the source string's encoding $habari_encoding = strtoupper( MultiByte::hab_encoding() ); $source_encoding = 'Windows-1252'; // Is the charset in the headers? if ( isset( $headers['Content-Type'] ) && strpos( $headers['Content-Type'], 'charset' ) !== false ) { // This regex should be changed to meet the HTTP spec at some point if ( preg_match("/charset[\x09\x0A\x0C\x0D\x20]*=[\x09\x0A\x0C\x0D\x20]*('?)([A-Za-z0-9\-\_]+)\1/i", $headers['Content-Type'], $matches ) ) { $source_encoding = strtoupper( $matches[2] ); } } // Can we tell the charset from the stream itself? else if ( ( $enc = MultiByte::detect_bom_encoding( $source_contents ) ) !== false ) { $source_encoding = $enc; } // Is the charset in a meta tag? else if ( preg_match( "/<meta[^>]+charset[\x09\x0A\x0C\x0D\x20]*=[\x09\x0A\x0C\x0D\x20]*([\"']?)([A-Za-z0-9\-\_]+)\1/i", $source_contents, $matches ) ) { $source_encoding = strtoupper( $matches[2] ); if (in_array($source_encoding, array("UTF-16", "UTF-16BE", "UTF-16LE"))) { $source_encoding = "UTF-8"; } } // Then, convert the string $ret = MultiByte::convert_encoding( $source_contents, $habari_encoding, $source_encoding ); if ( $ret !== false ) { $source_contents = $ret; } // Find the page's title preg_match( '/<title>(.*)<\/title>/is', $source_contents, $matches ); $source_title = $matches[1]; // Find the reciprocal links and their context preg_match( '/<body[^>]*>(.+)<\/body>/is', $source_contents, $matches ); $source_contents_filtered = preg_replace( '/\s{2,}/is', ' ', strip_tags( $matches[1], '<a>' ) ); // Get rid of all the non-recriprocal links $ht = new HTMLTokenizer( trim( $source_contents_filtered ) ); $set = $ht->parse(); $all_links = $set->slice( 'a', array() ); $keep_links = $set->slice( 'a', array( 'href' => $target_uri ) ); $bad_links = array_diff( $all_links, $keep_links ); foreach( $bad_links as $link ) { $link->tokenize_replace( '' ); $set->replace_slice( $link ); } $source_contents_filtered = (string)$set; // Get the excerpt if ( !preg_match( '%.{0,100}?<a[^>]*?href\\s*=\\s*("|\'|)' . $target_uri . '\\1[^>]*?'.'>(.+?)</a>.{0,100}%s', $source_contents_filtered, $source_excerpt ) ) { throw new XMLRPCException( 17 ); } /** Sanitize Data */ $source_excerpt = '…' . InputFilter::filter( $source_excerpt[0] ) . '…'; $source_title = InputFilter::filter($source_title); $source_uri = InputFilter::filter($source_uri); /* Sanitize the URL */ if (!empty($source_uri)) { $parsed = InputFilter::parse_url( $source_uri ); if ( $parsed['is_relative'] ) { // guess if they meant to use an absolute link $parsed = InputFilter::parse_url( 'http://' . $source_uri ); if ( ! $parsed['is_error'] ) { $source_uri = InputFilter::glue_url( $parsed ); } else { // disallow relative URLs $source_uri = ''; } } if ( $parsed['is_pseudo'] || ( $parsed['scheme'] !== 'http' && $parsed['scheme'] !== 'https' ) ) { // allow only http(s) URLs $source_uri = ''; } else { // reconstruct the URL from the error-tolerant parsing // http:moeffju.net/blog/ -> http://moeffju.net/blog/ $source_uri = InputFilter::glue_url( $parsed ); } } // Add a new pingback comment $pingback = new Comment( array( 'post_id' => $target_post->id, 'name' => $source_title, 'email' => '', 'url' => $source_uri, 'ip' => Utils::get_ip(), 'content' => $source_excerpt, 'status' => Comment::STATUS_UNAPPROVED, 'date' => HabariDateTime::date_create(), 'type' => Comment::PINGBACK, ) ); $pingback->insert(); // Respond to the Pingback return 'The pingback has been registered'; } catch ( XMLRPCException $e ) { $e->output_fault_xml(); } }
/** * The plugin sink for the auth_ajax_wp_import_comments hook. * Responds via authenticated ajax to requests for comment importing. * * @param AjaxHandler $handler The handler that handled the request, contains $_POST info */ public function action_auth_ajax_wp_import_comments($handler) { $valid_fields = array('db_name', 'db_host', 'db_user', 'db_pass', 'db_prefix', 'commentindex', 'category_import', 'utw_import'); $inputs = array_intersect_key($_POST->getArrayCopy(), array_flip($valid_fields)); extract($inputs); $wpdb = $this->wp_connect($db_host, $db_name, $db_user, $db_pass, $db_prefix); if ($wpdb) { if (!DB::in_transaction()) { DB::begin_transaction(); } $commentcount = $wpdb->get_value("SELECT count( comment_ID ) FROM {$db_prefix}comments;"); $min = $commentindex * IMPORT_BATCH + 1; $max = min(($commentindex + 1) * IMPORT_BATCH, $commentcount); echo "<p>Importing comments {$min}-{$max} of {$commentcount}.</p>"; $postinfo = DB::table('postinfo'); $post_info = DB::get_results("SELECT post_id, value FROM {$postinfo} WHERE name= 'wp_id';"); foreach ($post_info as $info) { $post_map[$info->value] = $info->post_id; } $comments = $wpdb->get_results("\n\t\t\t\tSELECT\n\t\t\t\tcomment_content as content,\n\t\t\t\tcomment_author as name,\n\t\t\t\tcomment_author_email as email,\n\t\t\t\tcomment_author_url as url,\n\t\t\t\tINET_ATON( comment_author_IP ) as ip,\n\t\t\t \tcomment_approved as status,\n\t\t\t\tcomment_date as date,\n\t\t\t\tcomment_type as type,\n\t\t\t\tID as wp_post_id\n\t\t\t\tFROM {$db_prefix}comments\n\t\t\t\tINNER JOIN\n\t\t\t\t{$db_prefix}posts on ( {$db_prefix}posts.ID= {$db_prefix}comments.comment_post_ID )\n\t\t\t\tLIMIT {$min}, " . IMPORT_BATCH, array(), 'Comment'); foreach ($comments as $comment) { switch ($comment->type) { case 'pingback': $comment->type = Comment::PINGBACK; break; case 'trackback': $comment->type = Comment::TRACKBACK; break; default: $comment->type = Comment::COMMENT; } $comment->content = MultiByte::convert_encoding($comment->content); $comment->name = MultiByte::convert_encoding($comment->name); $carray = $comment->to_array(); if ($carray['ip'] == '') { $carray['ip'] = 0; } switch ($carray['status']) { case '0': $carray['status'] = Comment::STATUS_UNAPPROVED; break; case '1': $carray['status'] = Comment::STATUS_APPROVED; break; case 'spam': $carray['status'] = Comment::STATUS_SPAM; break; } if (isset($post_map[$carray['wp_post_id']])) { $carray['post_id'] = $post_map[$carray['wp_post_id']]; unset($carray['wp_post_id']); $c = new Comment($carray); //Utils::debug( $c ); try { $c->insert(); } catch (Exception $e) { EventLog::log($e->getMessage(), 'err', null, null, print_r(array($c, $e), 1)); Session::error($e->getMessage()); $errors = Options::get('import_errors'); $errors[] = $e->getMessage(); Options::set('import_errors', $errors); } } } if (DB::in_transaction()) { DB::commit(); } if ($max < $commentcount) { $ajax_url = URL::get('auth_ajax', array('context' => 'wp_import_comments')); $commentindex++; $vars = Utils::addslashes(array('host' => $db_host, 'name' => $db_name, 'user' => $db_user, 'pass' => $db_pass, 'prefix' => $db_prefix)); echo <<<WP_IMPORT_AJAX1 \t\t\t\t\t<script type="text/javascript"> \t\t\t\t\t\$( '#import_progress' ).load( \t\t\t\t\t\t"{$ajax_url}", \t\t\t\t\t\t{ \t\t\t\t\t\t\tdb_host: "{$vars['host']}", \t\t\t\t\t\t\tdb_name: "{$vars['name']}", \t\t\t\t\t\t\tdb_user: "******", \t\t\t\t\t\t\tdb_pass: "******", \t\t\t\t\t\t\tdb_prefix: "{$vars['prefix']}", \t\t\t\t\t\t\tcategory_import: "{$category_import}", \t\t\t\t\t\t\tutw_import: "{$utw_import}", \t\t\t\t\t\t\tcommentindex: {$commentindex} \t\t\t\t\t\t} \t\t\t\t\t ); \t\t\t\t</script> WP_IMPORT_AJAX1; } else { EventLog::log('Import complete from "' . $db_name . '"'); echo '<p>' . _t('Import is complete.') . '</p>'; $errors = Options::get('import_errors'); if (count($errors) > 0) { echo '<p>' . _t('There were errors during import:') . '</p>'; echo '<ul>'; foreach ($errors as $error) { echo '<li>' . $error . '</li>'; } echo '</ul>'; } } } else { EventLog::log(sprintf(_t('Failed to import from "%s"'), $db_name), 'crit'); Session::error($e->getMessage()); echo '<p>' . _t('Failed to connect using the given database connection details.') . '</p>'; } }
/** * Receive a Pingback via XMLRPC * @param array $params An array of XMLRPC parameters from the remote call * @return string The success state of the pingback */ public function xmlrpc_pingback__ping($params) { try { list($source_uri, $target_uri) = $params; // This should really be done by an Habari core function $target_parse = InputFilter::parse_url($target_uri); $target_stub = $target_parse['path']; $base_url = Site::get_path('base', TRUE); if ('/' != $base_url) { $target_stub = str_replace($base_url, '', $target_stub); } $target_stub = trim($target_stub, '/'); if (strpos($target_stub, '?') !== FALSE) { list($target_stub, $query_string) = explode('?', $target_stub); } // Can this be used as a target? $target_slug = URL::parse($target_stub)->named_arg_values['slug']; if ($target_slug === FALSE) { throw new XMLRPCException(33); } // Does the target exist? $target_post = Post::get(array('slug' => $target_slug)); if ($target_post === FALSE) { throw new XMLRPCException(32); } // Is comment allowed? if ($target_post->info->comments_disabled) { throw new XMLRPCException(33); } // Is this Pingback already registered? if (Comments::get(array('post_id' => $target_post->id, 'url' => $source_uri, 'type' => Comment::PINGBACK))->count() > 0) { throw new XMLRPCException(48); } // Retrieve source contents $rr = new RemoteRequest($source_uri); $rr->execute(); if (!$rr->executed()) { throw new XMLRPCException(16); } $source_contents = $rr->get_response_body(); // encoding is converted into internal encoding. // @todo check BOM at beginning of file before checking for a charset attribute $habari_encoding = MultiByte::hab_encoding(); if (preg_match("/<meta[^>]+charset=([A-Za-z0-9\\-\\_]+)/i", $source_contents, $matches) !== FALSE && strtolower($habari_encoding) != strtolower($matches[1])) { $ret = MultiByte::convert_encoding($source_contents, $habari_encoding, $matches[1]); if ($ret !== FALSE) { $source_contents = $ret; } } // Find the page's title preg_match('/<title>(.*)<\\/title>/is', $source_contents, $matches); $source_title = $matches[1]; // Find the reciprocal links and their context preg_match('/<body[^>]*>(.+)<\\/body>/is', $source_contents, $matches); $source_contents_filtered = preg_replace('/\\s{2,}/is', ' ', strip_tags($matches[1], '<a>')); if (!preg_match('%.{0,100}?<a[^>]*?href\\s*=\\s*("|\'|)' . $target_uri . '\\1[^>]*?' . '>(.+?)</a>.{0,100}%s', $source_contents_filtered, $source_excerpt)) { throw new XMLRPCException(17); } /** Sanitize Data */ $source_excerpt = '...' . InputFilter::filter($source_excerpt[0]) . '...'; $source_title = InputFilter::filter($source_title); $source_uri = InputFilter::filter($source_uri); /* Sanitize the URL */ if (!empty($source_uri)) { $parsed = InputFilter::parse_url($source_uri); if ($parsed['is_relative']) { // guess if they meant to use an absolute link $parsed = InputFilter::parse_url('http://' . $source_uri); if (!$parsed['is_error']) { $source_uri = InputFilter::glue_url($parsed); } else { // disallow relative URLs $source_uri = ''; } } if ($parsed['is_pseudo'] || $parsed['scheme'] !== 'http' && $parsed['scheme'] !== 'https') { // allow only http(s) URLs $source_uri = ''; } else { // reconstruct the URL from the error-tolerant parsing // http:moeffju.net/blog/ -> http://moeffju.net/blog/ $source_uri = InputFilter::glue_url($parsed); } } // Add a new pingback comment $pingback = new Comment(array('post_id' => $target_post->id, 'name' => $source_title, 'email' => '', 'url' => $source_uri, 'ip' => sprintf("%u", ip2long($_SERVER['REMOTE_ADDR'])), 'content' => $source_excerpt, 'status' => Comment::STATUS_UNAPPROVED, 'date' => HabariDateTime::date_create(), 'type' => Comment::PINGBACK)); $pingback->insert(); // Respond to the Pingback return 'The pingback has been registered'; } catch (XMLRPCException $e) { $e->output_fault_xml(); } }
public function test_detect_encoding() { $this->assert_equal(MultiByte::detect_encoding('foo'), 'ASCII'); $str = MultiByte::convert_encoding($this->test_strings['jis'], 'JIS'); $this->assert_equal(MultiByte::detect_encoding($str), 'JIS'); }
public function action_auth_ajax_wp_import_comments() { // get the values post'd in $inputs = $_POST->filter_keys(array('db_name', 'db_host', 'db_user', 'db_pass', 'db_prefix', 'category_import', 'import_index')); $inputs = $inputs->getArrayCopy(); // make sure we have all our default values $inputs = array_merge($this->default_values, $inputs); // get the wpdb $wpdb = $this->wp_connect($inputs['db_host'], $inputs['db_name'], $inputs['db_user'], $inputs['db_pass']); // if we couldn't connect, error out if (!$wpdb) { EventLog::log(_t('Failed to import from "%s"', array($inputs['db_name']))); Session::error(_t('Failed to import from "%s"', array($inputs['db_name']))); echo '<p>' . _t('Failed to connect using the given database connection details.') . '</p>'; } // we connected just fine, let's get moving! // begin a transaction. if we error out at any point, we want to roll back to before import began DB::begin_transaction(); // fetch the number of comments from the wordpress database so we can batch things up $num_comments = $wpdb->get_value('select count(comment_id) from ' . $inputs['db_prefix'] . 'comments'); // figure out the LIMIT we're at $min = $inputs['import_index'] * IMPORT_BATCH; $max = min($min + IMPORT_BATCH, $num_comments); // for display only echo '<p>' . _t('Importing comments %1$d - %2$d of %3$d.', array($min, $max, $num_comments)) . '</p>'; // get all the imported users so we can link old comment authors to new comment authors $users = DB::get_results('select user_id, value from {userinfo} where name = :name', array(':name' => 'wp_id')); // create an easy user map of old ID -> new ID $user_map = array(); foreach ($users as $info) { $user_map[$info->value] = $info->user_id; } // get all the imported posts so we can link old post IDs to new post IDs $posts = DB::get_results('select post_id, value from {postinfo} where name = :name', array(':name' => 'wp_id')); // create an easy post map of old ID -> new ID $post_map = array(); foreach ($posts as $info) { $post_map[$info->value] = $info->post_id; } // get all the comment IDs we've imported so far to make sure we don't duplicate any $comment_map = DB::get_column('select value from {commentinfo} where name = :name', array(':name' => 'wp_id')); // now we're ready to start importing comments $comments = $wpdb->get_results('select comment_id, comment_post_id, comment_author, comment_author_email, comment_author_url, comment_author_ip, comment_date, comment_content, comment_karma, comment_approved, comment_agent, comment_type, comment_parent, user_id from ' . $inputs['db_prefix'] . 'comments order by comment_id asc limit ' . $min . ', ' . IMPORT_BATCH); foreach ($comments as $comment) { // if this post is already in the list we've imported, skip it if (in_array($comment->id, $comment_map)) { continue; } // if the post this comment belongs to is not in the list of imported posts, skip it if (!isset($post_map[$comment->comment_post_id])) { continue; } // create the new comment $c = new Comment(array('content' => MultiByte::convert_encoding($comment->comment_content), 'name' => MultiByte::convert_encoding($comment->comment_author), 'email' => MultiByte::convert_encoding($comment->comment_author_email), 'url' => MultiByte::convert_encoding($comment->comment_author_url), 'date' => HabariDateTime::date_create($comment->comment_date), 'post_id' => $post_map[$comment->comment_post_id])); // figure out the comment type switch ($comment->comment_type) { case 'pingback': $c->type = Comment::type('pingback'); break; case 'trackback': $c->type = Comment::type('trackback'); break; default: case 'comment': $c->type = Comment::type('comment'); break; } // figure out the comment status switch ($comment->comment_approved) { case '1': $c->status = Comment::status('approved'); break; case '': case '0': $c->status = Comment::status('unapproved'); break; case 'spam': $c->status = Comment::status('spam'); break; default: // Comment::status() returns false if it doesn't recognize the status type $status = Comment::status($comment->comment_status); // store in a temp value because if you try and set ->status to an invalid value the Comment class freaks if ($status == false) { // we're not importing statuses we don't recognize - continue 2 to break out of the switch and the loop and continue to the next comment continue 2; } else { $c->status = $status; } break; } // save the old comment ID in info $c->info->wp_id = $comment->comment_id; // save the old post ID in info $c->info->wp_post_id = $comment->comment_post_id; // save the old comment karma - but only if it is something if ($comment->comment_karma != '0') { $c->info->wp_karma = $comment->comment_karma; } // save the old comment user agent - but only if it is something if ($comment->comment_agent != '') { $c->info->wp_agent = $comment->comment_agent; } // now that we've got all the pieces in place, save the comment try { $c->insert(); } catch (Exception $e) { EventLog::log($e->getMessage(), 'err'); echo '<p class="error">' . _t('There was an error importing comment ID %d. See the EventLog for the error message.', array($comment->comment_id)); echo '<p>' . _t('Rolling back changes…') . '</p>'; // rollback all changes before we return so the import hasn't changed anything yet DB::rollback(); // and return so they don't get AJAX to send them on to the next step return false; } } // if we've finished without an error, commit the import DB::commit(); if ($max < $num_comments) { // if there are more posts to import // get the next ajax url $ajax_url = URL::get('auth_ajax', array('context' => 'wp_import_comments')); // bump the import index by one so we get a new batch next time $inputs['import_index']++; } else { // display the completed message! EventLog::log(_t('Import completed from "%s"', array($inputs['db_name']))); echo '<p>' . _t('Import is complete.') . '</p>'; return; } // and spit out ajax to send them to the next step - posts! echo $this->get_ajax($ajax_url, $inputs); }
public function testConvert_encoding() { $this->assertEquals( MultiByte::convert_encoding( self::$test_str ), mb_convert_encoding( self::$test_str, 'UTF-8', mb_detect_encoding( self::$test_str ) ) ); $this->assertEquals( MultiByte::convert_encoding( self::$test_str, 'ASCII' ), mb_convert_encoding( self::$test_str, 'ASCII', mb_detect_encoding( self::$test_str ) ) ); }
/** * action: auth_ajax_mt_file_import_all * * @access public * @param array $handler */ public function action_auth_ajax_mt_file_import_all($handler) { $text = file_get_contents($_SESSION['mtimport_mt_file']); try { $parser = new MTFileParser($text); } catch (Exception $e) { echo '<p>' . _t('Failed parsing File: ') . $e->getMessage() . '</p>'; return; } $posts = $parser->getResult(); @reset($posts); while (list(, $mt_post) = @each($posts)) { // Post $t_post = array(); $tags = array(); if (isset($mt_post['_META']['BASENAME'])) { $t_post['slug'] = $mt_post['_META']['BASENAME']; } $t_post['content_type'] = Post::type('entry'); $t_post['title'] = strip_tags(htmlspecialchars_decode(MultiByte::convert_encoding($mt_post['_META']['TITLE']))); if (isset($mt_post['EXTENDED BODY']['_BODY'])) { $t_post['content'] = MultiByte::convert_encoding($mt_post['BODY']['_BODY'] . $mt_post['EXTENDED BODY']['_BODY']); } else { $t_post['content'] = MultiByte::convert_encoding($mt_post['BODY']['_BODY']); } if (!isset($mt_post['_META']['STATUS']) || $mt_post['_META']['STATUS'] == 'Publish') { $t_post['status'] = Post::status('published'); } else { $t_post['status'] = Post::status('draft'); } $t_post['pubdate'] = $t_post['updated'] = HabariDateTime::date_create($mt_post['_META']['DATE']); if (isset($mt_post['_META']['CATEGORY'])) { $tags = array_merge($tags, $mt_post['_META']['CATEGORY']); } if (isset($mt_post['_META']['TAGS'])) { $t_tags = explode(',', $mt_post['_META']['TAGS']); $t_tags = array_map('trim', $t_tags, array('"')); $tags = array_merge($tags, $t_tags); } $post = new Post($t_post); if (isset($mt_post['_META']['ALLOW COMMENTS']) && $mt_post['_META']['ALLOW COMMENTS'] != 1) { $post->info->comments_disabled = 1; } $post->tags = array_unique($tags); $post->user_id = User::identify()->id; // TODO: import MT author try { $post->insert(); } catch (Exception $e) { EventLog::log($e->getMessage(), 'err', null, null, print_r(array($p, $e), 1)); Session::error($e->getMessage()); $errors = Options::get('import_errors'); $errors[] = $p->title . ' : ' . $e->getMessage(); Options::set('import_errors', $errors); } // Comments if (isset($mt_post['COMMENT'])) { @reset($mt_post['COMMENT']); while (list(, $mt_comment) = @each($mt_post['COMMENT'])) { $t_comment = array(); $t_comment['post_id'] = $post->id; $t_comment['name'] = MultiByte::convert_encoding($mt_comment['AUTHOR']); if (isset($mt_comment['EMAIL'])) { $t_comment['email'] = $mt_comment['EMAIL']; } if (isset($mt_comment['URL'])) { $t_comment['url'] = strip_tags($mt_comment['URL']); } if (isset($mt_comment['IP'])) { $t_comment['ip'] = $mt_comment['IP']; } $t_comment['content'] = MultiByte::convert_encoding($mt_comment['_BODY']); $t_comment['status'] = Comment::STATUS_APPROVED; $t_comment['date'] = HabariDateTime::date_create($mt_comment['DATE']); $t_comment['type'] = Comment::COMMENT; $comment = new Comment($t_comment); try { $comment->insert(); } catch (Exception $e) { EventLog::log($e->getMessage(), 'err', null, null, print_r(array($c, $e), 1)); Session::error($e->getMessage()); $errors = Options::get('import_errors'); $errors[] = $e->getMessage(); Options::set('import_errors', $errors); } } } // Trackbacks if (isset($mt_post['PING'])) { @reset($mt_post['PING']); while (list(, $mt_comment) = @each($mt_post['PING'])) { $t_comment = array(); $t_comment['post_id'] = $post->id; $t_comment['name'] = MultiByte::convert_encoding($mt_comment['BLOG NAME'] . ' - ' . $mt_comment['TITLE']); if (isset($mt_comment['EMAIL'])) { $t_comment['email'] = $mt_comment['EMAIL']; } if (isset($mt_comment['URL'])) { $t_comment['url'] = strip_tags($mt_comment['URL']); } if (isset($mt_comment['IP'])) { $t_comment['ip'] = $mt_comment['IP']; } $t_comment['content'] = MultiByte::convert_encoding($mt_comment['_BODY']); $t_comment['status'] = Comment::STATUS_APPROVED; $t_comment['date'] = HabariDateTime::date_create($mt_comment['DATE']); $t_comment['type'] = Comment::TRACKBACK; $comment = new Comment($t_comment); try { $comment->insert(); } catch (Exception $e) { EventLog::log($e->getMessage(), 'err', null, null, print_r(array($c, $e), 1)); Session::error($e->getMessage()); $errors = Options::get('import_errors'); $errors[] = $e->getMessage(); Options::set('import_errors', $errors); } } } } EventLog::log(_t('Import complete from MT Export File', 'mtimport')); echo '<p>' . _t('Import is complete.') . '</p>'; $errors = Options::get('import_errors'); if (count($errors) > 0) { echo '<p>' . _t('There were errors during import:') . '</p>'; echo '<ul>'; foreach ($errors as $error) { echo '<li>' . $error . '</li>'; } echo '</ul>'; } }
/** * action: auth_ajax_blogger_import_all * * @access public * @param array $handler */ public function action_auth_ajax_blogger_import_all($handler) { $feed = simplexml_load_file($_SESSION['bloggerimport_file']); if (!$feed) { echo '<p>' . _t('Failed parsing File', 'bloggerimport') . '</p>'; return; } $post_id_map = array(); $post_map = array(); $result = DB::get_results("SELECT post_id,value FROM " . DB::table('postinfo') . " WHERE name='blogger_id';"); for ($i = 0; $i < count($result); $i++) { $post_id_map[$result[$i]->value] = $result[$i]->post_id; $post_map[] = $result[$i]->value; } $comment_map = DB::get_column("SELECT value FROM " . DB::table('commentinfo') . " WHERE name='blogger_id';"); $entry_count = count($feed->entry); for ($i = 0; $i < $entry_count; $i++) { $entry = $feed->entry[$i]; switch ((string) $entry->category[0]['term']) { // post case 'http://schemas.google.com/blogger/2008/kind#post': // already exists skipped if (in_array((string) $entry->id, $post_map)) { continue; } $t_post = array(); $t_post['title'] = MultiByte::convert_encoding((string) $entry->title); $t_post['content'] = MultiByte::convert_encoding((string) $entry->content); $t_post['user_id'] = User::identify()->id; // TODO: import Blogger author $t_post['pubdate'] = HabariDateTime::date_create((string) $entry->published); $t_post['content_type'] = Post::type('entry'); $entry->registerXPathNamespace('app', 'http://purl.org/atom/app#'); $result = $entry->xpath('//app:draft'); if (!empty($result) && (string) $result[0] == 'yes') { $t_post['status'] = Post::status('draft'); } else { $t_post['status'] = Post::status('published'); } $tags = array(); $category_count = count($entry->category); for ($j = 0; $j < count($category_count); $j++) { $tags[] = (string) $entry->category[$i]['term']; } $post = new Post($t_post); $post->tags = array_unique($tags); $post->info->blogger_id = (string) $entry->id; try { $post->insert(); } catch (Exception $e) { EventLog::log($e->getMessage(), 'err', null, null, print_r(array($p, $e), 1)); Session::error($e->getMessage()); $errors = Options::get('import_errors'); $errors[] = $post->title . ' : ' . $e->getMessage(); Options::set('import_errors', $errors); } $post_id_map[(string) $entry->id] = $post->id; break; // comment // comment case 'http://schemas.google.com/blogger/2008/kind#comment': // already exists skipped if (in_array((string) $entry->id, $comment_map)) { continue; } $result = $entry->xpath('//thr:in-reply-to'); if (empty($result) || !isset($post_id_map[(string) $result[0]['ref']])) { continue; } $t_comment = array(); $t_comment['post_id'] = $post_id_map[(string) $result[0]['ref']]; $t_comment['name'] = MultiByte::convert_encoding((string) $entry->author->name); if (isset($entry->author->email)) { $t_comment['email'] = (string) $entry->author->email; } if (isset($entry->author->uri)) { $t_comment['url'] = (string) $entry->author->uri; } $t_comment['content'] = MultiByte::convert_encoding((string) $entry->content); $t_comment['status'] = Comment::STATUS_APPROVED; $t_comment['date'] = HabariDateTime::date_create((string) $entry->published); $t_comment['type'] = Comment::COMMENT; $comment = new Comment($t_comment); $comment->info->blogger_id = (string) $entry->id; try { $comment->insert(); } catch (Exception $e) { EventLog::log($e->getMessage(), 'err', null, null, print_r(array($c, $e), 1)); Session::error($e->getMessage()); $errors = Options::get('import_errors'); $errors[] = $e->getMessage(); Options::set('import_errors', $errors); } break; default: break; } } EventLog::log(_t('Import complete from Blogger Export File', 'bloggerimport')); echo '<p>' . _t('Import is complete.') . '</p>'; $errors = Options::get('import_errors'); if (count($errors) > 0) { echo '<p>' . _t('There were errors during import:') . '</p>'; echo '<ul>'; foreach ($errors as $error) { echo '<li>' . $error . '</li>'; } echo '</ul>'; } }
/** * The plugin sink for the auth_ajax_drupal_import_comments hook. * Responds via authenticated ajax to requests for comment importing. * * @param AjaxHandler $handler The handler that handled the request, contains $_POST info */ public function action_auth_ajax_drupal_import_comments($handler) { $valid_fields = array('db_name', 'db_host', 'db_user', 'db_pass', 'db_prefix', 'import_comments', 'commentindex', 'entry_type', 'page_type', 'tag_vocab'); $inputs = array_intersect_key($_POST->getArrayCopy(), array_flip($valid_fields)); extract($inputs); $drupaldb = $this->drupal_connect($db_host, $db_name, $db_user, $db_pass, $db_prefix); if ($drupaldb) { $commentcount = $drupaldb->get_value("SELECT count( c.cid ) FROM {$db_prefix}comments AS c INNER JOIN {$db_prefix}node AS n ON (n.nid = c.nid) WHERE n.type IN ('{$entry_type}', '{$page_type}')"); $min = $commentindex * DRUPAL_IMPORT_BATCH + 1; $max = min(($commentindex + 1) * DRUPAL_IMPORT_BATCH, $commentcount); echo "<p>Importing comments {$min}-{$max} of {$commentcount}.</p>"; $postinfo = DB::table('postinfo'); $post_info = DB::get_results("SELECT post_id, value FROM {$postinfo} WHERE name= 'drupal_nid';"); foreach ($post_info as $info) { $post_map[$info->value] = $info->post_id; } if ($import_comments) { $comments = $drupaldb->get_results("\n\t\t\t\t\tSELECT\n\t\t\t\t\t\tc.nid as drupal_post_nid,\n\t\t\t\t\t\tc.comment as content,\n\t\t\t\t\t\tc.name,\n\t\t\t\t\t\tc.mail as email,\n\t\t\t\t\t\tc.homepage as url,\n\t\t\t\t\t\tINET_ATON( c.hostname ) as ip,\n\t\t\t\t\t\tc.status,\n\t\t\t\t\t\tFROM_UNIXTIME( c.timestamp ) as date\n\t\t\t\t\tFROM {$db_prefix}comments AS c\n\t\t\t\t\tINNER JOIN {$db_prefix}node AS n on ( n.nid = c.nid )\n\t\t\t\t\tLIMIT {$min}, " . DRUPAL_IMPORT_BATCH, array(), 'Comment'); } else { $comments = array(); } foreach ($comments as $comment) { $comment->type = Comment::COMMENT; $comment->status = $comment->status == '0' ? 1 : 0; $comment->content = MultiByte::convert_encoding($comment->content); $comment->name = MultiByte::convert_encoding($comment->name); $carray = $comment->to_array(); if ($carray['ip'] == '') { $carray['ip'] = 0; } if (!isset($post_map[$carray['drupal_post_nid']])) { Utils::debug($carray); } else { $carray['post_id'] = $post_map[$carray['drupal_post_nid']]; unset($carray['drupal_post_nid']); $c = new Comment($carray); //Utils::debug( $c ); try { $c->insert(); } catch (Exception $e) { EventLog::log($e->getMessage(), 'err', null, null, print_r(array($c, $e), 1)); $errors = Options::get('import_errors'); $errors[] = $e->getMessage(); Options::set('import_errors', $errors); } } } if ($max < $commentcount) { $ajax_url = URL::get('auth_ajax', array('context' => 'drupal_import_comments')); $commentindex++; echo <<<DRUPAL_IMPORT_AJAX1 \t\t\t\t\t<script type="text/javascript"> \t\t\t\t\t\$( '#import_progress' ).load( \t\t\t\t\t\t"{$ajax_url}", \t\t\t\t\t\t{ \t\t\t\t\t\t\tdb_host: "{$db_host}", \t\t\t\t\t\t\tdb_name: "{$db_name}", \t\t\t\t\t\t\tdb_user: "******", \t\t\t\t\t\t\tdb_pass: "******", \t\t\t\t\t\t\tdb_prefix: "{$db_prefix}", \t\t\t\t\t\t\timport_comments: "{$import_comments}", \t\t\t\t\t\t\tentry_type: "{$entry_type}", \t\t\t\t\t\t\tpage_type: "{$page_type}", \t\t\t\t\t\t\ttag_vocab: "{$tag_vocab}", \t\t\t\t\t\t\tcommentindex: {$commentindex} \t\t\t\t\t\t} \t\t\t\t\t ); \t\t\t\t</script> DRUPAL_IMPORT_AJAX1; } else { EventLog::log('Import complete from "' . $db_name . '"'); echo '<p>' . _t('Import is complete.') . '</p>'; $errors = Options::get('import_errors'); if (count($errors) > 0) { echo '<p>' . _t('There were errors during import:') . '</p>'; echo '<ul>'; foreach ($errors as $error) { echo '<li>' . $error . '</li>'; } echo '</ul>'; } } } else { EventLog::log(sprintf(_t('Failed to import from "%s"'), $db_name), 'crit'); echo '<p>' . _t('Failed to connect using the given database connection details.') . '</p>'; } }