Skip to:
Content

bbPress.org

Ticket #2731: 2731.first.diff

File 2731.first.diff, 5.7 KB (added by tharsheblows, 10 years ago)

cycles through the function, although incomplete

  • src/includes/admin/tools.php

     
    13721372        $statement = __( 'Recalculating reply menu order … %s', 'bbpress' );
    13731373        $result    = __( 'No reply positions to recalculate!',         'bbpress' );
    13741374
     1375
    13751376        // Delete cases where `_bbp_reply_to` was accidentally set to itself
    13761377        if ( is_wp_error( $wpdb->query( "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` = '_bbp_reply_to' AND `post_id` = `meta_value`;" ) ) ) {
    13771378                return array( 1, sprintf( $statement, $result ) );
     
    13791380
    13801381        // Post type
    13811382        $rpt = bbp_get_reply_post_type();
     1383        $pst = bbp_get_pending_status_id();
    13821384
    1383         // Get an array of reply id's to update the menu oder for each reply
    1384         $replies = $wpdb->get_results( "SELECT `a`.`ID` FROM `{$wpdb->posts}` AS `a`
     1385        // Set the menu_order of pending replies who have a post_date_gmt of 0 (posts that have been posted but never published)
     1386        $wpdb->update('wp_posts', array('menu_order'=>'0'), array('post_type'=>$rpt, 'post_status'=>$pst, 'post_date_gmt'=>'0000-00-00 00:00:00') );
     1387
     1388        // Post type
     1389        $offset = 0;
     1390        $limit = 500;
     1391        $done = false;
     1392        $replies_count = 0;
     1393       
     1394        $queryTimes = array();
     1395        $updateTimes = array();
     1396       
     1397        // ready steady GO
     1398        $startTime = microtime(true);
     1399       
     1400        // first look through the menu_order 0 replies. Are there any in topics which aren't spammed or pending? Only topics that are....
     1401        $replies_mozero = $wpdb->get_results( "SELECT `a`.`post_parent` FROM `{$wpdb->posts}` AS `a`
    13851402                                                                                INNER JOIN (
    13861403                                                                                        SELECT `menu_order`, `post_parent`
    1387                                                                                         FROM `{$wpdb->posts}`
     1404                                                                                        FROM `{$wpdb->posts}` WHERE `post_type` = '{$rpt}' AND `menu_order` = 0
    13881405                                                                                        GROUP BY `menu_order`, `post_parent`
    13891406                                                                                        HAVING COUNT( * ) >1
    13901407                                                                                )`b`
    13911408                                                                                ON `a`.`menu_order` = `b`.`menu_order`
    13921409                                                                                AND `a`.`post_parent` = `b`.`post_parent`
    1393                                                                                 WHERE `post_type` = '{$rpt}';", OBJECT_K );
     1410       
     1411                                                                         LIMIT {$limit}", OBJECT_K );
     1412       
     1413        while( !$done ){
     1414                // Get an array of reply id's to update the menu oder for each reply
     1415                $startQueryTime = microtime(true);
     1416                $replies = $wpdb->get_results( "SELECT `a`.`ID` FROM `{$wpdb->posts}` AS `a`
     1417                                                                                INNER JOIN (
     1418                                                                                        SELECT `menu_order`, `post_parent`
     1419                                                                                        FROM `{$wpdb->posts}` WHERE `post_type` = '{$rpt}' AND `menu_order` != 0
     1420                                                                                        GROUP BY `menu_order`, `post_parent`
     1421                                                                                        HAVING COUNT( * ) >1
     1422                                                                                )`b`
     1423                                                                                ON `a`.`menu_order` = `b`.`menu_order`
     1424                                                                                AND `a`.`post_parent` = `b`.`post_parent`
     1425       
     1426                                                                         LIMIT {$limit}", OBJECT_K );
    13941427
    1395         // Bail if no replies returned
    1396         if ( empty( $replies ) ) {
    1397                 return array( 1, sprintf( $statement, $result ) );
    1398         }
     1428                $endQueryTime = microtime(true);
     1429               
     1430                $queryTimes[] = ( $endQueryTime - $startQueryTime );
     1431               
     1432                if( $offset >= 2000 ){
     1433                        $done = true;
     1434                }
    13991435
    1400         // Recalculate the menu order position for each reply
    1401         foreach ( $replies as $reply ) {
    1402                 bbp_update_reply_position( $reply->ID );
     1436                $replies_count += count( $replies );
     1437                $offset += $limit;
     1438       
     1439                // Bail if no replies returned
     1440                if ( $replies_count == 0 ) {
     1441                        return array( 1, sprintf( $statement, $result ) );
     1442                }
     1443
     1444                // Recalculate the menu order position for each reply
     1445                foreach ( $replies as $reply ) {
     1446                        $startUpdateTime = microtime(true);
     1447                        bbp_update_reply_position( $reply->ID );
     1448                        $endUpdateTime = microtime(true);
     1449                        $updateTimes[] = ( $endUpdateTime - $startUpdateTime );
     1450                       
     1451                }
    14031452        }
    1404 
    14051453        // Cleanup
    14061454        unset( $replies, $reply );
    1407 
     1455        $midTime = microtime(true);
     1456       
     1457        $averageQueryTime = array_sum( $queryTimes ) / count( $queryTimes );
     1458               
     1459        $averageUpdateTime = array_sum( $updateTimes ) / count( $updateTimes );
     1460        $stdDevUpdateTime = stats_standard_deviation( $updateTimes );
     1461       
    14081462        // Flush the cache; things are about to get ugly.
    14091463        wp_cache_flush();
     1464        $endTime = microtime(true);
    14101465
    1411         return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
     1466        return array( 0, sprintf( $statement, __('Average update time: ' . $averageUpdateTime . "\n\nStandard deviation: " . $stdDevUpdateTime . "\n\nAverage query time: " . $averageQueryTime . "\n\nOffset: " . $offset . ' // Replies count: ' . $replies_count . ' // Mid time: ' . ($midTime - $startTime) . ' // End time: ' . ($endTime - $startTime), 'bbpress' ) ) );
    14121467}
    14131468
     1469/**
     1470 *  standard deviation thing from http://php.net/manual/en/function.stats-standard-deviation.php
     1471 */
     1472
     1473if (!function_exists('stats_standard_deviation')) {
     1474    /**
     1475     * This user-land implementation follows the implementation quite strictly;
     1476     * it does not attempt to improve the code or algorithm in any way. It will
     1477     * raise a warning if you have fewer than 2 values in your array, just like
     1478     * the extension does (although as an E_USER_WARNING, not E_WARNING).
     1479     *
     1480     * @param array $a
     1481     * @param bool $sample [optional] Defaults to false
     1482     * @return float|bool The standard deviation or false on error.
     1483     */
     1484    function stats_standard_deviation(array $a, $sample = false) {
     1485        $n = count($a);
     1486        if ($n === 0) {
     1487            trigger_error("The array has zero elements", E_USER_WARNING);
     1488            return false;
     1489        }
     1490        if ($sample && $n === 1) {
     1491            trigger_error("The array has only 1 element", E_USER_WARNING);
     1492            return false;
     1493        }
     1494        $mean = array_sum($a) / $n;
     1495        $carry = 0.0;
     1496        foreach ($a as $val) {
     1497            $d = ((double) $val) - $mean;
     1498            $carry += $d * $d;
     1499        };
     1500        if ($sample) {
     1501           --$n;
     1502        }
     1503        return sqrt($carry / $n);
     1504    }
     1505}
     1506
    14141507/** Reset ********************************************************************/
    14151508
    14161509/**