Advertisement
Guest User

Untitled

a guest
Jun 14th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. <?php
  2. /**
  3. для каждого сайта надо передить из wp-config.php этого же сайта
  4. dbname=название базы данных
  5. $user = имя пользователя;
  6. $pass = пароль;
  7. $table_prefix = префикс таблицы;
  8.  
  9. копируем данный скрипт в папку public_html сайта
  10. входим по ssh
  11.  
  12. в терминале вводим:
  13. cd папка_сайта/public_html/
  14.  
  15. далее вводим команду
  16.  
  17. php -f title_alt.php
  18. */
  19.  
  20. $dsn = "mysql:host=127.0.0.1;dbname=krovldb;charset=utf8";
  21. $user = 'xxxx';
  22. $pass = 'xxxx';
  23. $table_prefix = 'xxx';
  24.  
  25. $db = new PDO($dsn, $user, $pass);
  26.  
  27. // setup PDO to throw an exception if an invalid query is provided
  28. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  29.  
  30. $id = 0;
  31.  
  32. $sql = "SELECT * FROM `{$table_prefix}posts` as `posts`
  33. WHERE `posts`.`ID`> {$id}
  34. AND `posts`.`post_content` LIKE '%[caption%]%title=%src=%[/caption]%'
  35. AND `posts`.`post_type` = 'post'
  36. ORDER BY `posts`.`ID` LIMIT 50";
  37.  
  38.  
  39. // Next, let's prepare a statement for execution, with a single placeholder
  40.  
  41. $statement = $db->prepare($sql);
  42.  
  43. // Create some parameters to fill the placeholders, and execute the statement
  44. $parameters = [ "221B" ];
  45. $statement->execute($parameters);
  46. $rows = $statement->fetchAll(PDO::FETCH_OBJ);
  47.  
  48. while ( count($rows) > 0 ) {
  49. foreach ($rows as $row) {
  50. $content = $row->post_content;
  51. $content_arr = explode( '[/caption]', $content );
  52. $new_content_arr = array();
  53. foreach ($content_arr as $content_element) {
  54. $id = $row->ID;
  55.  
  56. //Находим url
  57. $captionpos = mb_strpos( $content_element, '[caption id="attachment', 0,'UTF-8' );
  58. $href_pos_start = mb_strpos( $content_element, '<a href="', $captionpos,'UTF-8' );
  59.  
  60. $href_pos_end = mb_strpos( $content_element, '"', $href_pos_start,'UTF-8' );
  61.  
  62. //Находим title
  63. $img_url = mb_substr($content_element, $href_pos_start, $href_pos_end - $href_pos_start, 'UTF-8' );
  64.  
  65. //Находим строки для замены
  66. if ($img_url !== "") {
  67.  
  68. $alt_start += 5;
  69. $title_start += 7;
  70. $alt = mb_substr($content_element, $alt_start, $alt_end - $alt_start, 'UTF-8' );
  71. $title = mb_substr($content_element, $title_start, $title_end - $title_start, 'UTF-8' );
  72. //var_dump( $title );
  73. $content_element = str_replace($alt, $title, $content_element);
  74.  
  75. }
  76. $new_content_arr[] = $content_element;
  77.  
  78. }
  79. $content = implode( '[/caption]', $new_content_arr );
  80. //if ( $id == 1005 ){
  81. try {
  82. echo '>>>>>Начало<<<<<<<' . PHP_EOL;
  83. $sql = "UPDATE `{$table_prefix}posts` as `posts`
  84. SET `posts`.`post_content` = ?
  85. WHERE `posts`.`ID` = ?";
  86.  
  87. $statement = $db->prepare( $sql );
  88. $statement->execute([$content, $id]);
  89. echo 'id = '.$id.' >>>>>Обновлено<<<<<<<' . PHP_EOL;
  90. } catch (\Exception $e) {
  91. if ($db->inTransaction()) {
  92. $db->rollback();
  93. // If we got here our data updates are not in the database
  94. }
  95. echo '>>>>>Ошибке<<<<<<< ' . PHP_EOL . $e->getMessage() . PHP_EOL;
  96. throw $e;
  97. }
  98. //}
  99. }
  100.  
  101. $sql = "SELECT * FROM `{$table_prefix}posts` as `posts`
  102. WHERE `posts`.`ID`> {$id}
  103. AND `posts`.`post_content` LIKE '%[caption%]%title=%src=%[/caption]%'
  104. AND `posts`.`post_type` = 'post'
  105. ORDER BY `posts`.`ID` LIMIT 50";
  106.  
  107. $statement = $db->prepare($sql);
  108. // Create some parameters to fill the placeholders, and execute the statement
  109. $parameters = [ "221B" ];
  110. $statement->execute($parameters);
  111. $rows = null;
  112. $rows = $statement->fetchAll(PDO::FETCH_OBJ);
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement