/bp-forums/bbpress/bb-admin/includes/functions.bb-recount.php

  1. <?php 
  2.  
  3. function bb_recount_topic_posts() 
  4. global $bbdb; 
  5.  
  6. $statement = __( 'Counting the number of posts in each topic… %s' ); 
  7. $result = __( 'Failed!' ); 
  8.  
  9. $sql = "INSERT INTO `$bbdb->topics` (`topic_id`, `topic_posts`) (SELECT `topic_id`, COUNT(`post_status`) as `topic_posts` FROM `$bbdb->posts` WHERE `post_status` = '0' GROUP BY `topic_id`) ON DUPLICATE KEY UPDATE `topic_posts` = VALUES(`topic_posts`);"; 
  10. if ( is_wp_error( $bbdb->query( $sql ) ) ) { 
  11. return array( 1, sprintf( $statement, $result ) ); 
  12.  
  13. $result = __( 'Complete!' ); 
  14. return array( 0, sprintf( $statement, $result ) ); 
  15.  
  16. function bb_recount_topic_voices() 
  17. global $bbdb; 
  18.  
  19. $statement = __( 'Counting the number of voices in each topic… %s' ); 
  20. $result = __( 'Failed!' ); 
  21.  
  22. $sql_delete = "DELETE FROM `$bbdb->meta` WHERE `object_type` = 'bb_topic' AND `meta_key` = 'voices_count';"; 
  23. if ( is_wp_error( $bbdb->query( $sql_delete ) ) ) { 
  24. return array( 1, sprintf( $statement, $result ) ); 
  25.  
  26. $sql = "INSERT INTO `$bbdb->meta` (`object_type`, `object_id`, `meta_key`, `meta_value`) (SELECT 'bb_topic', `topic_id`, 'voices_count', COUNT(DISTINCT `poster_id`) as `meta_value` FROM `$bbdb->posts` WHERE `post_status` = '0' GROUP BY `topic_id`);"; 
  27. if ( is_wp_error( $bbdb->query( $sql ) ) ) { 
  28. return array( 2, sprintf( $statement, $result ) ); 
  29.  
  30. $result = __( 'Complete!' ); 
  31. return array( 0, sprintf( $statement, $result ) ); 
  32.  
  33. function bb_recount_topic_deleted_posts() 
  34. global $bbdb; 
  35.  
  36. $statement = __( 'Counting the number of deleted posts in each topic… %s' ); 
  37. $result = __( 'Failed!' ); 
  38.  
  39. $sql_delete = "DELETE FROM `$bbdb->meta` WHERE `object_type` = 'bb_topic' AND `meta_key` = 'deleted_posts';"; 
  40. if ( is_wp_error( $bbdb->query( $sql_delete ) ) ) { 
  41. return array( 1, sprintf( $statement, $result ) ); 
  42.  
  43. $sql = "INSERT INTO `$bbdb->meta` (`object_type`, `object_id`, `meta_key`, `meta_value`) (SELECT 'bb_topic', `topic_id`, 'deleted_posts', COUNT(`post_status`) as `meta_value` FROM `$bbdb->posts` WHERE `post_status` != '0' GROUP BY `topic_id`);"; 
  44. if ( is_wp_error( $bbdb->query( $sql ) ) ) { 
  45. return array( 2, sprintf( $statement, $result ) ); 
  46.  
  47. $result = __( 'Complete!' ); 
  48. return array( 0, sprintf( $statement, $result ) ); 
  49.  
  50. function bb_recount_forum_topics() 
  51. global $bbdb; 
  52.  
  53. $statement = __( 'Counting the number of topics in each forum… %s' ); 
  54. $result = __( 'Failed!' ); 
  55.  
  56. $sql = "INSERT INTO `$bbdb->forums` (`forum_id`, `topics`) (SELECT `forum_id`, COUNT(`topic_status`) as `topics` FROM `$bbdb->topics` WHERE `topic_status` = '0' GROUP BY `forum_id`) ON DUPLICATE KEY UPDATE `topics` = VALUES(`topics`);"; 
  57. if ( is_wp_error( $bbdb->query( $sql ) ) ) { 
  58. return array( 1, sprintf( $statement, $result ) ); 
  59.  
  60. $result = __( 'Complete!' ); 
  61. return array( 0, sprintf( $statement, $result ) ); 
  62.  
  63. function bb_recount_forum_posts() 
  64. global $bbdb; 
  65.  
  66. $statement = __( 'Counting the number of posts in each forum… %s' ); 
  67. $result = __( 'Failed!' ); 
  68.  
  69. $sql = "INSERT INTO `$bbdb->forums` (`forum_id`, `posts`) (SELECT `forum_id`, COUNT(`post_status`) as `posts` FROM `$bbdb->posts` WHERE `post_status` = '0' GROUP BY `forum_id`) ON DUPLICATE KEY UPDATE `posts` = VALUES(`posts`);"; 
  70. if ( is_wp_error( $bbdb->query( $sql ) ) ) { 
  71. return array( 1, sprintf( $statement, $result ) ); 
  72.  
  73. $result = __( 'Complete!' ); 
  74. return array( 0, sprintf( $statement, $result ) ); 
  75.  
  76. function bb_recount_user_topics_replied() 
  77. global $bbdb; 
  78.  
  79. $statement = __( 'Counting the number of topics to which each user has replied… %s' ); 
  80. $result = __( 'Failed!' ); 
  81.  
  82. $sql_select = "SELECT `poster_id`, COUNT(DISTINCT `topic_id`) as `_count` FROM `$bbdb->posts` WHERE `post_status` = '0' GROUP BY `poster_id`;"; 
  83. $insert_rows = $bbdb->get_results( $sql_select ); 
  84.  
  85. if ( is_wp_error( $insert_rows ) ) { 
  86. return array( 1, sprintf( $statement, $result ) ); 
  87.  
  88. $meta_key = $bbdb->prefix . 'topics_replied'; 
  89.  
  90. $insert_values = array(); 
  91. foreach ( $insert_rows as $insert_row ) { 
  92. $insert_values[] = "('$insert_row->poster_id', '$meta_key', '$insert_row->_count')"; 
  93.  
  94. if ( !count( $insert_values ) ) { 
  95. return array( 2, sprintf( $statement, $result ) ); 
  96.  
  97. $sql_delete = "DELETE FROM `$bbdb->usermeta` WHERE `meta_key` = '$meta_key';"; 
  98. if ( is_wp_error( $bbdb->query( $sql_delete ) ) ) { 
  99. return array( 3, sprintf( $statement, $result ) ); 
  100.  
  101. $insert_values = array_chunk( $insert_values, 10000 ); 
  102. foreach ( $insert_values as $chunk ) { 
  103. $chunk = "\n" . join( ", \n", $chunk ); 
  104. $sql_insert = "INSERT INTO `$bbdb->usermeta` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;"; 
  105.  
  106. if ( is_wp_error( $bbdb->query( $sql_insert ) ) ) { 
  107. return array( 4, sprintf( $statement, $result ) ); 
  108.  
  109. $result = __( 'Complete!' ); 
  110. return array( 0, sprintf( $statement, $result ) ); 
  111.  
  112. // This function bypasses the taxonomy API 
  113. function bb_recount_topic_tags() 
  114. global $bbdb; 
  115.  
  116. $statement = __( 'Counting the number of topic tags in each topic… %s' ); 
  117. $result = __( 'Failed!' ); 
  118.  
  119. // Delete empty tags 
  120. $delete = bb_recount_tag_delete_empty(); 
  121. if ( $delete[0] > 0 ) { 
  122. $result = __( 'Could not delete empty tags.' ); 
  123. return array( 1, sprintf( $statement, $result ) ); 
  124.  
  125. // Get all tags 
  126. $sql_terms = "SELECT 
  127. `$bbdb->term_relationships`.`object_id`,  
  128. `$bbdb->term_taxonomy`.`term_id` 
  129. FROM `$bbdb->term_relationships` 
  130. JOIN `$bbdb->term_taxonomy` 
  131. ON `$bbdb->term_taxonomy`.`term_taxonomy_id` = `$bbdb->term_relationships`.`term_taxonomy_id` 
  132. WHERE 
  133. `$bbdb->term_taxonomy`.`taxonomy` = 'bb_topic_tag' 
  134. ORDER BY 
  135. `$bbdb->term_relationships`.`object_id`,  
  136. `$bbdb->term_taxonomy`.`term_id`;"; 
  137.  
  138. $terms = $bbdb->get_results( $sql_terms ); 
  139. if ( is_wp_error( $terms ) || !is_array( $terms ) ) { 
  140. return array( 2, sprintf( $statement, $result ) ); 
  141. if ( empty( $terms ) ) { 
  142. $result = __( 'No topic tags found.' ); 
  143. return array( 3, sprintf( $statement, $result ) ); 
  144.  
  145. // Count the tags in each topic 
  146. $topics = array(); 
  147. foreach ( $terms as $term ) { 
  148. if ( !isset( $topics[$term->object_id] ) ) { 
  149. $topics[$term->object_id] = 1; 
  150. } else { 
  151. $topics[$term->object_id]++; 
  152. if ( empty( $topics ) ) { 
  153. return array( 4, sprintf( $statement, $result ) ); 
  154.  
  155. // Build the values to insert into the SQL statement 
  156. $values = array(); 
  157. foreach ($topics as $topic_id => $tag_count) { 
  158. $values[] = '(' . $topic_id . ', ' . $tag_count . ')'; 
  159. if ( empty( $values ) ) { 
  160. return array( 5, sprintf( $statement, $result ) ); 
  161.  
  162. // Update the topics with the new tag counts 
  163. $values = array_chunk( $values, 10000 ); 
  164. foreach ($values as $chunk) { 
  165. $sql = "INSERT INTO `$bbdb->topics` (`topic_id`, `tag_count`) VALUES " . implode(", ", $chunk) . " ON DUPLICATE KEY UPDATE `tag_count` = VALUES(`tag_count`);"; 
  166. if ( is_wp_error( $bbdb->query( $sql ) ) ) { 
  167. return array( 6, sprintf( $statement, $result ) ); 
  168.  
  169. $result = __( 'Complete!' ); 
  170. return array( 0, sprintf( $statement, $result ) ); 
  171.  
  172. // This function bypasses the taxonomy API 
  173. function bb_recount_tag_topics() 
  174. global $bbdb; 
  175.  
  176. $statement = __( 'Counting the number of topics in each topic tag… %s' ); 
  177. $result = __( 'Failed!' ); 
  178.  
  179. // Delete empty tags 
  180. $delete = bb_recount_tag_delete_empty(); 
  181. if ( $delete[0] > 0 ) { 
  182. $result = __( 'Could not delete empty tags.' ); 
  183. return array( 1, sprintf( $statement, $result ) ); 
  184.  
  185. // Get all tags 
  186. $sql_terms = "SELECT 
  187. `$bbdb->term_taxonomy`.`term_taxonomy_id`,  
  188. `$bbdb->term_relationships`.`object_id` 
  189. FROM `$bbdb->term_relationships` 
  190. JOIN `$bbdb->term_taxonomy` 
  191. ON `$bbdb->term_taxonomy`.`term_taxonomy_id` = `$bbdb->term_relationships`.`term_taxonomy_id` 
  192. WHERE 
  193. `$bbdb->term_taxonomy`.`taxonomy` = 'bb_topic_tag' 
  194. ORDER BY 
  195. `$bbdb->term_taxonomy`.`term_taxonomy_id`,  
  196. `$bbdb->term_relationships`.`object_id`;"; 
  197.  
  198. $terms = $bbdb->get_results( $sql_terms ); 
  199. if ( is_wp_error( $terms ) || !is_array( $terms ) ) { 
  200. return array( 2, sprintf( $statement, $result ) ); 
  201. if ( empty( $terms ) ) { 
  202. $result = __( 'No topic tags found.' ); 
  203. return array( 3, sprintf( $statement, $result ) ); 
  204.  
  205. // Count the topics in each tag 
  206. $tags = array(); 
  207. foreach ( $terms as $term ) { 
  208. if ( !isset( $tags[$term->term_taxonomy_id] ) ) { 
  209. $tags[$term->term_taxonomy_id] = 1; 
  210. } else { 
  211. $tags[$term->term_taxonomy_id]++; 
  212. if ( empty( $tags ) ) { 
  213. return array( 4, sprintf( $statement, $result ) ); 
  214.  
  215. // Build the values to insert into the SQL statement 
  216. $values = array(); 
  217. foreach ($tags as $term_taxonomy_id => $count) { 
  218. $values[] = '(' . $term_taxonomy_id . ', ' . $count . ')'; 
  219. if ( empty( $values ) ) { 
  220. return array( 5, sprintf( $statement, $result ) ); 
  221.  
  222. // Update the terms with the new tag counts 
  223. $values = array_chunk( $values, 10000 ); 
  224. foreach ($values as $chunk) { 
  225. $sql = "INSERT INTO `$bbdb->term_taxonomy` (`term_taxonomy_id`, `count`) VALUES " . implode(", ", $chunk) . " ON DUPLICATE KEY UPDATE `count` = VALUES(`count`);"; 
  226. if ( is_wp_error( $bbdb->query( $sql ) ) ) { 
  227. return array( 6, sprintf( $statement, $result ) ); 
  228.  
  229. if ($return_boolean) { 
  230. return true; 
  231. $result = __( 'Complete!' ); 
  232. return array( 0, sprintf( $statement, $result ) ); 
  233.  
  234. // This function bypasses the taxonomy API 
  235. function bb_recount_tag_delete_empty() 
  236. global $bbdb; 
  237.  
  238. $statement = __( 'Deleting topic tags with no topics… %s' ); 
  239. $result = __( 'Failed!' ); 
  240.  
  241. static $run_once; 
  242. if ( isset( $run_once ) ) { 
  243. if ($run_once > 0) { 
  244. $exit = sprintf( __( 'failure (returned code %s)' ), $run_once ); 
  245. } else { 
  246. $exit = __( 'success' ); 
  247. $result = sprintf( __( 'Already run with %s.' ), $exit ); 
  248. return array( $run_once, sprintf( $statement, $result ) ); 
  249.  
  250. // Get all topic ids 
  251. $sql_topics = "SELECT `topic_id` FROM $bbdb->topics ORDER BY `topic_id`;"; 
  252. $topics = $bbdb->get_results( $sql_topics ); 
  253. if ( is_wp_error( $topics ) ) { 
  254. $result = __('No topics found.'); 
  255. $run_once = 1; 
  256. return array( 1, sprintf( $statement, $result ) ); 
  257. $topic_ids = array(); 
  258. foreach ($topics as $topic) { 
  259. $topic_ids[] = $topic->topic_id; 
  260.  
  261. // Get all topic tag term relationships without a valid topic id 
  262. $in_topic_ids = implode(', ', $topic_ids); 
  263. $sql_bad_term_relationships = "SELECT 
  264. `$bbdb->term_taxonomy`.`term_taxonomy_id`,  
  265. `$bbdb->term_taxonomy`.`term_id`,  
  266. `$bbdb->term_relationships`.`object_id` 
  267. FROM `$bbdb->term_relationships` 
  268. JOIN `$bbdb->term_taxonomy` 
  269. ON `$bbdb->term_taxonomy`.`term_taxonomy_id` = `$bbdb->term_relationships`.`term_taxonomy_id` 
  270. WHERE 
  271. `$bbdb->term_taxonomy`.`taxonomy` = 'bb_topic_tag' AND 
  272. `$bbdb->term_relationships`.`object_id` NOT IN ($in_topic_ids) 
  273. ORDER BY 
  274. `$bbdb->term_relationships`.`object_id`,  
  275. `$bbdb->term_taxonomy`.`term_id`,  
  276. `$bbdb->term_taxonomy`.`term_taxonomy_id`;"; 
  277.  
  278. $bad_term_relationships = $bbdb->get_results( $sql_bad_term_relationships ); 
  279. if ( is_wp_error( $bad_term_relationships ) || !is_array( $bad_term_relationships ) ) { 
  280. $run_once = 2; 
  281. return array( 2, sprintf( $statement, $result ) ); 
  282.  
  283. // Delete those bad term relationships 
  284. if ( !empty( $bad_term_relationships ) ) { 
  285. $values = array(); 
  286. foreach ( $bad_term_relationships as $bad_term_relationship ) { 
  287. $values[] = '(`object_id` = ' . $bad_term_relationship->object_id . ' AND `term_taxonomy_id` = ' . $bad_term_relationship->term_taxonomy_id . ')'; 
  288. if ( !empty( $values ) ) { 
  289. $values = join(' OR ', $values); 
  290. $sql_bad_term_relationships_delete = "DELETE 
  291. FROM `$bbdb->term_relationships` 
  292. WHERE $values;"; 
  293. if ( is_wp_error( $bbdb->query( $sql_bad_term_relationships_delete ) ) ) { 
  294. $run_once = 3; 
  295. return array( 3, sprintf( $statement, $result ) ); 
  296.  
  297. // Now get all term taxonomy ids with term relationships 
  298. $sql_term_relationships = "SELECT `term_taxonomy_id` FROM $bbdb->term_relationships ORDER BY `term_taxonomy_id`;"; 
  299. $term_taxonomy_ids = $bbdb->get_col($sql_term_relationships); 
  300. if ( is_wp_error( $term_taxonomy_ids ) ) { 
  301. $run_once = 4; 
  302. return array( 4, sprintf( $statement, $result ) ); 
  303. $term_taxonomy_ids = array_unique( $term_taxonomy_ids ); 
  304.  
  305. // Delete topic tags that don't have any term relationships 
  306. if ( !empty( $term_taxonomy_ids ) ) { 
  307. $in_term_taxonomy_ids = implode(', ', $term_taxonomy_ids); 
  308. $sql_delete_term_relationships = "DELETE 
  309. FROM $bbdb->term_taxonomy 
  310. WHERE 
  311. `taxonomy` = 'bb_topic_tag' AND 
  312. `term_taxonomy_id` NOT IN ($in_term_taxonomy_ids);"; 
  313. if ( is_wp_error( $bbdb->query( $sql_delete_term_relationships ) ) ) { 
  314. $run_once = 5; 
  315. return array( 5, sprintf( $statement, $result ) ); 
  316.  
  317. // Get all valid term ids 
  318. $sql_terms = "SELECT `term_id` FROM $bbdb->term_taxonomy ORDER BY `term_id`;"; 
  319. $term_ids = $bbdb->get_col($sql_terms); 
  320. if ( is_wp_error( $term_ids ) ) { 
  321. $run_once = 6; 
  322. return array( 6, sprintf( $statement, $result ) ); 
  323. $term_ids = array_unique( $term_ids ); 
  324.  
  325. // Delete terms that don't have any associated term taxonomies 
  326. if ( !empty( $term_ids ) ) { 
  327. $in_term_ids = implode(', ', $term_ids); 
  328. $sql_delete_terms = "DELETE 
  329. FROM $bbdb->terms 
  330. WHERE 
  331. `term_id` NOT IN ($in_term_ids);"; 
  332. if ( is_wp_error( $bbdb->query( $sql_delete_terms ) ) ) { 
  333. $run_once = 7; 
  334. return array( 7, sprintf( $statement, $result ) ); 
  335.  
  336. $result = __( 'Complete!' ); 
  337. $run_once = 0; 
  338. return array( 0, sprintf( $statement, $result ) ); 
  339.  
  340. function bb_recount_clean_favorites() 
  341. global $bbdb; 
  342.  
  343. $statement = __( 'Removing deleted topics from user favorites… %s' ); 
  344. $result = __( 'Failed!' ); 
  345.  
  346. $meta_key = $bbdb->prefix . 'favorites'; 
  347.  
  348. $users = $bbdb->get_results( "SELECT `user_id`, `meta_value` AS `favorites` FROM `$bbdb->usermeta` WHERE `meta_key` = '$meta_key';" ); 
  349. if ( is_wp_error( $users ) ) { 
  350. return array( 1, sprintf( $statement, $result ) ); 
  351.  
  352. $topics = $bbdb->get_col( "SELECT `topic_id` FROM `$bbdb->topics` WHERE `topic_status` = '0';" ); 
  353.  
  354. if ( is_wp_error( $topics ) ) { 
  355. return array( 2, sprintf( $statement, $result ) ); 
  356.  
  357. $values = array(); 
  358. foreach ( $users as $user ) { 
  359. if ( empty( $user->favorites ) || !is_string( $user->favorites ) ) { 
  360. continue; 
  361. $favorites = explode( ', ', $user->favorites ); 
  362. if ( empty( $favorites ) || !is_array( $favorites ) ) { 
  363. continue; 
  364. $favorites = join( ', ', array_intersect( $topics, $favorites ) ); 
  365. $values[] = "('$user->user_id', '$meta_key', '$favorites')"; 
  366.  
  367. if ( !count( $values ) ) { 
  368. $result = __( 'Nothing to remove!' ); 
  369. return array( 0, sprintf( $statement, $result ) ); 
  370.  
  371. $sql_delete = "DELETE FROM `$bbdb->usermeta` WHERE `meta_key` = '$meta_key';"; 
  372. if ( is_wp_error( $bbdb->query( $sql_delete ) ) ) { 
  373. return array( 4, sprintf( $statement, $result ) ); 
  374.  
  375. $values = array_chunk( $values, 10000 ); 
  376. foreach ( $values as $chunk ) { 
  377. $chunk = "\n" . join( ", \n", $chunk ); 
  378. $sql_insert = "INSERT INTO `$bbdb->usermeta` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;"; 
  379. if ( is_wp_error( $bbdb->query( $sql_insert ) ) ) { 
  380. return array( 5, sprintf( $statement, $result ) ); 
  381.  
  382. $result = __( 'Complete!' ); 
  383. return array( 0, sprintf( $statement, $result ) ); 
.