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