Guest User

Untitled

a guest
Apr 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.60 KB | None | 0 0
  1. diff --git a/var/tools/migration/migrations/20180411075710_delete_companies_spammers.php b/var/tools/migration/migrations/20180411075710_delete_companies_spammers.php
  2. new file mode 100644
  3. index 0000000..7ae30f3
  4. --- /dev/null
  5. +++ b/var/tools/migration/migrations/20180411075710_delete_companies_spammers.php
  6. @@ -0,0 +1,199 @@
  7. +<?php
  8. +
  9. +use Migration\MigrationHelper;
  10. +
  11. +class DeleteCompaniesSpammers extends MigrationHelper
  12. +{
  13. + /**
  14. + * Migrate Up.
  15. + */
  16. + public function up()
  17. + {
  18. + $pr = $this->prefix;
  19. +
  20. + $email_all = $this->getEmailList();
  21. +
  22. + foreach ($email_all as $email_list) {
  23. + $companies = $this->fetchAll(
  24. + "SELECT c.company_id, c.email,"
  25. + . " COUNT(t.ticket_id) AS t_count, COUNT(m.message_id) AS m_count, COUNT(l.license_id) as l_count"
  26. + . " FROM {$pr}companies"
  27. + . " LEFT JOIN {$pr}hd_tickets AS t ON t.company_id = c.company_id"
  28. + . " LEFT JOIN {$pr}hd_messages AS m ON m.ticket_id = t.ticket_id"
  29. + . " LEFT JOIN {$pr}hd_licenses AS l ON l.company_id = c.company_id"
  30. + . " WHERE c.email IN " . implode(',', $email_list)
  31. + . " GROUP BY c.company_id, t.ticket_id"
  32. + . " HAVING t_count <= 1"
  33. + . " AND m_count <= 1"
  34. + . " AND l_count = 0"
  35. + );
  36. +
  37. + if (!$companies) {
  38. + return;
  39. + }
  40. +
  41. + $companies_ids = array_column($companies, 'company_id');
  42. + $companies_list = implode(',', $companies_ids);
  43. +
  44. + $this->query("DELETE FROM {$pr}companies WHERE company_id IN " . $companies_list);
  45. + $this->query("DELETE FROM {$pr}company_descriptions WHERE company_id IN " . $companies_list);
  46. + $this->query("DELETE FROM {$pr}seo_names WHERE type = 'M' AND object_id IN " . $companies_list);
  47. +
  48. + $this->query("DELETE FROM {$pr}hd_companies WHERE company_id IN " . $companies_list);
  49. + $this->query("DELETE FROM {$pr}hd_company_addresses WHERE company_id IN " . $companies_list);
  50. + $this->query("DELETE FROM {$pr}hd_company_access WHERE company_id IN " . $companies_list);
  51. + $this->query("DELETE FROM {$pr}hd_company_user_links WHERE company_id IN " . $companies_list);
  52. + $this->query("DELETE FROM {$pr}hd_license_tracking_stats WHERE company_id IN " . $companies_list);
  53. + $this->query("DELETE FROM {$pr}hd_support_periods WHERE company_id IN " . $companies_list);
  54. + $this->query("DELETE FROM {$pr}hd_support_periods_history WHERE company_id IN " . $companies_list);
  55. + $this->query("DELETE FROM {$pr}hd_rebrand WHERE company_id IN " . $companies_list);
  56. +
  57. + $products = $this->fetchAll("SELECT product_id FROM {$pr}products WHERE company_id IN " . $companies_list);
  58. +
  59. + if ($products) {
  60. + $products_ids = array_column($products, 'product_id');
  61. + $products_ids = implode(',', $products_ids);
  62. +
  63. + $this->query("DELETE FROM {$pr}products_categories WHERE product_id IN " . $products_ids);
  64. + $this->query("DELETE FROM {$pr}products WHERE product_id IN " . $products_ids);
  65. + $this->query("DELETE FROM {$pr}product_descriptions WHERE product_id IN " . $products_ids);
  66. + $this->query("DELETE FROM {$pr}product_prices WHERE product_id IN " . $products_ids);
  67. + $this->query("DELETE FROM {$pr}product_features_values WHERE product_id IN " . $products_ids);
  68. + $this->query("DELETE FROM {$pr}product_options_exceptions WHERE product_id IN " . $products_ids);
  69. + $this->query("DELETE FROM {$pr}product_popularity WHERE product_id IN " . $products_ids);
  70. +
  71. + //images
  72. +
  73. + /*$_opts = db_get_fields("SELECT option_id FROM {$pr}product_options WHERE product_id = ?i", $product_id);
  74. + if (!fn_is_empty($_opts)) {
  75. + foreach ($_opts as $k => $v) {
  76. + $_vars = db_get_fields("SELECT variant_id FROM {$pr}product_option_variants WHERE option_id = ?i", $v);
  77. + db_query("DELETE FROM {$pr}product_options_descriptions WHERE option_id = ?i", $v);
  78. + if (!fn_is_empty($_vars)) {
  79. + foreach ($_vars as $k1 => $v1) {
  80. + db_query("DELETE FROM {$pr}product_option_variants_descriptions WHERE variant_id = ?i", $v1);
  81. + }
  82. + db_query("DELETE FROM {$pr}product_option_variants WHERE option_id = ?i", $v);
  83. + }
  84. + }
  85. + }*/
  86. +
  87. + $this->query("DELETE FROM {$pr}product_options WHERE product_id IN " . $products_ids);
  88. + $this->query("DELETE FROM {$pr}product_options_exceptions WHERE product_id IN " . $products_ids);
  89. + $this->query("DELETE FROM {$pr}product_options_inventory WHERE product_id IN " . $products_ids);
  90. +
  91. + //fn_build_products_cache
  92. + }
  93. +
  94. + $shippings = $this->fetchAll("SELECT shipping_id FROM {$pr}shippings WHERE company_id IN " . $companies_list);
  95. + if ($shippings) {
  96. + $shippings_ids = array_column($shippings, 'shipping_id');
  97. + $shippings_ids = implode(',', $shippings_ids);
  98. +
  99. + $this->query("DELETE FROM {$pr}shipping_rates WHERE shipping_id IN " . $shippings_ids);
  100. + $this->query("DELETE FROM {$pr}shipping_descriptions WHERE shipping_id IN " . $shippings_ids);
  101. + $this->query("DELETE FROM {$pr}shippings WHERE shipping_id IN " . $shippings_ids);
  102. + }
  103. +
  104. + //fn_delete_product_option($option_id)
  105. +
  106. + //fn_delete_user($user_id);
  107. +
  108. + //fn_delete_page($page_id)
  109. +
  110. + //fn_delete_promotions($promotion_ids);
  111. +
  112. + $thread = $this->fetchAll(
  113. + "SELECT thread_id FROM {$pr}discussion"
  114. + . " WHERE object_type = 'M' object_id IN " . $companies_list
  115. + );
  116. +
  117. + if ($thread) {
  118. + $thread_ids = array_column($thread, 'thread_id');
  119. + $thread_ids = implode(',', $thread_ids);
  120. +
  121. + $this->query("DELETE FROM {$pr}discussion_messages WHERE thread_id IN " . $thread_ids);
  122. + $this->query("DELETE FROM {$pr}discussion_posts WHERE thread_id IN " . $thread_ids);
  123. + $this->query("DELETE FROM {$pr}discussion_rating WHERE thread_id IN " . $thread_ids);
  124. + $this->query("DELETE FROM {$pr}discussion WHERE thread_id IN " . $thread_ids);
  125. + }
  126. +
  127. + $license = $this->fetchAll("SELECT license_id FROM {$pr}hd_licenses WHERE company_id IN " . $companies_list);
  128. + if ($license) {
  129. + $license_ids = array_column($license, 'license_id');
  130. + $license_ids = implode(',', $license_ids);
  131. +
  132. + $this->query(
  133. + "DELETE l, t, p, s, pl FROM {$pr}hd_licenses as l "
  134. + . " LEFT JOIN {$pr}hd_license_csrf_tokens as t USING(license_id)"
  135. + . " LEFT JOIN {$pr}hd_license_prolongations as p ON l.license_id = p.object_id AND (p.type = 'license' OR p.type = 'updates')"
  136. + . " LEFT JOIN {$pr}hd_license_status_log as s USING(license_id)"
  137. + . " LEFT JOIN {$pr}hd_project_licenses as pl USING(license_id)"
  138. + . " WHERE l.license_id IN " . $license_ids
  139. + );
  140. + }
  141. +
  142. + $projects = $this->fetchAll("SELECT project_id FROM {$pr}hd_projects WHERE company_id IN " . $companies_list);
  143. + if ($projects) {
  144. + $project_ids = array_column($projects, 'project_id');
  145. + $project_ids = implode(',', $project_ids);
  146. +
  147. + $this->query("DELETE FROM {$pr}hd_projects WHERE project_id IN ", $project_ids);
  148. + $this->query("DELETE FROM {$pr}hd_project_licenses WHERE project_id IN ", $project_ids);
  149. + }
  150. +
  151. + $tasks = $this->fetchAll("SELECT task_id FROM {$pr}hd_tasks WHERE company_id IN " . $companies_list);
  152. + if ($tasks) {
  153. + $tasks_ids = array_column($tasks, 'task_id');
  154. + $tasks_ids = implode(',', $tasks_ids);
  155. +
  156. + $this->query("DELETE FROM {$pr}hd_tasks WHERE task_id IN " . $tasks_ids);
  157. + $this->query("DELETE FROM {$pr}hd_tasks_users WHERE task_id IN " . $tasks_ids);
  158. + $this->query("DELETE FROM {$pr}hd_task_status_log WHERE task_id IN " . $tasks_ids);
  159. +
  160. + $attachments = $this->fetchAll("SELECT attachment_id FROM {$pr}attachments WHERE object_type = 'task' AND object_id IN " . $tasks_ids);
  161. + if ($attachments) {
  162. + $attachments_ids = array_column($attachments, 'attachment_id');
  163. + $attachments_ids = implode(',', $attachments_ids);
  164. +
  165. + $this->query("DELETE FROM {$pr}hd_task_attachment_descriptions WHERE attachment_id IN " . $attachments_ids);
  166. + }
  167. +
  168. + //attachments
  169. + }
  170. +
  171. + $tickets = $this->fetchAll("SELECT ticket_id FROM {$pr}hd_tickets WHERE company_id IN ", $companies_list);
  172. + if ($tickets) {
  173. + $ticket_ids = array_column($tickets, 'ticket_id');
  174. + $ticket_ids = implode(',', $ticket_ids);
  175. +
  176. + $this->query("DELETE FROM {$pr}hd_ticket_support WHERE ticket_id IN ", $ticket_ids);
  177. + $this->query("DELETE FROM {$pr}hd_ticket_bugs WHERE ticket_id IN ", $ticket_ids);
  178. + $this->query("DELETE FROM {$pr}hd_ticket_quote WHERE ticket_id IN ", $ticket_ids);
  179. + $this->query("DELETE FROM {$pr}hd_ticket_responsible WHERE ticket_id IN " . $ticket_ids);
  180. + $this->query("DELETE FROM {$pr}hd_ticket_respondents WHERE ticket_id IN " . $ticket_ids);
  181. + $this->query("DELETE FROM {$pr}hd_tickets WHERE ticket_id IN " . $ticket_ids);
  182. + }
  183. +
  184. +
  185. + $attachments = $this->fetchAll(
  186. + "SELECT attachment_id FROM {$pr}attachments"
  187. + . " WHERE object_type = 'company' AND object_id IN " . $companies_list
  188. + );
  189. + if ($attachments) {
  190. + $attachments_ids = array_column($attachments, 'attachment_id');
  191. + $attachments_ids = implode(',', $attachments_ids);
  192. +
  193. + $this->query("DELETE FROM {$pr}hd_task_attachment_descriptions WHERE attachment_id IN " . $attachments_ids);
  194. + }
  195. + }
  196. + }
  197. +
  198. + protected function getEmailList()
  199. + {
  200. + return array(
  201. + array(),
  202. + array()
  203. + );
  204. + }
  205. +}
  206. \ No newline at end of file
Add Comment
Please, Sign In to add comment