Skip to content

Commit

Permalink
Merge branch 'release/1.7.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
r-guimaraes committed Oct 11, 2018
2 parents 5ddc1a7 + f10f034 commit c79a52a
Show file tree
Hide file tree
Showing 53 changed files with 6,111 additions and 54,878 deletions.
63 changes: 60 additions & 3 deletions migration-scripts/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
class RHSImporter {

var $steps = array(

// nomeDoArquivo => Descrição do passo

'posts' => 'Importação básica dos posts',
'posts-follow' => 'Importação das usuários que seguem posts',
'users' => 'Importação básica dos usuários',
Expand All @@ -29,7 +27,8 @@ class RHSImporter {
'estados-cidades-users' => 'Importação das informações de estado e cidade para usuários',
'categories-new' => 'Cria Novas Categorias',
'add-users-to-channels' => 'Adicionar usuários a canais de notificações',
'comunities' => 'Importação das comunidades'
'comunities' => 'Importação das comunidades',
'attachments' => "Importação de anexos" //17 ou 18
);


Expand Down Expand Up @@ -249,6 +248,64 @@ function query($sqlname, $substitutions = array()) {

$this->log("🍕 $c registros afetados\n");
}

function get_results($sqlname, $substitutions = array()) {
$query = $this->get_sql($sqlname, $substitutions);
if ($query === false)
return false;

global $wpdb;
$c = $wpdb->get_results($query, 'ARRAY_A');

$this->log("🍕 $c registros afetados\n");
return $c;
}

function insert_attachment_from_url($url, $post_id = null) {

if( !class_exists( 'WP_Http' ) )
include_once( ABSPATH . WPINC . '/class-http.php' );

$http = new WP_Http();
$response = $http->request( $url );
if( $response['response']['code'] != 200 ) {
return false;
}

$upload = wp_upload_bits( basename($url), null, $response['body'] );
if( !empty( $upload['error'] ) ) {
return false;
}

$file_path = $upload['file'];
$file_name = basename( $file_path );
$file_type = wp_check_filetype( $file_name, null );
$attachment_title = sanitize_file_name( pathinfo( $file_name, PATHINFO_FILENAME ) );
$wp_upload_dir = wp_upload_dir();

$post_info = array(
'guid' => $wp_upload_dir['url'] . '/' . $file_name,
'post_mime_type' => $file_type['type'],
'post_title' => $attachment_title,
'post_content' => '',
'post_status' => 'inherit',
);

// Create the attachment
$attach_id = wp_insert_attachment( $post_info, $file_path, $post_id );

// Include image.php
require_once( ABSPATH . 'wp-admin/includes/image.php' );

// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file_path );

// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id, $attach_data );

return $attach_id;

}

function wpcli($command) {

Expand Down
3 changes: 3 additions & 0 deletions migration-scripts/sql/attachments.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT fu.fid, fu.id, f.filename, f.filepath
FROM {{drupaldb}}.file_usage AS fu join {{drupaldb}}.files AS f
ON fu.fid = f.fid;
5,573 changes: 5,573 additions & 0 deletions migration-scripts/sql/populacao_cidades.sql

Large diffs are not rendered by default.

85 changes: 85 additions & 0 deletions migration-scripts/steps/attachments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');


function handle_upload_from_path( $path, $parent_post_id, $add_to_media = true ) {
if ( !file_exists($path) ) {
return array( 'error' => 'File does not exist.' );
}
$filename = basename($path);
$filename_no_ext = pathinfo($path, PATHINFO_FILENAME);
$extension = pathinfo($path, PATHINFO_EXTENSION);
// Simulate uploading a file through $_FILES. We need a temporary file for this.
$tmp = tmpfile();
$tmp_path = stream_get_meta_data($tmp)['uri'];
fwrite($tmp, file_get_contents( $path ));
fseek($tmp, 0); // If we don't do this, WordPress thinks the file is empty
$fake_FILE = array(
'name' => $filename,
'type' => 'image/' . $extension,
'tmp_name' => $tmp_path,
'error' => UPLOAD_ERR_OK,
'size' => filesize($path),
);
// Trick is_uploaded_file() by adding it to the superglobal
$_FILES[basename($tmp_path)] = $fake_FILE;
$result = wp_handle_upload( $fake_FILE, array( 'test_form' => false, 'action' => 'local' ) );
fclose($tmp); // Close tmp file
@unlink($tmp_path); // Delete the tmp file. Closing it should also delete it, so hide any warnings with @
unset( $_FILES[basename($tmp_path)] ); // Clean up our $_FILES mess.
$result['attachment_id'] = 0;

if ( empty($result['error']) && $add_to_media ) {
$args = array(
'guid' => $result['url'],
'post_title' => $filename_no_ext,
'post_content' => '',
'post_status' => 'publish',
'post_mime_type' => $result['type'],
);
$result['attachment_id'] = wp_insert_attachment( $args, $result['file'], $parent_post_id );
if ( is_wp_error( $result['attachment_id'] ) ) {
$result['attachment_id'] = 0;
}else{
$attach_data = wp_generate_attachment_metadata( $result['attachment_id'], $result['file'] );
wp_update_attachment_metadata( $result['attachment_id'], $attach_data );
}
}
return $result;
}

$this->log('Importando anexos dos posts...');
$results = $this->get_results('attachments');

$attachments_path_dir = "/path/to/attachment_files/"; //Diretorio de anexos deve ser configurado

$wp_upload_dir = wp_upload_dir();

foreach ($results as $attachment)
{
if(strpos($attachment['filepath'], '/files') !== false)
{
$file_name = explode("files/", $attachment['filepath']);
$file_name = end($file_name);

$file_path = $attachments_path_dir . $file_name;

if(file_exists($file_path))
{

$f = explode(".", $file_path);
$filetype = end($f);
$filename = $attachment['filename'];

$up_file = handle_upload_from_path($file_path, $attachment['id'], true);

$info = $up_file['attachment_id'] . " ==> " . $up_file['file'] . " ============= " . $up_file['url'];
$logFile = file_put_contents('logs.txt', $info.PHP_EOL , FILE_APPEND | LOCK_EX);
}
}
}

$this->log('Anexos importados: ' . count($results));
Binary file removed public/wp-content/languages/admin-network-pt_BR.mo
Binary file not shown.
Loading

0 comments on commit c79a52a

Please sign in to comment.