Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.60 KB | None | 0 0
  1. <?php
  2. require_once("config.php");
  3.  
  4. $db = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
  5. // verificar si hay conexión con la base de datos
  6. if ($db->connect_errno) {
  7.   echo "Error: Fallo al conectarse a MySQL debido a:", EOL;
  8.   echo "Errno: " . $db->connect_errno . EOL;
  9.   echo "Error: " . $db->connect_error . EOL;
  10.   exit;
  11. }
  12. // // Consulta para extraer los registros a importar
  13. // $sqlOriginPosts = "SELECT * FROM wp5x_posts WHERE ID > 1116; ";
  14. // // Ejecución de consulta
  15. // $rsPostA = $db->query($sqlOriginPosts);
  16. // // Mensaje debug
  17. // echo " Se encontraron: {$rsPostA->num_rows} artículos ", EOL;
  18. // // fecha de hoy
  19. // $today = Date('Y-m-d h:i:s');
  20. // // Si hay registros
  21. // if ($rsPostA->num_rows) {
  22. //   // Mientras el recordset pueda obtener objetos
  23. //   while ($postOrigen = $rsPostA->fetch_object()) {
  24. //     // Debug para el post que se elige
  25. //     echo "postOrigen->ID: {$postOrigen->ID}", EOL;
  26. //     // Consulta SQL para insertar el registro actual en la tabla de wp5x_posts
  27. //     $sqlInsertPost = "INSERT INTO `wp5x_posts`
  28. //     (`ID`,`post_author`, `post_date`, `post_date_gmt`, `post_content`,
  29. //     `post_title`, `post_excerpt`, `post_status`, `comment_status`,
  30. //     `ping_status`, `post_password`, `post_name`, `to_ping`,
  31. //     `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`,
  32. //     `post_parent`, `guid`, `menu_order`, `post_type`,
  33. //     `post_mime_type`, `comment_count`)
  34. //     VALUES
  35. //    ('".mysqli_escape_string($db, $postOrigen->ID)."', 1, '{$today}', '{$today}', '".mysqli_escape_string($db, $postOrigen->post_content)."',
  36. //     '".mysqli_escape_string($db, $postOrigen->post_title)."', '".mysqli_escape_string($db, $postOrigen->post_date_gmt)."', '".mysqli_escape_string($db, $postOrigen->post_status)."', '".mysqli_escape_string($db, $postOrigen->comment_status)."',
  37. //     '".mysqli_escape_string($db, $postOrigen->ping_status)."', '".mysqli_escape_string($db, $postOrigen->post_password)."', '".mysqli_escape_string($db, $postOrigen->post_name)."', '".mysqli_escape_string($db, $postOrigen->to_ping)."',
  38. //     '".mysqli_escape_string($db, $postOrigen->pinged)."', '".mysqli_escape_string($db, $postOrigen->post_modified)."', '".mysqli_escape_string($db, $postOrigen->post_modified_gmt)."', '".mysqli_escape_string($db, $postOrigen->post_content_filtered)."',
  39. //     '".mysqli_escape_string($db, $postOrigen->post_parent)."', '".mysqli_escape_string($db, $postOrigen->guid)."', '".mysqli_escape_string($db, $postOrigen->menu_order)."', '".mysqli_escape_string($db, $postOrigen->post_type)."',
  40. //     '".mysqli_escape_string($db, $postOrigen->post_mime_type)."', '".mysqli_escape_string($db, $postOrigen->comment_count)."');";
  41. //
  42. //     // Ejecución de la primer inserción
  43. //     if (!mysqli_query($db, $sqlInsertPost)) {
  44. //       printf("\$sqlInsertPost: Errormessage: %s\n", mysqli_error($db));
  45. //     } else {
  46. //       echo " --- --- \t POST: Registro insertado ".EOL;
  47. //     }
  48. //
  49. //     // Consulta para insertar las relaciones del post con los terms de destino;
  50. //     $sqlInsertTermRelationship = "INSERT INTO `wp5x_term_relationships`
  51. //     (`object_id`, `term_taxonomy_id`)
  52. //     VALUES
  53. //     ('".mysqli_escape_string($db, $postOrigen->ID)."', '".mysqli_escape_string($db, $postOrigen->post_parent)."');";
  54. //
  55. //
  56. //     if (!mysqli_query($db, $sqlInsertTermRelationship)) {
  57. //       printf("\$sqlInsertTermRelationship: Errormessage: %s\n", mysqli_error($link));
  58. //     } else {
  59. //       echo " --- --- \t RELATIONSHIP: Registro insertado ".EOL;
  60. //     }
  61. //
  62. //
  63. //   }
  64. // }
  65. function slugify($text)
  66. {
  67.   // replace non letter or digits by -
  68.   $text = preg_replace('~[^\pL\d]+~u', '-', $text);
  69.  
  70.   // transliterate
  71.   $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  72.  
  73.   // remove unwanted characters
  74.   $text = preg_replace('~[^-\w]+~', '', $text);
  75.  
  76.   // trim
  77.   $text = trim($text, '-');
  78.  
  79.   // remove duplicate -
  80.   $text = preg_replace('~-+~', '-', $text);
  81.  
  82.   // lowercase
  83.   $text = strtolower($text);
  84.  
  85.   if (empty($text)) {
  86.     return 'n-a';
  87.   }
  88.  
  89.   return $text;
  90. }
  91.  
  92. $db = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
  93. // verificar si hay conexión con la base de datos
  94. if ($db->connect_errno) {
  95.   echo "Error: Fallo al conectarse a MySQL debido a:", EOL;
  96.   echo "Errno: " . $db->connect_errno . EOL;
  97.   echo "Error: " . $db->connect_error . EOL;
  98.   exit;
  99. }
  100. // Consulta para extraer los registros a importar
  101. $sqlOriginPosts = "SELECT * FROM wp5x_posts WHERE ID > 1116; ";
  102. // Ejecución de consulta
  103. $rsPostA = $db->query($sqlOriginPosts);
  104. // Mensaje debug
  105. echo " Se encontraron: {$rsPostA->num_rows} artículos ", EOL;
  106. // fecha de hoy
  107. // $today = Date('Y-m-d h:i:s');
  108. // Si hay registros
  109. if ($rsPostA->num_rows) {
  110.   // Mientras el recordset pueda obtener objetos
  111.   echo " START TRANSACTION ; ", EOL;
  112.   while ($postOrigen = $rsPostA->fetch_object()) {
  113.     // Debug para el post que se elige
  114.     // echo "postOrigen->ID: {$postOrigen->ID}", EOL;
  115.     // Consulta SQL para insertar el registro actual en la tabla de wp5x_posts
  116.     if (slugify($postOrigen->post_name) == 'n-a') {
  117.       // Si el guid tiene extensión png o jpg
  118.       if (strstr($postOrigen->guid, 'png') || strstr($postOrigen->guid, 'jpg')) {
  119.         // le quito todo lo que no ocupo hasta donde empieza la ruta del artículo de social engine
  120.         $tmpImgName = substr($postOrigen->guid, 15);
  121.         echo $sqlUpdateSlug = "UPDATE wp5x_posts SET guid = 'http://studiopigmento.com/bodycultures/".mysqli_escape_string($db, $tmpImgName)."' WHERE ID = '".mysqli_escape_string($db, $postOrigen->ID)."' ;";
  122.         echo EOL;
  123.       }
  124.     }
  125.   }
  126.   echo " COMMIT;", EOL;
  127. }
  128.  ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement